pgsql: Fix slotsync when logical decoding is disabled and re-enabled.

From: Masahiko Sawada <msawada(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix slotsync when logical decoding is disabled and re-enabled.
Date: 2026-10-07 18:56:27
Message-ID: E1xEWoV-00000000ijf-2lph@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix slotsync when logical decoding is disabled and re-enabled.

Commit 6aba42c660c made slot synchronization skip persisting a new
slot if logical decoding got disabled after the remote slot
information was fetched. As noted in its XXX comment, the check
missed the case where the last logical slot on the primary is dropped
and re-created with the same name in the meantime. If the standby had
replayed both the deactivation and the re-activation by then, the
slot was persisted with a restart_lsn preceding the deactivation.
Subsequent synchronization cycles failed with "unexpected logical
decoding status change" when advancing the slot, which also stopped
the synchronization of all other failover slots, and the slot could
not be decoded after promotion.

Fix this by remembering the end LSN of the last replayed
XLOG_LOGICAL_DECODING_STATUS_CHANGE record that enables logical
decoding, and by requiring the remote restart_lsn to be at or after it
right after creating the local slot. The check is skipped until replay
reaches the remote restart_lsn, as we can't tell anything about WAL that
hasn't been replayed yet. Once the slot exists, a later deactivation
invalidates it, so remove the check before persisting the slot. We
compare the remote restart_lsn rather than the local one, so we may drop
a slot that would have been usable, but the retry in the next
synchronization cycle fetches fresh information.

Backpatch to v19, where commit 67c20979ce7 made logical decoding toggle
dynamically.

Reported-by: Nikolay Samokhvalov <nik(at)postgres(dot)ai>
Reviewed-by: shveta malik <shveta(dot)malik(at)gmail(dot)com>
Reviewed-by: Nisha Moond <nisha(dot)moond412(at)gmail(dot)com>
Reviewed-by: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
Reviewed-by: Zhijie Hou <houzhijie22(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAM527d_eV_BAYFiQnfZLSPfHoihye=nOi-OnAM_57pdH+F+gfA@mail.gmail.com
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/1f2245371e31f0def347546f5ad250552a4eb8a4

Modified Files
--------------
src/backend/access/transam/xlog.c | 2 +-
src/backend/replication/logical/logicalctl.c | 62 ++++++++++++++++-
src/backend/replication/logical/slotsync.c | 93 ++++++++++++++++----------
src/include/replication/logicalctl.h | 5 +-
src/test/recovery/t/051_effective_wal_level.pl | 80 ++++++++++++++++++++++
5 files changed, 203 insertions(+), 39 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Masahiko Sawada 2026-10-07 18:56:31 pgsql: Fix slotsync when logical decoding is disabled and re-enabled.
Previous Message Masahiko Sawada 2026-10-07 18:09:36 pgsql: Add an init callback for the logical decoding control data.