patternsqlMajor
Track all modifications to a PostgreSQL table
Viewed 0 times
postgresqltrackallmodificationstable
Problem
We have a table which has only roughly 500 rows, but it is very important for us.
I want see all changes which happen to this table. The changes should get tracked with a timestamp.
I don't want the tracking to happen in the application code, since I want to track changes which happen via
I am happy with a PostgreSQL specific solution, since I don't use a different DB in this context.
I want see all changes which happen to this table. The changes should get tracked with a timestamp.
I don't want the tracking to happen in the application code, since I want to track changes which happen via
psql shell, too.I am happy with a PostgreSQL specific solution, since I don't use a different DB in this context.
Solution
Answer compiled from comments
You would put triggers for any DML event on the table that write information about the event to a log table. If you want to track DDL too, have a look at event triggers - sticky bit.
Some example implementations given by a-horse-with-no-name:
Even if they are several years old, the basic idea hasn't changed and all of them are valid choices. The source for the fourth alternative has been updated recently: https://github.com/2ndQuadrant/audit-trigger
You would put triggers for any DML event on the table that write information about the event to a log table. If you want to track DDL too, have a look at event triggers - sticky bit.
Some example implementations given by a-horse-with-no-name:
- Tracking changes in PostgreSQL by Hans-Jürgen Schönig
- Impossibly Lean Audit System for Postgres with hstore
- Copying Pavel Stehule’s simple history table but with the jsonb type by Glyn Astill
- Audit trigger in the PostgreSQL Wiki
Even if they are several years old, the basic idea hasn't changed and all of them are valid choices. The source for the fourth alternative has been updated recently: https://github.com/2ndQuadrant/audit-trigger
Context
StackExchange Database Administrators Q#233735, answer score: 27
Revisions (0)
No revisions yet.