【问题标题】:Redshift - How to display dependencies that CASCADE will delete?Redshift - 如何显示 CASCADE 将删除的依赖项?
【发布时间】:2020-06-11 22:44:17
【问题描述】:

在 Redshift 中删除表时,我收到一条错误消息:

亚马逊无效操作:无法删除表 rawdata.bss_edw_customer_account 因为其他对象依赖它;

我不想使用 CASCADE,因为我不知道它会杀死哪些其他表/视图。如何找出哪些表或视图依赖于我要删除的表?我想确保我不会踩到别人的工作。

谢谢

【问题讨论】:

  • 我不认为你可以“杀死”其他人的工作,因为如果存在依赖关系,DROP...CASCADE 将停止。由于 Amazon Redshift 基于 PostgreSQL,这可能有效:Find dependent objects for a table or view

标签: amazon-redshift cascade


【解决方案1】:

对象 [1] 和约束 [2] 依赖项的 Redshift 管理视图有助于识别依赖对象。您还可以按照 DROP 表文档 [3] 中的说明创建 find_depend 视图以查看任何依赖项。

我还发现这些视图可能并不总是将物化视图列为依赖对象。如果您过去创建了任何 MV,那么您可能希望使用视图 DDL 检查依赖关系。以下查询可能会有所帮助:

select schemaname, viewname from pg_views where schemaname not like 'pg_catalog' and schemaname not like 'information_schema' and definition like '%<tablename>%';

[1]https://github.com/awslabs/amazon-redshift-utils/blob/master/src/AdminViews/v_object_dependency.sql

[2]https://github.com/awslabs/amazon-redshift-utils/blob/master/src/AdminViews/v_constraint_dependency.sql

[3]https://docs.aws.amazon.com/redshift/latest/dg/r_DROP_TABLE.html

【讨论】:

    【解决方案2】:

    如果您需要更全面地了解架构中的(物化)视图依赖项,您可以使用以下代码段。只需将 schema_name 替换为模式的名称即可。如果您需要显示跨模式的依赖关系,这应该很容易通过调整正则表达式或进行联合来实现。

    create view admin.v_view_dependencies_fixed as (
      with h1 as (
          select generate_series as i
          from generate_series(1, 100) -- we can use this since the query only touches the leader node
      ),
      h2 as (
          select schemaname                                         as dependent_schema,
                 viewname                                           as dependent_view,
                 schemaname || '.' || viewname                      as dependent_full,
                 regexp_substr(definition, 'schema_name\\.\\w+', 1, i) as dependency
          from pg_views
                   cross join h1
          where schemaname = 'schema_name'
            and dependency is not null
            and dependency != ''
      )
      select distinct
             dependent_full,
             dependent_schema,
             dependent_view,
             dependency as source_full,
             split_part(dependency, '.', 1) as source_schema,
             split_part(dependency, '.', 2) as source_object
      from h2
      where dependent_full != source_full
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      相关资源
      最近更新 更多