remote_apply commit hangs when wal_receiver_status_interval = 0
Hi hackers,
On master, a commit with synchronous_commit = remote_apply does not
return when the standby has wal_receiver_status_interval = 0. The
standby applies the commit, but it never sends the apply reply to the
primary, so the backend keeps waiting in SyncRep. With default
timeouts it waits about 30 seconds, until the walsender keepalive
forces a reply. With wal_sender_timeout = 0 and wal_receiver_timeout
= 0 it waits forever.
The docs for wal_receiver_status_interval say that updates are sent
while ignoring this parameter "when synchronous_commit is set to
remote_apply", so I think this is a regression.
Steps to reproduce:
initdb -D pg20_primary -U postgres -A trust
cat >> pg20_primary/postgresql.conf <<EOF
port = 5521
wal_sender_timeout = 0
EOF
pg_ctl -D pg20_primary -l primary.log -w st
psql -p 5521 -U postgres -c "CREATE TABLE t (id int, note text);"
pg_basebackup -h 127.0.0.1 -p 5521 -U postgres -D pg20_standby -R -d
"application_name=standby_5522"
cat >> pg20_standby/postgresql.conf <<EOF
port = 5522
wal_receiver_status_interval = 0
wal_receiver_timeout = 0
hot_standby_feedback = off
EOF
pg_ctl -D pg20_standby -l standby.log -w start
psql -p 5521 -U postgres \
-c "ALTER SYSTEM SET synchronous_standby_names = 'standby_5522';"
-c "SELECT pg_reload_conf();"
The standby is streaming and shows as sync:
application_name | state | sync_state
------------------+-----------+------------
standby_5522 | streaming | sync
Now run a remote_apply commit on the primary:
postgres=# SET synchronous_commit = remote_apply;
postgres=# \timing on
postgres=# INSERT INTO t VALUES (1, 'remote_apply');
^CCancel request sent
WARNING: canceling wait for synchronous replication due to user request
DETAIL: The transaction has already committed locally, but might
not have been replicated to the standby.
INSERT 0 1
Time: 184888.783 ms (03:04.889)
While the INSERT was stuck, the backend was waiting in SyncRep:
pid | wait_event_type | wait_event | waiting
---------+-----------------+------------+--
2947177 | IPC | SyncRep | 00:00:51.399588
But the row was already visible on the standby:
id | note
----+--------------
1 | remote_apply
The primary's replay_lsn in pg_stat_replication stays at the value
from connection time. So the standby has applied the commit, but the
primary never learns about it.
*Root cause:*
Commit *400a790a48e* changed the apply notification call in the
walreceiver main loop from
XLogWalRcvSendReply(true, false);
to
XLogWalRcvSendReply(false, false, true);
But XLogWalRcvSendReply() still has this early return at the top:
if (!force && wal_receiver_status_interval <=0)
return;
So with the interval set to 0, the apply reply is dropped before the
new checkApply logic is reached. The same happens for the reply
requested when the startup process has replayed all the WAL
received so far.
Fix:
The attached patch skips the early return when checkApply is set:
- if (!force && wal_receiver_status_interval <= 0)
+ if (!force && !checkApply && wal_receiver_status_interval <= 0)
With interval 0 the reply wakeup is infinity, so the call goes into
the existing duplicate check, and the reply is sent only when the
apply position advances. This keeps the purpose of *400a790a48e*,
which was to avoid sending duplicate replies.
The patch also adds a TAP test,
src/test/recovery/t/058_remote_apply_status_interval.pl. It fails on
master because the commit never completes, and passes with the fix.
Thoughts?
Regards,
Vaibhav Dalvi
EnterpriseDB
Hi,
On Wed, Sep 30, 2026 at 1:52 PM Vaibhav Dalvi
<vaibhav.dalvi@enterprisedb.com> wrote:
Hi hackers,
On master, a commit with synchronous_commit = remote_apply does not
return when the standby has wal_receiver_status_interval = 0. The
standby applies the commit, but it never sends the apply reply to the
primary, so the backend keeps waiting in SyncRep. With default
timeouts it waits about 30 seconds, until the walsender keepalive
forces a reply. With wal_sender_timeout = 0 and wal_receiver_timeout
= 0 it waits forever.The docs for wal_receiver_status_interval say that updates are sent
while ignoring this parameter "when synchronous_commit is set to
remote_apply", so I think this is a regression.
This indeed looks like a regression, the fix looks correct but I have
one small comment to share for the following change:
+my $bg = $primary->background_psql('postgres', on_error_stop => 0);
+my $pid = $bg->query_safe('SELECT pg_backend_pid()');
+$bg->query_safe('SET synchronous_commit = remote_apply');
+$bg->query_until(qr/start/, "\\echo start\nINSERT INTO t VALUES (1);\n");
+
+ok( $primary->poll_query_until(
+ 'postgres',
+ "SELECT state = 'idle' FROM pg_stat_activity WHERE pid = $pid"),
+ 'remote_apply commit completes with wal_receiver_status_interval = 0');
+is($standby->safe_psql('postgres', 'SELECT count(*) FROM t'),
+ '1', 'remote_apply commit is visible on standby');
There is one small race here - it is quite possible that before the
INSERT starts asynchronously, the pg_stat_activity says the backend is
in the idle state, making the test case false positive.
So the right thing would be to maybe pause the replay on the standby,
start the INSERT, confirm that the backend enters the SyncRep wait,
resume replay, and then verify that the backend becomes idle.
$standby->safe_psql('postgres', 'SELECT pg_wal_replay_pause()');
$bg->query_until(qr/start/, "\\echo start\nINSERT INTO t VALUES (1);\n");
ok($primary->poll_query_until(
'postgres',
"SELECT wait_event = 'SyncRep'
FROM pg_stat_activity WHERE pid = $pid"),
'remote_apply commit waits for standby replay');
$standby->safe_psql('postgres', 'SELECT pg_wal_replay_resume()');
ok($primary->poll_query_until(
'postgres',
"SELECT state = 'idle'
FROM pg_stat_activity WHERE pid = $pid"),
'remote_apply commit completes with wal_receiver_status_interval = 0');
--
With Regards,
Ashutosh Sharma.
Hi,
Thanks for the review. You're right, the test had a race.
If the INSERT hadn't started yet, the backend could show up as idle
and the check would pass without the commit ever waiting.
I've changed the test as you suggested. It now does the following:
1. Pauses replay on the standby with pg_wal_replay_pause().
2. Starts the remote_apply INSERT in the background session.
3. Waits until the backend shows wait_event = 'SyncRep'. This is a new
assertion.
4. Resumes replay with pg_wal_replay_resume().
5. Waits until the backend is idle, which is the original check.
The walreceiver.c change is the same as in v1. Only the test changed.
The v2 patch is attached.
Regards,
Vaibhav
On Wed, Sep 30, 2026 at 5:38 PM Ashutosh Sharma <ashu.coek88@gmail.com>
wrote:
Show quoted text
Hi,
On Wed, Sep 30, 2026 at 1:52 PM Vaibhav Dalvi
<vaibhav.dalvi@enterprisedb.com> wrote:Hi hackers,
On master, a commit with synchronous_commit = remote_apply does not
return when the standby has wal_receiver_status_interval = 0. The
standby applies the commit, but it never sends the apply reply to the
primary, so the backend keeps waiting in SyncRep. With default
timeouts it waits about 30 seconds, until the walsender keepalive
forces a reply. With wal_sender_timeout = 0 and wal_receiver_timeout
= 0 it waits forever.The docs for wal_receiver_status_interval say that updates are sent
while ignoring this parameter "when synchronous_commit is set to
remote_apply", so I think this is a regression.This indeed looks like a regression, the fix looks correct but I have
one small comment to share for the following change:+my $bg = $primary->background_psql('postgres', on_error_stop => 0); +my $pid = $bg->query_safe('SELECT pg_backend_pid()'); +$bg->query_safe('SET synchronous_commit = remote_apply'); +$bg->query_until(qr/start/, "\\echo start\nINSERT INTO t VALUES (1);\n"); + +ok( $primary->poll_query_until( + 'postgres', + "SELECT state = 'idle' FROM pg_stat_activity WHERE pid = $pid"), + 'remote_apply commit completes with wal_receiver_status_interval = 0'); +is($standby->safe_psql('postgres', 'SELECT count(*) FROM t'), + '1', 'remote_apply commit is visible on standby');There is one small race here - it is quite possible that before the
INSERT starts asynchronously, the pg_stat_activity says the backend is
in the idle state, making the test case false positive.So the right thing would be to maybe pause the replay on the standby,
start the INSERT, confirm that the backend enters the SyncRep wait,
resume replay, and then verify that the backend becomes idle.$standby->safe_psql('postgres', 'SELECT pg_wal_replay_pause()');
$bg->query_until(qr/start/, "\\echo start\nINSERT INTO t VALUES (1);\n");
ok($primary->poll_query_until(
'postgres',
"SELECT wait_event = 'SyncRep'
FROM pg_stat_activity WHERE pid = $pid"),
'remote_apply commit waits for standby replay');$standby->safe_psql('postgres', 'SELECT pg_wal_replay_resume()');
ok($primary->poll_query_until(
'postgres',
"SELECT state = 'idle'
FROM pg_stat_activity WHERE pid = $pid"),
'remote_apply commit completes with wal_receiver_status_interval = 0');--
With Regards,
Ashutosh Sharma.
On Tue, Oct 6, 2026 at 9:31 PM Vaibhav Dalvi
<vaibhav.dalvi@enterprisedb.com> wrote:
The walreceiver.c change is the same as in v1. Only the test changed.
The v2 patch is attached.
Thanks for the patch! The walreceiver change looks correct to me. I have a
couple of comments on the test.
+$primary->safe_psql('postgres',
+ "ALTER SYSTEM SET synchronous_standby_names = '*'");
+$primary->reload;
+$primary->poll_query_until(
+ 'postgres',
+ "SELECT count(*) = 1 FROM pg_stat_replication
+ WHERE state = 'streaming' AND sync_state = 'sync'
+ AND flush_lsn IS NOT NULL"
There seems to be another race in the test. After the configuration reload,
the checkpointer needs to read the new synchronous_standby_names setting
and update the shared SYNC_STANDBY_DEFINED flag before backends can wait
for sync replication. But, walsender could read the new setting first,
causing sync_state to show sync before the checkpointer updates that flag.
If the INSERT commits during this window, it could skip the SyncRep wait
and cause the test to fail.
How about configuring sync replication at startup instead?
$primary->append_conf(
'postgresql.conf', qq(
wal_sender_timeout = 0
synchronous_standby_names = '*'
synchronous_commit = local
));
Using local allows setup to proceed before the standby is available. Only
the test session needs to switch to remote_apply:
$bg->query_safe('SET synchronous_commit = remote_apply');
Also, do we really need to pause replay? Isn't it sufficient to wait
directly for the INSERT to complete using query_safe() in a background psql
session with a timeout, and then check that the row is visible on the standby?
Without the fix, the INSERT would remain blocked and the test would fail on
timeout. With the fix, it would complete and the test would pass. This would
simplify the test.
Regards,
--
Fujii Masao
Hi Fujii-san,
Thanks for the review.
Attached v3. Only the test changed.
How about configuring sync replication at startup instead?
Done. synchronous_standby_names = '*' and synchronous_commit = local
are now set in the primary's initial configuration, so there is no reload
and
no window where the standby appears synchronous before the backends are
aware of it. Only the test session uses remote_apply.
Also, do we really need to pause replay?
No, it is not needed. Thank you for pointing that out. The test now runs the
INSERT with query_safe() and then checks that the row is visible on the
standby.
Without the fix the INSERT remains blocked, and the test fails on timeout.
I ran it a few times with the fix and it passes. With the walreceiver.c
change
reverted, it fails on a timeout.
Thanks again for your time and for the review.
Regards,
Vaibhav
On Tue, Oct 6, 2026 at 9:11 PM Fujii Masao <masao.fujii@gmail.com> wrote:
Show quoted text
On Tue, Oct 6, 2026 at 9:31 PM Vaibhav Dalvi
<vaibhav.dalvi@enterprisedb.com> wrote:The walreceiver.c change is the same as in v1. Only the test changed.
The v2 patch is attached.Thanks for the patch! The walreceiver change looks correct to me. I have a
couple of comments on the test.+$primary->safe_psql('postgres', + "ALTER SYSTEM SET synchronous_standby_names = '*'"); +$primary->reload; +$primary->poll_query_until( + 'postgres', + "SELECT count(*) = 1 FROM pg_stat_replication + WHERE state = 'streaming' AND sync_state = 'sync' + AND flush_lsn IS NOT NULL"There seems to be another race in the test. After the configuration reload,
the checkpointer needs to read the new synchronous_standby_names setting
and update the shared SYNC_STANDBY_DEFINED flag before backends can wait
for sync replication. But, walsender could read the new setting first,
causing sync_state to show sync before the checkpointer updates that flag.
If the INSERT commits during this window, it could skip the SyncRep wait
and cause the test to fail.How about configuring sync replication at startup instead?
$primary->append_conf(
'postgresql.conf', qq(
wal_sender_timeout = 0
synchronous_standby_names = '*'
synchronous_commit = local
));Using local allows setup to proceed before the standby is available. Only
the test session needs to switch to remote_apply:$bg->query_safe('SET synchronous_commit = remote_apply');
Also, do we really need to pause replay? Isn't it sufficient to wait
directly for the INSERT to complete using query_safe() in a background psql
session with a timeout, and then check that the row is visible on the
standby?
Without the fix, the INSERT would remain blocked and the test would fail on
timeout. With the fix, it would complete and the test would pass. This
would
simplify the test.Regards,
--
Fujii Masao
On Wed, Oct 7, 2026 at 1:45 PM Vaibhav Dalvi
<vaibhav.dalvi@enterprisedb.com> wrote:
Attached v3. Only the test changed.
Thanks for updating the patch!
't/057_snapshot_commit_race.pl',
+ 't/058_remote_apply_status_interval.pl',
Since 058_shutdown_crash_restart.pl was recently added to master, I
renumbered the test as 059_remote_apply_status_interval.pl. I also made
some cosmetic changes and then pushed the patch. Thanks!
Regards,
--
Fujii Masao