Do queries executed in a Postgres trigger procedure run in the same transaction?

postgresql, postgresql-9.1, sql, stored-procedures, triggers

Solution

Yes, everything in triggers is in the same transaction as the triggering event.

Not directly related to the question, but normally you want to put side-effects in the `AFTER` trigger, rather than the `BEFORE` trigger.

Problem

I have a `BEFORE DELETE` trigger which inserts rows into another table using `SPI_exec`. Do these `INSERT` queries run in the same transaction as the one in which the original delete is executing? Hence, will the delete and all inserts roll back or commit together? If not, how can I make that happen?

Original source