From 456a5a66148790792c32eb71b4a4f11bb157d25d Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <postgres@jeltef.nl>
Date: Sun, 7 Dec 2025 15:07:08 +0100
Subject: [PATCH v13 1/2] Bump postmaster soft open file limit (RLIMIT_NOFILE)
 when necessary

The default open file limit of 1024 on Linux is extremely low. The
reason that this hasn't changed change is because doing so would break
legacy programs that use the select(2) system call in hard to debug
ways. So instead programs that want to opt-in to a higher open file
limit are expected to bump their soft limit to their hard limit on
startup. Details on this are very well explained in a blogpost by the
systemd author[1]. There's also a similar change done by the Go
language[2].

This starts bumping postmaster its soft open file limit when we realize
that we'll run into the soft limit with the requested
max_files_per_process GUC. We do so by slightly changing the meaning of
the max_files_per_process GUC. The actual (not publicly exposed) limit
is max_safe_fds, previously this would be set to:
max_files_per_process - already_open_files - NUM_RESERVED_FDS
After this change we now try to set max_safe_fds to
max_files_per_process if the system allows that. This is deemed more
natural to understand for users, because now the limit of files that
they can open is actually what they configured in max_files_per_process.

Adding this infrastructure to change RLIMIT_NOFILE when needed is
especially useful for the AIO work, because io_uring consumes a lot of
file descriptors. Even without looking at AIO there is a large number of
reports from people that require changing their soft file limit before
starting Postgres, sometimes falling back to lowering
max_files_per_process when they fail to do so[3-8].
It's also not all that strange to fail at setting the soft open file
limit because there are multiple places where one can configure such
limits and usually only one of them is effective (which one depends on
how Postgres is started). In cloud environments its also often not
possible for user to change the soft limit, because they don't control
the way that Postgres is started. Finally, for the multi-threading work
this is also very important because then the open file limit is not
per-backend anymore, but for the whole system.

The most complex change in this patch is how we shell out to other
systems executables. Instead of using system and popen directly we now
implement our own versions of these commands that restore the original
file limits before starting the other executables. This is done as a
precaution in case those executables are using select(2).

P.S. The initdb test needed to be updated because on our OpenBSD CI the
stderr would now contain:

LOG:  increased open file limit to 1004

[1]: https://0pointer.net/blog/file-descriptor-limits.html
[2]: https://github.com/golang/go/issues/46279
[3]: https://serverfault.com/questions/785330/getting-too-many-open-files-error-for-postgres
[4]: https://serverfault.com/questions/716982/how-to-raise-max-no-of-file-descriptors-for-daemons-running-on-debian-jessie
[5]: https://www.postgresql.org/message-id/flat/CAKtc8vXh7NvP_qWj8EqqorPY97bvxSaX3h5u7a9PptRFHW5x7g%40mail.gmail.com
[6]: https://www.postgresql.org/message-id/flat/113ce31b0908120955w77029099i7ececc053084095a%40mail.gmail.com
[7]: https://github.com/abiosoft/colima/discussions/836
[8]: https://www.postgresql.org/message-id/flat/29663.1007738957%40sss.pgh.pa.us#2079ec9e2d8b251593812a3711bfe9e9
---
 configure                                |   2 +-
 configure.ac                             |   1 +
 meson.build                              |   1 +
 src/backend/access/transam/xlogarchive.c |  16 +-
 src/backend/archive/shell_archive.c      |  13 +-
 src/backend/storage/file/fd.c            | 493 +++++++++++++++++++++--
 src/bin/initdb/t/001_initdb.pl           |   6 +-
 src/include/pg_config.h.in               |   3 +
 src/include/storage/fd.h                 |   1 +
 9 files changed, 491 insertions(+), 45 deletions(-)

