Hi,

While reviewing the dynamic logical decoding status control code (introduced in commit 67c20979ce7), I noticed a race condition when creating a logical replication slot on a standby while logical decoding is being dynamically enabled.

During recovery, ReplicationSlotReserveWal() sets the new logical slot's restart_lsn to GetXLogReplayRecPtr(). However, logical decoding can become enabled before GetXLogReplayRecPtr() advances past the corresponding XLOG_LOGICAL_DECODING_STATUS_CHANGE record in two cases:

1. During WAL replay, xlog_redo() calls EnableLogicalDecoding() and sets logical_decoding_enabled to true before ApplyWalRecord() updates lastReplayedEndRecPtr to the end of the status-change record.

2. At the end of recovery, UpdateLogicalDecodingStatusEndOfRecovery() sets logical_decoding_enabled to true (even before writing the status-change record) while RecoveryInProgress() is still true and lastReplayedEndRecPtr remains at EndOfLog, never advancing past that record.

In either window, a concurrent logical slot creation on the standby observes logical decoding as enabled, reserves a restart_lsn preceding the status-change record, and fails in xlog_decode() with:

    ERROR:  unexpected logical decoding status change 1

Attached is a patch to fix this by Holding LogicalDecodingControlLock in UpdateLogicalDecodingStatusEndOfRecovery() until the status-change record is written and recording its end LSN in last_replayed_enable_lsn when enabling logical decoding (alongside EnableLogicalDecoding() during replay), and clamping the reserved restart_lsn in ReplicationSlotReserveWal() to at least GetLogicalDecodingEnabledLSN() during recovery.

Note that the test in the patch covers only case 2 (reusing the existing end-of-recovery injection point) to avoid adding a new injection point in xlog_redo() just for case 1.

And I kept the field name last_replayed_enable_lsn to keep diff minimal, though we could rename it (e.g., to last_enable_lsn) if others prefer since it now also records an end-of-recovery LSN.

--
Best Regards,
Zhijie Hou
From 9adcced805a505d558245abd2ca4dddb6c5f0f90 Mon Sep 17 00:00:00 2001
From: Zhijie Hou <[email protected]>
Date: Sat, 21 Nov 2026 23:54:43 +0800
Subject: [PATCH v1] Fix standby logical slot creation while enabling logical
 decoding.

When creating a logical replication slot during recovery,
ReplicationSlotReserveWal() reserves WAL starting at
GetXLogReplayRecPtr(). However, logical decoding can become enabled
before GetXLogReplayRecPtr() advances past the corresponding
XLOG_LOGICAL_DECODING_STATUS_CHANGE record in two cases:

1. During WAL replay, xlog_redo() calls EnableLogicalDecoding() and
   sets logical_decoding_enabled to true before ApplyWalRecord()
   updates lastReplayedEndRecPtr to the end of the status-change
   record.

2. At the end of recovery, UpdateLogicalDecodingStatusEndOfRecovery()
   sets logical_decoding_enabled to true (even before writing the
   status-change record) while RecoveryInProgress() is still true and
   lastReplayedEndRecPtr remains at EndOfLog, never advancing past
   that record.

In either window, a concurrent logical slot creation on the standby
observes logical decoding as enabled, reserves a restart_lsn preceding
the status-change record, and fails in xlog_decode() with "unexpected
logical decoding status change 1".

Fix this by extending last_replayed_enable_lsn (already updated in
EnableLogicalDecoding() during replay) to also record the end LSN of
the status-change record written in
UpdateLogicalDecodingStatusEndOfRecovery() before releasing
LogicalDecodingControlLock, and by clamping the reserved restart_lsn in
ReplicationSlotReserveWal() to at least GetLogicalDecodingEnabledLSN()
during recovery.

Author: Zhijie Hou <[email protected]>
Backpatch-through: 19
---
 src/backend/replication/logical/logicalctl.c  | 57 ++++++++++++++----
 src/backend/replication/slot.c                | 13 +++-
 src/include/replication/logicalctl.h          |  1 +
 .../recovery/t/051_effective_wal_level.pl     | 59 +++++++++++++++++++
 4 files changed, 117 insertions(+), 13 deletions(-)

