Report
Slot sync sets a synced slot's effective_catalog_xmin to the remote catalog_xmin even when the advance path left the local catalog_xmin older, so hot_standby_feedback lets the primary prune catalog rows and a standby restart then invalidates the failover slot as rows_removed
- Severity
- 45
- Blamed commit
- 2ec005b4e29740f0d36e6646d149af192328b2ff REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
When the remote failover slot's restart_lsn has no serialized snapshot on the standby, update_local_synced_slot() advances the local slot with LogicalSlotAdvanceAndCheckSnapState(). That path can leave the local data.catalog_xmin older than the remote catalog_xmin: a single fast- forward pass only accepts the first running-xacts record after the old confirmed_flush as the xmin candidate, while the primary, consuming in several steps, has moved its catalog_xmin further. The function then nevertheless sets
slot->effective_catalog_xmin = remote_slot->catalog_xmin;
so the in-memory value (used for hot_standby_feedback and for rows_removed conflict checks) is newer than the value persisted in the slot file. The primary is allowed to prune catalog rows up to the remote catalog_xmin. After a standby restart, effective_catalog_xmin is reloaded from the older on-disk catalog_xmin, and replaying the prune record invalidates the synced failover slot with rows_removed, although the primary's slot is valid. The next sync drops it and recreates it as a not-yet-sync-ready temporary slot, so it would be lost on failover.
Reproduction
Self-contained script (needs master binaries in PATH; ports P1/P2):
#!/bin/bash
# Synced failover slot: local catalog_xmin lags the remote one after the
# advance path, but effective_catalog_xmin (hot_standby_feedback) is set to the
# remote value. After a standby restart the slot is invalidated as rows_removed.
# Needs PostgreSQL master binaries in PATH. Ports P1/P2 overridable.
set -u
P1=${P1:-55446}; P2=${P2:-55447}
W=$(mktemp -d)
cleanup() { for d in "$W/s" "$W/p"; do [ -f "$d/postmaster.pid" ] && pg_ctl -D "$d" -m immediate stop >/dev/null 2>&1; done; rm -rf "$W"; }
trap cleanup EXIT
p() { timeout 60 psql -X -qAt -h "$W" -p $P1 -d postgres -c "$1"; }
s() { timeout 60 psql -X -qAt -h "$W" -p $P2 -d postgres -c "$1"; }
initdb -A trust -D $W/p >/dev/null
cat >> $W/p/postgresql.conf <<C
port=$P1
unix_socket_directories='$W'
listen_addresses=''
wal_level=logical
autovacuum=off
synchronized_standby_slots='phys'
C
pg_ctl -D $W/p -l $W/p.log -w start >/dev/null
p "select pg_create_physical_replication_slot('phys', true)" >/dev/null
pg_basebackup -h $W -p $P1 -D $W/s >/dev/null; touch $W/s/standby.signal
cat >> $W/s/postgresql.conf <<C
port=$P2
primary_slot_name='phys'
primary_conninfo='host=$W port=$P1 dbname=postgres'
hot_standby_feedback=on
wal_receiver_status_interval=1
sync_replication_slots=off
C
pg_ctl -D $W/s -l $W/s.log -w start >/dev/null
p "select pg_create_logical_replication_slot('lsub','test_decoding',false,false,true)" >/dev/null
p "select pg_log_standby_snapshot()" >/dev/null; sleep 1
s "select pg_sync_replication_slots()" >/dev/null
# The primary consumes the slot in several small steps, each past a new
# running-xacts record, so its catalog_xmin moves forward each time.
for i in 1 2 3 4 5 6; do
p "create table t_$i(a int)"; p "drop table t_$i"
p "select pg_log_standby_snapshot()" >/dev/null
p "select count(*) from pg_logical_slot_get_changes('lsub',NULL,NULL)" >/dev/null
done
sleep 1
# One sync: the standby has no serialized snapshot at the remote restart_lsn,
# so it advances via LogicalSlotAdvanceAndCheckSnapState in one step.
s "select pg_sync_replication_slots()" >/dev/null
sleep 3
echo "primary lsub catalog_xmin: $(p "select catalog_xmin from pg_replication_slots where slot_name='lsub'")"
echo "standby lsub catalog_xmin: $(s "select catalog_xmin from pg_replication_slots where slot_name='lsub'")"
echo "primary phys catalog_xmin (standby feedback): $(p "select catalog_xmin from pg_replication_slots where slot_name='phys'")"
# Restart the standby; VACUUM a catalog on the primary meanwhile.
pg_ctl -D $W/s -m fast -w stop >/dev/null
p "vacuum pg_class"
pg_ctl -D $W/s -l $W/s.log -w start >/dev/null
sleep 3
R=$(s "select coalesce(invalidation_reason,'none') from pg_replication_slots where slot_name='lsub'")
PR=$(p "select coalesce(invalidation_reason,'none') from pg_replication_slots where slot_name='lsub'")
grep -A1 'invalidating obsolete replication slot' $W/s.log | sed 's/^[^]]*] //'
echo "standby synced slot invalidation_reason: $R; primary slot invalidation_reason: $PR"
if [ "$R" = rows_removed ] && [ "$PR" = none ]; then
echo "BUG REPRODUCED: synced failover slot invalidated (rows_removed) on standby while the primary slot is valid"
else
echo "NOT REPRODUCED"
fi
Expected: the synced slot stays valid across the standby restart (the primary slot is valid and the standby fed back a catalog_xmin it would honor after restart).
Actual (master dc168a83874f):
primary lsub catalog_xmin: 674
standby lsub catalog_xmin: 666
primary phys catalog_xmin (standby feedback): 674
LOG: invalidating obsolete replication slot "lsub"
DETAIL: The slot conflicted with xid horizon 667.
standby synced slot invalidation_reason: rows_removed; primary slot invalidation_reason: none
BUG REPRODUCED: synced failover slot invalidated (rows_removed) on standby while the primary slot is valid
Analysis
src/backend/replication/logical/slotsync.c, update_local_synced_slot(): in the "else" branch (no serialized snapshot at remote restart_lsn), LogicalSlotAdvanceAndCheckSnapState() decides data.catalog_xmin via LogicalIncreaseXminForSlot()/LogicalConfirmReceivedLocation(), which only takes one new candidate per pass. The final block then copies remote_slot->catalog_xmin into effective_catalog_xmin instead of the value just saved to disk (data.catalog_xmin), breaking the invariant documented right above it ("We have to write the changed xmin to disk before we change the in-memory value, otherwise after a crash we wouldn't know that some catalog tuples might have been removed"). ReplicationSlotsComputeRequiredXmin() then publishes the newer value, the walreceiver feeds it back, and the primary prunes accordingly. InvalidatePossiblyObsoleteSlot() checks effective_catalog_xmin, which after restart is the older data.catalog_xmin.
Commit 2ec005b4e297 introduced the advance path, which made it possible for data.catalog_xmin to differ from the remote value while effective_catalog_xmin kept being set from the remote slot (the line was later moved by 3741f2a09d52 without changing the source value). Using slot->data.catalog_xmin for effective_catalog_xmin would keep the two consistent.
Notes
Tested on master dc168a83874fbf5cee4afb76a623e1349a539724 (cassert). The example uses pg_sync_replication_slots() with the slot sync worker off to make the steps deterministic; the worker calls the same function. A clean standby restart is enough. A crash restart that re-replays an earlier prune record should hit the same check (not tested).
Branches
2ec005b4e297 and 3741f2a09d52 are in REL_17_STABLE through master. Reproduced on master only; back branches not yet tested.