From 064bc8ecb31ea7f00068f860f23b2254065e1fce Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Wed, 20 Jul 2022 14:10:37 +1200
Subject: [PATCH v3 12/13] Remove fdatasync configure probe.

fdatasync() is in SUSv2, and all targeted Unix systems have it.  We have
a replacement function for Windows.

We retain the probe for the function declaration, which allows us to
supply the missing declaration for macOS and Windows.  No need to keep a
HAVE_FDATASYNC macro around.

Also rename src/port/fdatasync.c to win32fdatasync.c since the replacement
code is Windows-only.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CA+hUKGJ3LHeP9w5Fgzdr4G8AnEtJ=z=p6hGDEm4qYGEUX5B6fQ@mail.gmail.com
Discussion: https://postgr.es/m/CA%2BhUKGJZJVO%3DiX%2Beb-PXi2_XS9ZRqnn_4URh0NUQOwt6-_51xQ%40mail.gmail.com
---
 configure                                  | 71 +++-------------------
 configure.ac                               |  5 +-
 src/backend/access/transam/xlog.c          |  4 --
 src/backend/storage/file/fd.c              |  8 ---
 src/bin/pg_test_fsync/pg_test_fsync.c      |  4 --
 src/include/access/xlogdefs.h              |  7 +--
 src/include/pg_config.h.in                 |  3 -
 src/include/port/freebsd.h                 |  2 -
 src/include/port/win32_port.h              |  8 ---
 src/port/{fdatasync.c => win32fdatasync.c} |  6 +-
 src/tools/msvc/Mkvcbuild.pm                |  3 +-
 src/tools/msvc/Solution.pm                 |  1 -
 12 files changed, 15 insertions(+), 107 deletions(-)
 rename src/port/{fdatasync.c => win32fdatasync.c} (88%)

diff --git a/configure b/configure
index 21f4520f81..a70768f04e 100755
--- a/configure
+++ b/configure
@@ -12315,63 +12315,6 @@ if test "$ac_res" != no; then :
 
 fi
 
-# Solaris:
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing fdatasync" >&5
-$as_echo_n "checking for library containing fdatasync... " >&6; }
-if ${ac_cv_search_fdatasync+:} false; then :
-  $as_echo_n "(cached) " >&6
-else
-  ac_func_search_save_LIBS=$LIBS
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h.  */
-
-/* Override any GCC internal prototype to avoid an error.
-   Use char because int might match the return type of a GCC
-   builtin and then its argument prototype would still apply.  */
-#ifdef __cplusplus
-extern "C"
-#endif
-char fdatasync ();
-int
-main ()
-{
-return fdatasync ();
-  ;
-  return 0;
-}
-_ACEOF
-for ac_lib in '' rt posix4; do
-  if test -z "$ac_lib"; then
-    ac_res="none required"
-  else
-    ac_res=-l$ac_lib
-    LIBS="-l$ac_lib  $ac_func_search_save_LIBS"
-  fi
-  if ac_fn_c_try_link "$LINENO"; then :
-  ac_cv_search_fdatasync=$ac_res
-fi
-rm -f core conftest.err conftest.$ac_objext \
-    conftest$ac_exeext
-  if ${ac_cv_search_fdatasync+:} false; then :
-  break
-fi
-done
-if ${ac_cv_search_fdatasync+:} false; then :
-
-else
-  ac_cv_search_fdatasync=no
-fi
-rm conftest.$ac_ext
-LIBS=$ac_func_search_save_LIBS
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_fdatasync" >&5
-$as_echo "$ac_cv_search_fdatasync" >&6; }
-ac_res=$ac_cv_search_fdatasync
-if test "$ac_res" != no; then :
-  test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
-
-fi
-
 # Cygwin:
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing shmget" >&5
 $as_echo_n "checking for library containing shmget... " >&6; }
@@ -16039,7 +15982,7 @@ fi
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-for ac_func in backtrace_symbols copyfile fdatasync getifaddrs getpeerucred inet_pton kqueue mbstowcs_l memset_s posix_fallocate ppoll pthread_is_threaded_np setproctitle setproctitle_fast strchrnul strsignal syncfs sync_file_range uselocale wcstombs_l
+for ac_func in backtrace_symbols copyfile getifaddrs getpeerucred inet_pton kqueue mbstowcs_l memset_s posix_fallocate ppoll pthread_is_threaded_np setproctitle setproctitle_fast strchrnul strsignal syncfs sync_file_range uselocale wcstombs_l
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
@@ -16929,12 +16872,6 @@ fi
  ;;
 esac
 
-  case " $LIBOBJS " in
-  *" fdatasync.$ac_objext "* ) ;;
-  *) LIBOBJS="$LIBOBJS fdatasync.$ac_objext"
- ;;
-esac
-
   case " $LIBOBJS " in
   *" getrusage.$ac_objext "* ) ;;
   *) LIBOBJS="$LIBOBJS getrusage.$ac_objext"
