diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2020-10-19 19:03:47 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2020-10-19 19:03:47 -0400 |
commit | 5c78f797701b90546279b994791fc07af5bbb5c9 (patch) | |
tree | 15c5d847fcfc69bd1ad740bd9685d1630290c7f3 /src | |
parent | 67a6af69870d1c95e6e78295849fee807ba5691b (diff) |
Fix connection string handling in src/bin/scripts/ programs.
When told to process all databases, clusterdb, reindexdb, and vacuumdb
would reconnect by replacing their --maintenance-db parameter with the
name of the target database. If that parameter is a connstring (which
has been allowed for a long time, though we failed to document that
before this patch), we'd lose any other options it might specify, for
example SSL or GSS parameters, possibly resulting in failure to connect.
Thus, this is the same bug as commit a45bc8a4f fixed in pg_dump and
pg_restore. We can fix it in the same way, by using libpq's rules for
handling multiple "dbname" parameters to add the target database name
separately. I chose to apply the same refactoring approach as in that
patch, with a struct to handle the command line parameters that need to
be passed through to connectDatabase. (Maybe someday we can unify the
very similar functions here and in pg_dump/pg_restore.)
Per Peter Eisentraut's comments on bug #16604. Back-patch to all
supported branches.
Discussion: https://postgr.es/m/16604-933f4b8791227b15@postgresql.org
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/scripts/clusterdb.c | 67 | ||||
-rw-r--r-- | src/bin/scripts/common.c | 89 | ||||
-rw-r--r-- | src/bin/scripts/common.h | 26 | ||||
-rw-r--r-- | src/bin/scripts/createdb.c | 11 | ||||
-rw-r--r-- | src/bin/scripts/createlang.c | 12 | ||||
-rw-r--r-- | src/bin/scripts/createuser.c | 11 | ||||
-rw-r--r-- | src/bin/scripts/dropdb.c | 12 | ||||
-rw-r--r-- | src/bin/scripts/droplang.c | 12 | ||||
-rw-r--r-- | src/bin/scripts/dropuser.c | 13 | ||||
-rw-r--r-- | src/bin/scripts/reindexdb.c | 85 | ||||
-rw-r--r-- | src/bin/scripts/vacuumdb.c | 70 |
11 files changed, 229 insertions, 179 deletions
diff --git a/src/bin/scripts/clusterdb.c b/src/bin/scripts/clusterdb.c index ee28dd142d2..3af6b4938e2 100644 --- a/src/bin/scripts/clusterdb.c +++ b/src/bin/scripts/clusterdb.c @@ -15,15 +15,10 @@ #include "fe_utils/string_utils.h" -static void cluster_one_database(const char *dbname, bool verbose, const char *table, - const char *host, const char *port, - const char *username, enum trivalue prompt_password, - const char *progname, bool echo); -static void cluster_all_databases(bool verbose, const char *maintenance_db, - const char *host, const char *port, - const char *username, enum trivalue prompt_password, - const char *progname, bool echo, bool quiet); - +static void cluster_one_database(const ConnParams *cparams, const char *table, + const char *progname, bool verbose, bool echo); +static void cluster_all_databases(ConnParams *cparams, const char *progname, + bool verbose, bool echo, bool quiet); static void help(const char *progname); @@ -56,6 +51,7 @@ main(int argc, char *argv[]) char *port = NULL; char *username = NULL; enum trivalue prompt_password = TRI_DEFAULT; + ConnParams cparams; bool echo = false; bool quiet = false; bool alldb = false; @@ -131,6 +127,13 @@ main(int argc, char *argv[]) exit(1); } + /* fill cparams except for dbname, which is set below */ + cparams.pghost = host; + cparams.pgport = port; + cparams.pguser = username; + cparams.prompt_password = prompt_password; + cparams.override_dbname = NULL; + setup_cancel_handler(); if (alldb) @@ -149,8 +152,9 @@ main(int argc, char *argv[]) exit(1); } - cluster_all_databases(verbose, maintenance_db, host, port, username, prompt_password, - progname, echo, quiet); + cparams.dbname = maintenance_db; + + cluster_all_databases(&cparams, progname, verbose, echo, quiet); } else { @@ -164,21 +168,21 @@ main(int argc, char *argv[]) dbname = get_user_name_or_exit(progname); } + cparams.dbname = dbname; + if (tables.head != NULL) { SimpleStringListCell *cell; for (cell = tables.head; cell; cell = cell->next) { - cluster_one_database(dbname, verbose, cell->val, - host, port, username, prompt_password, - progname, echo); + cluster_one_database(&cparams, cell->val, + progname, verbose, echo); } } else - cluster_one_database(dbname, verbose, NULL, - host, port, username, prompt_password, - progname, echo); + cluster_one_database(&cparams, NULL, + progname, verbose, echo); } exit(0); @@ -186,17 +190,14 @@ main(int argc, char *argv[]) static void -cluster_one_database(const char *dbname, bool verbose, const char *table, - const char *host, const char *port, - const char *username, enum trivalue prompt_password, - const char *progname, bool echo) +cluster_one_database(const ConnParams *cparams, const char *table, + const char *progname, bool verbose, bool echo) { PQExpBufferData sql; PGconn *conn; - conn = connectDatabase(dbname, host, port, username, prompt_password, - progname, echo, false, false); + conn = connectDatabase(cparams, progname, echo, false, false); initPQExpBuffer(&sql); @@ -227,22 +228,17 @@ cluster_one_database(const char *dbname, bool verbose, const char *table, static void -cluster_all_databases(bool verbose, const char *maintenance_db, - const char *host, const char *port, - const char *username, enum trivalue prompt_password, - const char *progname, bool echo, bool quiet) +cluster_all_databases(ConnParams *cparams, const char *progname, + bool verbose, bool echo, bool quiet) { PGconn *conn; PGresult *result; - PQExpBufferData connstr; int i; - conn = connectMaintenanceDatabase(maintenance_db, host, port, username, - prompt_password, progname, echo); + conn = connectMaintenanceDatabase(cparams, progname, echo); result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo); PQfinish(conn); - initPQExpBuffer(&connstr); for (i = 0; i < PQntuples(result); i++) { char *dbname = PQgetvalue(result, i, 0); @@ -253,15 +249,10 @@ cluster_all_databases(bool verbose, const char *maintenance_db, fflush(stdout); } - resetPQExpBuffer(&connstr); - appendPQExpBuffer(&connstr, "dbname="); - appendConnStrVal(&connstr, dbname); + cparams->override_dbname = dbname; - cluster_one_database(connstr.data, verbose, NULL, - host, port, username, prompt_password, - progname, echo); + cluster_one_database(cparams, NULL, progname, verbose, echo); } - termPQExpBuffer(&connstr); PQclear(result); } diff --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c index f1820e756ca..64bf1f42270 100644 --- a/src/bin/scripts/common.c +++ b/src/bin/scripts/common.c @@ -57,7 +57,7 @@ handle_help_version_opts(int argc, char *argv[], * Make a database connection with the given parameters. * * An interactive password prompt is automatically issued if needed and - * allowed by prompt_password. + * 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 @@ -65,15 +65,16 @@ handle_help_version_opts(int argc, char *argv[], * as before, else we might create password exposure hazards.) */ PGconn * -connectDatabase(const char *dbname, const char *pghost, - const char *pgport, const char *pguser, - enum trivalue prompt_password, const char *progname, +connectDatabase(const ConnParams *cparams, const char *progname, bool echo, bool fail_ok, bool allow_password_reuse) { PGconn *conn; static char *password = NULL; bool new_pass; + /* Callers must supply at least dbname; other params can be NULL */ + Assert(cparams->dbname); + if (!allow_password_reuse) { if (password) @@ -81,7 +82,7 @@ connectDatabase(const char *dbname, const char *pghost, password = NULL; } - if (password == NULL && prompt_password == TRI_YES) + if (cparams->prompt_password == TRI_YES && password == NULL) password = simple_prompt("Password: ", 100, false); /* @@ -90,23 +91,35 @@ connectDatabase(const char *dbname, const char *pghost, */ do { - const char *keywords[7]; - const char *values[7]; - - keywords[0] = "host"; - values[0] = pghost; - keywords[1] = "port"; - values[1] = pgport; - keywords[2] = "user"; - values[2] = pguser; - keywords[3] = "password"; - values[3] = password; - keywords[4] = "dbname"; - values[4] = dbname; - keywords[5] = "fallback_application_name"; - values[5] = progname; - keywords[6] = NULL; - values[6] = NULL; + 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); @@ -114,7 +127,7 @@ connectDatabase(const char *dbname, const char *pghost, if (!conn) { fprintf(stderr, _("%s: could not connect to database %s: out of memory\n"), - progname, dbname); + progname, cparams->dbname); exit(1); } @@ -123,7 +136,7 @@ connectDatabase(const char *dbname, const char *pghost, */ if (PQstatus(conn) == CONNECTION_BAD && PQconnectionNeedsPassword(conn) && - prompt_password != TRI_NO) + cparams->prompt_password != TRI_NO) { PQfinish(conn); if (password) @@ -142,10 +155,11 @@ connectDatabase(const char *dbname, const char *pghost, return NULL; } fprintf(stderr, _("%s: could not connect to database %s: %s"), - progname, dbname, PQerrorMessage(conn)); + progname, cparams->dbname, PQerrorMessage(conn)); exit(1); } + /* Start strict; callers may override this. */ PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL, progname, echo)); @@ -154,27 +168,30 @@ connectDatabase(const char *dbname, const char *pghost, /* * 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(const char *maintenance_db, - const char *pghost, const char *pgport, - const char *pguser, enum trivalue prompt_password, +connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo) { PGconn *conn; /* If a maintenance database name was specified, just connect to it. */ - if (maintenance_db) - return connectDatabase(maintenance_db, pghost, pgport, pguser, - prompt_password, progname, echo, false, false); + if (cparams->dbname) + return connectDatabase(cparams, progname, echo, false, false); /* Otherwise, try postgres first and then template1. */ - conn = connectDatabase("postgres", pghost, pgport, pguser, prompt_password, - progname, echo, true, false); + cparams->dbname = "postgres"; + conn = connectDatabase(cparams, progname, echo, true, false); if (!conn) - conn = connectDatabase("template1", pghost, pgport, pguser, - prompt_password, progname, echo, false, false); - + { + cparams->dbname = "template1"; + conn = connectDatabase(cparams, progname, echo, false, false); + } return conn; } diff --git a/src/bin/scripts/common.h b/src/bin/scripts/common.h index 566b59a8734..a644f34ea2a 100644 --- a/src/bin/scripts/common.h +++ b/src/bin/scripts/common.h @@ -23,20 +23,32 @@ enum trivalue extern bool CancelRequested; +/* 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 char *dbname, const char *pghost, - const char *pgport, const char *pguser, - enum trivalue prompt_password, const char *progname, - bool echo, bool fail_ok, bool allow_password_reuse); +extern PGconn *connectDatabase(const ConnParams *cparams, + const char *progname, + bool echo, bool fail_ok, + bool allow_password_reuse); -extern PGconn *connectMaintenanceDatabase(const char *maintenance_db, - const char *pghost, const char *pgport, - const char *pguser, enum trivalue prompt_password, +extern PGconn *connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo); extern PGresult *executeQuery(PGconn *conn, const char *query, diff --git a/src/bin/scripts/createdb.c b/src/bin/scripts/createdb.c index bd1f3f04f4c..6c34bc8a27f 100644 --- a/src/bin/scripts/createdb.c +++ b/src/bin/scripts/createdb.c @@ -50,6 +50,7 @@ main(int argc, char *argv[]) char *port = NULL; char *username = NULL; enum trivalue prompt_password = TRI_DEFAULT; + ConnParams cparams; bool echo = false; char *owner = NULL; char *tablespace = NULL; @@ -181,8 +182,14 @@ main(int argc, char *argv[]) if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0) maintenance_db = "template1"; - conn = connectMaintenanceDatabase(maintenance_db, host, port, username, - prompt_password, progname, echo); + cparams.dbname = maintenance_db; + cparams.pghost = host; + cparams.pgport = port; + cparams.pguser = username; + cparams.prompt_password = prompt_password; + cparams.override_dbname = NULL; + + conn = connectMaintenanceDatabase(&cparams, progname, echo); initPQExpBuffer(&sql); diff --git a/src/bin/scripts/createlang.c b/src/bin/scripts/createlang.c index 0e034ddebd8..b143ba74f14 100644 --- a/src/bin/scripts/createlang.c +++ b/src/bin/scripts/createlang.c @@ -42,6 +42,7 @@ main(int argc, char *argv[]) char *port = NULL; char *username = NULL; enum trivalue prompt_password = TRI_DEFAULT; + ConnParams cparams; bool echo = false; char *langname = NULL; @@ -130,6 +131,13 @@ main(int argc, char *argv[]) dbname = get_user_name_or_exit(progname); } + cparams.dbname = dbname; + cparams.pghost = host; + cparams.pgport = port; + cparams.pguser = username; + cparams.prompt_password = prompt_password; + cparams.override_dbname = NULL; + initPQExpBuffer(&sql); /* @@ -140,7 +148,7 @@ main(int argc, char *argv[]) printQueryOpt popt; static const bool translate_columns[] = {false, true}; - conn = connectDatabase(dbname, host, port, username, prompt_password, + conn = connectDatabase(&cparams, progname, echo, false, false); printfPQExpBuffer(&sql, "SELECT lanname as \"%s\", " @@ -180,7 +188,7 @@ main(int argc, char *argv[]) if (*p >= 'A' && *p <= 'Z') *p += ('a' - 'A'); - conn = connectDatabase(dbname, host, port, username, prompt_password, + conn = connectDatabase(&cparams, progname, echo, false, false); /* diff --git a/src/bin/scripts/createuser.c b/src/bin/scripts/createuser.c index 6ba8ef08aea..31b2ade66d7 100644 --- a/src/bin/scripts/createuser.c +++ b/src/bin/scripts/createuser.c @@ -61,6 +61,7 @@ main(int argc, char *argv[]) char *username = NULL; SimpleStringList roles = {NULL, NULL}; enum trivalue prompt_password = TRI_DEFAULT; + ConnParams cparams; bool echo = false; bool interactive = false; int conn_limit = -2; /* less than minimum valid value */ @@ -260,8 +261,14 @@ main(int argc, char *argv[]) if (login == 0) login = TRI_YES; - conn = connectDatabase("postgres", host, port, username, prompt_password, - progname, echo, false, false); + cparams.dbname = NULL; /* this program lacks any dbname option... */ + cparams.pghost = host; + cparams.pgport = port; + cparams.pguser = username; + cparams.prompt_password = prompt_password; + cparams.override_dbname = NULL; + + conn = connectMaintenanceDatabase(&cparams, progname, echo); initPQExpBuffer(&sql); diff --git a/src/bin/scripts/dropdb.c b/src/bin/scripts/dropdb.c index 0721f1e8e5e..c67b3c077a3 100644 --- a/src/bin/scripts/dropdb.c +++ b/src/bin/scripts/dropdb.c @@ -46,6 +46,7 @@ main(int argc, char *argv[]) char *port = NULL; char *username = NULL; enum trivalue prompt_password = TRI_DEFAULT; + ConnParams cparams; bool echo = false; bool interactive = false; @@ -128,9 +129,14 @@ main(int argc, char *argv[]) if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0) maintenance_db = "template1"; - conn = connectMaintenanceDatabase(maintenance_db, - host, port, username, prompt_password, - progname, echo); + cparams.dbname = maintenance_db; + cparams.pghost = host; + cparams.pgport = port; + cparams.pguser = username; + cparams.prompt_password = prompt_password; + cparams.override_dbname = NULL; + + conn = connectMaintenanceDatabase(&cparams, progname, echo); if (echo) printf("%s\n", sql.data); diff --git a/src/bin/scripts/droplang.c b/src/bin/scripts/droplang.c index de70f3a51b5..fac40d58fd7 100644 --- a/src/bin/scripts/droplang.c +++ b/src/bin/scripts/droplang.c @@ -44,6 +44,7 @@ main(int argc, char *argv[]) char *port = NULL; char *username = NULL; enum trivalue prompt_password = TRI_DEFAULT; + ConnParams cparams; bool echo = false; char *langname = NULL; char *p; @@ -129,6 +130,13 @@ main(int argc, char *argv[]) dbname = get_user_name_or_exit(progname); } + cparams.dbname = dbname; + cparams.pghost = host; + cparams.pgport = port; + cparams.pguser = username; + cparams.prompt_password = prompt_password; + cparams.override_dbname = NULL; + initPQExpBuffer(&sql); /* @@ -139,7 +147,7 @@ main(int argc, char *argv[]) printQueryOpt popt; static const bool translate_columns[] = {false, true}; - conn = connectDatabase(dbname, host, port, username, prompt_password, + conn = connectDatabase(&cparams, progname, echo, false, false); printfPQExpBuffer(&sql, "SELECT lanname as \"%s\", " @@ -181,7 +189,7 @@ main(int argc, char *argv[]) if (*p >= 'A' && *p <= 'Z') *p += ('a' - 'A'); - conn = connectDatabase(dbname, host, port, username, prompt_password, + conn = connectDatabase(&cparams, progname, echo, false, false); /* diff --git a/src/bin/scripts/dropuser.c b/src/bin/scripts/dropuser.c index 492283996c1..fabbe0c7f01 100644 --- a/src/bin/scripts/dropuser.c +++ b/src/bin/scripts/dropuser.c @@ -44,6 +44,7 @@ main(int argc, char *argv[]) char *port = NULL; char *username = NULL; enum trivalue prompt_password = TRI_DEFAULT; + ConnParams cparams; bool echo = false; bool interactive = false; @@ -124,13 +125,19 @@ main(int argc, char *argv[]) exit(0); } + cparams.dbname = NULL; /* this program lacks any dbname option... */ + cparams.pghost = host; + cparams.pgport = port; + cparams.pguser = username; + cparams.prompt_password = prompt_password; + cparams.override_dbname = NULL; + + conn = connectMaintenanceDatabase(&cparams, progname, echo); + initPQExpBuffer(&sql); appendPQExpBuffer(&sql, "DROP ROLE %s%s;", (if_exists ? "IF EXISTS " : ""), fmtId(dropuser)); - conn = connectDatabase("postgres", host, port, username, prompt_password, - progname, echo, false, false); - if (echo) printf("%s\n", sql.data); result = PQexec(conn, sql.data); diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c index 01795c7abda..76f0daaadf3 100644 --- a/src/bin/scripts/reindexdb.c +++ b/src/bin/scripts/reindexdb.c @@ -15,19 +15,14 @@ #include "fe_utils/string_utils.h" -static void reindex_one_database(const char *name, const char *dbname, - const char *type, const char *host, - const char *port, const char *username, - enum trivalue prompt_password, const char *progname, +static void reindex_one_database(const ConnParams *cparams, + const char *type, const char *name, + const char *progname, bool echo, bool verbose); -static void reindex_all_databases(const char *maintenance_db, - const char *host, const char *port, - const char *username, enum trivalue prompt_password, +static void reindex_all_databases(ConnParams *cparams, const char *progname, bool echo, bool quiet, bool verbose); -static void reindex_system_catalogs(const char *dbname, - const char *host, const char *port, - const char *username, enum trivalue prompt_password, +static void reindex_system_catalogs(const ConnParams *cparams, const char *progname, bool echo, bool verbose); static void help(const char *progname); @@ -63,6 +58,7 @@ main(int argc, char *argv[]) const char *port = NULL; const char *username = NULL; enum trivalue prompt_password = TRI_DEFAULT; + ConnParams cparams; bool syscatalog = false; bool alldb = false; bool echo = false; @@ -151,6 +147,13 @@ main(int argc, char *argv[]) exit(1); } + /* fill cparams except for dbname, which is set below */ + cparams.pghost = host; + cparams.pgport = port; + cparams.pguser = username; + cparams.prompt_password = prompt_password; + cparams.override_dbname = NULL; + setup_cancel_handler(); if (alldb) @@ -181,8 +184,10 @@ main(int argc, char *argv[]) exit(1); } - reindex_all_databases(maintenance_db, host, port, username, - prompt_password, progname, echo, quiet, verbose); + cparams.dbname = maintenance_db; + + reindex_all_databases(&cparams, + progname, echo, quiet, verbose); } else if (syscatalog) { @@ -212,7 +217,9 @@ main(int argc, char *argv[]) dbname = get_user_name_or_exit(progname); } - reindex_system_catalogs(dbname, host, port, username, prompt_password, + cparams.dbname = dbname; + + reindex_system_catalogs(&cparams, progname, echo, verbose); } else @@ -227,14 +234,16 @@ main(int argc, char *argv[]) dbname = get_user_name_or_exit(progname); } + cparams.dbname = dbname; + if (schemas.head != NULL) { SimpleStringListCell *cell; for (cell = schemas.head; cell; cell = cell->next) { - reindex_one_database(cell->val, dbname, "SCHEMA", host, port, - username, prompt_password, progname, echo, verbose); + reindex_one_database(&cparams, "SCHEMA", cell->val, + progname, echo, verbose); } } @@ -244,8 +253,8 @@ main(int argc, char *argv[]) for (cell = indexes.head; cell; cell = cell->next) { - reindex_one_database(cell->val, dbname, "INDEX", host, port, - username, prompt_password, progname, echo, verbose); + reindex_one_database(&cparams, "INDEX", cell->val, + progname, echo, verbose); } } if (tables.head != NULL) @@ -254,8 +263,8 @@ main(int argc, char *argv[]) for (cell = tables.head; cell; cell = cell->next) { - reindex_one_database(cell->val, dbname, "TABLE", host, port, - username, prompt_password, progname, echo, verbose); + reindex_one_database(&cparams, "TABLE", cell->val, + progname, echo, verbose); } } @@ -264,25 +273,24 @@ main(int argc, char *argv[]) * specified */ if (indexes.head == NULL && tables.head == NULL && schemas.head == NULL) - reindex_one_database(NULL, dbname, "DATABASE", host, port, - username, prompt_password, progname, echo, verbose); + reindex_one_database(&cparams, "DATABASE", NULL, + progname, echo, verbose); } exit(0); } static void -reindex_one_database(const char *name, const char *dbname, const char *type, - const char *host, const char *port, const char *username, - enum trivalue prompt_password, const char *progname, bool echo, - bool verbose) +reindex_one_database(const ConnParams *cparams, + const char *type, const char *name, + const char *progname, + bool echo, bool verbose) { PQExpBufferData sql; PGconn *conn; - conn = connectDatabase(dbname, host, port, username, prompt_password, - progname, echo, false, false); + conn = connectDatabase(cparams, progname, echo, false, false); initPQExpBuffer(&sql); @@ -325,22 +333,17 @@ reindex_one_database(const char *name, const char *dbname, const char *type, } static void -reindex_all_databases(const char *maintenance_db, - const char *host, const char *port, - const char *username, enum trivalue prompt_password, +reindex_all_databases(ConnParams *cparams, const char *progname, bool echo, bool quiet, bool verbose) { PGconn *conn; PGresult *result; - PQExpBufferData connstr; int i; - conn = connectMaintenanceDatabase(maintenance_db, host, port, username, - prompt_password, progname, echo); + conn = connectMaintenanceDatabase(cparams, progname, echo); result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo); PQfinish(conn); - initPQExpBuffer(&connstr); for (i = 0; i < PQntuples(result); i++) { char *dbname = PQgetvalue(result, i, 0); @@ -351,29 +354,23 @@ reindex_all_databases(const char *maintenance_db, fflush(stdout); } - resetPQExpBuffer(&connstr); - appendPQExpBuffer(&connstr, "dbname="); - appendConnStrVal(&connstr, dbname); + cparams->override_dbname = dbname; - reindex_one_database(NULL, connstr.data, "DATABASE", host, - port, username, prompt_password, + reindex_one_database(cparams, "DATABASE", NULL, progname, echo, verbose); } - termPQExpBuffer(&connstr); PQclear(result); } static void -reindex_system_catalogs(const char *dbname, const char *host, const char *port, - const char *username, enum trivalue prompt_password, +reindex_system_catalogs(const ConnParams *cparams, const char *progname, bool echo, bool verbose) { PGconn *conn; PQExpBufferData sql; - conn = connectDatabase(dbname, host, port, username, prompt_password, - progname, echo, false, false); + conn = connectDatabase(cparams, progname, echo, false, false); initPQExpBuffer(&sql); diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index 14c7306bab9..0f55cfa7e73 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -45,19 +45,16 @@ typedef struct vacuumingOptions } vacuumingOptions; -static void vacuum_one_database(const char *dbname, vacuumingOptions *vacopts, +static void vacuum_one_database(const ConnParams *cparams, + vacuumingOptions *vacopts, int stage, SimpleStringList *tables, - const char *host, const char *port, - const char *username, enum trivalue prompt_password, int concurrentCons, const char *progname, bool echo, bool quiet); -static void vacuum_all_databases(vacuumingOptions *vacopts, +static void vacuum_all_databases(ConnParams *cparams, + vacuumingOptions *vacopts, bool analyze_in_stages, - const char *maintenance_db, - const char *host, const char *port, - const char *username, enum trivalue prompt_password, int concurrentCons, const char *progname, bool echo, bool quiet); @@ -124,6 +121,7 @@ main(int argc, char *argv[]) char *port = NULL; char *username = NULL; enum trivalue prompt_password = TRI_DEFAULT; + ConnParams cparams; bool echo = false; bool quiet = false; vacuumingOptions vacopts; @@ -250,6 +248,13 @@ main(int argc, char *argv[]) /* allow 'and_analyze' with 'analyze_only' */ } + /* fill cparams except for dbname, which is set below */ + cparams.pghost = host; + cparams.pgport = port; + cparams.pguser = username; + cparams.prompt_password = prompt_password; + cparams.override_dbname = NULL; + setup_cancel_handler(); /* Avoid opening extra connections. */ @@ -271,10 +276,10 @@ main(int argc, char *argv[]) exit(1); } - vacuum_all_databases(&vacopts, + cparams.dbname = maintenance_db; + + vacuum_all_databases(&cparams, &vacopts, analyze_in_stages, - maintenance_db, - host, port, username, prompt_password, concurrentCons, progname, echo, quiet); } @@ -290,25 +295,25 @@ main(int argc, char *argv[]) dbname = get_user_name_or_exit(progname); } + cparams.dbname = dbname; + if (analyze_in_stages) { int stage; for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++) { - vacuum_one_database(dbname, &vacopts, + vacuum_one_database(&cparams, &vacopts, stage, &tables, - host, port, username, prompt_password, concurrentCons, progname, echo, quiet); } } else - vacuum_one_database(dbname, &vacopts, + vacuum_one_database(&cparams, &vacopts, ANALYZE_NO_STAGE, &tables, - host, port, username, prompt_password, concurrentCons, progname, echo, quiet); } @@ -330,11 +335,10 @@ main(int argc, char *argv[]) * a list of tables from the database. */ static void -vacuum_one_database(const char *dbname, vacuumingOptions *vacopts, +vacuum_one_database(const ConnParams *cparams, + vacuumingOptions *vacopts, int stage, SimpleStringList *tables, - const char *host, const char *port, - const char *username, enum trivalue prompt_password, int concurrentCons, const char *progname, bool echo, bool quiet) { @@ -360,8 +364,7 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts, Assert(stage == ANALYZE_NO_STAGE || (stage >= 0 && stage < ANALYZE_NUM_STAGES)); - conn = connectDatabase(dbname, host, port, username, prompt_password, - progname, echo, false, true); + conn = connectDatabase(cparams, progname, echo, false, true); if (!quiet) { @@ -433,8 +436,7 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts, { for (i = 1; i < concurrentCons; i++) { - conn = connectDatabase(dbname, host, port, username, prompt_password, - progname, echo, false, true); + conn = connectDatabase(cparams, progname, echo, false, true); /* * Fail and exit immediately if trying to use a socket in an @@ -557,28 +559,23 @@ finish: * quickly everywhere before generating more detailed ones. */ static void -vacuum_all_databases(vacuumingOptions *vacopts, +vacuum_all_databases(ConnParams *cparams, + vacuumingOptions *vacopts, bool analyze_in_stages, - const char *maintenance_db, const char *host, - const char *port, const char *username, - enum trivalue prompt_password, int concurrentCons, const char *progname, bool echo, bool quiet) { PGconn *conn; PGresult *result; - PQExpBufferData connstr; int stage; int i; - conn = connectMaintenanceDatabase(maintenance_db, host, port, username, - prompt_password, progname, echo); + conn = connectMaintenanceDatabase(cparams, progname, echo); result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo); PQfinish(conn); - initPQExpBuffer(&connstr); if (analyze_in_stages) { /* @@ -593,14 +590,11 @@ vacuum_all_databases(vacuumingOptions *vacopts, { for (i = 0; i < PQntuples(result); i++) { - resetPQExpBuffer(&connstr); - appendPQExpBuffer(&connstr, "dbname="); - appendConnStrVal(&connstr, PQgetvalue(result, i, 0)); + cparams->override_dbname = PQgetvalue(result, i, 0); - vacuum_one_database(connstr.data, vacopts, + vacuum_one_database(cparams, vacopts, stage, NULL, - host, port, username, prompt_password, concurrentCons, progname, echo, quiet); } @@ -610,19 +604,15 @@ vacuum_all_databases(vacuumingOptions *vacopts, { for (i = 0; i < PQntuples(result); i++) { - resetPQExpBuffer(&connstr); - appendPQExpBuffer(&connstr, "dbname="); - appendConnStrVal(&connstr, PQgetvalue(result, i, 0)); + cparams->override_dbname = PQgetvalue(result, i, 0); - vacuum_one_database(connstr.data, vacopts, + vacuum_one_database(cparams, vacopts, ANALYZE_NO_STAGE, NULL, - host, port, username, prompt_password, concurrentCons, progname, echo, quiet); } } - termPQExpBuffer(&connstr); PQclear(result); } |