BUG #19488: Standby connection fails after dropping on login event trigger enabled always

Started by PG Bug reporting form6 months ago14 messagesbugs
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19488
Logged by: Egor Chindyaskin
Email address: kyzevan23@mail.ru
PostgreSQL version: 18.4
Operating system: Ubuntu 26.04
Description:

Hello!
In a master + physical standby setup, connection to the standby fails after
creating a login event trigger on the master, enabling it as always, and
then dropping it without reconnecting to the master.
Also reproduces on master branch.
Steps to reproduce:

1. Run the following SQL script on the master:
CREATE OR REPLACE FUNCTION init_session()
RETURNS event_trigger SECURITY DEFINER
LANGUAGE plpgsql AS
$$
BEGIN
RAISE NOTICE 'init_session';
END;
$$;

CREATE EVENT TRIGGER init_session
ON login
EXECUTE FUNCTION init_session();

ALTER EVENT TRIGGER init_session ENABLE ALWAYS;

DROP EVENT TRIGGER init_session;

2. Try to connect to the standby:
psql -p5433

Result:
psql: error: connection to server on socket "/tmp/.s.PGSQL.5433" failed:
FATAL: cannot acquire lock mode AccessExclusiveLock on database objects
while recovery is in progress
HINT: Only RowExclusiveLock or less can be acquired on database objects
during recovery.

--
With best regards,
Egor Chindyaskin

Postgres Professional: https://postgrespro.com

#2Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

Hi,

On Wed, 20 May 2026 at 02:34, PG Bug reporting form <noreply@postgresql.org>
wrote:

The following bug has been logged on the website:

Bug reference: 19488
Logged by: Egor Chindyaskin
Email address: kyzevan23@mail.ru
PostgreSQL version: 18.4
Operating system: Ubuntu 26.04
Description:

Hello!
In a master + physical standby setup, connection to the standby fails after
creating a login event trigger on the master, enabling it as always, and
then dropping it without reconnecting to the master.
Also reproduces on master branch.
Steps to reproduce:

1. Run the following SQL script on the master:
CREATE OR REPLACE FUNCTION init_session()
RETURNS event_trigger SECURITY DEFINER
LANGUAGE plpgsql AS
$$
BEGIN
RAISE NOTICE 'init_session';
END;
$$;

CREATE EVENT TRIGGER init_session
ON login
EXECUTE FUNCTION init_session();

ALTER EVENT TRIGGER init_session ENABLE ALWAYS;

DROP EVENT TRIGGER init_session;

2. Try to connect to the standby:
psql -p5433

Result:
psql: error: connection to server on socket "/tmp/.s.PGSQL.5433" failed:
FATAL: cannot acquire lock mode AccessExclusiveLock on database objects
while recovery is in progress
HINT: Only RowExclusiveLock or less can be acquired on database objects
during recovery.

--
With best regards,
Egor Chindyaskin

Postgres Professional: https://postgrespro.com

Thanks for the report and the precise repro.

The cause is in EventTriggerOnLogin(). When a session connects to a
database whose pg_database.dathasloginevt flag is set but no login
event triggers are actually present, the function tries to clear the
flag via:

ConditionalLockSharedObject(DatabaseRelationId, MyDatabaseId, 0,
AccessExclusiveLock);

On a hot standby, LockAcquireExtended() refuses any lock stronger than
RowExclusiveLock on LOCKTAG_OBJECT/LOCKTAG_RELATION while
RecoveryInProgress() is true, which surfaces as a FATAL on the new
connection. The standby ends up in this state precisely after your
steps because the primary set dathasloginevt = true while the trigger
was active, then dropped the trigger but (intentionally) left the
flag set on disk for the next normal connection to clean up. That
state replicates to the standby, and any subsequent connection on the
standby then enters the cleanup path and crashes.

A standby should not try to clear that flag itself(?) the only correct
update path on a standby is WAL replay from the primary.

