The Ultimate Guide to PostgreSQL Data Change Tracking
exaspark.medium.com
exaspark.medium.com
I use both the trigger + audit table approaches in my sass (for user-facing activity feeds) and subscribe to CDC WAL changes for dealing with callback-like logic (i.e., send registration email, clear cache).
I'm not a fan of the pg_notify approach due to requirement of adding triggers (performance penalty) and the 8k character limit per column (you will lose data as it will splice off anything larger than that).
Application-level tracking or callbacks makes the stack dependent on the application for data integrity, I'd rather the database be the source of truth on all things data. Especially in the age of microservices.
For something turn-key, Bemi looks like a really good option - especially if you need persistence of the changes!
For something very light wight - check out WalEx (I'm the maintainer):
I’ve done some of this with an event sourcing system, but not the query rewriting. All my reads use the “latest view”, but the app uses functions to write to the underlying scheme rather than doing “simple” DML.
I wondered if keeping the storage as append only under the hood would keep the app layer simple.
In general for most apps you'd rather have 2 writes (write to table and write to audit table) than 1 write to the event log and hard to scale reads.
In the app (Django or whatever) after inserting news item, call a service (async) that matches users, sends alerts
I’d create a message queue table that would be inserted with jobs according to changes in other tables. Makes it easier to handle the message queue asynchronously.
Doing it in application side in a reliable way needs some kind of a distributed transaction mechanism which might not be feasible. I don’t want to fire an insert and then fail to send an alert and find myself within an if statement thinking “what the fuck do I do now?”
You could insert the job on the application code too. I just like triggers and abuse them which might not be a good idea in all cases.
Depends on the requirements though.
The business logic is clear to read, easy to change.
Ive enjoyed writing complex SQL, but when it comes to app development I want my engineers to just write python. Triggers are mostly invisible in the codebase and I would end up having to do all the maintenance myself.
There is of course more than one way to skin a cat, but I want our codebase to have just one single way.
On top of that, triggers are hard to test.
CREATE PUBLICATION news_item FOR TABLE news WHERE (topic IS "AAPL");
you'd add a hash column to the table you want to track. this would be used in a data warehouse where you want to track what has changed after a truncate and load. you'd keep additional tables on the side to track added/and delete hashes for a delta copy to downstream application tables.
It's the "brute force" type approach. It doesn't scale, has performance penalties, but it's incredibly easy to use and understand, and time walking is trivial.
Relies heavily on DISTINCT ON.
On truly old tables I've added "maintenance tasks" that push old rows into an archive/audit table.
Edit/note - I realise if you suggest this at any of the big players, you'll probably be fired on the spot.
- https://aiven.io/blog/two-dimensional-time-with-bitemporal-d...
- https://github.com/scalegenius/pg_bitemporal
4 timestamps and some ugly queries.