2003-06-18 08:19:11 -04:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* clusterdb
|
|
|
|
|
*
|
2018-01-02 23:30:12 -05:00
|
|
|
* Portions Copyright (c) 2002-2018, PostgreSQL Global Development Group
|
2003-06-18 08:19:11 -04:00
|
|
|
*
|
2010-09-20 16:08:53 -04:00
|
|
|
* src/bin/scripts/clusterdb.c
|
2003-06-18 08:19:11 -04:00
|
|
|
*
|
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
|
#include "common.h"
|
2016-03-24 15:55:44 -04:00
|
|
|
#include "fe_utils/simple_list.h"
|
2016-08-08 10:07:46 -04:00
|
|
|
#include "fe_utils/string_utils.h"
|
2003-06-18 08:19:11 -04: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
2020-10-19 19:03:47 -04:00
|
|
|
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);
|
2003-06-18 08:19:11 -04:00
|
|
|
static void help(const char *progname);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
|
{"host", required_argument, NULL, 'h'},
|
|
|
|
|
{"port", required_argument, NULL, 'p'},
|
|
|
|
|
{"username", required_argument, NULL, 'U'},
|
2009-02-26 11:02:39 -05:00
|
|
|
{"no-password", no_argument, NULL, 'w'},
|
2003-06-18 08:19:11 -04:00
|
|
|
{"password", no_argument, NULL, 'W'},
|
|
|
|
|
{"echo", no_argument, NULL, 'e'},
|
|
|
|
|
{"quiet", no_argument, NULL, 'q'},
|
|
|
|
|
{"dbname", required_argument, NULL, 'd'},
|
|
|
|
|
{"all", no_argument, NULL, 'a'},
|
|
|
|
|
{"table", required_argument, NULL, 't'},
|
2008-11-24 03:46:04 -05:00
|
|
|
{"verbose", no_argument, NULL, 'v'},
|
2011-12-06 08:48:15 -05:00
|
|
|
{"maintenance-db", required_argument, NULL, 2},
|
2003-06-18 08:19:11 -04:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
|
};
|
|
|
|
|
|
2004-05-12 09:38:49 -04:00
|
|
|
const char *progname;
|
2003-06-18 08:19:11 -04:00
|
|
|
int optindex;
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
|
|
const char *dbname = NULL;
|
2011-12-06 08:48:15 -05:00
|
|
|
const char *maintenance_db = NULL;
|
2003-06-18 08:19:11 -04:00
|
|
|
char *host = NULL;
|
|
|
|
|
char *port = NULL;
|
|
|
|
|
char *username = NULL;
|
2009-02-26 11:02:39 -05:00
|
|
|
enum trivalue prompt_password = TRI_DEFAULT;
|
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
2020-10-19 19:03:47 -04:00
|
|
|
ConnParams cparams;
|
2003-06-18 08:19:11 -04:00
|
|
|
bool echo = false;
|
|
|
|
|
bool quiet = false;
|
|
|
|
|
bool alldb = false;
|
2008-11-24 03:46:04 -05:00
|
|
|
bool verbose = false;
|
2013-01-17 05:24:47 -05:00
|
|
|
SimpleStringList tables = {NULL, NULL};
|
2003-06-18 08:19:11 -04:00
|
|
|
|
|
|
|
|
progname = get_progname(argv[0]);
|
2008-12-11 02:34:09 -05:00
|
|
|
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
|
2004-05-31 22:54:09 -04:00
|
|
|
|
2003-06-18 08:19:11 -04:00
|
|
|
handle_help_version_opts(argc, argv, "clusterdb", help);
|
|
|
|
|
|
2009-02-26 11:02:39 -05:00
|
|
|
while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:at:v", long_options, &optindex)) != -1)
|
2003-06-18 08:19:11 -04:00
|
|
|
{
|
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case 'h':
|
2012-10-12 13:35:40 -04:00
|
|
|
host = pg_strdup(optarg);
|
2003-06-18 08:19:11 -04:00
|
|
|
break;
|
|
|
|
|
case 'p':
|
2012-10-12 13:35:40 -04:00
|
|
|
port = pg_strdup(optarg);
|
2003-06-18 08:19:11 -04:00
|
|
|
break;
|
|
|
|
|
case 'U':
|
2012-10-12 13:35:40 -04:00
|
|
|
username = pg_strdup(optarg);
|
2003-06-18 08:19:11 -04:00
|
|
|
break;
|
2009-02-26 11:02:39 -05:00
|
|
|
case 'w':
|
|
|
|
|
prompt_password = TRI_NO;
|
|
|
|
|
break;
|
2003-06-18 08:19:11 -04:00
|
|
|
case 'W':
|
2009-02-26 11:02:39 -05:00
|
|
|
prompt_password = TRI_YES;
|
2003-06-18 08:19:11 -04:00
|
|
|
break;
|
|
|
|
|
case 'e':
|
|
|
|
|
echo = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'q':
|
|
|
|
|
quiet = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'd':
|
2012-10-12 13:35:40 -04:00
|
|
|
dbname = pg_strdup(optarg);
|
2003-06-18 08:19:11 -04:00
|
|
|
break;
|
|
|
|
|
case 'a':
|
|
|
|
|
alldb = true;
|
|
|
|
|
break;
|
|
|
|
|
case 't':
|
2013-01-17 05:24:47 -05:00
|
|
|
simple_string_list_append(&tables, optarg);
|
2003-06-18 08:19:11 -04:00
|
|
|
break;
|
2008-11-24 03:46:04 -05:00
|
|
|
case 'v':
|
|
|
|
|
verbose = true;
|
|
|
|
|
break;
|
2011-12-06 08:48:15 -05:00
|
|
|
case 2:
|
2012-10-12 13:35:40 -04:00
|
|
|
maintenance_db = pg_strdup(optarg);
|
2011-12-06 08:48:15 -05:00
|
|
|
break;
|
2003-06-18 08:19:11 -04:00
|
|
|
default:
|
2003-07-23 04:47:41 -04:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
2003-06-18 08:19:11 -04:00
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-10 15:20:04 -04:00
|
|
|
/*
|
|
|
|
|
* Non-option argument specifies database name as long as it wasn't
|
|
|
|
|
* already specified with -d / --dbname
|
2012-04-17 18:30:34 -04:00
|
|
|
*/
|
|
|
|
|
if (optind < argc && dbname == NULL)
|
2003-06-18 08:19:11 -04:00
|
|
|
{
|
2012-04-17 18:30:34 -04:00
|
|
|
dbname = argv[optind];
|
|
|
|
|
optind++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
|
2012-09-06 15:43:46 -04:00
|
|
|
progname, argv[optind]);
|
2012-04-17 18:30:34 -04:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
|
|
|
|
exit(1);
|
2003-06-18 08:19:11 -04: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
2020-10-19 19:03:47 -04:00
|
|
|
/* 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;
|
|
|
|
|
|
2007-04-09 14:21:22 -04:00
|
|
|
setup_cancel_handler();
|
|
|
|
|
|
2003-06-18 08:19:11 -04:00
|
|
|
if (alldb)
|
|
|
|
|
{
|
|
|
|
|
if (dbname)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, _("%s: cannot cluster all databases and a specific one at the same time\n"),
|
|
|
|
|
progname);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2013-01-17 05:24:47 -05:00
|
|
|
|
|
|
|
|
if (tables.head != NULL)
|
2003-06-18 08:19:11 -04:00
|
|
|
{
|
2013-01-17 05:24:47 -05:00
|
|
|
fprintf(stderr, _("%s: cannot cluster specific table(s) in all databases\n"),
|
2003-06-18 08:19:11 -04:00
|
|
|
progname);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2003-08-03 20:43:34 -04: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
2020-10-19 19:03:47 -04:00
|
|
|
cparams.dbname = maintenance_db;
|
|
|
|
|
|
|
|
|
|
cluster_all_databases(&cparams, progname, verbose, echo, quiet);
|
2003-06-18 08:19:11 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (dbname == NULL)
|
|
|
|
|
{
|
|
|
|
|
if (getenv("PGDATABASE"))
|
|
|
|
|
dbname = getenv("PGDATABASE");
|
|
|
|
|
else if (getenv("PGUSER"))
|
|
|
|
|
dbname = getenv("PGUSER");
|
|
|
|
|
else
|
2013-12-18 12:16:16 -05:00
|
|
|
dbname = get_user_name_or_exit(progname);
|
2003-06-18 08:19:11 -04: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
2020-10-19 19:03:47 -04:00
|
|
|
cparams.dbname = dbname;
|
|
|
|
|
|
2013-01-17 05:24:47 -05:00
|
|
|
if (tables.head != NULL)
|
|
|
|
|
{
|
|
|
|
|
SimpleStringListCell *cell;
|
|
|
|
|
|
|
|
|
|
for (cell = tables.head; cell; cell = cell->next)
|
|
|
|
|
{
|
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
2020-10-19 19:03:47 -04:00
|
|
|
cluster_one_database(&cparams, cell->val,
|
|
|
|
|
progname, verbose, echo);
|
2013-01-17 05:24:47 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
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
2020-10-19 19:03:47 -04:00
|
|
|
cluster_one_database(&cparams, NULL,
|
|
|
|
|
progname, verbose, echo);
|
2003-06-18 08:19:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-01 14:27:15 -05:00
|
|
|
static void
|
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
2020-10-19 19:03:47 -04:00
|
|
|
cluster_one_database(const ConnParams *cparams, const char *table,
|
|
|
|
|
const char *progname, bool verbose, bool echo)
|
2003-06-18 08:19:11 -04:00
|
|
|
{
|
|
|
|
|
PQExpBufferData sql;
|
|
|
|
|
|
|
|
|
|
PGconn *conn;
|
|
|
|
|
|
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
2020-10-19 19:03:47 -04:00
|
|
|
conn = connectDatabase(cparams, progname, echo, false, false);
|
Empty search_path in Autovacuum and non-psql/pgbench clients.
This makes the client programs behave as documented regardless of the
connect-time search_path and regardless of user-created objects. Today,
a malicious user with CREATE permission on a search_path schema can take
control of certain of these clients' queries and invoke arbitrary SQL
functions under the client identity, often a superuser. This is
exploitable in the default configuration, where all users have CREATE
privilege on schema "public".
This changes behavior of user-defined code stored in the database, like
pg_index.indexprs and pg_extension_config_dump(). If they reach code
bearing unqualified names, "does not exist" or "no schema has been
selected to create in" errors might appear. Users may fix such errors
by schema-qualifying affected names. After upgrading, consider watching
server logs for these errors.
The --table arguments of src/bin/scripts clients have been lax; for
example, "vacuumdb -Zt pg_am\;CHECKPOINT" performed a checkpoint. That
now fails, but for now, "vacuumdb -Zt 'pg_am(amname);CHECKPOINT'" still
performs a checkpoint.
Back-patch to 9.3 (all supported versions).
Reviewed by Tom Lane, though this fix strategy was not his first choice.
Reported by Arseniy Sharoglazov.
Security: CVE-2018-1058
2018-02-26 10:39:44 -05:00
|
|
|
|
2003-06-18 08:19:11 -04:00
|
|
|
initPQExpBuffer(&sql);
|
|
|
|
|
|
2013-11-18 11:29:01 -05:00
|
|
|
appendPQExpBufferStr(&sql, "CLUSTER");
|
2008-11-24 03:46:04 -05:00
|
|
|
if (verbose)
|
2013-11-18 11:29:01 -05:00
|
|
|
appendPQExpBufferStr(&sql, " VERBOSE");
|
2003-06-18 08:19:11 -04:00
|
|
|
if (table)
|
Empty search_path in Autovacuum and non-psql/pgbench clients.
This makes the client programs behave as documented regardless of the
connect-time search_path and regardless of user-created objects. Today,
a malicious user with CREATE permission on a search_path schema can take
control of certain of these clients' queries and invoke arbitrary SQL
functions under the client identity, often a superuser. This is
exploitable in the default configuration, where all users have CREATE
privilege on schema "public".
This changes behavior of user-defined code stored in the database, like
pg_index.indexprs and pg_extension_config_dump(). If they reach code
bearing unqualified names, "does not exist" or "no schema has been
selected to create in" errors might appear. Users may fix such errors
by schema-qualifying affected names. After upgrading, consider watching
server logs for these errors.
The --table arguments of src/bin/scripts clients have been lax; for
example, "vacuumdb -Zt pg_am\;CHECKPOINT" performed a checkpoint. That
now fails, but for now, "vacuumdb -Zt 'pg_am(amname);CHECKPOINT'" still
performs a checkpoint.
Back-patch to 9.3 (all supported versions).
Reviewed by Tom Lane, though this fix strategy was not his first choice.
Reported by Arseniy Sharoglazov.
Security: CVE-2018-1058
2018-02-26 10:39:44 -05:00
|
|
|
{
|
|
|
|
|
appendPQExpBufferChar(&sql, ' ');
|
|
|
|
|
appendQualifiedRelation(&sql, table, conn, progname, echo);
|
|
|
|
|
}
|
2015-07-02 05:32:48 -04:00
|
|
|
appendPQExpBufferChar(&sql, ';');
|
2003-06-18 08:19:11 -04:00
|
|
|
|
2007-04-09 14:21:22 -04:00
|
|
|
if (!executeMaintenanceCommand(conn, sql.data, echo))
|
2003-06-18 08:19:11 -04:00
|
|
|
{
|
|
|
|
|
if (table)
|
|
|
|
|
fprintf(stderr, _("%s: clustering of table \"%s\" in database \"%s\" failed: %s"),
|
2016-08-08 10:07:46 -04:00
|
|
|
progname, table, PQdb(conn), PQerrorMessage(conn));
|
2003-06-18 08:19:11 -04:00
|
|
|
else
|
|
|
|
|
fprintf(stderr, _("%s: clustering of database \"%s\" failed: %s"),
|
2016-08-08 10:07:46 -04:00
|
|
|
progname, PQdb(conn), PQerrorMessage(conn));
|
2003-06-18 08:19:11 -04:00
|
|
|
PQfinish(conn);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
PQfinish(conn);
|
|
|
|
|
termPQExpBuffer(&sql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-01 14:27:15 -05:00
|
|
|
static void
|
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
2020-10-19 19:03:47 -04:00
|
|
|
cluster_all_databases(ConnParams *cparams, const char *progname,
|
|
|
|
|
bool verbose, bool echo, bool quiet)
|
2003-06-18 08:19:11 -04:00
|
|
|
{
|
|
|
|
|
PGconn *conn;
|
|
|
|
|
PGresult *result;
|
|
|
|
|
int i;
|
|
|
|
|
|
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
2020-10-19 19:03:47 -04:00
|
|
|
conn = connectMaintenanceDatabase(cparams, progname, echo);
|
2007-02-13 13:06:18 -05:00
|
|
|
result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
|
2003-06-18 08:19:11 -04:00
|
|
|
PQfinish(conn);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < PQntuples(result); i++)
|
|
|
|
|
{
|
|
|
|
|
char *dbname = PQgetvalue(result, i, 0);
|
|
|
|
|
|
|
|
|
|
if (!quiet)
|
2007-06-04 06:02:40 -04:00
|
|
|
{
|
|
|
|
|
printf(_("%s: clustering database \"%s\"\n"), progname, dbname);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
2003-06-18 08:19:11 -04: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
2020-10-19 19:03:47 -04:00
|
|
|
cparams->override_dbname = dbname;
|
2016-08-08 10:07:46 -04: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
2020-10-19 19:03:47 -04:00
|
|
|
cluster_one_database(cparams, NULL, progname, verbose, echo);
|
2003-06-18 08:19:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PQclear(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
help(const char *progname)
|
|
|
|
|
{
|
2003-07-23 04:47:41 -04:00
|
|
|
printf(_("%s clusters all previously clustered tables in a database.\n\n"), progname);
|
2003-06-18 08:19:11 -04:00
|
|
|
printf(_("Usage:\n"));
|
|
|
|
|
printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
|
|
|
|
|
printf(_("\nOptions:\n"));
|
|
|
|
|
printf(_(" -a, --all cluster all databases\n"));
|
|
|
|
|
printf(_(" -d, --dbname=DBNAME database to cluster\n"));
|
|
|
|
|
printf(_(" -e, --echo show the commands being sent to the server\n"));
|
|
|
|
|
printf(_(" -q, --quiet don't write any messages\n"));
|
2013-01-17 05:24:47 -05:00
|
|
|
printf(_(" -t, --table=TABLE cluster specific table(s) only\n"));
|
2008-11-24 03:46:04 -05:00
|
|
|
printf(_(" -v, --verbose write a lot of output\n"));
|
2012-06-17 19:44:00 -04:00
|
|
|
printf(_(" -V, --version output version information, then exit\n"));
|
|
|
|
|
printf(_(" -?, --help show this help, then exit\n"));
|
2003-06-18 08:19:11 -04:00
|
|
|
printf(_("\nConnection options:\n"));
|
|
|
|
|
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
|
|
|
|
|
printf(_(" -p, --port=PORT database server port\n"));
|
|
|
|
|
printf(_(" -U, --username=USERNAME user name to connect as\n"));
|
2009-02-26 11:02:39 -05:00
|
|
|
printf(_(" -w, --no-password never prompt for password\n"));
|
2007-12-11 14:57:32 -05:00
|
|
|
printf(_(" -W, --password force password prompt\n"));
|
2011-12-06 08:48:15 -05:00
|
|
|
printf(_(" --maintenance-db=DBNAME alternate maintenance database\n"));
|
2003-06-18 08:19:11 -04:00
|
|
|
printf(_("\nRead the description of the SQL command CLUSTER for details.\n"));
|
|
|
|
|
printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
|
|
|
|
|
}
|