截断后的pg_catalog.pg_class.relfilenode

时间:2016-10-24 16:23:01

标签: postgresql

我有一个视图取决于一个表:创建视图hello_v as select * from hello; 要从视图名称中检索引用的表名,我认为pg_class和pg_depend表可能很有用。 在表上截断导致pg_depend中没有条目。

如何返回pg_depend.refobjid,它等于pg_catalog.relfilenode?

我可以重现我的观点:

select version();

create table hello(name text);
create view hello_v as select * from hello;

select c.relname, c.relfilenode from pg_catalog.pg_class c inner join pg_catalog.pg_depend d on d.refobjid = c.relfilenode
    where c.relname in ('hello', 'hello_v') group by relname, relfilenode order by c.relname;

 relname | relfilenode
---------+-------------
 hello   |      305818
 hello_v |      305824

truncate hello;

select c.relname, c.relfilenode from pg_catalog.pg_class c inner join pg_catalog.pg_depend d on d.refobjid = c.relfilenode
    where c.relname in ('hello', 'hello_v') group by relname, relfilenode order by c.relname;

 relname | relfilenode
---------+-------------
 hello_v |      305824

 -- I hope to exist a record where relname = hello and relfilenode = xxx

或者简单地说:

create table hello(name text);

select c.relname, c.relfilenode from pg_catalog.pg_class c inner join pg_catalog.pg_depend d on d.refobjid = c.relfilenode
    where c.relname = 'hello' group by relname, relfilenode order by c.relname;

truncate hello;

select c.relname, c.relfilenode from pg_catalog.pg_class c inner join pg_catalog.pg_depend d on d.refobjid = c.relfilenode
    where c.relname = 'hello' group by relname, relfilenode order by c.relname;

感谢。

1 个答案:

答案 0 :(得分:0)

您的加入条件错误。你想加入反对c.oid,而不是relfilenode。

这会正确显示您的表格:

select c.relname, c.relfilenode from pg_catalog.pg_class c inner join pg_catalog.pg_depend d on d.refobjid = c.oid
where c.relname = 'hello' group by relname, relfilenode order by c.relname;