I have a table with 120 columns. I need to set up an audit trail that will record any column if it has been modified. As now, I assume that a trigger with the condition must be set for each column:
IF(NEW.columnName != OLD.columnName) THEN
This needs to be done 120 times ... Although I would take this approach 20 years ago, today I refuse to believe that it is impossible to automate such a simple procedure that automatically finds modified columns.
This is what I have discovered so far:
- Neither NEW nor OLD is a table, it is a kind of language construct, so you cannot do "SELECT NOW. *" Or something like that.
- Dynamic SQL is not allowed in triggers (this could solve the problem).
- Procedures using dynamic SQL are not allowed in triggers (seriously, Oracle, it looks like you worked very hard to disable this feature no matter what).
I thought of using BEFORE and AFTER triggers in combination with temporary tables and variables that could solve this problem, however, dynamic SQL would be needed again. I feel like I'm at a standstill.
Is there a solution for this?
Question: is this possible in PostgreSQL?
UPDATE : I found 2 potential solutions, however none of them seem clear enough to me:
- Using EVENTS as a workaround to use triggers in combination with dynamic SQL workaround . I have to admit that I did not quite understand this, does this mean that EVENT is triggered every second no matter what?
- This article says that you can use dynamic SQL inside a trigger while a temporary table is being used with it. It still uses dynamic SQL, so I donβt quite understand.
source share