From ed60223555ae6b30c203276f1cf8890711e0c267 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Tue, 17 Feb 2015 19:00:07 +0900
Subject: [PATCH] Make walreceiver to keep regular reply message even on heavy
 load v2.

Wal receiver cannot send receiver reply message while it is receiving
continuous WAL stream caused by heavy load or something else. This
patch makes wal receiver to send reply message even on such a
situation.
---
 src/backend/replication/walreceiver.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index 753316e..407e70b 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -319,13 +319,22 @@ WalReceiverMain(void)
 		/* Wait a while for data to arrive */
 		if (walrcv_receive(NAPTIME_PER_CYCLE, &type, &buf, &len))
 		{
-			/* Accept the received data, and process it */
-			XLogWalRcvProcessMsg(type, buf, len);
-
 			/* Receive any more data we can without sleeping */
-			while (walrcv_receive(0, &type, &buf, &len))
+			do
+			{
+				/* Accept the received data, and process it */
 				XLogWalRcvProcessMsg(type, buf, len);
 
+				/*
+				 * walrecv->lastMsgReceiptTime has the time as of
+				 * XLogWalRcvProcessMsg just above.
+				 */
+				if (TimestampDifferenceExceeds(reply_message.sendTime,
+											   walrcv->lastMsgReceiptTime,
+											   wal_receiver_status_interval * 1000))
+					XLogWalRcvSendReply();
+			} while (walrcv_receive(0, &type, &buf, &len));
+
 			/* Let the master know that we received some data. */
 			XLogWalRcvSendReply();
 
-- 
2.1.0.GIT

