呼唤远方
Ollyc的答案很好,因为它不是Postgres特有的,但是,当外键引用多个列时它会崩溃。以下查询适用于任意数量的列,但它在很大程度上依赖于Postgres扩展:select
att2.attname as "child_column",
cl.relname as "parent_table",
att.attname as "parent_column",
connamefrom
(select
unnest(con1.conkey) as "parent",
unnest(con1.confkey) as "child",
con1.confrelid,
con1.conrelid,
con1.conname from
pg_class cl join pg_namespace ns on cl.relnamespace = ns.oid join pg_constraint con1 on con1.conrelid = cl.oid where
cl.relname = 'child_table'
and ns.nspname = 'child_schema'
and con1.contype = 'f'
) con join pg_attribute att on
att.attrelid = con.confrelid and att.attnum = con.child join pg_class cl on
cl.oid = con.confrelid join pg_attribute att2 on
att2.attrelid = con.conrelid and att2.attnum = con.parent