select a.attname as 字段名称,format_type(a.atttypid,a.atttypmod) as 字段类型,
(case
when atttypmod-4>0 then atttypmod-4
else 0
end)字段长度,
(case
when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='p')>0 then 'Y'
else 'N'
end) as 主键,
(case
when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='u')>0 then 'Y'
else 'N'
end) as U,
(case
when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='f')>0 then 'Y'
else 'N'
end) as R,
(case
when a.attnotnull=true then 'N'
else 'Y'
end) as 不是null,
col_description(a.attrelid,a.attnum) as comment,'XEditText' as control
from pg_attribute a
where attstattarget=-1 and attrelid = (select oid from pg_class where relname ='user')
如user表执行结果如下:
参考原文: