1. 程式人生 > >【greenplum】greenplum 資料字典實踐--通過sql指令碼查詢表結構,拼裝建表語句

【greenplum】greenplum 資料字典實踐--通過sql指令碼查詢表結構,拼裝建表語句

作者lianghc

     在greenplum中pg_catalog是儲存資料庫基本元資料的表,information_schema 裡包含了大量的檢視,實現了類似mysql中 information_schema 比較易讀的資料庫元資料管理的功能。     greenplum 的pg_catalog 庫包含的資料表基本都用oid關聯,其中oid是全域性id,最大42億,可重置,也可迴圈使用,對oid有興趣的可以讀 周老師(德哥)的文章:PostgreSQL OID 原始碼分析 ,greenplum  pg_catalog 可以參考postgresql的文件。     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)。如果此型別不是域,那麼為-1atttypmod 記錄建立新表時支援特定型別的資料(比如一個 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