From d38cc95dceda5fcf98c8aae6379085e9c38a3155 Mon Sep 17 00:00:00 2001
From: Tristan Partin <tristan@neon.tech>
Date: Fri, 16 Jun 2023 12:47:26 -0500
Subject: [PATCH v1] Default client_connection_check_interval to 10s on
 supported systems

client_connection_check_interval is an inherently valuable parameter
when supported. It can help cancel long running queries server-side when
a connection drops client-side.
---
 configure.ac                        |  3 +++
 doc/src/sgml/config.sgml            | 10 ++++++----
 meson.build                         |  2 ++
 src/backend/utils/misc/guc_tables.c |  7 ++++++-
 src/include/pg_config.h.in          |  6 ++++++
 src/tools/msvc/Solution.pm          |  2 ++
 6 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/configure.ac b/configure.ac
index 09558ada0f..3df7bbeb4f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1846,6 +1846,9 @@ AC_CHECK_DECLS([strlcat, strlcpy, strnlen])
 AC_CHECK_DECLS([preadv], [], [AC_LIBOBJ(preadv)], [#include <sys/uio.h>])
 AC_CHECK_DECLS([pwritev], [], [AC_LIBOBJ(pwritev)], [#include <sys/uio.h>])
 
+AC_CHECK_DECLS(EPOLLHUP, [], [], [#include <epoll.h>])
+AC_CHECK_DECLS(EPOLLRDHUP, [], [], [#include <epoll.h>])
+
 # This is probably only present on macOS, but may as well check always
 AC_CHECK_DECLS(F_FULLFSYNC, [], [], [#include <fcntl.h>])
 
diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 6262cb7bb2..24f69ebd15 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -1044,10 +1044,12 @@ include_dir 'conf.d'
        </para>
        <para>
         If the value is specified without units, it is taken as milliseconds.
-        The default value is <literal>0</literal>, which disables connection
-        checks.  Without connection checks, the server will detect the loss of
-        the connection only at the next interaction with the socket, when it
-        waits for, receives or sends data.
+        The default value is <literal>0</literal> on systems not previously
+        mentioned, such as Windows, which disables connection checks, and a
+        default value of <literal>10000</literal>, otherwise.  Without
+        connection checks, the server will detect the loss of the connection
+        only at the next interaction with the socket, when it waits for,
+        receives or sends data.
        </para>
        <para>
         For the kernel itself to detect lost TCP connections reliably and within
diff --git a/meson.build b/meson.build
index 82f2782673..8de6a7a7f1 100644
--- a/meson.build
+++ b/meson.build
@@ -2163,6 +2163,8 @@ endforeach
 
 
 decl_checks = [
+  ['EPOLLHUP', 'sys/epoll.h'],
+  ['EPOLLRDHUP', 'sys/epoll.h'],
   ['F_FULLFSYNC', 'fcntl.h'],
   ['fdatasync', 'unistd.h'],
   ['posix_fadvise', 'fcntl.h'],
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index 71e27f8eb0..8cfd6b61bb 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -3478,7 +3478,12 @@ struct config_int ConfigureNamesInt[] =
 			GUC_UNIT_MS
 		},
 		&client_connection_check_interval,
-		0, 0, INT_MAX,
+#if defined(HAVE_DECL_EPOLLHUP) || defined(HAVE_DECL_EPOLLRDHUP)
+		10000,
+#else
+		0,
+#endif
+		0, INT_MAX,
 		check_client_connection_check_interval, NULL, NULL
 	},
 
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 6d572c3820..7ce5004550 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -91,6 +91,12 @@
 /* Define to 1 if you have the `CRYPTO_lock' function. */
 #undef HAVE_CRYPTO_LOCK
 
+/* Define to 1 if you have the EPOLLHUP flag */
+#undef HAVE_DECL_EPOLLHUP
+
+/* Define to 1 if you have the EPOLLRDHUP flag */
+#undef HAVE_DECL_EPOLLRDHUP
+
 /* Define to 1 if you have the declaration of `fdatasync', and to 0 if you
    don't. */
 #undef HAVE_DECL_FDATASYNC
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index b6d31c3583..14c43fefb0 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -232,6 +232,8 @@ sub GenerateFiles
 		HAVE_COPYFILE_H => undef,
 		HAVE_CRTDEFS_H => undef,
 		HAVE_CRYPTO_LOCK => undef,
+		HAVE_DECL_EPOLLHUP => 0,
+		HAVE_DECL_EPOLLRDHUP => 0,
 		HAVE_DECL_FDATASYNC => 0,
 		HAVE_DECL_F_FULLFSYNC => 0,
 		HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER => 0,
-- 
Tristan Partin
Neon (https://neon.tech)

