X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id C9480475ECE for ; Fri, 4 Apr 2003 06:16:16 -0500 (EST) Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by postgresql.org (Postfix) with SMTP id 9FEB3475EE2 for ; Fri, 4 Apr 2003 06:16:15 -0500 (EST) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Fri Apr 04 13:16:17 2003 +0200 Received: œby rodos.fzk.de with ESMTP (8.8.6 (PHNE_17135)/8.7.1) id NAA03586; Fri, 4 Apr 2003 13:16:17 +0200 (METDST) Message-ID: <3E8D6980.5060308@rodos.fzk.de> Date: Fri, 04 Apr 2003 13:16:16 +0200 From: Christoph Haller User-Agent: Mozilla/5.0 (X11; U; HP-UX 9000/780; en-US; rv:1.3) Gecko/20030321 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Cc: m.papper@fantastic.com Subject: Re: Finding if a temp table exists in the current connection Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, hits=-12.9 required=5.0 tests=BAYES_01,USER_AGENT_MOZILLA_UA autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/65 X-Sequence-Number: 12766 > > Is there a way (i.e., access theinternal pg_ tables) to find out if a > particular temp table already exists (in the current connection)? > > I have written some stored procedures that check if a table exists and if a > column exists in a table. This is used so I can perform upgrades of our > system (if table doesnt exist, call create table... sql). > > I would like to do a similar thing for temp tables. > > I have noticed that if another connection creates a temp table, it will > show up in the pg_* tables so that all ocnnections can see the table. > > Is there some standard SQL way to test? > I'm using 7.3.2 and it seems there is a way. I've found out by starting a psql session using the -E option. This shows you the SQL behind \dt which shows the current temp tables. It looks like they are created in name spaces called "pg_temp_, where N is simply a connection counter. SELECT n.nspname as "Schema",c.relname as "Name" FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace where n.nspname like 'pg_temp_%' AND pg_catalog.pg_table_is_visible(c.oid) ; If I do (within 1st connection) create temp table tgif (dummy int); and the select above returns Schema | Name -----------+------ pg_temp_1 | tgif (1 row) And within a 2nd connection create temp table tgif (dummy int); and the select above returns Schema | Name -----------+------ pg_temp_2 | tgif (1 row) Does this help? Regards, Christoph