Prepared transaction lost after restoring a base backup or after a crash during a checkpoint, with data corruption (17, 18)

From: Greg Lamberson <greg(at)lamco(dot)io>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Prepared transaction lost after restoring a base backup or after a crash during a checkpoint, with data corruption (17, 18)
Date: 2026-11-23 19:44:43
Message-ID: 179546308369.2122612.4038951160263037363.pgsql-bugs@lamco.io
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

On PostgreSQL 17 and 18 a restored base backup can come up without a transaction that was prepared while the backup was running. The same thing happens after a crash at the wrong moment in a checkpoint. If the tables that transaction touched are used before the server is restarted again, the damage is permanent: The transaction ends up committed with none of its changes, or there are two live rows under one primary key. I found this while testing recovery for a storage manager extension I'm working on.

To see it with a base backup and no crash, start pg_basebackup against a server that has max_prepared_transactions above zero. While it's copying files, run two small transactions, prepare a third as 'kept', and run CHECKPOINT. If that checkpoint writes the state file before pg_basebackup copies pg_twophase, a server started on the copy logs this and has nothing in pg_prepared_xacts:

WARNING: removing future two-phase state file for transaction 755
WARNING: could not remove file "pg_twophase/FFFFFFFF000002F3": No such file or directory
WARNING: could not recover two-phase state file for transaction 755

COMMIT PREPARED 'kept' says the transaction doesn't exist, and after one more restart it's back. A crash between CheckPointTwoPhase() and the control file update does the same thing. I confirmed that with gdb stopped in update_controlfile() and a kill -9 of the server's processes.

While the transaction is missing, its xid reads as aborted and it holds no locks. In my tests on 18.7, another session updating one of its rows by primary key left two live versions of that row after the restart and COMMIT PREPARED, with errors from bt_index_check() and verify_heapam(). A sequential scan or a VACUUM of the table left the transaction committed with none of its changes.

The cause is the removal of a future two-phase file at startup. The name is rebuilt from the 32-bit xid and nextXid, which gives the wrong epoch for an xid above nextXid, so unlink() fails. Vitaly Davydov reported that file name in December 2024:

https://postgr.es/m/13b5b6-676c3080-4d-531db900@47931709

What I haven't seen described is what follows from it. The file is still there when replay reaches the PREPARE record, so PrepareRedoAdd() skips the record.

The details:

- Affected: 17.0, 17.12, 18.0, 18.7 and the tips of both branches. Assert-enabled builds don't start at all. 16.16 and 19.0 aren't affected.
- Condition: The state file's xid is strictly above the nextXid of the checkpoint that recovery starts from.
- Patch: One for each of REL_17_STABLE and REL_18_STABLE, attached. restoreTwoPhaseData() already parses a FullTransactionId from each file name, and the patch passes it down so a rejected file is removed under the name it was found with. Only static functions in twophase.c change. It doesn't change which files are rejected and it judges no file by its epoch, which is where it differs from the fix that was reverted in January 2025. It adds a test to 009_twophase.pl.
- Tested: Both branches pass make check and the recovery TAP suite on production and assert-enabled builds, and make check-world on production builds. The new test fails without the fix. With it, the crash, base backup and data cases above come out right.
- Scripts: Three are attached, for the base backup case, the crash case and the data damage. They need only initdb, pg_ctl, psql and pg_basebackup, and each prints PASS or FAIL.

Greg Lamberson

Attachment Content-Type Size
v1-0001-Fix-removal-of-two-phase-state-files-in-the-future-REL_17.patch text/x-patch 12.3 KB
v1-0001-Fix-removal-of-two-phase-state-files-in-the-future-REL_18.patch text/x-patch 12.3 KB
twophase-basebackup-repro.sh text/x-shellscript 6.6 KB
twophase-crash-repro.sh text/x-shellscript 4.9 KB
twophase-damage-repro.sh text/x-shellscript 9.8 KB

Browse pgsql-bugs by date

  From Date Subject
Next Message Tor Erik Linnerud 2026-11-23 19:54:20 Re: PG 18 JIT regression: int4range hash breaks multi-key GROUP BY
Previous 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