Nikša Baldun
Nikša Baldun

Reputation: 1894

How to check if deletion is caused by CASCADE in PostgreSQL trigger

In PL/pgSQL trigger function, is there a way to know that the deletion was invoked by cascading delete action?
I have a number of checks in a trigger function to see if deletion is allowed, which I don't want to perform if the deletion is cascading from master table.

Upvotes: 4

Views: 945

Answers (1)

Erwin Brandstetter
Erwin Brandstetter

Reputation: 656872

I can't think of a built-in way to check that.
You could instead check for existence of the master row in the master table ...

IF EXISTS (
   SELECT 1
   FROM   master_table m
   WHERE  m.master_id = OLD.master_id) THEN

   -- run checks

END IF;

If it's a cascading delete the master row should be gone already.

Upvotes: 4

Related Questions