On Sat, Nov 21, 2026 at 7:18 AM Zhijie Hou <[email protected]> wrote: > > I analyzed this bit more, and think the same issue can also cause a > false positive in 0001. visibilitymap_prepare_truncate() also modifies > the VM page without advancing pd_lsn when checksums and wal_log_hints > are off (as in Zsolt's second test case). If the server crashes > immediately after that VM page is flushed to disk, recovery sees the > post-truncation VM page contents with the pre-truncation pd_lsn. When > replaying the last VM-modifying record before the truncation, > wal_consistency_checking fails because the on-disk page does not match > the image logged before the truncation. We could fix this by > unconditionally WAL-logging the VM page there, which advances pd_lsn as > well. (But the fix looks like separate change worth some discussions and > some consensus)
Yea, it wouldn't be too much overhead to WAL-log clearing the tail of the VM after truncation. But, unless we are actually going to be able to unmask VM pages and do WAL consistency checking on them, there's probably no point because some of the other issues seem much harder to fix. On the bright side, I surveyed changes to the VM and this seems like one of the only ones, except for fixing corruption in vacuum -- and that should be wal-logged anyway, that modifies VM pages without advancing the LSN. > And I also noticed two other cases (when full_page_writes is off) that > cause similar failures: > > 1. If the server crashes after dropping a table, the VM file has already > been unlinked. During recovery, replaying an earlier XLOG_HEAP2_PRUNE_* > record passes RBM_ZERO_ON_ERROR when reading the VM page, which creates > a new zero-filled page if the file does not exist. After replaying that > record's VM change onto the new page, its contents still do not match > the logged image because any other bits previously set on that VM page > were lost. Right, we would need to use RBM_NORMAL (or a new mode) which does log_invalid_page after the crash when trying to set the VM bit and there is no file. The problem with this is that we also don't have a WILL_INIT equivalent for when we make a VM page for the first time. That means if we use RBM_NORMAL, the standby would panic during normal consistent replay operations that create new VM pages because they would do log_invalid_page() as well. To fix this, we'd have to add some equivalent of WILL_INIT, which is harder than it seems for the VM, because it has to detect a new page. For heap pages it is easy if (ItemPointerGetOffsetNumber(&(heaptup->t_self)) == FirstOffsetNumber && PageGetMaxOffsetNumber(page) == FirstOffsetNumber) but how do you do this without the equivalent for VM pages. How do you detect a never modified VM page? They don't have the same kind of header. The code seems non-trivial. I do think we want to stop zeroing the page on checksum errors, but stopping creating a new page when it is missing because it is a new page is harder. > 2. If the VM fork is truncated and then re-extended, vm_extend() writes > an all-zero page on disk while the page header is initialized only in > shared_buffers, leaving an all-zero page on disk. If the server crashes > immediately after extending the fork, recovery runs the consistency > check against that all-zero page and fails (note that this issue also > exists on master for zero-extended heap pages, where mask_unused_space() > fails on pd_lower == 0). Attached is a patch to fix this for heap which would also fix it for clearing the VM since it reads the page with RBM_NORMAL now. > To fix this, we could skip the consistency > check when the page is new (PageIsNew()) and the current WAL record does > not initialize it. We wouldn't need to do this with the attached fix which is needed for heap anyway. To summarize, if we committed the attached patch (easy), WAL-logged clearing the tail of the VM after truncation (easy), WAL-logged clearing VM corruption (easy), and came up with a way to WAL log VM page initialization (harder), it seems we could enable WAL consistency checking on the VM. I don't know if the last one is worth the trade off if it doesn't bring advantages outside of wal consistency checking though. I think we should anyway commit something like the attached and WAL log VM corruption fixing (heap_page_fix_vm_corruption()). - Melanie
From 26447cb08770c337c7a1cf6189c9744c42ffb90f Mon Sep 17 00:00:00 2001 From: Melanie Plageman <[email protected]> Date: Mon, 23 Nov 2026 12:27:13 -0500 Subject: [PATCH v1] Skip uninitialized pages during WAL consistency checking After a heap is truncated and re-extended, redo can find an all-zero block and skip it, but wal consitency checking's reread passes it to the heap mask, which rejects the zero header. Return InvalidBuffer instead, as for a block beyond EOF. Redo still tracks the invalid reference. RBM_NORMAL_NO_LOG originally returned zero pages for a btree cleanup lock scan that did not inspect their contents. That caller is gone. --- src/backend/access/transam/xlogutils.c | 10 +++---- src/test/recovery/t/036_truncated_dropped.pl | 30 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c index ff71e14015d..e14275f4bca 100644 --- a/src/backend/access/transam/xlogutils.c +++ b/src/backend/access/transam/xlogutils.c @@ -467,9 +467,8 @@ XLogReadBufferForRedoExtended(XLogReaderState *record, * In RBM_ZERO_* modes, if the page doesn't exist, the relation is extended * with all-zeroes pages up to the given block number. * - * In RBM_NORMAL_NO_LOG mode, we return InvalidBuffer if the page doesn't - * exist, and we don't check for all-zeroes. Thus, no log entry is made - * to imply that the page should be dropped or truncated later. + * RBM_NORMAL_NO_LOG also returns InvalidBuffer for a missing or uninitialized + * page, but does not record an invalid-page reference. * * Optionally, recent_buffer can be used to provide a hint about the location * of the page in the buffer pool; it does not have to be correct, but avoids @@ -544,7 +543,7 @@ XLogReadBufferExtended(RelFileLocator rlocator, ForkNumber forknum, } recent_buffer_fast_path: - if (mode == RBM_NORMAL) + if (mode == RBM_NORMAL || mode == RBM_NORMAL_NO_LOG) { /* check that page has been initialized */ Page page = BufferGetPage(buffer); @@ -557,7 +556,8 @@ XLogReadBufferExtended(RelFileLocator rlocator, ForkNumber forknum, if (PageIsNew(page)) { ReleaseBuffer(buffer); - log_invalid_page(rlocator, forknum, blkno, true); + if (mode == RBM_NORMAL) + log_invalid_page(rlocator, forknum, blkno, true); return InvalidBuffer; } } diff --git a/src/test/recovery/t/036_truncated_dropped.pl b/src/test/recovery/t/036_truncated_dropped.pl index bf7bba39960..eb577a29d6e 100644 --- a/src/test/recovery/t/036_truncated_dropped.pl +++ b/src/test/recovery/t/036_truncated_dropped.pl @@ -133,4 +133,34 @@ is( $node->safe_psql( $node->safe_psql('postgres', 'DROP TABLE truncme'); +# Test WAL consistency checking when a truncated heap block is reused. The +# DELETE must have a consistency image but no image to apply during redo. +# Keep the background writer from flushing the re-inserted tuple before the +# crash, so that redo of the DELETE finds the zero page left by extension. +$node->append_conf( + 'postgresql.conf', qq[ +full_page_writes = off +bgwriter_lru_maxpages = 0 +]); +$node->reload(); + +is( $node->safe_psql( + 'postgres', q[ +CREATE TABLE truncme(i int); +INSERT INTO truncme VALUES (1); +CHECKPOINT; +SET wal_consistency_checking = 'heap'; +DELETE FROM truncme; +VACUUM (TRUNCATE TRUE) truncme; +SELECT pg_relation_size('truncme'); +]), + '0', 'VACUUM truncated the heap to zero blocks'); + +$node->safe_psql('postgres', 'INSERT INTO truncme VALUES (2)'); +$node->stop('immediate'); +$node->start(); + +is($node->safe_psql('postgres', 'TABLE truncme'), + '2', 'WAL consistency checking skips the uninitialized heap block'); + done_testing(); -- 2.43.0