diff --git a/src/backend/replication/logical/logicalctl.c 
b/src/backend/replication/logical/logicalctl.c
index 182d15e5653..a00bf44e305 100644
--- a/src/backend/replication/logical/logicalctl.c
+++ b/src/backend/replication/logical/logicalctl.c
@@ -99,10 +99,12 @@ typedef struct LogicalDecodingCtlData
 
        /*
         * End LSN of the last XLOG_LOGICAL_DECODING_STATUS_CHANGE record that
-        * enabled logical decoding, or InvalidXLogRecPtr if none has been
-        * replayed since the server started. Checking it with
-        * logical_decoding_enabled tells from which point on WAL was written 
with
-        * logical decoding enabled; see StandbyLogicalDecodingEnabledSince().
+        * enabled logical decoding during recovery (either replayed or written 
at
+        * the end of recovery), or InvalidXLogRecPtr if none has occurred since
+        * the server started. Checking it with logical_decoding_enabled tells
+        * from which point on WAL was written with logical decoding enabled; 
see
+        * GetLogicalDecodingEnabledLSN() and
+        * StandbyLogicalDecodingEnabledSince().
         *
         * WAL records that disable logical decoding are deliberately not 
tracked
         * here. logical_decoding_enabled is false while decoding is off, which 
is
@@ -144,7 +146,7 @@ static bool XLogLogicalInfoUpdatePending = false;
 
 static void update_xlog_logical_info(void);
 static void abort_logical_decoding_activation(int code, Datum arg);
-static void write_logical_decoding_status_update_record(bool status);
+static XLogRecPtr write_logical_decoding_status_update_record(bool status);
 
 static void
 LogicalDecodingCtlShmemRequest(void *arg)
@@ -238,6 +240,28 @@ IsLogicalDecodingEnabled(void)
        return enabled;
 }
 
+/*
+ * Return the end LSN of the last XLOG_LOGICAL_DECODING_STATUS_CHANGE record
+ * that enabled logical decoding during recovery, or InvalidXLogRecPtr if
+ * none has occurred since the server started.
+ *
+ * last_replayed_enable_lsn is maintained only during recovery, so this is
+ * intended for callers that have checked RecoveryInProgress(). Unlike
+ * StandbyLogicalDecodingEnabledSince(), we do not assert RecoveryInProgress()
+ * here because recovery may end concurrently right after the caller's check.
+ */
+XLogRecPtr
+GetLogicalDecodingEnabledLSN(void)
+{
+       XLogRecPtr      lsn;
+
+       LWLockAcquire(LogicalDecodingControlLock, LW_SHARED);
+       lsn = LogicalDecodingCtl->last_replayed_enable_lsn;
+       LWLockRelease(LogicalDecodingControlLock);
+
+       return lsn;
+}
+
 /*
  * Return true if logical decoding has been enabled continuously from the given
  * LSN up to the current replay position, that is, if the WAL in that range
@@ -299,9 +323,9 @@ AtEOXact_LogicalCtl(void)
 
 /*
  * Writes an XLOG_LOGICAL_DECODING_STATUS_CHANGE WAL record with the given
- * status.
+ * status, and returns the end LSN of the inserted record.
  */
-static void
+static XLogRecPtr
 write_logical_decoding_status_update_record(bool status)
 {
        XLogRecPtr      recptr;
@@ -310,6 +334,8 @@ write_logical_decoding_status_update_record(bool status)
        XLogRegisterData(&status, sizeof(bool));
        recptr = XLogInsert(RM_XLOG_ID, XLOG_LOGICAL_DECODING_STATUS_CHANGE);
        XLogFlush(recptr);
+
+       return recptr;
 }
 
 /*
@@ -717,6 +743,8 @@ UpdateLogicalDecodingStatusEndOfRecovery(void)
         */
        if (new_status != LogicalDecodingCtl->logical_decoding_enabled)
        {
+               XLogRecPtr      recptr;
+
                /*
                 * Update both the logical decoding status and logical WAL 
logging
                 * status. Unlike toggling these status during non-recovery, we 
don't
@@ -738,12 +766,17 @@ UpdateLogicalDecodingStatusEndOfRecovery(void)
                 */
                LogicalDecodingCtl->pending_disable = false;
 
-               LWLockRelease(LogicalDecodingControlLock);
-
-               write_logical_decoding_status_update_record(new_status);
+               /*
+                * Write the WAL record and update last_replayed_enable_lsn 
before
+                * releasing LogicalDecodingControlLock so that concurrent slot
+                * creation before recovery completes sees the new LSN.
+                */
+               recptr = 
write_logical_decoding_status_update_record(new_status);
+               if (new_status)
+                       LogicalDecodingCtl->last_replayed_enable_lsn = recptr;
        }
