Collecting changes using triggers

Collecting changes using triggers requires more work, but it also allows much more flexibility:

  • Write a trigger function to collect new (and if needed, old) values from tuples, and save them to auditing table(s)
  • Add suitable triggers to those tables for which changes need to be tracked

This can be achieved by using a handy universal audit trigger; it logs both old and new values of rows in any table, serialized as hstore data type values. The latest version of the trigger and its documentation are both available at https://github.com/2ndQuadrant/audit-trigger.

All of the audit trigger code is included in a single SQL file, which can be loaded as follows, using a database superuser account:

psql -f audit.sql ...

Get PostgreSQL Administration Cookbook, 9.5/9.6 Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.