On Thu, Nov 26, 2026 at 12:04 AM Melanie Plageman
<[email protected]> wrote:
>
> That's right. I've updated that.
>

One minor comment:
- * 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.
...
...
+ * Setting or clearing VM bits requires locks on both the heap and VM buffers.
+ * The heap buffer lock keeps the heap page and its VM bits consistent, while
+ * the exclusive VM buffer lock protects the VM page. Callers acquire the VM
+ * 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.

The previous comment mentions about the Vacuum's race "That prevents
the race condition where VACUUM sees that .." which the new text
doesn't seem to cover. If you omitted that intentionally then fine,
otherwise, something like attached atop your patch could be helpful.

BTW, does following text in same file also needs an update:
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.

The code is setting the visibility map bit even on pruning via
heap_page_prune_opt()->heap_page_prune_and_freeze(), so the "...
because only VACUUM ..." part probably requires an update.

-- 
With Regards,
Amit Kapila.
diff --git a/src/backend/access/heap/visibilitymap.c 
b/src/backend/access/heap/visibilitymap.c
index aeb615ec531..deaeebf02be 100644
--- a/src/backend/access/heap/visibilitymap.c
+++ b/src/backend/access/heap/visibilitymap.c
@@ -85,8 +85,12 @@
  *
  * Setting or clearing VM bits requires locks on both the heap and VM buffers.
  * The heap buffer lock keeps the heap page and its VM bits consistent, while
- * the exclusive VM buffer lock protects the VM page. Callers acquire the VM
- * lock after the heap buffer lock and before entering the critical section.
+ * the exclusive VM buffer lock protects the VM page. In particular, holding
+ * the heap buffer lock while setting a bit prevents the race 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.
+ * Callers acquire the VM 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.

Reply via email to