Odd(?) RI-trigger behavior

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)postgreSQL(dot)org
Subject: Odd(?) RI-trigger behavior
Date: 2002-04-19 02:02:45
Message-ID: 19587.1019181765@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-patches

I was just fooling around with replacing the existing plain index on
pg_trigger.tgrelid with a unique index on (tgrelid, tgname). In theory
this should not affect anything --- the code already enforced that two
triggers on the same relation can't have the same name. The index
should merely provide a backup check.

So I was a tad surprised to get a regression test failure:

*** ./expected/foreign_key.out Thu Apr 11 15:13:36 2002
--- ./results/foreign_key.out Thu Apr 18 21:26:20 2002
***************
*** 899,905 ****
ERROR: <unnamed> referential integrity violation - key in pktable still referenced from pktable
-- fails (1,1) is being referenced (twice)
update pktable set base1=3 where base1=1;
! ERROR: <unnamed> referential integrity violation - key in pktable still referenced from pktable
-- this sequence of two deletes will work, since after the first there will be no (2,*) references
delete from pktable where base2=2;
delete from pktable where base1=2;
--- 899,905 ----
ERROR: <unnamed> referential integrity violation - key in pktable still referenced from pktable
-- fails (1,1) is being referenced (twice)
update pktable set base1=3 where base1=1;
! ERROR: <unnamed> referential integrity violation - key referenced from pktable not found in pktable
-- this sequence of two deletes will work, since after the first there will be no (2,*) references
delete from pktable where base2=2;
delete from pktable where base1=2;

======================================================================

This particular test involves a table with a foreign-key reference to
itself, ie, it's both PK and FK. What apparently is happening is that
the two RI triggers are now being fired in a different order than
before. While either of them would have detected an error, we now get
the other error first.

Does this bother anyone? It seems to me that the old code essentially
had no guarantee at all about the order in which the triggers would
fire, and so it was pure luck that the regression test never showed
the other message.

With the modified code, because we load the triggers by scanning
an index on (tgrelid, tgname), it is actually true that triggers are
fired in name order. We've had requests in the past to provide a
well-defined firing order for triggers --- should we document this
behavior and support it, or should we pretend it ain't there?

BTW, the same goes for rules: it would now be pretty easy to guarantee
that rules are fired in name order.

regards, tom lane

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Christopher Kings-Lynne 2002-04-19 02:07:12 Re: Schema (namespace) privilege details
Previous Message Rod Taylor 2002-04-19 02:02:23 Re: Schema (namespace) privilege details

Browse pgsql-patches by date

  From Date Subject
Next Message Stephan Szabo 2002-04-19 03:09:55 Re: Odd(?) RI-trigger behavior
Previous Message Nicolas Bazin 2002-04-18 22:56:36 Re: [HACKERS] build of 7.2.1 on SCO Openserver and Unixware