Last active
April 5, 2024 19:52
-
-
Save brito/7eecfc70f5580b75690ad5694f09deac to your computer and use it in GitHub Desktop.
Sometimes you just need to figure out what's what in your tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
List all schemas, tables, and columns | |
*/ | |
create view inventory.index 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' | |
when relkind = 'p' then 'partitioned table' | |
end as _kind, | |
table_comment, | |
attname as _column, | |
typname as _type, | |
column_comment | |
from pg_class | |
join pg_namespace on pg_class.relnamespace = pg_namespace.oid | |
join pg_attribute on pg_class.oid = pg_attribute.attrelid | |
join pg_type on typelem = atttypid, | |
format('%I.%I', nspname, relname) id, | |
obj_description(id::regclass, 'pg_class') table_comment | |
left join lateral (select col_description(id::regclass, ordinal_position) | |
as column_comment | |
from information_schema.columns | |
where table_catalog = current_database() | |
and table_schema = nspname | |
and table_name = relname | |
and column_name = attname) l on true | |
where pg_attribute.attnum > 0 | |
and relkind in ('r', 'v', 'm', 'p') | |
and nspname not in ('ext', 'information_schema') | |
and nspname !~ '^pg' | |
and relname !~ '^pg' | |
and typanalyze::text = 'array_typanalyze' | |
order by _schema, _relname, _type, _column; | |
comment on view inventory.index is 'List all schemas, tables, and columns'; | |
comment on column inventory.index._schema is 'Schema in this database'; | |
comment on column inventory.index._relname is 'Relation name'; | |
comment on column inventory.index._kind is 'Table, view or materialized view'; | |
comment on column inventory.index.table_comment is 'Table documentation'; | |
comment on column inventory.index._column is 'Column name in table'; | |
comment on column inventory.index._type is 'Database type of column'; | |
comment on column inventory.index.column_comment is 'Column documentation'; | |
alter view inventory.index owner to postgres; | |
table inventory.index; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment