Move some code from src/bin/scripts to src/fe_utils to permit reuse.
authorRobert Haas <rhaas@postgresql.org>
Fri, 5 Feb 2021 18:33:38 +0000 (13:33 -0500)
committerRobert Haas <rhaas@postgresql.org>
Fri, 5 Feb 2021 18:33:38 +0000 (13:33 -0500)
The parallel slots infrastructure (which implements client-side
multiplexing of server connections doing similar things, not
threading or multiple processes or anything like that) are moved from
src/bin/scripts/scripts_parallel.c to src/fe_utils/parallel_slot.c.

The functions consumeQueryResult() and processQueryResult() which were
previously part of src/bin/scripts/common.c are now moved into that
file as well, becoming static helper functions. This might need to be
changed in the future, but currently they're not used for anything
else.

Some other functions from src/bin/scripts/common.c are moved to to
src/fe_utils and are split up among several files.  connectDatabase(),
connectMaintenanceDatabase(), and disconnectDatabase() are moved to
connect_utils.c.  executeQuery(), executeCommand(), and
executeMaintenanceCommand() are move to query_utils.c.
handle_help_version_opts() is moved to option_utils.c.

Mark Dilger, reviewed by me. The larger patch series of which this is
a part has also had review from Peter Geoghegan, Andres Freund, Álvaro
Herrera, Michael Paquier, and Amul Sul, but I don't know whether any
of them have reviewed this bit specifically.

Discussion: http://postgr.es/m/12ED3DA8-25F0-4B68-937D-D907CFBF08E7@enterprisedb.com
Discussion: http://postgr.es/m/5F743835-3399-419C-8324-2D424237E999@enterprisedb.com
Discussion: http://postgr.es/m/70655DF3-33CE-4527-9A4D-DDEB582B6BA0@enterprisedb.com

22 files changed:
src/bin/scripts/Makefile
src/bin/scripts/clusterdb.c
src/bin/scripts/common.c
src/bin/scripts/common.h
src/bin/scripts/createdb.c
src/bin/scripts/createuser.c
src/bin/scripts/dropdb.c
src/bin/scripts/dropuser.c
src/bin/scripts/nls.mk
src/bin/scripts/pg_isready.c
src/bin/scripts/reindexdb.c
src/bin/scripts/vacuumdb.c
src/fe_utils/Makefile
src/fe_utils/connect_utils.c [new file with mode: 0644]
src/fe_utils/option_utils.c [new file with mode: 0644]
src/fe_utils/parallel_slot.c [moved from src/bin/scripts/scripts_parallel.c with 80% similarity]
src/fe_utils/query_utils.c [new file with mode: 0644]
src/include/fe_utils/connect_utils.h [new file with mode: 0644]
src/include/fe_utils/option_utils.h [new file with mode: 0644]
src/include/fe_utils/parallel_slot.h [moved from src/bin/scripts/scripts_parallel.h with 82% similarity]
src/include/fe_utils/query_utils.h [new file with mode: 0644]
src/tools/msvc/Mkvcbuild.pm

index a02e4e430c87ba8f31e319182231ae6ea7d4b9cc..b8d7cf2f2d85d589488c39fc2af3282bb6bb4545 100644 (file)
@@ -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:
index 7d25bb31d4e3591f229527fbe8d08f69416d4b66..fc771eed77bcc4c11a69eb43ffa93dc75cf16a6c 100644 (file)
@@ -13,6 +13,8 @@
 #include "common.h"
 #include "common/logging.h"
 #include "fe_utils/cancel.h"
+#include "fe_utils/option_utils.h"
+#include "fe_utils/query_utils.h"
 #include "fe_utils/simple_list.h"
 #include "fe_utils/string_utils.h"
 
index 21ef297e6eb658b55e28ff709b289f8d614815f9..c86c19eae28b3ca446c92e2deb1567e290b2b529 100644 (file)
 #include "common/logging.h"
 #include "common/string.h"
 #include "fe_utils/cancel.h"
