| From: | Shlomi Noach <shlomi(at)planetscale(dot)com> |
|---|---|
| To: | pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Subject: | Exported snapshot of CREATE_REPLICATION_SLOT can lose tuples to pruning |
| Date: | 2026-11-30 10:12:46 |
| Message-ID: | CAEQBPGoDOjUPvEbGyHTTmQ5JduNv=jmy4ZwdApo6cZGNYudzFQ@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
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
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Clear-PROC_IN_LOGICAL_DECODING-before-dropping-a-.patch | application/octet-stream | 3.5 KB |
| v1-0002-Add-a-test-for-the-xmin-of-a-snapshot-exported-by.patch | application/octet-stream | 4.7 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Alain LESAGE | 2026-11-30 10:22:45 | Re: Improve hash aggregate spilling by writing only the needed columns |
| Previous Message | Amit Kapila | 2026-11-30 09:52:57 | Re: Preserve conflict log table during upgrade |