Skip to content

Commit 95aa823

Browse files
committed
Set the all-visible flag on heap page before writing WAL record, not after.
If we set the all-visible flag after writing WAL record, and XLogInsert takes a full-page image of the page, the image would not include the flag. We will then proceed to set the VM bit, which would then be set without the corresponding all-visible flag on the heap page. Found by comparing page images on master and standby, after writing/replaying each WAL record. (There is still a discrepancy: the all-visible flag won't be set after replaying the HEAP_CLEAN record, even though it is set in the master. However, it will be set when replaying the HEAP2_VISIBLE record and setting the VM bit, so the all-visible flag and VM bit are always consistent on the standby, even though they are momentarily out-of-sync with master) Backpatch to 9.3 where this code was introduced.
1 parent aa0a519 commit 95aa823

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/backend/commands/vacuumlazy.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,13 @@ lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer,
12061206

12071207
PageRepairFragmentation(page);
12081208

1209+
/*
1210+
* Now that we have removed the dead tuples from the page, once again
1211+
* check if the page has become all-visible.
1212+
*/
1213+
if (heap_page_is_all_visible(buffer, &visibility_cutoff_xid))
1214+
PageSetAllVisible(page);
1215+
12091216
/*
12101217
* Mark buffer dirty before we write WAL.
12111218
*/
@@ -1224,14 +1231,13 @@ lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer,
12241231
}
12251232

12261233
/*
1227-
* Now that we have removed the dead tuples from the page, once again
1228-
* check if the page has become all-visible.
1234+
* All the changes to the heap page have been done. If the all-visible
1235+
* flag is now set, also set the VM bit.
12291236
*/
1230-
if (!visibilitymap_test(onerel, blkno, vmbuffer) &&
1231-
heap_page_is_all_visible(buffer, &visibility_cutoff_xid))
1237+
if (PageIsAllVisible(page) &&
1238+
!visibilitymap_test(onerel, blkno, vmbuffer))
12321239
{
12331240
Assert(BufferIsValid(*vmbuffer));
1234-
PageSetAllVisible(page);
12351241
visibilitymap_set(onerel, blkno, buffer, InvalidXLogRecPtr, *vmbuffer,
12361242
visibility_cutoff_xid);
12371243
}

0 commit comments

Comments
 (0)