Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wPgJD-000wvO-2Q for pgsql-bugs@arkaria.postgresql.org; Wed, 20 May 2026 12:45:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wPgJB-007gsN-2O for pgsql-bugs@arkaria.postgresql.org; Wed, 20 May 2026 12:45:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wPgJB-007gsE-1C for pgsql-bugs@lists.postgresql.org; Wed, 20 May 2026 12:45:58 +0000 Received: from mail-yw1-x1131.google.com ([2607:f8b0:4864:20::1131]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wPgJ9-00000000UMQ-1lRZ for pgsql-bugs@lists.postgresql.org; Wed, 20 May 2026 12:45:57 +0000 Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-7b4ee3a88e1so43589307b3.1 for ; Wed, 20 May 2026 05:45:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1779281155; cv=none; d=google.com; s=arc-20240605; b=Vhc3GN4oXwJHoU0C3BwOUCXwrlO5lGXh0TQEb7BOUdbRbOU7FzirVc36Bf/x+mysax 8MjmDU8qTMSM1dD8N0PDSshLN5Anq8UK4KbDjkOCqKBNCbX1+YnLo/3LLKC6o0ZUDon4 83kmgYNUw80MCSsC8ZrebCMW1Z92GWbiKc4jVWkaxKjk43nlm2vtgS45qNsaxEmxyDrk cr3ldy6AEk7INGIm8O6M7kPB7yjxgugwVV/qglg9zfDVPk+QgfRPPYBu0JgLuT7wn2Dd /FTCWio7KDIH2VYjS/hB4c7ZqtWypsYHOlo/paExyoiJYQB9F4PwBxAMtpAJv9wvDhOc fxqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=CuJOAU5YQc9KXUxC7z0jvNl6C+kxwN8BAL9j94Gy0ks=; fh=itDyx2q/ZBEV/y7rnjOghmMZ5ptPoon/Ku52+xerHYE=; b=RfIKFtVwXGh+MmK43YiUQrZLDrRv7/JE+s44WK322y202Ms+W75CBYkwF+756nZQbV ld1TChI1XNBPGGEe5cPavkANjO7UCnu9dRpDRx5nozzkJVyhq5Dr2K1lObnKJO6aleVy EeONqhxYZF+a8qfhd7Akk2g5JdqMRaFtFH/0URoQs4bKRMUUrDpeY+bdPOfG2Lofpt3M pG8oj7qNpWo1M+UHGNWVaTCpi/qbqr4zVOKp5zWPEyifo5385W0Ewb74dX+/lCQHQJr5 wrsqEdsasRWIyJ4PRZx10SqCjTNZAzhj5NnZPOKBXreQelGZl0Uigx2Saz1eQBfQYJUh NiwA==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779281155; x=1779885955; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=CuJOAU5YQc9KXUxC7z0jvNl6C+kxwN8BAL9j94Gy0ks=; b=qKXTE07JvtF/XpwOxaPLp8mA4kLXGFhKfyJHP1guuAxmJA41V5SRnXy3J8oYYgJPx4 UKwibSTimDkIK0GjKfxBMLjHXyfSdiLuQca3rBOe6NE+NGHYUBRJ4EPHcW3YUjXDb1VU fOXNv3A3RMWe6XaJqnbeXUGVJ3o/wDCGu9Vsjgu4FXEFj+QITCW7FLdEqcOJ3Ctsp4k7 +vNOMhNXNZuRc6hHCKm5DMrKXAll1Baw11cHKVzSzSA8eSynIWCI5JlbtFxI9GBGLpE1 beX4k35SLqqaCmANgYUuIXY5rzhDWiX47ee8RBGP16XBo1QhCCNJhnEOdlpRPdoOmXW2 SmUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779281155; x=1779885955; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=CuJOAU5YQc9KXUxC7z0jvNl6C+kxwN8BAL9j94Gy0ks=; b=q5wBT2++8C3ue71gE4nIkE39duQIscQsRc77t56SqSjA0NI7wluTPy/Gqbr647AH9D FkuhopG8Cz3RXUCueVospljAwvR+/z1T4IrE0bTNGFh3kpkqD5d6zkTVseHyLArfvWyH y5Wji8YNYdpgkwC62s8Z8lkZFTm+1tgOs2qQtfwuz+LVIVq97PHbjJygclZ5BfPPB5LN +EhNd2NVluwBlyPe3q99gCTVQWKmcvxYPI0ImsQBm3ut73DmfS6CFZu4YIC5ETQSARqo hNPvEKBfKYbUCrYmeOas8Xd8+cWvU9gdxNTBe+b1whuZrzrPmwVSKLkVNkk3yqta9A8g L2ew== X-Forwarded-Encrypted: i=1; AFNElJ9nXkHaVnbK2np627/H3r86Byt2uD/znWR9mIjETx3Xz4MFgD1WoJ1f/nZbP8qj6Y/iOgP/+aZ8GsKk@lists.postgresql.org X-Gm-Message-State: AOJu0YznjriRA99I9GgNdYvg491Hah6h35uF/BBjwOUGRnUge7tG8PLE 0FnIOM5WOHOM3F0NO7QUjAJzcrcSVj1DEuOY9Ntyme79oTwps+Y69YBi5kdIvwPQTyQ9YEGHkcV /UyTs8+5CkVpJA989CtEjh0LP46caMHs= X-Gm-Gg: Acq92OFnN/NsXfGL0/fIrvEfYbbbiQKYLRb1OZ+Q8/94fdXar955U/rZj85046Wd7Cd pY2n6maUE57/vu7UQMjCN4u8eSVyRTecvqLwVx8X5CA78O7983IcBlLUbCvRF8W2ehDpp+msEHS N7Wz86tFZ0Ja+sWQopAaiv+LIG74BYbxvru2lzfupTrp9lM5lA0SdF5PRPdF6TSvtMEgDqRiiZs vi3WS+jdHvui2NNSqosHH+3PSGis63sibZ6lsi4faC7G/116OKoj0CiUod5So9c072A2yqU2Trk 58idezY7AhJT9eX9A6OAeG63sr4b36UCOP5ADQ== X-Received: by 2002:a05:690c:6:b0:7bd:7afc:ec4b with SMTP id 00721157ae682-7c95c200ab7mr253743957b3.31.1779281155484; Wed, 20 May 2026 05:45:55 -0700 (PDT) MIME-Version: 1.0 References: <19488-d7ccfca2bf6b74b0@postgresql.org> In-Reply-To: From: Ayush Tiwari Date: Wed, 20 May 2026 18:15:44 +0530 X-Gm-Features: AVHnY4LKbw4z1kXbVXozzD7dG6v3L4xKBQk_dVNLzuZBXss-mzX8VuxLLJmDOBA Message-ID: Subject: Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always To: Fujii Masao Cc: Alexander Korotkov , kyzevan23@mail.ru, pgsql-bugs@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000003a721706523f2d34" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000003a721706523f2d34 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, On Wed, 20 May 2026 at 17:35, Fujii Masao wrote: > On Wed, May 20, 2026 at 8:31=E2=80=AFPM Alexander Korotkov > wrote: > > > > On Wed, May 20, 2026 at 1:03=E2=80=AFPM Fujii Masao > wrote: > > > On Wed, May 20, 2026 at 1:37=E2=80=AFPM Ayush Tiwari > > > wrote: > > > > Thanks for the report and the precise repro. > > > > > > +1 > > > > > > > Attached patch adds a RecoveryInProgress() check to skip the cleanu= p > > > > branch on standbys. > > > > > > Thanks for investigating this issue and for the patch! > > > The patch looks good to me. > > > > > > > I think this needs to be backpatched too. > > > > > > Yes. Seems this should be backpatched to v17, where login event > triggers > > > were introduced. > > > > I've added a tap test reproducing the bug. I'm going to push and > > backpatch this to v17 if no objections. > > +# Wait for the standby to replay the CREATE/DROP catalog state. At > +# this point the standby's pg_database.dathasloginevt is still true. > +$primary->wait_for_replay_catchup($standby); > + > +# A new connection to the standby exercises EventTriggerOnLogin()'s > +# cleanup branch. With the RecoveryInProgress() guard, that branch is > +# skipped on the standby and the connection succeeds. Without it the > +# session aborts with a FATAL about AccessExclusiveLock. Probing the > +# flag itself via safe_psql is what triggers the cleanup path. > +is( $standby->safe_psql( > + 'postgres', > + "SELECT dathasloginevt FROM pg_database WHERE datname =3D 'postgres'"), > + 't', > + 'standby accepts connection and reports dangling dathasloginevt'); > > The test looks unstable to me. wait_for_replay_catchup() may connect to > the primary to obtain the flush LSN, which could cause dathasloginevt to > become false before the subsequent safe_psql() call on the standby. > I think Fuji-san is right, can we do something like this: my $drop_lsn =3D $primary->safe_psql( 'postgres', q{ BEGIN; DROP EVENT TRIGGER init_session; DROP FUNCTION init_session(); COMMIT; SELECT pg_current_wal_lsn(); }); $primary->wait_for_catchup($standby, 'replay', $drop_lsn); Then the following standby connection should reliably exercise the case where dathasloginevt is still true on the standby but no login event trigger remains. Regards, Ayush --0000000000003a721706523f2d34 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

On Wed, 20 May 2= 026 at 17:35, Fujii Masao <masa= o.fujii@gmail.com> wrote:
On Wed, May 20, 2026 at 8:31=E2=80=AFPM Alexander Korotkov= <aekorotkov@g= mail.com> wrote:
>
> On Wed, May 20, 2026 at 1:03=E2=80=AFPM Fujii Masao <masao.fujii@gmail.com> = wrote:
> > On Wed, May 20, 2026 at 1:37=E2=80=AFPM Ayush Tiwari
> > <ayushtiwari.slg01@gmail.com> wrote:
> > > Thanks for the report and the precise repro.
> >
> > +1
> >
> > > Attached patch adds a RecoveryInProgress() check to skip the= cleanup
> > > branch on standbys.
> >
> > Thanks for investigating this issue and for the patch!
> > The patch looks good to me.
> >
> > > I think this needs to be backpatched too.
> >
> > Yes. Seems this should be backpatched to v17, where login event t= riggers
> > were introduced.
>
> I've added a tap test reproducing the bug.=C2=A0 I'm going to = push and
> backpatch this to v17 if no objections.

+# Wait for the standby to replay the CREATE/DROP catalog state.=C2=A0 At +# this point the standby's pg_database.dathasloginevt is still true. +$primary->wait_for_replay_catchup($standby);
+
+# A new connection to the standby exercises EventTriggerOnLogin()'s +# cleanup branch.=C2=A0 With the RecoveryInProgress() guard, that branch i= s
+# skipped on the standby and the connection succeeds.=C2=A0 Without it the=
+# session aborts with a FATAL about AccessExclusiveLock.=C2=A0 Probing the=
+# flag itself via safe_psql is what triggers the cleanup path.
+is( $standby->safe_psql(
+ 'postgres',
+ "SELECT dathasloginevt FROM pg_database WHERE datname =3D 'postg= res'"),
+ 't',
+ 'standby accepts connection and reports dangling dathasloginevt')= ;

The test looks unstable to me. wait_for_replay_catchup() may connect to
the primary to obtain the flush LSN, which could cause dathasloginevt to become false before the subsequent safe_psql() call on the standby.
=C2=A0
I think Fuji-san is right, can we do somethi= ng like this:

my $drop_lsn =3D $primary->safe_psql(
=C2=A0 =C2= =A0 'postgres', q{
BEGIN;
DROP EVENT TRIGGER init_session;DROP FUNCTION init_session();
COMMIT;
SELECT pg_current_wal_lsn();});

$primary->wait_for_catchup($standby, 'replay', $dro= p_lsn);

Then the following standby connection should reliably exerci= se the case
where dathasloginevt is still true on the standby but no log= in event
trigger remains.

Regards,
Ayush=C2=A0
--0000000000003a721706523f2d34--