Re: pg_rewind and hint bits leads to visibility corruption

From: Jan Nidzwetzki <jan(at)planetscale(dot)com>
To: shihao zhong <zhong950419(at)gmail(dot)com>, Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org, Tom Pang <tom(at)planetscale(dot)com>
Subject: Re: pg_rewind and hint bits leads to visibility corruption
Date: 2026-11-23 15:31:39
Message-ID: 03903832-a9d4-41d3-8f67-664662ff7178@planetscale.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Zsolt, hi Shihao,

Thanks for the reviews. Attached is v2, which addresses the first two
points from Zsolt, mostly as Shihao suggested.

On 21.11.26 18:56, shihao zhong wrote:

> MarkBufferDirtyHint() forces a WAL record on a dirty page, even for
> hints that don't depend on a transaction outcome. The pd_prune_xid
> update in heap_page_prune_and_freeze() hits this on every page. With
> 1M rows committed before the last checkpoint, the first scan after
> promotion wrote 85 MB.

Thanks for measuring this and for the diff. I agree, this is the main
reason for the WAL burst that Zsolt mentioned. Since an INSERT sets
pd_prune_xid, v1 wrote a full-page image for every page pruned by the
first scan after a promotion. As you said, a stale pd_prune_xid is not a
problem, and the same goes for other hints set by MarkBufferDirtyHint().
Because of this, v2 no longer forces a WAL record for these on a dirty
buffer. However, on a clean buffer, they are still WAL-logged until the
first checkpoint after a promotion, since the buffer could have hints
set in memory during recovery. This adds at most one WAL record per page
that was last changed before the last replayed checkpoint.

In a similar test with 1 million rows, shared_buffers set to 3GB, and
data checksums enabled, the first scan after promotion wrote 60.8 MB of
WAL with v1, but less than 20 kB with v2 and the unpatched version. This
scan is still a bit slower with v2 (53 ms compared to 47 ms, based on
the median of five runs), probably because of the xid check on pages
that are still dirty from recovery. For the index-only scan after a
promotion from my last email, v2 wrote 1.318 GB, while the unpatched
version wrote 1.317 GB and v1 wrote 1.338 GB.

> For Zsolt's 2:
>
> xlog_redo() already gets the oldest prepared xid from
> PrescanPreparedTransactions()
> for a shutdown checkpoint, we can just reuse this?

Yes, good idea, v2 does this now. Only prepared transactions make it
through a shutdown checkpoint, so any xid lower than the oldest prepared
one finished before that point. This value is only calculated when hot
standby is enabled. If hot standby is off, no queries run before
promotion, and unsafe hints after promotion are WAL-logged as in v1. The
TAP test now includes a scenario where the primary is restarted cleanly
before the fork.

On 21.11.26 10:22, Zsolt Parragi wrote:

> And while testing this I found a different issue: pg_rewind can't
> detect properly a mixed wal_log_hint_bits cluster if data checksums
> are also off, and operates in scenarios it shouldn't be; I'll create a
> separate thread about that

I saw a similar thing while working on this patch. pg_rewind looks at
the value in the target's control file, which on a standby reflects the
primary's setting, but XLogHintBitIsNeeded() uses the local setting.
This issue isn't related to this patch, so thanks for opening a separate
thread.

Changes in v2:

* MarkBufferDirtyHint() no longer forces a WAL record on a dirty buffer
(Shihao).

* A replayed shutdown checkpoint uses the oldest prepared xid instead of
oldestActiveXid (Shihao).

* New TAP test scenario with a clean restart of the primary.

* The commit message mentions the remaining WAL overhead after a promotion.

Best regards
Jan

--
Jan Nidzwetzki
PlanetScale

Attachment Content-Type Size
v2-0001-Don-t-let-unlogged-hint-bits-reach-disk-on-buffer.patch text/plain 30.1 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Daniel Migowski 2026-11-23 16:44:57 Re: BUG #19787: "could not find pathkey item to sort" for Gather Merge over a merge join below reordered LEFT JOINs
Previous Message PG Bug reporting form 2026-11-23 14:46:01 BUG #19932: `repack (concurrently)` does not work in `psql` with `AUTOCOMMIT` set to `off`