Simplify vacuum VM update logging counters
authorMelanie Plageman <[email protected]>
Thu, 26 Jun 2025 18:25:45 +0000 (14:25 -0400)
committerMelanie Plageman <[email protected]>
Thu, 26 Jun 2025 18:25:45 +0000 (14:25 -0400)
We can simplify the VM counters added in dc6acfd910b8 to
lazy_vacuum_heap_page() and lazy_scan_new_or_empty().

We won't invoke lazy_vacuum_heap_page() unless there are dead line
pointers, so we know the page can't be all-visible.

In lazy_scan_new_or_empty(), we only update the VM if the page-level
hint PD_ALL_VISIBLE is clear, and the VM bit cannot be set if the page
level bit is clear because a subsequent page update would fail to clear
the visibility map bit.

Simplify the logic for determining which log counters to increment based
on this knowledge. Doing so is worthwhile because the old logic was
confusing and misguided.

Author: Melanie Plageman <[email protected]>
Reviewed-by: Nazir Bilal Yavuz <[email protected]>
Reviewed-by: Masahiko Sawada <[email protected]>
Discussion: https://postgr.es/m/flat/CAAKRu_a9w_n2mwY%3DG4LjfWTvRTJtjbfvnYAKi4WjO8QXHHrA0g%40mail.gmail.com

src/backend/access/heap/vacuumlazy.c

index 09416450af96221edb25d6dad9112683561438f7..8a42e17aec210fcac1a861be007371d719f297a4 100644 (file)
@@ -1872,8 +1872,6 @@ lazy_scan_new_or_empty(LVRelState *vacrel, Buffer buf, BlockNumber blkno,
         */
        if (!PageIsAllVisible(page))
        {
-           uint8       old_vmbits;
-
            START_CRIT_SECTION();
 
            /* mark buffer dirty before writing a WAL record */
@@ -1893,24 +1891,16 @@ lazy_scan_new_or_empty(LVRelState *vacrel, Buffer buf, BlockNumber blkno,
                log_newpage_buffer(buf, true);
 
            PageSetAllVisible(page);
-           old_vmbits = visibilitymap_set(vacrel->rel, blkno, buf,
-                                          InvalidXLogRecPtr,
-                                          vmbuffer, InvalidTransactionId,
-                                          VISIBILITYMAP_ALL_VISIBLE |
-                                          VISIBILITYMAP_ALL_FROZEN);
+           visibilitymap_set(vacrel->rel, blkno, buf,
+                             InvalidXLogRecPtr,
+                             vmbuffer, InvalidTransactionId,
+                             VISIBILITYMAP_ALL_VISIBLE |
+                             VISIBILITYMAP_ALL_FROZEN);
            END_CRIT_SECTION();
 
-           /*
-            * If the page wasn't already set all-visible and/or all-frozen in
-            * the VM, count it as newly set for logging.
-            */
-           if ((old_vmbits & VISIBILITYMAP_ALL_VISIBLE) == 0)
-           {
-               vacrel->vm_new_visible_pages++;
-               vacrel->vm_new_visible_frozen_pages++;
-           }
-           else if ((old_vmbits & VISIBILITYMAP_ALL_FROZEN) == 0)
-               vacrel->vm_new_frozen_pages++;
+           /* Count the newly all-frozen pages for logging */
+           vacrel->vm_new_visible_pages++;
+           vacrel->vm_new_visible_frozen_pages++;
        }
 
        freespace = PageGetHeapFreeSpace(page);
@@ -2915,7 +2905,6 @@ lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno, Buffer buffer,
    if (heap_page_is_all_visible(vacrel, buffer, &visibility_cutoff_xid,
                                 &all_frozen))
    {
-       uint8       old_vmbits;
        uint8       flags = VISIBILITYMAP_ALL_VISIBLE;
 
        if (all_frozen)
@@ -2925,25 +2914,15 @@ lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno, Buffer buffer,
        }
 
        PageSetAllVisible(page);
-       old_vmbits = visibilitymap_set(vacrel->rel, blkno, buffer,
-                                      InvalidXLogRecPtr,
-                                      vmbuffer, visibility_cutoff_xid,
-                                      flags);
+       visibilitymap_set(vacrel->rel, blkno, buffer,
+                         InvalidXLogRecPtr,
+                         vmbuffer, visibility_cutoff_xid,
+                         flags);
 
-       /*
-        * If the page wasn't already set all-visible and/or all-frozen in the
-        * VM, count it as newly set for logging.
-        */
-       if ((old_vmbits & VISIBILITYMAP_ALL_VISIBLE) == 0)
-       {
-           vacrel->vm_new_visible_pages++;
-           if (all_frozen)
-               vacrel->vm_new_visible_frozen_pages++;
-       }
-
-       else if ((old_vmbits & VISIBILITYMAP_ALL_FROZEN) == 0 &&
-                all_frozen)
-           vacrel->vm_new_frozen_pages++;
+       /* Count the newly set VM page for logging */
+       vacrel->vm_new_visible_pages++;
+       if (all_frozen)
+           vacrel->vm_new_visible_frozen_pages++;
    }
 
    /* Revert to the previous phase information for error traceback */