Hello, A snapshot exported by CREATE_REPLICATION_SLOT ... (SNAPSHOT 'export') can show the wrong row versions under concurrent updates: it can miss row versions it must show, or show row versions committed after the slot's consistent point. A client that copies its tables under the snapshot and then streams changes from the slot can 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 xmin horizon back. The walsender's own xmin, which is the exported snapshot's, does not count, because PROC_IN_LOGICAL_DECODING is set and ComputeXidHorizons() skips such backends. At the end of the command, ReplicationSlotRelease() resets the slot's data xmin first and clears the flag only afterwards. In between, nothing protects the exported snapshot, and pruning or VACUUM can remove row versions it still needs. 0001 clears PROC_IN_LOGICAL_DECODING before the slot's data xmin is reset, so the walsender's xmin counts again before the slot stops holding the horizon back. 0002 adds an injection point right after the reset and a TAP test (src/test/recovery/t/062_slot_export_snapshot_xmin.pl). The test stops the walsender there, updates a row and runs VACUUM in another session, and then checks that the exported snapshot still shows the old row version. Without 0001 the test fails: the snapshot shows the updated row instead. I kept the test in a separate patch, so it is easy to leave out if it is not wanted. Both patches are against master. make check passes, and the new test passes with 0001 and fails without it (built with --enable-injection-points, macOS on arm64). I also ran a stress test that creates temporary slots with an exported snapshot in a loop while 16 sessions update a 200-row table, and counts the rows under each snapshot. On a REL_18_STABLE build, 50 of 8,057 snapshots missed rows; with 0001, none of 7,772 did. ReplicationSlotRelease() has the same ordering on every supported branch, so this looks like a candidate for backpatching. -- Shlomi Noach PlanetScale
v1-0001-Clear-PROC_IN_LOGICAL_DECODING-before-dropping-a-.patch
Description: Binary data
v1-0002-Add-a-test-for-the-xmin-of-a-snapshot-exported-by.patch
Description: Binary data
