暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

原创 阎书利 2022-11-25
607

一、实现语句

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)
复制
最后修改时间:2022-11-25 23:26:02
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论