AnalyticDB PostgreSQL获取表信息

本文提供了一套全面的AnalyticDB PostgreSQL表信息查询方法,包括DDL语句获取、表及列基本信息查询、表中文名获取、表字段中文名及类型查询等,并介绍了如何处理死锁进程及锁定表的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查找AnalyticDB表的DDL语句

select dump_talbe_ddl('tablename'::regclass);

1.获取AnalyticDB表

select * from information_schema.tables;

2.获取AnalyticDB列

select * from information_schema.columns;

3.获取ADB PG表中文名
获取表中文名:方法一

select t.schemaname  
       ,t.relname   --表英文名
	   ,description --表中文名
from pg_description d
     ,pg_class c
	 ,pg_stat_all_tables t
where d.objoid = c.oid
  and objsubid = 0 
  and t.relname = c.relname
  and t.schemaname = 'SCHEMANAME'
  and c.relname = 'tablename'
;

获取表中文名:方法二

select relname                --表英文名
      ,obj_description(c.oid) --表中文名
from pg_class c
where obj_description(c.oid) is not null
and relname = 'tablename'
;

获取表中文名:方法三

select distinct a.relname --表英文名
       ,b.description     --表中文名
from pg_class a
left join pg_description b
on a.oid = b.objoid
and b.objsubid = '0'
where a.relname = 'tablename'
;

4.获取表中文名、字段中文名、字段类型

select 
       --t2.attnum,
       t2.relname   --表英文名
       ,obj_description(t2.oid)  --表中文名
       ,t2.field                 --字段英文名
       ,t2.comment               --字段中文名
       ,case when t2.type = 'varchar' then concat('varchar(',cast(t1.charachter_maximum_length as varchar),')')
             when t2.type = 'numeric' then concat('numeric(',numeric_precistion,',',numeric_scale,')')
             else t2.type
        end  as type     --字段类型
from information_schema.columns t1
left join(
select a.attnum
      ,c.oid
      ,c.relname
      ,a.attname as field
      ,t.typname as type
      ,a.attlen  as length
      ,a.atttypmod as lengthvar
      ,attnotnull  as notnull
      ,b.description as comment
from pg_class c,
     pg_attribute a
     left outer join pg_description b on a.attrelid = b.objoid and a.attnum =  b.objsubid,
     pg_type t
--where c.regclass = ('tablename')
where a.attnum >0
and a.attrelid = c.oid
and a.atttypid = t.oid
order by a.attnum
) t2
on t1.table_name = t2.relname
and t1.column_name = t2.field
where t1.table_schmea = 'SCHEMA'
and table_name = 'tablename'
order by relname,attnum
;

5.获取表中文名、字段中文名、字段类型、分布键

select
      b.relname                         as 表名
	  ,obj_description(b.oid)           as 表中文名
	  ,a.attname                        as 字段英文名称
	  ,col_description(a.attrelid,a.attnum)   as 字段中文名称
	  ,replace(format_type(a.atttypid,a.atttypmod),'character varying','varchar') as 字段类型
	  ,(case when atttypmod -4 >0 then atttypmod -4 else 0 end) as 字段长度
	  ,(case when (select count(1) from gp_constraint where conrelid = a.attrelid and conkey[1] = attnum and contype = 'p') > 0
	         then 'Y' else 'N' end) as 主键
	  ,(case when (select count(1) from gp_constraint where conrelid = a.attrelid and conkey[1] = attnum and contype = 'u') > 0
	         then 'Y' else 'N' end) as 唯一约束
	  ,(case when (select count(1) from gp_constraint where conrelid = a.attrelid and conkey[1] = attnum and contype = 'f') > 0
	         then 'Y' else 'N' end) as 外键约束
	  ,(case when a.attnotnull = true then 'N' else 'Y' end) as 是否允许NULL
	  ,case when c.attname is not null then 'Y' else 'N' end as 是否分布键
			 
