Created
October 7, 2014 12:38
-
-
Save djheru/d128ab296a03ed9ea19b to your computer and use it in GitHub Desktop.
Example audit table and trigger function for Postgresql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE audit_log ( | |
username text, -- who did the change | |
event_time_utc timestamp, -- when the event was recorded | |
table_name text, -- contains schema-qualified table name | |
operation text, -- INSERT, UPDATE, DELETE or TRUNCATE | |
before_value json, -- the OLD tuple value | |
after_value json -- the NEW tuple value | |
); | |
CREATE OR REPLACE FUNCTION audit_trigger() | |
RETURNS trigger AS $$ | |
DECLARE | |
old_row json := NULL; | |
new_row json := NULL; | |
BEGIN | |
IF TG_OP IN ('UPDATE','DELETE') THEN | |
old_row = row_to_json(OLD); | |
END IF; | |
IF TG_OP IN ('INSERT','UPDATE') THEN | |
new_row = row_to_json(NEW); | |
END IF; | |
INSERT INTO audit_log( | |
username, | |
event_time_utc, | |
table_name, | |
operation, | |
before_value, | |
after_value | |
) VALUES ( | |
session_user, | |
current_timestamp AT TIME ZONE 'UTC', | |
TG_TABLE_SCHEMA || '.' || TG_TABLE_NAME, | |
TG_OP, | |
old_row, | |
new_row | |
); | |
RETURN NEW; | |
END; | |
$$ LANGUAGE plpgsql; | |
CREATE TRIGGER audit_log | |
AFTER INSERT OR UPDATE OR DELETE | |
ON notify_test | |
FOR EACH ROW | |
EXECUTE PROCEDURE audit_trigger(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment