Column data types for materialized views?

白昼怎懂夜的黑 提交于 2019-12-04 07:20:53

I think you're very close. Last step would be to join with pg_type:

join pg_catalog.pg_type as tp on tp.typelem = a.atttypid

The field tp.typname would have the datatype. Although it seems a filter must be added to remove line datatypes, whatever that is:

cast(tp.typanalyze as text) = 'array_typanalyze'

I don't fully understand the underlying data model, so use my solution below with a grain of salt. Anyway, based on your contribution I ended up with the following query which gets column datatypes using namespace (e.g., schema) and relation (e.g., materialized view) name:

select 
    ns.nspname as schema_name, 
    cls.relname as table_name, 
    attr.attname as column_name,
    trim(leading '_' from tp.typname) as datatype
from pg_catalog.pg_attribute as attr
join pg_catalog.pg_class as cls on cls.oid = attr.attrelid
join pg_catalog.pg_namespace as ns on ns.oid = cls.relnamespace
join pg_catalog.pg_type as tp on tp.typelem = attr.atttypid
where 
    ns.nspname = 'your_schema' and
    cls.relname = 'your_materialized_view' and 
    not attr.attisdropped and 
    cast(tp.typanalyze as text) = 'array_typanalyze' and 
    attr.attnum > 0
order by 
    attr.attnum

You have to change 'your_schema'and 'your_materialized_view'.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!