walreceiver: stale hot_standby_feedback xmin on primary when wal_receiver_status_interval = 0 (PG16 and later)

Started by Vaibhav Dalviabout 1 month ago5 messageshackers
Jump to latest
#1Vaibhav Dalvi
vaibhav.dalvi@enterprisedb.com

Hi Hackers,

When hot_standby_feedback is on and wal_receiver_status_interval is 0,
the primary keeps the xmin sent at connection time and never releases
it, so VACUUM on the primary is held back. I see this on PG16, 17, 18,
19 and master. PG14 and PG15 are not affected (reason at the end).

*Cause:* XLogWalRcvSendHSFeedback() decides "feedback is disabled" in two
different ways. The wakeup time and the early return treat it as disabled
when
the interval is <= 0 or hot_standby_feedback is off, but the code that
picks the
xmin checks only hot_standby_feedback. So the first message at connect
(immed = true)
carries a real xmin, and then no more messages are sent because the wakeup
is infinity.

The same happens when the interval is changed from a positive value to 0
and the configuration is reloaded. The feedback sent on reload carries
a fresh valid xmin, and then nothing follows.

*Steps to reproduce:*

initdb -D pg20_primary
echo "port = 5525" >> pg20_primary/postgresql.conf
pg_ctl -D pg20_primary -l primary.log -w start

pg_basebackup -p 5525 -D pg20_standby -R -X stream
cat >> pg20_standby/postgresql.conf <<EOF
port = 5526
hot_standby_feedback = on
wal_receiver_status_interval = 0
EOF
pg_ctl -D pg20_standby -l standby.log -w start

primary="psql -p 5525 -d postgres -X"
$primary -c "select backend_xmin, age(backend_xmin)
from pg_stat_replication"
backend_xmin | age
--------------+-----
664 | 0

$primary -c "create table hsf_t with (autovacuum_enabled = off)
as select g a from generate_series(1,100000) g"
for i in $(seq 2000); do echo "select txid_current();"; done |
$primary -Atq > /dev/null
$primary -c "delete from hsf_t"
$primary -c "checkpoint"
sleep 6

$primary -c "select backend_xmin, age(backend_xmin) from
pg_stat_replication"
backend_xmin | age
--------------+------
664 | 2013

$primary -c "vacuum (verbose, index_cleanup off) hsf_t"
tuples: 0 removed, 100000 remain, 100000 are dead but not yet removable
removable cutoff: 664, which was 2013 XIDs old when operation ended

The xmin does not move. As a control, set the interval to 1 on the
standby and reload:

psql -p 5526 -d postgres -X -c "alter system set
wal_receiver_status_interval = 1" -c "select pg_reload_conf()"
sleep 3

Now backend_xmin is 2677 (age 0), and the next VACUUM removes all the
100000 tuples.

*Fix:* compute the xmin only when feedback is really enabled.

-       if (hot_standby_feedback)
+       if (hot_standby_feedback && wal_receiver_status_interval > 0)

With interval 0, the walreceiver now sends InvalidTransactionId, so the
primary releases the xmin. This keeps "0 disables the status updates",
as the documentation says. Please let me know if people agree with it.

The patches also add a TAP test (0NN_hot_standby_feedback_status_interval.pl
in src/test/recovery). It uses a physical slot, so the xmin kept by the
primary is visible in pg_replication_slots.xmin. It checks that a positive
interval sends a valid xmin (control), and that interval 0 leaves no
xmin, both on reload and on connect. The zero-interval cases look at the
DEBUG2 feedback message in the standby log, so there is no timing race,
and then poll until the slot xmin is NULL. It fails without the fix and
passes with it.

The test makes the walreceiver reconnect before checking the connect-time
feedback. No feedback is sent while Hot Standby is not yet active, and
with feedback disabled it is not retried. So an old slot xmin may stay if
the walreceiver connects too early. That is a separate issue, and I will
start a new thread for it.

I am attaching 4 patches. The code change is the same in all. Only the
test file number and the meson.build context differ per branch:

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patch

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG19.patch

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG18.patch

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG16-17.patch

