Report
Prepare before two_phase_at is sent, then FinishPrepared asserts or repeats it
- Severity
- 50
- Blamed commit
- a8fd13cab0ba815e9925dc9676e6309f699b5f72
On 6 branches
REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Enabling two-phase logical decoding at a start LSN ahead of the slot's confirmed_flush stores that LSN in two_phase_at and leaves confirmed_flush behind. A later decode that starts at confirmed_flush sends a PREPARE whose LSN falls in the gap, then treats the same transaction as one whose prepare was skipped. On an assert-enabled build the decoding backend aborts. On a production build the PREPARE is sent twice, and COMMIT PREPARED follows the second copy. A subscriber that applied the first PREPARE rejects the second and stops.
The score is 50. The nearest anchor is 40: the window is unlikely, and a client that keeps reading from the LSN where two-phase was enabled does not decode the gap. Ten points are added because an assert build restarts the server, and a production build leaves a pgoutput subscriber failing on the duplicate transaction id until that prepared transaction is removed by hand.
Introducing commit
a8fd13cab0ba815e9925dc9676e6309f699b5f72 ("Add support for prepared transactions to built-in logical replication") stores the decoding start LSN in two_phase_at the first time two-phase decoding is enabled, and ReorderBufferFinishPrepared replays a commit of a prepare that precedes that LSN on the assumption that the prepare was not sent. DecodePrepare never learned to skip those prepares. The commit is an ancestor of master and of REL_15_STABLE through REL_19_STABLE. REL_14_STABLE has no two_phase_at and is not affected.
The assertion that aborts assert builds was added later, in abfb29648f9adcde84656afde1d50bc8b8e9b6e0, and is present on master, REL_19_STABLE, and REL_18_STABLE. REL_15_STABLE through REL_17_STABLE do not have that assertion; they still send the second PREPARE.
Reproduction
Use a server with wal_level=logical and max_prepared_transactions at least 1. Create a pgoutput slot without two-phase, consume it once so confirmed_flush is current, then prepare a transaction and move WAL past it:
CREATE TABLE t (id int primary key);
CREATE PUBLICATION pub FOR TABLE t;
SELECT pg_create_logical_replication_slot('s', 'pgoutput');
INSERT INTO t VALUES (1);
SELECT pg_logical_slot_get_binary_changes('s', NULL, NULL,
'proto_version', '4', 'publication_names', 'pub');
BEGIN;
INSERT INTO t VALUES (2);
PREPARE TRANSACTION 'g3';
INSERT INTO t VALUES (3);
SELECT pg_current_wal_lsn();
Start decoding at that LSN with two_phase on, and disconnect before the client confirms a flush. The slot is saved as soon as decoding starts. pg_recvlogical does this:
pg_recvlogical -d postgres -S s --start -I <that LSN> -f - \
-o proto_version=4 -o publication_names=pub -o two_phase=on
The slot then has two_phase true and two_phase_at equal to the start LSN, still ahead of confirmed_flush. In one run those values were two_phase_at 0/017CF770 and confirmed_flush 0/017CF548. Commit the prepared transaction and decode from confirmed_flush:
COMMIT PREPARED 'g3';
SELECT get_byte(data, 0)
FROM pg_logical_slot_peek_binary_changes('s', NULL, NULL,
'proto_version', '4', 'publication_names', 'pub',
'two_phase', 'on');
REL_15_STABLE accepts protocol version 3, not 4. The same steps with proto_version 3 reach the same state.
Expected
The server stays up. The prepare in the gap is not sent at PREPARE time. At COMMIT PREPARED it is sent once: begin prepare, the insert, PREPARE, and COMMIT PREPARED. The ordinary insert that committed in the gap is sent as its own transaction. The message type bytes are 66, 82, 73, 67, 98, 73, 80, 75: one PREPARE (80) and one COMMIT PREPARED (75).
Actual
Assert build, master (reorderbuffer.c line 3060; REL_19_STABLE and REL_18_STABLE have the same check):
TRAP: failed Assert("(txn->txn_flags & RBTXN_PREPARE_STATUS_MASK) == (RBTXN_IS_PREPARED | RBTXN_SKIPPED_PREPARE)"), File: "../src/backend/replication/logical/reorderbuffer.c", Line: 3060
ReorderBufferFinishPrepared
xact_decode
LogicalDecodingProcessRecord
pg_logical_slot_peek_binary_changes
The decoding backend is terminated by signal 6 and the postmaster reinitializes.
Without that assertion, DecodePrepare has already emitted begin prepare, the insert, and PREPARE. FinishPrepared still takes the final_lsn < two_phase_at branch, replays the transaction after its changes were discarded, and emits begin prepare and PREPARE again, then COMMIT PREPARED. The stream contains two PREPARE messages for one GID. A pgoutput subscriber accepts the first and rejects the second with an error that the transaction identifier is already in use. The apply worker keeps failing until that prepared transaction is removed by hand.
Diagnosis
CreateDecodingContext sets two_phase and two_phase_at to the requested start LSN, then saves the slot, when two-phase decoding is turned on for a slot that did not have it. confirmed_flush is not advanced to that LSN. A start position ahead of confirmed_flush is otherwise valid: only a start behind confirmed_flush is forwarded.
two_phase_at is the point from which prepares are sent at PREPARE time. ReorderBufferFinishPrepared replays a commit whose prepare LSN is lower and requires the flags RBTXN_IS_PREPARED | RBTXN_SKIPPED_PREPARE. That matches a prepare skipped because decoding had not reached a consistent snapshot, or because the record is before the decoding start LSN.
FilterPrepare does not look at two_phase_at. It returns false whenever two-phase decoding is enabled and the output plugin does not filter the GID, so DecodePrepare calls ReorderBufferPrepare and sets RBTXN_SENT_PREPARE. A later session that starts at confirmed_flush, behind two_phase_at, therefore both sends the prepare and later takes the skipped-prepare branch.
Clamping the stored two_phase_at down to confirmed_flush is the wrong repair. The session that enabled two-phase started at the higher LSN, so it skipped the prepare record as before its start position and depends on two_phase_at remaining there to replay the transaction at COMMIT PREPARED. Saving a lower value drops those changes if that session restarts before the client confirms the commit.
Fix
In DecodePrepare, after the usual skip checks, call ReorderBufferSkipPrepare and return when the prepare LSN is before SnapBuildGetTwoPhaseAt. The changes stay queued. FinishPrepared's existing branch then replays them once and sends one PREPARE followed by COMMIT PREPARED. A prepare at or after two_phase_at is unchanged: it is sent at PREPARE time, and commit sends only COMMIT PREPARED.
With that change the reproduction returns the eight message bytes listed under Expected, and the server log has no TRAP. The same single PREPARE and single COMMIT PREPARED result was checked on REL_19_STABLE through REL_15_STABLE. A prepare written after two_phase_at is still emitted once, at PREPARE time.