From af7d8b679e7e7920aa086b913c3c6ab292f722ed Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Sat, 23 Jan 2021 17:35:51 -0800
Subject: [PATCH v33 4/8] Moving and renaming scripts_parallel

Moving src/bin/scripts/scripts_parallel.[ch] to
fe_utils/parallel_slot.[ch].  During the move, I couldn't resist
removing an unnecessary blank line from the header.  This header
file is about to get changed in the next patch right around where
this blank line is located, and I don't want that change to appear
as a substitution but merely as an addition.

Moving functions consumeQueryResult() and processQueryResult() from
src/bin/scripts/scripts_parallel.[ch] int new file
fe_utils/parallel_slot.c and making them static, since they are used
nowhere else.
---
 src/bin/scripts/Makefile                      |  6 +-
 src/bin/scripts/common.c                      | 53 ----------------
 src/bin/scripts/common.h                      |  4 --
 src/bin/scripts/nls.mk                        |  2 +-
 src/bin/scripts/reindexdb.c                   |  2 +-
 src/bin/scripts/vacuumdb.c                    |  2 +-
 src/fe_utils/Makefile                         |  1 +
 .../parallel_slot.c}                          | 63 +++++++++++++++++--
 .../fe_utils/parallel_slot.h}                 | 13 ++--
 9 files changed, 71 insertions(+), 75 deletions(-)
 rename src/{bin/scripts/scripts_parallel.c => fe_utils/parallel_slot.c} (80%)
 rename src/{bin/scripts/scripts_parallel.h => include/fe_utils/parallel_slot.h} (82%)

diff --git a/src/bin/scripts/Makefile b/src/bin/scripts/Makefile
index a02e4e430c..b8d7cf2f2d 100644
--- a/src/bin/scripts/Makefile
+++ b/src/bin/scripts/Makefile
@@ -28,8 +28,8 @@ createuser: createuser.o common.o $(WIN32RES) | submake-libpq submake-libpgport
 dropdb: dropdb.o common.o $(WIN32RES) | submake-libpq submake-libpgport submake-libpgfeutils
 dropuser: dropuser.o common.o $(WIN32RES) | submake-libpq submake-libpgport submake-libpgfeutils
 clusterdb: clusterdb.o common.o $(WIN32RES) | submake-libpq submake-libpgport submake-libpgfeutils
-vacuumdb: vacuumdb.o common.o scripts_parallel.o $(WIN32RES) | submake-libpq submake-libpgport submake-libpgfeutils
-reindexdb: reindexdb.o common.o scripts_parallel.o $(WIN32RES) | submake-libpq submake-libpgport submake-libpgfeutils
+vacuumdb: vacuumdb.o common.o $(WIN32RES) | submake-libpq submake-libpgport submake-libpgfeutils
+reindexdb: reindexdb.o common.o $(WIN32RES) | submake-libpq submake-libpgport submake-libpgfeutils
 pg_isready: pg_isready.o common.o $(WIN32RES) | submake-libpq submake-libpgport submake-libpgfeutils
 
 install: all installdirs
@@ -50,7 +50,7 @@ uninstall:
 
 clean distclean maintainer-clean:
 	rm -f $(addsuffix $(X), $(PROGRAMS)) $(addsuffix .o, $(PROGRAMS))
-	rm -f common.o scripts_parallel.o $(WIN32RES)
+	rm -f common.o $(WIN32RES)
 	rm -rf tmp_check
 
 check:
diff --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c
index 62645fd276..c7fdd3adcb 100644
--- a/src/bin/scripts/common.c
+++ b/src/bin/scripts/common.c
@@ -25,8 +25,6 @@
 #include "fe_utils/query_utils.h"
 #include "fe_utils/string_utils.h"
 
