| From: | Dmitry Fomin <fomin(dot)list(at)gmail(dot)com> |
|---|---|
| To: | pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Cc: | Vamshidhar Poralla <vporalla(at)gitlab(dot)com> |
| Subject: | Synced failover slot publishes remote catalog_xmin after local decoding |
| Date: | 2026-12-04 20:57:04 |
| Message-ID: | CAPHG-0mviasCp=-rkfQ+jWU6U3V+mzj=e_UPsNzcAdyYNrAmvQ@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hello,
After locally decoding WAL to advance a synchronized failover slot,
update_local_synced_slot() can
publish the remote slot's catalog_xmin while saving an older local
catalog_xmin. Hot standby feedback
then lets the primary remove catalog tuples the standby's slot still
needs. A later local xmin update
resets the published horizon to the locally saved value, and replay of
a catalog cleanup invalidates
the slot, with hot_standby_feedback = on and a physical slot for the
standby in place:
LOG: terminating process 6852 to release replication slot "siphon_slot"
DETAIL: The slot conflicted with xid horizon 116356.
LOG: invalidating obsolete replication slot "siphon_slot"
DETAIL: The slot conflicted with xid horizon 116356.
The slot sync worker then drops the copy and recreates it as a
temporary slot. A promotion before the
replacement becomes persistent leaves no usable failover slot on the
new primary (in our two-node lab
pg_replication_slots was empty after such a promotion).
This issue was found during a joint investigation with Vamshidhar
Poralla (vporalla(at)gitlab(dot)com, CC'd)
after synced copies of a logical slot were invalidated 12 times in 13
days on a production PostgreSQL
18.4 cluster (Vamshidhar identified the invalidations in production
and captured the production
telemetry confirming the behavior; the synthetic reproduction and
proposed patch below are mine). The
reproduction below is on 18.7.
== Where ==
src/backend/replication/logical/slotsync.c,
update_local_synced_slot(), REL_18_7 line 358 (block at
lines 355-362). The same assignment is present on REL_17_STABLE
through REL_19_STABLE and master at
the time of writing.
if (updated_xmin_or_lsn)
{
SpinLockAcquire(&slot->mutex);
slot->effective_catalog_xmin = remote_slot->catalog_xmin;
SpinLockRelease(&slot->mutex);
ReplicationSlotsComputeRequiredXmin(false);
...
When a serialized snapshot exists at the remote restart_lsn, the
function copies restart_lsn,
confirmed_flush and catalog_xmin from the remote slot (lines 270-274).
data.catalog_xmin then equals
the remote value, so assigning remote_slot->catalog_xmin matches what was saved.
When no snapshot exists there, the function advances the copy by local
decoding with
LogicalSlotAdvanceAndCheckSnapState() (line 287). data.catalog_xmin
then comes from the local candidate
handling in logical.c (LogicalIncreaseXminForSlot, applied in
LogicalConfirmReceivedLocation). It can
keep its previous value or take a local candidate, and either can be
older than the fetched remote
catalog_xmin. The assignment above nevertheless publishes the remote
value. That contradicts the rule
in the comment just before it ("We have to write the changed xmin to
disk *before* we change the
in-memory value"): the published horizon is ahead of the saved one.
(Conversely, if local decoding ever
advances data.catalog_xmin ahead of remote_slot->catalog_xmin,
assigning remote_slot->catalog_xmin
moves effective_catalog_xmin backward without ProcArrayLock.)
While effective_catalog_xmin is inflated to remote_slot->catalog_xmin,
DetermineSlotInvalidationCause()
in slot.c checks effective_catalog_xmin rather than data.catalog_xmin.
Replayed catalog cleanups with a
conflict horizon between data.catalog_xmin and
remote_slot->catalog_xmin therefore do not invalidate
the slot, even though catalog tuples required for data.catalog_xmin
are being removed. A crash/restart
or promotion in that window can leave a non-invalidated slot whose
required catalog tuples are already
gone.
The walreceiver sends the standby's published catalog horizon as hot
standby feedback. On the primary,
PhysicalReplicationSlotNewXmin() does not lower an established
catalog_xmin on older feedback, so the
released catalog retention cannot be taken back.
A later xmin update in LogicalConfirmReceivedLocation() (logical.c
line 2013) sets
effective_catalog_xmin = data.catalog_xmin. The saved value advances,
but the published value moves
back relative to what slotsync published. A replayed catalog cleanup
for the slot's database or a
shared catalog then invalidates the slot when its conflict horizon is
at or beyond the slot's
effective catalog_xmin (ResolveRecoveryConflictWithSnapshot ->
InvalidateObsoleteReplicationSlots,
RS_INVAL_HORIZON).
== Reproduction ==
Script attached (repro-one-host.sh): two instances on one host,
PostgreSQL 18.7 built from source
(./configure --without-icu --without-readline --enable-debug, macOS
26.4 arm64, also run on Ubuntu
24.04 x86_64 with the PGDG 18.7 package on two separate hosts).
primary: wal_level = logical, autovacuum_naptime = 1s, a physical slot
for the standby, a table in a
publication, a pgoutput slot created with failover = true,
pg_recvlogical as the consumer with
--status-interval 1 --fsync-interval 1. Workload: continuous inserts
(WAL and xids), an ANALYZE loop
over 200 small tables (pg_statistic churn and dead catalog tuples),
and overlapping transactions that
hold an xid for 60 s, a new one every 10 s.
standby: pg_basebackup -R with primary_slot_name, hot_standby_feedback
= on, sync_replication_slots = on,
dbname in primary_conninfo, wal_receiver_status_interval = 1,
log_min_messages = debug2. The DEBUG2
line "sending hot standby feedback xmin N ... catalog_xmin M" gives
the published catalog horizon. With
one logical slot on the standby that is the copy's effective_catalog_xmin.
1. Wait for the copy to be synced = t, temporary = f.
2. On the standby: ALTER SYSTEM SET sync_replication_slots = off;
SELECT pg_reload_conf(). Wait 180 s.
The copy stands still, the consumer and the remote slot move on.
3. ALTER SYSTEM SET sync_replication_slots = on; SELECT pg_reload_conf().
4. In the observed runs the first synchronization used local decoding.
The unpatched standby published
newer catalog horizons, later sent lower values, and the copy was
invalidated 4 to 8 minutes after
the resume. Timing depends on workload and scheduling.
The overlapping transactions matter for this workload: in one run with
only short transactions the
published value never moved backward. We did not establish a minimum duration.
== Observed, unpatched 18.7, one host, 2026-12-03 (standby log excerpt
attached; times below converted
to UTC, attached standby logs use CET, UTC+1) ==
12:36:28 slot sync resumed, copy catalog_xmin 1372, primary's
slot catalog_xmin 73303
12:39:39 first decode cycle ends; published catalog_xmin jumps to
73303 (remote)
12:41:34 published catalog_xmin goes from 73303 back to 19771
(first xmin update in next cycle)
12:43:10 second decode cycle ends; published catalog_xmin jumps
to 113803 (remote)
12:43:37 published catalog_xmin goes from 113803 back to 79018
12:44:34 third decode cycle ends; published catalog_xmin jumps to
135871 (remote)
12:44:39 updated xmin: 1 in next decode cycle lowers
effective_catalog_xmin below 116356;
3 ms later (before the next 1 s feedback line):
terminating process ... to release replication slot "siphon_slot"
The slot conflicted with xid horizon 116356.
invalidating obsolete replication slot "siphon_slot"
12:44:40 dropped replication slot "siphon_slot" of database with OID 5
12:47:56 newly created replication slot "siphon_slot" is sync-ready now
The same order (copy's saved value < conflict horizon < published
value, the primary's physical slot
holding the published value) was seen in the two-node runs and in the
production logs.
== Proposed fix ==
Publish the value that was just saved (patch attached, against
REL_18_7, applies to master):
- slot->effective_catalog_xmin = remote_slot->catalog_xmin;
+ slot->effective_catalog_xmin = slot->data.catalog_xmin;
The direct-copy branch is unchanged, because data.catalog_xmin already
holds the remote value there. In
the decode branch the standby keeps feeding back the older value it
still needs, and the primary keeps
the catalog tuples until the copy catches up. The cost is catalog
retention on the primary for as long
as a copy lags, which is the retention the slot requires. The patch
cannot help a copy whose retention
was already released by the unpatched code.
Tested with the attached script, same workload, 180 s pause, the two
builds running in parallel on
different ports:
unpatched: invalidation 8 min 12 s after the resume, conflict
horizon 116356, two feedback-logged
decreases before it (73303 -> 19771, 113803 -> 79018)
plus a third jump to 135871
followed by an xmin update 3 ms before invalidation
patched: 20 min 27 s of log after the resume, 14 starts of
logical decoding, no observed decrease
of the published catalog_xmin, no invalidation
A second pair of runs (both builds again in parallel, ~28 min after
the resume): unpatched, two
decreases of the published catalog_xmin (81774 -> 20197, 124722 ->
90237) over 20 decode cycles but no
invalidation within that time. Patched, 21 decode cycles, no decrease.
The last step depends on a
catalog cleanup with a high enough horizon being replayed while the
published value is low, which the
workload does not force. The backward move itself was observed in
every unpatched run.
A third unpatched run alone on the machine: feedback-logged decreases
85488 -> 20340 and
157736 -> 102973, then a jump to 191860 and an xmin update 4 ms before
invalidation (8 min 19 s after
the resume, conflict horizon 170456). Its unedited transcript and
standby log excerpt are attached
(note: the 191860 -> 0 line in that transcript is post-invalidation
InvalidTransactionId; the attached
repro-one-host.sh includes minor post-run fixes to ignore 0 in the
summary, log in UTC, and guard empty
arrays under macOS bash 3.2).
We have not written a regression test. Calling
pg_sync_replication_slots() directly in a TAP test (e.g.
040_standby_failover_slots_sync.pl) with sync_replication_slots = off
while holding an open xid behind
the remote slot's catalog_xmin could drive the decode branch
deterministically without needing an
injection point.
Attachments:
- v1-0001-Publish-local-catalog_xmin-in-update_local_synced_slot.patch
- repro-one-host.sh
- standby-logs.tar.gz (standby log lines after the resume for the
first and second pairs, filtered to
feedback, decoding and slot messages and deduplicated by content:
unpatched-standby-since-resume.txt,
patched-standby-since-resume.txt, *-run2-*; plus the transcript and
standby log excerpt of the third
unpatched run: *-run3-*)
Regards,
Dmitry Fomin
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Publish-local-catalog_xmin-in-update_local_synced_slot.patch | application/octet-stream | 2.9 KB |
| repro-one-host.sh | application/x-sh | 8.9 KB |
| standby-logs.tar.gz | application/x-gzip | 17.4 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Jacob Champion | 2026-12-04 20:54:02 | Re: [PATCH] Clarify that ssl_groups is for any key exchange groups |