Get table changes details without using triggers in postgres
I am working on a live dashboard which records changes in database and shows details related to it on the dashboard. Currently to get these live changes I am using a combination of triggers
, functions
and pg_notify
to generate notifications from postgres when some tables have CRUD operations in them.
All of this is working okay for now, but based on some suggestions I read online many people do not recommend to use triggers if it is possible, as they are hard to maintain and don't sit best with rest of our code base. So for this reason I want to try updating my trigger based approach with something which fits with my code better and easier to maintain. But I don't know what I can use instead of triggers to get live changes from database.
Most triggers in my database are just used to send postgres notification back to my backend where I am handling the changes directly, so if I can get the changes from the database without using triggers I think I can make it work, but I can't figure out how to do so.
Also, if you think that trigger based approach is better for my case then please inform me the same with why.
You can use logical decoding to replicate data modifications. If you don't want to write your own logical decoding plugin, you can use existing solutions like wal2json or tools like Debezium that can use the built-in pgoutput
plugin.
Note that you will only get data modifications that way. Data definition statements (CREATE
, ALTER
and DROP
) are not captured.