简体   繁体   English

PostgreSQL 9.3的REFRESH MATERIALIZED VIEW命令仅持续Django连接的时间

[英]PostgreSQL 9.3's REFRESH MATERIALIZED VIEW command only lasts for length of Django connection

I've got a materialized view in PostgreSQL that I would like to update periodically. 我在PostgreSQL中有一个物化视图,我想定期更新。 When I try to update it manually from the Django manage.py shell, the command to refresh the materialized view successfully completes, but the changes seem to disappear after I leave the shell. 当我尝试从Django manage.py shell手动更新它时,刷新实例化视图的命令已成功完成,但是离开外壳后,更改似乎消失了。 For example, on my first session, these are the commands that I run: 例如,在我的第一个会话中,这些是我运行的命令:

>>> Model.objects.get(...).subscribers.count
2L
>>> RefreshMaterializedViews()
>>> Model.objects.get(...).subscribers.count
3L

I then exit from the shell, go back in, and run the query again: 然后,我从外壳退出,返回,然后再次运行查询:

>>> Model.objects.get(...).subscribers.count
2L

The RefreshMaterializedViews function is written like this: RefreshMaterializedViews函数的编写方式如下:

def RefreshMaterializedViews():
    for materialized_view in MATERIALIZED_VIEWS:
        cursor = connection.cursor()

        try:
            cursor.execute("REFRESH MATERIALIZED VIEW %s;" % materialized_view)
        finally:
            cursor.close()

So, it seems that PostgreSQL only bothered to refresh the materialized view for the current connection. 因此,似乎PostgreSQL只为刷新当前连接的实例化视图而烦恼。 On the other hand, going into the database shell itself and simply running REFRESH MATERIALIZED VIEW xxx; 另一方面,进入数据库外壳本身并仅运行REFRESH MATERIALIZED VIEW xxx; works. 作品。

好像您忘记了:

connection.commit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM