{"id":113458,"date":"2026-11-23T12:00:00","date_gmt":"2026-11-23T12:00:00","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=113458"},"modified":"2026-10-22T13:36:54","modified_gmt":"2026-10-22T13:36:54","slug":"postgresql-bloat-after-vacuum","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/postgresql-bloat-after-vacuum\/","title":{"rendered":"PostgreSQL bloat after VACUUM: why it happens, and how to fix it"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong><code>VACUUM<\/code> in <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL<\/a> makes the space held by dead row versions reusable, but doesn\u2019t shrink tables, compact half-empty pages, or remove rows an older transaction can still see. <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s why tables and indexes keep growing, and queries keep slowing down, even when <code>autovacuum<\/code> is running and <code>n_dead_tup<\/code> looks healthy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your busiest tables keep getting bigger, sequential scans read more pages every month, and a query that took 20ms last quarter now takes 60 (with nobody having changed anything), this is the likely reason.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This article covers four causes of bloat that health checks often miss, plus where <code>fillfactor<\/code> helps, all reproduced on a PostgreSQL 18.4 server with a workload you can run on a laptop. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For each one, you\u2019ll <strong>see what happens inside the table and its indexes, how to spot it with tools PostgreSQL already ships, and what to do about it. <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The final section is a remediation playbook: <strong>which settings to change per table, what to monitor, and how to rebuild a bloated table or index safely.<\/strong><\/p>\n\n\n\n<h2 id=\"h-postgresql-bloat-after-vacuum-the-four-causes-at-a-glance\" class=\"wp-block-heading\">PostgreSQL bloat after <code>VACUUM<\/code>: the four causes at a glance<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Symptom<\/th><th>What you see<\/th><th>Why <code>VACUUM<\/code> <em>doesn\u2019t<\/em> fix it<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>1. Table stays the same size<\/td><td><code>n_dead_tup<\/code> is 0, but free space is high and full scans read the same number of pages<\/td><td><code>VACUUM<\/code> marks space reusable but doesn\u2019t move rows. It only truncates empty pages at the end of the file<\/td><td>Usually nothing. After a one-off mass delete, rewrite with <code>pg_repack<\/code> or <code>VACUUM FULL<\/code><\/td><\/tr><tr><td>2. Indexes keep growing<\/td><td>Index size stays flat or grows, leaf density drops (as low as 9.2%) and deleted pages pile up<\/td><td>A B-tree page is only freed when every entry on it is gone. Timestamp-style keys never refill old gaps<\/td><td><code>REINDEX INDEX CONCURRENTLY<\/code>, and stop indexing columns that change on every update<\/td><\/tr><tr><td>3. <code>autovacuum<\/code> falls behind<\/td><td>Dead rows build to hundreds of thousands before a run starts<\/td><td>Trigger is 50 + 20% of rows, and the launcher only checks once per <code>autovacuum_naptime<\/code><\/td><td>Per-table scale factor (0.01\u20130.05), higher cost limit, shorter naptime<\/td><\/tr><tr><td>4. One old transaction blocks cleanup<\/td><td><code>VACUUM<\/code> reports rows \u201cdead but not yet removable\u201d. Heap and indexes grow<\/td><td>The xmin horizon applies to the whole database<\/td><td>End the session or drop the slot, and set <code>idle_in_transaction_session_timeout<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 id=\"h-what-update-and-delete-leave-behind-in-postgresql\" class=\"wp-block-heading\">What <code>UPDATE<\/code> and <code>DELETE<\/code> leave behind in PostgreSQL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Everything in this article follows from one design decision, so it&#8217;s worth being precise about it before we look at any numbers. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Firstly, PostgreSQL never changes a row in place. When you run an <code>UPDATE<\/code>, it writes a complete new version of the row, and marks the old version as expired by your transaction. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then, a <code>DELETE<\/code> simply marks the current version as expired, and the old versions stay exactly where they were &#8211; in the same 8 kB page &#8211; because other transactions that started earlier may still need to see them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/multi-version-concurrency-control-mvcc-in-postgresql-learning-postgresql-with-grant\/\" target=\"_blank\" rel=\"noreferrer noopener\">multi-version concurrency control (MVCC)<\/a>, and it&#8217;s the reason <strong>readers never block writers in PostgreSQL<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once no running transaction can possibly see an old version any more, it becomes a dead tuple &#8211; and <strong>dead tuples are pure waste<\/strong>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They take up space in the page, they are still read by sequential scans, and indexes still point at them  &#8211; and that&#8217;s where <code>VACUUM<\/code> comes in.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>VACUUM<\/code> removes dead tuples<\/strong> and marks their space as reusable inside the same page, but it only returns space to the operating system when completely empty pages sit at the end of the file. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>page itself stays in the file, which doesn&#8217;t get smaller.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s the life of a single page through that cycle:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"979\" height=\"551\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-21.png\" alt=\"Figure 1: A heap page through DELETE, VACUUM and INSERT. VACUUM turns dead row versions into free space that new rows can reuse, but the page, and the file, stay the same size.\" class=\"wp-image-113459\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-21.png 979w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-21-300x169.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-21-768x432.png 768w\" sizes=\"auto, (max-width: 979px) 100vw, 979px\" \/><figcaption class=\"wp-element-caption\"><em>A heap page through <code>DELETE<\/code>, <code>VACUUM<\/code>, and <code>INSERT<\/code>. <code>VACUUM<\/code> turns dead row versions into free space that new rows can reuse, but the page, and the file, stay the same size.<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So, <em>vacuumed<\/em> means the dead space can be reused &#8211; but the table is still not compact, neither is it healthy. I&#8217;ll now explain where these two ideas drift apart.<\/p>\n\n\n\n<h2 id=\"h-how-we-tested-lab-setup-and-autovacuum-defaults\" class=\"wp-block-heading\">How we tested: lab setup and <code>autovacuum<\/code> defaults<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Everything that follows was run on PostgreSQL 18.4 (Homebrew build) on a MacBook with an Apple M5 Max (48GB memory) using the server\u2019s default configuration. <\/p>\n\n\n\n<h3 id=\"h-the-settings\" class=\"wp-block-heading\">The settings<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The settings that matter most here are the <code>autovacuum<\/code> defaults: <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>autovacuum_naptime = 60s<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>autovacuum_vacuum_scale_factor = 0.2<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>autovacuum_vacuum_threshold = 50<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>autovacuum_vacuum_cost_limit = -1<\/code> (which falls back to <code>vacuum_cost_limit = 200<\/code>)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>autovacuum_vacuum_cost_delay = 2ms<\/code> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>autovacuum_max_workers = 3<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL 18 also added <code>autovacuum_vacuum_max_threshold<\/code>, which caps the trigger point at 100 million dead rows. <em>(Our tables are far smaller than that, so this never comes into play.)<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"941\" height=\"450\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-7.jpg\" alt=\"image showing the server and autovacuum settings used for this guide.\" class=\"wp-image-113460\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-7.jpg 941w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-7-300x143.jpg 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-7-768x367.jpg 768w\" sizes=\"auto, (max-width: 941px) 100vw, 941px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><em>The server and <code>autovacuum<\/code> settings used throughout this article. (Recorded terminal session.)<\/em><\/p>\n\n\n\n<h3 id=\"h-what-postgresql-contributor-extensions-do-i-need-to-follow-this-guide\" class=\"wp-block-heading\">What PostgreSQL contributor extensions do I need to follow this guide?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this guide, I&#8217;ll use three contributor extensions that ship with PostgreSQL, plus <code>pg_repack<\/code>:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li><code><a href=\"https:\/\/www.postgresql.org\/docs\/current\/pgstattuple.html\" target=\"_blank\" rel=\"noreferrer noopener\">pgstattuple<\/a><\/code> to precisely measure live, dead, and free space.<br><br><\/li>\n\n\n\n<li><code><a href=\"https:\/\/www.postgresql.org\/docs\/current\/pageinspect.html\" target=\"_blank\" rel=\"noreferrer noopener\">pageinspect<\/a><\/code> to look inside B-tree indexes.<br><br><\/li>\n\n\n\n<li><a href=\"https:\/\/www.postgresql.org\/docs\/current\/pgfreespacemap.html\" target=\"_blank\" rel=\"noreferrer noopener\"><code>pg_freespacemap<\/code><\/a> to read the free space map and, in the last section, <code><a href=\"https:\/\/github.com\/reorg\/pg_repack\" target=\"_blank\" rel=\"noreferrer noopener\">pg_repack<\/a><\/code> 1.5.3 to rebuild a table online.<\/li>\n<\/ul>\n<\/div>\n\n\n<div id=\"callout-block_b3b6b2bfd46b2e20a038a19c24401844\" class=\"callout alignnone\">\n    <div class=\"child-last:mb-0 child-first:mt-0 bg-gray-50 dark:bg-gray-950 p-4xl my-3xl\">\n\n<p class=\"wp-block-paragraph\"><strong>Where to find the lab repository<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The scripts, SQL, and load generator are in the <a href=\"https:\/\/github.com\/chandanshukla1989\/postgres-vacuum-bloat\" target=\"_blank\" rel=\"noreferrer noopener\">lab repository<\/a> (also linked to at the end of the guide), so you can reproduce every number on your own machine.<\/p>\n\n<\/div>\n<\/div> \n\n\n<p class=\"wp-block-paragraph\">To keep the measurements comparable, most of the examples use a small helper function that runs a query under <code>EXPLAIN<\/code> (<code>ANALYZE<\/code>, <code>BUFFERS<\/code>) and returns the number of 8 kB pages it touched. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Timings on a warm laptop cache move around a lot, unlike the number of pages a query has to read. This is what turns into <a href=\"https:\/\/en.wikipedia.org\/wiki\/Input\/output\" target=\"_blank\" rel=\"noreferrer noopener\">I\/O (input\/output)<\/a> on a real server.<\/p>\n\n\n\n<h2 id=\"h-symptom-1-a-vacuumed-postgresql-table-doesn-t-get-smaller\" class=\"wp-block-heading\">Symptom 1: a vacuumed PostgreSQL table doesn\u2019t get smaller<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You delete half the rows in a table and run <code>VACUUM<\/code>. The dead rows are gone, so you would expect the table to be roughly half the size, and a full scan to read roughly half as many pages &#8211; but that&#8217;s not what happens.<\/p>\n\n\n\n<h3 id=\"h-what-actually-happens-to-table-size-after-vacuum\" class=\"wp-block-heading\">What <em>actually<\/em> happens to table size after <code>VACUUM<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Shown below is a table with a scattered 50% delete. It&#8217;s a one-million-row orders table, with <code>autovacuum<\/code> switched <em>off<\/em> for <em>t<\/em>his table only (so we can control exactly when <code>VACUUM<\/code> runs.) We then delete every second row, and vacuum:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">CREATE TABLE orders (\n  id          bigint PRIMARY KEY,\n  customer_id int NOT NULL,\n  status      text NOT NULL,\n  amount      numeric(10,2) NOT NULL,\n  note        text NOT NULL,\n  created_at  timestamptz NOT NULL\n) WITH (autovacuum_enabled = off);\nCREATE INDEX orders_customer_idx ON orders (customer_id);\n\nINSERT INTO orders\nSELECT g, (random() * 50000)::int, 'shipped', round((random() * 500)::numeric, 2),\n       md5(g::text) || repeat('x', 60), now() - g * interval '1 second'\nFROM generate_series(1, 1000000) g;\nVACUUM ANALYZE orders;\n\nDELETE FROM orders WHERE id % 2 = 0;   -- 500,000 rows, spread across every page\nVACUUM (VERBOSE) orders;<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><code>VACUUM<\/code> reports that it did its work. The interesting lines of its output are:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">pages: 0 removed, 19231 remain, 19231 scanned (100.00% of total), 0 eagerly scanned\ntuples: 20 removed, 500000 remain, 0 are dead but not yet removable\nindex scan needed: 19231 pages from table (100.00% of total) had 500000 dead item identifiers removed<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Only 20 tuples were reported as removed because the full scan we ran straight after the <code>DELETE<\/code> had already pruned nearly all the dead rows inside their pages. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Note that PostgreSQL does this opportunistically when a query reads a page that is nearly full.<\/em> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>VACUUM<\/code> then finished the job by removing the 500,000 leftover item pointers and the matching index entries, and the dead rows are now gone. <\/p>\n\n\n\n<h4 id=\"h-now-look-at-what-the-table-looks-like-at-each-of-the-six-steps-measured-with-pgstattuple-and-the-page-count-helper\" class=\"wp-block-heading\">Now, look at what the table looks like at each of the six steps (measured with <code>pgstattuple<\/code> and the page-count helper):<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><td>Stage<\/td><td>Heap<\/td><td>Indexes<\/td><td>Live rows<\/td><td>Dead rows<\/td><td>Free space<\/td><td>Pages read by <code>SELECT count(*)<\/code>, <code>sum(amount)<\/code><\/td><\/tr><\/thead><tbody><tr><td>1. Loaded<\/td><td>150.2 MB<\/td><td>30.7 MB<\/td><td>1,000,000<\/td><td>0<\/td><td>0.7 %<\/td><td>19,231<\/td><\/tr><tr><td>2. Deleted half, before <code>VACUUM<\/code><\/td><td>150.2 MB<\/td><td>30.7 MB<\/td><td>500,000<\/td><td>500,000<\/td><td>0.7 %<\/td><td>19,231<\/td><\/tr><tr><td>3. After <code>VACUUM<\/code><\/td><td>150.2 MB<\/td><td>30.7 MB<\/td><td>500,000<\/td><td>0<\/td><td>49.0 %<\/td><td>19,231<\/td><\/tr><tr><td>4. After inserting 250,000 new rows<\/td><td>150.2 MB<\/td><td>36.1 MB<\/td><td>750,000<\/td><td>0<\/td><td>24.8 %<\/td><td>19,231<\/td><\/tr><tr><td>5. After emptying the last 20% of pages and <code>VACUUM<\/code><\/td><td>120.2 MB<\/td><td>36.1 MB<\/td><td>650,010<\/td><td>0<\/td><td>18.8 %<\/td><td>15,385<\/td><\/tr><tr><td>6. After <code>VACUUM FULL<\/code><\/td><td>97.7 MB<\/td><td>19.4 MB<\/td><td>650,010<\/td><td>0<\/td><td>0.7 %<\/td><td>12,501<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Stage 3 is the one that catches people out. <code>VACUUM<\/code> ran, dead rows are zero, and the table is <strong>exactly the same size: 150MB, now 49% empty.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A full scan still reads all 19,231 pages to find half as many rows, so every row now costs twice the I\/O it did before:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"368\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-22.png\" alt=\"image showing the heap size and pages read by a full scan of orders at each stage.\" class=\"wp-image-113462\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-22.png 940w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-22-300x117.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-22-768x301.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><figcaption class=\"wp-element-caption\"><em>Heap size and pages read by a full scan of orders at each stage. Only removing pages from the end of the file (stage 5) or rewriting the table (stage 6) makes either number go down.<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can see the same behavior in a couple of minutes on a smaller table. In the session shown below, a 200,000-row table is 27MB. Half its rows are deleted and the table is vacuumed: <code>pgstattuple<\/code> now counts 100,000 live rows, no dead ones, and 48.8% free space, and the table is still 27MB. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Only <code>VACUUM FULL<\/code> brings it down to 13MB:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"471\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-8.jpg\" alt=\"Image showing: dead rows go to zero after VACUUM, free space goes to 48.8%, and the size doesn't move until VACUUM FULL\" class=\"wp-image-113461\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-8.jpg 939w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-8-300x150.jpg 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-8-768x385.jpg 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><figcaption class=\"wp-element-caption\"><em>Dead rows go to zero after <code>VACUUM<\/code>, free space goes to 48.8%, and the size doesn&#8217;t move until <code>VACUUM FULL<\/code>. (Recorded terminal session.)<\/em><\/figcaption><\/figure>\n\n\n\n<h3 id=\"h-why-vacuum-can-t-shrink-the-table\" class=\"wp-block-heading\">Why <code>VACUUM<\/code> can&#8217;t shrink the table<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This happens because <strong><code>VACUUM<\/code> works page-by-page<\/strong>. Each page that held deleted rows now holds half as many live rows, and a <em>lot<\/em> of free space. This is confirmed below, showing the free space inside the table after <code>VACUUM<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">SELECT count(*) AS pages,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count(*) FILTER (WHERE avail &gt;= 4000) AS pages_half_or_more_free,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; round(avg(avail)) AS avg_free_bytes\nFROM pg_freespace('orders');\n\n&nbsp;pages | pages_half_or_more_free | avg_free_bytes\n-------+-------------------------+----------------\n&nbsp;19231 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 19231 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4000<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Every single page is now about half empty. The rows that survived are still spread across all 19,231 pages, so any query that has to look at all of them, or even a large range of them, reads twice as many pages as it would in a compact table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That space is not wasted forever, though. In stage 4, despite inserting 250,000 new rows, the table didn&#8217;t grow by a single page; PostgreSQL found room for <em>every one of them<\/em> in the gaps <code>VACUUM<\/code> had left. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s the good news &#8211; and is why a table that churns at a steady rate usually settles at a stable size instead of growing without limit.<\/p>\n\n\n\n<h4 id=\"h-how-does-plain-vacuum-give-space-back\" class=\"wp-block-heading\">How does plain <code>VACUUM<\/code> give space <em>back<\/em>?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The only way plain <code>VACUUM<\/code> gives space back to the operating system is by cutting off completely empty pages at the <em>end<\/em> of the file. <\/strong>Stage 5 shows it: we deleted every row stored in the last 20% of the file and then <code>VACUUM<\/code> reported truncated 19231 to 15385 pages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL doesn&#8217;t move rows around during a normal <code>VACUUM<\/code>, so any empty pages in the middle of the file can&#8217;t be returned. To actually compact the table you need to rewrite it &#8211; exactly what  <code>VACUUM FULL<\/code> did in stage 6.<\/p>\n\n\n\n<h3 id=\"h-how-to-spot-table-bloat-when-n-dead-tup-is-zero\" class=\"wp-block-heading\">How to spot table bloat when <code>n_dead_tup<\/code> is zero<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>n_dead_tup<\/code> is zero, so is of no help here. The signal is free space (a table whose <code>pgstattuple<\/code> free percentage stays high, or whose size stays flat while its live row count falls.) <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On large tables, meanwhile, <code>pgstattuple<\/code> reads the whole relation &#8211; so for routine checks use <code>pgstattuple_approx()<\/code>, which skips pages the visibility map says are all-visible (plus, is much cheaper!) <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll return to monitoring in the playbook at the end of the guide.<\/p>\n\n\n\n<h3 id=\"h-what-to-do-about-a-bloated-table\" class=\"wp-block-heading\">What to do about a bloated table<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Nothing, most of the time. A table that&#8217;s 20 or 30% free <em>and<\/em> stable in size is doing exactly what MVCC tables do, and the free space will be used by the next inserts. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The problem cases are a table that had a one-off mass delete and will never refill that space, or a table whose size is climbing enough that <code>VACUUM<\/code> can&#8217;t keep up (as detailed in Symptom 3 later), or is being blocked (Symptom 4). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the first case, a rewrite with <code>pg_repack<\/code> or <code>VACUUM FULL<\/code> is the answer. The playbook covers how to do that safely.<\/p>\n\n\n\n<h2 id=\"h-symptom-2-postgresql-indexes-keep-growing-after-vacuum\" class=\"wp-block-heading\">Symptom 2: PostgreSQL indexes keep growing after <code>VACUUM<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>VACUUM<\/code> cleans indexes as well as the table. When it removes a dead row, it also removes the index entries that pointed at it. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, after a big delete and a <code>VACUUM<\/code>, you&#8217;d expect the indexes to shrink along with the data &#8211; and index scans to get cheaper &#8211; but that&#8217;s not what happens&#8230;<\/p>\n\n\n\n<h3 id=\"h-what-actually-happens-to-a-b-tree-index-after-deletes-and-updates\" class=\"wp-block-heading\">What <em>actually<\/em> happens to a B-tree index after deletes and updates<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As shown below, it builds an events table with a million rows and an index on <code>created_at<\/code>, then runs the kind of retention job many systems have: keep one row in ten, delete the rest, and vacuum:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">CREATE TABLE events (id bigint PRIMARY KEY, created_at timestamptz NOT NULL, payload text NOT NULL)\n&nbsp; WITH (autovacuum_enabled = off);\nINSERT INTO events\nSELECT g, timestamptz '2026-01-01' + g * interval '1 second', repeat('e', 40)\nFROM generate_series(1, 1000000) g;\nCREATE INDEX events_created_idx ON events (created_at);\nVACUUM ANALYZE events;\n\nDELETE FROM events WHERE id % 10 &lt;&gt; 0;&nbsp;&nbsp; -- keep 1 row in 10\nVACUUM ANALYZE events;\n\nSELECT pg_relation_size('events_created_idx') \/ 8192 AS pages, leaf_pages, deleted_pages,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; round(avg_leaf_density::numeric, 1) AS avg_leaf_density\nFROM pgstatindex('events_created_idx');<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pgstatindex<\/code> (from the <code>pgstattuple<\/code> extension) describes the B-tree page-by-page. <em>Before<\/em> the delete, the index had 2,745 pages and its leaf pages were 90% full. <em>After<\/em> the delete and <code>VACUUM<\/code>, however, it <strong>still had 2,745 pages, and they were 9.2% full.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Additionally, a query that counts the rows in a three-day window read 715 pages <em>before<\/em> the delete, and 712 <em>after<\/em> &#8211; even though it now returns a tenth as many rows. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After <code>REINDEX INDEX CONCURRENTLY<\/code>, the same index was 276 pages and the same query read 74. <\/p>\n\n\n\n<h4 id=\"h-here-are-the-three-states\" class=\"wp-block-heading\">Here are the three states:<\/h4>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"528\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-23.png\" alt=\"Image showing a timestamp index before and after a retention delete\" class=\"wp-image-113466\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-23.png 939w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-23-300x169.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-23-768x432.png 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><figcaption class=\"wp-element-caption\"><em>The <code>created_atindex<\/code> BEFORE and AFTER a retention delete. <code>VACUUM<\/code> removes the dead entries but leaves each leaf page in place, so a range scan walks the same number of pages. Only a rebuild packs the survivors together.<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Shown below is a repeat of the experiment interactively on a copy of the table. The index stays at 21MB and 2,733 leaf pages after the delete and <code>VACUUM<\/code>, while leaf density drops from 90% to 9.2%. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>REINDEX INDEX CONCURRENTLY<\/code> brings it down to 2,208kB:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"471\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-9.jpg\" alt=\"Image showing the index keeping its size after a retention delete and VACUUM, while only a rebuild shrinks it.\" class=\"wp-image-113463\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-9.jpg 939w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-9-300x150.jpg 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-9-768x385.jpg 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><figcaption class=\"wp-element-caption\"><em>The index keeps its size after a retention delete and <code>VACUUM<\/code>; only a rebuild shrinks it. (Recorded terminal session.)<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The second pattern is more common and harder to see. As shown below, it updates 20% of an accounts table in each of ten rounds and changes <code>updated_at<\/code> every time &#8211; exactly what an application does when it stamps every row it touches. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s also an index on <code>updated_at<\/code>, and <code>VACUUM<\/code> runs after every round:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">UPDATE accounts\nSET balance = balance + 1, updated_at = clock_timestamp()\nWHERE id % 5 = :round % 5;\nVACUUM accounts;<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The index grew from 551 to 1,208 pages over the first six rounds and then stopped growing. Stopping sounds like good news until you look at what <code>pgstatindex<\/code> says about it:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">&nbsp;&nbsp;&nbsp; stage&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp; size&nbsp;&nbsp; | pages | leaf_pages | deleted_pages | avg_leaf_density\n--------------+---------+-------+------------+---------------+------------------\n&nbsp;B0 fresh&nbsp;&nbsp;&nbsp;&nbsp; | 4408 kB |&nbsp;&nbsp; 551 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 547 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 90.0\n&nbsp;B6 round 6&nbsp;&nbsp; | 9664 kB |&nbsp; 1208 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 547 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 657 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 89.9\n&nbsp;B10 round 10 | 9664 kB |&nbsp; 1208 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 547 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 656 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 89.9<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The live part of the index is the same 547 leaf pages it started with, packed to the same 90% density. <strong>More than half the file (656 pages) is made up of pages that <code>VACUUM<\/code> emptied and marked as deleted.<\/strong> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL will reuse them for future splits in this index, which is why growth levelled off, but it will never hand them back without a rebuild.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is the index version of Symptom 1, and it&#8217;s well-hidden &#8211; the density figure most bloat queries look at say 90%.<\/p>\n\n\n\n<h3 id=\"h-why-b-tree-pages-aren-t-reclaimed\" class=\"wp-block-heading\">Why B-tree pages aren\u2019t reclaimed<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A B-tree only frees a leaf page when every entry on it is gone. When some entries are deleted and some survive, the page keeps its place in the tree with whatever is left. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inserts can only refill the gaps if new keys belong in that part of the key range &#8211; and, for a timestamp index, they never do. New keys <em>always<\/em> go to the right-hand end, so the emptied pages on the left stay emptied.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s one piece of genuinely good news here, though, and it shows up in the third and fourth runs of the same experiment. PostgreSQL has two defenses against index growth from updates that do <em>not<\/em> change any indexed column.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They are <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/fillfactor-in-postgresql-what-it-means-and-when-to-adjust-it\/#:~:text=%E2%80%98HOT%E2%80%99%20updates%20in%20PostgreSQL\" target=\"_blank\" rel=\"noreferrer noopener\"><code>HOT<\/code> (heap-only tuple) updates<\/a> (which avoid creating index entries entirely) and, since PostgreSQL 14, <a href=\"https:\/\/www.postgresql.org\/docs\/16\/btree-implementation.html#BTREE-DELETION:~:text=67.4.2.%C2%A0Bottom%2Dup%20Index%20Deletion%20%23\" target=\"_blank\" rel=\"noreferrer noopener\">bottom-up index deletion<\/a>, which removes entries for old row versions when a leaf page is about to split. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Both need the old row versions to be dead already.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s a comparison of the three patterns:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"431\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-24.png\" alt=\"Image showing the index on updated_atunder three update patterns.\" class=\"wp-image-113465\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-24.png 940w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-24-300x138.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-24-768x352.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><figcaption class=\"wp-element-caption\"><em>The index on <code>updated_atunder<\/code> three update patterns. Changing the indexed column doubles the index, whereas leaving it alone costs nothing if the work is committed in small transactions.<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With one big <code><a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/making-temporal-databases-work-part-3-saving-data\/#:~:text=UPDATE%20is%20logically%20a%20delete%2Dthen%2Dinsert%3A%20close%20the%20old%20period%20and%20insert%20a%20new%20row%20with%20updated%20values%20and%20a%20new%20validity%20period\" target=\"_blank\" rel=\"noreferrer noopener\">UPDATE<\/a><\/code> per round, the first round doubled the index to 1,099 pages. Every table page was full, so each new row version went to a different page and needed a new index entry. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, the old versions weren&#8217;t dead yet, since the statement that made them hadn&#8217;t committed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the same work was done as 40 transactions of 1,000 rows each, 96% of the updates were <code>HOT<\/code> (383,604 of 400,000), and the index didn&#8217;t grow by a single page. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The bottom line is, the shape of your write transactions matters just as much as the schema.<\/p>\n\n\n\n<h3 id=\"h-how-to-spot-index-bloat-with-pgstatindex\" class=\"wp-block-heading\">How to spot index bloat with <code>pgstatindex<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Compare an index\u2019s size with the number of pages it actually needs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, <code>pgstatindex<\/code> gives you <code>leaf_pages<\/code>, <code>deleted_pages<\/code>, and <code>avg_leaf_density<\/code>. Here, <code>deleted_pages<\/code> is a large share of the total and is carrying dead weight wherever density is low. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, watch for indexes that have grown much faster than their table, and\/or range scans whose buffer counts climb while the number of rows they return does not. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On very large indexes, <code>pgstatindex<\/code> reads the whole index, so run it off-peak or on a replica.<\/p>\n\n\n\n<h3 id=\"h-how-to-fix-index-bloat\" class=\"wp-block-heading\">How to fix index bloat<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Rebuild the index with <code>REINDEX INDEX CONCURRENTLY<\/code>, which builds a fresh copy alongside the old one and swaps it in <em>without<\/em> blocking reads or writes. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Just be aware that it needs enough disk for a second copy of the index, it takes longer than a plain <code>REINDEX<\/code>, and it&#8217;ll leave an invalid index behind (which you must drop) if it fails part-way. The playbook covers all these details.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Longer term, take frequently changing columns such as <code>updated_at<\/code> <em>out<\/em> of indexes (unless a query <em>really<\/em> needs them), and prioritize many small committed transactions over having a single large one on <code>HOT<\/code> tables.<\/p>\n\n\n\n<h2 id=\"h-symptom-3-autovacuum-is-running-but-falling-behind-on-busy-tables\" class=\"wp-block-heading\">Symptom 3: <code>autovacuum<\/code> is running, but falling behind on busy tables<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>autovacuum<\/code> is on, it runs regularly, and the logs show it finishing. It&#8217;s therefore natural to assume it&#8217;s keeping every table (close to) clean, but that&#8217;s not actually happening.<\/p>\n\n\n\n<h3 id=\"h-what-actually-happens-the-autovacuum-trigger-grows-with-table-size\" class=\"wp-block-heading\">What <em>actually<\/em> happens: the <code>autovacuum<\/code> trigger grows with table size<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In reality, <code>autovacuum<\/code> doesn&#8217;t look at a table until it has accumulated enough dead rows to cross a threshold &#8211; and that threshold grows with the table:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">vacuum threshold = autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor x reltuples\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = 50 + 0.2 x (number of rows)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- with the defaults<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>(PostgreSQL 18 also caps the result at <code>autovacuum_vacuum_max_threshold<\/code> 100 million by default.)<\/em> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>For a table of two million rows, the default trigger is 400,050 dead rows.<\/strong> For a table of 200 million rows, it&#8217;s 40 million. The bigger and more important the table, the more garbage it&#8217;s allowed to collect before anybody looks at it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To see what that means in practice, the lab ran a steady load against a sessions table of two million rows (300MB of heap and 56MB of indexes). Four workers updated 2,000 random rows per second in small transactions, with each update setting <code>last_seen<\/code> and incrementing a counter. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It ran for seven minutes (first with the default settings and then with the per-table settings shown below), while a sampler recorded <code>pg_stat_user_tables<\/code> and <code>pg_stat_progress_vacuum<\/code> every five seconds:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">ALTER TABLE sessions SET (\n&nbsp; autovacuum_vacuum_scale_factor = 0.01,&nbsp;&nbsp; -- trigger at 1% dead rows instead of 20%\n&nbsp; autovacuum_vacuum_threshold&nbsp;&nbsp;&nbsp; = 1000,\n&nbsp; autovacuum_vacuum_cost_limit&nbsp;&nbsp; = 2000&nbsp;&nbsp;&nbsp; -- let this table's vacuum do 10x more work per cycle\n);<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"389\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-27.png\" alt=\"Image showing dead rows in the sessionstable under the same load.\" class=\"wp-image-113473\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-27.png 939w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-27-300x124.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-27-768x318.png 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><figcaption class=\"wp-element-caption\"><em>Dead rows in the <code>sessionstable<\/code> under the same load. With the defaults, <code>autovacuum<\/code> waited until nearly half a million dead rows had built up. With per-table settings, it ran every minute.<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, the dead rows climbed in a straight line for three and a half minutes with the defaults (in orange). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, once the count passed 400,050 (at about 206 seconds), <code>autovacuum<\/code> started. It needed roughly 45 seconds to work through the table, by which time the peak was 489,294 dead rows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As soon as it finished, the count started climbing again towards the next trigger. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With the per-table settings (in blue), <code>autovacuum<\/code> ran seven times in the same seven minutes. Each run took around five seconds, and the peak was 125,211.<\/p>\n\n\n\n<h4 id=\"h-here-s-seven-minutes-of-the-same-load\" class=\"wp-block-heading\">Here&#8217;s seven minutes of the same load:<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><td>&nbsp;<\/td><td>Default settings<\/td><td>Per-table settings<\/td><\/tr><\/thead><tbody><tr><td><code>autovacuum<\/code> runs<\/td><td>1<\/td><td>7<\/td><\/tr><tr><td>Peak dead rows<\/td><td>489,294<\/td><td>125,211<\/td><\/tr><tr><td>Dead rows at the end<\/td><td>404,327<\/td><td>82,899<\/td><\/tr><tr><td>Heap size, start to end<\/td><td>301 to 309 MB<\/td><td>301 to 306 MB<\/td><\/tr><tr><td>Index size, start to end<\/td><td>56 to 73 MB<\/td><td>56 to 73 MB<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">There are two things of note here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, the tuned table still carried about 120,000 dead rows before each run (far above its 21,000 trigger) &#8211; a result of <code>autovacuum_naptime<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The launcher only checks each database once a minute, and at 2,000 updates per second a minute\u2019s worth of changes is 120,000 dead rows. A per-table scale factor can&#8217;t make <code>autovacuum<\/code> look any more often than that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Second, the sizes barely differ after seven minutes; 840,000 updates spread over two million rows had plenty of free space to fall into! <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The difference is the trend. The default table is carrying three times as many dead rows as the tuned one, every page of which is read by every scan, and the next time it crosses 400,000 the cycle starts again. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And, over <em>days<\/em> rather than <em>minutes<\/em>, that&#8217;s exactly how a table drifts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The indexes grew by the same amount in both runs. Every update changed <code>last_seen<\/code>, which is indexed &#8211; so none of the 840,000 updates was <code>HOT<\/code>, and each one added an index entry no matter how often <code>VACUUM<\/code> ran. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With Symptom 2, we&#8217;ve already seen <em>why<\/em> that growth stays.<\/p>\n\n\n\n<h4 id=\"h-three-settings-decide-how-fast-autovacuum-can-keep-up-with-a-table\" class=\"wp-block-heading\">Three settings decide how fast <code>autovacuum<\/code> can keep up with a table:<\/h4>\n\n\n<div class=\"block-core-list\">\n<ol class=\"wp-block-list\">\n<li><strong>How much garbage it waits for<\/strong> (<code>autovacuum_vacuum_scale_factor<\/code> and <code>autovacuum_vacuum_threshold<\/code>).<br><br><\/li>\n\n\n\n<li><strong>How often it looks<\/strong> (<code>autovacuum_naptime<\/code>, <code>global<\/code>).<br><br><\/li>\n\n\n\n<li><strong>How fast it works once it starts<\/strong> (<code>autovacuum_vacuum_cost_limit<\/code> and <code>autovacuum_vacuum_cost_delay<\/code>).<\/li>\n<\/ol>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\" id=\"h-three-settings-decide-how-fast-autovacuum-can-keep-up-with-a-table-how-much-garbage-it-waits-for-autovacuum-vacuum-scale-factor-and-autovacuum-vacuum-threshold-how-often-it-looks-autovacuum-naptime-global-and-how-fast-it-works-once-it-starts-autovacuum-vacuum-cost-limit-and-autovacuum-vacuum-cost-delay-the-defaults-are-sensible-for-a-database-with-many-modest-tables-for-a-handful-of-large-hot-tables-they-are-far-too-relaxed-and-because-there-are-only-three-autovacuum-workers-by-default-a-few-slow-runs-on-big-tables-can-also-delay-the-others\">The defaults are sensible for a database with many modest tables. However, for a handful of large, <code>HOT<\/code> tables, they&#8217;re far too relaxed. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"h-three-settings-decide-how-fast-autovacuum-can-keep-up-with-a-table-how-much-garbage-it-waits-for-autovacuum-vacuum-scale-factor-and-autovacuum-vacuum-threshold-how-often-it-looks-autovacuum-naptime-global-and-how-fast-it-works-once-it-starts-autovacuum-vacuum-cost-limit-and-autovacuum-vacuum-cost-delay-the-defaults-are-sensible-for-a-database-with-many-modest-tables-for-a-handful-of-large-hot-tables-they-are-far-too-relaxed-and-because-there-are-only-three-autovacuum-workers-by-default-a-few-slow-runs-on-big-tables-can-also-delay-the-others\">This is because, with only three <code>autovacuum<\/code> workers by default, a few slow runs on big tables can delay the others.<\/p>\n\n\n\n<section id=\"my-first-block-block_2498aafe885775ff5a93f7761be6c31b\" class=\"my-first-block alignwide\">\n    <div class=\"bg-brand-600 text-base-white py-5xl px-4xl rounded-sm bg-gradient-to-r from-brand-600 to-brand-500 red\">\n        <div class=\"gap-4xl items-start md:items-center flex flex-col md:flex-row justify-between\">\n            <div class=\"flex-1 col-span-10 lg:col-span-7\">\n                <h3 class=\"mt-0 font-display mb-2 text-display-sm\">Get started with PostgreSQL &#8211; free book download<\/h3>\n                <div class=\"child:last-of-type:mb-0\">\n                                            &#8216;Introduction to PostgreSQL for the data professional&#8217;, written by Grant Fritchey and Ryan Booz, covers all the basics of how to get started with PostgreSQL.                                    <\/div>\n            <\/div>\n                                            <a href=\"https:\/\/www.red-gate.com\/hub\/books\/introduction-to-postgresql-for-the-data-professional\/\" class=\"btn btn--secondary btn--lg\" aria-label=\"Download your free copy: Get started with PostgreSQL - free book download\">Download your free copy<\/a>\n                    <\/div>\n    <\/div>\n<\/section>\n\n\n<h3 id=\"h-how-to-spot-autovacuum-falling-behind\" class=\"wp-block-heading\">How to spot <code>autovacuum<\/code> falling behind<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The early warning signs are visible in <code>pg_stat_user_tables<\/code> <em>long<\/em> before anyone notices slower queries. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s the query I would put on a <a href=\"https:\/\/www.red-gate.com\/hub\/product-learning\/redgate-monitor\/overview-postgres-monitoring-redgate-monitor\/\" target=\"_blank\" rel=\"noreferrer noopener\">dashboard<\/a>, showing the tables where <code>autovacuum<\/code> is falling behind:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">SELECT relname,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n_live_tup,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n_dead_tup,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; round(100.0 * n_dead_tup \/ nullif(n_live_tup + n_dead_tup, 0), 1) AS dead_pct,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; last_autovacuum,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; now() - last_autovacuum AS since_last_autovacuum,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; autovacuum_count,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pg_size_pretty(pg_total_relation_size(relid)) AS total_size\nFROM pg_stat_user_tables\nWHERE n_dead_tup &gt; 10000\nORDER BY n_dead_tup DESC\nLIMIT 20;<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>HOT<\/code> table whose dead percentage keeps climbing between runs, whose <code>last_autovacuum<\/code> is far older than you would expect for its write rate, or which appears in <code>pg_stat_progress_vacuum<\/code> for a large part of the day, is a table that <code>autovacuum<\/code> is <em>not<\/em> keeping up with.<\/p>\n\n\n\n<h3 id=\"h-how-to-tune-autovacuum-for-large-tables\" class=\"wp-block-heading\">How to tune <code>autovacuum<\/code> for large tables<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Tune the <code>HOT<\/code> tables individually rather than changing the global defaults. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A scale factor of 0.01 to 0.05 and a fixed threshold suited to the table\u2019s size is a good starting point, while a higher per-table cost limit lets each run finish quickly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your hottest tables change faster than one naptime can absorb, lower <code>autovacuum_naptime<\/code> for the whole server and consider more <code>autovacuum_max_workers<\/code>. Just remember that workers share the cost limit!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, make sure to look again at which columns you index. An index on a column that changes on every update will grow regardless of how aggressively you vacuum.<\/p>\n\n\n\n<h2 id=\"h-symptom-4-one-old-transaction-stops-vacuum-across-the-whole-database\" class=\"wp-block-heading\">Symptom 4: one old transaction stops <code>VACUUM<\/code> across the whole database<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ve tuned <code>autovacuum<\/code> for the <code>HOT<\/code> tables. It runs every minute and finishes quickly. The table <em>should<\/em> stay close to clean &#8211; but it doesn&#8217;t.<\/p>\n\n\n\n<h3 id=\"h-what-actually-happens-how-an-old-snapshot-blocks-dead-row-removal\" class=\"wp-block-heading\">What <em>actually<\/em> happens (how an old snapshot blocks dead-row removal)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>VACUUM<\/code> can only remove a dead row version when no running transaction could still need it. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL tracks the oldest snapshot that any session is still using &#8211; often called the <em>xmin horizon<\/em> &#8211; and <strong>every dead row newer than that horizon has to stay, in every table of the database<\/strong>, no matter how often <code>VACUUM<\/code> runs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In brief:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"528\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-26.png\" alt=\"Image showing where the new row version goes.\" class=\"wp-image-113470\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-26.png 939w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-26-300x169.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-26-768x432.png 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><figcaption class=\"wp-element-caption\"><em>While one old snapshot is open, the row versions that later updates leave behind are still visible to it, so <code>VACUUM<\/code> must keep them. Once it ends, they can all be removed together.<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To measure the effect, the lab repeated the tuned run from Symptom 3 with the exact same table and per-table settings, and the same 2,000 updates per second. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There was just one difference: a separate session opened a <code>REPEATABLE READ<\/code> transaction at 90 seconds, ran one small query to take its snapshot, and then sat idle until 240 seconds. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s the shape of a reporting job that opens a transaction and spends two and a half minutes thinking &#8211; or of an application that forgets to commit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A manual <code>VACUUM<\/code> revealed exactly what was going on just two minutes into that window:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">pages: 0 removed, 42693 remain, 42693 scanned (100.00% of total), 0 eagerly scanned\ntuples: 0 removed, 2219884 remain, 219892 are dead but not yet removable\nremovable cutoff: 1350952, which was 4401 XIDs old when operation ended<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">It scanned the whole table and removed nothing. The removable cutoff (1350952) is the snapshot of the idle report. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The recorded session shown below reproduces the same situation by hand on a smaller table, and ends with the query that finds the culprit:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"591\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-12.jpg\" alt=\"image showing a REPEATABLE READ transaction that reads the table once.\" class=\"wp-image-113469\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-12.jpg 939w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-12-300x189.jpg 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-12-768x483.jpg 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><figcaption class=\"wp-element-caption\"><em>Session 1 opens a <code>REPEATABLE READ<\/code> transaction and reads the table once. Session 2 updates a quarter of the rows, runs <code>VACUUM (VERBOSE)<\/code>, and then looks for the session holding the oldest <code>backend_xmin<\/code>. (Recorded terminal session.)<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 id=\"h-here-s-a-comparison-of-the-two-runs-over-the-full-seven-minutes\" class=\"wp-block-heading\">Here&#8217;s a comparison of the two runs over the full seven minutes:<\/h4>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"389\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-27.png\" alt=\"The same load and the same autovacuum settings, with and without a 150-second snapshot.\" class=\"wp-image-113472\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-27.png 939w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-27-300x124.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-27-768x318.png 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><figcaption class=\"wp-element-caption\"><em>The same load and the same <code>autovacuum<\/code> settings, with and without a 150-second snapshot.<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As shown above, dead rows follow the familiar one-minute sawtooth and the heap stays flat at 306MB <em>without<\/em> the long transaction (in blue).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>With<\/em> the long transaction (in orange), however, <code>autovacuum<\/code> still ran at 141 and 201 seconds, and both runs finished &#8211; but the dead-row count went straight through them and reached 351,374. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Updates that couldn&#8217;t find reusable space went into new pages, increasing the heap from 306 to 345MB and the indexes from 60 to 93MB while the snapshot was open.<\/p>\n\n\n\n<h4 id=\"h-what-happened-when-the-report-ended\" class=\"wp-block-heading\">What happened when the report ended?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When the report ended, the next <code>autovacuum<\/code> cleared the backlog in one pass, but <strong>the file sizes didn&#8217;t come back down<\/strong>. The run finished with 39MB more heap and 26MB more index than the run <em>without<\/em> the snapshot. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And, as Symptom 1 demonstrated, a plain <code>VACUUM<\/code> won&#8217;t return that space &#8211; which is why having one long transaction is worth considering.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Every table updated during those two and a half minutes carries the extra pages from then on. On a production system with a report that runs every hour, the damage accumulates!<\/p>\n\n\n\n<h3 id=\"h-why-the-xmin-horizon-is-database-wide\" class=\"wp-block-heading\">Why the xmin horizon is database-wide<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MVCC promises that a transaction sees a consistent snapshot of the database for as long as it needs one &#8211; and the only way to keep that promise is to keep every row version the snapshot might read. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL doesn&#8217;t know which tables the report will touch next, though, so the horizon applies to the whole database. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Long-running transactions are the most common culprit, but not the only one. It could be an abandoned replication slot, a forgotten prepared transaction, or a standby with <code>hot_standby_feedback<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When it comes to running a long query, they all hold the horizon back in the same way.<\/p>\n\n\n\n<h3 id=\"h-how-to-find-what-s-holding-back-cleanup\" class=\"wp-block-heading\">How to find what\u2019s holding back cleanup<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">All the usual suspects are listed in the command below. Run it whenever <code>VACUUM (VERBOSE)<\/code> reports rows that are <em>\u201cdead but not yet removable\u201d<\/em>, or put it on a dashboard and alert on the age:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">-- sessions, oldest snapshot first\nSELECT pid, usename, application_name, state, backend_xmin,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; age(backend_xmin) AS xmin_age,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; now() - xact_start AS transaction_age,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; left(query, 60) AS last_query\nFROM pg_stat_activity\nWHERE backend_xmin IS NOT NULL\nORDER BY age(backend_xmin) DESC\nLIMIT 10;\n\n-- replication slots, including ones nobody is reading any more\nSELECT slot_name, active, xmin, catalog_xmin, age(xmin) AS xmin_age\nFROM pg_replication_slots;\n\n-- prepared transactions that were never committed or rolled back\nSELECT gid, prepared, owner, database FROM pg_prepared_xacts;<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In the lab, the first query found the report straight away: a session idle in transaction, one minute and 49 seconds old, with a <code>backend_xmin<\/code> 4,397 transactions behind everyone else.<\/p>\n\n\n\n<h3 id=\"h-how-to-stop-long-transactions-blocking-vacuum\" class=\"wp-block-heading\">How to stop long transactions blocking <code>VACUUM<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Keep transactions short and let the server enforce it. <code>idle_in_transaction_session_timeout<\/code> ends sessions that sit idle inside an open transaction, and <code>transaction_timeout<\/code> (<a href=\"https:\/\/www.postgresql.org\/docs\/current\/runtime-config-client.html#:~:text=transaction_timeout%20(integer)\" target=\"_blank\" rel=\"noreferrer noopener\">new in PostgreSQL 17<\/a>) puts a ceiling on any transaction. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Set them per-role for applications and reporting users rather than globally, so that a legitimate batch job doesn&#8217;t get cut off.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You should also run long reports against a replica and, if that replica uses <code>hot_standby_feedback<\/code>, remember: its long queries now hold back cleanup on the primary! <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, drop replication slots you no longer need, and make monitoring on age (<code>backend_xmin<\/code>) as routine as monitoring on disk space.<\/p>\n\n\n\n<h2 id=\"h-when-lowering-fillfactor-reduces-bloat-and-when-it-doesn-t\" class=\"wp-block-heading\">When lowering <code>fillfactor<\/code> reduces bloat (and when it doesn&#8217;t)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A common piece of advice for update-heavy tables is to lower <code>fillfactor<\/code> so that each page keeps some free space for future versions of its rows. The hope is for more <code>HOT<\/code> updates, fewer index entries, and less bloat.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, that&#8217;s not the reality.<\/p>\n\n\n\n<div id=\"callout-block_b3b6b2bfd46b2e20a038a19c24401844\" class=\"callout alignnone\">\n    <div class=\"child-last:mb-0 child-first:mt-0 bg-gray-50 dark:bg-gray-950 p-4xl my-3xl\">\n\n<p class=\"wp-block-paragraph\"><em><strong>You may also be interested in&#8230;<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/fillfactor-in-postgresql-what-it-means-and-when-to-adjust-it\/\" target=\"_blank\" rel=\"noreferrer noopener\">What is fillfactor in PostgreSQL &#8211; and when should you adjust it?<\/a><\/p>\n\n<\/div>\n<\/div> \n\n\n<h3 id=\"h-what-actually-happens-how-fillfactor-changes-hot-updates-and-heap-size\" class=\"wp-block-heading\">What actually happens (how <code>fillfactor<\/code> changes <code>HOT<\/code> updates and heap size)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The advice is sound, but its value depends heavily on how you update. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As shown below, <code>HOT<\/code> is only possible when the new row version fits in the same page as the old one <em>and<\/em> no indexed column changes:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"528\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-26.png\" alt=\"An image showing the new version staying in the same page.\" class=\"wp-image-113471\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-26.png 939w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-26-300x169.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-26-768x432.png 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><figcaption class=\"wp-element-caption\"><em>With free space in the page, the new version stays in the same page and no index changes (a <code>HOT<\/code> update). When the page is full, the new version goes elsewhere, and every index gets a new entry.<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The lab loaded 200,000 rows into an items table at <code>fillfactor<\/code> 100, 90, 80 and 70 respectively. It then ran five rounds, each updating an unindexed stock column in 20% of the rows, with a <code>VACUUM<\/code> after each round. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It did that twice for each <code>fillfactor<\/code> &#8211; once with the work split into transactions of 1,000 rows, and once as a single <code>UPDATE<\/code> per round.<\/p>\n\n\n\n<h4 id=\"h-that-s-shown-below\" class=\"wp-block-heading\">That&#8217;s shown below:<\/h4>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"941\" height=\"367\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-25.png\" alt=\"an image showing HOT updates and heap size for four fillfactors and two update patterns.\" class=\"wp-image-113468\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-25.png 941w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-25-300x117.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-25-768x300.png 768w\" sizes=\"auto, (max-width: 941px) 100vw, 941px\" \/><figcaption class=\"wp-element-caption\"><em><code>HOT<\/code> updates and heap size for four fillfactors and two update patterns.<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, <code>fillfactor<\/code> 100 already achieved 94.5% <code>HOT<\/code> updates when the transactions were small (the green columns). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is because PostgreSQL prunes dead versions inside a page as it goes, and the space they free is enough for the next update.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>fillfactor<\/code> 90, on the other hand, took that to 100% and stopped the heap growing at the price of a table that was 12% bigger from the moment it was loaded. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Meanwhile, with one large <code>UPDATE<\/code> per round (in orange), <code>fillfactor<\/code> made a real difference. Here, only 60% of updates were <code>HOT<\/code> at 100, 78% at 90, and 100% at 80 or 70. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then, at fillfactor 100, the heap grew by 22% &#8211; and at 80, it didn&#8217;t grow <em>at all<\/em>, but started 25% larger!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s also the case where <code>fillfactor<\/code> can&#8217;t help at all. When the same five rounds updated the indexed category column instead, the result was 0% <code>HOT<\/code> at both <code>fillfactor<\/code> 100 <em>and<\/em> 70. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The 70 table simply wasted 40% more space for nothing.<\/p>\n\n\n\n<h3 id=\"h-why-fillfactor-only-helps-in-some-workloads\" class=\"wp-block-heading\">Why <code>fillfactor<\/code> only helps in <em>some<\/em> workloads<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>fillfactor<\/code> reserves free space when rows are first written into a page. That space matters most in the window before <code>VACUUM<\/code> or page pruning has freed anything else, which is exactly what a large update of recently loaded rows hits. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a steady OLTP workload of small transactions, pruning keeps recycling space inside each page, and the extra reserve matters less &#8211; but neither can help when the update changes an indexed column, because a <code>HOT<\/code> update is ruled out <em>regardless<\/em> of how much room there is.<\/p>\n\n\n\n<h3 id=\"h-when-to-lower-fillfactor\" class=\"wp-block-heading\">When to lower <code>fillfactor<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Lower <code>fillfactor<\/code> for tables that receive large batches of updates to non-indexed columns, or whose rows are updated soon after insertion; 80 to 90 is usually enough. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Leave it at 100 for insert-only tables and for tables whose updates mostly change indexed columns, and check the effect with <code>n_tup_hot_upd<\/code> against <code>n_tup_upd<\/code> in <code>pg_stat_user_tables<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Changing <code>fillfactor<\/code> with <code>ALTER TABLE ... SET (fillfactor = 90)<\/code> only affects pages written from then on. To apply it to existing data, you need a rewrite &#8211; which brings us nicely to the playbook&#8230;<\/p>\n\n\n\n<h2 id=\"h-postgresql-bloat-remediation-playbook\" class=\"wp-block-heading\">PostgreSQL bloat remediation playbook<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">All the symptoms above have different causes but, in practice, you deal with them in the same order: <strong>stop the bloat from growing, find out where it already is, and then remove it.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While removing the bloat <em>first<\/em> is tempting (because it gives an immediate result), it doesn&#8217;t work in the long run, because a table rebuilt on Monday under the same conditions will be bloated again by Friday!<\/p>\n\n\n\n<h3 id=\"h-step-1-give-the-hot-tables-their-own-autovacuum-settings\" class=\"wp-block-heading\">Step 1: give the <code>HOT<\/code> tables their own <code>autovacuum<\/code> settings<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Most databases have a handful of tables that take most of the writes. Give each of them per-table settings, as covered earlier: a scale factor between 0.01 and 0.05, a fixed threshold sized to the table, and a cost limit high enough for each run to finish quickly. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the lab, those three settings cut the peak backlog on the sessions table from 489,294 dead rows to 125,211. Leave the global defaults alone unless many tables need the same change and, if the busiest tables still build up a large backlog within one <code>autovacuum_naptime<\/code>, shorten the naptime.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At the same time, set <code>idle_in_transaction_session_timeout<\/code> for application roles, find out which reports run for minutes rather than seconds, and check for unused replication slots. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Symptom 4 showed that just one open snapshot can undo <em>every single<\/em> <code>autovacuum<\/code> setting you have.<\/p>\n\n\n\n<h3 id=\"h-step-2-measure-where-the-bloat-is\" class=\"wp-block-heading\">Step 2: measure where the bloat is<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Shown below is a reasonable weekly check using <code>pgstattuple_approx()<\/code>, which reads only the pages the visibility map can&#8217;t vouch for (so is cheap enough to run on large tables):<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">SELECT s.relname,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pg_size_pretty(pg_table_size(s.relid)) AS table_size,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pg_size_pretty(pg_indexes_size(s.relid)) AS index_size,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; round(a.dead_tuple_percent::numeric, 1) AS dead_pct,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; round(a.approx_free_percent::numeric, 1) AS free_pct,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to_char(s.last_autovacuum, 'YYYY-MM-DD HH24:MI') AS last_autovacuum\nFROM pg_stat_user_tables s\nCROSS JOIN LATERAL pgstattuple_approx(s.relid) a\nWHERE pg_table_size(s.relid) &gt; 10 * 1024 * 1024\nORDER BY pg_table_size(s.relid) DESC\nLIMIT 10;<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">On the lab database at the end of the experiments, it gave the following (abbreviated):<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">&nbsp; relname&nbsp;&nbsp; | table_size | index_size | dead_pct | free_pct | last_autovacuum\n------------+------------+------------+----------+----------+------------------\n&nbsp;sessions&nbsp;&nbsp; | 346 MB&nbsp;&nbsp;&nbsp;&nbsp; | 101 MB&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0.0 |&nbsp;&nbsp;&nbsp;&nbsp; 13.0 | 2026-09-27 03:53\n&nbsp;orders&nbsp;&nbsp;&nbsp;&nbsp; | 98 MB&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 19 MB&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0.0 |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0.7 |\n&nbsp;events&nbsp;&nbsp;&nbsp;&nbsp; | 89 MB&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 24 MB&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0.0 |&nbsp;&nbsp;&nbsp;&nbsp; 85.9 |\n&nbsp;items&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 41 MB&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 8304 kB&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0.0 |&nbsp;&nbsp;&nbsp;&nbsp; 30.5 |<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Since <code>events<\/code> has no dead rows at all and is 85.9% empty, it has a retention delete that will never be refilled and is therefore a clear candidate for a rewrite. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the other hand, <code>sessions<\/code> is 13% free &#8211; much of it from the long transaction in Symptom 4 &#8211; so that space will be reused by future updates. It&#8217;s therefore worth just watching this, rather than fixing, for now.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then we have <code>items<\/code>, which is 30% free because it was (intentionally) loaded with <code>fillfactor<\/code> 70.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Remember, the numbers alone don&#8217;t decide anything &#8211; it&#8217;s just the <em>history<\/em> of the table that does!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For indexes, run <code>pgstatindex<\/code> on the large indexes of the <code>HOT<\/code> tables (Symptom 2), and compare their size with the size of a fresh build. You can estimate this from a copy of the table or a recent rebuild.<\/p>\n\n\n\n<h3 id=\"h-step-3-rebuild-bloated-indexes-with-reindex-concurrently\" class=\"wp-block-heading\">Step 3: rebuild bloated indexes with <code>REINDEX CONCURRENTLY<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An index is much cheaper to fix than a table, and in Symptom 2 the index was usually the bigger problem.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>REINDEX INDEX CONCURRENTLY<\/code> builds a new copy and swaps it in, and reads and writes carry on while it works, but keep four things in mind:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li><strong>It <em>cannot<\/em> run inside a transaction block<\/strong>, and needs disk space for a second copy of the index while it runs.<br><br><\/li>\n\n\n\n<li><strong>It waits for transactions that started before it<\/strong>, so a long-running report will also hold up the rebuild.<br><br><\/li>\n\n\n\n<li><strong>If it fails or is cancelled, it leaves an invalid index behind<\/strong> (with <code>a _ccnew<\/code> suffix) that still has to be maintained on every write. Check it with <code>SELECT indexrelid::regclass FROM pg_index WHERE NOT indisvalid<\/code>, and drop what you find.<br><br><\/li>\n\n\n\n<li><strong>Rebuild one large index at a time<\/strong>. Each rebuild does a full index build with its own I\/O and WAL.<\/li>\n<\/ul>\n<\/div>\n\n\n<h3 id=\"h-step-4-rewrite-a-table-with-pg-repack-or-vacuum-full-but-only-when-you-must\" class=\"wp-block-heading\">Step 4: rewrite a table with <code>pg_repack<\/code> or <code>VACUUM FULL<\/code> (but only when you must)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When a table like events has a lot of free space that it will never reuse, the only way to give that space back is to rewrite the table. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL\u2019s built-in way to do that is <code>VACUUM FULL<\/code>, and its cost is the lock &#8211; as shown below on a table with four million rows (half of them deleted):<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"941\" height=\"441\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-11.jpg\" alt=\"image showing what happens while VACUUM FULL runs\" class=\"wp-image-113467\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-11.jpg 941w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-11-300x141.jpg 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-11-768x360.jpg 768w\" sizes=\"auto, (max-width: 941px) 100vw, 941px\" \/><figcaption class=\"wp-element-caption\"><em>While <code>VACUUM FULL<\/code> runs, <code>pg_locks<\/code> shows it holding an <code>AccessExclusiveLock<\/code>, and an ordinary <code>SELECT<\/code> in the other session waits 5.9 seconds for it to finish. (Recorded terminal session.)<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><code>VACUUM FULL<\/code> took 10 seconds and, during that time, the table couldn&#8217;t be read (let alone written) &#8211; so the <code>SELECT<\/code> in session 2 simply waited. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On a production table many times larger, this could amount to tens of minutes, or even <em>hours<\/em>, of outage.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pg_repack<\/code> does the same rewrite online. It copies the table into a new one, records the changes made during the copy in a log table, replays them, and then swaps the files, holding an <code>ACCESS EXCLUSIVE<\/code> lock only briefly at the start and at the end &#8211; as shown below in the same table:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"941\" height=\"467\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-10.jpg\" alt=\"\" class=\"wp-image-113464\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-10.jpg 941w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-10-300x149.jpg 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2026\/09\/image-10-768x381.jpg 768w\" sizes=\"auto, (max-width: 941px) 100vw, 941px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><em><code>pg_repack<\/code> rewrites the table from 539MB to 269MB, while the query in session 2 keeps running every two seconds. No read took longer than 2.5 ms. (Recorded terminal session.)<\/em><\/p>\n\n\n\n<h4 id=\"h-but-it-s-not-free\" class=\"wp-block-heading\">But, it&#8217;s not free!<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s not free, though! <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pg_repack<\/code> needs the extension installed in the database and is normally run as a superuser, the table must have a primary key or a unique index on non-<code>NULL<\/code> columns, <em>and<\/em> it needs free disk space for a full copy of the table and its indexes. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Plus, its short locks can still be held up by a long-running transaction, so run it outside the reporting window.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It also carries the table\u2019s storage parameters over to the new copy. When we set <code>fillfactor = 70<\/code> on the same table and repacked it, for example, it grew from 269MB to 391MB, with 31.4% free space. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That makes a repack a convenient moment to apply a new <code>fillfactor<\/code> &#8211; and is also a reason to <em>check<\/em> that setting before you start!<\/p>\n\n\n\n<h3 id=\"h-step-5-which-fix-for-which-symptom\" class=\"wp-block-heading\">Step 5: which fix for which symptom<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>What you see<\/th><th>What it means<\/th><th>What to do<\/th><\/tr><\/thead><tbody><tr><td>High <code>n_dead_tup<\/code> that keeps growing<\/td><td><code>autovacuum<\/code> is behind<\/td><td>Tune that table first (Step 1)<\/td><\/tr><tr><td><code>VACUUM (VERBOSE)<\/code> reports rows \u201cdead but not yet removable\u201d<\/td><td>Something is holding the xmin horizon<\/td><td>Run the three queries from Symptom 4 \u2192 \u201cHow to find what\u2019s holding back cleanup\u201d, then end the session or drop the slot<\/td><\/tr><tr><td>No dead rows, lots of free space the workload will reuse<\/td><td>Normal MVCC behaviour<\/td><td>Leave it alone<\/td><\/tr><tr><td>No dead rows, lots of free space that will never be reused<\/td><td>Typically a one-off mass delete<\/td><td>Rewrite with <code>pg_repack<\/code>, or <code>VACUUM FULL<\/code> in a maintenance window<\/td><\/tr><tr><td>Index much larger than a fresh build, or low leaf density<\/td><td>Index bloat<\/td><td><code>REINDEX INDEX CONCURRENTLY<\/code>, then ask whether the indexed column needs to change so often<\/td><\/tr><tr><td>Many non-<code>HOT<\/code> updates, and updates don\u2019t touch indexed columns<\/td><td>Not enough room in pages<\/td><td>Lower <code>fillfactor<\/code> and apply it at the next rewrite<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 id=\"h-summary-what-vacuum-does-and-doesn-t-do\" class=\"wp-block-heading\">Summary: what <code>VACUUM<\/code> <em>does<\/em> and <em>doesn\u2019t<\/em> do<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>VACUUM<\/code> does exactly one thing well &#8211; it makes the space held by dead row versions available again. To summarize:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li>A vacuumed table keeps its size. The space is reused by new rows, and only empty pages at the end of the file are returned.<br><br><\/li>\n\n\n\n<li>Indexes keep their pages after a mass delete, and indexes on frequently changing columns grow under every update that is <em>not<\/em> <code>HOT<\/code>.<br><br><\/li>\n\n\n\n<li>With default settings, <code>autovacuum<\/code> on a large, busy table starts late and rarely runs. Per-table settings fix most of that, but <code>autovacuum_naptime<\/code> still sets a floor.<br><br><\/li>\n\n\n\n<li>One old snapshot stops cleanup across the whole database, and the growth it causes stays after it ends.<br><br><\/li>\n\n\n\n<li><code>fillfactor<\/code> helps bulk updates to non-indexed columns, helps small transactions much less, and does nothing when an indexed column changes.<\/li>\n<\/ul>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">None of this means <code>autovacuum<\/code> is broken &#8211; it&#8217;s more that <em>vacuumed<\/em> is a statement about dead rows rather than size, and a healthy system needs both. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tune the <code>HOT<\/code> tables, watch the horizon, measure free space and index density, and rebuild only what will not recover by itself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When done in that order, you&#8217;re no longer asking <code>VACUUM<\/code> for something it never promised, so it stops <em>looking<\/em> like it&#8217;s lying.<\/p>\n\n\n\n<h2 id=\"h-how-to-reproduce-the-lab\" class=\"wp-block-heading\">How to reproduce the lab<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/github.com\/chandanshukla1989\/postgres-vacuum-bloat\" target=\"_blank\" rel=\"noreferrer noopener\">lab is available on GitHub<\/a>. You need PostgreSQL with the <code>contrib<\/code> extensions (the scripts were run on 18.4 and use only standard features), and Python 3.10 or later for the load generator. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each experiment is a separate script and takes between a few seconds and about 15 minutes.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">git clone https:\/\/github.com\/chandanshukla1989\/postgres-vacuum-bloat\ncd postgres-vacuum-bloat\npython3 -m venv .venv &amp;&amp; .venv\/bin\/pip install -r requirements.txt\ncreatedb bloat_lab\npsql -d bloat_lab -f sql\/00_helpers.sql\n\n.\/experiments\/01_vacuumed_not_smaller.sh&nbsp;&nbsp;&nbsp; # Symptom 1\n.\/experiments\/02_index_bloat.sh&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Symptom 2\n.\/experiments\/03_autovacuum_outpaced.sh&nbsp;&nbsp;&nbsp;&nbsp; # Symptom 3 (two 7-minute runs)\n.\/experiments\/04_long_transaction.sh&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Symptom 4\n.\/experiments\/05_fillfactor.sh&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Fillfactor<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Results go to evidence\/raw (the <code>psql<\/code> output) and data (CSV files). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>pg_repack<\/code> example also needs the <code>pg_repack<\/code> extension, created with <code>CREATE EXTENSION pg_repack;<\/code>, and the matching <code>pg_repack<\/code> client on your <code>PATH<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The exact numbers will differ a little on other hardware and versions, but the shapes should not.<\/p>\n\n\n\n<section id=\"my-first-block-block_36c3da355cae8695b20735d9fc09d1e8\" class=\"my-first-block alignwide\">\n    <div class=\"bg-brand-600 text-base-white py-5xl px-4xl rounded-sm bg-gradient-to-r from-brand-600 to-brand-500 red\">\n        <div class=\"gap-4xl items-start md:items-center flex flex-col md:flex-row justify-between\">\n            <div class=\"flex-1 col-span-10 lg:col-span-7\">\n                <h3 class=\"mt-0 font-display mb-2 text-display-sm\">Free desktop tool for fast PostgreSQL monitoring and diagnostics<\/h3>\n                <div class=\"child:last-of-type:mb-0\">\n                                            Stay in control of PostgreSQL performance with Redgate pgNow \u2013 a free desktop tool for fast, focused diagnostics. No agents, no setup, just actionable insights when you need them.                                    <\/div>\n            <\/div>\n                                            <a href=\"https:\/\/www.red-gate.com\/products\/redgate-pgnow\/\" class=\"btn btn--secondary btn--lg\" aria-label=\"Learn more &amp; download now: Free desktop tool for fast PostgreSQL monitoring and diagnostics\">Learn more &amp; download now<\/a>\n                    <\/div>\n    <\/div>\n<\/section>\n\n\n<section id=\"faq\" class=\"faq-block my-5xl\">\n    <h2>FAQs: PostgreSQL bloat and VACUUM<\/h2>\n\n            <h3 class=\"mt-4xl\">1. Why is my PostgreSQL table still large after VACUUM?<\/h3>\n        <div class=\"faq-answer\">\n            <p dir=\"ltr\">Plain <code>VACUUM<\/code> marks dead row versions as reusable free space, but it doesn\u2019t move rows or shrink the file. It only returns space to the operating system when empty pages sit at the end of the file. A table with free space scattered through it stays the same size until you rewrite it with <code>VACUUM FULL<\/code> or <code>pg_repack<\/code>.<\/p>\n        <\/div>\n            <h3 class=\"mt-4xl\">2. Does VACUUM shrink PostgreSQL indexes?<\/h3>\n        <div class=\"faq-answer\">\n            <p dir=\"ltr\">No. <code>VACUUM<\/code> removes dead index entries but leaves leaf pages in place, so after a mass delete an index keeps its size while leaf density can drop below 10%. B-tree pages are only freed when every entry on them is gone. Rebuild with <code dir=\"ltr\">REINDEX INDEX CONCURRENTLY<\/code>, which doesn\u2019t block reads or writes.<\/p>\n        <\/div>\n            <h3 class=\"mt-4xl\">3. Why is autovacuum running but dead rows keep building up?<\/h3>\n        <div class=\"faq-answer\">\n            <p dir=\"ltr\">Usually for one of two reasons. By default, <code>autovacuum<\/code> only triggers once dead rows pass 50 plus 20% of the table\u2019s rows, so large tables collect hundreds of thousands first. Alternatively, an old transaction, replication slot or prepared transaction is holding back the xmin horizon, so <code>VACUUM<\/code> reports rows as \u201cdead but not yet removable\u201d.<\/p>\n        <\/div>\n            <h3 class=\"mt-4xl\">4. Should I use VACUUM FULL or pg_repack to reclaim space?<\/h3>\n        <div class=\"faq-answer\">\n            <p dir=\"ltr\"><code>VACUUM FULL<\/code> holds an <code>ACCESS EXCLUSIVE<\/code> lock for the whole rewrite, so the table can\u2019t be read or written. <code>pg_repack<\/code> rewrites online and only locks briefly at the start and end, but needs the extension, a primary key or unique index, and disk space for a full copy. Only use <code>VACUUM FULL<\/code>\u00a0in a maintenance window.<\/p>\n        <\/div>\n            <h3 class=\"mt-4xl\">5. Does lowering fillfactor reduce PostgreSQL bloat?<\/h3>\n        <div class=\"faq-answer\">\n            <p dir=\"ltr\">It <em>can<\/em>. A <code>fillfactor<\/code> of 80 to 90 leaves room in each page for <code>HOT<\/code> updates, which helps most with large batch updates to non-indexed columns. It helps little with small transactions (<code>fillfactor<\/code> 100 already reached 94.5% <code>HOT<\/code> in the lab) and nothing when an indexed column changes. It only applies to newly written pages until you rewrite the table.<\/p>\n        <\/div>\n    <\/section>\n","protected":false},"excerpt":{"rendered":"<p>VACUUM ran, dead rows are zero, yet tables and indexes keep growing. See four causes of PostgreSQL bloat, tested on 18.4, and how to fix each one.&hellip;<\/p>\n","protected":false},"author":346249,"featured_media":113672,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143523,53,143534],"tags":[4168,4170,158978,4150],"coauthors":[159333],"class_list":["post-113458","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-databases","category-featured","category-postgresql","tag-database","tag-database-administration","tag-postgresql","tag-sql"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/113458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/346249"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=113458"}],"version-history":[{"count":26,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/113458\/revisions"}],"predecessor-version":[{"id":113898,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/113458\/revisions\/113898"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media\/113672"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=113458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=113458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=113458"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=113458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}