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 34F639DCD70 for ; Thu, 12 Jan 2006 07:49:30 -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 20234-07 for ; Thu, 12 Jan 2006 07:49:30 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from uproxy.gmail.com (uproxy.gmail.com [66.249.92.206]) by postgresql.org (Postfix) with ESMTP id 9530B9DCD73 for ; Thu, 12 Jan 2006 07:49:25 -0400 (AST) Received: by uproxy.gmail.com with SMTP id s2so110383uge for ; Thu, 12 Jan 2006 03:49:28 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=Uk1j8mswNx+tw0XZKtCCECYROr+1mxmYN9MwZX1LbE89adDQbGIWHtXjUxk7/trJOrW8QJxCH2dWBPC3uXD0Ob8Vk125IAgmKJ8KFelHu9hZrQ4MbPe97nMorrzNChD0nTI+PGOrBaawk0aLmhvJRghA0b1NkpwULVesmyzyWcw= Received: by 10.67.31.14 with SMTP id i14mr732591ugj; Thu, 12 Jan 2006 03:49:27 -0800 (PST) Received: by 10.66.222.12 with HTTP; Thu, 12 Jan 2006 03:49:27 -0800 (PST) Message-ID: <5d0f60990601120349x11df4f30yfcbfbb7e2adae227@mail.gmail.com> Date: Thu, 12 Jan 2006 12:49:27 +0100 From: Dirk Jagdmann To: Tom Lane Subject: Re: foreign keys with on delete cascade and triggers Cc: pgsql-sql@postgresql.org In-Reply-To: <331.1137034941@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <5d0f60990601111456m136aff2se77e354354a1c6aa@mail.gmail.com> <331.1137034941@sss.pgh.pa.us> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.12 required=5 tests=[AWL=0.120] X-Spam-Score: 0.12 X-Spam-Level: X-Archive-Number: 200601/50 X-Sequence-Number: 23839 Hello Tom, > If you want the whole transaction rolled back, raise an error instead > of returning NULL. You're right, that's working. But now I have a slightly different problem. I assume that the trigger which watches the cascaded deletions first deletes the row in the monitored table and then deletes any dependant foreign keys. Thus the "foreign key tree" is deleted in a top-down manner. This hinders any triggers on delete queries in cascaded tables to query the referenced table any longer, since the referenced row is already deleted. The following code shows what I mean: create table a ( i serial primary key, name text ); create table b ( f int references a on delete cascade ); create or replace function f() returns trigger as $$ DECLARE n text; BEGIN SELECT name INTO n from a where i=3DOLD.f; IF FOUND THEN RAISE NOTICE '% deleted me', n; END IF; RETURN OLD; END; $$ language plpgsql; create trigger b_del before delete on b for each row execute procedure f(); insert into a(name) values('Dirk'); insert into b select currval('a_i_seq'); insert into a(name) values('Tom'); insert into b select currval('a_i_seq'); delete from b where f=3D1; -- will raise the notice delete from a where i=3D2; -- wont raise anything If the "foreign key tree" would be deleted in a bottom-up (or depth-first) manner the second delete would be able to retrieve the row in table a. Now I'd like to know if the current order of deletions in PostgreSQL is intended in the top-down way or if that could be changed? -- ---> Dirk Jagdmann ----> http://cubic.org/~doj -----> http://llg.cubic.org