@@ -16977,6 +16914,12 @@ esac
  ;;
 esac
 
+  case " $LIBOBJS " in
+  *" win32fdatasync.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS win32fdatasync.$ac_objext"
+ ;;
+esac
+
   case " $LIBOBJS " in
   *" win32link.$ac_objext "* ) ;;
   *) LIBOBJS="$LIBOBJS win32link.$ac_objext"
diff --git a/configure.ac b/configure.ac
index a7ab660179..6c035d8e20 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1253,8 +1253,6 @@ AC_SEARCH_LIBS(getopt_long, [getopt gnugetopt])
 AC_SEARCH_LIBS(shm_open, rt)
 AC_SEARCH_LIBS(shm_unlink, rt)
 AC_SEARCH_LIBS(clock_gettime, [rt posix4])
-# Solaris:
-AC_SEARCH_LIBS(fdatasync, [rt posix4])
 # Cygwin:
 AC_SEARCH_LIBS(shmget, cygipc)
 # *BSD:
@@ -1792,7 +1790,6 @@ LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 AC_CHECK_FUNCS(m4_normalize([
 	backtrace_symbols
 	copyfile
-	fdatasync
 	getifaddrs
 	getpeerucred
 	inet_pton
@@ -1928,7 +1925,6 @@ if test "$PORTNAME" = "win32"; then
   AC_CHECK_FUNCS(_configthreadlocale)
   AC_REPLACE_FUNCS(gettimeofday)
   AC_LIBOBJ(dirmod)
-  AC_LIBOBJ(fdatasync)
   AC_LIBOBJ(getrusage)
   AC_LIBOBJ(kill)
   AC_LIBOBJ(open)
@@ -1936,6 +1932,7 @@ if test "$PORTNAME" = "win32"; then
   AC_LIBOBJ(win32dlopen)
   AC_LIBOBJ(win32env)
   AC_LIBOBJ(win32error)
+  AC_LIBOBJ(win32fdatasync)
   AC_LIBOBJ(win32link)
   AC_LIBOBJ(win32ntdll)
   AC_LIBOBJ(win32pread)
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index eb5115fd5a..34f0150d1e 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -168,9 +168,7 @@ const struct config_enum_entry sync_method_options[] = {
 #ifdef HAVE_FSYNC_WRITETHROUGH
 	{"fsync_writethrough", SYNC_METHOD_FSYNC_WRITETHROUGH, false},
 #endif
-#ifdef HAVE_FDATASYNC
 	{"fdatasync", SYNC_METHOD_FDATASYNC, false},
-#endif
 #ifdef O_SYNC
 	{"open_sync", SYNC_METHOD_OPEN, false},
 #endif
@@ -8015,12 +8013,10 @@ issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
 				msg = _("could not fsync write-through file \"%s\": %m");
 			break;
 #endif
-#ifdef HAVE_FDATASYNC
 		case SYNC_METHOD_FDATASYNC:
 			if (pg_fdatasync(fd) != 0)
 				msg = _("could not fdatasync file \"%s\": %m");
 			break;
-#endif
 		case SYNC_METHOD_OPEN:
 		case SYNC_METHOD_OPEN_DSYNC:
 			/* not reachable */
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index 5a2eb01238..ccb540d617 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -442,20 +442,12 @@ pg_fsync_writethrough(int fd)
 
 /*
  * pg_fdatasync --- same as fdatasync except does nothing if enableFsync is off
- *
- * Not all platforms have fdatasync; treat as fsync if not available.
  */
 int
 pg_fdatasync(int fd)
 {
 	if (enableFsync)
-	{
-#ifdef HAVE_FDATASYNC
 		return fdatasync(fd);
-#else
-		return fsync(fd);
-#endif
-	}
 	else
 		return 0;
 }
diff --git a/src/bin/pg_test_fsync/pg_test_fsync.c b/src/bin/pg_test_fsync/pg_test_fsync.c
index 8f0ef8e66b..77f0db0376 100644
--- a/src/bin/pg_test_fsync/pg_test_fsync.c
+++ b/src/bin/pg_test_fsync/pg_test_fsync.c
@@ -331,7 +331,6 @@ test_sync(int writes_per_op)
 	printf(LABEL_FORMAT, "fdatasync");
 	fflush(stdout);
 
-#ifdef HAVE_FDATASYNC
 	if ((tmpfile = open(filename, O_RDWR | PG_BINARY, 0)) == -1)
 		die("could not open output file");
 	START_TIMER;
@@ -347,9 +346,6 @@ test_sync(int writes_per_op)
 	}
 	STOP_TIMER;
 	close(tmpfile);
-#else
-	printf(NA_FORMAT, _("n/a"));
-#endif
 
 /*
  * Test fsync
diff --git a/src/include/access/xlogdefs.h b/src/include/access/xlogdefs.h
index 810cd1fd86..49e581925c 100644
--- a/src/include/access/xlogdefs.h
+++ b/src/include/access/xlogdefs.h
@@ -67,8 +67,7 @@ typedef uint16 RepOriginId;
 /*
  * This chunk of hackery attempts to determine which file sync methods
  * are available on the current platform, and to choose an appropriate
- * default method.  We assume that fsync() is always available, and that
- * configure determined whether fdatasync() is.
+ * default method.
  *
  * Note that we define our own O_DSYNC on Windows, but not O_SYNC.
  */
@@ -76,10 +75,8 @@ typedef uint16 RepOriginId;
 #define DEFAULT_SYNC_METHOD		PLATFORM_DEFAULT_SYNC_METHOD
 #elif defined(O_DSYNC) && (!defined(O_SYNC) || O_DSYNC != O_SYNC)
 #define DEFAULT_SYNC_METHOD		SYNC_METHOD_OPEN_DSYNC
-#elif defined(HAVE_FDATASYNC)
-#define DEFAULT_SYNC_METHOD		SYNC_METHOD_FDATASYNC
 #else
-#define DEFAULT_SYNC_METHOD		SYNC_METHOD_FSYNC
+#define DEFAULT_SYNC_METHOD		SYNC_METHOD_FDATASYNC
 #endif
 
 #endif							/* XLOG_DEFS_H */
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 6505a84211..2a0d08d10f 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -170,9 +170,6 @@
 /* Define to 1 if you have the `explicit_bzero' function. */
 #undef HAVE_EXPLICIT_BZERO
 
-/* Define to 1 if you have the `fdatasync' function. */
-#undef HAVE_FDATASYNC
-
 /* Define to 1 if fseeko (and presumably ftello) exists and is declared. */
 #undef HAVE_FSEEKO
 
diff --git a/src/include/port/freebsd.h b/src/include/port/freebsd.h
index 2e2e749a6b..0e3fde55d6 100644
--- a/src/include/port/freebsd.h
+++ b/src/include/port/freebsd.h
@@ -5,6 +5,4 @@
  * would prefer open_datasync on FreeBSD 13+, but that is not a good choice on
  * many systems.
  */
-#ifdef HAVE_FDATASYNC
 #define PLATFORM_DEFAULT_SYNC_METHOD	SYNC_METHOD_FDATASYNC
-#endif
diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h
index 2954fabaa5..cd6c9c7515 100644
--- a/src/include/port/win32_port.h
+++ b/src/include/port/win32_port.h
@@ -83,14 +83,6 @@
 #define HAVE_FSYNC_WRITETHROUGH
 #define FSYNC_WRITETHROUGH_IS_FSYNC
 
-/*
- * We have a replacement for fdatasync() in src/port/fdatasync.c, which is
- * unconditionally used by MSVC and Mingw builds.
- */
-#ifndef HAVE_FDATASYNC
-#define HAVE_FDATASYNC
-#endif
-
 #define USES_WINSOCK
 
 /*
diff --git a/src/port/fdatasync.c b/src/port/win32fdatasync.c
similarity index 88%
rename from src/port/fdatasync.c
rename to src/port/win32fdatasync.c
index afef853aa3..31d631c696 100644
--- a/src/port/fdatasync.c
+++ b/src/port/win32fdatasync.c
@@ -1,12 +1,12 @@
 /*-------------------------------------------------------------------------
  *
- * fdatasync.c
+ * win32fdatasync.c
  *	   Win32 fdatasync() replacement
  *
  *
- * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  *
- * src/port/fdatasync.c
+ * src/port/win32fdatasync.c
  *
  *-------------------------------------------------------------------------
  */
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index a06dbcae4d..8d88e78f05 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -99,7 +99,7 @@ sub mkvcbuild
 	$solution = CreateSolution($vsVersion, $config);
 
 	our @pgportfiles = qw(
-	  chklocale.c explicit_bzero.c fdatasync.c
+	  chklocale.c explicit_bzero.c
 	  getpeereid.c getrusage.c inet_aton.c
 	  getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c
 	  snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c
@@ -110,6 +110,7 @@ sub mkvcbuild
 	  strerror.c tar.c
 	  win32dlopen.c
 	  win32env.c win32error.c
+	  win32fdatasync.c
 	  win32link.c
 	  win32pread.c
 	  win32pwrite.c
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index bd8b755b96..d7ab6ab258 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -252,7 +252,6 @@ sub GenerateFiles
 		HAVE_EDITLINE_READLINE_H                    => undef,
 		HAVE_EXECINFO_H                             => undef,
 		HAVE_EXPLICIT_BZERO                         => undef,
-		HAVE_FDATASYNC                              => 1,
 		HAVE_FSEEKO                                 => 1,
 		HAVE_FUNCNAME__FUNC                         => undef,
 		HAVE_FUNCNAME__FUNCTION                     => 1,
-- 
2.37.1

