1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-05 07:21:24 +03:00

REINDEX SCHEMA

Add new SCHEMA option to REINDEX and reindexdb.

Sawada Masahiko

Reviewed by Michael Paquier and Fabrízio de Royes Mello
This commit is contained in:
Simon Riggs
2014-12-09 00:28:00 +09:00
parent 8001fe67a3
commit fe263d115a
12 changed files with 253 additions and 61 deletions

View File

@ -41,6 +41,7 @@ main(int argc, char *argv[])
{"password", no_argument, NULL, 'W'},
{"echo", no_argument, NULL, 'e'},
{"quiet", no_argument, NULL, 'q'},
{"schema", required_argument, NULL, 'S'},
{"dbname", required_argument, NULL, 'd'},
{"all", no_argument, NULL, 'a'},
{"system", no_argument, NULL, 's'},
@ -66,6 +67,7 @@ main(int argc, char *argv[])
bool quiet = false;
SimpleStringList indexes = {NULL, NULL};
SimpleStringList tables = {NULL, NULL};
SimpleStringList schemas = {NULL, NULL};
progname = get_progname(argv[0]);
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
@ -73,7 +75,7 @@ main(int argc, char *argv[])
handle_help_version_opts(argc, argv, "reindexdb", help);
/* process command-line options */
while ((c = getopt_long(argc, argv, "h:p:U:wWeqd:ast:i:", long_options, &optindex)) != -1)
while ((c = getopt_long(argc, argv, "h:p:U:wWeqS:d:ast:i:", long_options, &optindex)) != -1)
{
switch (c)
{
@ -98,6 +100,9 @@ main(int argc, char *argv[])
case 'q':
quiet = true;
break;
case 'S':
simple_string_list_append(&schemas, optarg);
break;
case 'd':
dbname = pg_strdup(optarg);
break;
@ -154,6 +159,11 @@ main(int argc, char *argv[])
fprintf(stderr, _("%s: cannot reindex all databases and system catalogs at the same time\n"), progname);
exit(1);
}
if (schemas.head != NULL)
{
fprintf(stderr, _("%s: cannot reindex specific schema(s) in all databases\n"), progname);
exit(1);
}
if (tables.head != NULL)
{
fprintf(stderr, _("%s: cannot reindex specific table(s) in all databases\n"), progname);
@ -170,6 +180,11 @@ main(int argc, char *argv[])
}
else if (syscatalog)
{
if (schemas.head != NULL)
{
fprintf(stderr, _("%s: cannot reindex specific schema(s) and system catalogs at the same time\n"), progname);
exit(1);
}
if (tables.head != NULL)
{
fprintf(stderr, _("%s: cannot reindex specific table(s) and system catalogs at the same time\n"), progname);
@ -206,6 +221,17 @@ main(int argc, char *argv[])
dbname = get_user_name_or_exit(progname);
}
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);
}
}
if (indexes.head != NULL)
{
SimpleStringListCell *cell;
@ -226,8 +252,8 @@ main(int argc, char *argv[])
username, prompt_password, progname, echo);
}
}
/* reindex database only if neither index nor table is specified */
if (indexes.head == NULL && tables.head == NULL)
/* reindex database only if neither index nor table nor schema is specified */
if (indexes.head == NULL && tables.head == NULL && schemas.head == NULL)
reindex_one_database(dbname, dbname, "DATABASE", host, port,
username, prompt_password, progname, echo);
}
@ -251,6 +277,8 @@ reindex_one_database(const char *name, const char *dbname, const char *type,
appendPQExpBuffer(&sql, " TABLE %s", name);
else if (strcmp(type, "INDEX") == 0)
appendPQExpBuffer(&sql, " INDEX %s", name);
else if (strcmp(type, "SCHEMA") == 0)
appendPQExpBuffer(&sql, " SCHEMA %s", name);
else if (strcmp(type, "DATABASE") == 0)
appendPQExpBuffer(&sql, " DATABASE %s", fmtId(name));
appendPQExpBufferStr(&sql, ";");
@ -266,6 +294,9 @@ reindex_one_database(const char *name, const char *dbname, const char *type,
if (strcmp(type, "INDEX") == 0)
fprintf(stderr, _("%s: reindexing of index \"%s\" in database \"%s\" failed: %s"),
progname, name, dbname, PQerrorMessage(conn));
if (strcmp(type, "SCHEMA") == 0)
fprintf(stderr, _("%s: reindexing of schema \"%s\" in database \"%s\" failed: %s"),
progname, name, dbname, PQerrorMessage(conn));
else
fprintf(stderr, _("%s: reindexing of database \"%s\" failed: %s"),
progname, dbname, PQerrorMessage(conn));
@ -348,6 +379,7 @@ help(const char *progname)
printf(_(" -i, --index=INDEX recreate specific index(es) only\n"));
printf(_(" -q, --quiet don't write any messages\n"));
printf(_(" -s, --system reindex system catalogs\n"));
printf(_(" -S, --schema=SCHEMA recreate specific schema(s) only\n"));
printf(_(" -t, --table=TABLE reindex specific table(s) only\n"));
printf(_(" -V, --version output version information, then exit\n"));
printf(_(" -?, --help show this help, then exit\n"));

View File

@ -1,7 +1,7 @@
use strict;
use warnings;
use TestLib;
use Test::More tests => 16;
use Test::More tests => 17;
program_help_ok('reindexdb');
program_version_ok('reindexdb');
@ -27,7 +27,10 @@ issues_sql_like(
[ 'reindexdb', 'postgres', '-i', 'test1x' ],
qr/statement: REINDEX INDEX test1x;/,
'reindex specific index');
issues_sql_like(
[ 'reindexdb', 'postgres', '-S', 'pg_catalog' ],
qr/statement: REINDEX SCHEMA pg_catalog;/,
'reindex specific schema');
issues_sql_like(
[ 'reindexdb', 'postgres', '-s' ],
qr/statement: REINDEX SYSTEM postgres;/,