Re: Temp table exists test??

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Guerin <guerin(at)rentec(dot)com>
Cc: Michael Fuhr <mike(at)fuhr(dot)org>, pgsql-novice(at)postgresql(dot)org
Subject: Re: Temp table exists test??
Date: 2005-02-04 03:18:09
Message-ID: 9763.1107487089@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Michael Guerin <guerin(at)rentec(dot)com> writes:
> I'm trying to detect the existence of a temp table in a function for
> that connection.

Perhaps something like

select * from pg_class where relname = 'foo' and pg_table_is_visible(oid);

However this will potentially trigger on non-temp tables, if you have
any of matching names that are in your search path. Dunno if that's a
problem for your application.

Another way is to look at current_schemas(), though you would need to
check whether the first entry is a temp schema or not (it isn't until
you've created at least one temp table):

regression=# select current_schemas(true);
current_schemas
---------------------
{pg_catalog,public}
(1 row)

regression=# create temp table foo(f1 int);
CREATE TABLE
regression=# select current_schemas(true);
current_schemas
-------------------------------
{pg_temp_1,pg_catalog,public}
(1 row)

regression=# select (current_schemas(true))[1];
current_schemas
-----------------
pg_temp_1
(1 row)

regression=#

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Todd Lewis 2005-02-04 03:25:09 Re: Calling psql from a bat file on windows?
Previous Message Mike G. 2005-02-04 02:52:28 Re: how to generate printed output of queries