*** a/src/bin/pg_basebackup/receivelog.c --- b/src/bin/pg_basebackup/receivelog.c *************** *** 256,261 **** localTimestampDifferenceExceeds(TimestampTz start_time, --- 256,290 ---- #endif } + /* Send reply to Sendert task. + * replyRequested is used to decide whether any immediate reply is expected + * from sender + */ + static bool sendReplyToSender(PGconn *conn, int64 now, bool replyRequested, XLogRecPtr blockpos) + { + /* Time to send feedback! */ + char replybuf[sizeof(StandbyReplyMessage) + 1]; + StandbyReplyMessage *replymsg; + + replymsg = (StandbyReplyMessage *) (replybuf + 1); + replymsg->write = blockpos; + replymsg->flush = InvalidXLogRecPtr; + replymsg->apply = InvalidXLogRecPtr; + replymsg->sendTime = now; + replymsg->replyRequested = replyRequested; + replybuf[0] = 'r'; + + if (PQputCopyData(conn, replybuf, sizeof(replybuf)) <= 0 || + PQflush(conn)) + { + fprintf(stderr, _("%s: could not send feedback packet: %s"), + progname, PQerrorMessage(conn)); + return false; + } + + return true; + } + /* * Receive a log stream starting at the specified position. * *************** *** 291,296 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline, --- 320,326 ---- char *copybuf = NULL; int64 last_status = -1; XLogRecPtr blockpos = InvalidXLogRecPtr; + PrimaryKeepaliveMessage keepalive; if (sysidentifier != NULL) { *************** *** 381,402 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline, localTimestampDifferenceExceeds(last_status, now, standby_message_timeout)) { ! /* Time to send feedback! */ ! char replybuf[sizeof(StandbyReplyMessage) + 1]; ! StandbyReplyMessage *replymsg; ! ! replymsg = (StandbyReplyMessage *) (replybuf + 1); ! replymsg->write = blockpos; ! replymsg->flush = InvalidXLogRecPtr; ! replymsg->apply = InvalidXLogRecPtr; ! replymsg->sendTime = now; ! replybuf[0] = 'r'; ! ! if (PQputCopyData(conn, replybuf, sizeof(replybuf)) <= 0 || ! PQflush(conn)) { - fprintf(stderr, _("%s: could not send feedback packet: %s"), - progname, PQerrorMessage(conn)); goto error; } --- 411,419 ---- localTimestampDifferenceExceeds(last_status, now, standby_message_timeout)) { ! /* Sending false here, because no reply is expected from server */ ! if (sendReplyToSender(conn, now, false, blockpos) == false) { goto error; } *************** *** 487,492 **** ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline, --- 504,527 ---- progname, r); goto error; } + + /* copy the received buffer to keepalive */ + memcpy(&keepalive, copybuf, sizeof(PrimaryKeepaliveMessage)); + + /* If as part of keepalive message from sender, an immediate reply is requested + * then send the same to sender. + */ + if (keepalive.replyRequested) + { + now = localGetCurrentTimestamp(); + if (sendReplyToSender(conn, now, false, blockpos) == false) + { + goto error; + } + + last_status = now; + } + continue; } else if (copybuf[0] != 'w')