+#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.
- */
-void
-handle_help_version_opts(int argc, char *argv[],
-                        const char *fixed_progname, help_handler hlp)
-{
-   if (argc > 1)
-   {
-       if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
-       {
-           hlp(get_progname(argv[0]));
-           exit(0);
-       }
-       if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
-       {
-           printf("%s (PostgreSQL) " PG_VERSION "\n", fixed_progname);
-           exit(0);
-       }
-   }
-}
-
-
-/*
- * Make a database connection with the given parameters.
- *
- * An interactive password prompt is automatically issued if needed and
- * allowed by cparams->prompt_password.
- *
- * If allow_password_reuse is true, we will try to re-use any password
- * given during previous calls to this routine.  (Callers should not pass
- * allow_password_reuse=true unless reconnecting to the same database+user
- * as before, else we might create password exposure hazards.)
- */
-PGconn *
-connectDatabase(const ConnParams *cparams, const char *progname,
-               bool echo, bool fail_ok, bool allow_password_reuse)
-{
-   PGconn     *conn;
-   bool        new_pass;
-   static char *password = NULL;
-
-   /* Callers must supply at least dbname; other params can be NULL */
-   Assert(cparams->dbname);
-
-   if (!allow_password_reuse && password)
-   {
-       free(password);
-       password = NULL;
-   }
-
-   if (cparams->prompt_password == TRI_YES && password == NULL)
-       password = simple_prompt("Password: ", false);
-
-   /*
-    * Start the connection.  Loop until we have a password if requested by
-    * backend.
-    */
-   do
-   {
-       const char *keywords[8];
-       const char *values[8];
-       int         i = 0;
-
-       /*
-        * If dbname is a connstring, its entries can override the other
-        * values obtained from cparams; but in turn, override_dbname can
-        * override the dbname component of it.
-        */
-       keywords[i] = "host";
-       values[i++] = cparams->pghost;
-       keywords[i] = "port";
-       values[i++] = cparams->pgport;
-       keywords[i] = "user";
-       values[i++] = cparams->pguser;
-       keywords[i] = "password";
-       values[i++] = password;
-       keywords[i] = "dbname";
-       values[i++] = cparams->dbname;
-       if (cparams->override_dbname)
-       {
-           keywords[i] = "dbname";
-           values[i++] = cparams->override_dbname;
-       }
-       keywords[i] = "fallback_application_name";
-       values[i++] = progname;
-       keywords[i] = NULL;
-       values[i++] = NULL;
-       Assert(i <= lengthof(keywords));
-
-       new_pass = false;
-       conn = PQconnectdbParams(keywords, values, true);
-
-       if (!conn)
-       {
-           pg_log_error("could not connect to database %s: out of memory",
-                        cparams->dbname);
-           exit(1);
-       }
-
-       /*
-        * No luck?  Trying asking (again) for a password.
-        */
-       if (PQstatus(conn) == CONNECTION_BAD &&
-           PQconnectionNeedsPassword(conn) &&
-           cparams->prompt_password != TRI_NO)
-       {
-           PQfinish(conn);
-           if (password)
-               free(password);
-           password = simple_prompt("Password: ", false);
-           new_pass = true;
-       }
-   } while (new_pass);
-
-   /* check to see that the backend connection was successfully made */
-   if (PQstatus(conn) == CONNECTION_BAD)
-   {
-       if (fail_ok)
-       {
-           PQfinish(conn);
-           return NULL;
-       }
-       pg_log_error("%s", PQerrorMessage(conn));
-       exit(1);
-   }
-
-   /* Start strict; callers may override this. */
-   PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL, echo));
-
-   return conn;
-}
-
-/*
- * Try to connect to the appropriate maintenance database.
- *
- * This differs from connectDatabase only in that it has a rule for
- * inserting a default "dbname" if none was given (which is why cparams
- * is not const).  Note that cparams->dbname should typically come from
- * a --maintenance-db command line parameter.
- */
-PGconn *
-connectMaintenanceDatabase(ConnParams *cparams,
-                          const char *progname, bool echo)
-{
-   PGconn     *conn;
-
-   /* If a maintenance database name was specified, just connect to it. */
-   if (cparams->dbname)
-       return connectDatabase(cparams, progname, echo, false, false);
-
-   /* Otherwise, try postgres first and then template1. */
-   cparams->dbname = "postgres";
-   conn = connectDatabase(cparams, progname, echo, true, false);
-   if (!conn)
-   {
-       cparams->dbname = "template1";
-       conn = connectDatabase(cparams, progname, echo, false, false);
-   }
-   return conn;
-}
-
-/*
- * Disconnect the given connection, canceling any statement if one is active.
- */
-void
-disconnectDatabase(PGconn *conn)
-{
-   char        errbuf[256];
-
-   Assert(conn != NULL);
-
-   if (PQtransactionStatus(conn) == PQTRANS_ACTIVE)
-   {
-       PGcancel   *cancel;
-
-       if ((cancel = PQgetCancel(conn)))
-       {
-           (void) PQcancel(cancel, errbuf, sizeof(errbuf));
-           PQfreeCancel(cancel);
-       }
-   }
-
-   PQfinish(conn);
-}
-
-/*
- * Run a query, return the results, exit program on failure.
- */
-PGresult *
-executeQuery(PGconn *conn, const char *query, bool echo)
-{
-   PGresult   *res;
-
-   if (echo)
-       printf("%s\n", query);
-
-   res = PQexec(conn, query);
-   if (!res ||
-       PQresultStatus(res) != PGRES_TUPLES_OK)
-   {
-       pg_log_error("query failed: %s", PQerrorMessage(conn));
-       pg_log_info("query was: %s", query);
-       PQfinish(conn);
-       exit(1);
-   }
-
-   return res;
-}
-
-
-/*
- * As above for a SQL command (which returns nothing).
- */
-void
-executeCommand(PGconn *conn, const char *query, bool echo)
-{
-   PGresult   *res;
-
-   if (echo)
-       printf("%s\n", query);
-
-   res = PQexec(conn, query);
-   if (!res ||
-       PQresultStatus(res) != PGRES_COMMAND_OK)
-   {
-       pg_log_error("query failed: %s", PQerrorMessage(conn));
-       pg_log_info("query was: %s", query);
-       PQfinish(conn);
-       exit(1);
-   }
-
-   PQclear(res);
-}
-
-
-/*
- * As above for a SQL maintenance command (returns command success).
- * Command is executed with a cancel handler set, so Ctrl-C can
- * interrupt it.
- */
-bool
-executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
-{
-   PGresult   *res;
-   bool        r;
-
-   if (echo)
-       printf("%s\n", query);
-
-   SetCancelConn(conn);
-   res = PQexec(conn, query);
-   ResetCancelConn();
-
-   r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
-
-   if (res)
-       PQclear(res);
-
-   return r;
-}
-
-/*
- * 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
  * finish using them, pg_free(*table).  *columns is a pointer into "spec",
index 563097571241648e32bded42e4f51c31df1d792c..ddd8f352749e71a84dc68ee5fd2367d3cfbb4c42 100644 (file)
 #define COMMON_H
 
 #include "common/username.h"
+#include "fe_utils/connect_utils.h"
 #include "getopt_long.h"       /* pgrminclude ignore */
 #include "libpq-fe.h"
 #include "pqexpbuffer.h"       /* pgrminclude ignore */
 
