Re: ReplicationSlotRelease() crashes when the instance is in the single user mode

David G. Johnston <david.g.johnston@gmail.com>

From: "David G. Johnston" <david.g.johnston@gmail.com>
To: "Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>
Cc: "pgsql-hackers@lists.postgresql.org" <pgsql-hackers@lists.postgresql.org>
Date: 2025-02-18T06:49:45Z
Lists: pgsql-hackers
On Monday, February 17, 2025, Hayato Kuroda (Fujitsu) <
kuroda.hayato@fujitsu.com> wrote:

>
> backend> SELECT pg_create_physical_replication_slot(slot_name :=
> 'physical_slot', immediately_reserve := true);


Since this function releases the slot when it returns, re-acquisition, even
by the same backend, must always re-associate MyProcPid to the named slot.


>
> [1]:
> ```
> --- a/src/backend/replication/slot.c
> +++ b/src/backend/replication/slot.c
> @@ -599,7 +599,7 @@ retry:
>                 SpinLockRelease(&s->mutex);
>         }
>         else
> -               active_pid = MyProcPid;
> +               s->active_pid = active_pid = MyProcPid;
>         LWLockRelease(ReplicationSlotControlLock);
>
>         /*
> ```
>

This, but you cannot modify the slot without holding the spinlock.

I’d probably add an assert that the existing state of s->active_pid is
either 0 or MyProcPid already.  In single-user mode it mustn’t, really
cannot, be anything else.  But the failure here is because the SQL function
does a slot release; there are probably other reasonable paths where the
assignment of MyProcPid during slot creation is retained and encountered
during a subsequent slot acquire.

David J.

Commits

  1. Fix assertion failure with replication slot release in single-user mode