From 29e9f6e44be1f9187c4ca661104794af2ee5a380 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Sat, 23 Jul 2022 21:34:14 +1200
Subject: [PATCH 02/13] Remove configure probe and extra tests for getrlimit.

getrlimit() is in SUSv2 and all targeted systems have it.

Furthermore, it's not necessary to test for RLIMIT_CORE, RLIMIT_STACK or
RLIMIT_NOFILE macros, since SUSv2 requires those and all targeted
systems have them.

Windows doesn't have getrlimit(), but we can just test WIN32 when doing
Unix-only stuff.
---
 configure                     |  2 +-
 configure.ac                  |  1 -
 src/backend/storage/file/fd.c | 12 ++++--------
 src/backend/tcop/postgres.c   | 10 +++-------
 src/bin/pg_ctl/pg_ctl.c       |  8 ++++----
 src/bin/pgbench/pgbench.c     | 10 +++-------
 src/include/pg_config.h.in    |  3 ---
 src/test/regress/pg_regress.c |  4 ++--
 src/tools/msvc/Solution.pm    |  1 -
 9 files changed, 17 insertions(+), 34 deletions(-)

diff --git a/configure b/configure
index a2706684e3..3abc028412 100755
--- a/configure
+++ b/configure
@@ -16039,7 +16039,7 @@ fi
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-for ac_func in backtrace_symbols clock_gettime copyfile fdatasync getifaddrs getpeerucred getrlimit inet_pton kqueue mbstowcs_l memset_s poll posix_fallocate ppoll pthread_is_threaded_np readlink readv setproctitle setproctitle_fast setsid shm_open strchrnul strsignal symlink syncfs sync_file_range uselocale wcstombs_l writev
+for ac_func in backtrace_symbols clock_gettime copyfile fdatasync getifaddrs getpeerucred inet_pton kqueue mbstowcs_l memset_s poll posix_fallocate ppoll pthread_is_threaded_np readlink readv setproctitle setproctitle_fast setsid shm_open strchrnul strsignal symlink syncfs sync_file_range uselocale wcstombs_l writev
 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"