-enum trivalue
-{
-   TRI_DEFAULT,
-   TRI_NO,
-   TRI_YES
-};
-
-/* Parameters needed by connectDatabase/connectMaintenanceDatabase */
-typedef struct _connParams
-{
-   /* These fields record the actual command line parameters */
-   const char *dbname;         /* this may be a connstring! */
-   const char *pghost;
-   const char *pgport;
-   const char *pguser;
-   enum trivalue prompt_password;
-   /* If not NULL, this overrides the dbname obtained from command line */
-   /* (but *only* the DB name, not anything else in the connstring) */
-   const char *override_dbname;
-} ConnParams;
-
-typedef void (*help_handler) (const char *progname);
-
-extern void handle_help_version_opts(int argc, char *argv[],
-                                    const char *fixed_progname,
-                                    help_handler hlp);
-
-extern PGconn *connectDatabase(const ConnParams *cparams,
-                              const char *progname,
-                              bool echo, bool fail_ok,
-                              bool allow_password_reuse);
-
-extern PGconn *connectMaintenanceDatabase(ConnParams *cparams,
-                                         const char *progname, bool echo);
-
-extern void disconnectDatabase(PGconn *conn);
-
-extern PGresult *executeQuery(PGconn *conn, const char *query, bool echo);
-
-extern void executeCommand(PGconn *conn, const char *query, bool echo);
-
-extern bool executeMaintenanceCommand(PGconn *conn, const char *query,
-                                     bool echo);
-
-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);
 
