@@ -1428,10 +1428,8 @@ WalSndWaitForWal(XLogRecPtr loc)
14281428 /*
14291429 * We only send regular messages to the client for full decoded
14301430 * transactions, but a synchronous replication and walsender shutdown
1431- * possibly are waiting for a later location. So, before sleeping, we
1432- * send a ping containing the flush location. If the receiver is
1433- * otherwise idle, this keepalive will trigger a reply. Processing the
1434- * reply will update these MyWalSnd locations.
1431+ * possibly are waiting for a later location. So we send pings
1432+ * containing the flush location every now and then.
14351433 */
14361434 if (MyWalSnd -> flush < sentPtr &&
14371435 MyWalSnd -> write < sentPtr &&
@@ -2316,23 +2314,30 @@ WalSndLoop(WalSndSendDataCallback send_data)
23162314 WalSndKeepaliveIfNecessary ();
23172315
23182316 /*
2319- * Block if we have unsent data. Let WalSndWaitForWal() handle any
2320- * other blocking; idle receivers need its additional actions.
2317+ * We don't block if not caught up, unless there is unsent data
2318+ * pending in which case we'd better block until the socket is
2319+ * write-ready. This test is only needed for the case where the
2320+ * send_data callback handled a subset of the available data but then
2321+ * pq_flush_if_writable flushed it all --- we should immediately try
2322+ * to send more.
23212323 */
2322- if (pq_is_send_pending ())
2324+ if (( WalSndCaughtUp && ! streamingDoneSending ) || pq_is_send_pending ())
23232325 {
23242326 long sleeptime ;
23252327 int wakeEvents ;
23262328
23272329 wakeEvents = WL_LATCH_SET | WL_EXIT_ON_PM_DEATH | WL_TIMEOUT |
2328- WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE ;
2330+ WL_SOCKET_READABLE ;
23292331
23302332 /*
23312333 * Use fresh timestamp, not last_processing, to reduce the chance
23322334 * of reaching wal_sender_timeout before sending a keepalive.
23332335 */
23342336 sleeptime = WalSndComputeSleeptime (GetCurrentTimestamp ());
23352337
2338+ if (pq_is_send_pending ())
2339+ wakeEvents |= WL_SOCKET_WRITEABLE ;
2340+
23362341 /* Sleep until something happens or we time out */
23372342 (void ) WaitLatchOrSocket (MyLatch , wakeEvents ,
23382343 MyProcPort -> sock , sleeptime ,
0 commit comments