Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - Are BEFORE triggers more efficient then AFTER triggers?

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?

like image 628
Daniel Avatar asked Mar 28 '11 11:03

Daniel


People also ask

Which type of trigger should run once regardless of number of rows affected?

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.

Are Postgres triggers transactional?

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.

What is true for trigger and rules in PostgreSQL?

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.

Are Postgres triggers synchronous?

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.


1 Answers

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.

like image 128
Quassnoi Avatar answered Sep 20 '22 12:09

Quassnoi