pg_copy_logical_replication_slot() on a standby accepts a synced slot that has not reached a consistent snapshot; the copy is a normal persistent slot whose decoding silently skips committed transactions =========================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a202fe-d6ad-7a0c-9cfe-e8f0a54054ae Filed: 2026-11-22 11:23:05 UTC Severity: 55/100 Blamed commit: 2ec005b4e29740f0d36e6646d149af192328b2ff ## Summary On a standby, a synced failover slot stays temporary while decoding from its restart_lsn cannot reach a consistent snapshot before the remote confirmed_flush_lsn (slotsync_skip_reason = no_consistent_snapshot). Its confirmed_flush_lsn is nevertheless set to the remote value. Such slots are documented as unusable for logical decoding, and pg_logical_slot_get_changes() refuses synced slots during recovery. pg_copy_logical_replication_slot() does not check for this. Its only "is the source usable" test is that confirmed_flush_lsn is valid ("cannot copy unfinished logical replication slot"). The copy is an ordinary, non-synced, persistent slot with the source's restart_lsn and confirmed_flush_lsn. Decoding from it, on the standby or after promotion, starts at restart_lsn, reaches a consistent snapshot only after confirmed_flush_lsn, and silently skips every transaction that committed in between. The source slot on the primary returns those transactions. ## Reproduction Primary: wal_level = logical, a physical slot for the standby, synchronized_standby_slots set to it, hot_standby_feedback = on on the standby (streaming, hot standby, primary_conninfo with dbname). 1. On the standby, create a local (non-synced) logical slot so that newly synced slots can reserve an old enough catalog_xmin: -- standby (primary runs pg_log_standby_snapshot() meanwhile) SELECT pg_create_logical_replication_slot('anchor', 'test_decoding'); 2. On the primary, create the failover slot and a table: CREATE TABLE tt(a int); SELECT pg_create_logical_replication_slot('snap', 'test_decoding', false, false, true); 3. Primary, session X: hold a transaction with an XID open (BEGIN; SELECT txid_current(); then idle ~6 s; COMMIT). 4. Primary, while X is open, move the slot's restart_lsn to an xl_running_xacts record that lists X, a few times: SELECT txid_current(); SELECT pg_log_standby_snapshot(); SELECT pg_logical_emit_message(false, 'f', 'f', true); SELECT pg_replication_slot_advance('snap', pg_current_wal_lsn()); 5. Standby: start SELECT pg_sync_replication_slots() in another session (it retries until the slot is sync-ready). 6. Primary, while X is still open: INSERT INTO tt VALUES (1); INSERT INTO tt VALUES (2); 7. After X commits, the standby shows slot_name | snap synced | t temporary | t confirmed_flush_lsn | 0/030CF5B8 slotsync_skip_reason| no_consistent_snapshot Then, on the standby: SELECT * FROM pg_copy_logical_replication_slot('snap', 'cp', false); -- succeeds: cp | 0/030CF5B8 8. Cancel pg_sync_replication_slots(). Primary: INSERT 3, then a few pg_log_standby_snapshot() calls, then INSERT 4. 9. Compare (after the standby has replayed everything): -- standby SELECT data FROM pg_logical_slot_peek_changes('cp', NULL, NULL) WHERE data LIKE '%tt%'; -- primary SELECT data FROM pg_logical_slot_peek_changes('snap', NULL, NULL) WHERE data LIKE '%tt%'; Expected: both return INSERT 1, 2, 3 and 4 (or the copy is refused while the source is not sync-ready). Actual: the primary returns 1, 2, 3, 4; the standby's copy returns only 4. Inserts 1-3 committed after cp's confirmed_flush_lsn but before its consistent point and are silently skipped. cp is persistent (temporary = f, synced = f), so it is kept across promotion (promotion itself not tested). 4/4 runs. Control: running the same steps but letting the synced slot become sync-ready (temporary = f) before copying yields a copy that decodes exactly what the primary slot decodes (2/2). ## Analysis copy_replication_slot() (slotfuncs.c) uses a valid confirmed_flush as a proxy for "the source slot has a consistent snapshot" (added in bcd1c3630095e48bc3b1eb0fc8e8c8a7c851eba1). That holds for slots created locally, where confirmed_flush is set only once DecodingContextFindStartpoint() reached consistency. 2ec005b4e29740f0d36e6646d149af192328b2ff introduced synced slots that are kept RS_TEMPORARY until decoding from restart_lsn reaches a consistent snapshot, while LogicalSlotAdvanceAndCheckSnapState() still moves the local confirmed_flush to the remote value (see update_local_synced_slot(); ba11c48390004ed49b889cbd718c9e5f691fe493 kept this and only fixed persistence). Safety relies on such slots never being decoded: CreateDecodingContext() rejects synced slots in recovery, and the documentation says temporary synced slots cannot be used for decoding and must be dropped after promotion. The copy path neither checks data.synced nor the slot's sync-readiness, so it turns this not-yet-consistent state into a usable, persistent slot. The copied slot is created with find_startpoint = false, so no consistency is established for it either. Slot synchronization itself (ddd5f4f54a026db6a6692876d0d44aef902ab686) made such source slots possible on standbys; 2ec005b4e29740f0d36e6646d149af192328b2ff is where a valid confirmed_flush stopped implying a consistent point for synced slots. ## Notes Tested on master 9b5d4e3331753b3399e84965093109cac8f364bf. The blame commit first appeared in PostgreSQL 17 (untested there). Possible directions (not tested): refuse to copy a synced slot that is still temporary / not sync-ready, or refuse copying synced slots during recovery altogether. Related but different: 01a201d9-8420-7711-af15-7852846cfd1a (copy from a source dropped during the copy), 01a1cbe9-0f5b-7477-b245-4218f38cc379 (copy drops two_phase). Observed along the way: while a transaction with an XID is open on the primary, the first sync of a new slot blocks in SnapBuildWaitSnapshot()/XactLockTableWait() until it ends (cf. 01a1d172-c5c0-7dca-85a7-85c318eeb989, which covers blocking inside the advance waiting for replay). Branches: listed as REL_17_STABLE, REL_18_STABLE and master, the supported branches that contain the blame commit. Only master was tested.