Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change postgres get_catalog to not use information_schema #1540

Merged
merged 4 commits into from
Aug 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 31 additions & 59 deletions plugins/postgres/dbt/include/postgres/macros/catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,37 @@
{% set database = information_schemas[0].database %}
{{ adapter.verify_database(database) }}

with table_owners as (

select
'{{ database }}' as table_database,
schemaname as table_schema,
tablename as table_name,
tableowner as table_owner

from pg_tables

union all

select
'{{ database }}' as table_database,
schemaname as table_schema,
viewname as table_name,
viewowner as table_owner

from pg_views

),

tables as (

select
table_catalog as table_database,
table_schema,
table_name,
table_type

from information_schema.tables

),

columns as (

select
table_catalog as table_database,
table_schema,
table_name,
null as table_comment,
column_name,
ordinal_position as column_index,
data_type as column_type,
null as column_comment

from information_schema.columns

)

select *
from tables
join columns using (table_database, table_schema, table_name)
join table_owners using (table_database, table_schema, table_name)

where table_schema != 'information_schema'
and table_schema not like 'pg_%'

order by column_index
select
'{{ database }}' as table_database,
sch.nspname as table_schema,
tbl.relname as table_name,
case tbl.relkind
when 'v' then 'VIEW'
else 'BASE TABLE'
end as table_type,
null::text as table_comment,
col.attname as column_name,
col.attnum as column_index,
pg_catalog.format_type(col.atttypid, col.atttypmod) as column_type,
null::text as column_comment,
pg_get_userbyid(tbl.relowner) as table_owner

from pg_catalog.pg_namespace sch
join pg_catalog.pg_class tbl on tbl.relnamespace = sch.oid
join pg_catalog.pg_attribute col on col.attrelid = tbl.oid

where sch.nspname != 'information_schema'
and sch.nspname not like 'pg_%' -- avoid postgres system schemas
and not pg_is_other_temp_schema(sch.oid) -- not a temporary schema belonging to another session
and tbl.relpersistence = 'p' -- [p]ermanent table. Other values are [u]nlogged table, [t]emporary table
and tbl.relkind in ('r', 'v', 'f', 'p') -- o[r]dinary table, [v]iew, [f]oreign table, [p]artitioned table. Other values are [i]ndex, [S]equence, [c]omposite type, [t]OAST table, [m]aterialized view
and col.attnum > 0 -- negative numbers are used for system columns such as oid
and not col.attisdropped -- column as not been dropped

order by
sch.nspname,
tbl.relname,
col.attnum

{%- endcall -%}

Expand Down