Skip to content

Commit dcd1131

Browse files
committed
Send keepalives from walsender even when busy sending WAL.
If walsender doesn't hear from the client for the time specified by wal_sender_timeout, it will conclude the connection or client is dead, and disconnect. When half of wal_sender_timeout has elapsed, it sends a ping to the client, leaving it the remainig half of wal_sender_timeout to respond. However, it only checked if half of wal_sender_timeout had elapsed when it was about to sleep, so if it was busy sending WAL to the client for long enough, it would not send the ping request in time. Then the client would not know it needs to send a reply, and the walsender will disconnect even though the client is still alive. Fix that. Andres Freund, reviewed by Robert Haas, and some further changes by me. Backpatch to 9.3. Earlier versions relied on the client to send the keepalives on its own, and hence didn't have this problem.
1 parent 3973034 commit dcd1131

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

src/backend/replication/walsender.c

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,27 @@ WalSndLoop(void)
10671067
}
10681068
}
10691069

1070+
/*
1071+
* If half of wal_sender_timeout has lapsed without receiving any
1072+
* reply from standby, send a keep-alive message requesting an
1073+
* immediate reply.
1074+
*/
1075+
if (wal_sender_timeout > 0 && !ping_sent)
1076+
{
1077+
TimestampTz timeout;
1078+
1079+
timeout = TimestampTzPlusMilliseconds(last_reply_timestamp,
1080+
wal_sender_timeout / 2);
1081+
if (GetCurrentTimestamp() >= timeout)
1082+
{
1083+
WalSndKeepalive(true);
1084+
ping_sent = true;
1085+
/* Try to flush pending output to the client */
1086+
if (pq_flush_if_writable() != 0)
1087+
goto send_failure;
1088+
}
1089+
}
1090+
10701091
/*
10711092
* We don't block if not caught up, unless there is unsent data
10721093
* pending in which case we'd better block until the socket is
@@ -1076,7 +1097,7 @@ WalSndLoop(void)
10761097
*/
10771098
if ((caughtup && !streamingDoneSending) || pq_is_send_pending())
10781099
{
1079-
TimestampTz timeout = 0;
1100+
TimestampTz timeout;
10801101
long sleeptime = 10000; /* 10 s */
10811102
int wakeEvents;
10821103

@@ -1085,32 +1106,14 @@ WalSndLoop(void)
10851106

10861107
if (pq_is_send_pending())
10871108
wakeEvents |= WL_SOCKET_WRITEABLE;
1088-
else if (wal_sender_timeout > 0 && !ping_sent)
1089-
{
1090-
/*
1091-
* If half of wal_sender_timeout has lapsed without receiving
1092-
* any reply from standby, send a keep-alive message to
1093-
* standby requesting an immediate reply.
1094-
*/
1095-
timeout = TimestampTzPlusMilliseconds(last_reply_timestamp,
1096-
wal_sender_timeout / 2);
1097-
if (GetCurrentTimestamp() >= timeout)
1098-
{
1099-
WalSndKeepalive(true);
1100-
ping_sent = true;
1101-
/* Try to flush pending output to the client */
1102-
if (pq_flush_if_writable() != 0)
1103-
goto send_failure;
1104-
}
1105-
}
11061109

1107-
/* Determine time until replication timeout */
1110+
/*
1111+
* If wal_sender_timeout is active, sleep in smaller increments
1112+
* to not go over the timeout too much. XXX: Why not just sleep
1113+
* until the timeout has elapsed?
1114+
*/
11081115
if (wal_sender_timeout > 0)
1109-
{
1110-
timeout = TimestampTzPlusMilliseconds(last_reply_timestamp,
1111-
wal_sender_timeout);
11121116
sleeptime = 1 + (wal_sender_timeout / 10);
1113-
}
11141117

11151118
/* Sleep until something happens or we time out */
11161119
ImmediateInterruptOK = true;
@@ -1124,6 +1127,8 @@ WalSndLoop(void)
11241127
* possibility that the client replied just as we reached the
11251128
* timeout ... he's supposed to reply *before* that.
11261129
*/
1130+
timeout = TimestampTzPlusMilliseconds(last_reply_timestamp,
1131+
wal_sender_timeout);
11271132
if (wal_sender_timeout > 0 && GetCurrentTimestamp() >= timeout)
11281133
{
11291134
/*

0 commit comments

Comments
 (0)