-       else
-               LWLockRelease(LogicalDecodingControlLock);
+
+       LWLockRelease(LogicalDecodingControlLock);
 
        /*
         * With wal_level = logical, XLogLogicalInfoActive() is always true, so 
no
diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c
index a311aee85a9..5a4bf6638e0 100644
--- a/src/backend/replication/slot.c
+++ b/src/backend/replication/slot.c
@@ -1750,6 +1750,12 @@ ReplicationSlotReserveWal(void)
         * independently on the primary, so that a snapshot can be built using 
the
         * record.
         *
+        * During recovery, logical decoding can become enabled before
+        * GetXLogReplayRecPtr() advances past the status-change record (in
+        * xlog_redo() or UpdateLogicalDecodingStatusEndOfRecovery()), so 
advance
+        * restart_lsn to at least GetLogicalDecodingEnabledLSN() to avoid
+        * decoding that record.
+        *
         * None of this is needed (or indeed helpful) for physical slots as
         * they'll start replay at the last logged checkpoint anyway. Instead,
         * return the location of the last redo LSN, where a base backup has to
@@ -1758,7 +1764,12 @@ ReplicationSlotReserveWal(void)
        if (SlotIsPhysical(slot))
                restart_lsn = GetRedoRecPtr();
        else if (RecoveryInProgress())
-               restart_lsn = GetXLogReplayRecPtr(NULL);
+       {
+               XLogRecPtr      replay_lsn = GetXLogReplayRecPtr(NULL);
+               XLogRecPtr      enable_lsn = GetLogicalDecodingEnabledLSN();
+
+               restart_lsn = Max(replay_lsn, enable_lsn);
+       }
        else
                restart_lsn = GetXLogInsertRecPtr();
 
diff --git a/src/include/replication/logicalctl.h 
b/src/include/replication/logicalctl.h
index 97cacb02e4e..4aa4e5cac7a 100644
--- a/src/include/replication/logicalctl.h
+++ b/src/include/replication/logicalctl.h
@@ -20,6 +20,7 @@ extern void StartupLogicalDecodingStatus(bool last_status);
 extern void InitializeProcessXLogLogicalInfo(void);
 extern bool ProcessBarrierUpdateXLogLogicalInfo(void);
 extern bool IsLogicalDecodingEnabled(void);
+extern XLogRecPtr GetLogicalDecodingEnabledLSN(void);
 extern bool StandbyLogicalDecodingEnabledSince(XLogRecPtr lsn);
 extern bool IsXLogLogicalInfoEnabled(void);
 extern void AtEOXact_LogicalCtl(void);
diff --git a/src/test/recovery/t/051_effective_wal_level.pl 
b/src/test/recovery/t/051_effective_wal_level.pl
index 5129e6eb638..573a1a19fe0 100644
--- a/src/test/recovery/t/051_effective_wal_level.pl
+++ b/src/test/recovery/t/051_effective_wal_level.pl
@@ -811,6 +811,65 @@ select pg_create_logical_replication_slot('standby5_slot', 
'test_decoding');
        test_wal_level($standby5, "replica|logical",
                "logical decoding got activated on the promoted standby5 after 
retry"
        );
+       $standby5->stop;
+
+       # Test that logical slot creation during end-of-recovery activation does
+       # not reserve a restart_lsn preceding the status-change record.
+
+       # Disable logical decoding on the primary so that standby6 starts with
+       # logical decoding disabled, then activates it upon promotion due to its
+       # local wal_level = 'logical'.
+       $primary->safe_psql('postgres',
+               qq[select pg_drop_replication_slot('test_slot3')]);
+       wait_for_logical_decoding_disabled($primary);
+
+       my $standby6 = PostgreSQL::Test::Cluster->new('standby6');
+       $standby6->init_from_backup($primary, 'my_backup', has_streaming => 1);
+       $standby6->append_conf('postgresql.conf', qq[wal_level = 'logical']);
+       $standby6->start;
+       $primary->wait_for_replay_catchup($standby6);
+
+       # Promote standby6 without waiting and pause the startup process right
+       # after it enables logical decoding and writes the status-change record,
+       # while RecoveryInProgress() is still true.
+       $standby6->safe_psql(
+               'postgres', qq[
+select 
injection_points_attach('startup-logical-decoding-status-change-end-of-recovery',
 'wait');
+select pg_promote(false);
+]);
+       $standby6->wait_for_event('startup',
+               'startup-logical-decoding-status-change-end-of-recovery');
+
+       # Start creating a logical slot and wait until 
ReplicationSlotReserveWal()
+       # sets restart_lsn while the server is still in recovery.
+       $psql_create_slot =
+         $standby6->background_psql('postgres', on_error_stop => 0);
+       $psql_create_slot->query_until(
+               qr/create_standby6_slot/,
+               q(\echo create_standby6_slot
+select pg_create_logical_replication_slot('standby6_slot', 'test_decoding');
+));
+       $standby6->poll_query_until(
+               'postgres',
+               qq[select restart_lsn is not null from pg_replication_slots 
where slot_name = 'standby6_slot']
+       ) or die "timed out waiting for standby6_slot to reserve WAL";
+
+       # Complete promotion and log a standby snapshot so that the slot 
creation
+       # can finish finding its decoding start point.
+       $standby6->safe_psql(
+               'postgres', qq[
+select 
injection_points_detach('startup-logical-decoding-status-change-end-of-recovery');
+select 
injection_points_wakeup('startup-logical-decoding-status-change-end-of-recovery');
+]);
+       $standby6->poll_query_until('postgres',
+               qq[select not pg_is_in_recovery()])
+         or die "timed out waiting for standby6 promotion";
+       $standby6->safe_psql('postgres', qq[select pg_log_standby_snapshot()]);
+       $psql_create_slot->quit;
+
+       is($psql_create_slot->{stderr}, '',
+               "logical slot creation succeeds during end-of-recovery 
activation");
+       $standby6->stop;
 }
 
 $primary->stop;
-- 
2.54.0 (Apple Git-157)

Reply via email to