DEV Community

Cover image for Autovacuum ran 443,863 times and cleaned nothing: orphan queries on a read replica

Autovacuum ran 443,863 times and cleaned nothing: orphan queries on a read replica

Sergey Stebakov on December 01, 2026

Originally published at stebas.com. At night, deposits in a payments service started getting stuck in pending. The money had already been taken by...
Collapse
 
hritik_chaudhary_5db0056f profile image
Hritik Chaudhary •

Good diagnosis order. One design option beyond statement_timeout: run the admin and segment-builder queries on their own replica with hot_standby_feedback off. Long queries there get cancelled by recovery conflicts when they collide with vacuum cleanup (max_standby_streaming_delay sets how patient it is), but they can no longer pin the primary's horizon. The replicas serving latency-sensitive reads keep feedback on. You trade some cancelled reports for a primary that vacuum can always clean. For the alert, pg_stat_replication.backend_xmin on the primary shows the horizon each replica reports, so one query covers all of them.

Collapse
 
stebas profile image
Sergey Stebakov •

Agree, separate replica with feedback off is a good option. But for this query I think it should not be on postgres at all. Count over all users is analytics work.

One note about backend_xmin. If replica uses replication slot, backend_xmin is NULL and xmin is in pg_replication_slots. I checked it on PG 17 with test replica to be sure. In our case replicas had slots, so we found it there. Better to check both.

Collapse
 
hritik_chaudhary_5db0056f profile image
Hritik Chaudhary •

Good catch, I only had the no-slot case in mind. With a slot the feedback xmin sits in pg_replication_slots, so a check that reads only pg_stat_replication.backend_xmin will show NULL and miss the standby. Worth querying both. Agreed on the count too: all-users analytics belongs in a warehouse or a materialized rollup, not on the replica.

Collapse
 
stebas profile image
Sergey Stebakov •

Here is the test on PG 17, replica with slot:

Collapse
 
lycore profile image
Lycore Development •

Good diagnosis path, and the point that alive=1 is not the same as keeping up is the part many teams miss. With hot_standby_feedback on, a runaway replica query holding back the primary's xmin is nasty. Would you consider setting max_standby_streaming_delay or a statement_timeout on the replica role used by the admin segment builder? That would cap the damage even if another filter matches nearly every user.

Collapse
 
stebas profile image
Sergey Stebakov •

Thanks. statement_timeout on the read-only role yes, this is the plan, it caps the query no matter what filter somebody builds.

max_standby_streaming_delay will not help here. With hot_standby_feedback on, vacuum on the primary does not remove rows the replica still needs, so there is no recovery conflict and nothing to cancel. The query can run forever. That setting only works when feedback is off.

Collapse
 
kevinhall profile image
Kevin Hall •

one catch with putting statement_timeout on the role is that only new sessions pick it up, so long lived pool connections run without it until they get recycled

Collapse
 
userxnom profile image
Abdullah Naseer •

Sergey, the number that reframes the whole story for me is 443,863 โ€” autovacuum running constantly and removing nothing is the kind of metric that looks like effort and is actually a stuck horizon. The detail I would steal is the diagnosis order: sessions, then slots, then prepared transactions. Most people would start by tuning vacuum.

One thing that stuck with me: the slot age(xmin) of 9.3 million was visible from the primary the whole time โ€” pg_replication_slots lives there โ€” so the monitoring gap was not where the data lived, it was which number got an alert. Longest transaction at 30โ€“57 seconds looked calm because the 17-hour queries were sitting on the replicas.

And the orphaned application_name is the part that would keep me up at night: a ReplicaSet that no longer exists, still holding nine count(*) queries open for 17 hours. A client timeout clearly fired and nobody noticed, because the server kept running.

One question: after this, did you put a guard on the segment-builder role itself โ€” a statement_timeout, or a periodic check for sessions whose application_name no longer matches a live deployment โ€” or is the slot age(xmin) alert the only tripwire now?

Collapse
 
stebas profile image
Sergey Stebakov •

Thanks. You are right, the slot xmin was on the primary all the time, we just did not alert on it.

About your question. Not yet. Now we have alerts on the outbox (age of oldest event and relay rate) and a runbook for on-call: if vacuum runs all the time but dead tuples do not go down, look who holds xmin. statement_timeout for the read-only role and xmin alert on replicas are in the plan, not done yet. I need to push this.

Checking application_name against live pods is a good idea, I did not think about it. Will add it.

Collapse
 
syedfahads profile image
Syed Fahad •

did you look at partitioning the outbox and dropping old partitions? a pinned xmin can stop vacuum but it can't stop a drop

Collapse
 
hongke_shop profile image
HONGKE •

Great write-up, that debugging story was really interesting to follow!

Collapse
 
stebas profile image
Sergey Stebakov •

Thanks, glad you liked it!