X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id C26809DC959 for ; Wed, 11 Jan 2006 23:02:24 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 61188-02 for ; Wed, 11 Jan 2006 23:02:28 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id E63859DC8DE for ; Wed, 11 Jan 2006 23:02:19 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id k0C32LZr000332; Wed, 11 Jan 2006 22:02:22 -0500 (EST) To: Dirk Jagdmann cc: pgsql-sql@postgresql.org Subject: Re: foreign keys with on delete cascade and triggers In-reply-to: <5d0f60990601111456m136aff2se77e354354a1c6aa@mail.gmail.com> References: <5d0f60990601111456m136aff2se77e354354a1c6aa@mail.gmail.com> Comments: In-reply-to Dirk Jagdmann message dated "Wed, 11 Jan 2006 23:56:40 +0100" Date: Wed, 11 Jan 2006 22:02:21 -0500 Message-ID: <331.1137034941@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.084 required=5 tests=[AWL=0.084] X-Spam-Score: 0.084 X-Spam-Level: X-Archive-Number: 200601/48 X-Sequence-Number: 23837 Dirk Jagdmann writes: > The idea behind the sample commands below is, that the whole deletion > should be denied, because a trigger in a cascaded table blocked the > deletion. The trigger works as expected and prevents rows with a value > of "5" being deleted from table "b". However if the deletion was > triggered via the cascaded foreign key (trigger), the deletion in > table "a" is not rolled back, thus the row with "5" in "a" is lost. > This of course leaves the database in an inconsistant state, because > the foreign key in table "b" can no longer be referenced in "a". > Now I'd like to know if this is a bug in the current form of cascaded > deletions; or if this is desired behaviour and the oppression of > deletions via triggers is undefined behaviour in the cascaded case; or > if this issue just hasn't been addressed yet; or something completly > differnt. This is a bug in your trigger design. The database is doing what it's supposed to do, ie, trying to delete the dependent row. If you turn that into a no-op, it's your own fault if you don't like the resulting state. If you want the whole transaction rolled back, raise an error instead of returning NULL. (This has been discussed before ... see the archives.) regards, tom lane