I need to get metadata about all MATERIALIZED VIEW columns, including their data type. I can do this with this query:
SELECT
trim(leading '_' from pg_type.typname) as data_type,
pg_namespace.nspname as schema_name,
pg_namespace.nspname as udt_name,
attnum AS ordinal_position,
pg_attribute.attname as column_name,
pg_class.relname as matview_name,
'-------' AS spacer,
*
from pg_catalog.pg_attribute
join pg_catalog.pg_class ON pg_class.oid = pg_attribute.attrelid
join pg_catalog.pg_namespace ON pg_namespace.oid = pg_class.relnamespace
join pg_catalog.pg_type ON pg_type.typelem = pg_attribute.atttypid
where
pg_class.relkind = 'm' AND
not pg_attribute.attisdropped and
cast(pg_type.typanalyze as text) = 'array_typanalyze' and
pg_attribute.attnum > 0
order by
pg_attribute.attnum
However it doesn't list any array columns, due to them not existing in the pg_catalog.pg_type
table (which is the table I'm getting the data types from).
How can I also get metadata (including type) for array columns too?
I'm using postgres 13.3.
Best Answer
You can make your life easier using the
format_type()
function: