Fix standby logical slot creation during dynamic logical decoding activation
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
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
Hi Zhijie Hou and others,
On Sat, 21 Nov 2026 at 22:39, Zhijie Hou <houzhijie22@gmail.com> wrote:
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
I'm ready to investigate it by reviewing your patch file, and I'll send my
review here and if time permits me then I'm sending my patch here too.
On Sat, Nov 21, 2026 at 9:09 AM Zhijie Hou <houzhijie22@gmail.com> wrote:
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
Thank you for the report and the patch.
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.
The patch looks good to me.
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.
I prefer to rename it to last_enable_lsn. The record written at the
end of recovery is never replayed on that server, so "replayed"
doesn't fit anymore.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
Hi,
On Tue, Nov24, 2026 at 03:24 AM Masahiko Sawada wrote:
On Sat, Nov 21, 2026 at 9:09 AM Zhijie Hou <houzhijie22@gmail.com> wrote:
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.The patch looks good to me.
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.I prefer to rename it to last_enable_lsn. The record written at the
end of recovery is never replayed on that server, so "replayed"
doesn't fit anymore.
Thanks for the comments. Here is updated patch that renamed this.
--
Best Regards,
Zhijie Hou
On Mon, Nov 23, 2026 at 7:04 PM Zhijie Hou <houzhijie22@gmail.com> wrote:
Hi,
On Tue, Nov24, 2026 at 03:24 AM Masahiko Sawada wrote:
On Sat, Nov 21, 2026 at 9:09 AM Zhijie Hou <houzhijie22@gmail.com> wrote:
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.The patch looks good to me.
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.I prefer to rename it to last_enable_lsn. The record written at the
end of recovery is never replayed on that server, so "replayed"
doesn't fit anymore.Thanks for the comments. Here is updated patch that renamed this.
Thank you for updating the patch! I've pushed it.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com