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

Get constraints and indexes on materialized views

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

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

视图pg_indexes provides access to useful information about each index in the database (also indexes on materialized views). You can lookup 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)