From 6dc7760ac3c3aeb766f489121f39d26d6f9b8c46 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Fri, 20 May 2005 14:53:26 +0000
Subject: [PATCH] Add support for wal_fsync_writethrough for Darwin, and
 restructure the code to better handle writethrough.

Chris Campbell
---
 doc/src/sgml/runtime.sgml         |  4 +--
 src/backend/access/transam/xlog.c | 44 +++++++++++++++++--------------
 src/backend/storage/file/fd.c     | 38 ++++++++++++++++++++++++--
 src/include/access/xlog.h         |  9 ++++++-
 src/include/port/darwin.h         |  3 +++
 src/include/port/win32.h          |  6 ++---
 src/include/storage/fd.h          |  4 ++-
 7 files changed, 79 insertions(+), 29 deletions(-)

diff --git a/doc/src/sgml/runtime.sgml b/doc/src/sgml/runtime.sgml
index bbe9526e5f3..cb533a9cfe5 100644
--- a/doc/src/sgml/runtime.sgml
+++ b/doc/src/sgml/runtime.sgml
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.319 2005/05/15 00:26:18 momjian Exp $
+$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.320 2005/05/20 14:53:25 momjian Exp $
 -->
 
 <chapter Id="runtime">
@@ -1595,7 +1595,7 @@ SET ENABLE_SEQSCAN TO OFF;
         values are
         <literal>fsync</> (call <function>fsync()</> at each commit),
         <literal>fdatasync</> (call <function>fdatasync()</> at each commit),
-        <literal>fsync_writethrough</> (call <function>_commit()</> at each commit on Windows),
+        <literal>fsync_writethrough</> (force write-through of any disk write cache),
         <literal>open_sync</> (write WAL files with <function>open()</> option <symbol>O_SYNC</>), and
         <literal>open_datasync</> (write WAL files with <function>open()</> option <symbol>O_DSYNC</>).
         Not all of these choices are available on all platforms.
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index bff12a14375..35d914d8dde 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.192 2005/05/19 21:35:45 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.193 2005/05/20 14:53:25 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -51,11 +51,6 @@
  * default method.	We assume that fsync() is always available, and that
  * configure determined whether fdatasync() is.
  */
-#define SYNC_METHOD_FSYNC		0
-#define SYNC_METHOD_FDATASYNC	1
-#define SYNC_METHOD_OPEN		2		/* used for both O_SYNC and
-										 * O_DSYNC */
-
 #if defined(O_SYNC)
 #define OPEN_SYNC_FLAG	   O_SYNC
 #else
@@ -79,21 +74,19 @@
 #define DEFAULT_SYNC_METHOD_STR    "open_datasync"
 #define DEFAULT_SYNC_METHOD		   SYNC_METHOD_OPEN
 #define DEFAULT_SYNC_FLAGBIT	   OPEN_DATASYNC_FLAG
-#else
-#if defined(HAVE_FDATASYNC)
+#elif defined(HAVE_FDATASYNC)
 #define DEFAULT_SYNC_METHOD_STR   "fdatasync"
 #define DEFAULT_SYNC_METHOD		  SYNC_METHOD_FDATASYNC
 #define DEFAULT_SYNC_FLAGBIT	  0
-#else
-#ifndef FSYNC_IS_WRITE_THROUGH
+#elif !defined(HAVE_FSYNC_WRITETHROUGH_ONLY)
 #define DEFAULT_SYNC_METHOD_STR   "fsync"
+#define DEFAULT_SYNC_METHOD		  SYNC_METHOD_FSYNC
+#define DEFAULT_SYNC_FLAGBIT	  0
 #else
 #define DEFAULT_SYNC_METHOD_STR   "fsync_writethrough"
-#endif
-#define DEFAULT_SYNC_METHOD		  SYNC_METHOD_FSYNC
+#define DEFAULT_SYNC_METHOD		  SYNC_METHOD_FSYNC_WRITETHROUGH
 #define DEFAULT_SYNC_FLAGBIT	  0
 #endif
-#endif
 
 
 /* User-settable parameters */
@@ -122,7 +115,7 @@ bool		XLOG_DEBUG = false;
 
 
 /* these are derived from XLOG_sync_method by assign_xlog_sync_method */
-static int	sync_method = DEFAULT_SYNC_METHOD;
+int	sync_method = DEFAULT_SYNC_METHOD;
 static int	open_sync_bit = DEFAULT_SYNC_FLAGBIT;
 
 #define XLOG_SYNC_BIT  (enableFsync ? open_sync_bit : 0)
@@ -5249,16 +5242,18 @@ assign_xlog_sync_method(const char *method, bool doit, GucSource source)
 	int			new_sync_method;
 	int			new_sync_bit;
 
-#ifndef FSYNC_IS_WRITE_THROUGH
 	if (pg_strcasecmp(method, "fsync") == 0)
-#else
-	/* Win32 fsync() == _commit(), which writes through a write cache */
-	if (pg_strcasecmp(method, "fsync_writethrough") == 0)
-#endif
 	{
 		new_sync_method = SYNC_METHOD_FSYNC;
 		new_sync_bit = 0;
 	}
