From 53ded3d52f792c58ad3550ffb2244cec19981b66 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Fri, 18 May 2018 12:43:40 -0700
Subject: [PATCH v1 5/7] WIP: Allow more transient files and allow to query the
 max.

Author: Andres Freund
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:
---
 src/backend/storage/file/fd.c | 10 ++++++++--
 src/include/storage/fd.h      |  1 +
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index e2492ce94d5..b0db997edc7 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -2299,10 +2299,10 @@ reserveAllocatedDesc(void)
 	 *
 	 * We mustn't let allocated descriptors hog all the available FDs, and in
 	 * practice we'd better leave a reasonable number of FDs for VFD use.  So
-	 * set the maximum to max_safe_fds / 2.  (This should certainly be at
+	 * set the maximum to 80% of max_safe_fds.  (This should certainly be at
 	 * least as large as the initial size, FD_MINFREE / 2.)
 	 */
-	newMax = max_safe_fds / 2;
+	newMax = MaxTransientFiles(); // XXX: more accurate name
 	if (newMax > maxAllocatedDescs)
 	{
 		newDescs = (AllocateDesc *) realloc(allocatedDescs,
@@ -2610,6 +2610,12 @@ CloseTransientFile(int fd)
 	return close(fd);
 }
 
+int
+MaxTransientFiles(void)
+{
+	return (max_safe_fds * 8) / 10;
+}
+
 /*
  * Routines that want to use <dirent.h> (ie, DIR*) should use AllocateDir
  * rather than plain opendir().  This lets fd.c deal with freeing FDs if
diff --git a/src/include/storage/fd.h b/src/include/storage/fd.h
index 9bb32771602..2c3055e77cd 100644
--- a/src/include/storage/fd.h
+++ b/src/include/storage/fd.h
@@ -108,6 +108,7 @@ extern int	OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fil
 extern void ReserveTransientFile(void);
 extern void RegisterTransientFile(int fd);
 extern int	CloseTransientFile(int fd);
+extern int	MaxTransientFiles(void);
 
 /* If you've really really gotta have a plain kernel FD, use this */
 extern int	BasicOpenFile(const char *fileName, int fileFlags);
-- 
2.17.0.rc1.dirty

