对于常规表和视图,我可以通过运行以下查询来查看其数据类型:
select data_type from information_schema.columns
where .....
但是,似乎没有关于物化视图的任何信息出现在这里。
我可以通过运行来获取实体化视图的列列表:
SELECT
a.attname as column_name
FROM
pg_catalog.pg_attribute a
INNER JOIN
(SELECT c.oid,
n.nspname,
c.relname
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname ~ ('^(materializedview)$')
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 2, 3) b
ON a.attrelid = b.oid
INNER JOIN
(SELECT
a.attrelid,
max(a.attnum) as max_attnum
FROM pg_catalog.pg_attribute a
WHERE a.attnum > 0
AND NOT a.attisdropped
GROUP BY a.attrelid) e
ON a.attrelid=e.attrelid
WHERE a.attnum > 0
AND NOT a.attisdropped
ORDER BY a.attnum
但是,我无法弄清楚我是否可以确定基础列/数据类型是什么。
有没有办法查看这些信息?
答案 0 :(得分:6)
我觉得你很亲密。最后一步是加入pg_type
:
join pg_catalog.pg_type as tp on tp.typelem = a.atttypid
字段tp.typname
将具有数据类型。虽然看起来必须添加一个过滤器来删除line
数据类型,无论是什么:
cast(tp.typanalyze as text) = 'array_typanalyze'
我并不完全了解基础数据模型,因此请使用下面的解决方案。无论如何,根据您的贡献,我最终得到以下查询,该查询使用命名空间(例如,模式)和关系(例如,物化视图)名称获取列数据类型:
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
您必须更改'your_schema'
和'your_materialized_view'
。