mirror of
https://github.com/postgres/postgres.git
synced 2025-05-05 09:19:17 +03:00
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
This commit is contained in:
parent
25378db74f
commit
1814f915b5
@ -90,9 +90,9 @@ PostgreSQL documentation
|
|||||||
<term><option><optional>--dbname=</optional><replaceable class="parameter">dbname</replaceable></option></term>
|
<term><option><optional>--dbname=</optional><replaceable class="parameter">dbname</replaceable></option></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Specifies the name of the database to be clustered.
|
Specifies the name of the database to be clustered,
|
||||||
If this is not specified and <option>-a</option> (or
|
when <option>-a</option>/<option>--all</option> is not used.
|
||||||
<option>--all</option>) is not used, the database name is read
|
If this is not specified, the database name is read
|
||||||
from the environment variable <envar>PGDATABASE</envar>. If
|
from the environment variable <envar>PGDATABASE</envar>. If
|
||||||
that is not set, the user name specified for the connection is
|
that is not set, the user name specified for the connection is
|
||||||
used. The <replaceable>dbname</replaceable> can be a <link
|
used. The <replaceable>dbname</replaceable> can be a <link
|
||||||
@ -249,10 +249,16 @@ PostgreSQL documentation
|
|||||||
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
|
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Specifies the name of the database to connect to discover what other
|
Specifies the name of the database to connect to to discover which
|
||||||
databases should be clustered. If not specified, the
|
databases should be clustered,
|
||||||
<literal>postgres</literal> database will be used,
|
when <option>-a</option>/<option>--all</option> is used.
|
||||||
and if that does not exist, <literal>template1</literal> will be used.
|
If not specified, the <literal>postgres</literal> database will be used,
|
||||||
|
or if that does not exist, <literal>template1</literal> will be used.
|
||||||
|
This can be a <link linkend="libpq-connstring">connection
|
||||||
|
string</link>. If so, connection string parameters will override any
|
||||||
|
conflicting command line options. Also, connection string parameters
|
||||||
|
other than the database name itself will be re-used when connecting
|
||||||
|
to other databases.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
@ -284,6 +284,9 @@ PostgreSQL documentation
|
|||||||
database will be used; if that does not exist (or if it is the name
|
database will be used; if that does not exist (or if it is the name
|
||||||
of the new database being created), <literal>template1</literal> will
|
of the new database being created), <literal>template1</literal> will
|
||||||
be used.
|
be used.
|
||||||
|
This can be a <link linkend="libpq-connstring">connection
|
||||||
|
string</link>. If so, connection string parameters will override any
|
||||||
|
conflicting command line options.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
@ -217,6 +217,9 @@ PostgreSQL documentation
|
|||||||
target database. If not specified, the <literal>postgres</literal>
|
target database. If not specified, the <literal>postgres</literal>
|
||||||
database will be used; if that does not exist (or is the database
|
database will be used; if that does not exist (or is the database
|
||||||
being dropped), <literal>template1</literal> will be used.
|
being dropped), <literal>template1</literal> will be used.
|
||||||
|
This can be a <link linkend="libpq-connstring">connection
|
||||||
|
string</link>. If so, connection string parameters will override any
|
||||||
|
conflicting command line options.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
@ -134,9 +134,9 @@ PostgreSQL documentation
|
|||||||
<term><option><optional>--dbname=</optional><replaceable class="parameter">dbname</replaceable></option></term>
|
<term><option><optional>--dbname=</optional><replaceable class="parameter">dbname</replaceable></option></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Specifies the name of the database to be reindexed.
|
Specifies the name of the database to be reindexed,
|
||||||
If this is not specified and <option>-a</option> (or
|
when <option>-a</option>/<option>--all</option> is not used.
|
||||||
<option>--all</option>) is not used, the database name is read
|
If this is not specified, the database name is read
|
||||||
from the environment variable <envar>PGDATABASE</envar>. If
|
from the environment variable <envar>PGDATABASE</envar>. If
|
||||||
that is not set, the user name specified for the connection is
|
that is not set, the user name specified for the connection is
|
||||||
used. The <replaceable>dbname</replaceable> can be a <link
|
used. The <replaceable>dbname</replaceable> can be a <link
|
||||||
@ -351,10 +351,16 @@ PostgreSQL documentation
|
|||||||
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
|
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Specifies the name of the database to connect to discover what other
|
Specifies the name of the database to connect to to discover which
|
||||||
databases should be reindexed. If not specified, the
|
databases should be reindexed,
|
||||||
<literal>postgres</literal> database will be used,
|
when <option>-a</option>/<option>--all</option> is used.
|
||||||
and if that does not exist, <literal>template1</literal> will be used.
|
If not specified, the <literal>postgres</literal> database will be used,
|
||||||
|
or if that does not exist, <literal>template1</literal> will be used.
|
||||||
|
This can be a <link linkend="libpq-connstring">connection
|
||||||
|
string</link>. If so, connection string parameters will override any
|
||||||
|
conflicting command line options. Also, connection string parameters
|
||||||
|
other than the database name itself will be re-used when connecting
|
||||||
|
to other databases.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
@ -92,9 +92,9 @@ PostgreSQL documentation
|
|||||||
<term><option><optional>--dbname=</optional><replaceable class="parameter">dbname</replaceable></option></term>
|
<term><option><optional>--dbname=</optional><replaceable class="parameter">dbname</replaceable></option></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Specifies the name of the database to be cleaned or analyzed.
|
Specifies the name of the database to be cleaned or analyzed,
|
||||||
If this is not specified and <option>-a</option> (or
|
when <option>-a</option>/<option>--all</option> is not used.
|
||||||
<option>--all</option>) is not used, the database name is read
|
If this is not specified, the database name is read
|
||||||
from the environment variable <envar>PGDATABASE</envar>. If
|
from the environment variable <envar>PGDATABASE</envar>. If
|
||||||
that is not set, the user name specified for the connection is
|
that is not set, the user name specified for the connection is
|
||||||
used. The <replaceable>dbname</replaceable> can be a <link
|
used. The <replaceable>dbname</replaceable> can be a <link
|
||||||
@ -444,10 +444,16 @@ PostgreSQL documentation
|
|||||||
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
|
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Specifies the name of the database to connect to discover what other
|
Specifies the name of the database to connect to to discover which
|
||||||
databases should be vacuumed. If not specified, the
|
databases should be vacuumed,
|
||||||
<literal>postgres</literal> database will be used,
|
when <option>-a</option>/<option>--all</option> is used.
|
||||||
and if that does not exist, <literal>template1</literal> will be used.
|
If not specified, the <literal>postgres</literal> database will be used,
|
||||||
|
or if that does not exist, <literal>template1</literal> will be used.
|
||||||
|
This can be a <link linkend="libpq-connstring">connection
|
||||||
|
string</link>. If so, connection string parameters will override any
|
||||||
|
conflicting command line options. Also, connection string parameters
|
||||||
|
other than the database name itself will be re-used when connecting
|
||||||
|
to other databases.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
@ -17,15 +17,10 @@
|
|||||||
#include "fe_utils/string_utils.h"
|
#include "fe_utils/string_utils.h"
|
||||||
|
|
||||||
|
|
||||||
static void cluster_one_database(const char *dbname, bool verbose, const char *table,
|
static void cluster_one_database(const ConnParams *cparams, const char *table,
|
||||||
const char *host, const char *port,
|
const char *progname, bool verbose, bool echo);
|
||||||
const char *username, enum trivalue prompt_password,
|
static void cluster_all_databases(ConnParams *cparams, const char *progname,
|
||||||
const char *progname, bool echo);
|
bool verbose, bool echo, bool quiet);
|
||||||
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 help(const char *progname);
|
static void help(const char *progname);
|
||||||
|
|
||||||
|
|
||||||
@ -58,6 +53,7 @@ main(int argc, char *argv[])
|
|||||||
char *port = NULL;
|
char *port = NULL;
|
||||||
char *username = NULL;
|
char *username = NULL;
|
||||||
enum trivalue prompt_password = TRI_DEFAULT;
|
enum trivalue prompt_password = TRI_DEFAULT;
|
||||||
|
ConnParams cparams;
|
||||||
bool echo = false;
|
bool echo = false;
|
||||||
bool quiet = false;
|
bool quiet = false;
|
||||||
bool alldb = false;
|
bool alldb = false;
|
||||||
@ -134,6 +130,13 @@ main(int argc, char *argv[])
|
|||||||
exit(1);
|
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(NULL);
|
setup_cancel_handler(NULL);
|
||||||
|
|
||||||
if (alldb)
|
if (alldb)
|
||||||
@ -150,8 +153,9 @@ main(int argc, char *argv[])
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
cluster_all_databases(verbose, maintenance_db, host, port, username, prompt_password,
|
cparams.dbname = maintenance_db;
|
||||||
progname, echo, quiet);
|
|
||||||
|
cluster_all_databases(&cparams, progname, verbose, echo, quiet);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -165,21 +169,21 @@ main(int argc, char *argv[])
|
|||||||
dbname = get_user_name_or_exit(progname);
|
dbname = get_user_name_or_exit(progname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cparams.dbname = dbname;
|
||||||
|
|
||||||
if (tables.head != NULL)
|
if (tables.head != NULL)
|
||||||
{
|
{
|
||||||
SimpleStringListCell *cell;
|
SimpleStringListCell *cell;
|
||||||
|
|
||||||
for (cell = tables.head; cell; cell = cell->next)
|
for (cell = tables.head; cell; cell = cell->next)
|
||||||
{
|
{
|
||||||
cluster_one_database(dbname, verbose, cell->val,
|
cluster_one_database(&cparams, cell->val,
|
||||||
host, port, username, prompt_password,
|
progname, verbose, echo);
|
||||||
progname, echo);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
cluster_one_database(dbname, verbose, NULL,
|
cluster_one_database(&cparams, NULL,
|
||||||
host, port, username, prompt_password,
|
progname, verbose, echo);
|
||||||
progname, echo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
@ -187,17 +191,14 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
cluster_one_database(const char *dbname, bool verbose, const char *table,
|
cluster_one_database(const ConnParams *cparams, const char *table,
|
||||||
const char *host, const char *port,
|
const char *progname, bool verbose, bool echo)
|
||||||
const char *username, enum trivalue prompt_password,
|
|
||||||
const char *progname, bool echo)
|
|
||||||
{
|
{
|
||||||
PQExpBufferData sql;
|
PQExpBufferData sql;
|
||||||
|
|
||||||
PGconn *conn;
|
PGconn *conn;
|
||||||
|
|
||||||
conn = connectDatabase(dbname, host, port, username, prompt_password,
|
conn = connectDatabase(cparams, progname, echo, false, false);
|
||||||
progname, echo, false, false);
|
|
||||||
|
|
||||||
initPQExpBuffer(&sql);
|
initPQExpBuffer(&sql);
|
||||||
|
|
||||||
@ -228,22 +229,17 @@ cluster_one_database(const char *dbname, bool verbose, const char *table,
|
|||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
cluster_all_databases(bool verbose, const char *maintenance_db,
|
cluster_all_databases(ConnParams *cparams, const char *progname,
|
||||||
const char *host, const char *port,
|
bool verbose, bool echo, bool quiet)
|
||||||
const char *username, enum trivalue prompt_password,
|
|
||||||
const char *progname, bool echo, bool quiet)
|
|
||||||
{
|
{
|
||||||
PGconn *conn;
|
PGconn *conn;
|
||||||
PGresult *result;
|
PGresult *result;
|
||||||
PQExpBufferData connstr;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
|
conn = connectMaintenanceDatabase(cparams, progname, echo);
|
||||||
prompt_password, progname, echo);
|
|
||||||
result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
|
result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
|
|
||||||
initPQExpBuffer(&connstr);
|
|
||||||
for (i = 0; i < PQntuples(result); i++)
|
for (i = 0; i < PQntuples(result); i++)
|
||||||
{
|
{
|
||||||
char *dbname = PQgetvalue(result, i, 0);
|
char *dbname = PQgetvalue(result, i, 0);
|
||||||
@ -254,15 +250,10 @@ cluster_all_databases(bool verbose, const char *maintenance_db,
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
resetPQExpBuffer(&connstr);
|
cparams->override_dbname = dbname;
|
||||||
appendPQExpBufferStr(&connstr, "dbname=");
|
|
||||||
appendConnStrVal(&connstr, dbname);
|
|
||||||
|
|
||||||
cluster_one_database(connstr.data, verbose, NULL,
|
cluster_one_database(cparams, NULL, progname, verbose, echo);
|
||||||
host, port, username, prompt_password,
|
|
||||||
progname, echo);
|
|
||||||
}
|
}
|
||||||
termPQExpBuffer(&connstr);
|
|
||||||
|
|
||||||
PQclear(result);
|
PQclear(result);
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ handle_help_version_opts(int argc, char *argv[],
|
|||||||
* Make a database connection with the given parameters.
|
* Make a database connection with the given parameters.
|
||||||
*
|
*
|
||||||
* An interactive password prompt is automatically issued if needed and
|
* 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
|
* 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
|
* given during previous calls to this routine. (Callers should not pass
|
||||||
@ -61,9 +61,7 @@ handle_help_version_opts(int argc, char *argv[],
|
|||||||
* as before, else we might create password exposure hazards.)
|
* as before, else we might create password exposure hazards.)
|
||||||
*/
|
*/
|
||||||
PGconn *
|
PGconn *
|
||||||
connectDatabase(const char *dbname, const char *pghost,
|
connectDatabase(const ConnParams *cparams, const char *progname,
|
||||||
const char *pgport, const char *pguser,
|
|
||||||
enum trivalue prompt_password, const char *progname,
|
|
||||||
bool echo, bool fail_ok, bool allow_password_reuse)
|
bool echo, bool fail_ok, bool allow_password_reuse)
|
||||||
{
|
{
|
||||||
PGconn *conn;
|
PGconn *conn;
|
||||||
@ -71,10 +69,13 @@ connectDatabase(const char *dbname, const char *pghost,
|
|||||||
static bool have_password = false;
|
static bool have_password = false;
|
||||||
static char password[100];
|
static char password[100];
|
||||||
|
|
||||||
|
/* Callers must supply at least dbname; other params can be NULL */
|
||||||
|
Assert(cparams->dbname);
|
||||||
|
|
||||||
if (!allow_password_reuse)
|
if (!allow_password_reuse)
|
||||||
have_password = false;
|
have_password = false;
|
||||||
|
|
||||||
if (!have_password && prompt_password == TRI_YES)
|
if (cparams->prompt_password == TRI_YES && !have_password)
|
||||||
{
|
{
|
||||||
simple_prompt("Password: ", password, sizeof(password), false);
|
simple_prompt("Password: ", password, sizeof(password), false);
|
||||||
have_password = true;
|
have_password = true;
|
||||||
@ -86,23 +87,35 @@ connectDatabase(const char *dbname, const char *pghost,
|
|||||||
*/
|
*/
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
const char *keywords[7];
|
const char *keywords[8];
|
||||||
const char *values[7];
|
const char *values[8];
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
keywords[0] = "host";
|
/*
|
||||||
values[0] = pghost;
|
* If dbname is a connstring, its entries can override the other
|
||||||
keywords[1] = "port";
|
* values obtained from cparams; but in turn, override_dbname can
|
||||||
values[1] = pgport;
|
* override the dbname component of it.
|
||||||
keywords[2] = "user";
|
*/
|
||||||
values[2] = pguser;
|
keywords[i] = "host";
|
||||||
keywords[3] = "password";
|
values[i++] = cparams->pghost;
|
||||||
values[3] = have_password ? password : NULL;
|
keywords[i] = "port";
|
||||||
keywords[4] = "dbname";
|
values[i++] = cparams->pgport;
|
||||||
values[4] = dbname;
|
keywords[i] = "user";
|
||||||
keywords[5] = "fallback_application_name";
|
values[i++] = cparams->pguser;
|
||||||
values[5] = progname;
|
keywords[i] = "password";
|
||||||
keywords[6] = NULL;
|
values[i++] = have_password ? password : NULL;
|
||||||
values[6] = NULL;
|
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;
|
new_pass = false;
|
||||||
conn = PQconnectdbParams(keywords, values, true);
|
conn = PQconnectdbParams(keywords, values, true);
|
||||||
@ -110,7 +123,7 @@ connectDatabase(const char *dbname, const char *pghost,
|
|||||||
if (!conn)
|
if (!conn)
|
||||||
{
|
{
|
||||||
pg_log_error("could not connect to database %s: out of memory",
|
pg_log_error("could not connect to database %s: out of memory",
|
||||||
dbname);
|
cparams->dbname);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,7 +132,7 @@ connectDatabase(const char *dbname, const char *pghost,
|
|||||||
*/
|
*/
|
||||||
if (PQstatus(conn) == CONNECTION_BAD &&
|
if (PQstatus(conn) == CONNECTION_BAD &&
|
||||||
PQconnectionNeedsPassword(conn) &&
|
PQconnectionNeedsPassword(conn) &&
|
||||||
prompt_password != TRI_NO)
|
cparams->prompt_password != TRI_NO)
|
||||||
{
|
{
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
simple_prompt("Password: ", password, sizeof(password), false);
|
simple_prompt("Password: ", password, sizeof(password), false);
|
||||||
@ -137,10 +150,11 @@ connectDatabase(const char *dbname, const char *pghost,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
pg_log_error("could not connect to database %s: %s",
|
pg_log_error("could not connect to database %s: %s",
|
||||||
dbname, PQerrorMessage(conn));
|
cparams->dbname, PQerrorMessage(conn));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Start strict; callers may override this. */
|
||||||
PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL, echo));
|
PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL, echo));
|
||||||
|
|
||||||
return conn;
|
return conn;
|
||||||
@ -148,27 +162,30 @@ connectDatabase(const char *dbname, const char *pghost,
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Try to connect to the appropriate maintenance database.
|
* 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 *
|
PGconn *
|
||||||
connectMaintenanceDatabase(const char *maintenance_db,
|
connectMaintenanceDatabase(ConnParams *cparams,
|
||||||
const char *pghost, const char *pgport,
|
|
||||||
const char *pguser, enum trivalue prompt_password,
|
|
||||||
const char *progname, bool echo)
|
const char *progname, bool echo)
|
||||||
{
|
{
|
||||||
PGconn *conn;
|
PGconn *conn;
|
||||||
|
|
||||||
/* If a maintenance database name was specified, just connect to it. */
|
/* If a maintenance database name was specified, just connect to it. */
|
||||||
if (maintenance_db)
|
if (cparams->dbname)
|
||||||
return connectDatabase(maintenance_db, pghost, pgport, pguser,
|
return connectDatabase(cparams, progname, echo, false, false);
|
||||||
prompt_password, progname, echo, false, false);
|
|
||||||
|
|
||||||
/* Otherwise, try postgres first and then template1. */
|
/* Otherwise, try postgres first and then template1. */
|
||||||
conn = connectDatabase("postgres", pghost, pgport, pguser, prompt_password,
|
cparams->dbname = "postgres";
|
||||||
progname, echo, true, false);
|
conn = connectDatabase(cparams, progname, echo, true, false);
|
||||||
if (!conn)
|
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;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,20 +21,32 @@ enum trivalue
|
|||||||
TRI_YES
|
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);
|
typedef void (*help_handler) (const char *progname);
|
||||||
|
|
||||||
extern void handle_help_version_opts(int argc, char *argv[],
|
extern void handle_help_version_opts(int argc, char *argv[],
|
||||||
const char *fixed_progname,
|
const char *fixed_progname,
|
||||||
help_handler hlp);
|
help_handler hlp);
|
||||||
|
|
||||||
extern PGconn *connectDatabase(const char *dbname, const char *pghost,
|
extern PGconn *connectDatabase(const ConnParams *cparams,
|
||||||
const char *pgport, const char *pguser,
|
const char *progname,
|
||||||
enum trivalue prompt_password, const char *progname,
|
bool echo, bool fail_ok,
|
||||||
bool echo, bool fail_ok, bool allow_password_reuse);
|
bool allow_password_reuse);
|
||||||
|
|
||||||
extern PGconn *connectMaintenanceDatabase(const char *maintenance_db,
|
extern PGconn *connectMaintenanceDatabase(ConnParams *cparams,
|
||||||
const char *pghost, const char *pgport,
|
|
||||||
const char *pguser, enum trivalue prompt_password,
|
|
||||||
const char *progname, bool echo);
|
const char *progname, bool echo);
|
||||||
|
|
||||||
extern void disconnectDatabase(PGconn *conn);
|
extern void disconnectDatabase(PGconn *conn);
|
||||||
|
@ -51,6 +51,7 @@ main(int argc, char *argv[])
|
|||||||
char *port = NULL;
|
char *port = NULL;
|
||||||
char *username = NULL;
|
char *username = NULL;
|
||||||
enum trivalue prompt_password = TRI_DEFAULT;
|
enum trivalue prompt_password = TRI_DEFAULT;
|
||||||
|
ConnParams cparams;
|
||||||
bool echo = false;
|
bool echo = false;
|
||||||
char *owner = NULL;
|
char *owner = NULL;
|
||||||
char *tablespace = NULL;
|
char *tablespace = NULL;
|
||||||
@ -180,8 +181,14 @@ main(int argc, char *argv[])
|
|||||||
if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
|
if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
|
||||||
maintenance_db = "template1";
|
maintenance_db = "template1";
|
||||||
|
|
||||||
conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
|
cparams.dbname = maintenance_db;
|
||||||
prompt_password, progname, echo);
|
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);
|
initPQExpBuffer(&sql);
|
||||||
|
|
||||||
|
@ -58,6 +58,7 @@ main(int argc, char *argv[])
|
|||||||
char *username = NULL;
|
char *username = NULL;
|
||||||
SimpleStringList roles = {NULL, NULL};
|
SimpleStringList roles = {NULL, NULL};
|
||||||
enum trivalue prompt_password = TRI_DEFAULT;
|
enum trivalue prompt_password = TRI_DEFAULT;
|
||||||
|
ConnParams cparams;
|
||||||
bool echo = false;
|
bool echo = false;
|
||||||
bool interactive = false;
|
bool interactive = false;
|
||||||
int conn_limit = -2; /* less than minimum valid value */
|
int conn_limit = -2; /* less than minimum valid value */
|
||||||
@ -256,8 +257,14 @@ main(int argc, char *argv[])
|
|||||||
if (login == 0)
|
if (login == 0)
|
||||||
login = TRI_YES;
|
login = TRI_YES;
|
||||||
|
|
||||||
conn = connectDatabase("postgres", host, port, username, prompt_password,
|
cparams.dbname = NULL; /* this program lacks any dbname option... */
|
||||||
progname, echo, false, false);
|
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);
|
initPQExpBuffer(&sql);
|
||||||
|
|
||||||
|
@ -48,6 +48,7 @@ main(int argc, char *argv[])
|
|||||||
char *port = NULL;
|
char *port = NULL;
|
||||||
char *username = NULL;
|
char *username = NULL;
|
||||||
enum trivalue prompt_password = TRI_DEFAULT;
|
enum trivalue prompt_password = TRI_DEFAULT;
|
||||||
|
ConnParams cparams;
|
||||||
bool echo = false;
|
bool echo = false;
|
||||||
bool interactive = false;
|
bool interactive = false;
|
||||||
bool force = false;
|
bool force = false;
|
||||||
@ -137,9 +138,14 @@ main(int argc, char *argv[])
|
|||||||
if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
|
if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
|
||||||
maintenance_db = "template1";
|
maintenance_db = "template1";
|
||||||
|
|
||||||
conn = connectMaintenanceDatabase(maintenance_db,
|
cparams.dbname = maintenance_db;
|
||||||
host, port, username, prompt_password,
|
cparams.pghost = host;
|
||||||
progname, echo);
|
cparams.pgport = port;
|
||||||
|
cparams.pguser = username;
|
||||||
|
cparams.prompt_password = prompt_password;
|
||||||
|
cparams.override_dbname = NULL;
|
||||||
|
|
||||||
|
conn = connectMaintenanceDatabase(&cparams, progname, echo);
|
||||||
|
|
||||||
if (echo)
|
if (echo)
|
||||||
printf("%s\n", sql.data);
|
printf("%s\n", sql.data);
|
||||||
|
@ -45,6 +45,7 @@ main(int argc, char *argv[])
|
|||||||
char *port = NULL;
|
char *port = NULL;
|
||||||
char *username = NULL;
|
char *username = NULL;
|
||||||
enum trivalue prompt_password = TRI_DEFAULT;
|
enum trivalue prompt_password = TRI_DEFAULT;
|
||||||
|
ConnParams cparams;
|
||||||
bool echo = false;
|
bool echo = false;
|
||||||
bool interactive = false;
|
bool interactive = false;
|
||||||
char dropuser_buf[128];
|
char dropuser_buf[128];
|
||||||
@ -131,13 +132,19 @@ main(int argc, char *argv[])
|
|||||||
exit(0);
|
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);
|
initPQExpBuffer(&sql);
|
||||||
appendPQExpBuffer(&sql, "DROP ROLE %s%s;",
|
appendPQExpBuffer(&sql, "DROP ROLE %s%s;",
|
||||||
(if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
|
(if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
|
||||||
|
|
||||||
conn = connectDatabase("postgres", host, port, username, prompt_password,
|
|
||||||
progname, echo, false, false);
|
|
||||||
|
|
||||||
if (echo)
|
if (echo)
|
||||||
printf("%s\n", sql.data);
|
printf("%s\n", sql.data);
|
||||||
result = PQexec(conn, sql.data);
|
result = PQexec(conn, sql.data);
|
||||||
|
@ -34,15 +34,12 @@ static SimpleStringList *get_parallel_object_list(PGconn *conn,
|
|||||||
ReindexType type,
|
ReindexType type,
|
||||||
SimpleStringList *user_list,
|
SimpleStringList *user_list,
|
||||||
bool echo);
|
bool echo);
|
||||||
static void reindex_one_database(const char *dbname, ReindexType type,
|
static void reindex_one_database(const ConnParams *cparams, ReindexType type,
|
||||||
SimpleStringList *user_list, const char *host,
|
SimpleStringList *user_list,
|
||||||
const char *port, const char *username,
|
const char *progname,
|
||||||
enum trivalue prompt_password, const char *progname,
|
|
||||||
bool echo, bool verbose, bool concurrently,
|
bool echo, bool verbose, bool concurrently,
|
||||||
int concurrentCons);
|
int concurrentCons);
|
||||||
static void reindex_all_databases(const char *maintenance_db,
|
static void reindex_all_databases(ConnParams *cparams,
|
||||||
const char *host, const char *port,
|
|
||||||
const char *username, enum trivalue prompt_password,
|
|
||||||
const char *progname, bool echo,
|
const char *progname, bool echo,
|
||||||
bool quiet, bool verbose, bool concurrently,
|
bool quiet, bool verbose, bool concurrently,
|
||||||
int concurrentCons);
|
int concurrentCons);
|
||||||
@ -86,6 +83,7 @@ main(int argc, char *argv[])
|
|||||||
const char *port = NULL;
|
const char *port = NULL;
|
||||||
const char *username = NULL;
|
const char *username = NULL;
|
||||||
enum trivalue prompt_password = TRI_DEFAULT;
|
enum trivalue prompt_password = TRI_DEFAULT;
|
||||||
|
ConnParams cparams;
|
||||||
bool syscatalog = false;
|
bool syscatalog = false;
|
||||||
bool alldb = false;
|
bool alldb = false;
|
||||||
bool echo = false;
|
bool echo = false;
|
||||||
@ -188,6 +186,13 @@ main(int argc, char *argv[])
|
|||||||
exit(1);
|
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(NULL);
|
setup_cancel_handler(NULL);
|
||||||
|
|
||||||
if (alldb)
|
if (alldb)
|
||||||
@ -218,8 +223,9 @@ main(int argc, char *argv[])
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
reindex_all_databases(maintenance_db, host, port, username,
|
cparams.dbname = maintenance_db;
|
||||||
prompt_password, progname, echo, quiet, verbose,
|
|
||||||
|
reindex_all_databases(&cparams, progname, echo, quiet, verbose,
|
||||||
concurrently, concurrentCons);
|
concurrently, concurrentCons);
|
||||||
}
|
}
|
||||||
else if (syscatalog)
|
else if (syscatalog)
|
||||||
@ -256,9 +262,11 @@ main(int argc, char *argv[])
|
|||||||
dbname = get_user_name_or_exit(progname);
|
dbname = get_user_name_or_exit(progname);
|
||||||
}
|
}
|
||||||
|
|
||||||
reindex_one_database(dbname, REINDEX_SYSTEM, NULL, host,
|
cparams.dbname = dbname;
|
||||||
port, username, prompt_password, progname,
|
|
||||||
echo, verbose, concurrently, 1);
|
reindex_one_database(&cparams, REINDEX_SYSTEM, NULL,
|
||||||
|
progname, echo, verbose,
|
||||||
|
concurrently, 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -283,40 +291,40 @@ main(int argc, char *argv[])
|
|||||||
dbname = get_user_name_or_exit(progname);
|
dbname = get_user_name_or_exit(progname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cparams.dbname = dbname;
|
||||||
|
|
||||||
if (schemas.head != NULL)
|
if (schemas.head != NULL)
|
||||||
reindex_one_database(dbname, REINDEX_SCHEMA, &schemas, host,
|
reindex_one_database(&cparams, REINDEX_SCHEMA, &schemas,
|
||||||
port, username, prompt_password, progname,
|
progname, echo, verbose,
|
||||||
echo, verbose, concurrently, concurrentCons);
|
concurrently, concurrentCons);
|
||||||
|
|
||||||
if (indexes.head != NULL)
|
if (indexes.head != NULL)
|
||||||
reindex_one_database(dbname, REINDEX_INDEX, &indexes, host,
|
reindex_one_database(&cparams, REINDEX_INDEX, &indexes,
|
||||||
port, username, prompt_password, progname,
|
progname, echo, verbose,
|
||||||
echo, verbose, concurrently, 1);
|
concurrently, 1);
|
||||||
|
|
||||||
if (tables.head != NULL)
|
if (tables.head != NULL)
|
||||||
reindex_one_database(dbname, REINDEX_TABLE, &tables, host,
|
reindex_one_database(&cparams, REINDEX_TABLE, &tables,
|
||||||
port, username, prompt_password, progname,
|
progname, echo, verbose,
|
||||||
echo, verbose, concurrently,
|
concurrently, concurrentCons);
|
||||||
concurrentCons);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* reindex database only if neither index nor table nor schema is
|
* reindex database only if neither index nor table nor schema is
|
||||||
* specified
|
* specified
|
||||||
*/
|
*/
|
||||||
if (indexes.head == NULL && tables.head == NULL && schemas.head == NULL)
|
if (indexes.head == NULL && tables.head == NULL && schemas.head == NULL)
|
||||||
reindex_one_database(dbname, REINDEX_DATABASE, NULL, host,
|
reindex_one_database(&cparams, REINDEX_DATABASE, NULL,
|
||||||
port, username, prompt_password, progname,
|
progname, echo, verbose,
|
||||||
echo, verbose, concurrently, concurrentCons);
|
concurrently, concurrentCons);
|
||||||
}
|
}
|
||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
reindex_one_database(const char *dbname, ReindexType type,
|
reindex_one_database(const ConnParams *cparams, ReindexType type,
|
||||||
SimpleStringList *user_list, const char *host,
|
SimpleStringList *user_list,
|
||||||
const char *port, const char *username,
|
const char *progname, bool echo,
|
||||||
enum trivalue prompt_password, const char *progname, bool echo,
|
|
||||||
bool verbose, bool concurrently, int concurrentCons)
|
bool verbose, bool concurrently, int concurrentCons)
|
||||||
{
|
{
|
||||||
PGconn *conn;
|
PGconn *conn;
|
||||||
@ -328,8 +336,7 @@ reindex_one_database(const char *dbname, ReindexType type,
|
|||||||
bool failed = false;
|
bool failed = false;
|
||||||
int items_count = 0;
|
int items_count = 0;
|
||||||
|
|
||||||
conn = connectDatabase(dbname, host, port, username, prompt_password,
|
conn = connectDatabase(cparams, progname, echo, false, false);
|
||||||
progname, echo, false, false);
|
|
||||||
|
|
||||||
if (concurrently && PQserverVersion(conn) < 120000)
|
if (concurrently && PQserverVersion(conn) < 120000)
|
||||||
{
|
{
|
||||||
@ -436,8 +443,7 @@ reindex_one_database(const char *dbname, ReindexType type,
|
|||||||
|
|
||||||
Assert(process_list != NULL);
|
Assert(process_list != NULL);
|
||||||
|
|
||||||
slots = ParallelSlotsSetup(dbname, host, port, username, prompt_password,
|
slots = ParallelSlotsSetup(cparams, progname, echo, conn, concurrentCons);
|
||||||
progname, echo, conn, concurrentCons);
|
|
||||||
|
|
||||||
cell = process_list->head;
|
cell = process_list->head;
|
||||||
do
|
do
|
||||||
@ -705,23 +711,18 @@ get_parallel_object_list(PGconn *conn, ReindexType type,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
reindex_all_databases(const char *maintenance_db,
|
reindex_all_databases(ConnParams *cparams,
|
||||||
const char *host, const char *port,
|
|
||||||
const char *username, enum trivalue prompt_password,
|
|
||||||
const char *progname, bool echo, bool quiet, bool verbose,
|
const char *progname, bool echo, bool quiet, bool verbose,
|
||||||
bool concurrently, int concurrentCons)
|
bool concurrently, int concurrentCons)
|
||||||
{
|
{
|
||||||
PGconn *conn;
|
PGconn *conn;
|
||||||
PGresult *result;
|
PGresult *result;
|
||||||
PQExpBufferData connstr;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
|
conn = connectMaintenanceDatabase(cparams, progname, echo);
|
||||||
prompt_password, progname, echo);
|
|
||||||
result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
|
result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
|
|
||||||
initPQExpBuffer(&connstr);
|
|
||||||
for (i = 0; i < PQntuples(result); i++)
|
for (i = 0; i < PQntuples(result); i++)
|
||||||
{
|
{
|
||||||
char *dbname = PQgetvalue(result, i, 0);
|
char *dbname = PQgetvalue(result, i, 0);
|
||||||
@ -732,16 +733,12 @@ reindex_all_databases(const char *maintenance_db,
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
resetPQExpBuffer(&connstr);
|
cparams->override_dbname = dbname;
|
||||||
appendPQExpBufferStr(&connstr, "dbname=");
|
|
||||||
appendConnStrVal(&connstr, dbname);
|
|
||||||
|
|
||||||
reindex_one_database(connstr.data, REINDEX_DATABASE, NULL, host,
|
reindex_one_database(cparams, REINDEX_DATABASE, NULL,
|
||||||
port, username, prompt_password,
|
|
||||||
progname, echo, verbose, concurrently,
|
progname, echo, verbose, concurrently,
|
||||||
concurrentCons);
|
concurrentCons);
|
||||||
}
|
}
|
||||||
termPQExpBuffer(&connstr);
|
|
||||||
|
|
||||||
PQclear(result);
|
PQclear(result);
|
||||||
}
|
}
|
||||||
|
@ -205,8 +205,7 @@ ParallelSlotsGetIdle(ParallelSlot *slots, int numslots)
|
|||||||
* set.
|
* set.
|
||||||
*/
|
*/
|
||||||
ParallelSlot *
|
ParallelSlot *
|
||||||
ParallelSlotsSetup(const char *dbname, const char *host, const char *port,
|
ParallelSlotsSetup(const ConnParams *cparams,
|
||||||
const char *username, bool prompt_password,
|
|
||||||
const char *progname, bool echo,
|
const char *progname, bool echo,
|
||||||
PGconn *conn, int numslots)
|
PGconn *conn, int numslots)
|
||||||
{
|
{
|
||||||
@ -221,8 +220,7 @@ ParallelSlotsSetup(const char *dbname, const char *host, const char *port,
|
|||||||
{
|
{
|
||||||
for (i = 1; i < numslots; i++)
|
for (i = 1; i < numslots; i++)
|
||||||
{
|
{
|
||||||
conn = connectDatabase(dbname, host, port, username, prompt_password,
|
conn = connectDatabase(cparams, progname, echo, false, true);
|
||||||
progname, echo, false, true);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Fail and exit immediately if trying to use a socket in an
|
* Fail and exit immediately if trying to use a socket in an
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#ifndef SCRIPTS_PARALLEL_H
|
#ifndef SCRIPTS_PARALLEL_H
|
||||||
#define SCRIPTS_PARALLEL_H
|
#define SCRIPTS_PARALLEL_H
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
#include "libpq-fe.h"
|
#include "libpq-fe.h"
|
||||||
|
|
||||||
|
|
||||||
@ -23,10 +24,7 @@ typedef struct ParallelSlot
|
|||||||
|
|
||||||
extern ParallelSlot *ParallelSlotsGetIdle(ParallelSlot *slots, int numslots);
|
extern ParallelSlot *ParallelSlotsGetIdle(ParallelSlot *slots, int numslots);
|
||||||
|
|
||||||
extern ParallelSlot *ParallelSlotsSetup(const char *dbname, const char *host,
|
extern ParallelSlot *ParallelSlotsSetup(const ConnParams *cparams,
|
||||||
const char *port,
|
|
||||||
const char *username,
|
|
||||||
bool prompt_password,
|
|
||||||
const char *progname, bool echo,
|
const char *progname, bool echo,
|
||||||
PGconn *conn, int numslots);
|
PGconn *conn, int numslots);
|
||||||
|
|
||||||
|
@ -40,19 +40,16 @@ typedef struct vacuumingOptions
|
|||||||
} vacuumingOptions;
|
} vacuumingOptions;
|
||||||
|
|
||||||
|
|
||||||
static void vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
|
static void vacuum_one_database(const ConnParams *cparams,
|
||||||
|
vacuumingOptions *vacopts,
|
||||||
int stage,
|
int stage,
|
||||||
SimpleStringList *tables,
|
SimpleStringList *tables,
|
||||||
const char *host, const char *port,
|
|
||||||
const char *username, enum trivalue prompt_password,
|
|
||||||
int concurrentCons,
|
int concurrentCons,
|
||||||
const char *progname, bool echo, bool quiet);
|
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,
|
bool analyze_in_stages,
|
||||||
const char *maintenance_db,
|
|
||||||
const char *host, const char *port,
|
|
||||||
const char *username, enum trivalue prompt_password,
|
|
||||||
int concurrentCons,
|
int concurrentCons,
|
||||||
const char *progname, bool echo, bool quiet);
|
const char *progname, bool echo, bool quiet);
|
||||||
|
|
||||||
@ -108,6 +105,7 @@ main(int argc, char *argv[])
|
|||||||
char *port = NULL;
|
char *port = NULL;
|
||||||
char *username = NULL;
|
char *username = NULL;
|
||||||
enum trivalue prompt_password = TRI_DEFAULT;
|
enum trivalue prompt_password = TRI_DEFAULT;
|
||||||
|
ConnParams cparams;
|
||||||
bool echo = false;
|
bool echo = false;
|
||||||
bool quiet = false;
|
bool quiet = false;
|
||||||
vacuumingOptions vacopts;
|
vacuumingOptions vacopts;
|
||||||
@ -287,6 +285,13 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 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(NULL);
|
setup_cancel_handler(NULL);
|
||||||
|
|
||||||
/* Avoid opening extra connections. */
|
/* Avoid opening extra connections. */
|
||||||
@ -306,10 +311,10 @@ main(int argc, char *argv[])
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
vacuum_all_databases(&vacopts,
|
cparams.dbname = maintenance_db;
|
||||||
|
|
||||||
|
vacuum_all_databases(&cparams, &vacopts,
|
||||||
analyze_in_stages,
|
analyze_in_stages,
|
||||||
maintenance_db,
|
|
||||||
host, port, username, prompt_password,
|
|
||||||
concurrentCons,
|
concurrentCons,
|
||||||
progname, echo, quiet);
|
progname, echo, quiet);
|
||||||
}
|
}
|
||||||
@ -325,25 +330,25 @@ main(int argc, char *argv[])
|
|||||||
dbname = get_user_name_or_exit(progname);
|
dbname = get_user_name_or_exit(progname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cparams.dbname = dbname;
|
||||||
|
|
||||||
if (analyze_in_stages)
|
if (analyze_in_stages)
|
||||||
{
|
{
|
||||||
int stage;
|
int stage;
|
||||||
|
|
||||||
for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++)
|
for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++)
|
||||||
{
|
{
|
||||||
vacuum_one_database(dbname, &vacopts,
|
vacuum_one_database(&cparams, &vacopts,
|
||||||
stage,
|
stage,
|
||||||
&tables,
|
&tables,
|
||||||
host, port, username, prompt_password,
|
|
||||||
concurrentCons,
|
concurrentCons,
|
||||||
progname, echo, quiet);
|
progname, echo, quiet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
vacuum_one_database(dbname, &vacopts,
|
vacuum_one_database(&cparams, &vacopts,
|
||||||
ANALYZE_NO_STAGE,
|
ANALYZE_NO_STAGE,
|
||||||
&tables,
|
&tables,
|
||||||
host, port, username, prompt_password,
|
|
||||||
concurrentCons,
|
concurrentCons,
|
||||||
progname, echo, quiet);
|
progname, echo, quiet);
|
||||||
}
|
}
|
||||||
@ -365,11 +370,10 @@ main(int argc, char *argv[])
|
|||||||
* a list of tables from the database.
|
* a list of tables from the database.
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
|
vacuum_one_database(const ConnParams *cparams,
|
||||||
|
vacuumingOptions *vacopts,
|
||||||
int stage,
|
int stage,
|
||||||
SimpleStringList *tables,
|
SimpleStringList *tables,
|
||||||
const char *host, const char *port,
|
|
||||||
const char *username, enum trivalue prompt_password,
|
|
||||||
int concurrentCons,
|
int concurrentCons,
|
||||||
const char *progname, bool echo, bool quiet)
|
const char *progname, bool echo, bool quiet)
|
||||||
{
|
{
|
||||||
@ -401,8 +405,7 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
|
|||||||
Assert(stage == ANALYZE_NO_STAGE ||
|
Assert(stage == ANALYZE_NO_STAGE ||
|
||||||
(stage >= 0 && stage < ANALYZE_NUM_STAGES));
|
(stage >= 0 && stage < ANALYZE_NUM_STAGES));
|
||||||
|
|
||||||
conn = connectDatabase(dbname, host, port, username, prompt_password,
|
conn = connectDatabase(cparams, progname, echo, false, true);
|
||||||
progname, echo, false, true);
|
|
||||||
|
|
||||||
if (vacopts->disable_page_skipping && PQserverVersion(conn) < 90600)
|
if (vacopts->disable_page_skipping && PQserverVersion(conn) < 90600)
|
||||||
{
|
{
|
||||||
@ -630,8 +633,7 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
|
|||||||
if (concurrentCons <= 0)
|
if (concurrentCons <= 0)
|
||||||
concurrentCons = 1;
|
concurrentCons = 1;
|
||||||
|
|
||||||
slots = ParallelSlotsSetup(dbname, host, port, username, prompt_password,
|
slots = ParallelSlotsSetup(cparams, progname, echo, conn, concurrentCons);
|
||||||
progname, echo, conn, concurrentCons);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prepare all the connections to run the appropriate analyze stage, if
|
* Prepare all the connections to run the appropriate analyze stage, if
|
||||||
@ -703,28 +705,23 @@ finish:
|
|||||||
* quickly everywhere before generating more detailed ones.
|
* quickly everywhere before generating more detailed ones.
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
vacuum_all_databases(vacuumingOptions *vacopts,
|
vacuum_all_databases(ConnParams *cparams,
|
||||||
|
vacuumingOptions *vacopts,
|
||||||
bool analyze_in_stages,
|
bool analyze_in_stages,
|
||||||
const char *maintenance_db, const char *host,
|
|
||||||
const char *port, const char *username,
|
|
||||||
enum trivalue prompt_password,
|
|
||||||
int concurrentCons,
|
int concurrentCons,
|
||||||
const char *progname, bool echo, bool quiet)
|
const char *progname, bool echo, bool quiet)
|
||||||
{
|
{
|
||||||
PGconn *conn;
|
PGconn *conn;
|
||||||
PGresult *result;
|
PGresult *result;
|
||||||
PQExpBufferData connstr;
|
|
||||||
int stage;
|
int stage;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
|
conn = connectMaintenanceDatabase(cparams, progname, echo);
|
||||||
prompt_password, progname, echo);
|
|
||||||
result = executeQuery(conn,
|
result = executeQuery(conn,
|
||||||
"SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;",
|
"SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;",
|
||||||
echo);
|
echo);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
|
|
||||||
initPQExpBuffer(&connstr);
|
|
||||||
if (analyze_in_stages)
|
if (analyze_in_stages)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@ -739,14 +736,11 @@ vacuum_all_databases(vacuumingOptions *vacopts,
|
|||||||
{
|
{
|
||||||
for (i = 0; i < PQntuples(result); i++)
|
for (i = 0; i < PQntuples(result); i++)
|
||||||
{
|
{
|
||||||
resetPQExpBuffer(&connstr);
|
cparams->override_dbname = PQgetvalue(result, i, 0);
|
||||||
appendPQExpBufferStr(&connstr, "dbname=");
|
|
||||||
appendConnStrVal(&connstr, PQgetvalue(result, i, 0));
|
|
||||||
|
|
||||||
vacuum_one_database(connstr.data, vacopts,
|
vacuum_one_database(cparams, vacopts,
|
||||||
stage,
|
stage,
|
||||||
NULL,
|
NULL,
|
||||||
host, port, username, prompt_password,
|
|
||||||
concurrentCons,
|
concurrentCons,
|
||||||
progname, echo, quiet);
|
progname, echo, quiet);
|
||||||
}
|
}
|
||||||
@ -756,19 +750,15 @@ vacuum_all_databases(vacuumingOptions *vacopts,
|
|||||||
{
|
{
|
||||||
for (i = 0; i < PQntuples(result); i++)
|
for (i = 0; i < PQntuples(result); i++)
|
||||||
{
|
{
|
||||||
resetPQExpBuffer(&connstr);
|
cparams->override_dbname = PQgetvalue(result, i, 0);
|
||||||
appendPQExpBufferStr(&connstr, "dbname=");
|
|
||||||
appendConnStrVal(&connstr, PQgetvalue(result, i, 0));
|
|
||||||
|
|
||||||
vacuum_one_database(connstr.data, vacopts,
|
vacuum_one_database(cparams, vacopts,
|
||||||
ANALYZE_NO_STAGE,
|
ANALYZE_NO_STAGE,
|
||||||
NULL,
|
NULL,
|
||||||
host, port, username, prompt_password,
|
|
||||||
concurrentCons,
|
concurrentCons,
|
||||||
progname, echo, quiet);
|
progname, echo, quiet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
termPQExpBuffer(&connstr);
|
|
||||||
|
|
||||||
PQclear(result);
|
PQclear(result);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user