GreenPlum添加Column信息,以及获取数据库schema表表注释(Column信息)字段字段注释(Column信息)字段类型是否为空等信息,以及转JSON输出

Posted Mr.zhou_Zxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GreenPlum添加Column信息,以及获取数据库schema表表注释(Column信息)字段字段注释(Column信息)字段类型是否为空等信息,以及转JSON输出相关的知识,希望对你有一定的参考价值。

GreenPlum添加Column信息,以及获取Column信息

一、创建表(添加Column信息)

create table ods.columndetail(
id bigint null,
name text null
);
comment on table ods.columndetail is '字段注释测试';
comment on column ods.columndetail.id is 'id';
comment on column ods.columndetail.name is '名称';

二、查看DDL记录(Column信息)

三、获取数据库、schema、表、表注释、字段、字段注释、字段类型、是否为空

通过指定schema、table_name获取表相关的信息

select t1.table_catalog , --数据库
t1.table_schema , --schema名
t1.table_name , --表名
t4.description as table_des, --表名注释
t1.column_name , --字段名
t3.description as column_des, -- 字段注释
t1.data_type , --字段类型
t1.is_nullable -- 是否为空
from information_schema.columns as t1 
inner join pg_catalog.pg_class as t2 
		on t1.table_name = t2.relname 
left join pg_catalog.pg_description as t3 
		on t2.OID = t3.objoid and t1.ordinal_position = t3.objsubid 
left join pg_catalog.pg_description as t4 
		on t2.OID = t4.objoid and t4.objsubid = 0
where table_schema = 'ods' and table_name = 'columndetail'

四、将以上信息转json输出

4.1 SQL

select ('"database1":"' || t1.table_catalog || '", "schema": "' || t1.table_schema 
||'", "table":"' || t1.table_name|| '", "table_des":"' || t4.description 
|| '","column":"' || t1.column_name || '","column_des":"' || t3.description 
|| '","type":"' || t1.data_type || '","isnull":"' || t1.is_nullable 
|| '"')::json
from information_schema.columns as t1 
inner join pg_catalog.pg_class as t2 on t1.table_name = t2.relname 
left join pg_catalog.pg_description as t3 on t2.OID = t3.objoid and t1.ordinal_position = t3.objsubid 
left join pg_catalog.pg_description as t4 on t2.OID = t4.objoid and t4.objsubid = 0
where table_schema = 'ods' and table_name = 'columndetail'

4.2 输出结果

"database1":"zxy", "schema": "ods", "table":"columndetail", "table_des":"字段注释测试","column":"id","column_des":"id","type":"bigint","isnull":"YES"
"database1":"zxy", "schema": "ods", "table":"columndetail", "table_des":"字段注释测试","column":"name","column_des":"名称","type":"text","isnull":"YES"

4.3 格式化JSON查看

以上是关于GreenPlum添加Column信息,以及获取数据库schema表表注释(Column信息)字段字段注释(Column信息)字段类型是否为空等信息,以及转JSON输出的主要内容,如果未能解决你的问题,请参考以下文章

如何获取 Greenplum 中用户最后登录时间和登录频率

Greenplum gpload - 最后一个错误:最后一个预期列之后的额外数据

如何查看greenplum函数何时、何人、何地在数据库中创建的信息?

greenplum 集群新增 standby节点

Greenplum 监控与运维

Greenplum - 在单个服务器上安装 Greenplum