peter.petrov
peter.petrov

Reputation: 39437

Triggers in Postgres fired unexpectedly

I have a parent table and several child tables with FKs pointing to the parent table. I deleted a record from the parent table and I noticed this fires delete triggers on all child tables, even though the parent record which I am deleting has no child records in these child tables.

I find this weird. But I come from SQL Server, so the behavior could be different here in Postgres.

My triggers are statement level triggers if that matters.

Could someone explain please, point me to some references?

I don't have any code with me right now to post... but I think the situation would be clear for some folks with more experience in Postgres.

Upvotes: 1

Views: 62

Answers (1)

Gregor Raýman
Gregor Raýman

Reputation: 3081

A statement level trigger is fired regardless of the number of affected records. So even though no child records exist, the automatic delete statement (assuming your FK is on delete cascade) is executed and so the trigger is fired.

Upvotes: 2

Related Questions