| From: | PG Bug reporting form <noreply(at)postgresql(dot)org> |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Cc: | rushsky518(at)gmail(dot)com |
| Subject: | BUG #19978: synchronized_standby_slots: stale ss_oldest_flush_lsn cache bypasses slot validation |
| Date: | 2026-12-04 09:15:19 |
| Message-ID: | 19978-7f1e271d65d67355@postgresql.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
The following bug has been logged on the website:
Bug reference: 19978
Logged by: Zejiang Zhang
Email address: rushsky518(at)gmail(dot)com
PostgreSQL version: 19.0
Operating system: Linux x86_64 (RHEL 9, kernel 5.14.0)
Description:
Summary
-------
StandbySlotsHaveCaughtup() keeps a process-local cache, ss_oldest_flush_lsn,
of the oldest restart_lsn among the physical slots listed in
synchronized_standby_slots. The cache is reset only by the
synchronized_standby_slots GUC assign hook. It is not invalidated when a
configured physical slot is dropped, invalidated, recreated with a lower
restart_lsn, or when its restart_lsn moves backwards.
As a result, after the cache has once been populated, a later
pg_logical_slot_get_changes() call in the same backend can return changes
and
advance the failover logical slot's confirmed_flush without checking that
the
configured physical slot still exists, is valid, or has actually caught up.
This contradicts the documented guarantee of synchronized_standby_slots:
"Logical WAL sender processes will send decoded changes to plugins only
after the specified replication slots confirm receiving WAL. This
guarantees that logical replication failover slots do not consume
changes
until those changes are received and flushed to corresponding physical
standbys."
and:
"Note that logical replication will not proceed if the slots specified
in
the synchronized_standby_slots do not exist or are invalidated."
The cache is per-process, so the reproduction must run in a single backend.
A
new backend rescans the slots and behaves correctly, which confirms the
cache
is the cause.
Reproduction
------------
Configure synchronized_standby_slots = 'phys' in postgresql.conf and start
the
server, or create the physical slot first, set the GUC, reload, and use a
new
session. Use a database with the test_decoding output plugin installed.
Then run the following in ONE psql session:
\set ON_ERROR_STOP on
SET statement_timeout = '10s';
CREATE TABLE t(a int);
SELECT pg_create_physical_replication_slot('phys', true);
SELECT pg_create_logical_replication_slot('logic', 'test_decoding',
false, false, true);
INSERT INTO t VALUES (1);
SELECT pg_current_wal_flush_lsn() AS f1 \gset
INSERT INTO t VALUES (2);
SELECT pg_current_wal_flush_lsn() AS f2 \gset
-- Make phys caught up, but only consume the first transaction.
SELECT pg_replication_slot_advance('phys', :'f2'::pg_lsn);
-- First call: sets ss_oldest_flush_lsn to phys.restart_lsn (f2).
SELECT count(*) FROM pg_logical_slot_get_changes('logic', :'f1'::pg_lsn,
NULL, 'skip-empty-xacts', '1', 'include-xids', '0');
-- Remove the synchronized physical slot.
SELECT pg_drop_replication_slot('phys');
-- Second call: should block/error because phys no longer exists.
-- Instead it returns the second transaction and advances
-- logic.confirmed_flush to f2.
SELECT lsn, data FROM pg_logical_slot_get_changes('logic',
:'f2'::pg_lsn, NULL, 'skip-empty-xacts', '1', 'include-xids', '0');
SELECT slot_name, restart_lsn, confirmed_flush_lsn FROM
pg_replication_slots ORDER BY 1;
Observed
--------
The second pg_logical_slot_get_changes() call returns:
0/017EB620 | BEGIN
0/017EB620 | table public.t: INSERT: a[integer]:2
0/017EB690 | COMMIT
and the slot listing shows:
logic | restart_lsn 0/017EB578 | confirmed_flush_lsn 0/017EB690
phys | dropped
No "replication slot \"phys\" ... does not exist" warning is emitted.
If the second call is executed in a new session, it logs the missing-slot
warning and waits, as expected.
The same happens after recreating the physical slot with the same name:
SELECT pg_drop_replication_slot('phys');
SELECT pg_create_physical_replication_slot('phys', true);
-- New phys.restart_lsn is much older than f2:
-- new phys.restart_lsn = 0/017EE078
-- f2 = 0/01808BF0
-- But the second call still returns the second transaction and sets
-- logic.confirmed_flush_lsn = 0/01808BF0.
I also reproduced it with invalidation instead of drop, using
idle_replication_slot_timeout:
- create phys and logic, insert rows, advance phys to f2;
- the first pg_logical_slot_get_changes() call populates the cache;
- sleep longer than idle_replication_slot_timeout;
- CHECKPOINT invalidates phys (pg_replication_slots shows
invalidation_reason = 'idle_timeout');
- the next call in the same session still returns changes and advances
logic.confirmed_flush to f2, without reporting the invalidated slot.
Expected
--------
The second call should not return changes and should not advance
confirmed_flush while phys is missing, invalidated, or has a restart_lsn
below
the requested position. It should block or report the slot state, as it
does
in a fresh backend.
Root Cause
----------
All writes to ss_oldest_flush_lsn are:
src/backend/replication/slot.c:185 initialization
src/backend/replication/slot.c:3088 assign_synchronized_standby_slots()
src/backend/replication/slot.c:3266 StandbySlotsHaveCaughtup()
The assign hook is only invoked when the GUC is (re)processed. Slot
drop/invalidation/creation/reservation and PhysicalConfirmReceivedLocation()
do not reset the cache.
StandbySlotsHaveCaughtup() then short-circuits before checking the slots:
if (XLogRecPtrIsValid(ss_oldest_flush_lsn) &&
ss_oldest_flush_lsn >= wait_for_lsn)
return true;
The existence, slot-type, invalidation, and restart_lsn checks below are
therefore skipped. Even if a later scan detects the problem, the cache is
not
cleared and remains usable for future lower wait_for_lsn values.
Regards,
Zhang
| From | Date | Subject | |
|---|---|---|---|
| Next Message | hi | 2026-12-04 09:24:06 | EXCEPTION block in a PARALLEL SAFE function fails CREATE TABLE AS |
| Previous Message | ZhangChi | 2026-12-04 08:58:46 | Re: BUG #19977: constraint_exclusion Silently Returns 0 Rows on Subpartitioned Table |