Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 2F492B5DC04 for ; Tue, 25 Oct 2011 17:56:02 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 10557-07 for ; Tue, 25 Oct 2011 20:55:55 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id DA6A9B5DBE1 for ; Tue, 25 Oct 2011 17:55:55 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p9PKtqAZ028358; Tue, 25 Oct 2011 16:55:52 -0400 (EDT) To: "Roman Lytovchenko" cc: pgsql-bugs@postgresql.org Subject: Re: BUG #6268: multiple update with on cascade In-reply-to: <201110250858.p9P8wtvv085658@wwwmaster.postgresql.org> References: <201110250858.p9P8wtvv085658@wwwmaster.postgresql.org> Comments: In-reply-to "Roman Lytovchenko" message dated "Tue, 25 Oct 2011 08:58:55 +0000" Date: Tue, 25 Oct 2011 16:55:52 -0400 Message-ID: <28357.1319576152@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201110/242 X-Sequence-Number: 31512 "Roman Lytovchenko" writes: > Description: multiple update with on cascade Yeah, this is a bug all right, see my analysis at http://archives.postgresql.org/pgsql-hackers/2011-10/msg01361.php As a stopgap workaround, you could manually rename the triggers generated by the FK constraint so that they execute in the opposite order. Try a command like this to see the triggers on t2: regression=# select tgname, tgfoid::regproc from pg_trigger where tgrelid = 't2'::regclass order by 1; tgname | tgfoid ----------------------------+----------------------- RI_ConstraintTrigger_53586 | "RI_FKey_check_ins" RI_ConstraintTrigger_53587 | "RI_FKey_check_upd" RI_ConstraintTrigger_53588 | "RI_FKey_cascade_del" RI_ConstraintTrigger_53589 | "RI_FKey_cascade_upd" (4 rows) (The numbers at the ends of the names will almost certainly be different for you.) You need to adjust the names so that the RI_FKey_check_upd trigger sorts after the one that calls RI_FKey_cascade_upd. For example, on my copy of your test case, update pg_trigger set tgname = 'RI_ConstraintTrigger_x53587' where tgname = 'RI_ConstraintTrigger_53587' and tgrelid = 't2'::regclass; followed by starting a fresh database session fixes it. (You'll need to be superuser to mess with the system catalogs directly like that.) Keep in mind that until we fix this in the source code, any freshly-created self-referential FK constraint is vulnerable to the same problem; in particular the problem would come back if you did a dump and reload. regards, tom lane