BUG #4992: "BEFORE DELETE" trigger puts the database in an inconsistent state

Lists: pgsql-bugs
From: "Stratsimir Kolchevski" <stratsimir(at)bastun(dot)net>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #4992: "BEFORE DELETE" trigger puts the database in an inconsistent state
Date: 2009-08-18 11:51:21
Message-ID: 200908181151.n7IBpLkK007028@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs


The following bug has been logged online:

Bug reference: 4992
Logged by: Stratsimir Kolchevski
Email address: stratsimir(at)bastun(dot)net
PostgreSQL version: 8.4.0
Operating system: Linux
Description: "BEFORE DELETE" trigger puts the database in an
inconsistent state
Details:

http://www.postgresql.org/docs/8.4/static/plpgsql-trigger.html:
"Row-level triggers fired BEFORE can return null to signal the trigger
manager to skip the rest of the operation for this row (i.e., subsequent
triggers are not fired, and the INSERT/UPDATE/DELETE does not occur for this
row)"

In the following simplified example table "b" has a record that does not
exist in "a", although we have a FOREIGN KEY on the "i" column.

postgres=# CREATE TABLE a(i INTEGER PRIMARY KEY);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "a_pkey" for
table "a"
CREATE TABLE
postgres=# CREATE TABLE b(i INTEGER REFERENCES a(i) ON DELETE CASCADE);
CREATE TABLE
postgres=# CREATE OR REPLACE FUNCTION test_func() RETURNS TRIGGER AS $$
postgres$# BEGIN
postgres$# RETURN NULL;
postgres$# END
postgres$# $$ LANGUAGE 'plpgsql';
CREATE FUNCTION
postgres=# CREATE TRIGGER test_trig BEFORE DELETE ON b FOR EACH ROW EXECUTE
PROCEDURE test_func();
CREATE TRIGGER
postgres=# INSERT INTO a(i) VALUES(1);
INSERT 0 1
postgres=# INSERT INTO b(i) VALUES(1);
INSERT 0 1
postgres=# DELETE FROM a;
DELETE 1

And the result is:

postgres=# SELECT * FROM b;
i
---
1
(1 row)

postgres=# SELECT * FROM a;
i
---
(0 rows)


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Stratsimir Kolchevski" <stratsimir(at)bastun(dot)net>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #4992: "BEFORE DELETE" trigger puts the database in an inconsistent state
Date: 2009-08-18 14:03:34
Message-ID: 7060.1250604214@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

"Stratsimir Kolchevski" <stratsimir(at)bastun(dot)net> writes:
> [ BEFORE DELETE triggers can suppress foreign key updates too ]

This is not a bug, it's operating as designed. If we didn't do that
it would foreclose many useful applications of triggers.

regards, tom lane