Re: How can I find a schema that a table belong to? - Mailing list pgsql-general

From Tom Lane
Subject Re: How can I find a schema that a table belong to?
Date
Msg-id 23710.1295461157@sss.pgh.pa.us
Whole thread Raw
In response to Re: How can I find a schema that a table belong to?  (Thomas Kellerer <spam_eater@gmx.net>)
Responses Re: How can I find a schema that a table belong to?
Re: How can I find a schema that a table belong to?
List pgsql-general
Thomas Kellerer <spam_eater@gmx.net> writes:
> Jerry LeVan, 19.01.2011 17:35:
>> So I guess the question is:
>> Given a bare table name, how can I recover the schema
>> qualified name with whatever the current search path happens
>> to be?

> SELECT table_schema
> FROM information_schema.tables
> WHERE table_name = 'your_table'
> ;

That's not going to work, at least not in the interesting case where you
have more than one candidate table --- that SELECT will list all of 'em.

In most cases the answer to this type of problem is "use regclass",
but regclass doesn't quite solve Jerry's problem because it won't
schema-qualify the name if the table is visible in the search path.
The best solution I can think of is

select nspname from pg_namespace n join pg_class c on n.oid = c.relnamespace
  where c.oid = 'my_table_name'::regclass;

which works but seems a bit brute-force.

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: debug_print_plan logs table alias used in join, not table name itself
Next
From: Christian Ullrich
Date:
Subject: Re: PostgreSQL 9.0.1 PITR can not copy WAL file