Re: Fix race between copying and dropping a replication slot

From: Masahiko Sawada <sawada(dot)mshk(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>
Subject: Re: Fix race between copying and dropping a replication slot
Date: 2026-11-30 18:44:55
Message-ID: CAD21AoBJcnJwNZWRmphUUr+JGrdfHmpzE2euhhSNBDyxxM8kHw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Thu, Nov 26, 2026 at 6:39 AM 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.

Thank you for the report. I agree with your analysis.

> 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".
>
> 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.

Right.

> 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.

I've reviewed the patch and have some comments:

- if (copy_restart_lsn < src_restart_lsn ||
+ if (!second_slot_contents.in_use ||
+ src == MyReplicationSlot ||
+ copy_restart_lsn < src_restart_lsn ||

I think the new in_use check is right, but IIUC the second check (src
== MyReplicationSlot) is needed only when the source and the
destination have the same name. If the names differ and the
destination reuses the source's entry, the existing name check catches
it. Copying a slot to itself doesn't make sense to me, so I think we
should not allow such an operation at the beginning of this function
instead.

---
It's not a part of your patch but the following comment is not true
for physical slots:

* Since erroring out will release and drop the destination slot we
* don't need to release it here.
*/

While it's not a new issue that a physical slot remains when erroring
out at the creation, I think we can update the comment as well.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Sami Imseih 2026-11-30 19:15:08 Re: pgstat: Flush some statistics within running transactions, take 2
Previous Message Bernd Reiß 2026-11-30 18:42:43 Re: Stack-based tracking of per-node WAL/buffer usage