pg_tables and temp tables

Lists: pgsql-general
From: "Gauthier, Dave" <dave(dot)gauthier(at)intel(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: pg_tables and temp tables
Date: 2007-11-12 03:23:02
Message-ID: D7FF158337303A419CF4A183F48302D603525AAC@hdsmsx411.amr.corp.intel.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi:

How user specific is pg_tables when it comes to temporary tables? It
seems to pick up the existence of a a temp table created by the same
user but a different session. Here's the demo scenario....

Session 1:

mydb=# create temporary table foo (a int);

Session 2:

stdb2=# select tablename from pg_tables where tablename = 'foo';

tablename

-----------

foo

(1 row)

Session 1:

stdb=# drop table foo;

Session 2:

stdb2=# select tablename from pg_tables where tablename = 'foo';

tablename

-----------

(0 rows)

mydb=# insert into foo (a) values (3);

ERROR: relation "foo" does not exist

So Session 2 is detecting foo, but it doesn't own it.

I can live with this as long as there's a way that I can determine if
the table it found does not belong to the current session. Is there a
more elegant way of doing this other than trying to do something with
the table (select, insert, delete...) and trapping a failed attempt? The
contents of pg_tables appears to be the same from both the owning and
non-owning sessions.

Thanks in Advance for any help.

v8.2.0 on 64-bit suse-linux

-dave


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Gauthier, Dave" <dave(dot)gauthier(at)intel(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: pg_tables and temp tables
Date: 2007-11-12 16:05:34
Message-ID: 6282.1194883534@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

"Gauthier, Dave" <dave(dot)gauthier(at)intel(dot)com> writes:
> How user specific is pg_tables when it comes to temporary tables?

regression=# \d pg_tables
...
View definition:
SELECT n.nspname AS schemaname, c.relname AS tablename, pg_get_userbyid(c.relowner) AS tableowner, t.spcname AS tablespace, c.relhasindex AS hasindexes, c.relhasrules AS hasrules, c.reltriggers > 0 AS hastriggers
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
WHERE c.relkind = 'r'::"char";

Not at all, apparently. This looks to me like it will pick up every
table in the database, temporary or otherwise. Perhaps that's not a
good idea ...

> I can live with this as long as there's a way that I can determine if
> the table it found does not belong to the current session.

You need to pay attention to the schema it was found in.
"has_schema_privilege(schemaname, 'USAGE')" might be a suitable filter.

regards, tom lane