+#ifdef HAVE_FSYNC_WRITETHROUGH
+	else if (pg_strcasecmp(method, "fsync_writethrough") == 0)
+	{
+		new_sync_method = SYNC_METHOD_FSYNC_WRITETHROUGH;
+		new_sync_bit = 0;
+	}
+#endif
 #ifdef HAVE_FDATASYNC
 	else if (pg_strcasecmp(method, "fdatasync") == 0)
 	{
@@ -5328,12 +5323,21 @@ issue_xlog_fsync(void)
 	switch (sync_method)
 	{
 		case SYNC_METHOD_FSYNC:
-			if (pg_fsync(openLogFile) != 0)
+			if (pg_fsync_no_writethrough(openLogFile) != 0)
 				ereport(PANIC,
 						(errcode_for_file_access(),
 					errmsg("could not fsync log file %u, segment %u: %m",
 						   openLogId, openLogSeg)));
 			break;
+#ifdef HAVE_FSYNC_WRITETHROUGH
+		case SYNC_METHOD_FSYNC_WRITETHROUGH:
+			if (pg_fsync_writethrough(openLogFile) != 0)
+				ereport(PANIC,
+						(errcode_for_file_access(),
+					errmsg("could not fsync write-through log file %u, segment %u: %m",
+						   openLogId, openLogSeg)));
+			break;
+#endif
 #ifdef HAVE_FDATASYNC
 		case SYNC_METHOD_FDATASYNC:
 			if (pg_fdatasync(openLogFile) != 0)
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index 1db54650eef..81ba3edbbec 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.115 2004/12/31 22:00:51 pgsql Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.116 2005/05/20 14:53:26 momjian Exp $
  *
  * NOTES:
  *
@@ -232,10 +232,26 @@ static void RemovePgTempFilesInDir(const char *tmpdirname);
 
 
 /*
- * pg_fsync --- same as fsync except does nothing if enableFsync is off
+ * pg_fsync --- do fsync with or without writethrough
  */
 int
 pg_fsync(int fd)
+{
+#ifndef HAVE_FSYNC_WRITETHROUGH_ONLY
+	if (sync_method != SYNC_METHOD_FSYNC_WRITETHROUGH)
+		return pg_fsync_no_writethrough(fd);
+	else
+#endif
+		return pg_fsync_writethrough(fd);
+}
+
+
+/*
+ * pg_fsync_no_writethrough --- same as fsync except does nothing if
+ *	enableFsync is off
+ */
+int
+pg_fsync_no_writethrough(int fd)
 {
 	if (enableFsync)
 		return fsync(fd);
@@ -243,6 +259,24 @@ pg_fsync(int fd)
 		return 0;
 }
 
+/*
+ * pg_fsync_writethrough
+ */
+int
+pg_fsync_writethrough(int fd)
+{
+	if (enableFsync)
+#ifdef WIN32
+		return _commit(fd);
+#elif defined(__darwin__)
+		return (fcntl(fd, F_FULLFSYNC, 0) == -1) ? -1 : 0;
+#else
+		return -1;
+#endif
+	else
+		return 0;
+}
+
 /*
  * pg_fdatasync --- same as fdatasync except does nothing if enableFsync is off
  *
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index 27a076c7e0d..ab471738970 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/xlog.h,v 1.60 2005/04/28 21:47:17 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/access/xlog.h,v 1.61 2005/05/20 14:53:26 momjian Exp $
  */
 #ifndef XLOG_H
 #define XLOG_H
@@ -75,6 +75,13 @@ typedef struct XLogRecord
  */
 #define XLOG_NO_TRAN			XLR_INFO_MASK
 
+/* Sync methods */
+#define SYNC_METHOD_FSYNC		0
+#define SYNC_METHOD_FDATASYNC	1
+#define SYNC_METHOD_OPEN		2			/* for O_SYNC and O_DSYNC */
+#define SYNC_METHOD_FSYNC_WRITETHROUGH	3
+extern int	sync_method;
+
 /*
  * List of these structs is used to pass data to XLogInsert().
  *
diff --git a/src/include/port/darwin.h b/src/include/port/darwin.h
index 06c559d2ba8..0a802e5abbb 100644
--- a/src/include/port/darwin.h
+++ b/src/include/port/darwin.h
@@ -1 +1,4 @@
 #define __darwin__	1
+
+#define HAVE_FSYNC_WRITETHROUGH
+
diff --git a/src/include/port/win32.h b/src/include/port/win32.h
index de2483add01..c551b78ebdf 100644
--- a/src/include/port/win32.h
+++ b/src/include/port/win32.h
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.44 2005/03/24 04:36:19 momjian Exp $ */
+/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.45 2005/05/20 14:53:26 momjian Exp $ */
 
 /* undefine and redefine after #include */
 #undef mkdir
@@ -16,8 +16,8 @@
 #define mkdir(a,b)	mkdir(a)
 
 
-#define fsync(a)	_commit(a)
-#define FSYNC_IS_WRITE_THROUGH
+#define HAVE_FSYNC_WRITETHROUGH
+#define HAVE_FSYNC_WRITETHROUGH_ONLY
 #define ftruncate(a,b)	chsize(a,b)
 
 #define USES_WINSOCK
diff --git a/src/include/storage/fd.h b/src/include/storage/fd.h
index 74a05d5aae6..20c2c64c898 100644
--- a/src/include/storage/fd.h
+++ b/src/include/storage/fd.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/fd.h,v 1.50 2004/12/31 22:03:42 pgsql Exp $
+ * $PostgreSQL: pgsql/src/include/storage/fd.h,v 1.51 2005/05/20 14:53:26 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -89,6 +89,8 @@ extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
 							  SubTransactionId parentSubid);
 extern void RemovePgTempFiles(void);
 extern int	pg_fsync(int fd);
+extern int	pg_fsync_no_writethrough(int fd);
+extern int	pg_fsync_writethrough(int fd);
 extern int	pg_fdatasync(int fd);
 
 /* Filename components for OpenTemporaryFile */
-- 
GitLab