diff --git a/configure.ac b/configure.ac
index 0861a10ec3..73242d76a6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1797,7 +1797,6 @@ AC_CHECK_FUNCS(m4_normalize([
 	fdatasync
 	getifaddrs
 	getpeerucred
-	getrlimit
 	inet_pton
 	kqueue
 	mbstowcs_l
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index f904f60c08..28401a8881 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -886,7 +886,7 @@ count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
 	int			highestfd = 0;
 	int			j;
 
-#ifdef HAVE_GETRLIMIT
+#ifndef WIN32
 	struct rlimit rlim;
 	int			getrlimit_status;
 #endif
@@ -894,22 +894,18 @@ count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
 	size = 1024;
 	fd = (int *) palloc(size * sizeof(int));
 
-#ifdef HAVE_GETRLIMIT
-#ifdef RLIMIT_NOFILE			/* most platforms use RLIMIT_NOFILE */
+#ifndef WIN32
 	getrlimit_status = getrlimit(RLIMIT_NOFILE, &rlim);
-#else							/* but BSD doesn't ... */
-	getrlimit_status = getrlimit(RLIMIT_OFILE, &rlim);
-#endif							/* RLIMIT_NOFILE */
 	if (getrlimit_status != 0)
 		ereport(WARNING, (errmsg("getrlimit failed: %m")));
-#endif							/* HAVE_GETRLIMIT */
+#endif							/* !WIN32 */
 
 	/* dup until failure or probe limit reached */
 	for (;;)
 	{
 		int			thisfd;
 
-#ifdef HAVE_GETRLIMIT
+#ifndef WIN32
 
 		/*
 		 * don't go beyond RLIMIT_NOFILE; causes irritating kernel logs on
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index bdb11f430f..e4e972cce0 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -4765,12 +4765,12 @@ forbidden_in_wal_sender(char firstchar)
 /*
  * Obtain platform stack depth limit (in bytes)
  *
- * Return -1 if unknown
+ * -1 for error, and errno is set.
  */
 long
 get_stack_depth_rlimit(void)
 {
-#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_STACK)
+#ifndef WIN32
 	static long val = 0;
 
 	/* This won't change after process launch, so check just once */
@@ -4789,13 +4789,9 @@ get_stack_depth_rlimit(void)
 			val = rlim.rlim_cur;
 	}
 	return val;
-#else							/* no getrlimit */
-#if defined(WIN32) || defined(__CYGWIN__)
+#else
 	/* On Windows we set the backend stack size in src/backend/Makefile */
 	return WIN32_STACK_RLIMIT;
-#else							/* not windows ... give up */
-	return -1;
-#endif
 #endif
 }
 
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index ef58883a5c..3157d51918 100644
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -160,7 +160,7 @@ static bool wait_for_postmaster_stop(void);
 static bool wait_for_postmaster_promote(void);
 static bool postmaster_is_alive(pid_t pid);
 
-#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
+#ifndef WIN32
 static void unlimit_core_size(void);
 #endif
 
@@ -776,7 +776,7 @@ wait_for_postmaster_promote(void)
 }
 
 
-#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
+#ifndef WIN32
 static void
 unlimit_core_size(void)
 {
@@ -949,7 +949,7 @@ do_start(void)
 	if (exec_path == NULL)
 		exec_path = find_other_exec_or_die(argv0, "postgres", PG_BACKEND_VERSIONSTR);
 
-#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
+#ifndef WIN32
 	if (allow_core_files)
 		unlimit_core_size();
 #endif
@@ -2069,7 +2069,7 @@ do_help(void)
 	printf(_("If the -D option is omitted, the environment variable PGDATA is used.\n"));
 
 	printf(_("\nOptions for start or restart:\n"));
-#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
+#ifndef WIN32
 	printf(_("  -c, --core-files       allow postgres to produce core files\n"));
 #else
 	printf(_("  -c, --core-files       not applicable on this platform\n"));
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index bcaea8f5ea..71682a3300 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -6583,7 +6583,7 @@ main(int argc, char **argv)
 	int			i;
 	int			nclients_dealt;
 
-#ifdef HAVE_GETRLIMIT
+#ifndef WIN32
 	struct rlimit rlim;
 #endif
 
@@ -6661,12 +6661,8 @@ main(int argc, char **argv)
 				{
 					exit(1);
 				}
-#ifdef HAVE_GETRLIMIT
-#ifdef RLIMIT_NOFILE			/* most platforms use RLIMIT_NOFILE */
+#ifndef WIN32
 				if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
-#else							/* but BSD doesn't ... */
-				if (getrlimit(RLIMIT_OFILE, &rlim) == -1)
-#endif							/* RLIMIT_NOFILE */
 					pg_fatal("getrlimit failed: %m");
 				if (rlim.rlim_cur < nclients + 3)
 				{
@@ -6675,7 +6671,7 @@ main(int argc, char **argv)
 					pg_log_error_hint("Reduce number of clients, or use limit/ulimit to increase the system limit.");
 					exit(1);
 				}
-#endif							/* HAVE_GETRLIMIT */
+#endif							/* !WIN32 */
 				break;
 			case 'j':			/* jobs */
 				benchmarking_option_set = true;
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index c213f31273..8e9318904f 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -237,9 +237,6 @@
 /* Define to 1 if you have the `getpeerucred' function. */
 #undef HAVE_GETPEERUCRED
 
-/* Define to 1 if you have the `getrlimit' function. */
-#undef HAVE_GETRLIMIT
-
 /* Define to 1 if you have the `gettimeofday' function. */
 #undef HAVE_GETTIMEOFDAY
 
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 982801e029..d3505cd13b 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -129,7 +129,7 @@ static void psql_end_command(StringInfo buf, const char *database);
 /*
  * allow core files if possible.
  */
-#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
+#ifndef WIN32
 static void
 unlimit_core_size(void)
 {
@@ -2229,7 +2229,7 @@ regression_main(int argc, char *argv[],
 
 	initialize_environment();
 
-#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
+#ifndef WIN32
 	unlimit_core_size();
 #endif
 
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index ce56b23bba..1018b57f00 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -273,7 +273,6 @@ sub GenerateFiles
 		HAVE_GETOPT_LONG                            => undef,
 		HAVE_GETPEEREID                             => undef,
 		HAVE_GETPEERUCRED                           => undef,
-		HAVE_GETRLIMIT                              => undef,
 		HAVE_GETTIMEOFDAY                           => undef,
 		HAVE_GSSAPI_GSSAPI_H                        => undef,
 		HAVE_GSSAPI_H                               => undef,
-- 
2.30.2

