psql物化视图自动更新

匿名 (未验证) 提交于 2019-12-02 23:42:01

更新物化视图示例

CREATE TABLE model.test (
id SERIAL PRIMARY KEY,
name VARCHAR(60)
);

. 创建物化视图

CREATE MATERIALIZED VIEW model.test_materialized_view AS
SELECT * FROM model.test;

. 创建唯一索引,因为 CONCURRENTLY 同步更新需要唯一索引

CREATE UNIQUE INDEX test_materialized_view_index ON model.test_materialized_view(id);

. 同步更新,但是需要触发,不能向 oracle 那种可以自动触发更新

REFRESH MATERIALIZED VIEW CONCURRENTLY model.test_materialized_view;

. 创建函数执行同步更新和触发器执行函数

create or replace function update_test_materialized_view() returns trigger as $$ declare begin REFRESH MATERIALIZED VIEW CONCURRENTLY model.test_materialized_view; return new; end; $$ language plpgsql; CREATE TRIGGER update_test_materialized_view AFTER UPDATE ON model.test FOR EACH STATEMENT EXECUTE PROCEDURE update_test_materialized_view(); CREATE TRIGGER update_test_materialized_view AFTER UPDATE ON model.test FOR EACH STATEMENT EXECUTE PROCEDURE update_test_materialized_view(); CREATE TRIGGER update_test_materialized_view AFTER UPDATE ON model.test FOR EACH STATEMENT EXECUTE PROCEDURE update_test_materialized_view(); 

  


---------------------
作者:Resemble_
来源:CSDN
原文:https://blog.csdn.net/qq_27657429/article/details/76643294
版权声明:本文为博主原创文章,转载请附上博文链接!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!