Skip to content

Commit 497f863

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 52dbd9f commit 497f863

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
@@ -517,7 +517,7 @@ DecodeLogicalMsgOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
517517
TransactionId xid = XLogRecGetXid(r);
518518
uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
519519
RepOriginId origin_id = XLogRecGetOrigin(r);
520-
Snapshot snapshot;
520+
Snapshot snapshot = NULL;
521521
xl_logical_message *message;
522522

523523
if (info != XLOG_LOGICAL_MESSAGE)
@@ -547,7 +547,17 @@ DecodeLogicalMsgOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
547547
SnapBuildXactNeedsSkip(builder, buf->origptr)))
548548
return;
549549

550-
snapshot = SnapBuildGetOrBuildSnapshot(builder, xid);
550+
/*
551+
* If this is a non-transactional change, get the snapshot we're expected
552+
* to use. We only get here when the snapshot is consistent, and the
553+
* change is not meant to be skipped.
554+
*
555+
* For transactional changes we don't need a snapshot, we'll use the
556+
* regular snapshot maintained by ReorderBuffer. We just leave it NULL.
557+
*/
558+
if (!message->transactional)
559+
snapshot = SnapBuildGetOrBuildSnapshot(builder, xid);
560+
551561
ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
552562
message->transactional,
553563
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
@@ -622,6 +622,13 @@ ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid,
622622

623623
Assert(xid != InvalidTransactionId);
624624

625+
/*
626+
* We don't expect snapshots for transactional changes - we'll use the
627+
* snapshot derived later during apply (unless the change gets
628+
* skipped).
629+
*/
630+
Assert(!snapshot);
631+
625632
oldcontext = MemoryContextSwitchTo(rb->context);
626633

627634
change = ReorderBufferGetChange(rb);
@@ -640,6 +647,9 @@ ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid,
640647
ReorderBufferTXN *txn = NULL;
641648
volatile Snapshot snapshot_now = snapshot;
642649

650+
/* Non-transactional changes require a valid snapshot. */
651+
Assert(snapshot_now);
652+
643653
if (xid != InvalidTransactionId)
644654
txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
645655

0 commit comments

Comments
 (0)