PostgreSQL:如何获取物化视图的源列?
我想获取有关物化视图的列的信息,例如源表和列。
我通过连接 student
和 teacher
表创建了一个名为 student_teacher_mv
的物化视图,如下所示。
CREATE MATERIALIZED VIEW student_teacher_mv AS (
SELECT
student.id AS student_id,
student.name AS student_name,
student.teacher_id as teacher_id,
teacher.name as teacher_name
FROM student
JOIN teacher
ON teacher.id = student.teacher_id
);
我编写了这个查询,它为我提供了有关物化视图及其列的信息:
SELECT
pg_namespace.nspname AS mv_schema,
pg_class.relname AS mv_name,
pg_attribute.attname AS mv_col_name,
pg_type.typname AS mv_col_type,
pg_attribute.attnum AS mv_col_sort_order
FROM pg_catalog.pg_class
INNER JOIN pg_catalog.pg_namespace
ON pg_class.relnamespace = pg_namespace.oid
INNER JOIN pg_catalog.pg_attribute
ON pg_class.oid = pg_attribute.attrelid
INNER JOIN pg_catalog.pg_statio_all_tables st
ON pg_namespace.nspname = st.schemaname
AND pg_class.relname = st.relname
INNER JOIN pg_type
ON pg_type.oid = pg_attribute.atttypid
WHERE pg_class.relkind = 'm'
AND pg_attribute.attnum >= 1
AND pg_class.relname = 'student_teacher_mv'
ORDER BY mv_schema, mv_name, mv_col_sort_order;
我还编写了这个查询,它为我提供了有关物化视图以及哪些表、视图和/或物化视图及其派生自的列的信息:
SELECT
pg_namespace.nspname AS mv_schema,
pg_rewrite.ev_class::regclass AS mv_name,
pg_class.relname AS source_name,
pg_attribute.attname AS source_col_name,
pg_depend.refobjsubid AS source_col_sort_order
FROM pg_rewrite
JOIN pg_depend ON
pg_depend.classid = 'pg_rewrite'::regclass AND
pg_depend.objid = pg_rewrite.oid AND
pg_depend.refclassid = 'pg_class'::regclass AND
pg_depend.refobjid <> pg_rewrite.ev_class
JOIN pg_class ON
pg_class.oid = pg_depend.refobjid AND
pg_class.relkind IN ('r','v','m')
JOIN pg_catalog.pg_namespace
ON pg_namespace.oid = pg_class.relnamespace
LEFT JOIN pg_catalog.pg_attribute
ON pg_class.oid = pg_attribute.attrelid
AND pg_attribute.attnum = pg_depend.refobjsubid
WHERE
pg_rewrite.ev_class = 'student_teacher_mv'::regclass
本质上,我希望能够合并这两个查询的结果,但很难弄清楚如何/什么加入。我似乎找不到任何类型的标识符将新列链接到派生列。
I would like to get information on the columns of the materialized view such as the source tables and columns.
I created a materialized view called student_teacher_mv
by joining the student
and teacher
tables as shown below.
CREATE MATERIALIZED VIEW student_teacher_mv AS (
SELECT
student.id AS student_id,
student.name AS student_name,
student.teacher_id as teacher_id,
teacher.name as teacher_name
FROM student
JOIN teacher
ON teacher.id = student.teacher_id
);
I have written this query which gives me information about the materialized view and its columns:
SELECT
pg_namespace.nspname AS mv_schema,
pg_class.relname AS mv_name,
pg_attribute.attname AS mv_col_name,
pg_type.typname AS mv_col_type,
pg_attribute.attnum AS mv_col_sort_order
FROM pg_catalog.pg_class
INNER JOIN pg_catalog.pg_namespace
ON pg_class.relnamespace = pg_namespace.oid
INNER JOIN pg_catalog.pg_attribute
ON pg_class.oid = pg_attribute.attrelid
INNER JOIN pg_catalog.pg_statio_all_tables st
ON pg_namespace.nspname = st.schemaname
AND pg_class.relname = st.relname
INNER JOIN pg_type
ON pg_type.oid = pg_attribute.atttypid
WHERE pg_class.relkind = 'm'
AND pg_attribute.attnum >= 1
AND pg_class.relname = 'student_teacher_mv'
ORDER BY mv_schema, mv_name, mv_col_sort_order;
I have also written this query which gives me information about the materialized view and which tables, views and/or materialized views along with the columns it is derived from:
SELECT
pg_namespace.nspname AS mv_schema,
pg_rewrite.ev_class::regclass AS mv_name,
pg_class.relname AS source_name,
pg_attribute.attname AS source_col_name,
pg_depend.refobjsubid AS source_col_sort_order
FROM pg_rewrite
JOIN pg_depend ON
pg_depend.classid = 'pg_rewrite'::regclass AND
pg_depend.objid = pg_rewrite.oid AND
pg_depend.refclassid = 'pg_class'::regclass AND
pg_depend.refobjid <> pg_rewrite.ev_class
JOIN pg_class ON
pg_class.oid = pg_depend.refobjid AND
pg_class.relkind IN ('r','v','m')
JOIN pg_catalog.pg_namespace
ON pg_namespace.oid = pg_class.relnamespace
LEFT JOIN pg_catalog.pg_attribute
ON pg_class.oid = pg_attribute.attrelid
AND pg_attribute.attnum = pg_depend.refobjsubid
WHERE
pg_rewrite.ev_class = 'student_teacher_mv'::regclass
Essentially I want to be able to combine the results of these two queries, but having a hard time figuring out how/what to join on. I can't seem to find any sort of identifier linking the new column to the derived ones.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似的东西(我已经做了左连接,但这应该是内连接):
Something like (I have done a LEFT JOIN, but that should have been an INNER JOIN):