| From: | Zhijie Hou <houzhijie22(at)gmail(dot)com> |
|---|---|
| To: | shveta malik <shveta(dot)malik(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-30 13:52:59 |
| Message-ID: | f9b97850-2f43-4a52-a7a6-8e9740d5843f@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
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.
--
Best Regards,
Zhijie Hou
| From | Date | Subject | |
|---|---|---|---|
| Next Message | David E. Wheeler | 2026-11-30 13:54:37 | Re: ABI Compliance Checker GSoC Project |
| Previous Message | Xuneng Zhou | 2026-11-30 13:50:13 | Re: Small WAL replay perf improvements (smgropen()+reduce membarriers for LSN waiters) |