From 215ef7c5f2703ae296cc894d258242ae6d3dadf9 Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Wed, 25 Nov 2026 13:25:34 -0500
Subject: [PATCH v2] Update visibilitymap.c comments

ed62d26cac made visibility map clear operations register the VM block in
the WAL record and stamp the VM pages with that record's LSN. There were
a few inaccuracies left over in visibilitymap.c documenting VM update
behavior. Fix those and refresh that section. These edits are
backpatched through 17.

While at it, fix a few references to vacuum since b46e1e54d078 allowed
on-access pruning to set the VM. These edits are only backpatched to 19
(where that feature was introduced).

Reported-by: Amit Kapila <amit.kapila16@gmail.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Discussion: https://postgr.es/m/CAA4eK1JfAu8p2TFZ56vXTeNX9OGBXRgTB_2uf7Ci4jKvmTqJjw%40mail.gmail.com
Backpatch-through: 17
---
 src/backend/access/heap/visibilitymap.c | 92 ++++++++++++++-----------
 1 file changed, 52 insertions(+), 40 deletions(-)

diff --git a/src/backend/access/heap/visibilitymap.c b/src/backend/access/heap/visibilitymap.c
index 308a6034f91..755a63f97b5 100644
--- a/src/backend/access/heap/visibilitymap.c
+++ b/src/backend/access/heap/visibilitymap.c
@@ -25,30 +25,34 @@
  * The visibility map is a bitmap with two bits (all-visible and all-frozen)
  * per heap page. A set all-visible bit means that all tuples on the page are
  * known visible to all transactions, and therefore the page doesn't need to
- * be vacuumed. A set all-frozen bit means that all tuples on the page are
- * completely frozen, and therefore the page doesn't need to be vacuumed even
- * if whole table scanning vacuum is required (e.g. anti-wraparound vacuum).
- * The all-frozen bit must be set only when the page is already all-visible.
+ * be pruned. A set all-frozen bit means that all tuples on the page are
+ * completely frozen, so even an aggressive VACUUM can skip the page. The
+ * all-frozen bit must be set only when the page is already all-visible.
  *
  * The map is conservative in the sense that we make sure that whenever a bit
  * is set, we know the condition is true, but if a bit is not set, it might or
  * might not be true.
  *
