My report on the PostgreSQL read replica fails with “canceling statement due to conflict with recovery”. How do I run long reports without that error?
A PostgreSQL 18 replica cancels a 67 second report. The error reproduced, what each of the two usual settings did to the replica and to the primary, and where the report can run instead.

We reproduced the error on PostgreSQL 18: a primary, a streaming replica with default settings, and an orders table of 40 million rows. The report is a monthly summary with a window function. Left alone, it takes 67 seconds on the replica.
replica=# SELECT date_trunc('month', created_at)::date AS month, count(*) AS orders, round(avg(amount), 2) AS avg_amount, percentile_cont(0.5) WITHIN GROUP (ORDER BY gap) AS median_gap FROM ( SELECT created_at, amount, created_at - lag(created_at) OVER (PARTITION BY customer_id ORDER BY created_at) AS gap FROM orders ) t GROUP BY 1 ORDER BY 1; ERROR: canceling statement due to conflict with recovery DETAIL: User query might have needed to see row versions that must be removed. Time: 39411.527 ms (00:39.412)
Five seconds after the report started, this ran on the primary:
primary=# UPDATE orders SET status = 'shipped' WHERE id BETWEEN 3000001 AND 3500000; UPDATE 500000 Time: 3676.069 ms (00:03.676) primary=# VACUUM (VERBOSE) orders; INFO: finished vacuuming "shop.public.orders": index scans: 0 tuples: 500000 removed, 31422483 remain, 0 are dead but not yet removable

- 15:11:03, replica. The report starts. It reads
ordersas it was at this moment. - 15:11:08, primary. The
UPDATErewrites 500,000 rows and leaves their old versions behind. - 15:11:12, primary.
VACUUMremoves those old versions. Nobody on the primary needs them. The removal goes to the replica, where the report still needs them. The replica stops applying changes and waits. - 15:11:43, replica. The wait reaches 30 seconds, the default of
max_standby_streaming_delay. The replica cancels the report and applies the removal.
We ran VACUUM by hand to choose the moment. In production autovacuum does the same whenever a table has collected enough dead rows.
Count your cancellations
On the replica:
replica=# SELECT datname, confl_snapshot, confl_lock, confl_bufferpin FROM pg_stat_database_conflicts WHERE datname = 'shop'; datname | confl_snapshot | confl_lock | confl_bufferpin ---------+----------------+------------+----------------- shop | 1 | 1 | 0
confl_snapshot counts the cancellation above. confl_lock counts a second kind, shown further down.
Setting 1: let the replica wait longer
On the replica:
replica=# ALTER SYSTEM SET max_standby_streaming_delay = -1; replica=# SELECT pg_reload_conf();
-1 waits as long as the report takes. A value such as '30min' waits that long and then cancels.
Same report, same UPDATE and VACUUM on the primary. This time the report finished:
(17 rows)
Time: 67104.309 ms (01:07.104)
While it ran, the primary wrote one row a second into a heartbeat table, and a second session on the replica asked every 10 seconds for the newest row it could see:
replica=# SELECT now()::time(0) AS now, max(beat)::time(0) AS newest_row, date_trunc('second', now() - max(beat)) AS behind FROM heartbeat; now | newest_row | behind ----------+------------+---------- 15:05:45 | 15:05:40 | 00:00:05 15:05:55 | 15:05:40 | 00:00:15 15:06:05 | 15:05:40 | 00:00:25 15:06:15 | 15:05:40 | 00:00:35 15:06:26 | 15:05:40 | 00:00:45 15:06:36 | 15:05:40 | 00:00:55 15:06:46 | 15:06:46 | 00:00:00
That is seven runs of the query, one line each. The replica stopped at 15:05:40 and stayed there until the report ended at 15:06:40. For that minute every other session on the replica read old data, and a failover to this replica would have had that minute to apply first. With -1, a report that runs for an hour holds the replica for an hour.
Setting 2: tell the primary what the replica still needs
On the replica, with the delay back at 30 seconds:
replica=# ALTER SYSTEM SET hot_standby_feedback = on; replica=# SELECT pg_reload_conf();
The report finished in 68.8 seconds. This is the primary while it ran:
primary=# SELECT application_name, state, backend_xmin FROM pg_stat_replication; application_name | state | backend_xmin ------------------+-----------+-------------- walreceiver | streaming | 926
backend_xmin is the replica telling the primary that one of its queries still needs every row version from transaction 926 on. Then three rounds on the primary, each one an UPDATE of 500,000 rows followed by VACUUM (VERBOSE) orders. The two lines that matter from each VACUUM:
tuples: 0 removed, 39431678 remain, 500000 are dead but not yet removable removable cutoff: 926, which was 16 XIDs old when operation ended tuples: 0 removed, 39011961 remain, 1000000 are dead but not yet removable removable cutoff: 926, which was 21 XIDs old when operation ended tuples: 0 removed, 38507374 remain, 1500000 are dead but not yet removable removable cutoff: 926, which was 26 XIDs old when operation ended
VACUUM ran three times and removed nothing. After one minute the primary held 1.5 million dead rows, and orders had grown from 2363 MB to 2403 MB, because of a report running on another machine. They could be removed once the report ended. A report that runs for an hour keeps an hour of dead rows in every table that is updated during it.
The cancellation that feedback does not prevent
With hot_standby_feedback still on, the same report on the replica, and this on the primary:
primary=# ALTER TABLE orders ADD COLUMN note text; ALTER TABLE Time: 3.287 ms
On the replica:
ERROR: canceling statement due to conflict with recovery DETAIL: User was holding a relation lock for too long. Time: 35269.358 ms (00:35.269)
The change took 3 milliseconds on the primary. To apply it, the replica needs the same exclusive lock on orders, and the report is reading orders. After 30 seconds the report is cancelled. VACUUM causes the same conflict when it gives empty pages at the end of a table back to the disk. This is the confl_lock counter, and only a longer max_standby_streaming_delay avoids it, with the delay shown in setting 1.
On Amazon RDS both settings are in the replica’s parameter group.
Running the report outside the replica
A streaming replica applies the primary’s changes page by page, and the report reads those same pages. That is where both conflicts come from. Logical replication sends row changes instead: this row was inserted, this one went from these values to those. A reader of that stream keeps its own copy of the tables, and no replay has to wait for a report.
A logical replication slot on the same primary, with nobody reading from it:
primary=# SELECT pg_create_logical_replication_slot('reports_copy', 'pgoutput'); primary=# UPDATE orders SET status = 'returned' WHERE id BETWEEN 2000001 AND 2500000; UPDATE 500000 primary=# SELECT slot_name, slot_type, active, xmin, catalog_xmin FROM pg_replication_slots; slot_name | slot_type | active | xmin | catalog_xmin --------------+-----------+--------+------+-------------- reports_copy | logical | f | | 1063 primary=# VACUUM (VERBOSE) orders; tuples: 1500000 removed, 31719200 remain, 0 are dead but not yet removable primary=# SELECT slot_name, pg_size_pretty(pg_current_wal_lsn() - restart_lsn) AS wal_kept FROM pg_replication_slots; slot_name | wal_kept --------------+---------- reports_copy | 169 MB
xmin is empty, so the slot does not hold VACUUM back on your tables, and VACUUM removed all the dead rows. catalog_xmin holds back only the system catalogs. What the slot keeps is WAL: 169 MB here, because nothing was reading it.
DBTrail is an open source replica built for reports: heavy PostgreSQL queries come back in seconds, without touching production. It reads a slot like that one with pgoutput, the plugin that ships with PostgreSQL, keeps a copy of your tables stored by column, and answers on a port that speaks the PostgreSQL protocol:
$ psql "host=<dbtrail host> port=5433 dbname=shop user=dbtrail_connect sslmode=require"

