Updated on 2026-10-08 GMT+08:00

Query Replication Conflicts on Read Replicas

All SQL operations in this section need to be performed using the root user.

What Is a Streaming Replication Conflict?

PostgreSQL uses streaming replication to synchronize data between a primary instance and its read replicas. As the primary instance continuously generates WAL logs, conflicts will occur when these logs are applied on the read replicas while the read replicas are executing queries.

Simply put, a streaming replication conflict occurs when the recovery process on a read replica cannot apply the WAL logs synchronized from the primary instance.

If a streaming replication conflict occurs, the following error log is displayed on the read replica:

ERROR: canceling statement due to conflict with recovery

Why Does a Streaming Replication Conflict Occur?

For example, a primary instance executes DROP TABLE on a table while a read replica is simultaneously running a query (either from an application or a manual connection) against the same table. Once the DROP TABLE operation is recorded in the WAL and synchronized to the read replica, the read replica attempts to apply the change immediately for data consistency. This causes a conflict with the ongoing SELECT query. Such a conflict also occurs when:

  1. A read replica applies the WAL records about operations that require an ACCESS EXCLUSIVE lock (including DROP TABLE, TRUNCATE TABLE, and most ALTER TABLE operations) while running a query on a table (holding an ACCESS SHARE lock).
  2. A read replica starts a query before the primary instance executes VACUUM and the VACUUM command removes dead tuples that the read replica still needs for that query.

What Can I Do If a Streaming Replication Conflict Occurs?

Parameter Control

PostgreSQL provides the following parameters to control streaming replication conflicts. All these parameters must be modified on the corresponding read replicas.

  • max_standby_streaming_delay

    Description: The default value is 30 (unit: second). It specifies the maximum allowable time that the WAL application process (replay) can be blocked when the WAL data being applied conflicts with a query on a read replica.

    If this parameter is set to -1, WAL replay proceeds only after the query executed by the read replica is complete. Note that max_standby_streaming_delay is not the same as the maximum length of time a query can run before being canceled. It is the maximum total time allowed to apply WAL data once it has been received from the primary instance. Thus, if one query has resulted in a significant delay, subsequent conflicting queries will have much less grace time until the read replica has caught up again.

    Disadvantages: If this parameter is set to a large value or -1, long-running transactions on the read replica will cause a data delay between the primary instance and read replica.

  • hot_standby_feedback

    Description: If this parameter is set to on, the primary instance is notified when a query is executed on a read replica. During the query execution, the primary instance does not delete data required by the read replica. There will be no streaming replication conflicts due to VACUUM.

    Disadvantages: Even if this parameter is set to on, there will still be conflicts caused by operations such as DROP. In addition, long-running transactions on the read replica can prevent the timely removal of dead tuples, leading to database bloat on the primary instance. When the transaction ID age on the primary instance reaches the value of autovacuum_freeze_max_age, a wraparound-prevention VACUUM is triggered. This forced cleanup ignores the standby instance's feedback and may still remove old row versions required by the queries on the standby instance, resulting in snapshot conflicts.

Recommendations:

  1. Optimize SQL queries executed by the read replica and ensure that the SQL query duration does not exceed the value of max_standby_streaming_delay.
  2. Monitor long-running transactions on the read replica. If any long-running transaction exceeds the specified duration (for example, 30 minutes), it must be forcibly terminated.