PostgreSQLQuestion 08 of 11
Triggers — what are they and when should you use them?
Answer out loud or in your head first, then compare.
Answer
A trigger runs a defined function in response to a table event, e.g. INSERT, UPDATE or DELETE. It can run for each row or once per statement, before or after the change.
Example: after a record is updated, I write the old and new values to an audit table. The audit write is part of the same transaction.
To be precise: a trigger can be useful when a rule must apply regardless of which application writes the data.
Pitfall: hidden logic complicates debugging and can increase write cost. For simple integrity I first consider CHECK, UNIQUE and FOREIGN KEY. I don't assume a trigger can safely make an external HTTP request.
How did it go? Be honest — “Not yet” puts the question on your review list.