The numbers below are from a different test, that board: a PostgreSQL 18 source taking writes for 6 hours, and 22 reports running on a streaming replica and on DBTrail at the same moment, on machines of the same size. These are the six that were slowest on the replica, by median time:

In those 6 hours the copy was typically 5 seconds behind the source, and 10 seconds at most. The replica was typically 0.05 seconds behind, and 23 seconds at most while it ran the reports. All 22 reports, the setup and the limits are in the PostgreSQL announcement.
What it costs
wal_level = logicalon your database. It needs a restart if it is not already set.- Each table you capture is set to
REPLICA IDENTITY FULL, so everyUPDATEandDELETEwrites the whole previous row to the WAL. The measurement is below. - The slot keeps WAL while DBTrail is stopped, as the 169 MB above show. Set
max_slot_wal_keep_sizeso a stopped DBTrail cannot fill your disk. - The data is a few seconds old. A report that must see a row written one second ago belongs on your database.
- One more machine to run, beside your database.
The same UPDATE of 500,000 rows on orders, each one right after a checkpoint and on rows never updated before:

That is 10% more on a table with five short columns. Wider rows add more.
It works with PostgreSQL 15 to 18, on your own servers and on Amazon RDS and Aurora. It installs nothing inside PostgreSQL.
Which one to pick
If your reports finish within a minute or two and there are few of them, the two settings are enough. Turn on hot_standby_feedback, set max_standby_streaming_delay a little above your longest report, and watch pg_stat_database_conflicts.
If they run for many minutes, or the counters keep going up after that, move the reports off the replica.
To try DBTrail on a Linux server:
$ curl -fsSL https://raw.githubusercontent.com/dbtrail/dbtrail/main/install-server.sh | sudo DBTRAIL_FOR=postgres sh
The PostgreSQL quick start has every step, including the statements that prepare your database.
Get new posts by email
One email when a new post is out. Nothing else, and you can unsubscribe from any email.