<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sergey Stebakov</title>
    <description>The latest articles on DEV Community by Sergey Stebakov (@stebas).</description>
    <link>https://dev.to/stebas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4275531%2F96e4849c-078b-4697-a3ad-43dc8ca6ece9.jpg</url>
      <title>DEV Community: Sergey Stebakov</title>
      <link>https://dev.to/stebas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stebas"/>
    <language>en</language>
    <item>
      <title>Autovacuum ran 443,863 times and cleaned nothing: orphan queries on a read replica</title>
      <dc:creator>Sergey Stebakov</dc:creator>
      <pubDate>Tue, 01 Dec 2026 05:47:51 +0000</pubDate>
      <link>https://dev.to/stebas/autovacuum-ran-443863-times-and-cleaned-nothing-orphan-queries-on-a-read-replica-5af6</link>
      <guid>https://dev.to/stebas/autovacuum-ran-443863-times-and-cleaned-nothing-orphan-queries-on-a-read-replica-5af6</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://stebas.com/posts/vacuum-cannot-clean-xmin-pinned-by-replica/?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=crosspost" rel="noopener noreferrer"&gt;stebas.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;At night, deposits in a payments service started getting stuck in &lt;code&gt;pending&lt;/code&gt;. The money had already been taken by the payment provider, the webhook had been accepted, and the balance was not credited. The delay kept growing until it peaked at &lt;strong&gt;85 minutes&lt;/strong&gt;. Nothing was lost in the end. But that was a long night for support and for players.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pipeline, and the first lie
&lt;/h2&gt;

&lt;p&gt;The service uses a transactional outbox. The webhook handler writes an event to an outbox table in the same transaction as the business change. A relay worker reads the table in order, publishes to Kafka and deletes the rows it has sent. The wallet consumes the event and credits the balance.&lt;/p&gt;

&lt;p&gt;The relay's health metric said &lt;code&gt;alive = 1&lt;/code&gt; the whole night, while the queue grew to &lt;strong&gt;560,000&lt;/strong&gt; events. &lt;strong&gt;Alive is not the same as keeping up.&lt;/strong&gt; The signals that mattered were the age of the oldest pending event (normal is 1–2 seconds, it reached 5,100) and the publish rate compared with the same hour on previous nights. The publish rate was half of normal, and incoming traffic was flat. So this was not a flood: the relay had slowed down.&lt;/p&gt;

&lt;p&gt;The relay's logs showed its own queries to the outbox table timing out.&lt;/p&gt;

&lt;h2&gt;
  
  
  A table that vacuum could not clean
&lt;/h2&gt;

&lt;p&gt;On the primary, the table statistics told the story:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;n_live_tup&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n_dead_tup&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;autovacuum_count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;pg_size_pretty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;pg_total_relation_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;relid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pg_stat_user_tables&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;relname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'outbox_message'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;About 0.5 million live rows, &lt;strong&gt;10.8 million dead&lt;/strong&gt; ones and 9.7 GB on disk for a table that is normally a few hundred megabytes. &lt;code&gt;autovacuum_count&lt;/code&gt; was &lt;strong&gt;443,863&lt;/strong&gt; and went up every time I looked. Vacuum was working constantly, and it still could not remove anything.&lt;/p&gt;

&lt;p&gt;When vacuum runs and removes nothing, something is holding the &lt;strong&gt;xmin horizon&lt;/strong&gt;: an old snapshot that might still need those row versions. A table that inserts and deletes every second turns into a field of dead tuples that every ordered &lt;code&gt;SELECT&lt;/code&gt; has to walk through.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three places to look, in order
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Long or &lt;code&gt;idle in transaction&lt;/code&gt; sessions on the primary (&lt;code&gt;pg_stat_activity&lt;/code&gt;, &lt;code&gt;backend_xmin&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Replication slots (&lt;code&gt;pg_replication_slots&lt;/code&gt;, &lt;code&gt;age(xmin)&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Forgotten two-phase transactions (&lt;code&gt;pg_prepared_xacts&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The primary was clean. The slots were not: both physical slots, one per read replica, had &lt;code&gt;age(xmin)&lt;/code&gt; around &lt;strong&gt;9.3 million&lt;/strong&gt;. The database ran with &lt;code&gt;hot_standby_feedback&lt;/code&gt; on. Each replica reports the oldest snapshot it still needs back to the primary, so a long query on a &lt;strong&gt;replica&lt;/strong&gt; stops vacuum on the &lt;strong&gt;primary&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;On the replicas I found nine &lt;code&gt;SELECT count(*)&lt;/code&gt; queries that had been running for &lt;strong&gt;17 hours&lt;/strong&gt;. Their &lt;code&gt;application_name&lt;/code&gt; pointed to a ReplicaSet that no longer existed. They came from an admin segment builder: an operator had built a filter that matched almost every user. The HTTP request returned a 500 after 60 seconds, the operator retried, and each retry started another query. The application gave up but never cancelled the query in the database. Restarting or replacing the pod does not stop a query that is already running on the server either.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix took seconds
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;pg_cancel_backend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pg_stat_activity&lt;/span&gt;
 &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;application_name&lt;/span&gt; &lt;span class="k"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;dead-replicaset&amp;gt;-%'&lt;/span&gt;
   &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;xact_start&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="kt"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'1 hour'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I ran this on each replica, with a narrow filter, after writing down the pids and the query texts. The slots' &lt;code&gt;age(xmin)&lt;/code&gt; dropped from 9.3 million to double digits. The next vacuum pass finished, and the backlog went from 510,000 to 11 in about two minutes. All delayed deposits were published.&lt;/p&gt;

&lt;p&gt;What I deliberately did &lt;strong&gt;not&lt;/strong&gt; do: restart the relay (the backlog lives in the table, and an interrupted batch can duplicate messages), run &lt;code&gt;VACUUM FULL&lt;/code&gt; (an exclusive lock that stops the relay completely), or delete rows from the outbox (they are undelivered payments).&lt;/p&gt;

&lt;h2&gt;
  
  
  Why monitoring missed it
&lt;/h2&gt;

&lt;p&gt;The Postgres exporter scraped only the primary. Its longest-transaction metric showed 30–57 seconds while the replicas held 17-hour queries. The first alert that fired was the outbox age alert, hours after the horizon was pinned.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Vacuum that runs and removes nothing is a pinned horizon, not a slow vacuum.&lt;/strong&gt; Check sessions, slots and prepared transactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;With &lt;code&gt;hot_standby_feedback&lt;/code&gt;, a replica can block vacuum on the primary.&lt;/strong&gt; Monitor the replicas, not only the writer, and alert on slot &lt;code&gt;age(xmin)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A client timeout does not cancel a server query.&lt;/strong&gt; Set &lt;code&gt;statement_timeout&lt;/code&gt; on read-only roles. A query should not outlive the request that started it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Alive" is not throughput.&lt;/strong&gt; For queues, alert on the age of the oldest item and compare rates with yesterday, not with zero.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>postgres</category>
      <category>sre</category>
      <category>database</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
