postgres.git / summary / log / commit / refs

commit    2ac618d717a34b984ef34ccb3f9764daa34d086b
Author:   Fujii Masao <fujii@postgresql.org>
Date:     Tue Nov 03 01:48:12 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 e4f1459d35e..37b45d8d286 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -3479,7 +3479,47 @@ WalSndWait(uint32 socket_events, long timeout, uint32 wait_event) * walsenders for selective wake ups, see WalSndWakeup() for more details. */ 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: f1d27a92252b]