index abf21d49428676459c748469f0b957e8345e750c..041454f075fa3f5c7fe94071fe0c64ed13cd5b93 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "common.h"
 #include "common/logging.h"
+#include "fe_utils/option_utils.h"
 #include "fe_utils/string_utils.h"
 
 
index 47b0e28bc6755e8ca7dd72b1f002eba5f98f7536..ef7e0e549fbf2c16a75838185caa31ea12802387 100644 (file)
@@ -14,6 +14,7 @@
 #include "common.h"
 #include "common/logging.h"
 #include "common/string.h"
+#include "fe_utils/option_utils.h"
 #include "fe_utils/simple_list.h"
 #include "fe_utils/string_utils.h"
 
index ba0dcdecb95f59809c776bc7ec1c6219bb07ba56..b154ed1bb6dcfa0e484e1e6b5d4e76637bf0b0cf 100644 (file)
@@ -13,6 +13,7 @@
 #include "postgres_fe.h"
 #include "common.h"
 #include "common/logging.h"
+#include "fe_utils/option_utils.h"
 #include "fe_utils/string_utils.h"
 
 
index ff5b455ae5119a0f087e7c53b16041573c90ca3e..61b8557bc7e940412297a32fb414fed54a47ecfb 100644 (file)
@@ -14,6 +14,7 @@
 #include "common.h"
 #include "common/logging.h"
 #include "common/string.h"
+#include "fe_utils/option_utils.h"
 #include "fe_utils/string_utils.h"
 
 
index 5d5dd11b7be6496f193b4e09778524e4e864c577..7fc716092e73a7bb074f7e75cfc03dcadd1bccdb 100644 (file)
@@ -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
index ceb8a09b4c5b4e181feac65146dd734db55b1e76..fc6f7b0a9391b40d37c8b23160c244d781719a3f 100644 (file)
@@ -12,6 +12,7 @@
 #include "postgres_fe.h"
 #include "common.h"
 #include "common/logging.h"
+#include "fe_utils/option_utils.h"
 
 #define DEFAULT_CONNECT_TIMEOUT "3"
 
index dece8200fa2d2134677d27132fda0f4a4ddb34e9..7781fb1151ab4942ced4f1a5b7466556a10f21f7 100644 (file)
 #include "common/connect.h"
 #include "common/logging.h"
 #include "fe_utils/cancel.h"
