*** a/src/backend/replication/walsender.c
--- b/src/backend/replication/walsender.c
***************
*** 142,147 **** static XLogRecPtr sendTimeLineValidUpto = InvalidXLogRecPtr;
--- 142,155 ----
   */
  static XLogRecPtr sentPtr = 0;
  
+ /*
+  * How far have WAL been replicated to a client already? This basically
+  * indicates a flush location which a client returns, but a write location
+  * if an invalid flush location is always returned from a client such as
+  * pg_basebackup background process.
+  */
+ static XLogRecPtr replicatedPtr = 0;
+ 
  /* Buffers for constructing outgoing messages and processing reply messages. */
  static StringInfoData output_message;
  static StringInfoData reply_message;
***************
*** 1185,1193 **** WalSndWaitForWal(XLogRecPtr loc)
  		 * We only send regular messages to the client for full decoded
  		 * transactions, but a synchronous replication and walsender shutdown
  		 * possibly are waiting for a later location. So we send pings
! 		 * containing the flush location every now and then.
  		 */
! 		if (MyWalSnd->flush < sentPtr && !waiting_for_ping_response)
  		{
  			WalSndKeepalive(true);
  			waiting_for_ping_response = true;
--- 1193,1201 ----
  		 * We only send regular messages to the client for full decoded
  		 * transactions, but a synchronous replication and walsender shutdown
  		 * possibly are waiting for a later location. So we send pings
! 		 * requesting the flush location every now and then.
  		 */
! 		if (replicatedPtr < sentPtr && !waiting_for_ping_response)
  		{
  			WalSndKeepalive(true);
  			waiting_for_ping_response = true;
***************
*** 1541,1546 **** ProcessStandbyReplyMessage(void)
--- 1549,1555 ----
  		walsnd->apply = applyPtr;
  		SpinLockRelease(&walsnd->mutex);
  	}
+ 	replicatedPtr = XLogRecPtrIsInvalid(flushPtr) ? writePtr : flushPtr;
  
  	if (!am_cascading_walsender)
  		SyncRepReleaseWaiters();
***************
*** 2446,2464 **** XLogSendLogical(void)
  static void
  WalSndDone(WalSndSendDataCallback send_data)
  {
- 	XLogRecPtr	replicatedPtr;
- 
  	/* ... let's just be real sure we're caught up ... */
  	send_data();
  
- 	/*
- 	 * Check a write location to see whether all the WAL have
- 	 * successfully been replicated if this walsender is connecting
- 	 * to a standby such as pg_receivexlog which always returns
- 	 * an invalid flush location. Otherwise, check a flush location.
- 	 */
- 	replicatedPtr = XLogRecPtrIsInvalid(MyWalSnd->flush) ?
- 		MyWalSnd->write : MyWalSnd->flush;
  	if (WalSndCaughtUp && sentPtr == replicatedPtr &&
  		!pq_is_send_pending())
  	{
--- 2455,2463 ----
