Skip to content

Commit e2999ab

Browse files
committed
Fix assertion failure in logical decoding.
Logical decoding set SnapshotData's regd_count field to avoid the snapshot manager from prematurely freeing snapshots that are generated by the decoding system. That was always an abuse of the field, as it was never supposed to be used outside the snapshot manager. Commit 9402869 made snapshot manager's tracking of the snapshots smarter, and that scheme fell apart. The snapshot manager got confused and hit the assertion, when a snapshot that was marked with regd_count==1 was not found in the heap, where the snapshot manager tracks registered the snapshots. To fix, don't abuse the regd_count field like that. Logical decoding still abuses the active_count field for similar purposes, but that's currently harmless. The assertion failure was first reported by Michael Paquier
1 parent 90898af commit e2999ab

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/backend/replication/logical/reorderbuffer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,8 +1188,8 @@ ReorderBufferCopySnap(ReorderBuffer *rb, Snapshot orig_snap,
11881188
memcpy(snap, orig_snap, sizeof(SnapshotData));
11891189

11901190
snap->copied = true;
1191-
snap->active_count = 0;
1192-
snap->regd_count = 1;
1191+
snap->active_count = 1; /* mark as active so nobody frees it */
1192+
snap->regd_count = 0;
11931193
snap->xip = (TransactionId *) (snap + 1);
11941194

11951195
memcpy(snap->xip, orig_snap->xip, sizeof(TransactionId) * snap->xcnt);

src/backend/replication/logical/snapbuild.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ SnapBuildFreeSnapshot(Snapshot snap)
348348
Assert(snap->curcid == FirstCommandId);
349349
Assert(!snap->suboverflowed);
350350
Assert(!snap->takenDuringRecovery);
351-
Assert(snap->regd_count == 1);
351+
Assert(snap->regd_count == 0);
352352

353353
/* slightly more likely, so it's checked even without c-asserts */
354354
if (snap->copied)
@@ -407,16 +407,16 @@ SnapBuildSnapDecRefcount(Snapshot snap)
407407
Assert(!snap->suboverflowed);
408408
Assert(!snap->takenDuringRecovery);
409409

410-
Assert(snap->regd_count == 1);
410+
Assert(snap->regd_count == 0);
411411

412-
Assert(snap->active_count);
412+
Assert(snap->active_count > 0);
413413

414414
/* slightly more likely, so it's checked even without casserts */
415415
if (snap->copied)
416416
elog(ERROR, "cannot free a copied snapshot");
417417

418418
snap->active_count--;
419-
if (!snap->active_count)
419+
if (snap->active_count == 0)
420420
SnapBuildFreeSnapshot(snap);
421421
}
422422

@@ -495,7 +495,7 @@ SnapBuildBuildSnapshot(SnapBuild *builder, TransactionId xid)
495495
snapshot->copied = false;
496496
snapshot->curcid = FirstCommandId;
497497
snapshot->active_count = 0;
498-
snapshot->regd_count = 1; /* mark as registered so nobody frees it */
498+
snapshot->regd_count = 0;
499499

500500
return snapshot;
501501
}

0 commit comments

Comments
 (0)