SELECT fk.conrelid::regclass AS referencing_table,
a2.attname AS referencing_column
FROM pg_constraint AS fk
JOIN pg_constraint AS uk ON fk.conindid = uk.conindid
CROSS JOIN LATERAL unnest(uk.conkey) WITH ORDINALITY AS ukc(attnum, n)
JOIN LATERAL unnest(fk.conkey) WITH ORDINALITY AS fkc(attnum, n) USING (n)
JOIN pg_attribute AS a1 ON ukc.attnum = a1.attnum AND a1.attrelid = uk.conrelid
JOIN pg_attribute AS a2 ON fkc.attnum = a2.attnum AND a2.attrelid = fk.conrelid
WHERE fk.contype = 'f'
AND uk.contype IN ('p'::"char", 'u'::"char")
AND fk.conislocal
AND uk.conrelid = 'data'::regclass
AND a1.attname = 'id';
此查询显示引用 table 中列
id
的所有表和列名称data
:在这些表中查询引用特定行的条目作为练习留给读者。您将无法在单个查询中完成所有操作。