给定一个表名,如何从plpgsql函数中提取主键列及其数据类型的列表?
上面的查询非常糟糕,因为它确实很慢。
我会推荐这个正式版本:
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