From 1e8ae136407f2aedd07cc748589f46087b98b950 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sun, 18 Jun 2006 18:30:21 +0000
Subject: [PATCH] Don't try to call posix_fadvise() unless <fcntl.h> supplies a
 declaration for it.  Hopefully will fix core dump evidenced by some buildfarm
 members since fadvise patch went in.  The actual definition of the function
 is not ABI-compatible with compiler's default assumption in the absence of
 any declaration, so it's clearly unsafe to try to call it without seeing a
 declaration.

---
 configure                         | 73 +++++++++++++++++++++++++++++++
 configure.in                      |  3 +-
 src/backend/access/transam/xlog.c | 12 ++---
 src/include/pg_config.h.in        |  4 ++
 4 files changed, 86 insertions(+), 6 deletions(-)

diff --git a/configure b/configure
index f7947ebe72e..5ef6be0cd16 100755
--- a/configure
+++ b/configure
@@ -13917,6 +13917,79 @@ _ACEOF
 fi
 
 
+echo "$as_me:$LINENO: checking whether posix_fadvise is declared" >&5
+echo $ECHO_N "checking whether posix_fadvise is declared... $ECHO_C" >&6
+if test "${ac_cv_have_decl_posix_fadvise+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+#include <fcntl.h>
+
+int
+main ()
+{
+#ifndef posix_fadvise
+  char *p = (char *) posix_fadvise;
+#endif
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+	 { ac_try='test -z "$ac_c_werror_flag"
+			 || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+	 { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_cv_have_decl_posix_fadvise=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+ac_cv_have_decl_posix_fadvise=no
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+echo "$as_me:$LINENO: result: $ac_cv_have_decl_posix_fadvise" >&5
+echo "${ECHO_T}$ac_cv_have_decl_posix_fadvise" >&6
+if test $ac_cv_have_decl_posix_fadvise = yes; then
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_DECL_POSIX_FADVISE 1
+_ACEOF
+
+
+else
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_DECL_POSIX_FADVISE 0
+_ACEOF
+
+
+fi
+
+
 
 HAVE_IPV6=no
 echo "$as_me:$LINENO: checking for struct sockaddr_in6" >&5
diff --git a/configure.in b/configure.in
index 9075690fccb..0f58eb7a4b8 100644
--- a/configure.in
+++ b/configure.in
@@ -1,5 +1,5 @@
 dnl Process this file with autoconf to produce a configure script.
-dnl $PostgreSQL: pgsql/configure.in,v 1.466 2006/06/07 22:24:43 momjian Exp $
+dnl $PostgreSQL: pgsql/configure.in,v 1.467 2006/06/18 18:30:20 tgl Exp $
 dnl
 dnl Developers, please strive to achieve this order:
 dnl
@@ -864,6 +864,7 @@ PGAC_FUNC_GETTIMEOFDAY_1ARG
 AC_CHECK_FUNCS([cbrt dlopen fcvt fdatasync getpeereid memmove poll pstat readlink setproctitle setsid sigprocmask symlink sysconf towlower utime utimes waitpid wcstombs])
 
 AC_CHECK_DECLS(fdatasync, [], [], [#include <unistd.h>])
+AC_CHECK_DECLS(posix_fadvise, [], [], [#include <fcntl.h>])
 
 HAVE_IPV6=no
 AC_CHECK_TYPE([struct sockaddr_in6],
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 57a8d1833d8..85618fcf01c 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.239 2006/06/16 04:11:48 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.240 2006/06/18 18:30:20 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -2147,11 +2147,13 @@ XLogFileClose(void)
 {
 	Assert(openLogFile >= 0);
 
-#ifdef POSIX_FADV_DONTNEED
+#if defined(HAVE_DECL_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
 	/*
-	 * WAL caches will not be accessed in the future, so we advise OS to
-	 * free them. But we will not do so if WAL archiving is active,
-	 * because archivers might use the caches to read the WAL segment.
+	 * WAL segment files will not be re-read in normal operation, so we advise
+	 * OS to release any cached pages.  But do not do so if WAL archiving is
+	 * active, because archiver process could use the cache to read the WAL
+	 * segment.
+	 *
 	 * While O_DIRECT works for O_SYNC, posix_fadvise() works for fsync()
 	 * and O_SYNC, and some platforms only have posix_fadvise().
 	 */
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 5ccfbfefd28..82ccde63c1c 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -76,6 +76,10 @@
    don't. */
 #undef HAVE_DECL_F_FULLFSYNC
 
+/* Define to 1 if you have the declaration of `posix_fadvise', and to 0 if you
+   don't. */
+#undef HAVE_DECL_POSIX_FADVISE
+
 /* Define to 1 if you have the declaration of `snprintf', and to 0 if you
    don't. */
 #undef HAVE_DECL_SNPRINTF
-- 
GitLab