【问题标题】:Postgres : How to alter the name of an MV without it affecting other MVs that reference it?Postgres:如何在不影响引用它的其他 MV 的情况下更改 MV 的名称?
【发布时间】:2021-01-11 15:37:59
【问题描述】:

基本上我的问题是我有一个像这样工作的 MV

create MATERIALIZED VIEW fake_mv as select * from other_fake_mv;

我想将 other_fake_mv 换成另一个具有相同定义的 MV。我试图通过将原始 mv 重命名为某个临时名称来做到这一点,而我试图将 mv 换成原始名称,如下所示:

ALTER TABLE other_fake_mv rename TO other_fake_mv_temp;
ALTER TABLE other_fake_mv_backup rename TO other_fake_mv;

问题是 fake_mv 仍然引用原始的 other_fake_mv(现在命名为 other_fake_mv_temp)而不是新的 other_fake_mv(other_fake_mv_backup)。如何解决这个问题或明确引用指向名称而不是表?

【问题讨论】:

    标签: postgresql rename


    【解决方案1】:

    如果您要经常这样做,您可以在常规视图中定义查询并从该视图构建 MV。这样,您可以根据需要更改视图定义,之后仍然可以刷新 MV。

    例如,它可能是

    -- Create the view containing the query to be (frequently) updated
    CREATE VIEW myView as SELECT a,b,c FROM table_1;
    
    -- Create the MV using the view
    CREATE MATERIALIZED VIEW fake_mv as SELECT a,b,c FROM myView;
    
    
    -- Update the view to point to another table
    CREATE OR REPLACE myView as SELECT a,b,c FROM table_2;
    
    -- Now you can just refresh the MV, as its definition has not changed (it is still just reading the same view)
    REFRESH MATERIALIZED VIEW fake_mv;
    
    

    【讨论】:

    • 您能详细说明一下吗?我不确定我是否完全理解让 mv 指向视图首先会改变事情?我的想法是:create MATERIALIZED VIEW fake_mv as select * from other_fake_view;和视图:创建 VIEW other_fake_view as select * from other_fake_mv;
    • 啊,明白了 - 指向注册更改的视图非常聪明,这不是我会想到的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多