On 10/23/2014 01:25 PM, Michael Paquier wrote:
On Thu, Oct 23, 2014 at 10:09 AM, Heikki Linnakangas <
hlinnakan...@vmware.com> wrote:

On 10/23/2014 08:59 AM, Fujii Masao wrote:
Sounds reasonable, for back-branches. Although I'm still worried we might
miss some corner-case unless we go with a more wholesale solution.


Don't really want to be the intruder here, but isn't that the simple patch
attached?

That's not right. Should check *after* the write if the segment was completed, and close it if so. Like the attached.

There is still a small window between XLogWalRcvFlush and
XLogArchiveForceDone in XLogWalRcvWrite if the standby crashes exactly
between them.

Yeah. I think we can live with that.

- Heikki
diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index c2d4ed3..ca24acc 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -138,6 +138,7 @@ static void WalRcvDie(int code, Datum arg);
 static void XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len);
 static void XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr);
 static void XLogWalRcvFlush(bool dying);
+static void XLogWalRcvFileClose(void);
 static void XLogWalRcvSendReply(bool force, bool requestReply);
 static void XLogWalRcvSendHSFeedback(bool immed);
 static void ProcessWalSndrMessage(XLogRecPtr walEnd, TimestampTz sendTime);
@@ -887,30 +888,7 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr)
 			 * would otherwise have to reopen this file to fsync it later
 			 */
 			if (recvFile >= 0)
-			{
-				char		xlogfname[MAXFNAMELEN];
-
-				XLogWalRcvFlush(false);
-
-				/*
-				 * XLOG segment files will be re-read by recovery in startup
-				 * process soon, so we don't advise the OS to release cache
-				 * pages associated with the file like XLogFileClose() does.
-				 */
-				if (close(recvFile) != 0)
-					ereport(PANIC,
-							(errcode_for_file_access(),
-							 errmsg("could not close log segment %s: %m",
-									XLogFileNameP(recvFileTLI, recvSegNo))));
-
-				/*
-				 * Create .done file forcibly to prevent the streamed segment
-				 * from being archived later.
-				 */
-				XLogFileName(xlogfname, recvFileTLI, recvSegNo);
-				XLogArchiveForceDone(xlogfname);
-			}
-			recvFile = -1;
+				XLogWalRcvFileClose();
 
 			/* Create/use new log file */
 			XLByteToSeg(recptr, recvSegNo);
@@ -966,6 +944,10 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr)
 
 		LogstreamResult.Write = recptr;
 	}
+
+	/* If we just finished writing to the current file, fsync and close it */
+	if (recvFile >= 0 && recvOff == XLOG_SEG_SIZE)
+		XLogWalRcvFileClose();
 }
 
 /*
@@ -1022,6 +1004,38 @@ XLogWalRcvFlush(bool dying)
 }
 
 /*
+ * Close the currently open WAL file.
+ */
+static void
+XLogWalRcvFileClose(void)
+{
+	char		xlogfname[MAXFNAMELEN];
+
+	Assert(recvFile >= 0);
+
+	XLogWalRcvFlush(false);
+
+	/*
+	 * XLOG segment files will be re-read by recovery in startup process soon,
+	 * so we don't advise the OS to release cache pages associated with the
+	 * file like XLogFileClose() does.
+	 */
+	if (close(recvFile) != 0)
+		ereport(PANIC,
+				(errcode_for_file_access(),
+				 errmsg("could not close log segment %s: %m",
+						XLogFileNameP(recvFileTLI, recvSegNo))));
+	recvFile = -1;
+
+	/*
+	 * Create .done file forcibly to prevent the streamed segment from being
+	 * archived later.
+	 */
+	XLogFileName(xlogfname, recvFileTLI, recvSegNo);
+	XLogArchiveForceDone(xlogfname);
+}
+
+/*
  * Send reply message to primary, indicating our current XLOG positions, oldest
  * xmin and the current time.
  *
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to