Skip to content

Commit 4665352

Browse files
committed
Fix issue with WAL archiving in standby.
Previously, walreceiver always closed the currently-opened WAL segment and created its archive notification file, after it finished writing the current segment up and received any WAL data that should be written into the next segment. If walreceiver exited just before any WAL data in the next segment arrived at standby, it did not create the archive notification file of the current segment even though that's known completed. This behavior could cause WAL archiving of the segment to be delayed until subsequent restartpoints or checkpoints created its notification file. To fix the issue, this commit changes walreceiver so that it creates an archive notification file of a current WAL segment immediately if that's known completed before receiving next WAL data. Back-patch to all supported branches. Reported-by: Kyotaro Horiguchi Author: Fujii Masao Reviewed-by: Kyotaro Horiguchi Discussion: https://postgr.es/m/20200630.165503.1465894182551545886.horikyota.ntt@gmail.com
1 parent df290e5 commit 4665352

File tree

1 file changed

+62
-36
lines changed

1 file changed

+62
-36
lines changed

src/backend/replication/walreceiver.c

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ static void WalRcvDie(int code, Datum arg);
119119
static void XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len);
120120
static void XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr);
121121
static void XLogWalRcvFlush(bool dying);
122+
static void XLogWalRcvClose(XLogRecPtr recptr);
122123
static void XLogWalRcvSendReply(bool force, bool requestReply);
123124
static void XLogWalRcvSendHSFeedback(bool immed);
124125
static void ProcessWalSndrMessage(XLogRecPtr walEnd, TimestampTz sendTime);
@@ -906,46 +907,16 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr)
906907
{
907908
int segbytes;
908909

909-
if (recvFile < 0 || !XLByteInSeg(recptr, recvSegNo, wal_segment_size))
910-
{
911-
bool use_existent;
912-
913-
/*
914-
* fsync() and close current file before we switch to next one. We
915-
* would otherwise have to reopen this file to fsync it later
916-
*/
917-
if (recvFile >= 0)
918-
{
919-
char xlogfname[MAXFNAMELEN];
910+
/* Close the current segment if it's completed */
911+
if (recvFile >= 0 && !XLByteInSeg(recptr, recvSegNo, wal_segment_size))
912+
XLogWalRcvClose(recptr);
920913

921-
XLogWalRcvFlush(false);
922-
923-
/*
924-
* XLOG segment files will be re-read by recovery in startup
925-
* process soon, so we don't advise the OS to release cache
926-
* pages associated with the file like XLogFileClose() does.
927-
*/
928-
if (close(recvFile) != 0)
929-
ereport(PANIC,
930-
(errcode_for_file_access(),
931-
errmsg("could not close log segment %s: %m",
932-
XLogFileNameP(recvFileTLI, recvSegNo))));
933-
934-
/*
935-
* Create .done file forcibly to prevent the streamed segment
936-
* from being archived later.
937-
*/
938-
XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size);
939-
if (XLogArchiveMode != ARCHIVE_MODE_ALWAYS)
940-
XLogArchiveForceDone(xlogfname);
941-
else
942-
XLogArchiveNotify(xlogfname);
943-
}
944-
recvFile = -1;
914+
if (recvFile < 0)
915+
{
916+
bool use_existent = true;
945917

946918
/* Create/use new log file */
947919
XLByteToSeg(recptr, recvSegNo, wal_segment_size);
948-
use_existent = true;
949920
recvFile = XLogFileInit(recvSegNo, &use_existent, true);
950921
recvFileTLI = ThisTimeLineID;
951922
recvOff = 0;
@@ -997,6 +968,15 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr)
997968

998969
LogstreamResult.Write = recptr;
999970
}
971+
972+
/*
973+
* Close the current segment if it's fully written up in the last cycle of
974+
* the loop, to create its archive notification file soon. Otherwise WAL
975+
* archiving of the segment will be delayed until any data in the next
976+
* segment is received and written.
977+
*/
978+
if (recvFile >= 0 && !XLByteInSeg(recptr, recvSegNo, wal_segment_size))
979+
XLogWalRcvClose(recptr);
1000980
}
1001981

1002982
/*
@@ -1051,6 +1031,52 @@ XLogWalRcvFlush(bool dying)
10511031
}
10521032
}
10531033

1034+
/*
1035+
* Close the current segment.
1036+
*
1037+
* Flush the segment to disk before closing it. Otherwise we have to
1038+
* reopen and fsync it later.
1039+
*
1040+
* Create an archive notification file since the segment is known completed.
1041+
*/
1042+
static void
1043+
XLogWalRcvClose(XLogRecPtr recptr)
1044+
{
1045+
char xlogfname[MAXFNAMELEN];
1046+
1047+
Assert(recvFile >= 0 && !XLByteInSeg(recptr, recvSegNo, wal_segment_size));
1048+
1049+
/*
1050+
* fsync() and close current file before we switch to next one. We would
1051+
* otherwise have to reopen this file to fsync it later
1052+
*/
1053+
XLogWalRcvFlush(false);
1054+
1055+
XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size);
1056+
1057+
/*
1058+
* XLOG segment files will be re-read by recovery in startup process soon,
1059+
* so we don't advise the OS to release cache pages associated with the
1060+
* file like XLogFileClose() does.
1061+
*/
1062+
if (close(recvFile) != 0)
1063+
ereport(PANIC,
1064+
(errcode_for_file_access(),
1065+
errmsg("could not close log segment %s: %m",
1066+
xlogfname)));
1067+
1068+
/*
1069+
* Create .done file forcibly to prevent the streamed segment from being
1070+
* archived later.
1071+
*/
1072+
if (XLogArchiveMode != ARCHIVE_MODE_ALWAYS)
1073+
XLogArchiveForceDone(xlogfname);
1074+
else
1075+
XLogArchiveNotify(xlogfname);
1076+
1077+
recvFile = -1;
1078+
}
1079+
10541080
/*
10551081
* Send reply message to primary, indicating our current WAL locations, oldest
10561082
* xmin and the current time.

0 commit comments

Comments
 (0)