如何获取引用PostgreSQL中表的实例化视图

问题描述

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

对于以下作品的看法

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标准的东西,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,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",'USAGE'::text)  ;
 view_catalog | view_schema | view_name | table_catalog | table_schema | table_name 
--------------+-------------+-----------+---------------+--------------+------------
 testdb       | public      | mvt       | testdb        | public       | t
(1 row)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...