Greenplum数据库通过sql查询表结构,拼装建表语句

Greenplum数据库通过sql查询表结构,拼装建表语句

在greenplum中pg_catalog是存储数据库基本元数据的表,information_schema 里包含了大量的视图,实现了类似mysql中 information_schema 比较易读的数据库元数据管理的功能。

greenplum 的pg_catalog 库包含的数据表基本都用oid关联,其中oid是全局id,最大42亿,可重置,也可循环使用。

1,查询表结构

select 
       attname, -- 字段名
       typname,-- 类型
       CASE WHEN pg_truetypmod = -1 /* default typmod */
       THEN null
       WHEN pg_truetypid IN (1042, 1043) /* char, varchar */
       THEN pg_truetypmod - 4
       WHEN pg_truetypid IN (1560, 1562) /* bit, varbit */
       THEN pg_truetypmod
       ELSE null end type_max_length, -- 获取变长类型最大长度
       is_null,  -- 是否空
       default_data, -- 默认值
       isunique,  -- 是否唯一索引
       isprimary, -- 是否主键
       is_index,  --是否索引
       distribution,  -- 是否分布键
       description  -- 注释
from 
( 
SELECT   
         t1.attname,
         t2.typname,
         case when t1.attnotnull=true then 'Y' else '' end is_null ,
         t3.description,
         t4.adbin as default_data, -- 默认值
         case when t5.attrnums is not null then 'Y' else null end distribution, -- 分布键
         t6.indisunique isunique,
         t6.indisprimary isprimary,
         case when t6.indkey is not null then 'Y' else null end is_index,
         t1.attnum, -- 字段位置顺序
         CASE WHEN t2.typtype = 'd' THEN t2.typbasetype ELSE t1.atttypid END pg_truetypid, 
         CASE WHEN t2.typtype = 'd' THEN t2.typtypmod ELSE t1.atttypmod END  pg_truetypmod 
     FROM
           pg_attribute t1 -- 属性
           left join pg_type t2  on t1.atttypid = t2.oid -- 类型
           left join "pg_catalog"."pg_description" t3 on  t1.attrelid=t3.objoid and t3.objsubid = t1.attnum -- 注释
           left join pg_attrdef  t4 on t4.adrelid = t1.attrelid AND t4.adnum = t1.attnum -- 默认值
           left join gp_distribution_policy  t5 on t5.localoid = t1.attrelid and t1.attnum = any(t5.attrnums) -- 分布键
           left join pg_index t6 on t6.indrelid=t1.attrelid and t1.attnum = any(t6.indkey)  -- 索引,主键等
     WHERE
           t1.attnum > 0
       AND t1.attisdropped <> 't'
       and t1.attrelid= 'table_schema.table_name'::regclass
) tt
order by attnum;

2,简洁版

SELECT   
         t1.attname,
         t2.typname,
         format_type (t1.atttypid, t1.atttypmod) AS TYPE,
         case when t1.attnotnull=true then 'is not null ' else null end is_null ,
         col_description (t1.attrelid, t1.attnum) AS comment,
         t4.adbin as default_attr, -- 默认值
         case when t5.attrnums is not null then 'Y' else null end distribution,
end is_index,
         t1.attnum -- 字段位置顺序
     FROM
           pg_attribute t1 -- 属性
           left join pg_type t2  on t1.atttypid = t2.oid -- 类型
           left join pg_attrdef  t4 on t4.adrelid = t1.attrelid AND t4.adnum = t1.attnum -- 默认值
           left join gp_distribution_policy  t5 on t5.localoid = t1.attrelid and t1.attnum = any(t5.attrnums)
           left join pg_index t6 on t6.indrelid=t1.attrelid and t1.attnum = any(t6.indkey)
     WHERE
           t1.attnum > 0
       AND t1.attisdropped <> 't'
       and t1.attrelid= 'resumes.base_common'::regclass
order by attnum;
posted @ 2019-12-26 15:30  整合侠  阅读(3886)  评论(0编辑  收藏  举报