获取物化视图的约束和索引

时间:2019-08-15 20:21:15

标签: sql postgresql

查询以获取Postgres中所有物化视图的所有索引和所有约束。以下查询仅返回表的索引。

SELECT indexname, indexdef 
FROM pg_indexes 
WHERE schemaname = 'public' AND tablename = 'table'

1 个答案:

答案 0 :(得分:0)

视图pg_indexes可以访问有关数据库中每个索引的有用信息(还包括物化视图的索引)。您可以查找pg_class以仅过滤实例化视图(relkind = 'm'):

select i.*
from pg_indexes i
join pg_class c
    on schemaname = relnamespace::regnamespace::text 
    and tablename = relname
where relkind = 'm'

 schemaname | tablename |    indexname    | tablespace |                             indexdef                             
------------+-----------+-----------------+------------+------------------------------------------------------------------
 public     | my_view   | my_view_id_idx  |            | CREATE UNIQUE INDEX my_view_id_idx ON my_view USING btree (id)
 public     | sen_view  | sen_view_id_idx |            | CREATE UNIQUE INDEX sen_view_id_idx ON sen_view USING btree (id)
(2 rows)