Why PG14 and PG15 are not affected: I ran the same steps there, and the
xmin keeps moving (733 to 2735 on PG14, 734 to 2736 on PG15). These
versions have no wakeup time array. Their "at most once per interval"
check is

if (!TimestampDifferenceExceeds(sendTime, now,
wal_receiver_status_interval * 1000))
return;

and with interval 0 it is always true, so feedback with a fresh xmin
goes out on every call. The infinite wakeup was added in PG16. (Note
that in PG14 and PG15 an interval of 0 does not really disable the
feedback, but I did not change that.)

Thanks and regards,
Vaibhav Dalvi

Attachments:

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG18.patchapplication/octet-stream; name=v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG18.patchDownload+107-6
v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patchapplication/octet-stream; name=v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patchDownload+107-6
v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG16-17.patchapplication/octet-stream; name=v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG16-17.patchDownload+107-6
v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG19.patchapplication/octet-stream; name=v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG19.patchDownload+107-6
#2Vaibhav Dalvi
vaibhav.dalvi@enterprisedb.com
In reply to: Vaibhav Dalvi (#1)
Re: walreceiver: stale hot_standby_feedback xmin on primary when wal_receiver_status_interval = 0 (PG16 and later)

Hi,

I am planning to add a CommitFest entry for this bug. Please find the patch
attached for master.

Could you please review it?

Regards,
Vaibhav

On Thu, Oct 15, 2026 at 4:55 PM Vaibhav Dalvi <
vaibhav.dalvi@enterprisedb.com> wrote:

Show quoted text

Hi Hackers,

When hot_standby_feedback is on and wal_receiver_status_interval is 0,
the primary keeps the xmin sent at connection time and never releases
it, so VACUUM on the primary is held back. I see this on PG16, 17, 18,
19 and master. PG14 and PG15 are not affected (reason at the end).

*Cause:* XLogWalRcvSendHSFeedback() decides "feedback is disabled" in two
different ways. The wakeup time and the early return treat it as disabled
when
the interval is <= 0 or hot_standby_feedback is off, but the code that
picks the
xmin checks only hot_standby_feedback. So the first message at connect
(immed = true)
carries a real xmin, and then no more messages are sent because the wakeup
is infinity.

The same happens when the interval is changed from a positive value to 0
and the configuration is reloaded. The feedback sent on reload carries
a fresh valid xmin, and then nothing follows.

*Steps to reproduce:*

initdb -D pg20_primary
echo "port = 5525" >> pg20_primary/postgresql.conf
pg_ctl -D pg20_primary -l primary.log -w start

pg_basebackup -p 5525 -D pg20_standby -R -X stream
cat >> pg20_standby/postgresql.conf <<EOF
port = 5526
hot_standby_feedback = on
wal_receiver_status_interval = 0
EOF
pg_ctl -D pg20_standby -l standby.log -w start

primary="psql -p 5525 -d postgres -X"
$primary -c "select backend_xmin, age(backend_xmin)
from pg_stat_replication"
backend_xmin | age
--------------+-----
664 | 0

$primary -c "create table hsf_t with (autovacuum_enabled = off)
as select g a from generate_series(1,100000) g"
for i in $(seq 2000); do echo "select txid_current();"; done |
$primary -Atq > /dev/null
$primary -c "delete from hsf_t"
$primary -c "checkpoint"
sleep 6

$primary -c "select backend_xmin, age(backend_xmin) from
pg_stat_replication"
backend_xmin | age
--------------+------
664 | 2013

$primary -c "vacuum (verbose, index_cleanup off) hsf_t"
tuples: 0 removed, 100000 remain, 100000 are dead but not yet removable
removable cutoff: 664, which was 2013 XIDs old when operation ended

The xmin does not move. As a control, set the interval to 1 on the
standby and reload:

psql -p 5526 -d postgres -X -c "alter system set
wal_receiver_status_interval = 1" -c "select pg_reload_conf()"
sleep 3

Now backend_xmin is 2677 (age 0), and the next VACUUM removes all the
100000 tuples.

*Fix:* compute the xmin only when feedback is really enabled.

-       if (hot_standby_feedback)
+       if (hot_standby_feedback && wal_receiver_status_interval > 0)

With interval 0, the walreceiver now sends InvalidTransactionId, so the
primary releases the xmin. This keeps "0 disables the status updates",
as the documentation says. Please let me know if people agree with it.

The patches also add a TAP test
(0NN_hot_standby_feedback_status_interval.pl
in src/test/recovery). It uses a physical slot, so the xmin kept by the
primary is visible in pg_replication_slots.xmin. It checks that a positive
interval sends a valid xmin (control), and that interval 0 leaves no
xmin, both on reload and on connect. The zero-interval cases look at the
DEBUG2 feedback message in the standby log, so there is no timing race,
and then poll until the slot xmin is NULL. It fails without the fix and
passes with it.

The test makes the walreceiver reconnect before checking the connect-time
feedback. No feedback is sent while Hot Standby is not yet active, and
with feedback disabled it is not retried. So an old slot xmin may stay if
the walreceiver connects too early. That is a separate issue, and I will
start a new thread for it.

I am attaching 4 patches. The code change is the same in all. Only the
test file number and the meson.build context differ per branch:

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patch

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG19.patch

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG18.patch

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG16-17.patch

Why PG14 and PG15 are not affected: I ran the same steps there, and the
xmin keeps moving (733 to 2735 on PG14, 734 to 2736 on PG15). These
versions have no wakeup time array. Their "at most once per interval"
check is

if (!TimestampDifferenceExceeds(sendTime, now,
wal_receiver_status_interval * 1000))
return;

and with interval 0 it is always true, so feedback with a fresh xmin
goes out on every call. The infinite wakeup was added in PG16. (Note
that in PG14 and PG15 an interval of 0 does not really disable the
feedback, but I did not change that.)

Thanks and regards,
Vaibhav Dalvi

Attachments:

t254208_2
v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patchapplication/octet-stream; name=v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patchDownload+107-6
#3Vaibhav Dalvi
vaibhav.dalvi@enterprisedb.com
In reply to: Vaibhav Dalvi (#2)
Re: walreceiver: stale hot_standby_feedback xmin on primary when wal_receiver_status_interval = 0 (PG16 and later)

Hi,

I added a CommitFest entry: https://commitfest.postgresql.org/patch/7480.

CFBot requested a rebase, so I rebased it, and here's the revised patch.

Regards,
Vaibhav

On Mon, Oct 19, 2026 at 9:43 AM Vaibhav Dalvi <
vaibhav.dalvi@enterprisedb.com> wrote:

Show quoted text

Hi,

I am planning to add a CommitFest entry for this bug. Please find the
patch attached for master.

Could you please review it?

Regards,
Vaibhav

On Thu, Oct 15, 2026 at 4:55 PM Vaibhav Dalvi <
vaibhav.dalvi@enterprisedb.com> wrote:

Hi Hackers,

When hot_standby_feedback is on and wal_receiver_status_interval is 0,
the primary keeps the xmin sent at connection time and never releases
it, so VACUUM on the primary is held back. I see this on PG16, 17, 18,
19 and master. PG14 and PG15 are not affected (reason at the end).

*Cause:* XLogWalRcvSendHSFeedback() decides "feedback is disabled" in two
different ways. The wakeup time and the early return treat it as
disabled when
the interval is <= 0 or hot_standby_feedback is off, but the code that
picks the
xmin checks only hot_standby_feedback. So the first message at connect
(immed = true)
carries a real xmin, and then no more messages are sent because the
wakeup is infinity.

The same happens when the interval is changed from a positive value to 0
and the configuration is reloaded. The feedback sent on reload carries
a fresh valid xmin, and then nothing follows.

*Steps to reproduce:*

initdb -D pg20_primary
echo "port = 5525" >> pg20_primary/postgresql.conf
pg_ctl -D pg20_primary -l primary.log -w start

pg_basebackup -p 5525 -D pg20_standby -R -X stream
cat >> pg20_standby/postgresql.conf <<EOF
port = 5526
hot_standby_feedback = on
wal_receiver_status_interval = 0
EOF
pg_ctl -D pg20_standby -l standby.log -w start

primary="psql -p 5525 -d postgres -X"
$primary -c "select backend_xmin, age(backend_xmin)
from pg_stat_replication"
backend_xmin | age
--------------+-----
664 | 0

$primary -c "create table hsf_t with (autovacuum_enabled = off)
as select g a from generate_series(1,100000) g"
for i in $(seq 2000); do echo "select txid_current();"; done |
$primary -Atq > /dev/null
$primary -c "delete from hsf_t"
$primary -c "checkpoint"
sleep 6

$primary -c "select backend_xmin, age(backend_xmin) from
pg_stat_replication"
backend_xmin | age
--------------+------
664 | 2013

$primary -c "vacuum (verbose, index_cleanup off) hsf_t"
tuples: 0 removed, 100000 remain, 100000 are dead but not yet removable
removable cutoff: 664, which was 2013 XIDs old when operation ended

The xmin does not move. As a control, set the interval to 1 on the
standby and reload:

psql -p 5526 -d postgres -X -c "alter system set
wal_receiver_status_interval = 1" -c "select pg_reload_conf()"
sleep 3

Now backend_xmin is 2677 (age 0), and the next VACUUM removes all the
100000 tuples.

*Fix:* compute the xmin only when feedback is really enabled.

-       if (hot_standby_feedback)
+       if (hot_standby_feedback && wal_receiver_status_interval > 0)

With interval 0, the walreceiver now sends InvalidTransactionId, so the
primary releases the xmin. This keeps "0 disables the status updates",
as the documentation says. Please let me know if people agree with it.

The patches also add a TAP test
(0NN_hot_standby_feedback_status_interval.pl
in src/test/recovery). It uses a physical slot, so the xmin kept by the
primary is visible in pg_replication_slots.xmin. It checks that a
positive
interval sends a valid xmin (control), and that interval 0 leaves no
xmin, both on reload and on connect. The zero-interval cases look at the
DEBUG2 feedback message in the standby log, so there is no timing race,
and then poll until the slot xmin is NULL. It fails without the fix and
passes with it.

The test makes the walreceiver reconnect before checking the connect-time
feedback. No feedback is sent while Hot Standby is not yet active, and
with feedback disabled it is not retried. So an old slot xmin may stay if
the walreceiver connects too early. That is a separate issue, and I will
start a new thread for it.

I am attaching 4 patches. The code change is the same in all. Only the
test file number and the meson.build context differ per branch:

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patch

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG19.patch

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG18.patch

v1-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG16-17.patch

Why PG14 and PG15 are not affected: I ran the same steps there, and the
xmin keeps moving (733 to 2735 on PG14, 734 to 2736 on PG15). These
versions have no wakeup time array. Their "at most once per interval"
check is

if (!TimestampDifferenceExceeds(sendTime, now,
wal_receiver_status_interval * 1000))
return;

and with interval 0 it is always true, so feedback with a fresh xmin
goes out on every call. The infinite wakeup was added in PG16. (Note
that in PG14 and PG15 an interval of 0 does not really disable the
feedback, but I did not change that.)

Thanks and regards,
Vaibhav Dalvi

Attachments:

t254208_3
v2-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patchapplication/octet-stream; name=v2-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patchDownload+107-6
#4Joao Rito
joaovictor.rito@gmail.com
In reply to: Vaibhav Dalvi (#3)
Re: walreceiver: stale hot_standby_feedback xmin on primary when wal_receiver_status_interval = 0 (PG16 and later)

Hi Vaibhav,

Thank you for finding and fixing this. A stale xmin that holds back vacuum
on the primary, with nothing visible on the standby side, is hard to
diagnose in production, so I think the fix is worth having.

I reviewed
v2-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patch
(2026-10-19). I did not test the back-branch variants.

Tested against master c074737f687f604b64f6ee830fccbbe854719823 (2026-11-09),
Debian bookworm container, ./configure --enable-cassert --enable-debug
--enable-tap-tests.

Applying and building
- git apply works cleanly on current master.
- Build is clean, with no compiler warnings.

Tests
- New test 061_hot_standby_feedback_status_interval.pl, with only the test
applied (no fix): fails. The first check passes, then wait_for_log times
out after the default 180 seconds waiting for "sending hot standby
feedback xmin 0 ...". So the test does catch the bug.
- Same test with the fix: 5 tests, PASS.
- make check: all 243 tests passed.
- make -C src/test/recovery check: 60 files, 695 tests, PASS.

Manual testing
Setup: primary on port 5525 (wal_level = logical, autovacuum = off, physical
slot sb), standby created with pg_basebackup -R -S sb, hot_standby_feedback
= on.

1. Standby started with wal_receiver_status_interval = 0, walsender
terminated once so the walreceiver reconnects after Hot Standby is active.
Then on the primary: create a table with 100000 rows, burn 1000 XIDs, delete
all rows, VACUUM VERBOSE.
- master: slot xmin stays at 664. VACUUM: "0 removed, 100000 remain,
100000 are dead but not yet removable", "removable cutoff: 664, which
was 1002 XIDs old".
- patched: slot xmin is NULL. VACUUM: "100000 removed, 0 remain",
"removable cutoff: 1666, which was 1 XIDs old".
2. Reload interval 0 -> 1 (control): slot xmin advances on both builds, as
expected.
3. Reload interval 1 -> 0:
- master: slot xmin stays at the value sent on reload (1666) and does not
move after 500 more XIDs.
- patched: slot xmin becomes NULL right after the reload.
4. hot_standby_feedback = off: slot xmin NULL on both builds, as expected.

So the patch does what the commit message says, and the stale xmin is gone.

5. Logical slot on the standby (test_decoding), created with interval = 1,
then interval reloaded to 0. On the primary: 50 CREATE TABLE / DROP TABLE
pairs, then VACUUM pg_class, pg_attribute, pg_type, pg_depend.
- master: the primary slot keeps catalog_xmin 664 (stale, but it protects
the standby slot). The standby slot is not invalidated, and
pg_logical_slot_get_changes() works.
- patched: the primary slot catalog_xmin becomes NULL. On the standby:
LOG: invalidating obsolete replication slot "lsb"
DETAIL: The slot conflicted with xid horizon 675.
pg_replication_slots shows invalidation_reason = rows_removed, and
decoding fails with
ERROR: can no longer access replication slot "lsb"

See comment 2 for this case.

Comments

1. Semantics of wal_receiver_status_interval = 0. The patch makes "interval
= 0" mean "hot standby feedback is off". If I read REL_15_STABLE correctly,
the behaviour there was different. In XLogWalRcvSendHSFeedback(), with
interval = 0, TimestampDifferenceExceeds(sendTime, now, 0) is always true,
so feedback with a valid xmin was sent on every non-immediate call. So on
PG14/15 the combination hot_standby_feedback = on with interval = 0 worked
(feedback was sent often). On PG16+ it sends one message and then leaves the
xmin stale. With this patch it sends no xmin at all. A user upgrading from
15 with this setting would see a third behaviour, which may cause unexpected
query cancellations on the standby. I think the "off" semantics is a
reasonable choice, and it matches the early-return condition, but I wonder
if restoring the PG15 behaviour (keep sending feedback when interval = 0)
was considered. In any case, it would help to say in the commit message
which behaviour is intended, and why, since this is going to the back
branches.

2. catalog_xmin and logical slots on the standby. As case 5 shows, with the
fix a logical slot on the standby can be invalidated when interval = 0, even
with hot_standby_feedback = on and a physical slot, which is the setup that
logicaldecoding.sgml recommends. Before the fix, the stale catalog_xmin kept
the slot usable (as a side effect of the bug). I think this is a consequence
of comment 1 rather than a new bug, but it is user visible. A few options,
not all needed:
- Mention it in the commit message and the release notes.
- slotsync.c (the check at line 1291) requires hot_standby_feedback for
slot synchronization, but does not check wal_receiver_status_interval.
With this patch, interval = 0 means the primary gets no catalog_xmin
for the synced slots, so the same check could also reject interval = 0,
or at least warn about it.
- A WARNING at walreceiver start, or on reload, when hot_standby_feedback
= on and wal_receiver_status_interval = 0, saying that no feedback will
be sent.

3. Documentation. Neither the hot_standby_feedback nor the
wal_receiver_status_interval entry in config.sgml says that interval = 0
disables hot standby feedback. hot_standby_feedback says "Feedback messages
will not be sent more frequently than once per
wal_receiver_status_interval", which a reader could take to mean "no limit"
when the value is 0. A sentence such as "If wal_receiver_status_interval is
zero, hot standby feedback is not sent." would make the new behaviour clear.

4. Code. The condition "hot_standby_feedback && wal_receiver_status_interval

0" now appears in a negated form in the early return at the top of the

function, in WalRcvComputeNextWakeup(), and in the new check. A small helper
or local bool (for example "feedback_enabled") would keep the three places
consistent. Minor, and a matter of taste.

5. Known gap. As you note in the commit message, an old xmin left on the
slot by a previous connection is not released when Hot Standby is not active
yet at connect time. Since that leaves the same symptom (vacuum held back by
a slot that nothing updates), it may be worth a short XXX comment near the
HotStandbyActive() check, so the gap is visible in the code and not only in
the commit message.

6. Test.
- The test covers the reload and connect cases well, and the comments
explain why the walsender is terminated. Nice.
- wal_receiver_timeout = 0 is set without a comment. I assume it avoids a
timeout when no status messages are sent with interval = 0. A short
comment would help.
- On a build without the fix, the test fails only after the 180 second
wait_for_log timeout. That is the usual pattern, so fine.
- There is no check of catalog_xmin. If the behaviour in comment 2 is
intended, a check that catalog_xmin is also released (or a logical slot
test) would document it.

Verdict: Waiting on Author, mainly for comments 1 and 2. I think the
semantics of interval = 0, and the impact on logical slots on the standby,
should be settled (and written down in the commit message and docs) before
this goes to the back branches. The code change itself is small and correct
for the chosen semantics, and the test passes and fails as expected.

Thanks again, and I am happy to test the next version, including the
back-branch patches.

Regards,
Joao Rito

#5Vaibhav Dalvi
vaibhav.dalvi@enterprisedb.com
In reply to: Joao Rito (#4)
Re: walreceiver: stale hot_standby_feedback xmin on primary when wal_receiver_status_interval = 0 (PG16 and later)

Hi Joao,

Thank you for the detailed review and for testing so many cases.

Attached are v3 patches for master, PG19, PG18, PG17 and PG16.
I have addressed your comments as below.

1. Semantics of wal_receiver_status_interval = 0.

I kept "0 means feedback is disabled". Since v16 the early return, the
wakeup computation and XLogWalRcvSendReply() already treat zero as
disabled, and the docs also say so. The PG14/15 behaviour was only a
side effect of TimestampDifferenceExceeds() being always true with zero,
so I did not restore it. I have written this clearly in the commit
message. PG14/15 are not changed.

2. catalog_xmin and logical slots on the standby.

Agreed, this is a consequence of point 1 and not a new bug. I have
documented it in the hot_standby_feedback entry, and added a test with a
logical slot on the standby. The test checks that catalog_xmin is held
with a positive interval, is released when the interval is set to 0, and
that the slot is then invalidated by vacuum on the primary.

I did not add a WARNING at walreceiver start, as it will add a new
message in all back branches for a setting which is now documented. A
check in slotsync.c for interval = 0 looks reasonable to me, but I think
it should be a separate master-only patch, because it needs the reload
detection to track the old interval also.

3. Documentation.

Done. Both hot_standby_feedback and wal_receiver_status_interval now say
that no feedback is sent when the interval is zero.

4. Code.

Done. I added a small helper, HotStandbyFeedbackEnabled(), and it is
used in the early return, WalRcvComputeNextWakeup() and the xmin
computation.

5. Known gap.

Added an XXX comment near the HotStandbyActive() check.

6. Test.

Added a comment for wal_receiver_timeout = 0. The new logical slot case
covers the catalog_xmin check.

Notes for the back branches: the test is numbered 062 on PG16-18, since
061 is already used there. Also, per-process log_min_messages is
available only in PG19, so PG16-18 use log_min_messages = debug2, and
PG16 checks pg_replication_slots.conflicting because there is no
invalidation_reason column. I ran the new test on all five branches and
it passes.

Please let me know if you have any more comments.

Regards,
Vaibhav Dalvi

On Tue, Nov 10, 2026 at 11:13 AM Joao Rito <joaovictor.rito@gmail.com>
wrote:

Show quoted text

Hi Vaibhav,

Thank you for finding and fixing this. A stale xmin that holds back vacuum
on the primary, with nothing visible on the standby side, is hard to
diagnose in production, so I think the fix is worth having.

I reviewed

v2-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patch
(2026-10-19). I did not test the back-branch variants.

Tested against master c074737f687f604b64f6ee830fccbbe854719823
(2026-11-09),
Debian bookworm container, ./configure --enable-cassert --enable-debug
--enable-tap-tests.

Applying and building
- git apply works cleanly on current master.
- Build is clean, with no compiler warnings.

Tests
- New test 061_hot_standby_feedback_status_interval.pl, with only the test
applied (no fix): fails. The first check passes, then wait_for_log times
out after the default 180 seconds waiting for "sending hot standby
feedback xmin 0 ...". So the test does catch the bug.
- Same test with the fix: 5 tests, PASS.
- make check: all 243 tests passed.
- make -C src/test/recovery check: 60 files, 695 tests, PASS.

Manual testing
Setup: primary on port 5525 (wal_level = logical, autovacuum = off,
physical
slot sb), standby created with pg_basebackup -R -S sb, hot_standby_feedback
= on.

1. Standby started with wal_receiver_status_interval = 0, walsender
terminated once so the walreceiver reconnects after Hot Standby is active.
Then on the primary: create a table with 100000 rows, burn 1000 XIDs,
delete
all rows, VACUUM VERBOSE.
- master: slot xmin stays at 664. VACUUM: "0 removed, 100000 remain,
100000 are dead but not yet removable", "removable cutoff: 664, which
was 1002 XIDs old".
- patched: slot xmin is NULL. VACUUM: "100000 removed, 0 remain",
"removable cutoff: 1666, which was 1 XIDs old".
2. Reload interval 0 -> 1 (control): slot xmin advances on both builds, as
expected.
3. Reload interval 1 -> 0:
- master: slot xmin stays at the value sent on reload (1666) and does
not
move after 500 more XIDs.
- patched: slot xmin becomes NULL right after the reload.
4. hot_standby_feedback = off: slot xmin NULL on both builds, as expected.

So the patch does what the commit message says, and the stale xmin is gone.

5. Logical slot on the standby (test_decoding), created with interval = 1,
then interval reloaded to 0. On the primary: 50 CREATE TABLE / DROP TABLE
pairs, then VACUUM pg_class, pg_attribute, pg_type, pg_depend.
- master: the primary slot keeps catalog_xmin 664 (stale, but it
protects
the standby slot). The standby slot is not invalidated, and
pg_logical_slot_get_changes() works.
- patched: the primary slot catalog_xmin becomes NULL. On the standby:
LOG: invalidating obsolete replication slot "lsb"
DETAIL: The slot conflicted with xid horizon 675.
pg_replication_slots shows invalidation_reason = rows_removed, and
decoding fails with
ERROR: can no longer access replication slot "lsb"

See comment 2 for this case.

Comments

1. Semantics of wal_receiver_status_interval = 0. The patch makes "interval
= 0" mean "hot standby feedback is off". If I read REL_15_STABLE correctly,
the behaviour there was different. In XLogWalRcvSendHSFeedback(), with
interval = 0, TimestampDifferenceExceeds(sendTime, now, 0) is always true,
so feedback with a valid xmin was sent on every non-immediate call. So on
PG14/15 the combination hot_standby_feedback = on with interval = 0 worked
(feedback was sent often). On PG16+ it sends one message and then leaves
the
xmin stale. With this patch it sends no xmin at all. A user upgrading from
15 with this setting would see a third behaviour, which may cause
unexpected
query cancellations on the standby. I think the "off" semantics is a
reasonable choice, and it matches the early-return condition, but I wonder
if restoring the PG15 behaviour (keep sending feedback when interval = 0)
was considered. In any case, it would help to say in the commit message
which behaviour is intended, and why, since this is going to the back
branches.

2. catalog_xmin and logical slots on the standby. As case 5 shows, with the
fix a logical slot on the standby can be invalidated when interval = 0,
even
with hot_standby_feedback = on and a physical slot, which is the setup that
logicaldecoding.sgml recommends. Before the fix, the stale catalog_xmin
kept
the slot usable (as a side effect of the bug). I think this is a
consequence
of comment 1 rather than a new bug, but it is user visible. A few options,
not all needed:
- Mention it in the commit message and the release notes.
- slotsync.c (the check at line 1291) requires hot_standby_feedback for
slot synchronization, but does not check wal_receiver_status_interval.
With this patch, interval = 0 means the primary gets no catalog_xmin
for the synced slots, so the same check could also reject interval =
0,
or at least warn about it.
- A WARNING at walreceiver start, or on reload, when
hot_standby_feedback
= on and wal_receiver_status_interval = 0, saying that no feedback
will
be sent.

3. Documentation. Neither the hot_standby_feedback nor the
wal_receiver_status_interval entry in config.sgml says that interval = 0
disables hot standby feedback. hot_standby_feedback says "Feedback messages
will not be sent more frequently than once per
wal_receiver_status_interval", which a reader could take to mean "no limit"
when the value is 0. A sentence such as "If wal_receiver_status_interval is
zero, hot standby feedback is not sent." would make the new behaviour
clear.

4. Code. The condition "hot_standby_feedback &&
wal_receiver_status_interval

0" now appears in a negated form in the early return at the top of the

function, in WalRcvComputeNextWakeup(), and in the new check. A small
helper
or local bool (for example "feedback_enabled") would keep the three places
consistent. Minor, and a matter of taste.

5. Known gap. As you note in the commit message, an old xmin left on the
slot by a previous connection is not released when Hot Standby is not
active
yet at connect time. Since that leaves the same symptom (vacuum held back
by
a slot that nothing updates), it may be worth a short XXX comment near the
HotStandbyActive() check, so the gap is visible in the code and not only in
the commit message.

6. Test.
- The test covers the reload and connect cases well, and the comments
explain why the walsender is terminated. Nice.
- wal_receiver_timeout = 0 is set without a comment. I assume it avoids
a
timeout when no status messages are sent with interval = 0. A short
comment would help.
- On a build without the fix, the test fails only after the 180 second
wait_for_log timeout. That is the usual pattern, so fine.
- There is no check of catalog_xmin. If the behaviour in comment 2 is
intended, a check that catalog_xmin is also released (or a logical
slot
test) would document it.

Verdict: Waiting on Author, mainly for comments 1 and 2. I think the
semantics of interval = 0, and the impact on logical slots on the standby,
should be settled (and written down in the commit message and docs) before
this goes to the back branches. The code change itself is small and correct
for the chosen semantics, and the test passes and fails as expected.

Thanks again, and I am happy to test the next version, including the
back-branch patches.

Regards,
Joao Rito

Attachments:

v3-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG18.patchapplication/octet-stream; name=v3-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG18.patchDownload+177-7
v3-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG19.patchapplication/octet-stream; name=v3-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG19.patchDownload+177-7
v3-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG17.patchapplication/octet-stream; name=v3-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG17.patchDownload+177-7
v3-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patchapplication/octet-stream; name=v3-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-Master.patchDownload+177-7
v3-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG16.patchapplication/octet-stream; name=v3-walreceiver-Do-not-leave-stale-xmin-on-primary-when-status-interval-is-zero-PG16.patchDownload+177-7