網上找了一圈也沒有一個支持多主鍵查詢的,自己參考資料寫了一個,可以查詢字段名,類型,長度,主鍵(支持多主鍵),備註等信息
select a.attnum,
a.attname columnName,
t.typname columnType,
coalesce(d.character_maximum_length, d.numeric_precision) columnLength,
d.numeric_scale numericScaleLength,
(a.attnotnull is false) isNullable,
d.column_default defaultValue,
col_description(a.attrelid, a.attnum) as columnComment,
case when length(b.attname) > 0 then 'PRI' end as columnKey
from pg_class c,
pg_attribute a,
pg_type t,
information_schema.columns d
left join (
select pg_attribute.attname
from pg_index,
pg_class,
pg_attribute
where pg_class.oid = '表名' :: regclass
and pg_index.indrelid = pg_class.oid
and pg_attribute.attrelid = pg_class.oid
and pg_attribute.attnum = any (pg_index.indkey)
) b on d.column_name = b.attname
where a.attrelid = c.oid
and a.atttypid = t.oid
and a.attnum > 0
and c.relname = d.table_name
and d.column_name = a.attname
and c.relname = '表名';