I just read in the PostgreSQL Documentation - Overview of Trigger behavior, that BEFORE triggers are "more effecient" than AFTER triggers:
If you have no specific reason to make a trigger before or after, the before case is more efficient, since the information about the operation doesn't have to be saved until end of statement.
I do not understand if this is true or what it means for me. Can someone enlighten me? Is this just a homeopatic performance improvement?
A statement trigger is fired once on behalf of the triggering statement, regardless of the number of rows in the table that the triggering statement affects, even if no rows are affected. For example, if a DELETE statement deletes several rows from a table, a statement-level DELETE trigger is fired only once.
All PostgreSQL triggers execute in the same transaction as the transaction that has triggered them. Edit: You can also use LISTEN + NOTIFY to send a message from your trigger to a code that executes outside of the transaction. In that case, the message will only be delivered at the point of a successful commit.
A trigger is fired for any row affected once. A rule manipulates the parsetree or generates an additional one. So if many rows are affected in one statement, a rule issuing one extra query would usually do a better job than a trigger that is called for any single row and must execute his operations this many times.
1 Answer. Show activity on this post. The trigger is always executed in the same transaction as the statement, so it must needs be synchronous.
Due to PostgreSQL
's MVCC
architecture, each operation increases the amount of data recorded in the system, even DELETE
.
So if you just need to make checks of your input and rollback the transaction if the checks fail, you better do it before the input data are saved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With