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 <[email protected]> Reviewed-by: shveta malik <[email protected]> Reviewed-by: Nisha Moond <[email protected]> Reviewed-by: Amit Kapila <[email protected]> Reviewed-by: Zhijie Hou <[email protected]> Discussion: https://postgr.es/m/CAM527d_eV_BAYFiQnfZLSPfHoihye=noi-onam_57pdh+f+...@mail.gmail.com Backpatch-through: 19 Branch ------ REL_19_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/3f5bfbce4c471aaffc8711090483460316728344 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(-)
