如何在 postgresql 中获取引用 table 的物化视图

How to get materialized views that refer to a table in postgresql

在 postgresql 中,由于 information_schema.views table,可以通过简单的 sql 获取所有引用 table 的视图。但是我需要一个等效的 sql 来获取引用 table.

的物化视图

关注作品

SELECT * 
FROM information_schema.view_table_usages AS u
JOIN information_schema.views AS v on u.view_schema = v.table_schema AND u.view_name = v.table_name
WHERE u.table_name = 'my_table'

我能想到的最接近的:

SELECT * FROM pg_matviews WHERE definition ilike '%my_table%' ;

从这里开始:

https://www.postgresql.org/docs/current/view-pg-matviews.html

information_schema 是 SQL-standard 的东西,SQL 标准没有物化视图的概念,这就是它们没有出现的原因。您可以在 psql 中使用 \d+ 来获取视图定义,包括 information_schema.view_table_usages,它本身就是一个视图。然后,您需要做的就是将过滤后的 relkindv(对于视图)更改为 m(对于物化视图)。

psql (12.4 (Debian 12.4-1+build2))
Type "help" for help.

testdb=# create table t as select c from generate_series(1, 3) c;
SELECT 3
testdb=# create view vt as select * from t;
CREATE VIEW
testdb=# create materialized view mvt as select * from t;
SELECT 3
testdb=# \d+ information_schema.view_table_usage 
                                 View "information_schema.view_table_usage"
    Column     |               Type                | Collation | Nullable | Default | Storage | Description 
---------------+-----------------------------------+-----------+----------+---------+---------+-------------
 view_catalog  | information_schema.sql_identifier |           |          |         | plain   | 
 view_schema   | information_schema.sql_identifier |           |          |         | plain   | 
 view_name     | information_schema.sql_identifier |           |          |         | plain   | 
 table_catalog | information_schema.sql_identifier |           |          |         | plain   | 
 table_schema  | information_schema.sql_identifier |           |          |         | plain   | 
 table_name    | information_schema.sql_identifier |           |          |         | plain   | 
View definition:
 SELECT DISTINCT current_database()::information_schema.sql_identifier AS view_catalog,
    nv.nspname::information_schema.sql_identifier AS view_schema,
    v.relname::information_schema.sql_identifier AS view_name,
    current_database()::information_schema.sql_identifier AS table_catalog,
    nt.nspname::information_schema.sql_identifier AS table_schema,
    t.relname::information_schema.sql_identifier AS table_name
   FROM pg_namespace nv,
    pg_class v,
    pg_depend dv,
    pg_depend dt,
    pg_class t,
    pg_namespace nt
  WHERE nv.oid = v.relnamespace AND v.relkind = 'v'::"char" AND v.oid = dv.refobjid AND dv.refclassid = 'pg_class'::regclass::oid AND dv.classid = 'pg_rewrite'::regclass::oid AND dv.deptype = 'i'::"char" AND dv.objid = dt.objid AND dv.refobjid <> dt.refobjid AND dt.classid = 'pg_rewrite'::regclass::oid AND dt.refclassid = 'pg_class'::regclass::oid AND dt.refobjid = t.oid AND t.relnamespace = nt.oid AND (t.relkind = ANY (ARRAY['r'::"char", 'v'::"char", 'f'::"char", 'p'::"char"])) AND pg_has_role(t.relowner, 'USAGE'::text);



testdb=# SELECT DISTINCT current_database()::information_schema.sql_identifier AS view_catalog,
    nv.nspname::information_schema.sql_identifier AS view_schema,
    v.relname::information_schema.sql_identifier AS view_name,
    current_database()::information_schema.sql_identifier AS table_catalog,
    nt.nspname::information_schema.sql_identifier AS table_schema,
    t.relname::information_schema.sql_identifier AS table_name
   FROM pg_namespace nv,
    pg_class v,
    pg_depend dv,
    pg_depend dt,
    pg_class t,
    pg_namespace nt
  WHERE nv.oid = v.relnamespace AND v.relkind = 'm'::"char" AND v.oid = dv.refobjid AND dv.refclassid = 'pg_class'::regclass::oid AND dv.classid = 'pg_rewrite'::regclass::oid AND dv.deptype = 'i'::"char" AND dv.objid = dt.objid AND dv.refobjid <> dt.refobjid AND dt.classid = 'pg_rewrite'::regclass::oid AND dt.refclassid = 'pg_class'::regclass::oid AND dt.refobjid = t.oid AND t.relnamespace = nt.oid AND (t.relkind = ANY (ARRAY['r'::"char", 'v'::"char", 'f'::"char", 'p'::"char"])) AND pg_has_role(t.relowner, 'USAGE'::text)  ;
 view_catalog | view_schema | view_name | table_catalog | table_schema | table_name 
--------------+-------------+-----------+---------------+--------------+------------
 testdb       | public      | mvt       | testdb        | public       | t
(1 row)