Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id A31CC2E01E5 for ; Fri, 1 Feb 2008 19:53:10 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 14779-09 for ; Fri, 1 Feb 2008 19:53:00 -0400 (AST) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id BE9D62E01D6 for ; Fri, 1 Feb 2008 19:53:06 -0400 (AST) 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 m11Nr0W3018000; Fri, 1 Feb 2008 18:53:01 -0500 (EST) To: "Peter Koczan" cc: "Alvaro Herrera" , pgsql-admin@postgresql.org Subject: Re: Legacy foreign keys In-reply-to: <4544e0330802011539j78ab6d2eofcf58e9442031115@mail.gmail.com> References: <4544e0330712101112r6aeccfcckef378a7ea1795e07@mail.gmail.com> <20071210195046.GE27126@alvh.no-ip.org> <20427.1197324492@sss.pgh.pa.us> <4544e0330712111901k1fa50c5dgb26287aff018e98c@mail.gmail.com> <10859.1197428770@sss.pgh.pa.us> <4544e0330712120940i413e3ad6y867fc2a8744f623b@mail.gmail.com> <4544e0330801311613t5cc6741cx1464947c2215f198@mail.gmail.com> <18279.1201845349@sss.pgh.pa.us> <4544e0330802011030l477b0d8ek57909e75ff367128@mail.gmail.com> <4544e0330802011539j78ab6d2eofcf58e9442031115@mail.gmail.com> Comments: In-reply-to "Peter Koczan" message dated "Fri, 01 Feb 2008 17:39:13 -0600" Date: Fri, 01 Feb 2008 18:53:00 -0500 Message-ID: <17999.1201909980@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/6 X-Sequence-Number: 28080 "Peter Koczan" writes: > I have a theory as to what's happening. It can find both tables The > new smarts in the 8.3 server seem to require a "complete" foreign key, > which, I'm guessing, would require additional triggers defining > RESTRICT or CASCADE on UPDATEs and DELETEs. Yeah, it's expecting to see a set of three related triggers. For instance, if I do this in a 7.0 database: play=> create table master(f1 int primary key); NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index 'master_pkey' for table 'master' CREATE play=> create table slave(ff int references master); NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY check(s) CREATE and then pg_dump it, I get these three triggers: -- -- Name: RI_ConstraintTrigger_4583989; Type: TRIGGER; Schema: public; Owner: tgl -- CREATE CONSTRAINT TRIGGER "" AFTER INSERT OR UPDATE ON slave FROM master NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE PROCEDURE "RI_FKey_check_ins"('', 'slave', 'master', 'UNSPECIFIED', 'ff', 'f1'); -- -- Name: RI_ConstraintTrigger_4583991; Type: TRIGGER; Schema: public; Owner: tgl -- CREATE CONSTRAINT TRIGGER "" AFTER DELETE ON master FROM slave NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE PROCEDURE "RI_FKey_noaction_del"('', 'slave', 'master', 'UNSPECIFIED', 'ff', 'f1'); -- -- Name: RI_ConstraintTrigger_4583993; Type: TRIGGER; Schema: public; Owner: tgl -- CREATE CONSTRAINT TRIGGER "" AFTER UPDATE ON master FROM slave NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE PROCEDURE "RI_FKey_noaction_upd"('', 'slave', 'master', 'UNSPECIFIED', 'ff', 'f1'); If your original database doesn't have all three triggers, then you had a problem already --- the FK constraint wasn't being enforced properly. > The other problem is that other legacy FK triggers on that same table > have the same value for the first field, "", which will cause > name conflicts. There's a hack to deal with that in the conversion code --- it'll assign a generated name instead. > One more thing. Is there any way to quickly find all the old-style > FKs? I tried looking in pg_trigger but it appears that even new, > legitimate foreign keys have triggers driving them. In 8.3, you could look for the pg_trigger entries with zero tgconstraint values. However, prior versions don't have that column; you'd have to look instead for entries that aren't linked to a pg_constraint entry by a pg_depend entry. regards, tom lane