PG&Gauss 查询表的索引列信息

举报
张谱继 发表于 2024/04/28 20:20:10 2024/04/28
【摘要】 1、查看表的索引以及索引的列信息SELECT t.tablename, i.indexname, indisunique, indisprimary, indexdef, ai.attname, ai.attnum, ai.* FROM pg_tables t JOIN pg_namespace n ON t.schemaname = n.nspname LEFT JOIN pg...

1、查看表的索引以及索引的列信息

SELECT t.tablename, i.indexname, indisunique, indisprimary, indexdef, ai.attname, ai.attnum, ai.*
  FROM pg_tables t
  JOIN pg_namespace n
    ON t.schemaname = n.nspname
  LEFT JOIN pg_indexes i
    ON t.schemaname = i.schemaname
   AND t.tablename = i.tablename
  LEFT JOIN pg_class ci
    ON i.indexname = ci.relname
   AND n.oid = ci.relnamespace
  LEFT JOIN pg_index ii
    ON ci.oid = ii.indexrelid
  LEFT JOIN pg_attribute ai
    ON ci.oid = ai.attrelid
WHERE t.schemaname = 'xxx'
   AND t.tablename LIKE 'xxx'
ORDER BY t.tablename, i.indexname, ai.attname, ai.attnum;

2、查看表的索、对应列在一起

 SELECT t.tablename,
        i.indexname,
        indisunique,
        indisprimary,
        indexdef,
        substring(indexdef,
                  strpos(indexdef, '(') + 1,
                  strpos(indexdef, ')') - strpos(indexdef, '(') - 1) cols
   FROM pg_tables t
   JOIN pg_namespace n
     ON t.schemaname = n.nspname
   LEFT JOIN pg_indexes i
     ON t.schemaname = i.schemaname
    AND t.tablename = i.tablename
   LEFT JOIN pg_class ci
     ON i.indexname = ci.relname
    AND n.oid = ci.relnamespace
   LEFT JOIN pg_index ii
     ON ci.oid = ii.indexrelid
  WHERE t.schemaname = 'xxx'
    AND t.tablename LIKE 'xxx'
  ORDER BY t.tablename, i.indexname;

3、表注释查看

SELECT nspname, c.relname, d.description
  FROM pg_namespace s
  JOIN pg_class c
    ON s.oid = c.relnamespace
  LEFT JOIN pg_description d
    ON d.objoid = c.oid
   AND d.objsubid = '0'
 WHERE nspname IN ('schema')
   AND c.relkind = 'r'
 ORDER BY nspname, c.relname, d.description;

4、列注释查看

SELECT nspname, c.relname, d.description
  FROM pg_namespace s
  JOIN pg_class c
    ON s.oid = c.relnamespace
  LEFT JOIN pg_description d
    ON d.objoid = c.oid
   AND d.objsubid = '0'
 WHERE nspname IN ('schema')
   AND c.relkind = 'r'
 ORDER BY nspname, c.relname, d.description;
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。