diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c index 86421e4b8b..19dec04cda 100644 --- a/src/backend/replication/logical/decode.c +++ b/src/backend/replication/logical/decode.c @@ -206,12 +206,16 @@ xact_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) uint8 info = XLogRecGetInfo(r) & XLOG_XACT_OPMASK; /* - * If the snapshot isn't yet fully built, we cannot decode anything, so - * bail out. + * If the snapshot hasn't started building yet, the transaction won't be + * decoded or tracked by the snapshot, so bail out. */ - if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT) + if (SnapBuildCurrentState(builder) < SNAPBUILD_BUILDING_SNAPSHOT) return; + /* + * Note that if the snapshot isn't yet fully built, the xlog is only used + * to build the snapshot and won't be decoded. + */ switch (info) { case XLOG_XACT_COMMIT: @@ -418,7 +422,7 @@ heap2_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) * determining the candidate catalog_xmin for the replication slot. See * SnapBuildProcessRunningXacts(). */ - if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT) + if (SnapBuildCurrentState(builder) < SNAPBUILD_BUILDING_SNAPSHOT) return; switch (info) @@ -429,15 +433,27 @@ heap2_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) DecodeMultiInsert(ctx, buf); break; case XLOG_HEAP2_NEW_CID: - if (!ctx->fast_forward) + if (!ctx->fast_forward && + SnapBuildCurrentState(builder) >= SNAPBUILD_FULL_SNAPSHOT) { xl_heap_new_cid *xlrec; xlrec = (xl_heap_new_cid *) XLogRecGetData(buf->record); SnapBuildProcessNewCid(builder, xid, buf->origptr, xlrec); - - break; } + else + { + /* + * In BUILDING_SNAPSHOT or fast-forward mode, the transaction + * will not be replayed, so tuple CID mappings and command IDs + * are not needed, but we still must mark the transaction as + * having catalog changes so that the historic snapshot tracks + * it. + */ + ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, + buf->origptr); + } + break; case XLOG_HEAP2_REWRITE: /* @@ -1304,6 +1320,7 @@ DecodeTXNNeedSkip(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, Oid txn_dbid, RepOriginId origin_id) { if (SnapBuildXactNeedsSkip(ctx->snapshot_builder, buf->origptr) || + SnapBuildCurrentState(ctx->snapshot_builder) < SNAPBUILD_CONSISTENT || (txn_dbid != InvalidOid && txn_dbid != ctx->slot->data.database) || FilterByOrigin(ctx, origin_id)) return true; diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c index 490dec290a..59f49ccbfa 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -3228,12 +3228,15 @@ ReorderBufferForget(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn) * are any. Even if we're not interested in the transaction's contents, it * could have manipulated the catalog and we need to update the caches * according to that. + * + * Note that transactions committing in SNAPBUILD_BUILDING_SNAPSHOT will + * not have a base snapshot even if they accumulated invalidation + * messages; no snapshot has been built or used to access catalogs yet, so + * those invalidations do not need to be executed. */ if (txn->base_snapshot != NULL && txn->ninvalidations > 0) ReorderBufferImmediateInvalidation(rb, txn->ninvalidations, txn->invalidations); - else - Assert(txn->ninvalidations == 0); /* remove potential on-disk data, and deallocate */ ReorderBufferCleanupTXN(rb, txn); diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c index 228ecd1887..5d001fca10 100644 --- a/src/backend/replication/logical/snapbuild.c +++ b/src/backend/replication/logical/snapbuild.c @@ -991,10 +991,15 @@ SnapBuildCommitTxn(SnapBuild *builder, XLogRecPtr lsn, TransactionId xid, /* * Transactions preceding BUILDING_SNAPSHOT will neither be decoded, nor * will they be part of a snapshot. So we don't need to record anything. + * Note that a transaction whose top-level XID precedes next_phase_at may + * still have subtransactions assigned during BUILDING_SNAPSHOT (>= + * next_phase_at); since subxacts are ordered by XID assignment (see + * AtSubCommit_childXids), check the last subxact XID if any. */ if (builder->state == SNAPBUILD_START || (builder->state == SNAPBUILD_BUILDING_SNAPSHOT && - TransactionIdPrecedes(xid, builder->next_phase_at))) + TransactionIdPrecedes(nsubxacts > 0 ? subxacts[nsubxacts - 1] : xid, + builder->next_phase_at))) { /* ensure that only commits after this are getting replayed */ if (builder->start_decoding_at <= lsn)