Dej*_*jan 5 postgresql index postgresql-11
我想获取 PostgreSQL 中特定数据库的所有 UNIQUE 索引(用于唯一列组合)的列表。我进行了搜索,但找不到与此相关的查询。
我找到了这两个查询。
SELECT indexname FROM pg_indexes;
SELECT * from pg_indexes where schemaname = 'public';
Run Code Online (Sandbox Code Playgroud)
但两者都不适合我的需要。我只需要那些标识唯一列索引的索引。谢谢。
小智 11
您需要使用pg_index并限制那些具有indisunique. 要获取实际的索引和表名称,您需要将该表连接到 pg_class 和 pg_namespace:
select idx.relname as index_name,
insp.nspname as index_schema,
tbl.relname as table_name,
tnsp.nspname as table_schema
from pg_index pgi
join pg_class idx on idx.oid = pgi.indexrelid
join pg_namespace insp on insp.oid = idx.relnamespace
join pg_class tbl on tbl.oid = pgi.indrelid
join pg_namespace tnsp on tnsp.oid = tbl.relnamespace
where pgi.indisunique --<< only unique indexes
and tnsp.nspname = 'public' --<< only for tables from the public schema
Run Code Online (Sandbox Code Playgroud)
要排除主键,您应该过滤掉indisprimary在where子句中添加此条件的那些:
...
and pgi.indisprimary = false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8045 次 |
| 最近记录: |