在greenplum中pg_catalog是存儲數據庫基本元數據的表,information_schema 裏包含了大量的視圖,實現了相似mysql中 information_schema 比較易讀的數據庫元數據管理的功能。mysql
greenplum 的pg_catalog 庫包含的數據表基本都用oid關聯,其中oid是全局id,最大42億,可重置,也可循環使用。sql
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,簡潔版spa
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;