Postgresql

列出 PostgreSQL 中沒有唯一索引的 MATERIALIZED VIEW

  • June 9, 2015

Postgresql 9.4 允許REFRESH 物化視圖 CONCURRENTLY,如果它們具有唯一索引。

我正在尋找一個 SQL 查詢,它列出了所有沒有這種唯一或主鍵索引的物化視圖。

提示:使用 psql 的 -E 標誌來獲取它,以向您展示它是如何針對它的\dm元命令發出的查詢,並從那裡進行調整。

這是一個粗略且根本沒有清理過的查詢,但它應該可以解決問題……

WITH matviews_with_unqiue_keys AS (
 SELECT c.oid, c.relname, c2.relname AS idx_name
 FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
 LEFT JOIN pg_catalog.pg_constraint con ON (
   conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u'))
 WHERE
   c.relkind = 'm' AND
   c.oid = i.indrelid AND i.indexrelid = c2.oid AND indisunique
)

SELECT c.relname
FROM pg_class c
WHERE c.relkind = 'm'
EXCEPT
SELECT mwk.relname
FROM matviews_with_unique_keys as mwk;

引用自:https://dba.stackexchange.com/questions/101899