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-12-01 20:00:18
Message-ID: CAD21AoDwQTKiBrc-7Fna5fpa4+jRj-cawC0UMVGHKZ8zG+fUyg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Dec 1, 2026 at 4:47 AM Zhijie Hou <houzhijie22(at)gmail(dot)com> wrote:
>
> Hi,
>
> On Tue, Dec 1, 2026 at 02:44 AM Masahiko Sawada wrote:
> >
> > On Thu, Nov 26, 2026 at 6:39 AM Zhijie Hou <houzhijie22(at)gmail(dot)com> wrote:
> > > 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.
>
> Thanks for the comments! Both comments have been addressed in v2 patch.

Thank you for updating the patch! The patch looks good to me. Could
you prepare the patch for back branches? The new isolation test uses
the injection point 'replication-slot-create-begin', which exists only
in v19 and later. For v17 and v18, I think we can have the same test
if we add the injection point there too. What do you think?

Also, regarding the following test steps, is there any reason we need
these steps?

+step s2_checkpoint
+{
+ CHECKPOINT;
+}
+step s2_switch_wal
+{
+ SELECT FROM pg_switch_wal();
+}

Regards,

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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2026-12-01 20:40:02 Re: pg_restore --format= option(without any value) should report an error as pg_dump is reporting an error
Previous Message Alexander Lakhin 2026-12-01 20:00:00 Re: Improving tracking/processing of buildfarm test failures