title: Speed up autovacuum in Postgres
body: <p>I have a question regarding Postgres autovacuum / vacuum settings.
  I have a table with 4.5 billion rows and there was a period of time with a lot of updates resulting in ~ 1.5 billion dead tuples. At this point autovacuum was taking a long time (days) to complete.
  When looking at the pg_stat_progress_vacuum view I noticed that:</p>
  <pre><code>max_dead_tuples = 178956970
  </code></pre>
  <p>resulting in multiple index rescans (index_vacuum_count)<br />
  According to <a href="https://www.postgresql.org/docs/12/progress-reporting.html" rel="nofollow noreferrer">docs</a> - max_dead_tuples is a number of dead tuples that we can store before needing to perform an index vacuum cycle, based on maintenance_work_mem.<br />
  According to <a href="http://rhaas.blogspot.com/2019/01/how-much-maintenanceworkmem-do-i-need.html" rel="nofollow noreferrer">this</a> one dead tuple requires 6 bytes of space.<br />
  So 6B x 178956970 = ~1GB<br />
  But my settings are</p>
  <pre><code>maintenance_work_mem = 20GB
  autovacuum_work_mem = -1
  </code></pre>
  <p>So what am I missing? why didn't all my 1.5b dead tuples fit in max_dead_tuples, since 20GB should give enough space, and why there were multiple runs necessary?</p>