diff --git a/configure b/configure
index f3b135c95ca..f5fddd8fd2b 100755
--- a/configure
+++ b/configure
@@ -15963,7 +15963,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 copy_file_range elf_aux_info explicit_memset getauxval getifaddrs getpeerucred inet_pton kqueue localeconv_l mbstowcs_l memset_explicit posix_fallocate ppoll pthread_is_threaded_np setproctitle setproctitle_fast strsignal syncfs sync_file_range uselocale wcstombs_l
+for ac_func in backtrace_symbols copyfile copy_file_range elf_aux_info explicit_memset getauxval getifaddrs getpeerucred inet_pton kqueue localeconv_l mbstowcs_l memset_explicit pipe2 posix_fallocate ppoll pthread_is_threaded_np setproctitle setproctitle_fast 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"
diff --git a/configure.ac b/configure.ac
index 18bbe596612..173362041b2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1877,6 +1877,7 @@ AC_CHECK_FUNCS(m4_normalize([
 	localeconv_l
 	mbstowcs_l
 	memset_explicit
+	pipe2
 	posix_fallocate
 	ppoll
 	pthread_is_threaded_np
diff --git a/meson.build b/meson.build
index 3238c849e1b..79f9e38de15 100644
--- a/meson.build
+++ b/meson.build
@@ -3221,6 +3221,7 @@ func_checks = [
   ['mbstowcs_l'],
   ['memset_explicit'],
   ['mkdtemp'],
+  ['pipe2'],
   ['posix_fadvise'],
   ['posix_fallocate'],
   ['ppoll'],
diff --git a/src/backend/access/transam/xlogarchive.c b/src/backend/access/transam/xlogarchive.c
index 0d9ffc99726..7d4e9185b6b 100644
--- a/src/backend/access/transam/xlogarchive.c
+++ b/src/backend/access/transam/xlogarchive.c
@@ -165,9 +165,9 @@ RestoreArchivedFile(char *path, const char *xlogfname,
 	/*
 	 * PreRestoreCommand() informs the SIGTERM handler for the startup process
 	 * that it should proc_exit() right away.  This is done for the duration
-	 * of the system() call because there isn't a good way to break out while
-	 * it is executing.  Since we might call proc_exit() in a signal handler,
-	 * it is best to put any additional logic before or after the
+	 * of the pg_system() call because there isn't a good way to break out
+	 * while it is executing.  Since we might call proc_exit() in a signal
+	 * handler, it is best to put any additional logic before or after the
 	 * PreRestoreCommand()/PostRestoreCommand() section.
 	 */
 	PreRestoreCommand();
@@ -175,7 +175,7 @@ RestoreArchivedFile(char *path, const char *xlogfname,
 	/*
 	 * Copy xlog from archival storage to XLOGDIR
 	 */
-	rc = system(xlogRestoreCmd);
+	rc = pg_system(xlogRestoreCmd);
 
 	PostRestoreCommand();
 
@@ -248,9 +248,9 @@ RestoreArchivedFile(char *path, const char *xlogfname,
 	 * However, if the failure was due to any sort of signal, it's best to
 	 * punt and abort recovery.  (If we "return false" here, upper levels will
 	 * assume that recovery is complete and start up the database!) It's
-	 * essential to abort on child SIGINT and SIGQUIT, because per spec
-	 * system() ignores SIGINT and SIGQUIT while waiting; if we see one of
-	 * those it's a good bet we should have gotten it too.
+	 * essential to abort on child SIGINT and SIGQUIT, because the postmaster
+	 * sends those to our whole process group; if the restore command died on
+	 * one of those, the signal was most likely meant for us too.
 	 *
 	 * On SIGTERM, assume we have received a fast shutdown request, and exit
 	 * cleanly. It's pure chance whether we receive the SIGTERM first, or the
@@ -328,7 +328,7 @@ ExecuteRecoveryCommand(const char *command, const char *commandName,
 	 */
 	fflush(NULL);
 	pgstat_report_wait_start(wait_event_info);
-	rc = system(xlogRecoveryCmd);
+	rc = pg_system(xlogRecoveryCmd);
 	pgstat_report_wait_end();
 
 	pfree(xlogRecoveryCmd);
diff --git a/src/backend/archive/shell_archive.c b/src/backend/archive/shell_archive.c
index 0b427a68809..fc012fe3ba6 100644
--- a/src/backend/archive/shell_archive.c
+++ b/src/backend/archive/shell_archive.c
@@ -22,6 +22,7 @@
 #include "archive/shell_archive.h"
 #include "common/percentrepl.h"
 #include "pgstat.h"
+#include "storage/fd.h"
 #include "utils/wait_event.h"
 
 static bool shell_archive_configured(ArchiveModuleState *state);
@@ -78,18 +79,18 @@ shell_archive_file(ArchiveModuleState *state, const char *file,
 
 	fflush(NULL);
 	pgstat_report_wait_start(WAIT_EVENT_ARCHIVE_COMMAND);
-	rc = system(xlogarchcmd);
+	rc = pg_system(xlogarchcmd);
 	pgstat_report_wait_end();
 
 	if (rc != 0)
 	{
 		/*
 		 * If either the shell itself, or a called command, died on a signal,
-		 * abort the archiver.  We do this because system() ignores SIGINT and
-		 * SIGQUIT while waiting; so a signal is very likely something that
-		 * should have interrupted us too.  Also die if the shell got a hard
-		 * "command not found" type of error.  If we overreact it's no big
-		 * deal, the postmaster will just start the archiver again.
+		 * abort the archiver.  We do this because a signal is very likely
+		 * something that should have interrupted us too, e.g. because the
+		 * postmaster signaled our whole process group.  Also die if the shell
+		 * got a hard "command not found" type of error.  If we overreact it's
+		 * no big deal, the postmaster will just start the archiver again.
 		 */
 		int			lev = wait_result_is_any_signal(rc, true) ? FATAL : LOG;
 
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index 443a5dd8f23..7d7990fe02d 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -80,6 +80,7 @@
 #include <sys/types.h>
 #ifndef WIN32
 #include <sys/mman.h>
+#include <sys/wait.h>
 #endif
 #include <limits.h>
 #include <unistd.h>
@@ -91,6 +92,7 @@
 #include "common/file_perm.h"
 #include "common/file_utils.h"
 #include "common/pg_prng.h"
+#include "libpq/pqsignal.h"
 #include "miscadmin.h"
 #include "pgstat.h"
 #include "postmaster/startup.h"
@@ -159,6 +161,13 @@ int			max_files_per_process = 1000;
  */
 int			max_safe_fds = FD_MINFREE;	/* default if not changed */
 
+#ifdef HAVE_GETRLIMIT
+static bool saved_original_max_open_files;
+static struct rlimit original_max_open_files;
+static struct rlimit custom_max_open_files;
+#endif
+
+
 /* Whether it is safe to continue running after fsync() fails. */
 bool		data_sync_retry = false;
 
@@ -265,6 +274,11 @@ typedef struct
 		FILE	   *file;
 		DIR		   *dir;
 		int			fd;
+		struct
+		{
+			FILE	   *file;
+			pid_t		pid;
+		}			pipe;
 	}			desc;
 } AllocateDesc;
 
@@ -950,6 +964,132 @@ InitTemporaryFileAccess(void)
 #endif
 }
 
+/*
+ * Returns true if the passed in highestfd is the last one that we're allowed
+ * to open based on the current soft open file limit (RLIMIT_NOFILE).
+ */
+static bool
+IsOpenFileLimit(int highestfd)
+{
+#ifdef HAVE_GETRLIMIT
+	if (!saved_original_max_open_files)
+	{
+		return false;
+	}
+
+	return highestfd >= custom_max_open_files.rlim_cur - 1;
+#else
+	return false;
+#endif
+}
+
+/*
+ * Increases the open file limit (RLIMIT_NOFILE) by the requested amount.
+ * Returns true if successful, false otherwise.
+ */
+static bool
+IncreaseOpenFileLimit(int extra_files)
+{
+#ifdef HAVE_GETRLIMIT
+	struct rlimit rlim;
+
+	if (!saved_original_max_open_files)
+	{
+		return false;
+	}
+
+	rlim = custom_max_open_files;
+
+	/* If we're already at the max we reached our limit */
+	if (rlim.rlim_cur == original_max_open_files.rlim_max)
+		return false;
+
+	/*
+	 * Otherwise increase the soft limit, but not beyond the hard limit.
+	 * Written this way so that it cannot overflow.
+	 */
+	rlim.rlim_cur += Min((rlim_t) extra_files, rlim.rlim_max - rlim.rlim_cur);
+
+	if (setrlimit(RLIMIT_NOFILE, &rlim) != 0)
+	{
+		/* We made sure not to exceed the hard limit, so this shouldn't fail */
+		ereport(WARNING, (errmsg("setrlimit failed: %m")));
+		return false;
+	}
+
+	custom_max_open_files = rlim;
+
+	elog(LOG, "increased open file limit to %ld", (long) rlim.rlim_cur);
+
+	return true;
+#else
+	return false;
+#endif
+}
+
+/*
+ * Saves the original open file limit (RLIMIT_NOFILE) the first time when this
+ * is called. If called again it's a no-op.
+ */
+static void
+SaveOriginalOpenFileLimit(void)
+{
+#ifdef HAVE_GETRLIMIT
+	int			status;
+
+	if (saved_original_max_open_files)
+	{
+		/* Already saved, no need to do it again */
+		return;
+	}
+
+	status = getrlimit(RLIMIT_NOFILE, &original_max_open_files);
+	if (status != 0)
+	{
+		ereport(WARNING, (errmsg("getrlimit failed: %m")));
+		return;
+	}
+
+	custom_max_open_files = original_max_open_files;
+	saved_original_max_open_files = true;
+	return;
+#endif
+}
+
+#ifndef WIN32
+/*
+ * UseOriginalOpenFileLimit --- Makes the process use the original open file
+ * 		limit that was present at postmaster start.
+ *
+ * This should be called in the child process after fork(), right before
+ * exec'ing a program that might use select(2), which can only handle file
+ * descriptors up to 1024.
+ */
+static void
+UseOriginalOpenFileLimit(void)
+{
+#ifdef HAVE_GETRLIMIT
+	if (!saved_original_max_open_files)
+	{
+		return;
+	}
+
+	if (custom_max_open_files.rlim_cur == original_max_open_files.rlim_cur)
+	{
+		/* Not changed, so no need to call setrlimit at all */
+		return;
+	}
+
+	/*
+	 * Errors are ignored because we can't ereport() in a forked child. This
+	 * shouldn't fail anyway, and keeping the raised limit is normally
+	 * harmless.
+	 */
+	(void) setrlimit(RLIMIT_NOFILE, &original_max_open_files);
+#endif
+}
+#endif							/* WIN32 */
+
 /*
  * count_usable_fds --- count how many FDs the system will let us open,
  *		and estimate how many are already open.
@@ -972,39 +1112,51 @@ count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
 	int			used = 0;
 	int			highestfd = 0;
 	int			j;
-
-#ifdef HAVE_GETRLIMIT
-	struct rlimit rlim;
-	int			getrlimit_status;
-#endif
+	bool		increased_limit = false;
 
 	size = 1024;
 	fd = palloc_array(int, size);
 
-#ifdef HAVE_GETRLIMIT
-	getrlimit_status = getrlimit(RLIMIT_NOFILE, &rlim);
-	if (getrlimit_status != 0)
-		ereport(WARNING, (errmsg("getrlimit failed: %m")));
-#endif							/* HAVE_GETRLIMIT */
+	SaveOriginalOpenFileLimit();
 
 	/* dup until failure or probe limit reached */
 	for (;;)
 	{
 		int			thisfd;
 
-#ifdef HAVE_GETRLIMIT
-
 		/*
-		 * don't go beyond RLIMIT_NOFILE; causes irritating kernel logs on
-		 * some platforms
+		 * Don't go beyond RLIMIT_NOFILE; causes irritating kernel logs on
+		 * some platforms. Instead raise the soft limit if we can.
+		 *
+		 * One raise leaves room for all remaining probes, so we do it at most
+		 * once. Hitting the limit again means the kernel enforces a lower one
+		 * (e.g. kern.maxfilesperproc on macOS), and raising it further would
+		 * loop forever.
 		 */
-		if (getrlimit_status == 0 && highestfd >= rlim.rlim_cur - 1)
-			break;
-#endif
+		if (IsOpenFileLimit(highestfd))
+		{
+			if (increased_limit || !IncreaseOpenFileLimit(max_to_probe - used))
+				break;
+			increased_limit = true;
+		}
 
 		thisfd = dup(2);
 		if (thisfd < 0)
 		{
+			/*
+			 * Even though we do the pre-check above, it's still possible that
+			 * the call to dup fails with EMFILE. This can happen if the last
+			 * file descriptor was already assigned to an "already open" file.
+			 * One example of this happening, is if we're already at the soft
+			 * limit when we call count_usable_fds.
+			 */
+			if (errno == EMFILE && !increased_limit &&
+				IncreaseOpenFileLimit(max_to_probe - used))
+			{
+				increased_limit = true;
+				continue;
+			}
+
 			/* Expect EMFILE or ENFILE, else it's fishy */
 			if (errno != EMFILE && errno != ENFILE)
 				elog(WARNING, "duplicating stderr file descriptor failed after %d successes: %m", used);
@@ -2721,6 +2873,293 @@ OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
 	return -1;					/* failure */
 }
 
+#ifndef WIN32
+/*
+ * Helper function to fork a child process for executing a shell command.
+ *
+ * This handles all the standard child process setup:
+ * - Blocks signals before fork to avoid race conditions
+ * - Lowers the file limit in the child (UseOriginalOpenFileLimit)
+ * - Resets signal handlers that the backend has set to SIG_IGN
+ * - Unblocks signals in both parent and child after setup
+ *
+ * Returns -1 on fork failure, 0 in the child, or the child PID in the parent.
+ * After this returns in the child, the caller should do any additional setup
+ * (like pipe redirection) and then call exec_shell_command().
+ *
+ * Until exec, the child must only make plain system calls (no ereport(),
+ * palloc(), etc.), because that's all that is safe after fork() in a
+ * multithreaded process. Handle errors with _exit().
+ */
+static pid_t
+fork_for_shell_command(void)
+{
+	pid_t		pid;
+	sigset_t	save_mask;
+
+	/* Block signals during fork to avoid race conditions in child setup */
+	sigprocmask(SIG_SETMASK, &BlockSig, &save_mask);
+
+	pid = fork();
+	if (pid == 0)
+	{
+		/* Child process */
+		UseOriginalOpenFileLimit();
+
+		/*
+		 * Reset signal handlers to default: SIG_IGN would survive exec, and
+		 * our own handlers must not run if the postmaster signals our process
+		 * group before we exec.
+		 */
+		pqsignal(SIGINT, PG_SIG_DFL);
+		pqsignal(SIGTERM, PG_SIG_DFL);
+		pqsignal(SIGQUIT, PG_SIG_DFL);
+		pqsignal(SIGPIPE, PG_SIG_DFL);
+	}
+
+	/* Restore/unblock signals in both parent and child (also if fork failed) */
+	sigprocmask(SIG_SETMASK, &save_mask, NULL);
+
+	return pid;
+}
+
+/*
+ * Execute a shell command via /bin/sh. This function does not return.
+ * Should be called in the child process after fork_for_shell_command().
+ */
+static pg_noreturn void
+exec_shell_command(const char *command)
+{
+	execl("/bin/sh", "sh", "-c", command, (char *) NULL);
+	_exit(127);
+}
+#endif
+
+/*
+ * pg_system - Custom system() that lowers file limit in child process.
+ *
+ * This is needed because we may have bumped the soft RLIMIT_NOFILE limit
+ * above its original value. The child process should use the original limit
+ * to avoid issues with programs that use select(2), which can only handle
+ * FDs up to FD_SETSIZE (typically 1024).
+ *
+ * We can't just call UseOriginalOpenFileLimit() before the standard system()
+ * because that would temporarily lower the limit in the parent process,
+ * which could cause concurrent file operations to fail in a multithreaded
+ * environment.
+ */
+int
+pg_system(const char *command)
+{
+#ifndef WIN32
+	pid_t		pid;
+	int			status;
+	pid_t		waitresult;
+
+	pid = fork_for_shell_command();
+	if (pid == (pid_t) -1)
+		return -1;
+	if (pid == 0)
+		exec_shell_command(command);	/* does not return */
+
+	/* Wait for child */
+	do
+	{
+		waitresult = waitpid(pid, &status, 0);
+	} while (waitresult == (pid_t) -1 && errno == EINTR);
+
+	if (waitresult != pid)
+		return -1;
+
+	return status;
+#else
+	/* On Windows, just use standard system() - no RLIMIT_NOFILE bumping there */
+	return system(command);
+#endif
+}
+
+
+/*
+ * pg_popen - Custom popen() that lowers file limit in child process.
+ *
+ * This is needed because we may have bumped the soft RLIMIT_NOFILE limit
+ * above its original value. The child process should use the original limit
+ * to avoid issues with programs that use select(2), which can only handle
+ * FDs up to FD_SETSIZE (typically 1024).
+ *
+ * We can't just call UseOriginalOpenFileLimit() before popen() of the stdlib
+ * because popen() opens a file descriptor in the parent process (the parent's
+ * side of the pipe), which would fail if we're already above the original
+ * limit.
+ *
+ * Returns the FILE* for the pipe, or NULL on error.
+ * On success, *child_pid is set to the child's PID (or 0 on Windows where
+ * we use standard popen).
+ */
+static FILE *
+pg_popen(const char *command, const char *mode, pid_t *child_pid)
+{
+#ifndef WIN32
+	int			pipe_fds[2];
+	int			parent_fd;
+	int			child_fd;
+	int			child_target_fd;
+	pid_t		pid;
+	FILE	   *pipe_file;
+	int			save_errno;
+
+	/* Only "r" and "w" modes are supported */
+	if (mode[0] == 'r')
+	{
+		/* Parent reads from child's stdout */
+		parent_fd = 0;
+		child_fd = 1;
+		child_target_fd = STDOUT_FILENO;
+	}
+	else if (mode[0] == 'w')
+	{
+		/* Parent writes to child's stdin */
+		parent_fd = 1;
+		child_fd = 0;
+		child_target_fd = STDIN_FILENO;
+	}
+	else
+	{
+		errno = EINVAL;
+		return NULL;
+	}
+
+	/*
+	 * Set close-on-exec to prevent other concurrently spawned child processes
+	 * from inheriting the pipe file descriptors. This is impossible while
+	 * we're not using threads, but since this is part of adding such
+	 * multithreading support let's pretend that we already have it. For
+	 * systems that don't have pipe2, there's always a risk of race
+	 * conditions, but let's minimize that by quickly setting the
+	 * close-on-exec flag with fcntl.
+	 */
+#ifdef HAVE_PIPE2
+	if (pipe2(pipe_fds, O_CLOEXEC) < 0)
+		return NULL;
+#else
+	if (pipe(pipe_fds) < 0)
+		return NULL;
+	if (fcntl(pipe_fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
+		fcntl(pipe_fds[1], F_SETFD, FD_CLOEXEC) == -1)
+	{
+		save_errno = errno;
+		close(pipe_fds[0]);
+		close(pipe_fds[1]);
+		errno = save_errno;
+		return NULL;
+	}
+#endif
+
+	fflush(NULL);
+
+	pid = fork_for_shell_command();
+	if (pid < 0)
+	{
+		/* fork failed */
+		save_errno = errno;
+		close(pipe_fds[0]);
+		close(pipe_fds[1]);
+		errno = save_errno;
+		return NULL;
+	}
+
+	if (pid == 0)
+	{
+		/* Child process - set up pipe redirection and exec */
+		close(pipe_fds[parent_fd]);
+		if (pipe_fds[child_fd] != child_target_fd)
+		{
+			if (dup2(pipe_fds[child_fd], child_target_fd) < 0)
+				_exit(127);
+			close(pipe_fds[child_fd]);
+		}
+		else
+		{
+			/*
+			 * The pipe already ended up on the target fd, so no dup2() is
+			 * needed. But then we have to clear O_CLOEXEC ourselves, because
+			 * otherwise the fd gets closed when we exec.
+			 */
+			if (fcntl(child_target_fd, F_SETFD, 0) < 0)
+				_exit(127);
+		}
+
+		exec_shell_command(command);	/* does not return */
+	}
+
+	/* Parent process */
+	close(pipe_fds[child_fd]);
+	pipe_file = fdopen(pipe_fds[parent_fd], mode);
+	if (pipe_file == NULL)
+	{
+		save_errno = errno;
+		close(pipe_fds[parent_fd]);
+		errno = save_errno;
+		return NULL;
+	}
+
+	*child_pid = pid;
+	return pipe_file;
+#else
+	/* On Windows, just use standard popen - no RLIMIT_NOFILE bumping there */
+	fflush(NULL);
+	*child_pid = 0;
+	return popen(command, mode);
+#endif
+}
+
+/*
+ * pg_pclose - Close a pipe opened by pg_popen().
+ *
+ * On Unix, this closes the FILE* and waits for the child process.
+ * On Windows, this just calls pclose().
+ *
+ * Returns the child's exit status (like pclose), or -1 on error.
+ */
+static int
+pg_pclose(FILE *fp, pid_t child_pid)
+{
+#ifndef WIN32
+	int			status;
+	pid_t		waitresult;
+	int			close_rc;
+	int			close_errno = 0;
+
+	/* Like pclose(), wait for the child even if fclose() fails */
+	close_rc = fclose(fp);
+	if (close_rc != 0)
+		close_errno = errno;
+
+	do
+	{
+		waitresult = waitpid(child_pid, &status, 0);
+	} while (waitresult == (pid_t) -1 && errno == EINTR);
+
+	if (waitresult != child_pid)
+		return -1;
+
+	/*
+	 * Like glibc's pclose(), a non-zero exit status takes precedence over an
+	 * fclose() error, which is usually EPIPE because the child exited early.
+	 */
+	if (status == 0 && close_rc != 0)
+	{
+		errno = close_errno;
+		return -1;
+	}
+
+	return status;
+#else
+	(void) child_pid;			/* unused on Windows */
+	return pclose(fp);
+#endif
+}
+
 /*
  * Routines that want to initiate a pipe stream should use OpenPipeStream
  * rather than plain popen().  This lets fd.c deal with freeing FDs if
@@ -2734,7 +3173,7 @@ FILE *
 OpenPipeStream(const char *command, const char *mode)
 {
 	FILE	   *file;
-	int			save_errno;
+	pid_t		child_pid;
 
 	DO_DB(elog(LOG, "OpenPipeStream: Allocated %d (%s)",
 			   numAllocatedDescs, command));
@@ -2750,26 +3189,24 @@ OpenPipeStream(const char *command, const char *mode)
 	ReleaseLruFiles();
 
 TryAgain:
-	fflush(NULL);
-	pqsignal(SIGPIPE, PG_SIG_DFL);
 	errno = 0;
-	file = popen(command, mode);
-	save_errno = errno;
-	pqsignal(SIGPIPE, PG_SIG_IGN);
-	errno = save_errno;
+	file = pg_popen(command, mode, &child_pid);
 	if (file != NULL)
 	{
 		AllocateDesc *desc = &allocatedDescs[numAllocatedDescs];
 
 		desc->kind = AllocateDescPipe;
-		desc->desc.file = file;
+		desc->desc.pipe.file = file;
+		desc->desc.pipe.pid = child_pid;
 		desc->create_subid = GetCurrentSubTransactionId();
 		numAllocatedDescs++;
-		return desc->desc.file;
+		return desc->desc.pipe.file;
 	}
 
 	if (errno == EMFILE || errno == ENFILE)
 	{
+		int			save_errno = errno;
+
 		ereport(LOG,
 				(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
 				 errmsg("out of file descriptors: %m; release and retry")));
@@ -2798,7 +3235,7 @@ FreeDesc(AllocateDesc *desc)
 			result = fclose(desc->desc.file);
 			break;
 		case AllocateDescPipe:
-			result = pclose(desc->desc.file);
+			result = pg_pclose(desc->desc.pipe.file, desc->desc.pipe.pid);
 			break;
 		case AllocateDescDir:
 			result = closedir(desc->desc.dir);
@@ -3050,7 +3487,7 @@ ClosePipeStream(FILE *file)
 	{
 		AllocateDesc *desc = &allocatedDescs[i];
 
-		if (desc->kind == AllocateDescPipe && desc->desc.file == file)
+		if (desc->kind == AllocateDescPipe && desc->desc.pipe.file == file)
 			return FreeDesc(desc);
 	}
 
diff --git a/src/bin/initdb/t/001_initdb.pl b/src/bin/initdb/t/001_initdb.pl
index 081535a22e4..d4784705fc5 100644
--- a/src/bin/initdb/t/001_initdb.pl
+++ b/src/bin/initdb/t/001_initdb.pl
@@ -131,7 +131,7 @@ if ($ENV{with_icu} eq 'yes')
 		],
 		'option --icu-locale');
 
-	command_like(
+	command_checks_all(
 		[
 			'initdb', '--no-sync',
 			'--auth' => 'trust',
@@ -145,7 +145,9 @@ if ($ENV{with_icu} eq 'yes')
 			'--lc-time' => 'C',
 			"$tempdir/data4"
 		],
-		qr/^\s+default collation:\s+und\n/ms,
+		0,
+		[qr/^\s+default collation:\s+und\n/ms],
+		[],    # stderr may have LOG messages
 		'options --locale-provider=icu --locale=und --lc-*=C');
 
 	command_fails_like(
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 2e5ed6f5693..070a22b6a3e 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -300,6 +300,9 @@
 /* Define to 1 if you have the <pam/pam_appl.h> header file. */
 #undef HAVE_PAM_PAM_APPL_H
 
+/* Define to 1 if you have the `pipe2' function. */
+#undef HAVE_PIPE2
+
 /* Define to 1 if you have the `posix_fadvise' function. */
 #undef HAVE_POSIX_FADVISE
 
diff --git a/src/include/storage/fd.h b/src/include/storage/fd.h
index 8ac466fd346..2111ec0ce83 100644
--- a/src/include/storage/fd.h
+++ b/src/include/storage/fd.h
@@ -213,6 +213,7 @@ extern int	pg_fsync_writethrough(int fd);
 extern int	pg_fdatasync(int fd);
 extern bool pg_file_exists(const char *name);
 extern void pg_flush_data(int fd, pgoff_t offset, pgoff_t nbytes);
+extern int	pg_system(const char *command);
 extern int	pg_truncate(const char *path, pgoff_t length);
 extern void fsync_fname(const char *fname, bool isdir);
 extern int	fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel);
-- 
2.56.1