Attached patch adds a RecoveryInProgress() check to skip the cleanup
branch on standbys. I think this needs to be backpatched too.

Regards,
Ayush

Attachments:

v1-0001-Skip-pg_database.dathasloginevt-cleanup-on-standb.patchapplication/octet-stream; name=v1-0001-Skip-pg_database.dathasloginevt-cleanup-on-standb.patchDownload+10-2
#3Fujii Masao
masao.fujii@gmail.com
In reply to: Ayush Tiwari (#2)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

On Wed, May 20, 2026 at 1:37 PM 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 triggers
were introduced.

Regards,

--
Fujii Masao

#4Alexander Korotkov
aekorotkov@gmail.com
In reply to: Fujii Masao (#3)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

On Wed, May 20, 2026 at 1:03 PM Fujii Masao <masao.fujii@gmail.com> wrote:

On Wed, May 20, 2026 at 1:37 PM 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 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.

------
Regards,
Alexander Korotkov
Supabase

Attachments:

v2-0001-Skip-pg_database.dathasloginevt-cleanup-on-standb.patchapplication/octet-stream; name=v2-0001-Skip-pg_database.dathasloginevt-cleanup-on-standb.patchDownload+88-2
#5Fujii Masao
masao.fujii@gmail.com
In reply to: Alexander Korotkov (#4)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

On Wed, May 20, 2026 at 8:31 PM Alexander Korotkov <aekorotkov@gmail.com> wrote:

On Wed, May 20, 2026 at 1:03 PM Fujii Masao <masao.fujii@gmail.com> wrote:

On Wed, May 20, 2026 at 1:37 PM 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 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 = '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.

Regards,

--
Fujii Masao

#6Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: Fujii Masao (#5)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

Hi,

On Wed, 20 May 2026 at 17:35, Fujii Masao <masao.fujii@gmail.com> wrote:

On Wed, May 20, 2026 at 8:31 PM Alexander Korotkov <aekorotkov@gmail.com>
wrote:

On Wed, May 20, 2026 at 1:03 PM Fujii Masao <masao.fujii@gmail.com>

wrote:

On Wed, May 20, 2026 at 1:37 PM 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

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 = '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 = $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

#7Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: Ayush Tiwari (#6)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

Hi,

On Wed, 20 May 2026 at 18:15, Ayush Tiwari <ayushtiwari.slg01@gmail.com>
wrote:

Hi,

On Wed, 20 May 2026 at 17:35, Fujii Masao <masao.fujii@gmail.com> wrote:

On Wed, May 20, 2026 at 8:31 PM Alexander Korotkov <aekorotkov@gmail.com>
wrote:

On Wed, May 20, 2026 at 1:03 PM Fujii Masao <masao.fujii@gmail.com>

wrote:

On Wed, May 20, 2026 at 1:37 PM 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

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 = '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 had registered this in commitfest, could see CI bot failing
https://commitfest.postgresql.org/patch/6790/

my $drop_lsn = $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);

Attaching v3 with this change on top of Alexander's changes.

Regards,
Ayush

Attachments:

v3-0001-Skip-pg_database.dathasloginevt-cleanup-on-standby.patchapplication/octet-stream; name=v3-0001-Skip-pg_database.dathasloginevt-cleanup-on-standby.patchDownload+89-2
#8Alexander Korotkov
aekorotkov@gmail.com
In reply to: Ayush Tiwari (#7)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

On Thu, May 21, 2026 at 11:10 AM Ayush Tiwari
<ayushtiwari.slg01@gmail.com> wrote:

On Wed, 20 May 2026 at 18:15, Ayush Tiwari <ayushtiwari.slg01@gmail.com> wrote:

Hi,

On Wed, 20 May 2026 at 17:35, Fujii Masao <masao.fujii@gmail.com> wrote:

On Wed, May 20, 2026 at 8:31 PM Alexander Korotkov <aekorotkov@gmail.com> wrote:

On Wed, May 20, 2026 at 1:03 PM Fujii Masao <masao.fujii@gmail.com> wrote:

On Wed, May 20, 2026 at 1:37 PM 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 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 = '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 had registered this in commitfest, could see CI bot failing
https://commitfest.postgresql.org/patch/6790/

my $drop_lsn = $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);

Attaching v3 with this change on top of Alexander's changes.

I suggest another approach. Create a separate test database and apply
event trigger on it. wait_for_catchup() and others use 'postgres'
database and wouldn't touch our test database.
I also added check for successful clearance of the flag on both
primary and standby. One issue spotted there: in-place heap update
doesn't issue a WAL flush. But I think that's minor, WAL could be
flushed by any subsequent operation.

------
Regards,
Alexander Korotkov
Supabase

Attachments:

v4-0001-Skip-pg_database.dathasloginevt-cleanup-on-standb.patchapplication/octet-stream; name=v4-0001-Skip-pg_database.dathasloginevt-cleanup-on-standb.patchDownload+135-2
#9Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: Alexander Korotkov (#8)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

Hi,

On Thu, 21 May 2026 at 17:01, Alexander Korotkov <aekorotkov@gmail.com>
wrote:

I suggest another approach. Create a separate test database and apply
event trigger on it. wait_for_catchup() and others use 'postgres'
database and wouldn't touch our test database.
I also added check for successful clearance of the flag on both
primary and standby. One issue spotted there: in-place heap update
doesn't issue a WAL flush. But I think that's minor, WAL could be
flushed by any subsequent operation.

I agree the approach you are suggesting is better.

Patch looks good to me!

Regards,
Ayush

#10Alexander Korotkov
aekorotkov@gmail.com
In reply to: Ayush Tiwari (#9)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

On Thu, May 21, 2026 at 2:48 PM Ayush Tiwari
<ayushtiwari.slg01@gmail.com> wrote:

On Thu, 21 May 2026 at 17:01, Alexander Korotkov <aekorotkov@gmail.com> wrote:

I suggest another approach. Create a separate test database and apply
event trigger on it. wait_for_catchup() and others use 'postgres'
database and wouldn't touch our test database.
I also added check for successful clearance of the flag on both
primary and standby. One issue spotted there: in-place heap update
doesn't issue a WAL flush. But I think that's minor, WAL could be
flushed by any subsequent operation.

I agree the approach you are suggesting is better.

Patch looks good to me!

Thank you. I'm going to push and backpatch it if no objections.

------
Regards,
Alexander Korotkov
Supabase

#11Alexander Lakhin
exclusion@gmail.com
In reply to: Alexander Korotkov (#10)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

Hello Alexander,

25.05.2026 11:53, Alexander Korotkov wrote:

On Thu, May 21, 2026 at 2:48 PM Ayush Tiwari
<ayushtiwari.slg01@gmail.com> wrote:

I agree the approach you are suggesting is better.

Patch looks good to me!

Thank you. I'm going to push and backpatch it if no objections.

Please look at a failure of 053_standby_login_event_trigger.pl produced
recently by skink [1]https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=skink&dt=2026-10-05%2002%3A20%3A34:
[05:59:54.150](0.000s) ok 4 - no AccessExclusiveLock FATAL on standby login
[05:59:59.905](5.755s) not ok 5 - primary clears dathasloginevt on next login after DROP
[05:59:59.906](0.000s) #   Failed test 'primary clears dathasloginevt on next login after DROP'
#   at /home/bf/bf-build/skink/REL_19_STABLE/pgsql/src/test/recovery/t/053_standby_login_event_trigger.pl line 109.
[05:59:59.906](0.000s) #          got: 't'
#     expected: 'f'
Waiting for replication conn standby's replay_lsn to pass 0/04000060 on primary
done
[06:00:11.434](11.528s) not ok 6 - cleared dathasloginevt replicates to standby
[06:00:11.434](0.000s) #   Failed test 'cleared dathasloginevt replicates to standby'
#   at /home/bf/bf-build/skink/REL_19_STABLE/pgsql/src/test/recovery/t/053_standby_login_event_trigger.pl line 118.
[06:00:11.434](0.000s) #          got: 't'
#     expected: 'f'
...
[06:00:12.023](0.588s) # Looks like you failed 2 tests of 6.

I've reproduced this locally when running 5 concurrent test instances
against Valgrind-instrumented build, with the following TEMP_CONFIG:
log_autovacuum_min_duration = 0
autovacuum_naptime = 1
log_min_messages = DEBUG3
log_line_prefix = '%m [%p][%b][%v:%x] '
log_connections = on
log_disconnections = on

and diagnostic logging:
--- a/src/backend/commands/event_trigger.c
+++ b/src/backend/commands/event_trigger.c
@@ -945,9 +945,12 @@ EventTriggerOnLogin(void)
          * pg_database flag ourselves; it will be cleared via WAL replay once the
          * primary's next login event trigger run clears it on the primary.
          */
-       else if (!RecoveryInProgress() &&
- ConditionalLockSharedObject(DatabaseRelationId, MyDatabaseId,
+       else if (!RecoveryInProgress())
+{
+if (!ConditionalLockSharedObject(DatabaseRelationId, MyDatabaseId,
0, AccessExclusiveLock))
+elog(LOG,  "!!!EventTriggerOnLogin| could not lock database %u", MyDatabaseId);
+else
         {
                 /*
                  * The lock is held.  Now we need to recheck that login event triggers
@@ -1002,6 +1005,7 @@ EventTriggerOnLogin(void)
                         list_free(runlist);
                 }
         }
+}
         CommitTransactionCommand();
  }

I'm getting:
[23:03:51.341](0.000s) ok 4 - no AccessExclusiveLock FATAL on standby login
[23:03:52.951](1.610s) not ok 5 - primary clears dathasloginevt on next login after DROP
[23:03:52.951](0.000s)
[23:03:52.951](0.000s) #   Failed test 'primary clears dathasloginevt on next login after DROP'
#   at t/053_standby_login_event_trigger.pl line 109.
[23:03:52.951](0.000s) #          got: 't'
#     expected: 'f'
Waiting for replication conn standby's replay_lsn to pass 0/04000000 on primary
done
[23:03:56.084](3.134s) not ok 6 - cleared dathasloginevt replicates to standby
[23:03:56.085](0.000s)
[23:03:56.085](0.000s) #   Failed test 'cleared dathasloginevt replicates to standby'
#   at t/053_standby_login_event_trigger.pl line 118.
[23:03:56.085](0.000s) #          got: 't'
#     expected: 'f'
[23:03:56.085](0.000s) 1..6

053_standby_login_event_trigger_primary.log contains:
2026-10-08 23:03:51.618 EDT [3254910][postmaster][:0] DEBUG: assigned pm child slot 1 for client backend
2026-10-08 23:03:51.619 EDT [3254910][postmaster][:0] DEBUG:  forked new client backend, pid=3255516 socket=8
2026-10-08 23:03:51.626 EDT [3255516][client backend][:0] LOG: connection received: host=[local]
2026-10-08 23:03:51.641 EDT [3255516][client backend][5/0:0] DEBUG: InitPostgres
...
2026-10-08 23:03:51.756 EDT [3254910][postmaster][:0] DEBUG: postmaster received pmsignal signal
2026-10-08 23:03:51.756 EDT [3254910][postmaster][:0] DEBUG: assigned pm child slot 42 for autovacuum worker
2026-10-08 23:03:51.757 EDT [3255516][client backend][5/1:0] LOG: connection authenticated: user="u2" method=trust
(.../src/test/recovery_4/tmp_check/t_053_standby_login_event_trigger_primary_data/pgdata/pg_hba.conf:117)
2026-10-08 23:03:51.759 EDT [3255516][client backend][5/1:0] LOG: connection authorized: user=u2
database=regress_login_evt application_name=053_standby_login_event_trigger.pl
...
2026-10-08 23:03:51.852 EDT [3255516][client backend][5/2:0] LOG: !!!EventTriggerOnLogin| could not lock database 16386
2026-10-08 23:03:51.935 EDT [3255522][autovacuum worker][13/0:0] DEBUG:  autovacuum: processing database "regress_login_evt"

So it looks like the test might fail due to autovacuum activity.

[1]: https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=skink&dt=2026-10-05%2002%3A20%3A34

Best regards,
Alexander

#12Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: Alexander Lakhin (#11)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

Hi,

On Fri, 9 Oct 2026 at 09:30, Alexander Lakhin <exclusion@gmail.com> wrote:

Hello Alexander,

25.05.2026 11:53, Alexander Korotkov wrote:

On Thu, May 21, 2026 at 2:48 PM Ayush Tiwari
<ayushtiwari.slg01@gmail.com> wrote:

I agree the approach you are suggesting is better.

Patch looks good to me!

Thank you. I'm going to push and backpatch it if no objections.

Please look at a failure of 053_standby_login_event_trigger.pl produced
recently by skink [1]:
[05:59:54.150](0.000s) ok 4 - no AccessExclusiveLock FATAL on standby login
[05:59:59.905](5.755s) not ok 5 - primary clears dathasloginevt on next login after DROP
[05:59:59.906](0.000s) # Failed test 'primary clears dathasloginevt on next login after DROP'
# at /home/bf/bf-build/skink/REL_19_STABLE/pgsql/src/test/recovery/t/053_standby_login_event_trigger.pl line 109.
[05:59:59.906](0.000s) # got: 't'
# expected: 'f'
Waiting for replication conn standby's replay_lsn to pass 0/04000060 on primary
done
[06:00:11.434](11.528s) not ok 6 - cleared dathasloginevt replicates to standby
[06:00:11.434](0.000s) # Failed test 'cleared dathasloginevt replicates to standby'
# at /home/bf/bf-build/skink/REL_19_STABLE/pgsql/src/test/recovery/t/053_standby_login_event_trigger.pl line 118.
[06:00:11.434](0.000s) # got: 't'
# expected: 'f'
...
[06:00:12.023](0.588s) # Looks like you failed 2 tests of 6.

I've reproduced this locally when running 5 concurrent test instances
against Valgrind-instrumented build, with the following TEMP_CONFIG:
log_autovacuum_min_duration = 0
autovacuum_naptime = 1
log_min_messages = DEBUG3
log_line_prefix = '%m [%p][%b][%v:%x] '
log_connections = on
log_disconnections = on

and diagnostic logging:
--- a/src/backend/commands/event_trigger.c
+++ b/src/backend/commands/event_trigger.c
@@ -945,9 +945,12 @@ EventTriggerOnLogin(void)
* pg_database flag ourselves; it will be cleared via WAL replay once the
* primary's next login event trigger run clears it on the primary.
*/
-       else if (!RecoveryInProgress() &&
-                        ConditionalLockSharedObject(DatabaseRelationId, MyDatabaseId,
+       else if (!RecoveryInProgress())
+{
+if (!ConditionalLockSharedObject(DatabaseRelationId, MyDatabaseId,
0, AccessExclusiveLock))
+elog(LOG,  "!!!EventTriggerOnLogin| could not lock database %u", MyDatabaseId);
+else
{
/*
* The lock is held.  Now we need to recheck that login event triggers
@@ -1002,6 +1005,7 @@ EventTriggerOnLogin(void)
list_free(runlist);
}
}
+}
CommitTransactionCommand();
}

I'm getting:
[23:03:51.341](0.000s) ok 4 - no AccessExclusiveLock FATAL on standby login
[23:03:52.951](1.610s) not ok 5 - primary clears dathasloginevt on next login after DROP
[23:03:52.951](0.000s)
[23:03:52.951](0.000s) # Failed test 'primary clears dathasloginevt on next login after DROP'
# at t/053_standby_login_event_trigger.pl line 109.
[23:03:52.951](0.000s) # got: 't'
# expected: 'f'
Waiting for replication conn standby's replay_lsn to pass 0/04000000 on primary
done
[23:03:56.084](3.134s) not ok 6 - cleared dathasloginevt replicates to standby
[23:03:56.085](0.000s)
[23:03:56.085](0.000s) # Failed test 'cleared dathasloginevt replicates to standby'
# at t/053_standby_login_event_trigger.pl line 118.
[23:03:56.085](0.000s) # got: 't'
# expected: 'f'
[23:03:56.085](0.000s) 1..6

053_standby_login_event_trigger_primary.log contains:
2026-10-08 23:03:51.618 EDT [3254910][postmaster][:0] DEBUG: assigned pm child slot 1 for client backend
2026-10-08 23:03:51.619 EDT [3254910][postmaster][:0] DEBUG: forked new client backend, pid=3255516 socket=8
2026-10-08 23:03:51.626 EDT [3255516][client backend][:0] LOG: connection received: host=[local]
2026-10-08 23:03:51.641 EDT [3255516][client backend][5/0:0] DEBUG: InitPostgres
...
2026-10-08 23:03:51.756 EDT [3254910][postmaster][:0] DEBUG: postmaster received pmsignal signal
2026-10-08 23:03:51.756 EDT [3254910][postmaster][:0] DEBUG: assigned pm child slot 42 for autovacuum worker
2026-10-08 23:03:51.757 EDT [3255516][client backend][5/1:0] LOG: connection authenticated: user="u2" method=trust (.../src/test/recovery_4/tmp_check/t_053_standby_login_event_trigger_primary_data/pgdata/pg_hba.conf:117)
2026-10-08 23:03:51.759 EDT [3255516][client backend][5/1:0] LOG: connection authorized: user=u2 database=regress_login_evt application_name=053_standby_login_event_trigger.pl
...
2026-10-08 23:03:51.852 EDT [3255516][client backend][5/2:0] LOG: !!!EventTriggerOnLogin| could not lock database 16386
2026-10-08 23:03:51.935 EDT [3255522][autovacuum worker][13/0:0] DEBUG: autovacuum: processing database "regress_login_evt"

So it looks like the test might fail due to autovacuum activity.

[1] https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=skink&dt=2026-10-05%2002%3A20%3A34

Thanks for tracking this down. Since the cleanup deliberately doesn't wait
for the database lock, I think the test should allow another attempt
instead of assuming that the first login clears the flag?

The attached patch retries fresh connections to regress_login_evt until
the flag is cleared.

Regards,
Ayush

Attachments:

t139681_12
v1-0001-Retry-login-event-trigger-cleanup-in-the-recovery.patchapplication/octet-stream; name=v1-0001-Retry-login-event-trigger-cleanup-in-the-recovery.patchDownload+9-15
#13Fujii Masao
masao.fujii@gmail.com
In reply to: Ayush Tiwari (#12)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

On Fri, Oct 9, 2026 at 1:53 PM Ayush Tiwari <ayushtiwari.slg01@gmail.com> wrote:

Thanks for tracking this down. Since the cleanup deliberately doesn't wait
for the database lock, I think the test should allow another attempt
instead of assuming that the first login clears the flag?

+1

The attached patch retries fresh connections to regress_login_evt until
the flag is cleared.

Thanks for the patch! LGTM.
Barring any objections, I will commit it.

Regards,

--
Fujii Masao

#14Fujii Masao
masao.fujii@gmail.com
In reply to: Fujii Masao (#13)
Re: BUG #19488: Standby connection fails after dropping on login event trigger enabled always

On Thu, Oct 15, 2026 at 11:55 AM Fujii Masao <masao.fujii@gmail.com> wrote:

Thanks for the patch! LGTM.
Barring any objections, I will commit it.

I've pushed the patch. Thanks!

Regards,

--
Fujii Masao