From 9e9b540aa9786ef424817e610fab323828ab4467 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 | 101 ++++++++++++++----------
 1 file changed, 60 insertions(+), 41 deletions(-)

diff --git a/src/backend/access/heap/visibilitymap.c b/src/backend/access/heap/visibilitymap.c
index 3551e8a91f6..78f3c7996d9 100644
--- a/src/backend/access/heap/visibilitymap.c
+++ b/src/backend/access/heap/visibilitymap.c
@@ -34,53 +34,72 @@
  * is set, we know the condition is true, but if a bit is not set, it might or
  * might not be true.
  *
- * Clearing visibility map bits is not separately WAL-logged.  The callers
- * must make sure that whenever a bit is cleared, the bit is cleared on WAL
- * replay of the updating operation as well.
- *
- * When we *set* a visibility map during VACUUM, we must write WAL.  This may
- * seem counterintuitive, since the bit is basically a hint: if it is 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.  The difficulty is that
- * there are two bits which are typically set together: the PD_ALL_VISIBLE bit
- * on the page itself, and the visibility map bit.  If a crash occurs after the
- * visibility map page makes it to disk and before the updated heap page makes
- * it to disk, redo must set the bit on the heap page.  Otherwise, the next
- * insert, update, or delete on the heap page will fail to realize that the
- * visibility map bit must be cleared, possibly causing index-only scans to
- * return wrong answers.
+ * When a WAL-logged heap operation clears VM bits, it registers each
+ * affected VM block in the same WAL record as the heap change. When setting
+ * bits, visibilitymap_set() registers the VM block in a separate
+ * XLOG_HEAP2_VISIBLE record.
+ *
+ * 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 scanning heap pages during vacuum 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.
+ *
+ * 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
+ * the next index-only scan can return wrong results or vacuum may incorrectly
+ * advance relfrozenxid.
+ *
+ * When setting the VM, if a crash occurs after the visibility map page makes
+ * it to disk and before the updated heap page makes it to disk, redo must set
+ * the bit on the heap page. Otherwise, the next insert, update, or delete on
+ * the heap page will fail to realize that the visibility map bit must be
+ * cleared, possibly causing index-only scans to return wrong answers.
  *
  * 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.
  *
  * 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.
+ *
+ * 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.
+ *
+ * To clear VM bits as part of a heap modification, the caller acquires the
+ * exclusive VM buffer lock after the heap buffer lock and before entering
+ * the critical section. To set bits, visibilitymap_set() takes the VM buffer
+ * lock itself. In both cases the VM buffer must remain locked through WAL
+ * insertion and the page LSN update, since the WAL record may include a
+ * full-page image of the VM page.
+ *
+ * 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

