Redshift 列出所有模式名、表名和列名

Redshift list all schemanames, tablenames and columnnames

我试图加入 information_schema.columns 但发现无法完成,而 pg_table_def 是等效的。

但是它有一个问题,就是只显示 search_path 中存在的模式,我怎样才能从 pg_table_def

中得到一个 information_schema.columns 等价物

或者设置search_path到处搜索?

第 1 步:获取所有模式

select nspname FROM pg_namespace
where nspname not like 'pg_%' --to execlude pg schemas unless you need them
and nspname not in (select schemaname from svv_external_schemas); -- to execlude external schemas because you cannot add them to search path

step2: 把它们变成逗号分隔的列表

将上面的查询结果转换为 schema1,schema2,schema3,schema4...schema(n)

第三步:设置搜索路径

SET search_path to schema1,schema2,schema3,schema4...schema(n)

步骤 4pg_table_def

SELECT * 来自 pg_table_def

通过 SQL 和 jinja2 模板将此作为自动化解决方案发布,这是 DBT

中的 运行
{% macro populate_schema_catalog(target_schema_name, target_table_name) %}
{%- set fetch_all_schemas -%}
    SELECT
        nspname
    FROM
        pg_namespace
    WHERE
        nspname NOT LIKE 'pg_%'
        AND nspname NOT IN (
            SELECT
                schemaname
            FROM
                svv_external_schemas
        );
{%- endset -%}

{%- set all_schemas = run_query(fetch_all_schemas).rows.values() -%}
{%- set schema_list = [] -%}

-- The schema list now contains all the schemas as such ['"meta"', '"adhoc_dbt"', ...]
{% for schema in all_schemas %}
    {%- do schema_list.append('"{}"'.format(schema[0]))-%}
{% endfor %}

-- Set the search_path
{%- set set_search_path -%}
    SET search_path TO {{ ', '.join(schema_list) }};
{%- endset -%}

{%- do run_query(set_search_path) -%}

{%- set query_pg_table_def -%}
    SELECT
        *
    FROM
        pg_table_def
{%- endset -%}

{%- set all_information = run_query(query_pg_table_def).rows.values() -%}

{% for row in all_information %}
{{ log(row) }}
{% endfor %}

{% endmacro %}

当您使用 Amazon Redshift 时,可以从 svv_column 视图中获取该信息。

例如查询

SELECT * FROM svv_columns;

参考:https://docs.aws.amazon.com/redshift/latest/dg/r_SVV_COLUMNS.html

这不受 search_path 和用户 运行 权限的影响,并且对所有用户可见。