Inactive physical replication slot can bypass synchronized_standby_slots confirmation

From: wangyang25(at)otcaix(dot)iscas(dot)ac(dot)cn
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Inactive physical replication slot can bypass synchronized_standby_slots confirmation
Date: 2026-11-23 09:41:14
Message-ID: 179542687448.3514454.15489738456778044149@otcaix.iscas.ac.cn
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PostgreSQL server version: PostgreSQL 19.0, REL_19_0, commit 5c839a2a08b9b87a349be97371fac6c5a2873b10
psql version: PostgreSQL 19.0
Platform: Ubuntu 24.04 LTS, Linux x86_64, kernel 7.0.0-38-generic, glibc 2.39; GCC 13.3.0
Build options: --without-readline --without-zlib --without-icu --without-llvm

Cluster setup: Run as the postgres operating-system user. Set PGDATA to a new empty data directory and SOCKET to a local socket directory, then run:

export PGDATA=/path/to/pgdata
export SOCKET=/path/to/socket
mkdir -p "$SOCKET"
initdb -D "$PGDATA" --no-locale -E UTF8 --auth-local=trust --auth-host=reject

Start the server with:

pg_ctl -D "$PGDATA" -l "$PGDATA/server.log" -o "-c listen_addresses= -c unix_socket_directories=$SOCKET -c port=55488 -c max_connections=10 -c shared_buffers=32MB -c wal_level=logical -c max_replication_slots=10 -c max_wal_senders=10" -w start

For the SQL below, connect with PGHOST=$SOCKET PGPORT=55488 psql -X -U postgres -d postgres. The replication settings are not needed for the other four reports.

Summary

An inactive physical replication slot can satisfy synchronized_standby_slots using its initial restart_lsn, even when no standby has connected or confirmed receipt of WAL. A failover logical slot can then return and consume changes before the physical standby has confirmed them.

This conflicts with the PostgreSQL 19 documentation for synchronized_standby_slots, which says logical failover slot consumers wait until the configured physical slots have confirmed WAL receipt. The pg_logical_slot_get_changes() documentation also says it blocks until those slots confirm receipt.

Reproduction

Build and install the standard test_decoding output plugin against this PostgreSQL installation. For example, from a PostgreSQL 19 source tree:

make -C contrib/test_decoding PG_CONFIG=/path/to/pg_config install

The cluster setup above enables logical WAL and replication slots. Do not start or connect a standby. Connect to the new cluster as a superuser and run:

\set ON_ERROR_STOP on
CREATE TABLE slot_probe (i integer);
SELECT * FROM pg_create_logical_replication_slot(
'report_logic', 'test_decoding', false, false, true);
INSERT INTO slot_probe VALUES (1);
SELECT pg_current_wal_flush_lsn() AS target \gset
\echo target=:target

CHECKPOINT;
SELECT * FROM pg_create_physical_replication_slot('report_phys', true);
SELECT slot_name, active, restart_lsn,
restart_lsn >= :'target'::pg_lsn AS restart_ge_target
FROM pg_replication_slots WHERE slot_name = 'report_phys';

ALTER SYSTEM SET synchronized_standby_slots = 'report_phys';
SELECT pg_reload_conf();
SELECT * FROM pg_logical_slot_get_changes(
'report_logic', :'target'::pg_lsn, NULL);
SELECT slot_name, active, restart_lsn, confirmed_flush_lsn
FROM pg_replication_slots
WHERE slot_name IN ('report_phys', 'report_logic')
ORDER BY slot_name;

Observed result

Example output from PostgreSQL 19.0 (LSNs and transaction IDs vary):

target=0/02982250
report_phys | f | 0/02982250 | t
0/029821E0 | 734 | BEGIN 734
0/029821E0 | 734 | table public.slot_probe: INSERT: i[integer]:1
0/02982250 | 734 | COMMIT 734
report_logic | f | 0/029821A8 | 0/02982250

The physical slot was inactive and its restart_lsn was at or beyond target. With no standby connected, pg_logical_slot_get_changes() nevertheless returned BEGIN, the INSERT row, and COMMIT. The logical slot's confirmed_flush_lsn advanced through target.

Expected result

The function should wait for the listed physical standby to confirm receipt through the requested WAL position. Since no standby is connected, it should not return the change or advance the failover logical slot.

Cleanup

In the disposable cluster, run:

ALTER SYSTEM RESET synchronized_standby_slots;
SELECT pg_reload_conf();
SELECT pg_drop_replication_slot('report_logic');
SELECT pg_drop_replication_slot('report_phys');
DROP TABLE slot_probe;

The same behavior was reproduced on PostgreSQL 18.6 (REL_18_6, commit 724edf9bde9d356724ad384a2e196edc3c9f80f7), PostgreSQL 18.7 (REL_18_7, commit f5c41f6681c910b3f69951bf6fb97c6d74605d25), and PostgreSQL 19.0 (REL_19_0, commit 5c839a2a08b9b87a349be97371fac6c5a2873b10). The 18.6 and 18.7 runs used separate scratch clusters.

Relevant source location: StandbySlotsHaveCaughtup() in src/backend/replication/slot.c. The current implementation checks whether the slot is inactive only when restart_lsn is below the requested LSN; a slot already at or beyond that position is counted as caught up. Source at the tested commit: https://github.com/postgres/postgres/blob/5c839a2a08b9b87a349be97371fac6c5a2873b10/src/backend/replication/slot.c#L3214-L3228

Documentation: https://www.postgresql.org/docs/19/runtime-config-replication.html#GUC-SYNCHRONIZED-STANDBY-SLOTS and https://www.postgresql.org/docs/19/functions-admin.html

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Shlok Kyal 2026-11-23 10:00:47 Re: Logical replication commits duplicates in DEFERRABLE unique indexes
Previous Message Raja Sai pranav 2026-11-23 09:36:25 Re: Stale partition constraint on sub-partitions of a partitioned DEFAULT partition after a sibling is detached/dropped/created