- * Changes to the visibility map bits are not separately WAL-logged. Callers
- * must make sure that whenever a visibility map bit is cleared, the bit is
- * cleared on WAL replay of the updating operation. And whenever a visibility
- * map bit is set, the bit is set on WAL replay of the operation that rendered
- * the page all-visible/all-frozen.
+ * Changes to the visibility map bits are WAL-logged as part of the operation
+ * setting or clearing the bit. Callers of visibility map functions must make
+ * sure that the VM block changes are recorded in the WAL record and the
+ * affected VM block(s) is registered in the same WAL record. Replay of the
+ * operation that rendered the bit set or cleared must then also read the VM
+ * page and make the appropriate changes.
  *
  * The visibility map bits operate as a hint in one direction: if they are
  * clear, it may still be the case that every tuple on the page is visible to
  * all transactions (we just don't know that for certain). However, if they
- * are set, we may skip vacuuming pages and advance relfrozenxid or skip
- * reading heap pages for an index-only scan. If they are incorrectly set,
- * this can lead to data corruption and wrong results.
+ * are set, we may skip scanning heap pages during vacuum and advance
+ * relfrozenxid, skip pruning on-access, and skip reading heap pages for an
+ * index-only scan. If they are incorrectly set, this can lead to data
+ * corruption and wrong results.
  *
- * Additionally, it is critical that the heap-page level PD_ALL_VISIBLE bit be
- * correctly set and cleared along with the VM bits.
+ * When the VM all-visible bit is set, PD_ALL_VISIBLE must be set on the heap
+ * page so that a later modification knows to clear the VM bit. When a
+ * modification makes the page no longer all-visible, it should clear
+ * PD_ALL_VISIBLE as well, since heap scans rely on that flag to skip
+ * per-tuple visibility checks.
  *
  * When clearing the VM, if a crash occurs after the heap page makes it to
  * disk but before the VM page makes it to disk, replay must clear the VM or
@@ -63,35 +67,43 @@
  *
  * VACUUM will normally skip pages for which the visibility map bit is set;
  * such pages can't contain any dead tuples and therefore don't need vacuuming.
+ * On-access pruning can skip checking individual tuples when the VM shows
+ * the page is all-visible.
  *
  * LOCKING
  *
- * In heapam.c, whenever a page is modified so that not all tuples on the
- * page are visible to everyone anymore, the corresponding bit in the
- * visibility map is cleared. In order to be crash-safe, we need to do this
- * while still holding a lock on the heap page and in the same critical
- * section that logs the page modification. However, we don't want to hold
- * the buffer lock over any I/O that may be required to read in the visibility
- * map page.  To avoid this, we examine the heap page before locking it;
- * if the page-level PD_ALL_VISIBLE bit is set, we pin the visibility map
- * bit.  Then, we lock the buffer.  But this creates a race condition: there
- * is a possibility that in the time it takes to lock the buffer, the
- * PD_ALL_VISIBLE bit gets set.  If that happens, we have to unlock the
- * buffer, pin the visibility map page, and relock the buffer.  This shouldn't
- * happen often, because only VACUUM currently sets visibility map bits,
- * and the race will only occur if VACUUM processes a given page at almost
- * exactly the same time that someone tries to further modify it.
- *
- * To set a bit, you need to hold a lock on the heap page. That prevents
- * the race condition where VACUUM sees that all tuples on the page are
- * visible to everyone, but another backend modifies the page before VACUUM
- * sets the bit in the visibility map.
- *
- * When a bit is set, the LSN of the visibility map page is updated to make
- * sure that the visibility map update doesn't get written to disk before the
- * WAL record of the changes that made it possible to set the bit is flushed.
- * But when a bit is cleared, we don't have to do that because it's always
- * safe to clear a bit in the map from correctness point of view.
+ * In heapam.c, whenever a page is modified so that not all tuples on the page
+ * are visible to everyone anymore, the corresponding bit in the visibility
+ * map is cleared. In order to be crash-safe, we need to do this while still
+ * holding a lock on the heap page and in the same critical section that logs
+ * the page modification. However, we don't want to hold the heap buffer lock
+ * over any I/O that may be required to read in the visibility map page. To
+ * avoid this, we examine the heap page before locking it; if the page-level
+ * PD_ALL_VISIBLE bit is set, we pin the visibility map page. Then, we lock
+ * the heap buffer. But this creates a race condition: there is a possibility
+ * that in the time it takes to lock the heap buffer, the PD_ALL_VISIBLE bit
+ * gets set. If that happens, we have to unlock the heap buffer, pin the
+ * visibility map page, and relock the heap buffer. This should be uncommon:
+ * another backend must set PD_ALL_VISIBLE between our unlocked check and
+ * acquisition of the heap buffer lock. For on-access pruning, that backend
+ * must already hold the cleanup lock when we pin the heap buffer.
+ *
+ * Setting or clearing VM bits requires locks on both the heap and VM buffers.
+ * When setting a bit, we hold the heap buffer lock from the time we check the
+ * heap page until we set the bit, so another backend cannot invalidate the
+ * result of the check in between.
+ *
+ * Callers acquire the exclusive VM buffer lock after the heap buffer lock and
+ * before entering the critical section. Both locks are held throughout the
+ * critical section, including WAL insertion and updating the page LSNs. The
+ * VM buffer must remain locked while the WAL record is constructed, since the
+ * record may include a full-page image of it.
+ *
+ * When a bit is set or cleared, the LSN of the visibility map page is updated
+ * if the change is WAL-logged. This ensures that the WAL record is flushed
+ * before the visibility map page is written to disk and that FPIs are
+ * correctly generated. When setting a bit, this ensures that the WAL record
+ * of the changes that made it possible to set the bit is flushed first.
  *
  *-------------------------------------------------------------------------
  */
-- 
2.43.0

