postgresqlplpgsqlcascading-deletespostgresql-9.3

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.


Solution

  • 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 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.