Patch
Skip prepares that fall before two_phase_at
- Branch
- REL_17_STABLE
- Risk
- –
- Base commit
- d35a59f1b9bcb239bdd6f1378fb996dac251ef7f
To apply it to a checkout of REL_17_STABLE at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1cb93-919f-711f-86c1-0e6de50ed321 | jq -r .body | git am
From 492c080bcd4e9ea4251245e8da8becb90b0ef4dc Mon Sep 17 00:00:00 2001
From: Grok <grok@x.ai>
Date: Wed, 11 Nov 2026 09:00:26 -0800
Subject: [PATCH] Skip prepares that fall before two_phase_at.
Enabling two-phase decoding at a start LSN ahead of confirmed_flush
persists that LSN as two_phase_at. A later decode from confirmed_flush
sends a PREPARE in the gap, then ReorderBufferFinishPrepared assumes
the prepare was skipped. Assert builds abort; other builds send the
PREPARE twice.
Skip those prepares so FinishPrepared replays them once at COMMIT
PREPARED.
---
src/backend/replication/logical/decode.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index 08615aeba60..7353b0f8dd4 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -813,6 +813,20 @@ DecodePrepare(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
return;
}
+ /*
+ * Do not send a prepare that falls before two_phase_at. Decoding may
+ * start at confirmed_flush, which can sit behind two_phase_at when
+ * two-phase decoding was enabled at a later start LSN.
+ * ReorderBufferFinishPrepared replays such a transaction at COMMIT
+ * PREPARED and requires that the prepare was skipped, so sending it here
+ * would duplicate the PREPARE and fail that check.
+ */
+ if (buf->origptr < SnapBuildGetTwoPhaseAt(builder))
+ {
+ ReorderBufferSkipPrepare(ctx->reorder, xid);
+ return;
+ }
+
/* Tell the reorderbuffer about the surviving subtransactions. */
for (i = 0; i < parsed->nsubxacts; i++)
{
base-commit: d35a59f1b9bcb239bdd6f1378fb996dac251ef7f
--
2.53.0