Michael Guerin <guerin@rentec.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