Skip to content

Instantly share code, notes, and snippets.

@brito
Created May 23, 2020 02:12
Show Gist options
  • Save brito/188f6f9d2ce8d3a8a164af945ec26691 to your computer and use it in GitHub Desktop.
Save brito/188f6f9d2ce8d3a8a164af945ec26691 to your computer and use it in GitHub Desktop.
List column names and types for relations in schemas
/*
List column names and types for _relations in _schemas
eg */ select * from _meta (
'{app_public}',
'{data_report_session,data_raw_session,data_report}');/*
*/
create function _meta (_schemas text[], _relations text[])
returns table (_schema name, _relname name, _kind text, _column name, _type name) as $$
select
nspname as _schema,
relname as _relname,
case when relkind = 'r' then 'table'
when relkind = 'v' then 'view'
when relkind = 'm' then 'materialized view'
end as _kind,
attname as _column,
typname as _type
from pg_catalog.pg_class
join pg_catalog.pg_namespace
on pg_class.relnamespace = pg_namespace.oid
join pg_catalog.pg_attribute
on pg_class.oid = pg_attribute.attrelid
join pg_catalog.pg_type
on typelem = atttypid
where array[nspname::text] <@ _schemas
and array[relname::text] <@ _relations
and pg_attribute.attnum > 0
and relkind in ('r','v','m')
and nspname not in ('pg_catalog','information_schema')
and typanalyze::text = 'array_typanalyze'
order by _type, _column, _schema, _relname
$$ language sql stable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment