Skip to content

Instantly share code, notes, and snippets.

@ianthrive
Last active August 29, 2015 14:01
Show Gist options
  • Save ianthrive/591dbf3836b4d7b36c24 to your computer and use it in GitHub Desktop.
Save ianthrive/591dbf3836b4d7b36c24 to your computer and use it in GitHub Desktop.
An audit changes on a PostgreSQL table.
CREATE TABLE customers_audit AS (
id SERIAL NOT NULL UNIQUE PRIMARY KEY,
created TIMESTAMPTZ NOT NULL DEFAULT NOW(),
before HSTORE,
after HSTORE
);
CREATE FUNCTION trigger_customers_audit RETURNS TRIGGER LANUAGE plpgsql AS $$
INSERT INTO customers_audit(before, after)
SELECT hstore(OLD), hstore(NEW);
RETURN NEW;
END; $$;
CREATE TRIGGER customers_audit
AFTER UPDATE ON customers FOR EACH ROW
EXECUTE PROCEDURE trigger_customers_audit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment