diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c index 8f360ec3e4..48a34cb554 100644 --- a/src/bin/pg_basebackup/receivelog.c +++ b/src/bin/pg_basebackup/receivelog.c @@ -918,6 +918,25 @@ HandleCopyStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline, /* If the server requested an immediate reply, send one. */ if (replyRequested && still_sending) { + if (reportFlushPosition && lastFlushPosition < blockpos && + walfile != 1) + { + /* + * If a valid flush location needs to be reported, + * flush the current WAL file so that the latest flush + * location is sent back to the server. This is necessary to + * see whether the last WAL data has been successfully + * replicated or not, at the normal shutdown of the server. + */ + if (fsync(walfile) != 0) + { + fprintf(stderr, _("%s: could not fsync file \"%s\": %s\n"), + progname, current_walfile_name, strerror(errno)); + goto error; + } + lastFlushPosition = blockpos; + } + now = feGetCurrentTimestamp(); if (!sendFeedback(conn, blockpos, now, false)) goto error;