-#define ERRCODE_UNDEFINED_TABLE  "42P01"
-
 /*
  * Provide strictly harmonized handling of --help and --version
  * options.
@@ -50,57 +48,6 @@ handle_help_version_opts(int argc, char *argv[],
 	}
 }
 
-/*
- * Consume all the results generated for the given connection until
- * nothing remains.  If at least one error is encountered, return false.
- * Note that this will block if the connection is busy.
- */
-bool
-consumeQueryResult(PGconn *conn)
-{
-	bool		ok = true;
-	PGresult   *result;
-
-	SetCancelConn(conn);
-	while ((result = PQgetResult(conn)) != NULL)
-	{
-		if (!processQueryResult(conn, result))
-			ok = false;
-	}
-	ResetCancelConn();
-	return ok;
-}
-
-/*
- * Process (and delete) a query result.  Returns true if there's no error,
- * false otherwise -- but errors about trying to work on a missing relation
- * are reported and subsequently ignored.
- */
-bool
-processQueryResult(PGconn *conn, PGresult *result)
-{
-	/*
-	 * If it's an error, report it.  Errors about a missing table are harmless
-	 * so we continue processing; but die for other errors.
-	 */
-	if (PQresultStatus(result) != PGRES_COMMAND_OK)
-	{
-		char	   *sqlState = PQresultErrorField(result, PG_DIAG_SQLSTATE);
-
-		pg_log_error("processing of database \"%s\" failed: %s",
-					 PQdb(conn), PQerrorMessage(conn));
-
-		if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0)
-		{
-			PQclear(result);
-			return false;
-		}
-	}
-
-	PQclear(result);
-	return true;
-}
-
 
 /*
  * Split TABLE[(COLUMNS)] into TABLE and [(COLUMNS)] portions.  When you
diff --git a/src/bin/scripts/common.h b/src/bin/scripts/common.h
index ae19c420df..54e6575a7b 100644
--- a/src/bin/scripts/common.h
+++ b/src/bin/scripts/common.h
@@ -21,10 +21,6 @@ extern void handle_help_version_opts(int argc, char *argv[],
 									 const char *fixed_progname,
 									 help_handler hlp);
 
-extern bool consumeQueryResult(PGconn *conn);
-
-extern bool processQueryResult(PGconn *conn, PGresult *result);
-
 extern void splitTableColumnsSpec(const char *spec, int encoding,
 								  char **table, const char **columns);
 
diff --git a/src/bin/scripts/nls.mk b/src/bin/scripts/nls.mk
index 5d5dd11b7b..7fc716092e 100644
--- a/src/bin/scripts/nls.mk
+++ b/src/bin/scripts/nls.mk
@@ -7,7 +7,7 @@ GETTEXT_FILES    = $(FRONTEND_COMMON_GETTEXT_FILES) \
                    clusterdb.c vacuumdb.c reindexdb.c \
                    pg_isready.c \
                    common.c \
-                   scripts_parallel.c \
+                   ../../fe_utils/parallel_slot.c \
                    ../../fe_utils/cancel.c ../../fe_utils/print.c \
                    ../../common/fe_memutils.c ../../common/username.c
 GETTEXT_TRIGGERS = $(FRONTEND_COMMON_GETTEXT_TRIGGERS) simple_prompt yesno_prompt
diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c
index c9289ae78d..b03c94f35f 100644
--- a/src/bin/scripts/reindexdb.c
+++ b/src/bin/scripts/reindexdb.c
@@ -16,10 +16,10 @@
 #include "common/connect.h"
 #include "common/logging.h"
 #include "fe_utils/cancel.h"
+#include "fe_utils/parallel_slot.h"
 #include "fe_utils/query_utils.h"
 #include "fe_utils/simple_list.h"
 #include "fe_utils/string_utils.h"
-#include "scripts_parallel.h"
 
 typedef enum ReindexType
 {
diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c
index 55c974ff6d..a4f5d545a7 100644
--- a/src/bin/scripts/vacuumdb.c
+++ b/src/bin/scripts/vacuumdb.c
@@ -18,10 +18,10 @@
 #include "common/connect.h"
 #include "common/logging.h"
 #include "fe_utils/cancel.h"
+#include "fe_utils/parallel_slot.h"
 #include "fe_utils/query_utils.h"
 #include "fe_utils/simple_list.h"
 #include "fe_utils/string_utils.h"
-#include "scripts_parallel.h"
 
 
 /* vacuum options controlled by user flags */
diff --git a/src/fe_utils/Makefile b/src/fe_utils/Makefile
index 9ddf324584..bd499e6045 100644
--- a/src/fe_utils/Makefile
+++ b/src/fe_utils/Makefile
@@ -26,6 +26,7 @@ OBJS = \
 	connect_utils.o \
 	exit_utils.o \
 	mbprint.o \
+	parallel_slot.o \
 	pgreshandler.o \
 	print.o \
 	psqlscan.o \
