Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 231E0475E29 for ; Mon, 29 Apr 2002 10:14:32 -0400 (EDT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id 0CFE1475B66 for ; Mon, 29 Apr 2002 10:12:46 -0400 (EDT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.4/8.11.4) with ESMTP id g3TECcF23412; Mon, 29 Apr 2002 10:12:38 -0400 (EDT) To: Joseph Barillari Cc: pgsql-sql Subject: Re: Temporary table weirdness In-reply-to: References: Comments: In-reply-to Joseph Barillari message dated "Mon, 29 Apr 2002 02:20:00 -0400" Date: Mon, 29 Apr 2002 10:12:37 -0400 Message-ID: <23409.1020089557@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200204/358 X-Sequence-Number: 7622 Joseph Barillari writes: > I have a function that creates and destroys temporary tables in the > course of its work, and have run into trouble if the function > is executed more than once during a database session. A small > proof-of-concept is attached below: > cal=3D> create or replace function frob() returns integer as 'begin create = > temporary table foo(bar INT); insert into foo (bar) values (1); drop table = > foo; return 1; end;' language 'plpgsql'; This should be in the FAQ :-(. Since plpgsql caches query plans, it will fall over the second time through this code, because the temp table is no longer the same table (same OID) as it was the first time --- but the cached plan for the INSERT still has the old OID. There's a TODO item for plpgsql to detect changes of schema that affect its cached plans, and drop the cache; but it's not exactly trivial to do. In the meantime, you need to use EXECUTE to defeat the plan caching for every plpgsql query that touches the temp table. Another answer is to arrange to create the temp table only once per session, but that's harder. regards, tom lane