Re: Temp table exists test??

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Michael Guerin <guerin(at)rentec(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Temp table exists test??
Date: 2005-02-04 03:51:15
Message-ID: 20050204035114.GA63946@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Thu, Feb 03, 2005 at 09:39:08PM -0500, Michael Guerin wrote:

> I'm trying to detect the existence of a temp table in a function for
> that connection.

Why do you need to know this? So you don't try to create a temporary
table multiple times?

> Connection 1:
> Create table foo (i int);
>
> Connection 2:
> select * from pg_class where relname = 'foo'
>
> returns the table from connection 1, however I need to know if there's a
> temp table foo for connection 2 not connection 1. Each row in pg_class
> is associated with a namespace, so I'm looking for something like:
>
> select * from pg_class where relname = 'foo' and relnamespace = ???

If you don't care whether the table is temporary or not then you
could simply test if it's visible:

SELECT *
FROM pg_class
WHERE relname = 'foo'
AND relkind = 'r'
AND pg_table_is_visible(oid);

If you want to limit the query to temporary tables, then you could
join pg_class with pg_namespace and look for temporary schema names:

SELECT n.nspname, c.relname
FROM pg_class AS c
JOIN pg_namespace AS n ON n.oid = c.relnamespace
WHERE c.relname = 'foo'
AND c.relkind = 'r'
AND n.nspname LIKE 'pg_temp_%'
AND pg_table_is_visible(c.oid);

Is that what you're looking for?

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Guerin 2005-02-04 04:10:40 Re: Temp table exists test??
Previous Message Todd Lewis 2005-02-04 03:25:09 Re: Calling psql from a bat file on windows?