Skip to content

Commit 7fe1aa9

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 d0460a3 commit 7fe1aa9

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
@@ -564,7 +564,7 @@ logicalmsg_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
564564
TransactionId xid = XLogRecGetXid(r);
565565
uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
566566
RepOriginId origin_id = XLogRecGetOrigin(r);
567-
Snapshot snapshot;
567+
Snapshot snapshot = NULL;
568568
xl_logical_message *message;
569569

570570
if (info != XLOG_LOGICAL_MESSAGE)
@@ -594,7 +594,17 @@ logicalmsg_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
594594
SnapBuildXactNeedsSkip(builder, buf->origptr)))
595595
return;
596596

597-
snapshot = SnapBuildGetOrBuildSnapshot(builder);
597+
/*
598+
* If this is a non-transactional change, get the snapshot we're expected
599+
* to use. We only get here when the snapshot is consistent, and the
600+
* change is not meant to be skipped.
601+
*
602+
* For transactional changes we don't need a snapshot, we'll use the
603+
* regular snapshot maintained by ReorderBuffer. We just leave it NULL.
604+
*/
605+
if (!message->transactional)
606+
snapshot = SnapBuildGetOrBuildSnapshot(builder);
607+
598608
ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
599609
message->transactional,
600610
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
@@ -856,6 +856,13 @@ ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid,
856856

857857
Assert(xid != InvalidTransactionId);
858858

859+
/*
860+
* We don't expect snapshots for transactional changes - we'll use the
861+
* snapshot derived later during apply (unless the change gets
862+
* skipped).
863+
*/
864+
Assert(!snap);
865+
859866
oldcontext = MemoryContextSwitchTo(rb->context);
860867

861868
change = ReorderBufferGetChange(rb);
@@ -874,6 +881,9 @@ ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid,
874881
ReorderBufferTXN *txn = NULL;
875882
volatile Snapshot snapshot_now = snap;
876883

884+
/* Non-transactional changes require a valid snapshot. */
885+
Assert(snapshot_now);
886+
877887
if (xid != InvalidTransactionId)
878888
txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
879889

0 commit comments

Comments
 (0)