15:12 IST, Wednesday. Datadog: p95 on
GET /ordersis 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))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.
Top comments (1)
Careful with the vacuum_defer_cleanup_age line if anyone copies that block onto Postgres 16+, it was removed there. On 17.12 a reload logs "contains errors; no changes were applied", so hot_standby_feedback = off silently doesn't take effect, and a restart fails with FATAL.