Re: Stop masking visibility information during WAL consistency checking

From: Melanie Plageman <melanieplageman(at)gmail(dot)com>
To: Zhijie Hou <houzhijie22(at)gmail(dot)com>
Cc: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org, surya poondla <suryapoondla4(at)gmail(dot)com>, Andres Freund <andres(at)anarazel(dot)de>, Michael Paquier <michael(at)paquier(dot)xyz>
Subject: Re: Stop masking visibility information during WAL consistency checking
Date: 2026-11-24 16:10:52
Message-ID: CAAKRu_YKoZfbOCBY6LXEZhoqdcuNwnNM8UCVBeM05b+pyO3OzA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Nov 21, 2026 at 7:18 AM Zhijie Hou <houzhijie22(at)gmail(dot)com> 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

Attachment Content-Type Size
v1-0001-Skip-uninitialized-pages-during-WAL-consistency-c.patch text/x-patch 3.7 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2026-11-24 16:12:48 Re: [PATCH] Fix encoding-dependent LIKE/regex selectivity estimates
Previous Message Matheus Alcantara 2026-11-24 15:49:55 Re: [PATCH v1] Fix for Bug#19724 - ALTER TYPE ... ALTER ATTRIBUTE triggers internal error for base type of domain with check