X-Original-To: pgsql-hackers-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 57CC5D1B555 for ; Fri, 19 Sep 2003 21:11:20 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 63072-02 for ; Fri, 19 Sep 2003 18:11:11 -0300 (ADT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id F3BADD1B550 for ; Fri, 19 Sep 2003 18:11:08 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.10/8.12.10) with ESMTP id h8JLAviP008361; Fri, 19 Sep 2003 17:10:57 -0400 (EDT) To: Stephan Szabo Cc: Kris Jurka , Merlin Moncure , pgsql-hackers@postgresql.org, Rod Taylor , Gavin Sherry Subject: Re: observations about temporary tables and schemas In-reply-to: <20030917075737.P33923@megazone.bigpanda.com> References: <2054.1063808792@sss.pgh.pa.us> <20030917075737.P33923@megazone.bigpanda.com> Comments: In-reply-to Stephan Szabo message dated "Wed, 17 Sep 2003 08:08:00 -0700" Date: Fri, 19 Sep 2003 17:10:57 -0400 Message-ID: <8360.1064005857@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/1043 X-Sequence-Number: 44055 Stephan Szabo writes: > On Wed, 17 Sep 2003, Tom Lane wrote: >> I think we have two choices: disallow foreign-key references from temp >> tables to permanent tables, or take out the optimization of storing >> temp table pages in private memory. > I think the first is probably better all in all. Done. >> That is not the same bug; the problem here is that ON COMMIT DELETE ROWS >> simply does an unconditional heap_truncate without bothering to run any >> deletion triggers. We could make it apply the same checks TRUNCATE >> TABLE does, whereupon you'd get some sort of "can't truncate table" >> error when you try to set up a foreign key reference to it. That could >> be extended to disallowing the FK reference in the first place, perhaps. >> Or we could turn it into a "DELETE FROM temptable", which would be a lot >> slower but would "do the right thing". Comments? > Since it's documented as doing a truncate, I think disallowing non-self > referential FK constraints is probably a good idea. I'm not sure that > doing all the work on commit to make the table rows delete individually > for the trigger/foreign key case is really worth it. Okay, I have made it do this. It's sort of an indirect thing: when you try to set up the reference, the end-of-transaction ON COMMIT DELETE ROWS barfs: regression=# create temp table t1 (f1 int primary key) on commit delete rows; NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1" CREATE TABLE regression=# create temp table t2(f1 int references t1); NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY check(s) ERROR: cannot truncate a table referenced in a foreign key constraint DETAIL: Table "t2" references "t1" via foreign key constraint "$1". I'm not sure if it's worth trying to improve the error message by detecting the conflict more directly (ie, checking for ON COMMIT DELETE ROWS status when creating the FK in the first place). >> BTW, it occurs to me that TRUNCATE TABLE refuses to truncate relations >> referenced by foreign keys, but this is really not a correct/complete >> test. What about user-defined deletion triggers? Arguably it should >> refuse to truncate if there are any ON DELETE triggers at all. > Oracle doesn't seem to list those as being a problem (it explicitly lists > the foreign key constraint). It looks like they have a separate action > type for truncate so you can make before/after truncate triggers. Okay, I just documented the existing behavior. regards, tom lane