How a Six-Hour Analytics Query on a Replica Stopped Vacuum on the Primary and Tripled Order Latency
15:12 IST, Wednesday. Datadog: p95 on GET /orders is 410 ms and climbing. It was 38 ms this
morning. Nothing deployed since Monday.
the scramble
Priya gets paged first because the alert routes to the orders on-call. She posts the graph in the incident channel: latency is not a step, it is a ramp, starting around 09:30 and getting steeper every hour. A ramp usually means something is accumulating.
First theory: the planner picked a bad plan after yesterday's stats refresh. I run EXPLAIN (ANALYZE, BUFFERS)
on the list query and compare it to the saved plan from last week. Same plan, same index
(orders_customer_id_created_at_idx), but the buffer count is wildly different:
Same plan, ninety times more pages touched for the same twenty rows. That looks like bloat. The index is full of pointers to dead tuples that the scan has to visit and discard.
the hunt
Dead end one: autovacuum is not running. Easy to check:
Autovacuum ran eight minutes ago, and there are still 11.8 million dead tuples on a 6-million-row table. It is running and it is not removing anything. In the logs, the verbose output says why, one line per run:
"Dead but not yet removable" means some transaction can still see those row versions. Something is holding the xmin horizon back by almost five million transactions.
Dead end two: a long transaction on the primary. I look in pg_stat_activity for anything old:
The oldest is 40 seconds. Not it. Dead end three: a forgotten replication slot, because we have been
burned by that before. pg_replication_slots shows one physical slot, active, with a restart_lsn
that is current. Not it either. I also check for prepared transactions. pg_prepared_xacts is empty.
It is 16:20 by now. Latency on the list endpoint is 780 ms and the incident channel has gone quiet. Then Arjun asks the question that matters: what does the replica look like? The BI replica is the one thing in this setup that is not on the primary.
the find
There it is. pg_stat_replication.backend_xmin is the oldest transaction the standby needs the
primary to keep. It is 4.9 million transactions behind, which matches the removable cutoff in the
vacuum log exactly. On the replica, hot_standby_feedback = on was set months ago, to stop
query cancellations ("canceling statement due to conflict with recovery") on the BI dashboards. With
feedback on, the standby reports its oldest running snapshot to the primary, and the primary
refuses to vacuum anything newer.
And the oldest running snapshot on the replica was a query from the data team, started at 09:26:
A cohort report with a bad join, six hours in and still running. Every row version created on the primary since 09:26 had to be kept for that query's sake. The primary is an order system with heavy updates, so that meant a lot of row versions, and the ramp in latency was just the bloat growing linearly with them.
the fix
Immediate: SELECT pg_terminate_backend(18842) on the replica, at 16:31. The next autovacuum on
the primary finished in four minutes and reported 41 million tuples removed. The list endpoint dropped to
60 ms straight away, but the index pages stayed bloated, so we ran
REINDEX INDEX CONCURRENTLY on the two hot indexes that night. After that it was back to 38 ms.
Permanent: turn feedback off for this replica and accept cancellations as the cost, but bound them so nobody is surprised.
The tradeoff is real. With feedback off, long BI queries that touch recently updated rows will be cancelled with a recovery conflict. The data team accepted that. For the reports that genuinely need an hour, they now run against a nightly snapshot restored from a base backup, not the live standby.
the aftermath
The incident ran from 09:30 to 16:35, about seven hours. At peak, orders had 11.8 million dead
tuples against 6.1 million live, order_events was 2.1 times its normal size on disk, and
GET /orders p95 reached 910 ms. No orders were lost, but the mobile app's order history screen
timed out for roughly 3,400 sessions.
-
A Prometheus alert on
max(age(backend_xmin))frompg_stat_replicationfires at 500,000 transactions, well before the bloat is user-visible. - A second alert on the ratio of dead to live tuples for the five hottest tables, at 0.5 for 15 minutes.
- The vacuum log line "dead but not yet removable" is now shipped to Datadog as a metric. That one log line would have saved us an hour.
-
The runbook for "latency ramps with no deploy" now starts with
pg_stat_replication, not the planner.
I spent an hour staring at the table. Vacuum on a primary can be blocked by a query that isn't on the primary at all, and when autovacuum runs and removes nothing, the useful question is who else holds a snapshot.