postgresql 监控 table、column 的备注

news/2024/7/8 6:43:19

最近在整理postgresql数据库的表、列的备注信息时,用到了如下的sql

表、视图、分区表的备注


with tmp_tab as (
    select pc.oid as ooid,pn.nspname,pc.*
      from pg_class pc
           left outer join pg_namespace pn
                        on pc.relnamespace = pn.oid
      where 1=1
       and pc.relkind in ('r','v','m','f','p')
       and pn.nspname not in ('pg_catalog','information_schema') -- select pn.oid, pn.* from pg_namespace pn where 1=1
       and pn.nspname not like 'pg_toast%'
       /*and pc.oid not in (
          select inhrelid
            from pg_inherits
       )*/
    order by pc.relname
),tmp_desc as (
   select pd.*
     from pg_description pd
    where 1=1
      and pd.objsubid = 0 --objsubid 对于一个表列上的一个注释,这里是列号(objoid和classoid指表本身)。对所有其他对象类型,此列为0。
      --and pd.objoid=168605
)
select t0.*,
       case when t0.relkind in ('r','p')
                 then 'comment on table '||t0.nspname||'.'||t0.relname||' is '''||coalesce(t0.description,'')||''';' 
            when t0.relkind='v' 
                 then 'comment on view '||t0.nspname||'.'||t0.relname||' is '''||coalesce(t0.description,'')||''';' 
        end as table_description
  from (
		select tab.nspname,
		       tab.relname,
		       tab.relkind,
		       de.description
		  from tmp_tab tab
		       left outer join tmp_desc de
		                    on tab.ooid = de.objoid 
		 where 1=1    
		) t0
 where 1=1
   --and t0.description is not null
order by t0.nspname,t0.relname   
;

列的备注


with tmp_tab as (
    select pc.oid as ooid,pn.nspname,pc.*
      from pg_class pc
           left outer join pg_namespace pn
                        on pc.relnamespace = pn.oid
      where 1=1
       and pc.relkind in ('r','v','m','f','p')
       and pn.nspname not in ('pg_catalog','information_schema') -- select pn.oid, pn.* from pg_namespace pn where 1=1
       and pn.nspname not like 'pg_toast%'
       /*
       and pc.oid not in (
          select inhrelid
            from pg_inherits
       )*/
    order by pc.relname
),tmp_col as (
   select pa.*
     from pg_attribute pa
    where 1=1
      --and pa.attrelid = 168605
      and pa.attisdropped = false
      and pa.attname not in (
      'tableoid',
      'cmax',
      'xmax',
      'cmin',
      'xmin',
      'ctid'
      )
),tmp_desc as (
   select pd.*
     from pg_description pd
    where 1=1
      and pd.objsubid <> 0 --objsubid 对于一个表列上的一个注释,这里是列号(objoid和classoid指表本身)。对所有其他对象类型,此列为0。
      --and pd.objoid=168605
)
select t0.*,
       'comment on COLUMN '||t0.nspname||'.'||t0.relname||'.'||t0.attname||' is '''||coalesce(t0.description,'')||''';' as column_description
  from (
		select tab.nspname,
		       tab.relname,
		       tc.attname,
		       tc.attnum,
		       de.description
		  from tmp_tab tab
		       left outer join tmp_col tc
		                    on tab.ooid = tc.attrelid
		       left outer join tmp_desc de
		                    on tc.attrelid = de.objoid and tc.attnum = de.objsubid
       ) t0
 where 1=1
   --and t0.description is not null
order by t0.nspname,t0.relname, t0.attnum
;

参考:
http://postgres.cn/docs/9.6/catalog-pg-class.html
http://postgres.cn/docs/9.6/catalog-pg-attribute.html
http://postgres.cn/docs/9.6/catalog-pg-description.html


http://www.niftyadmin.cn/n/2124304.html

相关文章

初识Oracle裸设备二十问

1&#xff0e;什么叫做裸设备&#xff1f; 裸设备&#xff0c;也叫裸分区&#xff08;原始分区&#xff09;&#xff0c;是一种没有经过格式化&#xff0c;不被Unix通过文件系统来读取的特殊字符设备。它由应用程序负责对它进行读写操作。不经过文件系统的缓冲。 2&#xff0…

postgresql 使用 analyze 收集统计信息

os: centos7.4 postgresql:9.6.8 查看os进程 $ ps -ef|grep -i post |grep -i stat postgres 10782 10770 0 May09 ? 00:02:42 postgres: stats collector process表的信息 mondb# select pg_size_pretty(pg_relation_size(t_gather_pgsql_space_table)), pg_size_…

postgresql 9.x 的base backup + restore + pitr(point in time recovery)

os: centos6.8 postgresql:9.1.22 base backup 最初使用 tar 花费4个小时&#xff0c;之后改为 pigz 只花费了30分钟。 # vi /backup_scripts/pg_basebackup.sh #!/bin/bashDATEdate %Y%m%d#删除14天前的物理备份 echo "############################################…

windows下的oracle迁移到linux上

最近学习了一下oracle数据库启动原理&#xff0c;于是&#xff0c;就把在Windows创建起来做测试的数据库。 移植到linux下使用,前几天把Linux移植到Windows成功&#xff0c;但Windows移植到Linux碰到问题会多&#xff0c; Windows下不区分大小写&#xff0c;但在Linux是区分的&…

postgresql 9.x 10.x 的 pg_dumpall 备份数据库

前一段时间使用pg_dump备份了单个数据库&#xff0c;今天试了下 pg_dumpall。 pg_dumpall -U postgres -h 127.0.0.1 -p 54320 -v -f /var/lib/pgsql/10/pgdumpall_20180529 中间会看到如下日志 pg_dumpall: dumping database "mondb"... pg_dumpall: running "…

mysql中limit用法详解

Mysql中limit的用法详解 在我们使用查询语句的时候&#xff0c;经常要返回前几条或者中间某几行数据&#xff0c;这个时候怎么办呢&#xff1f;不用担心&#xff0c;mysql已经为我们提供了这样一个功能&#xff0c;尽管语法逻辑很是怪异。这个功能就好比oracle里的rownum&#…

USE_PGXS 在 extension 编译中的作用

postgresql 在编译 extension时经常要使用 pgxs&#xff0c;如下&#xff1a; # source /var/lib/pgsql/.bash_profile # USE_PGXS1 make # USE_PGXS1 make install下面简单分析一下&#xff1a; 某个具体 contrib 的 Makefile文件 # PGXS build needs PostgreSQL 9.2 or …

mongodb 使用 profile + mtools 来分析慢查询

os&#xff1a;centos 7.4 mongdbo&#xff1a;3.2 mtools&#xff1a;1.4.1 mongodb 是一款优秀的nosql数据库&#xff0c;高灵活性和高性能。所以有必要监控mongodb的慢查询&#xff0c;以便进一步缩短响应时间。 mtools是一组用来解析和过滤MongoDB日志文件的脚本。 pr…