列出PostgreSQL数据库的所有索引名称,列名称及其表名

前端之家收集整理的这篇文章主要介绍了列出PostgreSQL数据库的所有索引名称,列名称及其表名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
什么是查询获取列表的所有索引名称,其列名称和其表名称的postgresql数据库

我试图通过使用这个查询,但如何获取索引列表,其列名称及其表名称数据库中的所有索引的列表?

SELECT *
 FROM pg_class,pg_index
 WHERE pg_class.oid = pg_index.indexrelid
 AND pg_class.oid IN (
     SELECT indexrelid
     FROM pg_index,pg_class
     WHERE pg_class.oid=pg_index.indrelid
     AND indisunique != 't'
     AND indisprimary != 't'
     AND relname !~ '^pg_');`
这将输出所有包含详细信息的索引(从我的视图定义中提取):
SELECT i.relname as indname,i.relowner as indowner,idx.indrelid::regclass,am.amname as indam,idx.indkey,ARRAY(
       SELECT pg_get_indexdef(idx.indexrelid,k + 1,true)
       FROM generate_subscripts(idx.indkey,1) as k
       ORDER BY k
       ) as indkey_names,idx.indexprs IS NOT NULL as indexprs,idx.indpred IS NOT NULL as indpred
FROM   pg_index as idx
JOIN   pg_class as i
ON     i.oid = idx.indexrelid
JOIN   pg_am as am
ON     i.relam = am.oid;

可以向结尾添加一个额外的连接,以修剪命名空间:

SELECT i.relname as indname,idx.indpred IS NOT NULL as indpred
FROM   pg_index as idx
JOIN   pg_class as i
ON     i.oid = idx.indexrelid
JOIN   pg_am as am
ON     i.relam = am.oid
JOIN   pg_namespace as ns
ON     ns.oid = i.relnamespace
AND    ns.nspname = ANY(current_schemas(false));

猜你在找的Postgre SQL相关文章