From a699ebf33b2afdfcda01e65934ca816d0610ab5b Mon Sep 17 00:00:00 2001 From: Shlomi Noach Date: Sun, 29 Nov 2026 16:42:16 +0200 Subject: [PATCH v1 1/2] Clear PROC_IN_LOGICAL_DECODING before dropping a slot's data xmin A snapshot exported by CREATE_REPLICATION_SLOT ... (SNAPSHOT 'export') could show the wrong row versions: under concurrent updates, it could miss row versions that it must show, or show row versions committed after the slot's consistent point. A client that copied its tables under the snapshot and then streamed changes from the slot could then lose rows, or receive a change both in the copy and from the slot. While the walsender creates the slot, the slot's temporary data xmin (effective_xmin) holds the horizon back. The walsender's own xmin, the exported snapshot's, does not count: PROC_IN_LOGICAL_DECODING is set, and ComputeXidHorizons() skips such backends. ReplicationSlotRelease() dropped the slot's data xmin first and cleared the flag only afterwards, so for a moment nothing protected the exported snapshot, and pruning or VACUUM could act on a horizon that had passed it. Clear PROC_IN_LOGICAL_DECODING before dropping the slot's data xmin, so the walsender's xmin counts again before the slot stops protecting the snapshot. On a REL_18_STABLE build, a test that creates temporary slots with an exported snapshot while 16 sessions update a 200-row table found snapshots with missing rows in 50 of 8,057 slots; with this change, in none of 7,772. The same ordering is on every supported branch. --- src/backend/replication/slot.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index 5a4bf6638e0..080cae21672 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -782,6 +782,25 @@ ReplicationSlotRelease(void) if (am_walsender) slotname = pstrdup(NameStr(slot->data.name)); + /* + * Clear PROC_IN_LOGICAL_DECODING before the slot's data xmin is reset + * below. Otherwise, nothing would hold back the xmin horizon for a + * snapshot this backend exported, and pruning could remove tuples it + * still needs. + * + * Update statusFlags only if PROC_IN_LOGICAL_DECODING is set. An + * auxiliary process (checkpointer or startup process) may invalidate a + * slot. Auxiliary processes are not in the proc array, and pgxactoff + * would point at something which is not its own. + */ + if (MyProc->statusFlags & PROC_IN_LOGICAL_DECODING) + { + LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); + MyProc->statusFlags &= ~PROC_IN_LOGICAL_DECODING; + ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; + LWLockRelease(ProcArrayLock); + } + if (slot->data.persistency == RS_EPHEMERAL) { /* @@ -831,20 +850,6 @@ ReplicationSlotRelease(void) MyReplicationSlot = NULL; } - /* - * Update statusFlags only if PROC_IN_LOGICAL_DECODING is set. An - * auxiliary process (checkpointer or startup process) may invalidate a - * slot. Auxiliary processes are not in the proc array, and pgxactoff - * would point at something which is not its own. - */ - if (MyProc->statusFlags & PROC_IN_LOGICAL_DECODING) - { - LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); - MyProc->statusFlags &= ~PROC_IN_LOGICAL_DECODING; - ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; - LWLockRelease(ProcArrayLock); - } - if (am_walsender) { ereport(log_replication_commands ? LOG : DEBUG1, -- 2.50.1 (Apple Git-155)