刷新还是创建实体化视图? Postgres

时间:2020-05-19 19:32:33

标签: sql postgresql materialized-views

我有一个物化视图,当我们的数据管道完全刷新时,有时会被删除。有没有一种方法可以在查询中包含一些逻辑来刷新(如果存在),如果不存在,则创建它?

类似这样的东西

refresh materialized view my_mat_view

if my_mat_view does not exist then execute the code below

create materialized view my_mat_view as select * from table

1 个答案:

答案 0 :(得分:2)

一种选择是将create .. if not exists与无数据选项一起使用,然后无条件运行refresh

create materialized view if not exists my_mat_view 
as 
select * 
from table
with no data; --<< don't populate the data just yet

-- the mview will exists now, no matter what
refresh materialized view my_mat_view;