PostgreSQL ERROR: canceling statement due to conflict with recovery

后端 未结 8 805
盖世英雄少女心
盖世英雄少女心 2020-12-02 04:22

I\'m getting the following error when running a query on a PostgreSQL db in standby mode. The query that causes the error works fine for 1 month but when you query for more

相关标签:
8条回答
  • 2020-12-02 04:48

    As stated here about hot_standby_feedback = on :

    Well, the disadvantage of it is that the standby can bloat the master, which might be surprising to some people, too

    And here:

    With what setting of max_standby_streaming_delay? I would rather default that to -1 than default hot_standby_feedback on. That way what you do on the standby only affects the standby


    So I added

    max_standby_streaming_delay = -1
    

    And no more pg_dump error for us, nor master bloat :)

    For AWS RDS instance, check http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.PostgreSQL.CommonDBATasks.html

    0 讨论(0)
  • 2020-12-02 04:51

    The table data on the hot standby slave server is modified while a long running query is running. A solution (PostgreSQL 9.1+) to make sure the table data is not modified is to suspend the replication and resume after the query:

    select pg_xlog_replay_pause(); -- suspend
    select * from foo; -- your query
    select pg_xlog_replay_resume(); --resume
    
    0 讨论(0)
  • 2020-12-02 04:52

    Likewise, here's a 2nd caveat to @Artif3x elaboration of @max-malysh's excellent answer, both above.

    With any delayed application of transactions from the master the follower(s) will have an older, stale view of the data. Therefore while providing time for the query on the follower to finish by setting max_standby_archive_delay and max_standby_streaming_delay makes sense, keep both of these caveats in mind:

    • the value of the follower as a standby / backup diminishes
    • any other queries running on the follower may return stale data.

    If the value of the follower for backup ends up being too much in conflict with hosting queries, one solution would be multiple followers, each optimized for one or the other.

    Also, note that several queries in a row can cause the application of wal entries to keep being delayed. So when choosing the new values, it’s not just the time for a single query, but a moving window that starts whenever a conflicting query starts, and ends when the wal entry is finally applied.

    0 讨论(0)
  • 2020-12-02 04:56

    It might be too late for the answer but we face the same kind of issue on the production. Earlier we have only one RDS and as the number of users increases on the app side, we decided to add Read Replica for it. Read replica works properly on the staging but once we moved to the production we start getting the same error.

    So we solve this by enabling hot_standby_feedback property in the Postgres properties. We referred the following link

    https://aws.amazon.com/blogs/database/best-practices-for-amazon-rds-postgresql-replication/

    I hope it will help.

    0 讨论(0)
  • 2020-12-02 05:05

    No need to touch hot_standby_feedback. As others have mentioned, setting it to on can bloat master. Imagine opening transaction on a slave and not closing it.

    Instead, set max_standby_archive_delay and max_standby_streaming_delay to some sane value:

    # /etc/postgresql/10/main/postgresql.conf on a slave
    max_standby_archive_delay = 900s
    max_standby_streaming_delay = 900s
    

    This way queries on slaves with a duration less than 900 seconds won't be cancelled. If your workload requires longer queries, just set these options to a higher value.

    0 讨论(0)
  • 2020-12-02 05:08

    Running queries on hot-standby server is somewhat tricky — it can fail, because during querying some needed rows might be updated or deleted on primary. As a primary does not know that a query is started on secondary it thinks it can clean up (vacuum) old versions of its rows. Then secondary has to replay this cleanup, and has to forcibly cancel all queries which can use these rows.

    Longer queries will be canceled more often.

    You can work around this by starting a repeatable read transaction on primary which does a dummy query and then sits idle while a real query is run on secondary. Its presence will prevent vacuuming of old row versions on primary.

    More on this subject and other workarounds are explained in Hot Standby — Handling Query Conflicts section in documentation.

    0 讨论(0)
提交回复
热议问题