TD有一些常用的字典视图,可能会在迁移TD的过程中脚本使用到,而且客户会更习惯使用TD的字典表,在过渡过程中建一个TD的视图字典表能够让客户更加容易上手GaussDB(DWS).附件有代码,可以直接拿去部署,但需要建一个dbc schema,同时赋予到public的角色。
TD主要的字典视图有:
字典名称
描述
GaussDB(DWS)替换视图
DBC.TABLESV
表、视图、函数等对象信息
pg_tables pg_views
DBC.COLUMNSV
表的字段信息
pg_attribute
DBC.TABLESIZEV
表的空间大小信息
pg_tables
DBC.STATSV
表的统计信息内容
pg_statistic
实现方式:
主要使用现有的DWS字典,查看TD字典所需的内容信息怎么样获取,然后再通过多表关联获取全部信息。类似DBC.TABELESV里面有很多是TD自己的信息,而且那些信息在GaussDB(DWS)无法获取,因此在视图中直接不显示。同时增加了DWS常用的字段,例如oid,分布列,分布方式等。
实现代码示例如下:
drop view dbc.tablesv;
create or replace view dbc.tablesv
as
select pn.nspname as databasename
,pc.relname as tablename
,case pc.relkind when 'r' then 't' when 'f' then 'ft' else pc.relkind end as tablekind
,case when (relkind = 'r' and getdistributekey(pc.oid) is not null) or relkind = 'f' then pg_get_tabledef(pc.oid) when relkind = 'v' then pg_get_viewdef(pc.oid) else '' end as RequestText
,pg_get_userbyid(pc.relowner) as CreatorName
,description as CommentString
,po.ctime as CreateTimestamp
,po.mtime as LastAlterTimeStamp
,case when pc.relkind = 'r' then array_length(string_to_array(getdistributekey(pc.oid),','),1) else 0 end as PIColumnCount
,ppt.partcnt as PartitioningLevels
,pn.oid as SchemaOid
,pc.oid as TableOid
,case when pc.relkind = 'r' then getdistributekey(pc.oid) else null end as DistributeKey
,replace(reloptions[1],'orientation=','') orientation
,replace(reloptions[array_upper(reloptions,1)],'compression=','') as compression
from pg_class pc
inner join pg_namespace pn
on pc.relnamespace = pn.oid
and pc.relkind in ('r','f','v')
and pc.oid > 16384
and pn.oid not in (100)
left join pg_description pd
on pc.oid = pd.objoid
and objsubid = 0
left join pg_object po
on pc.oid = po.object_oid
left join (select parentid,count(*) partcnt from pg_partition where parttype = 'p' group by 1) ppt
on pc.oid = ppt.parentid
;
查询结果如下:
其他的代码此处不一一展示,请见附件查看。