postgres.git / summary / log / commit / refs
commit a9b32f4c818b2ea2857f80337e660802def51c63
Author: Fujii Masao <fujii@postgresql.org>
Date: Tue Nov 03 01:46:41 2026 +0000
First released in: 16.16, 17.12, 18.7, 19.0
Fix lost wakeups in physical WAL senders
Physical WAL senders could sleep even when flushed WAL was waiting to
be sent, delaying replication and synchronous commits until another
event woke them. In the reported case, commits stalled for about ten
seconds, until the standby sent its next periodic status report.
Commit bc971f4025c changed WAL sender wakeups to use condition
variables, which notify only senders already on the waitlist.
However, XLogSendPhysical() checks the flush position before
WalSndWait() joins the waitlist. If WAL is flushed between these two
steps, the wakeup can be missed, leaving the sender asleep despite
having more WAL to send.
Fix this by rechecking the flush position after joining the waitlist
and returning to the send loop if more WAL is available. For cascading
senders, also check for promotion or a timeline change, which require
the sender to revisit the current timeline even if the flush position
has not advanced.
Backpatch to v16, where commit bc971f4025c introduced the
condition-variable wakeup mechanism.
Author: Alex Speller <alex@alexspeller.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/mvx5bs7q.33b99c32-67d8-4373-8611-7a8250fb2fd3@we.are.superhuman.com
Backpatch-through: 16
src/backend/replication/walsender.c | 40 +++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index 9c9c6587cc9..8b878ec6e66 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -4124,7 +4124,47 @@ WalSndWait(uint32 socket_events, long timeout, uint32 wait_event)
if (wait_event == WAIT_EVENT_WAIT_FOR_STANDBY_CONFIRMATION)
ConditionVariablePrepareToSleep(&WalSndCtl->wal_confirm_rcv_cv);
else if (MyWalSnd->kind == REPLICATION_KIND_PHYSICAL)
+ {
ConditionVariablePrepareToSleep(&WalSndCtl->wal_flush_cv);
+
+ /*
+ * Recheck the conditions tested by XLogSendPhysical() now that we're
+ * on the waitlist. Otherwise, we could miss a wakeup for a WAL flush,
+ * promotion, or timeline switch that occurred before we joined it.
+ */
+ if (wait_event == WAIT_EVENT_WAL_SENDER_MAIN &&
+ !sendTimeLineIsHistoric && !pq_is_send_pending())
+ {
+ XLogRecPtr flushPtr;
+ bool becameHistoric = false;
+
+ Assert(WalSndCaughtUp);
+ Assert(!streamingDoneSending);
+
+ if (am_cascading_walsender)
+ {
+ TimeLineID flushTLI;
+
+ flushPtr = GetStandbyFlushRecPtr(&flushTLI);
+
+ /*
+ * Promotion or a timeline switch need not advance the flush
+ * pointer. Let XLogSendPhysical() handle the now-historic
+ * timeline.
+ */
+ becameHistoric = !RecoveryInProgress() ||
+ sendTimeLine != flushTLI;
+ }
+ else
+ flushPtr = GetFlushRecPtr(NULL);
+
+ if (flushPtr > sentPtr || becameHistoric)
+ {
+ ConditionVariableCancelSleep();
+ return;
+ }
+ }
+ }
else if (MyWalSnd->kind == REPLICATION_KIND_LOGICAL)
ConditionVariablePrepareToSleep(&WalSndCtl->wal_replay_cv);
[parent: 0fd0e501a08f]