来自manual
CREATE MATERIALIZED VIEW is similar to CREATE TABLE AS, except that it also remembers the query used to initialize the view, so that it can be refreshed later upon demand.
据我所知,刷新物化 View 应该与重新创建 View
具有相同的效果。但这不是这里发生的事情。
创建单列表
drop table if exists t cascade;
create table t (a int);
insert into t (a) values (1);
创建物化 View
create materialized view mat_view_t as
select * from t ;
select * from mat_view_t;
a
---
1
现在源表中添加了一列
alter table t add column b int;
\d t
Table "public.t"
Column | Type | Modifiers
--------+---------+-----------
a | integer |
b | integer |
然后刷新物化 View
refresh materialized view mat_view_t;
select * from mat_view_t;
a
---
1
\d mat_view_t
Materialized view "public.mat_view_t"
Column | Type | Modifiers
--------+---------+-----------
a | integer |
新专栏在哪里?这是预期的行为吗?如果是,那么我认为该手册具有误导性。
最佳答案
SELECT *
在执行时展开,就像所有类似的操作(CREATE VIEW
、CREATE TABLE AS
)
关键字是“早期绑定(bind)”而不是“后期绑定(bind)”。 Postgres 保存在 SELECT *
执行时出现的列列表,稍后添加的列不会自动包括在内。 查询字符串本身不会保存,只有内部表示在扩展SELECT *
和解析所有标识符等其他东西之后。
REFRESH MATERIALIZED VIEW
从不更改数据定义,仅更改数据:
REFRESH MATERIALIZED VIEW
completely replaces the contents of a materialized view.
手册可能对此更明确,但 comparison to the behavior of CREATE TABLE AS
对我来说很清楚:
CREATE MATERIALIZED VIEW
is similar toCREATE TABLE AS
, except that it also remembers the query used to initialize the view.
关于postgresql - 刷新物化 View 不包括添加的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18948434/