PostgreSQL:如何获取物化视图的源列?

发布于 2025-01-10 17:34:45 字数 2214 浏览 0 评论 0原文

我想获取有关物化视图的列的信息,例如源表和列。

我通过连接 studentteacher 表创建了一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2025-01-17 17:34:45

类似的东西(我已经做了左连接,但这应该是内连接):

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,
    x.source_name,
    x.source_col_name
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
LEFT JOIN (
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
) x On x.mv_schema = pg_namespace.nspname and source_col_sort_order=pg_attribute.attnum
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;

Something like (I have done a LEFT JOIN, but that should have been an INNER JOIN):

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,
    x.source_name,
    x.source_col_name
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
LEFT JOIN (
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
) x On x.mv_schema = pg_namespace.nspname and source_col_sort_order=pg_attribute.attnum
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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文