From 2bf0292a6f63d3fcdaec341f8c07a4157286ebca Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Mon, 19 Aug 2019 13:47:43 +0900
Subject: [PATCH v2 1/2] Add ParallelSlotsMax()

This fixes a compilation failure on OpenBSD.
---
 src/bin/scripts/reindexdb.c        |  4 ++--
 src/bin/scripts/scripts_parallel.c | 14 ++++++++++++++
 src/bin/scripts/scripts_parallel.h |  2 ++
 src/bin/scripts/vacuumdb.c         |  4 ++--
 4 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c
index 14a4f4a91c..f93f7dd5fe 100644
--- a/src/bin/scripts/reindexdb.c
+++ b/src/bin/scripts/reindexdb.c
@@ -153,10 +153,10 @@ main(int argc, char *argv[])
 					pg_log_error("number of parallel jobs must be at least 1");
 					exit(1);
 				}
-				if (concurrentCons > FD_SETSIZE - 1)
+				if (concurrentCons > ParallelSlotsMax())
 				{
 					pg_log_error("too many parallel jobs requested (maximum: %d)",
-								 FD_SETSIZE - 1);
+								 ParallelSlotsMax());
 					exit(1);
 				}
 				break;
diff --git a/src/bin/scripts/scripts_parallel.c b/src/bin/scripts/scripts_parallel.c
index ffdc4e49ef..2b571a470e 100644
--- a/src/bin/scripts/scripts_parallel.c
+++ b/src/bin/scripts/scripts_parallel.c
@@ -94,6 +94,20 @@ select_loop(int maxFd, fd_set *workerset, bool *aborting)
 	return i;
 }
 
+/*
+ * ParallelSlotsMax
+ *		Returns the maximum number of parallel slots supported.
+ *
+ * Note that this is included here as FD_SETSIZE is declared in sys/select.h
+ * by POSIX.
+ */
+int
+ParallelSlotsMax(void)
+{
+	/* leave some room for existing fds */
+	return FD_SETSIZE - 10;
+}
+
 /*
  * ParallelSlotsGetIdle
  *		Return a connection slot that is ready to execute a command.
diff --git a/src/bin/scripts/scripts_parallel.h b/src/bin/scripts/scripts_parallel.h
index ab82c5e6a9..dd625577c6 100644
--- a/src/bin/scripts/scripts_parallel.h
+++ b/src/bin/scripts/scripts_parallel.h
@@ -21,6 +21,8 @@ typedef struct ParallelSlot
 	bool		isFree;			/* Is it known to be idle? */
 } ParallelSlot;
 
+extern int ParallelSlotsMax(void);
+
 extern ParallelSlot *ParallelSlotsGetIdle(ParallelSlot *slots, int numslots);
 
 extern ParallelSlot *ParallelSlotsSetup(const char *dbname, const char *host,
diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c
index d81bfa3a6b..c5c38692ed 100644
--- a/src/bin/scripts/vacuumdb.c
+++ b/src/bin/scripts/vacuumdb.c
@@ -181,10 +181,10 @@ main(int argc, char *argv[])
 					pg_log_error("number of parallel jobs must be at least 1");
 					exit(1);
 				}
-				if (concurrentCons > FD_SETSIZE - 1)
+				if (concurrentCons > ParallelSlotsMax())
 				{
 					pg_log_error("too many parallel jobs requested (maximum: %d)",
-								 FD_SETSIZE - 1);
+								 ParallelSlotsMax());
 					exit(1);
 				}
 				break;
-- 
2.23.0.rc1