+#include "fe_utils/option_utils.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
 {
index 82463277702aa136affdac4660265adbf1e9a057..ed320817bc4feac6e90cac8f2bdaff6572d91810 100644 (file)
 #include "common/connect.h"
 #include "common/logging.h"
 #include "fe_utils/cancel.h"
+#include "fe_utils/option_utils.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 */
index 10d6838cf96a13b240bc631058cdb3a523d08ed5..456c441a33451b791df8ec309fcded61d66e349c 100644 (file)
@@ -23,9 +23,13 @@ OBJS = \
    archive.o \
    cancel.o \
    conditional.o \
+   connect_utils.o \
    mbprint.o \
+   option_utils.o \
+   parallel_slot.o \
    print.o \
    psqlscan.o \
+   query_utils.o \
    recovery_gen.o \
    simple_list.o \
    string_utils.o
diff --git a/src/fe_utils/connect_utils.c b/src/fe_utils/connect_utils.c
new file mode 100644 (file)
index 0000000..96bb798
--- /dev/null
@@ -0,0 +1,181 @@
+/*-------------------------------------------------------------------------
+ *
+ * Facilities for frontend code to connect to and disconnect from databases.
+ *
+ * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/fe_utils/connect_utils.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres_fe.h"
+
+#include "common/connect.h"
+#include "common/logging.h"
+#include "common/string.h"
+#include "fe_utils/connect_utils.h"
+#include "fe_utils/query_utils.h"
+
+/*
+ * Make a database connection with the given parameters.
+ *
+ * An interactive password prompt is automatically issued if needed and
+ * allowed by cparams->prompt_password.
+ *
+ * If allow_password_reuse is true, we will try to re-use any password
+ * given during previous calls to this routine.  (Callers should not pass
+ * allow_password_reuse=true unless reconnecting to the same database+user
+ * as before, else we might create password exposure hazards.)
+ */
+PGconn *
+connectDatabase(const ConnParams *cparams, const char *progname,
+               bool echo, bool fail_ok, bool allow_password_reuse)
+{
+   PGconn     *conn;
+   bool        new_pass;
+   static char *password = NULL;
+
+   /* Callers must supply at least dbname; other params can be NULL */
+   Assert(cparams->dbname);
+
+   if (!allow_password_reuse && password)
+   {
+       free(password);
+       password = NULL;
+   }
+
+   if (cparams->prompt_password == TRI_YES && password == NULL)
+       password = simple_prompt("Password: ", false);
+
+   /*
+    * Start the connection.  Loop until we have a password if requested by
+    * backend.
+    */
+   do
+   {
+       const char *keywords[8];
+       const char *values[8];
+       int         i = 0;
+
+       /*
+        * If dbname is a connstring, its entries can override the other
+        * values obtained from cparams; but in turn, override_dbname can
+        * override the dbname component of it.
+        */
+       keywords[i] = "host";
+       values[i++] = cparams->pghost;
+       keywords[i] = "port";
+       values[i++] = cparams->pgport;
+       keywords[i] = "user";
+       values[i++] = cparams->pguser;
+       keywords[i] = "password";
+       values[i++] = password;
+       keywords[i] = "dbname";
+       values[i++] = cparams->dbname;
+       if (cparams->override_dbname)
+       {
+           keywords[i] = "dbname";
+           values[i++] = cparams->override_dbname;
+       }
+       keywords[i] = "fallback_application_name";
+       values[i++] = progname;
+       keywords[i] = NULL;
+       values[i++] = NULL;
+       Assert(i <= lengthof(keywords));
+
+       new_pass = false;
+       conn = PQconnectdbParams(keywords, values, true);
+
+       if (!conn)
+       {
+           pg_log_error("could not connect to database %s: out of memory",
+                        cparams->dbname);
+           exit(1);
+       }
+
+       /*
+        * No luck?  Trying asking (again) for a password.
+        */
+       if (PQstatus(conn) == CONNECTION_BAD &&
+           PQconnectionNeedsPassword(conn) &&
+           cparams->prompt_password != TRI_NO)
+       {
+           PQfinish(conn);
+           if (password)
+               free(password);
+           password = simple_prompt("Password: ", false);
+           new_pass = true;
+       }
+   } while (new_pass);
+
+   /* check to see that the backend connection was successfully made */
+   if (PQstatus(conn) == CONNECTION_BAD)
+   {
+       if (fail_ok)
+       {
+           PQfinish(conn);
+           return NULL;
+       }
+       pg_log_error("%s", PQerrorMessage(conn));
+       exit(1);
+   }
+
+   /* Start strict; callers may override this. */
+   PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL, echo));
+
+   return conn;
+}
+
+/*
+ * Try to connect to the appropriate maintenance database.
+ *
+ * This differs from connectDatabase only in that it has a rule for
+ * inserting a default "dbname" if none was given (which is why cparams
+ * is not const).  Note that cparams->dbname should typically come from
+ * a --maintenance-db command line parameter.
+ */
+PGconn *
+connectMaintenanceDatabase(ConnParams *cparams,
+                          const char *progname, bool echo)
+{
+   PGconn     *conn;
+
+   /* If a maintenance database name was specified, just connect to it. */
+   if (cparams->dbname)
+       return connectDatabase(cparams, progname, echo, false, false);
+
+   /* Otherwise, try postgres first and then template1. */
+   cparams->dbname = "postgres";
+   conn = connectDatabase(cparams, progname, echo, true, false);
+   if (!conn)
+   {
+       cparams->dbname = "template1";
+       conn = connectDatabase(cparams, progname, echo, false, false);
+   }
+   return conn;
+}
+
+/*
+ * Disconnect the given connection, canceling any statement if one is active.
+ */
+void
+disconnectDatabase(PGconn *conn)
+{
+   char        errbuf[256];
+
+   Assert(conn != NULL);
+
+   if (PQtransactionStatus(conn) == PQTRANS_ACTIVE)
+   {
+       PGcancel   *cancel;
+
+       if ((cancel = PQgetCancel(conn)))
+       {
+           (void) PQcancel(cancel, errbuf, sizeof(errbuf));
+           PQfreeCancel(cancel);
+       }
+   }
+
+   PQfinish(conn);
+}
diff --git a/src/fe_utils/option_utils.c b/src/fe_utils/option_utils.c
new file mode 100644 (file)
index 0000000..e19a495
--- /dev/null
@@ -0,0 +1,38 @@
+/*-------------------------------------------------------------------------
+ *
+ * Command line option processing facilities for frontend code
+ *
+ * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/fe_utils/option_utils.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres_fe.h"
+
+#include "fe_utils/option_utils.h"
+
+/*
+ * Provide strictly harmonized handling of --help and --version
+ * options.
+ */
+void
+handle_help_version_opts(int argc, char *argv[],
+                        const char *fixed_progname, help_handler hlp)
+{
+   if (argc > 1)
+   {
+       if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
+       {
+           hlp(get_progname(argv[0]));
+           exit(0);
+       }
+       if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
+       {
+           printf("%s (PostgreSQL) " PG_VERSION "\n", fixed_progname);
+           exit(0);
+       }
+   }
+}
similarity index 80%
rename from src/bin/scripts/scripts_parallel.c
rename to src/fe_utils/parallel_slot.c
index 1f863a1bb4616e06ed8828b4e9387c4d85b715de..3987a4702b5255a6258d1b4ade4a86cf810b7c23 100644 (file)
@@ -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
  *
  *-------------------------------------------------------------------------
  */
 #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/fe_utils/query_utils.c b/src/fe_utils/query_utils.c