diff --git a/src/bin/scripts/scripts_parallel.c b/src/fe_utils/parallel_slot.c
similarity index 80%
rename from src/bin/scripts/scripts_parallel.c
rename to src/fe_utils/parallel_slot.c
index 1f863a1bb4..3987a4702b 100644
--- a/src/bin/scripts/scripts_parallel.c
+++ b/src/fe_utils/parallel_slot.c
@@ -1,13 +1,13 @@
 /*-------------------------------------------------------------------------
  *
- *	scripts_parallel.c
- *		Parallel support for bin/scripts/
+ *	parallel_slot.c
+ *		Parallel support for front-end parallel database connections
  *
  *
  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * src/bin/scripts/scripts_parallel.c
+ * src/fe_utils/parallel_slot.c
  *
  *-------------------------------------------------------------------------
  */
@@ -22,13 +22,15 @@
 #include <sys/select.h>
 #endif
 
-#include "common.h"
 #include "common/logging.h"
 #include "fe_utils/cancel.h"
-#include "scripts_parallel.h"
+#include "fe_utils/parallel_slot.h"
+
+#define ERRCODE_UNDEFINED_TABLE  "42P01"
 
 static void init_slot(ParallelSlot *slot, PGconn *conn);
 static int	select_loop(int maxFd, fd_set *workerset);
+static bool processQueryResult(PGconn *conn, PGresult *result);
 
 static void
 init_slot(ParallelSlot *slot, PGconn *conn)
@@ -38,6 +40,57 @@ init_slot(ParallelSlot *slot, PGconn *conn)
 	slot->isFree = true;
 }
 
+/*
+ * Process (and delete) a query result.  Returns true if there's no error,
+ * false otherwise -- but errors about trying to work on a missing relation
+ * are reported and subsequently ignored.
+ */
+static bool
+processQueryResult(PGconn *conn, PGresult *result)
+{
+	/*
+	 * If it's an error, report it.  Errors about a missing table are harmless
+	 * so we continue processing; but die for other errors.
+	 */
+	if (PQresultStatus(result) != PGRES_COMMAND_OK)
+	{
+		char	   *sqlState = PQresultErrorField(result, PG_DIAG_SQLSTATE);
+
+		pg_log_error("processing of database \"%s\" failed: %s",
+					 PQdb(conn), PQerrorMessage(conn));
+
+		if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0)
+		{
+			PQclear(result);
+			return false;
+		}
+	}
+
+	PQclear(result);
+	return true;
+}
+
+/*
+ * Consume all the results generated for the given connection until
+ * nothing remains.  If at least one error is encountered, return false.
+ * Note that this will block if the connection is busy.
+ */
+static bool
+consumeQueryResult(PGconn *conn)
+{
+	bool		ok = true;
+	PGresult   *result;
+
+	SetCancelConn(conn);
+	while ((result = PQgetResult(conn)) != NULL)
+	{
+		if (!processQueryResult(conn, result))
+			ok = false;
+	}
+	ResetCancelConn();
+	return ok;
+}
+
 /*
  * Wait until a file descriptor from the given set becomes readable.
  *
diff --git a/src/bin/scripts/scripts_parallel.h b/src/include/fe_utils/parallel_slot.h
similarity index 82%
rename from src/bin/scripts/scripts_parallel.h
rename to src/include/fe_utils/parallel_slot.h
index f62692510a..99eeb3328d 100644
--- a/src/bin/scripts/scripts_parallel.h
+++ b/src/include/fe_utils/parallel_slot.h
@@ -1,21 +1,20 @@
 /*-------------------------------------------------------------------------
  *
- *	scripts_parallel.h
+ *	parallel_slot.h
  *		Parallel support for bin/scripts/
  *
  *	Copyright (c) 2003-2021, PostgreSQL Global Development Group
  *
- *	src/bin/scripts/scripts_parallel.h
+ *	src/include/fe_utils/parallel_slot.h
  *
  *-------------------------------------------------------------------------
  */
-#ifndef SCRIPTS_PARALLEL_H
-#define SCRIPTS_PARALLEL_H
+#ifndef PARALLEL_SLOT_H
+#define PARALLEL_SLOT_H
 
-#include "common.h"
+#include "fe_utils/connect_utils.h"
 #include "libpq-fe.h"
 
-
 typedef struct ParallelSlot
 {
 	PGconn	   *connection;		/* One connection */
@@ -33,4 +32,4 @@ extern void ParallelSlotsTerminate(ParallelSlot *slots, int numslots);
 extern bool ParallelSlotsWaitCompletion(ParallelSlot *slots, int numslots);
 
 
-#endif							/* SCRIPTS_PARALLEL_H */
+#endif							/* PARALLEL_SLOT_H */
-- 
2.21.1 (Apple Git-122.3)

