Crash restart of a hot standby re-replays an old LOGICAL_DECODING_STATUS_CHANGE(false) record and invalidates logical slots created or synced after it (wal_level_insufficient), although the primary has logical decoding enabled again ======================================================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a23157-19f5-7a24-8134-bc84414145ac Filed: 2026-12-01 11:22:01 UTC Severity: 50/100 Blamed commit: 67c20979ce72b8c236622e5603f9775968ff501c ## Summary With wal_level = replica, logical decoding is toggled dynamically, and the primary writes XLOG_LOGICAL_DECODING_STATUS_CHANGE records. On a hot standby, replaying a "false" record invalidates every logical slot (RS_INVAL_WAL_LEVEL), whether or not the slot was created after that record. If the standby crashes (or is stopped with -m immediate) before a restartpoint passes the old "disable" record, crash recovery replays that record again. It then invalidates logical slots that were created, or synced, after logical decoding had already been re-enabled. Those slots' restart_lsn is past the record, and all the WAL they need carries logical information. Affected slots: - **Logical slots created directly on the standby** are permanently invalidated (wal_level_insufficient). - **Sync-ready synced failover slots** are invalidated, then dropped and re-created by the slotsync worker. If the remote slot is idle, the re-created copy stays temporary, and a promotion in that window drops it, so the failover slot is lost after failover. This was observed with an idle remote slot: after promotion the slot was gone. A simple primary-side sequence is enough: create a logical slot, drop it, then create another one later. For example, a temporary slot used by a tool or a table sync, followed by a subscription's slot. The standby then has to crash within one restartpoint interval. ## Reproduction Self-contained script. It assumes the PostgreSQL binaries are in PATH; set P1 and P2 to two free ports. ```sh #!/bin/bash set -u P1=${P1:-5432}; P2=${P2:-5433} T=$(mktemp -d) cleanup() { for d in "$T/s" "$T/p"; do [ -f "$d/postmaster.pid" ] && pg_ctl -D "$d" -m immediate stop >/dev/null 2>&1; done rm -rf "$T"; return 0 } trap cleanup EXIT q() { local p=$1; shift echo "$*" | psql -X -At -q -h "$T" -p $p -d postgres; } initdb -D $T/p -A trust -N >/dev/null cat >> $T/p/postgresql.conf <> $T/p/pg_hba.conf pg_ctl -D $T/p -l $T/p.log -w start >/dev/null q $P1 "select pg_create_physical_replication_slot('sb1'); create table t(id int)" >/dev/null pg_basebackup -h "$T" -p $P1 -D $T/s -R -X stream -c fast -S sb1 cat >> $T/s/postgresql.conf <> $T/s/postgresql.auto.conf pg_ctl -D $T/s -l $T/s.log -w start >/dev/null # 1. enable, then disable logical decoding (drop the only slot) q $P1 "select pg_create_logical_replication_slot('tmp','test_decoding')" q $P1 "select pg_drop_replication_slot('tmp')" # 2. enable again with a failover slot; let it become sync-ready q $P1 "select pg_create_logical_replication_slot('ls','test_decoding', false,false,true)" for i in $(seq 1 60); do q $P1 "insert into t values ($i); checkpoint" q $P1 "select count(*) from pg_logical_slot_get_changes('ls',NULL,NULL)" r=$(q $P2 "select not temporary and synced and invalidation_reason is null from pg_replication_slots where slot_name='ls'") [ "$r" = t ] && break; sleep 1 done # plus a plain logical slot created on the standby q $P2 "select pg_create_logical_replication_slot('sl','test_decoding')" q $P2 "select slot_name, synced, temporary, restart_lsn, invalidation_reason from pg_replication_slots order by 1" # 3. crash the standby before any restartpoint and restart it pg_ctl -D $T/s -m immediate stop pg_ctl -D $T/s -l $T/s.log -w start sleep 3 q $P2 "select slot_name, synced, temporary, restart_lsn, invalidation_reason from pg_replication_slots order by 1" grep -A2 'invalidating obsolete replication slot' $T/s.log ``` Expected: the primary still has effective_wal_level = logical, so the standby's slots survive the crash restart unchanged. Actual, at cb94cb4124c1284092b53564894cbf372787989d: ``` primary effective_wal_level: logical before crash: ls|t|f|0/03001D48| sl|f|f|0/03001E08| after crash restart: ls|t|f|0/03001D48| <- dropped and re-synced sl|f|f|0/03001E08|wal_level_insufficient LOG: invalidating obsolete replication slot "sl" DETAIL: Logical decoding on standby requires the primary server to either set "wal_level" >= "logical" or have at least one logical slot when "wal_level" = "replica". CONTEXT: WAL redo at 0/03000080 for XLOG/LOGICAL_DECODING_STATUS_CHANGE: false LOG: invalidating obsolete replication slot "ls" CONTEXT: WAL redo at 0/03000080 for XLOG/LOGICAL_DECODING_STATUS_CHANGE: false ``` Both slots have restart_lsn well past 0/03000080, the record that invalidated them. In a variant where the primary's failover slot was idle after the crash, the slotsync worker re-created "ls" as a temporary slot. Promoting the standby then dropped it, and pg_logical_slot_get_changes('ls', ...) on the new primary failed with `replication slot "ls" does not exist`. ## Analysis The XLOG_LOGICAL_DECODING_STATUS_CHANGE branch of xlog_redo() in src/backend/access/transam/xlog.c: ```c if (InRecovery && InHotStandby) { if (!status) InvalidateObsoleteReplicationSlots(RS_INVAL_WAL_LEVEL, 0, InvalidOid, InvalidTransactionId); ``` DetermineSlotInvalidationCause() in slot.c returns RS_INVAL_WAL_LEVEL for any logical slot. At startup, slots are restored from pg_replslot with their current state, but crash recovery starts redo at the last restartpoint, which can be before the "disable" record. Nothing compares the record's LSN with the slot's restart_lsn (or creation point). So a slot that needs only WAL written after a later "enable" record is invalidated by an older "disable" record. Before dynamic toggling, the analogous case needed the primary to be restarted with a lower wal_level. Now it only needs the last logical slot on the primary to be dropped once. The record and its redo-time invalidation were introduced by commit 67c20979ce72b8c236622e5603f9775968ff501c ("Toggle logical decoding dynamically based on logical slot presence"). A possible fix is to skip slots whose restart_lsn is at or after the record's end LSN when invalidating for RS_INVAL_WAL_LEVEL during redo. ## Branches Tested on master only, at cb94cb4124c1284092b53564894cbf372787989d (cassert build). Blame 67c20979ce72b8c236622e5603f9775968ff501c is in REL_19 and master only. ## Notes A fast shutdown performs a restartpoint and avoids the problem. An immediate stop, or a crash within one checkpoint_timeout of the disable record, triggers it.