postgresql - 根据 table 名称确定架构

postgresql - determine schema from table name

我有多个模式,每个模式都包含一个 table 同名的 "t"。

我如何找出在 "select * from t" 中评估的架构?

示例:

create schema one;
create schema two;
create schema three;
create table one.t(i int);
create table two.t(i int);

set search_path to one,two;
select magic_function('t');  -- returns 'one'

set search_path to two,one;
select magic_function('t');  -- returns 'two'

set search_path to three,two,one;
select magic_function('t');  -- returns 'two'

在这种情况下 "magic_function" 是什么?

这里不需要 fn() - 只需 plain select:

t=# set search_path to one,two;
SET
t=# select relname, relnamespace::regnamespace from pg_class where oid = 't'::regclass;
 relname | relnamespace
---------+--------------
 t       | one
(1 row)

t=# set search_path to two,one;
SET
t=# select relname, relnamespace::regnamespace from pg_class where oid = 't'::regclass;
 relname | relnamespace
---------+--------------
 t       | two
(1 row)

当然可以根据需要包装