]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix missing FSM vacuum opportunities on tables without indexes.
authorMasahiko Sawada <msawada@postgresql.org>
Wed, 2 Jul 2025 06:25:20 +0000 (23:25 -0700)
committerMasahiko Sawada <msawada@postgresql.org>
Wed, 2 Jul 2025 06:25:20 +0000 (23:25 -0700)
Commit c120550edb86 optimized the vacuuming of relations without
indexes (a.k.a. one-pass strategy) by directly marking dead item IDs
as LP_UNUSED. However, the periodic FSM vacuum was still checking if
dead item IDs had been marked as LP_DEAD when attempting to vacuum the
FSM every VACUUM_FSM_EVERY_PAGES blocks. This condition was never met
due to the optimization, resulting in missed FSM vacuum
opportunities.

This commit modifies the periodic FSM vacuum condition to use the
number of tuples deleted during HOT pruning. This count includes items
marked as either LP_UNUSED or LP_REDIRECT, both of which are expected
to result in new free space to report.

Back-patch to v17 where the vacuum optimization for tables with no
indexes was introduced.

Reviewed-by: Melanie Plageman <melanieplageman@gmail.com>
Discussion: https://postgr.es/m/CAD21AoBL8m6B9GSzQfYxVaEgvD7-Kr3AJaS-hJPHC+avm-29zw@mail.gmail.com
Backpatch-through: 17

src/backend/access/heap/vacuumlazy.c

index 75979530897cd9fe2bee94c3b8f68045f9dc1032..14036c27e878aa5939b86d4be857e3815ae21d13 100644 (file)
@@ -431,7 +431,7 @@ static void find_next_unskippable_block(LVRelState *vacrel, bool *skipsallvis);
 static bool lazy_scan_new_or_empty(LVRelState *vacrel, Buffer buf,
                                                                   BlockNumber blkno, Page page,
                                                                   bool sharelock, Buffer vmbuffer);
-static void lazy_scan_prune(LVRelState *vacrel, Buffer buf,
+static int     lazy_scan_prune(LVRelState *vacrel, Buffer buf,
                                                        BlockNumber blkno, Page page,
                                                        Buffer vmbuffer, bool all_visible_according_to_vm,
                                                        bool *has_lpdead_items, bool *vm_page_frozen);
@@ -1245,6 +1245,7 @@ lazy_scan_heap(LVRelState *vacrel)
                Buffer          buf;
                Page            page;
                uint8           blk_info = 0;
+               int                     ndeleted = 0;
                bool            has_lpdead_items;
                void       *per_buffer_data = NULL;
                bool            vm_page_frozen = false;
@@ -1387,10 +1388,10 @@ lazy_scan_heap(LVRelState *vacrel)
                 * line pointers previously marked LP_DEAD.
                 */
                if (got_cleanup_lock)
-                       lazy_scan_prune(vacrel, buf, blkno, page,
-                                                       vmbuffer,
-                                                       blk_info & VAC_BLK_ALL_VISIBLE_ACCORDING_TO_VM,
-                                                       &has_lpdead_items, &vm_page_frozen);
+                       ndeleted = lazy_scan_prune(vacrel, buf, blkno, page,
+                                                                          vmbuffer,
+                                                                          blk_info & VAC_BLK_ALL_VISIBLE_ACCORDING_TO_VM,
+                                                                          &has_lpdead_items, &vm_page_frozen);
 
                /*
                 * Count an eagerly scanned page as a failure or a success.
@@ -1481,7 +1482,7 @@ lazy_scan_heap(LVRelState *vacrel)
                         * table has indexes. There will only be newly-freed space if we
                         * held the cleanup lock and lazy_scan_prune() was called.
                         */
-                       if (got_cleanup_lock && vacrel->nindexes == 0 && has_lpdead_items &&
+                       if (got_cleanup_lock && vacrel->nindexes == 0 && ndeleted > 0 &&
                                blkno - next_fsm_block_to_vacuum >= VACUUM_FSM_EVERY_PAGES)
                        {
                                FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum,
@@ -1936,8 +1937,10 @@ cmpOffsetNumbers(const void *a, const void *b)
  * *vm_page_frozen is set to true if the page is newly set all-frozen in the
  * VM. The caller currently only uses this for determining whether an eagerly
  * scanned page was successfully set all-frozen.
+ *
+ * Returns the number of tuples deleted from the page during HOT pruning.
  */
-static void
+static int
 lazy_scan_prune(LVRelState *vacrel,
                                Buffer buf,
                                BlockNumber blkno,
@@ -2208,6 +2211,8 @@ lazy_scan_prune(LVRelState *vacrel,
                        *vm_page_frozen = true;
                }
        }
+
+       return presult.ndeleted;
 }
 
 /*