如何在Redshift中为对象创建依赖项列表? [英] How to create a dependency list for an object in Redshift?
问题描述
我的团队的视图是建立在基于视图的视图上的,因此DROP TABLE CASCADE
通常是灾难的秘诀和大量的试验和错误.
My team has views built upon views built upon views, so often a DROP TABLE CASCADE
is a recipe for disaster and a lot of trial and error.
我想要的是一个查询,该查询返回需要以给定的schema
和table
正确顺序重新创建的所有依赖对象,以便它们可以自动化并在脚本中运行.我正在使用Redshift DROP TABLE
文档
What I would like is a query that returns all dependent objects that need to be re-created in the correct order given a certain schema
and table
so that they could be automated and ran in a script. I'm working with a modified version of the dependency query on the Redshift DROP TABLE
documentation http://docs.aws.amazon.com/redshift/latest/dg/r_DROP_TABLE.html.
似乎正在返回视图及其依赖关系,但不是常规表.我觉得我快要走了,我想念什么?
It seems to be returning views and their dependencies, but not regular tables. I feel like I'm close, what am I missing?
WITH dependencies AS (
SELECT DISTINCT
cls1.oid AS tbloid,
nsp1.nspname AS schemaname,
cls1.relname AS name,
nsp2.nspname AS refbyschemaname,
cls2.relname AS refbyname,
cls2.oid AS viewoid
FROM pg_catalog.pg_class cls1
JOIN pg_catalog.pg_depend dep1
ON cls1.relfilenode = dep1.refobjid
JOIN pg_catalog.pg_depend dep2
ON dep1.objid = dep2.objid
JOIN pg_catalog.pg_class cls2
ON dep2.refobjid = cls2.relfilenode
LEFT OUTER JOIN pg_namespace nsp1
ON cls1.relnamespace = nsp1.oid
LEFT OUTER JOIN pg_namespace nsp2
ON cls2.relnamespace = nsp2.oid
WHERE dep2.deptype IN ('i' :: "CHAR", 'n' :: "CHAR")
AND cls2.relkind IN ('v' :: "CHAR", 'r' :: "CHAR")
AND nsp1.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY 4, 5
),
joined_to_views AS (
SELECT
d.schemaname,
d.name,
d.refbyschemaname,
d.refbyname,
p.definition
FROM dependencies d
LEFT JOIN pg_views p
ON d.refbyschemaname = p.schemaname AND d.refbyname = p.viewname
)
SELECT *
FROM joined_to_views
推荐答案
这对您有用吗?
Does this work for you?
SELECT dependent_ns.nspname as dependent_schema
, dependent_view.relname as dependent_view
, source_ns.nspname as source_schema
, source_table.relname as source_table
, pg_attribute.attname as column_name
FROM pg_depend
JOIN pg_rewrite ON pg_depend.objid = pg_rewrite.oid
JOIN pg_class as dependent_view ON pg_rewrite.ev_class = dependent_view.oid
JOIN pg_class as source_table ON pg_depend.refobjid = source_table.oid
JOIN pg_attribute ON pg_depend.refobjid = pg_attribute.attrelid
AND pg_depend.refobjsubid = pg_attribute.attnum
JOIN pg_namespace dependent_ns ON dependent_ns.oid = dependent_view.relnamespace
JOIN pg_namespace source_ns ON source_ns.oid = source_table.relnamespace
WHERE
source_ns.nspname = 'my_schema'
AND source_table.relname = 'my_table'
AND pg_attribute.attnum > 0
AND pg_attribute.attname = 'my_column'
ORDER BY 1,2;
如果我做出了错误的假设,请发表评论,然后重新调整答案.
If I’ve made a bad assumption please comment and I’ll refocus my answer.
这篇关于如何在Redshift中为对象创建依赖项列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!