From 5762a4d9098ac0cba789ddd26286ac85c2d316f2 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Thu, 29 Mar 2012 08:19:11 +0300
Subject: [PATCH] Inherit max_safe_fds to child processes in EXEC_BACKEND mode.

Postmaster sets max_safe_fds by testing how many open file descriptors it
can open, and that is normally inherited by all child processes at fork().
Not so on EXEC_BACKEND, ie. Windows, however. Because of that, we
effectively ignored max_files_per_process on Windows, and always assumed
a conservative default of 32 simultaneous open files. That could have an
impact on performance, if you need to access a lot of different files
in a query. After this patch, the value is passed to child processes by
save/restore_backend_variables() among many other global variables.

It has been like this forever, but given the lack of complaints about it,
I'm not backpatching this.
---
 src/backend/postmaster/postmaster.c | 3 +++
 src/backend/storage/file/fd.c       | 2 +-
 src/include/storage/fd.h            | 5 +++++
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 5d7888ade18..1dac6954885 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -438,6 +438,7 @@ typedef struct
 	TimestampTz PgReloadTime;
 	bool		redirection_done;
 	bool		IsBinaryUpgrade;
+	int			max_safe_fds;
 #ifdef WIN32
 	HANDLE		PostmasterHandle;
 	HANDLE		initial_signal_pipe;
@@ -4741,6 +4742,7 @@ save_backend_variables(BackendParameters *param, Port *port,
 
 	param->redirection_done = redirection_done;
 	param->IsBinaryUpgrade = IsBinaryUpgrade;
+	param->max_safe_fds = max_safe_fds;
 
 #ifdef WIN32
 	param->PostmasterHandle = PostmasterHandle;
@@ -4964,6 +4966,7 @@ restore_backend_variables(BackendParameters *param, Port *port)
 
 	redirection_done = param->redirection_done;
 	IsBinaryUpgrade = param->IsBinaryUpgrade;
+	max_safe_fds = param->max_safe_fds;
 
 #ifdef WIN32
 	PostmasterHandle = param->PostmasterHandle;
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index 8f442f0a7f8..fa376ae4bb3 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -103,7 +103,7 @@ int			max_files_per_process = 1000;
  * Note: the value of max_files_per_process is taken into account while
  * setting this variable, and so need not be tested separately.
  */
-static int	max_safe_fds = 32;	/* default if not changed */
+int			max_safe_fds = 32;	/* default if not changed */
 
 
 /* Debugging.... */
diff --git a/src/include/storage/fd.h b/src/include/storage/fd.h
index 22e7fe89efe..bad9f10c62e 100644
--- a/src/include/storage/fd.h
+++ b/src/include/storage/fd.h
@@ -53,6 +53,11 @@ typedef int File;
 /* GUC parameter */
 extern int	max_files_per_process;
 
+/*
+ * This is private to fd.c, but exported for save/restore_backend_variables()
+ */
+extern int	max_safe_fds;
+
 
 /*
  * prototypes for functions in fd.c
-- 
GitLab