Skip to content

Commit 8b9cbd4

Browse files
committed
Fix snapshot handling in logicalmsg_decode
Whe decoding a transactional logical message, logicalmsg_decode called SnapBuildGetOrBuildSnapshot. But we may not have a consistent snapshot yet at that point. We don't actually need the snapshot in this case (during replay we'll have the snapshot from the transaction), so in practice this is harmless. But in assert-enabled build this crashes. Fixed by requesting the snapshot only in non-transactional case, where we are guaranteed to have SNAPBUILD_CONSISTENT. Backpatch to 11. The issue exists since 9.6. Backpatch-through: 11 Reviewed-by: Andres Freund Discussion: https://postgr.es/m/84d60912-6eab-9b84-5de3-41765a5449e8@enterprisedb.com
1 parent 482ab3e commit 8b9cbd4

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/backend/replication/logical/decode.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ DecodeLogicalMsgOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
628628
TransactionId xid = XLogRecGetXid(r);
629629
uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
630630
RepOriginId origin_id = XLogRecGetOrigin(r);
631-
Snapshot snapshot;
631+
Snapshot snapshot = NULL;
632632
xl_logical_message *message;
633633

634634
if (info != XLOG_LOGICAL_MESSAGE)
@@ -658,7 +658,17 @@ DecodeLogicalMsgOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
658658
SnapBuildXactNeedsSkip(builder, buf->origptr)))
659659
return;
660660

661-
snapshot = SnapBuildGetOrBuildSnapshot(builder, xid);
661+
/*
662+
* If this is a non-transactional change, get the snapshot we're expected
663+
* to use. We only get here when the snapshot is consistent, and the
664+
* change is not meant to be skipped.
665+
*
666+
* For transactional changes we don't need a snapshot, we'll use the
667+
* regular snapshot maintained by ReorderBuffer. We just leave it NULL.
668+
*/
669+
if (!message->transactional)
670+
snapshot = SnapBuildGetOrBuildSnapshot(builder, xid);
671+
662672
ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
663673
message->transactional,
664674
message->message, /* first part of message is

src/backend/replication/logical/reorderbuffer.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,13 @@ ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid,
821821

822822
Assert(xid != InvalidTransactionId);
823823

824+
/*
825+
* We don't expect snapshots for transactional changes - we'll use the
826+
* snapshot derived later during apply (unless the change gets
827+
* skipped).
828+
*/
829+
Assert(!snapshot);
830+
824831
oldcontext = MemoryContextSwitchTo(rb->context);
825832

826833
change = ReorderBufferGetChange(rb);
@@ -839,6 +846,9 @@ ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid,
839846
ReorderBufferTXN *txn = NULL;
840847
volatile Snapshot snapshot_now = snapshot;
841848

849+
/* Non-transactional changes require a valid snapshot. */
850+
Assert(snapshot_now);
851+
842852
if (xid != InvalidTransactionId)
843853
txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
844854

0 commit comments

Comments
 (0)