How a Six-Hour Analytics Query on a Replica Stopped Vacuum on the Primary and Tripled Order Latency
← Back
November 25, 2026Database8 min read

How a Six-Hour Analytics Query on a Replica Stopped Vacuum on the Primary and Tripled Order Latency

Published November 25, 20268 min read

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:

-- last week Index Scan using orders_customer_id_created_at_idx on orders Buffers: shared hit=31 Execution Time: 0.612 ms -- now Index Scan using orders_customer_id_created_at_idx on orders Buffers: shared hit=2,904 read=611 Execution Time: 71.8 ms

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:

SELECT relname, n_live_tup, n_dead_tup, last_autovacuum, autovacuum_count FROM pg_stat_user_tables WHERE relname IN ('orders', 'order_events'); relname | n_live_tup | n_dead_tup | last_autovacuum --------------+------------+------------+----------------------------- orders | 6104220 | 11870342 | 2026-11-25 15:04:51+05:30 order_events | 19330871 | 41290114 | 2026-11-25 15:06:12+05:30

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:

tuples: 0 removed, 6104220 remain, 11870342 are dead but not yet removable removable cutoff: 88213401, which was 4,902,118 XIDs old when operation ended

"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:

SELECT pid, state, xact_start, backend_xmin FROM pg_stat_activity WHERE backend_xmin IS NOT NULL ORDER BY age(backend_xmin) DESC LIMIT 5;

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

SELECT application_name, state, backend_xmin, age(backend_xmin) AS xmin_age FROM pg_stat_replication; application_name | state | backend_xmin | xmin_age ------------------+-----------+--------------+---------- bi-replica-1 | streaming | 83311283 | 4902118

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:

SELECT pid, usename, now() - xact_start AS running_for, left(query, 60) FROM pg_stat_activity WHERE state = 'active' ORDER BY xact_start LIMIT 1; pid | usename | running_for | left -------+------------+-------------+--------------------------------------------- 18842 | bi_reports | 06:03:41 | SELECT ... FROM order_events e JOIN orders o ...

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.

# postgresql.conf on bi-replica-1 hot_standby_feedback = off max_standby_streaming_delay = 120s # replay waits up to 2 min for a conflicting query vacuum_defer_cleanup_age = 0 # (primary) leave at default -- role-level cap so one report cannot run for six hours ALTER ROLE bi_reports SET statement_timeout = '20min'; ALTER ROLE bi_reports SET idle_in_transaction_session_timeout = '5min';

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)) from pg_stat_replication fires 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.

Share this
← All Posts8 min read