from pg_attribute a
left join pg_class b on a.attrelid = b.pid
left join (select aaa.relname as 表名 ,ccc.attname, aaa.oid
           from ( select aa.oid
		                 ,obj_description(aa.oid) as table_comment
						 ,aa.relname
						 ,bb.localoid
						 ,bb.distkey   as attrnums,
						 ,regexp_split_to_table(array_to_string(bb.distkey,','),',')  as att
						 ,dd.nspname
				   from pg_class aa -- 原数据信息,最主要的表
				   left join gp_distribution_policy bb on bb.localoid = aa.oid  -- 分布键表
				   left join pg_namespace dd on dd.oid = aa.relnamespace  -- 模式
				   left join pg_inherits hh on aa.oid = hh.inhrelid  -- 继承表
				   where dd.nspname = 'schema_name'  -- 替换需要的模式
				     and hh.inhrelid is null
		    ) aaa
            left join pg_attribute ccc on ccc.attrelid = aaa.oid
			  and     cast(ccc.attnum as text) = aaa.att
			  where ccc.attnum >0
          ) c
on a.attrelid = c.oid
and a.attname = c.attname
left join information_schema.columns e on attname = e.column_name
and b.relname = e.table_name
where attstattarget = -1
and e.table_schmea = 'schema_name'
and b.relname = 'table_name'
order by b.rename
        ,e.ordinal_position
;

6.adb查询死锁进程

-- 1.查看死锁进程
select * from pg_stat_activity where waiting='t'
-- 2.解锁进程
select pg_terminate_backend(pid);

7.查看adb锁表并解锁表

-- 1.获取表的oid号
select oid,t.* from pg_class t where relname ='table_name';
-- 2.获取表pid号
select pid,t.* from pg_locks t where relation ='oid';
-- 3.如果有pid号则解锁
select pg_cancel_backend(pid);
### AnalyticDB for PostgreSQL 使用指南和官方文档 #### 一、产品概述 AnalyticDB for PostgreSQL 是一种基于MPP架构的数据仓库服务,专为处理大规模在线数据分析而设计,在现代数据驱动的世界中为企业提供高效分析和管理大量数据的能力[^1]。 #### 二、安装与设置 对于希望部署该系统的用户来说,可以从官方提供的安装向导开始操作。具体而言,AnalyticDB for PostgreSQL 支持多种环境下的自动化部署流程,简化了用户的初次接触体验;同时提供了详细的命令行参数说明以及图形化界面指导,方便不同层次的技术人员完成初始化工作[^3]。 #### 三、功能特性详解 作为一款强大的大数据分析工具,AnalyticDB for PostgreSQL 不仅继承了Greenplum数据库项目的优秀基因,还经过阿里云团队的精心优化。其主要特点包括但不限于:兼容ANSI SQL 2003标准语法,能够无缝对接PostgreSQL 和 Oracle 数据库生态;采用先进的行列混合存储技术,实现了对PB级别海量数据集的极速查询响应速度;具备出色的多租户管理和资源隔离能力,确保业务高峰期也能保持稳定性能现。 #### 四、连接器使用方法 针对想要进一步挖掘潜力或者与其他平台集成的需求方,可以通过研究《AlibabaCloud AnalyticDB PostgreSQL Connector 指南》来获取更多关于API接口调用方式的信息。这份资料不仅涵盖了基础概念解释,还包括实际案例分享,有助于加速开发进度并提高应用程序质量[^2]。 #### 五、高级应用实例——结合LangChain框架 除了上述常规用途外,《探索AnalyticDB for PostgreSQL:高效处理海量数据的利器》这篇文章特别提到了当这款产品同自然语言处理领域内的知名解决方案之一—LangChain相结合时所能带来的独特价值。通过这种方式不仅可以实现更加智能化的数据检索过程,而且还能有效提升整个系统的可扩展性和灵活性。 ```sql -- 这里给出一段简单的SQL语句示例,展示如何利用AnalyticDB for PostgreSQL执行复杂查询任务 SELECT * FROM sales_data WHERE sale_date BETWEEN '2023-01-01' AND '2023-12-31'; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值