Racing in ROLLBACK PREAPRED and checkpoint

From: shihao zhong <zhong950419(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, Robert Haas <robertmhaas(at)gmail(dot)com>
Subject: Racing in ROLLBACK PREAPRED and checkpoint
Date: 2026-10-29 04:00:56
Message-ID: CAGRkXqRhpq=fd8Qf_-Ki1OW7b1iL+o3YuHiHQhLw5RUJy2JGXQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

RecordTransactionAbortPrepared() does not set DELAY_CHKPT_START, unlike
the commit path. So a prepared transaction that was rolled back can come
back after a crash:

1. ROLLBACK PREPARED writes and flushes its abort record.
2. A checkpoint runs with its redo pointer after that record. pg_xact
does not show the abort yet, so the state file is kept on disk.
3. ROLLBACK PREPARED sets the abort in pg_xact and removes the state
file.
4. The OS crashes before that removal is durable.
5. Recovery starts after the abort record. It finds the state file and
pg_xact says nothing, so the transaction is prepared again.
6. COMMIT PREPARED now succeeds.

Then the WAL will have ROLLBACK and CoMMIT for the same xid.

Standbys and already saw the abort in step 1. After
step 6, a standby with assertions fails an assert in clog.c and stops.

TRAP: failed Assert("curval == 0 || (curval ==
TRANSACTION_STATUS_SUB_COMMITTED && status !=
TRANSACTION_STATUS_IN_PROGRESS) || curval == status")

Without assertions it silently marks the transaction committed.

The fix is to add DELAY_CHKPT_START same as the commit side.

The commit side has had this since the two-phase commit went in. The abort
side was written like a regular abort, and its comment says the pg_xact
update "is not absolutely necessary". That is true for a regular abort,
but here the state file can bring the transaction back.

This needs to be backported to 14.

It feels like these WAL, file change operations are all at risk, mutlixact
may also have the same issue, it is late here, I will check it tomorrow.

Thanks,
Shihao

Attachment Content-Type Size
v1-0001-Delay-checkpoint-start-in-RecordTransactionAbortP.patch application/octet-stream 2.8 KB
v1-0002-Add-test-for-checkpoint-during-ROLLBACK-PREPARED.patch application/octet-stream 5.3 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Baji Shaik 2026-10-29 04:01:15 Re: two small tab-completion patches (ALTER CONSTRAINT, CHECK modifiers)
Previous Message shihao zhong 2026-10-29 03:02:06 Re: ALTER COLUMN SET NOT NULL differ from ALTER TABLE ADD CONSTRAINT