【问题标题】:postgresql 9.5 - lock child materialized view while parents refresh concurrentlypostgresql 9.5 - 在父母同时刷新时锁定子物化视图
【发布时间】:2016-07-06 23:40:33
【问题描述】:

我有一个依赖于其他几个物化视图的物化视图。

matviewA matviewB matviewC
    \       |       /
         matviewX

我想做的是同时刷新父实体化视图。 (这不是一个真正的问题——如下所示。) [在我当前的环境中,每个父母大约需要一个小时来刷新。 ]

psql -c "refresh materialized view concurrently matviewA" &
psql -c "refresh materialized view concurrently matviewB" &
psql -c "refresh materialized view concurrently matviewC" &

但是,如果我开始刷新孩子:

psql -c "refresh materialized view concurrently matviewX" &

它立即运行并且不等待父母完成刷新,因为他们不会在他们自己已经同时刷新时锁定他们的孩子进行刷新。

我试着在父母跑的时候给孩子加锁:

psql -c "lock matviewX in share mode; refresh materialized view concurrently matviewA" &
psql -c "lock matviewX in share mode; refresh materialized view concurrently matviewB" &
psql -c "lock matviewX in share mode; refresh materialized view concurrently matviewC" &

很遗憾,您不能对物化视图进行显式锁定。

如果我不对父母使用“并发”,则子物化视图变得不可读。 (但子刷新在运行前会等待。)

我可以在调用“psql -c”的(bash)包装脚本中编写一些锁管理。或者我可以使用更复杂的第三方作业调度程序。我希望有一个更简单的方法。

我也许可以编写一个函数并将所有刷新都放在该函数中,然后使用临时表进行手动显式锁管理。

或者也许以某种方式使用咨询锁。

建议?

【问题讨论】:

    标签: postgresql concurrency postgresql-9.5


    【解决方案1】:

    您可以简单地使用wait(参见https://stackoverflow.com/a/18663969/3886053):

    for parent in matviewA matviewB matviewC; do
      psql -c "refresh materialized view concurrently $parent" &
      echo "Started refreshing materialized view $parent"
    done
    echo -n "Waiting for all parents to finish... "
    wait
    echo "finished. Refreshing now the child materialized view"
    psql -c "refresh materialized view concurrently matviewX"
    

    【讨论】:

      猜你喜欢
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 2022-08-09
      • 1970-01-01
      • 2021-01-09
      • 2011-07-09
      • 2010-09-27
      相关资源
      最近更新 更多