Hash index corruption and CLOG lookup errors from checkpoint races

From: shihao zhong <zhong950419(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
Subject: Hash index corruption and CLOG lookup errors from checkpoint races
Date: 2026-11-01 01:05:27
Message-ID: CAGRkXqTSAb-H4oxk=J9QBv-Kh2-o=cnCWuiSduMVaVzrKYmKXQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Subject: Index corruption: two more checkpoint races

Hi,

Two more races like [1] and [2]. A checkpoint can start between a WAL
record and the file change.

1. vac_truncate_clog() truncates CLOG before it advances oldestXid.
After a crash, pg_xact_status() on a removed XID fails with "could not
open file pg_xact/0000" instead of returning NULL. A client that
checks an old transaction after a reconnect gets an error.

The next vacuum does not fix it. It stays until another CLOG segment
is removed.

0001 swaps the two steps, like commit_ts. 0002 is the repro.

2. _hash_alloc_buckets() WAL-logs the new splitpoint before it extends
the file. If a base backup starts in between, the hash index on the new
standby or restored server is broken. It looks fine at first. Once the
index needs an overflow page, inserts fail with "access to noncontiguous
page" until a REINDEX.

A power loss at the same moment can do the same on the primary.

0003 sets DELAY_CHKPT_START around the two steps,
like bulk_write.c. 0004 is the repro.

The fixes apply to 14 and up. 14 needs the old delayChkpt flag. The
tests need 18 and up.

[1]
https://www.postgresql.org/message-id/CAGRkXqRhpq=fd8Qf_-Ki1OW7b1iL+o3YuHiHQhLw5RUJy2JGXQ@mail.gmail.com
[2]
https://www.postgresql.org/message-id/CAGRkXqSuiJ_X4+Z6rsD42pcuACnD9PhoQ-Os+mUJNmJarBWkNg@mail.gmail.com

Thanks,
Shihao

Attachment Content-Type Size
v1-0002-Add-test-for-a-checkpoint-that-starts-during-CLOG.patch application/octet-stream 4.9 KB
v1-0003-Delay-checkpoint-start-while-allocating-hash-inde.patch application/octet-stream 2.9 KB
v1-0001-Advance-oldestXid-before-truncating-CLOG.patch application/octet-stream 3.8 KB
v1-0004-Add-test-for-a-checkpoint-that-starts-during-hash.patch application/octet-stream 7.2 KB

Browse pgsql-hackers by date

  From Date Subject
Previous Message shihao zhong 2026-11-01 00:56:33 Re: Incremental backup can use a summary past a timeline switch