Re: How can I find a schema that a table belong to?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: How can I find a schema that a table belong to?
Date: 2011-01-19 18:19:17
Message-ID: 23710.1295461157@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thomas Kellerer <spam_eater(at)gmx(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Christian Ullrich 2011-01-19 18:20:09 Re: PostgreSQL 9.0.1 PITR can not copy WAL file
Previous Message Tom Lane 2011-01-19 18:10:31 Re: debug_print_plan logs table alias used in join, not table name itself