On Sat, Nov 28, 2026 at 1:49 AM Amit Kapila <[email protected]> wrote: >
Thanks for taking another look > 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. Makes sense. I've done a version of that in the attached I plan to commit later today. > 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. Good call. I've gone through and updated any of the references to vacuum that seemed a bit out of date. Of course, that only applies to 19 and master, so I've changed the comments a bit for 17 and 18. - Melanie
From 215ef7c5f2703ae296cc894d258242ae6d3dadf9 Mon Sep 17 00:00:00 2001 From: Melanie Plageman <[email protected]> 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 <[email protected]> Reviewed-by: Amit Kapila <[email protected]> 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
From 9e9b540aa9786ef424817e610fab323828ab4467 Mon Sep 17 00:00:00 2001 From: Melanie Plageman <[email protected]> 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 <[email protected]> Reviewed-by: Amit Kapila <[email protected]> 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