new file mode 100644 (file)
index 0000000..d5ffe56
--- /dev/null
@@ -0,0 +1,92 @@
+/*-------------------------------------------------------------------------
+ *
+ * Facilities for frontend code to query a databases.
+ *
+ * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/fe_utils/query_utils.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres_fe.h"
+
+#include "common/logging.h"
+#include "fe_utils/cancel.h"
+#include "fe_utils/query_utils.h"
+
+/*
+ * Run a query, return the results, exit program on failure.
+ */
+PGresult *
+executeQuery(PGconn *conn, const char *query, bool echo)
+{
+   PGresult   *res;
+
+   if (echo)
+       printf("%s\n", query);
+
+   res = PQexec(conn, query);
+   if (!res ||
+       PQresultStatus(res) != PGRES_TUPLES_OK)
+   {
+       pg_log_error("query failed: %s", PQerrorMessage(conn));
+       pg_log_info("query was: %s", query);
+       PQfinish(conn);
+       exit(1);
+   }
+
+   return res;
+}
+
+
+/*
+ * As above for a SQL command (which returns nothing).
+ */
+void
+executeCommand(PGconn *conn, const char *query, bool echo)
+{
+   PGresult   *res;
+
+   if (echo)
+       printf("%s\n", query);
+
+   res = PQexec(conn, query);
+   if (!res ||
+       PQresultStatus(res) != PGRES_COMMAND_OK)
+   {
+       pg_log_error("query failed: %s", PQerrorMessage(conn));
+       pg_log_info("query was: %s", query);
+       PQfinish(conn);
+       exit(1);
+   }
+
+   PQclear(res);
+}
+
+
+/*
+ * As above for a SQL maintenance command (returns command success).
+ * Command is executed with a cancel handler set, so Ctrl-C can
+ * interrupt it.
+ */
+bool
+executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
+{
+   PGresult   *res;
+   bool        r;
+
+   if (echo)
+       printf("%s\n", query);
+
+   SetCancelConn(conn);
+   res = PQexec(conn, query);
+   ResetCancelConn();
+
+   r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
+
+   if (res)
+       PQclear(res);
+
+   return r;
+}
diff --git a/src/include/fe_utils/connect_utils.h b/src/include/fe_utils/connect_utils.h
new file mode 100644 (file)
index 0000000..5048940
--- /dev/null
@@ -0,0 +1,48 @@
+/*-------------------------------------------------------------------------
+ *
+ * Facilities for frontend code to connect to and disconnect from databases.
+ *
+ * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/fe_utils/connect_utils.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef CONNECT_UTILS_H
+#define CONNECT_UTILS_H
+
+#include "libpq-fe.h"
+
+enum trivalue
+{
+   TRI_DEFAULT,
+   TRI_NO,
+   TRI_YES
+};
+
+/* Parameters needed by connectDatabase/connectMaintenanceDatabase */
+typedef struct _connParams
+{
+   /* These fields record the actual command line parameters */
+   const char *dbname;         /* this may be a connstring! */
+   const char *pghost;
+   const char *pgport;
+   const char *pguser;
+   enum trivalue prompt_password;
+   /* If not NULL, this overrides the dbname obtained from command line */
+   /* (but *only* the DB name, not anything else in the connstring) */
+   const char *override_dbname;
+} ConnParams;
+
+extern PGconn *connectDatabase(const ConnParams *cparams,
+                              const char *progname,
+                              bool echo, bool fail_ok,
+                              bool allow_password_reuse);
+
+extern PGconn *connectMaintenanceDatabase(ConnParams *cparams,
+                                         const char *progname, bool echo);
+
+extern void disconnectDatabase(PGconn *conn);
+
+#endif                         /* CONNECT_UTILS_H */
diff --git a/src/include/fe_utils/option_utils.h b/src/include/fe_utils/option_utils.h
new file mode 100644 (file)
index 0000000..d653cb9
--- /dev/null
@@ -0,0 +1,23 @@
+/*-------------------------------------------------------------------------
+ *
+ * Command line option processing facilities for frontend code
+ *
+ * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/fe_utils/option_utils.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef OPTION_UTILS_H
+#define OPTION_UTILS_H
+
+#include "postgres_fe.h"
+
+typedef void (*help_handler) (const char *progname);
+
+extern void handle_help_version_opts(int argc, char *argv[],
+                                    const char *fixed_progname,
+                                    help_handler hlp);
+
+#endif                         /* OPTION_UTILS_H */
similarity index 82%
rename from src/bin/scripts/scripts_parallel.h
rename to src/include/fe_utils/parallel_slot.h
index f62692510a8a42aba1cb315890dcd4104189e286..99eeb3328d60879cc9185f39de38c689e2ad9e65 100644 (file)
@@ -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 */
diff --git a/src/include/fe_utils/query_utils.h b/src/include/fe_utils/query_utils.h
new file mode 100644 (file)
index 0000000..1099260
--- /dev/null
@@ -0,0 +1,26 @@
+/*-------------------------------------------------------------------------
+ *
+ * Facilities for frontend code to query a databases.
+ *
+ * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/fe_utils/query_utils.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef QUERY_UTILS_H
+#define QUERY_UTILS_H
+
+#include "postgres_fe.h"
+
+#include "libpq-fe.h"
+
+extern PGresult *executeQuery(PGconn *conn, const char *query, bool echo);
+
+extern void executeCommand(PGconn *conn, const char *query, bool echo);
+
+extern bool executeMaintenanceCommand(PGconn *conn, const char *query,
+                                     bool echo);
+
+#endif                         /* QUERY_UTILS_H */
index 90328db04e90d4122227c769bf82d935c8270eb5..49614106dc23a0dcc7ac3f35a391920ffc501cf2 100644 (file)
@@ -147,8 +147,9 @@ sub mkvcbuild
    our @pgcommonbkndfiles = @pgcommonallfiles;
 
    our @pgfeutilsfiles = qw(
-     archive.c cancel.c conditional.c mbprint.c print.c psqlscan.l
-     psqlscan.c simple_list.c string_utils.c recovery_gen.c);
+     archive.c cancel.c conditional.c connect_utils.c mbprint.c option_utils.c
+     parallel_slot.c print.c psqlscan.l psqlscan.c query_utils.c simple_list.c
+     string_utils.c recovery_gen.c);
 
    $libpgport = $solution->AddProject('libpgport', 'lib', 'misc');
    $libpgport->AddDefine('FRONTEND');