Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 0D0E8B5DBC0 for ; Fri, 30 Sep 2011 14:35:19 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 86069-05 for ; Fri, 30 Sep 2011 17:35:11 +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 B8540B5DBBD for ; Fri, 30 Sep 2011 14:35:10 -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 p8UHZ96V009653; Fri, 30 Sep 2011 13:35:10 -0400 (EDT) To: "Evan Martin" cc: pgsql-bugs@postgresql.org Subject: Re: BUG #6235: Delete fails with ON DELETE rule on inherited table In-reply-to: <201109300633.p8U6XCae089295@wwwmaster.postgresql.org> References: <201109300633.p8U6XCae089295@wwwmaster.postgresql.org> Comments: In-reply-to "Evan Martin" message dated "Fri, 30 Sep 2011 06:33:12 +0000" Date: Fri, 30 Sep 2011 13:35:08 -0400 Message-ID: <9652.1317404108@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: 201109/251 X-Sequence-Number: 31268 "Evan Martin" writes: > Defined a RULE that deletes from a child table whenever a parent table row > is deleted. If the parent (referencing) table INHERITS from another table > this rule doesn't work as intended, whether you delete from the base or > derived table. > If you delete from the base table then the DELETE succeeds (the row is > deleted), but the referenced row is not deleted. This might make sense to > someone who knows how inheritance is implemented, but it wasn't immediately > obvious to me. It would be nice if this worked, but if it doesn't, I think > the documentation should warn users about this trap. The reason this doesn't happpen is that inheritance expansion is done after rule expansion. While you could argue that rules on child tables should be considered too, it's not very clear to me that that would be well defined, and it's unlikely we'd make such a non-backwards-compatible change in rule behavior anyway. Personally I'd suggest using a trigger not a rule here, as it's not going to be subject to this problem and is generally the preferred way anyhow. > The more serious problem is that if you try to delete from the derived table > the delete fails with an error: > ERROR: update or delete on table "referenced" violates foreign key > constraint "fk_derived_referenced" on table "derived" > DETAIL: Key (id)=(2) is still referenced from table "derived". The reason for that is that the DO ALSO action occurs before the main action, so you're trying to delete a "referenced" row that is in fact still referenced. One solution would be to declare the foreign key constraint as DEFERRABLE INITIALLY DEFERRED. (The same would be the case for a trigger, unless you made it an AFTER trigger.) regards, tom lane