mirror of https://github.com/postgres/postgres
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.compull/61/head
parent
c444472af5
commit
e955bd4b6c
@ -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); |
||||
} |
@ -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); |
||||
} |
||||
} |
||||
} |
@ -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; |
||||
} |
@ -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 */ |
@ -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 */ |
@ -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 */ |
Loading…
Reference in new issue