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: shveta malik <shveta(dot)malik(at)gmail(dot)com>, "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>, shveta malik <shvetamalik(at)gmail(dot)com>
Subject: Re: Fix race between copying and dropping a replication slot
Date: 2026-12-01 19:41:25
Message-ID: CAD21AoBMWHFTtyOoEbTOGhDEQ3ax9vvVf3Y6VTxESA7D5VYtjg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Nov 30, 2026 at 5:53 AM Zhijie Hou <houzhijie22(at)gmail(dot)com> wrote:
>
> Hi,
>
> On Fri, Nov27, 2026 at 05:24 PM shveta malik wrote:
> > On Thu, Nov 26, 2026 at 8:09 PM Zhijie Hou <houzhijie22(at)gmail(dot)com> wrote:
> > > 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 for reviewing.
>
> I think the race you mentioned exists, but unlike logical slot case,
> this race is mostly harmless for physical slots. Because even in normal
> physical slot creation, xmin is not initialized at creation time; it is
> only set asynchronously when the first hot_standby_feedback message
> arrives (ProcessStandbyHSFeedbackMessage()), without checking whether
> VACUUM has already removed tuples for that xmin, and the worst
> consequence is normal recovery conflict handling on the standby. Since
> the behavior of this race is no worse than pre-existing slot creation
> behavior, I think we can consider it independently if needed.

I think it's a different issue from what the patch fixes. The patch is
about not detecting a drop that happened before the revalidation,
whereas this one is about having no interlock after it. The window is
also quite narrow; there is no I/O or lock wait between the
revalidation and installing the copied values, so I think it's very
unlikely to happen in practice. Also, as Hou-san pointed out, the
consequence is limited to recovery conflicts on the standby. So I'd
prefer to keep the proposed patch as it is and deal with this in a
separate patch (or even I guess it's not worth fixing it actively
unless there is a convenient way for it).

Regards,

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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message David Rowley 2026-12-01 19:52:52 Re: Should we skip foreign key checks in binary upgrade mode?
Previous Message Fabien Roy 2026-12-01 19:32:39 [PROPOSAL] Extension: searchable encrypted column type with externally supplied crypto