上面的查询是非常糟糕的,因为它真的很慢。
原文链接:https://www.f2er.com/postgresql/192985.html我会推荐这个官方版本:
http://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
如果需要模式,查询如下
SELECT pg_attribute.attname,format_type(pg_attribute.atttypid,pg_attribute.atttypmod) FROM pg_index,pg_class,pg_attribute,pg_namespace WHERE pg_class.oid = 'foo'::regclass AND indrelid = pg_class.oid AND nspname = 'public' AND pg_class.relnamespace = pg_namespace.oid AND pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum = any(pg_index.indkey) AND indisprimary