【greenplum】greenplum 数据字典实践--通过sql脚本查询表结构,拼装建表语句

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/you_xian/article/details/78864940

作者lianghc

     在greenplum中pg_catalog是存储数据库基本元数据的表,information_schema 里包含了大量的视图,实现了类似mysql中 information_schema 比较易读的数据库元数据管理的功能。
     greenplum 的pg_catalog 库包含的数据表基本都用oid关联,其中oid是全局id,最大42亿,可重置,也可循环使用,对oid有兴趣的可以读 周老师(德哥)的文章: PostgreSQL OID 源码分析 ,greenplum  pg_catalog 可以参考postgresql的文档。
     greenplum 的 information_schema 详情可参考: Chapter 34. The Information Schema
     information_schema.column 视图实现了表列属性查询的数据,以下是在不熟悉  information_schema.column 的时候自己写的。涉及到了6张元数据表,可以根据需要拼装成建表语句,例如将greemplum转换成mysql建表语句。
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;

备注:
typtypmod 记录作用到基础类型上的 typmod(如基础类型不使用 typmod 则为-1)。如果此类型不是域,那么为-1
atttypmod 记录创建新表时支持特定类型的数据(比如一个 varchar 字段的最大长度)。它传递给类型相关的输入和长度转换函数当做第三个参数。其值对 那些不需要 atttypmod 的类型通常为 -1

补充一些简洁版本脚本:
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,
         --t6.indisunique isunique,
         --t6.indisprimary isprimary,
         --case when t6.indkey is not null then 'is index' else null 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


猜你喜欢

转载自blog.csdn.net/you_xian/article/details/78864940
今日推荐