我正在寻找打印架构&架构中所有物化视图所依赖的关系名称:
select c.relname, d.classid, d.objid,
pg_describe_object(
d.classid, d.objid, d.objsubid)
from pg_class c
join pg_namespace n on c.relnamespace = n.oid
left join pg_depend d on c.oid = d.objid
where n.nspname = 'direct' and d.deptype = 'n'
这就是:
relname | relname | classid | objid | pg_describe_object
------------------------+---------+---------+-------+---------------------------------------------------
cases | | 2618 | 33736 | rule _RETURN on materialized view case_categories
benefit_investigations | | 2618 | 33928 | rule _RETURN on materialized view bi_intervals
返回的描述给出了一些提示,但它不包含关系的模式。如何获取实际的依赖关系模式和名称? [NB我正在使用postgres 9.6]
答案 0 :(得分:1)
你走了:
SELECT DISTINCT view_cs.nspname, view_c.relname, tab_cs.nspname, tab_c.relname
FROM pg_depend view_d
JOIN pg_class view_c ON view_c.oid = view_d.refobjid AND view_c.relkind = 'm'
JOIN pg_type view_ct ON view_ct.oid = view_c.reltype
JOIN pg_namespace view_cs ON view_cs.oid = view_ct.typnamespace
JOIN pg_depend tab_d ON tab_d.objid = view_d.objid
JOIN pg_class tab_c ON tab_c.oid = tab_d.refobjid AND tab_c.relkind = 'r'
JOIN pg_type tab_ct ON tab_ct.oid = tab_c.reltype
JOIN pg_namespace tab_cs ON tab_cs.oid = tab_ct.typnamespace
WHERE view_d.deptype = 'n'
诀窍是你必须JOIN pg_depend
两次 - 一次用于视图规则关系,一次用于规则表关系。
答案 1 :(得分:1)
添加
AND view_c.relname != tab_c.relname
@Boris 接受的答案的 WHERE 语句,以避免显示表格对自身的依赖性