ITPub博客

首页 > 数据库 > PostgreSQL > PostgreSQL/MogDB/openGauss怎样获取表上依赖于该表的视图

PostgreSQL/MogDB/openGauss怎样获取表上依赖于该表的视图

原创 PostgreSQL 作者:T1YSL 时间:2022-12-12 11:29:40 0 删除 编辑

一、实现语句

postgres=# select relnamespace,relname,relkind from pg_class where oid in(          
           select c.ev_class
     from pg_depend a,pg_depend b,pg_class pc,pg_rewrite c
    where a.refclassid=1259      
      and b.deptype='i'
      and a.classid=2618
      and a.objid=b.objid
      and a.classid=b.classid
      and a.refclassid=b.refclassid
      and a.refobjid<>b.refobjid
      and pc.oid=a.refobjid     
      and c.oid=b.objid
      and relnamespace in (select oid from pg_namespace where nspname='public') and pc.relname='t1');
 relnamespace | relname | relkind 
--------------+---------+---------
(0 rows)

二、创建一个依赖于t1表的视图a1测试

postgres=# create view  a1 as select * from t1;
CREATE VIEW
postgres=# select relnamespace,relname,relkind from pg_class where oid in(          
           select c.ev_class
     from pg_depend a,pg_depend b,pg_class pc,pg_rewrite c
    where a.refclassid=1259      
      and b.deptype='i'
      and a.classid=2618
      and a.objid=b.objid
      and a.classid=b.classid
      and a.refclassid=b.refclassid
      and a.refobjid<>b.refobjid
      and pc.oid=a.refobjid     
      and c.oid=b.objid
      and relnamespace in (select oid from pg_namespace where nspname='public') and pc.relname='t1');
 relnamespace | relname | relkind 
--------------+---------+---------
         2200 | a1      | v
(1 row)

查询语句里的, a.refclassid=1259,这个1259是pg_class的oid,a.classid=2618的2618是pg_rewrite的oid。

结果里relnamespace=2200是public这个schema的oid,可以查询pg_namespace得到

postgres=# select oid,nspname from pg_namespace where oid=2200;
 oid  | nspname 
------+---------
 2200 | public
(1 row)

因此依赖于public.t1的视图是public.a1。

三、MogDB/openGauss也可以用同样的方法

MogDB=# select relnamespace,relname,relkind from pg_class where oid in(          
MogDB(#            select c.ev_class
MogDB(#      from pg_depend a,pg_depend b,pg_class pc,pg_rewrite c
MogDB(#     where a.refclassid=1259      
MogDB(#       and b.deptype='i'
MogDB(#       and a.classid=2618
MogDB(#       and a.objid=b.objid
MogDB(#       and a.classid=b.classid
MogDB(#       and a.refclassid=b.refclassid
MogDB(#       and a.refobjid<>b.refobjid
MogDB(#       and pc.oid=a.refobjid     
MogDB(#       and c.oid=b.objid
MogDB(#       and relnamespace in (select oid from pg_namespace where nspname='public') and pc.relname='t1');
 relnamespace | relname | relkind 
--------------+---------+---------
         2200 | a1      | v
(1 row)


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69990629/viewspace-2927703/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
中国开源软件推进联盟PG分会系列技术丛书《快速掌握PostgreSQL版本新特性》一书副主编。数据库技术专家。数据库认证讲师、恩墨学院数据库讲师。擅长MySQL数据库、PostgreSQL等开源数据库。拥有MySQL-OCP 、PostgreSQL-PGCM等数据库认证。

注册时间:2020-12-12

  • 博文量
    152
  • 访问量
    160730