postgresql - 刷新物化 View 不包括添加的列

标签 postgresql view materialized-views postgresql-9.3

来自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 VIEWCREATE 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 to CREATE TABLE AS, except that it also remembers the query used to initialize the view.

关于postgresql - 刷新物化 View 不包括添加的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18948434/

相关文章:

android - 从按下的 ListView 项目中保持 subview 的未按下状态

android - 使用共享首选项单击后一分钟后显示 View

postgresql - 使用规则或通知自动刷新物化 View

postgresql - microk8s集群中如何访问本地安装的postgresql

java - 如何在postgreSQL数据库中存储ISO/IEC 19794-2指纹模板

c# - 在 MVC 中将文本与图像居中

database - 为每秒更新几次的表创建物化 View 是否有意义?

postgresql - 刷新物化 View : Concurrency, 事务行为

database - PostgreSQL - 恢复数据库时出错

postgresql - Postgis 允许添加无效的几何图形