Re: Fix race between copying and dropping a replication slot

From: shveta malik <shveta(dot)malik(at)gmail(dot)com>
To: Zhijie Hou <houzhijie22(at)gmail(dot)com>
Cc: "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>, shveta malik <shvetamalik(at)gmail(dot)com>
Subject: Re: Fix race between copying and dropping a replication slot
Date: 2026-11-27 09:24:35
Message-ID: CAJpy0uDQ29hVgybbP5QtHD2uAO=wO6MCy4B2GuQvKkQRdDQMww@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Nov 26, 2026 at 8:09 PM Zhijie Hou <houzhijie22(at)gmail(dot)com> wrote:
>
> Hi,
>
> (CCing Sawada-san for replication slot copying and dynamic logical
> decoding.)
>
> While looking into dynamic logical decoding toggling on PG 19+, I found
> a race condition in copy_replication_slot() when the source slot is
> concurrently dropped during the copy operation.
>
> copy_replication_slot() copies the source slot in two phases: it first
> reads the source slot's initial metadata, creates the destination slot
> without holding the source slot, and then re-reads the source slot to
> copy its latest LSN/xmin values and verify that it still exists.
>
> However, the second-phase check does not verify the source slot's in_use
> flag. Since dropping a slot only clears in_use without resetting its
> persistent data, a concurrent drop of the source slot before the
> destination slot reserves its resources can go undetected.
>
> On PG 19+ with wal_level = replica, this is easy to hit because dropping
> the last logical slot deactivates logical decoding, and
> create_logical_replication_slot() then re-enables it for the destination
> slot. Because the drop goes undetected, the copied slot is persisted
> with LSNs preceding the deactivation record, causing subsequent decoding
> to fail with "unexpected logical decoding status change 0".

Thanks for reporting this. I agree with the problem statement.

> Older branches are also affected, although there it requires a
> checkpoint or VACUUM to remove WAL segments or catalog tuples referenced
> by the stale restart_lsn and xmin values in that window, tripping
> Assert(XLogGetLastRemovedSegno() < segno) or leaving a corrupted copied
> slot.
>
> Attached a patch to fix this by checking second_slot_contents.in_use and
> ensuring MyReplicationSlot did not reuse src in the second phase. The
> code fix applies back to PG 15.

While reviewing the patch, I think there's still a window after the
revalidation check. The second read of the source slot takes only
src->mutex, just for copying the struct, and nothing protects the
source slot after that, so it can be dropped right after the check
passes. IIUC, for a physical slot copy, the destination slot has no
xmin until copy_xmin is copied into it. If the source is dropped in
between, ReplicationSlotsComputeRequiredXmin() might see no slot
holding that xmin, the horizon might move forward, and VACUUM could
remove tuples that the destination slot's xmin is then supposed to
protect? Let me know if my understanding is wrong. I will try to
reproduce this as well.

thanks
Shveta

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Nikhil Sontakke 2026-11-27 09:56:34 Re: Dropping a composite attribute causes data integrity violations
Previous Message Daniel Gustafsson 2026-11-27 09:08:30 Re: passwordcheck: Make the user name check case-insensitive