Inactive physical replication slot can bypass synchronized_standby_slots confirmation

Started by Noname6 days ago3 messagesbugs

Thread notes

1 note#bug#not-a-bug
hackorum-bot#bug#not-a-bug
hackorum-bot
2 days ago

@global #bug #not-a-bug

#bug#not-a-bug
Jump to latest
#1Noname
wangyang25@otcaix.iscas.ac.cn

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

#2Zhijie Hou
houzhijie22@gmail.com
In reply to: Noname (#1)
Re: Inactive physical replication slot can bypass synchronized_standby_slots confirmation

Hi,

On Mon, Nov23, 2026 at 05:41 PM wangyang25@otcaix.iscas.ac.cn wrote:

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 is as expected. If a standby has already confirmed WAL flush up to
restart_lsn and then disconnects, it already has those changes on disk,
so logical decoding should not wait for WAL at or before restart_lsn.
Requiring the slot to be active here would also break clean primary
shutdown if the physical walsender exits before the logical walsender
checks the slot.

In your test, pg_create_physical_replication_slot('report_phys', true)
sets restart_lsn to the latest checkpoint's redo LSN to reserve WAL for
a subsequent base backup. Any standby built from a base backup taken
afterward will already contain the earlier INSERT, while any WAL
generated after restart_lsn will still wait for standby confirmation. So
I don't think this is a bug.

--
Best Regards,
Zhijie Hou

#3shveta malik
shveta.malik@gmail.com
In reply to: Zhijie Hou (#2)
Re: Inactive physical replication slot can bypass synchronized_standby_slots confirmation

On Wed, Nov 25, 2026 at 7:58 AM Zhijie Hou <houzhijie22@gmail.com> wrote:

Hi,

On Mon, Nov23, 2026 at 05:41 PM wangyang25@otcaix.iscas.ac.cn wrote:

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 is as expected. If a standby has already confirmed WAL flush up to
restart_lsn and then disconnects, it already has those changes on disk,
so logical decoding should not wait for WAL at or before restart_lsn.
Requiring the slot to be active here would also break clean primary
shutdown if the physical walsender exits before the logical walsender
checks the slot.

+1, I agree this is expected behaviour.

The CHECKPOINT in the reproducer is what makes it work. It moves the
redo pointer up to target, so reserving WAL sets restart_lsn exactly
at target. I verified this on HEAD. Without the CHECKPOINT,
restart_lsn stays behind target, and without reserving WAL it is NULL.
In both cases pg_logical_slot_get_changes() waits and repeatedly logs
that the slot does not have an active_pid, and the logical slot's
confirmed_flush_lsn doesn't advance.

Also, a standby that has never connected has no synced failover slots,
so even in this case there is nothing on it for logical replication to
fail over to. So I don't see a practical data-loss (or inconsistency)
scenario either.

Show quoted text

In your test, pg_create_physical_replication_slot('report_phys', true)
sets restart_lsn to the latest checkpoint's redo LSN to reserve WAL for
a subsequent base backup. Any standby built from a base backup taken
afterward will already contain the earlier INSERT, while any WAL
generated after restart_lsn will still wait for standby confirmation. So
I don't think this is a bug.

--
Best Regards,
Zhijie Hou