16 #include "catalog/pg_class_d.h"
44 bool echo,
bool verbose,
bool concurrently,
48 bool quiet,
bool verbose,
bool concurrently,
52 bool concurrently,
bool async,
58 main(
int argc,
char *argv[])
60 static struct option long_options[] = {
87 const char *maintenance_db = NULL;
88 const char *host = NULL;
89 const char *
port = NULL;
94 bool syscatalog =
false;
99 bool concurrently =
false;
103 int concurrentCons = 1;
112 while ((
c =
getopt_long(argc, argv,
"ad:eh:i:j:qp:sS:t:U:vwW", long_options, &optindex)) != -1)
191 pg_log_error(
"too many command-line arguments (first is \"%s\")",
209 pg_fatal(
"cannot reindex all databases and a specific one at the same time");
211 pg_fatal(
"cannot reindex all databases and system catalogs at the same time");
212 if (schemas.
head != NULL)
213 pg_fatal(
"cannot reindex specific schema(s) in all databases");
214 if (tables.
head != NULL)
215 pg_fatal(
"cannot reindex specific table(s) in all databases");
216 if (indexes.
head != NULL)
217 pg_fatal(
"cannot reindex specific index(es) in all databases");
219 cparams.
dbname = maintenance_db;
226 if (schemas.
head != NULL)
227 pg_fatal(
"cannot reindex specific schema(s) and system catalogs at the same time");
228 if (tables.
head != NULL)
229 pg_fatal(
"cannot reindex specific table(s) and system catalogs at the same time");
230 if (indexes.
head != NULL)
231 pg_fatal(
"cannot reindex specific index(es) and system catalogs at the same time");
233 if (concurrentCons > 1)
234 pg_fatal(
"cannot use multiple jobs to reindex system catalogs");
238 if (getenv(
"PGDATABASE"))
239 dbname = getenv(
"PGDATABASE");
240 else if (getenv(
"PGUSER"))
241 dbname = getenv(
"PGUSER");
259 if (concurrentCons > 1 && indexes.
head != NULL)
260 pg_fatal(
"cannot use multiple jobs to reindex indexes");
264 if (getenv(
"PGDATABASE"))
265 dbname = getenv(
"PGDATABASE");
266 else if (getenv(
"PGUSER"))
267 dbname = getenv(
"PGUSER");
274 if (schemas.
head != NULL)
279 if (indexes.
head != NULL)
284 if (tables.
head != NULL)
293 if (indexes.
head == NULL && tables.
head == NULL && schemas.
head == NULL)
306 bool verbose,
bool concurrently,
int concurrentCons,
311 bool parallel = concurrentCons > 1;
323 pg_fatal(
"cannot use the \"%s\" option on server versions older than PostgreSQL %s",
324 "concurrently",
"12");
330 pg_fatal(
"cannot use the \"%s\" option on server versions older than PostgreSQL %s",
336 switch (process_type)
345 Assert(user_list == NULL);
353 Assert(user_list != NULL);
359 switch (process_type)
369 if (process_list == NULL)
374 Assert(user_list != NULL);
382 if (process_list == NULL)
407 for (cell = process_list->
head; cell; cell = cell->
next)
412 if (items_count >= concurrentCons)
415 concurrentCons =
Min(concurrentCons, items_count);
416 Assert(concurrentCons > 0);
418 Assert(process_list != NULL);
423 cell = process_list->
head;
426 const char *objname = cell->
val;
447 }
while (cell != NULL);
453 if (process_list != user_list)
468 bool echo,
bool verbose,
bool concurrently,
bool async,
471 const char *paren =
"(";
472 const char *
comma =
", ";
473 const char *sep = paren;
561 pg_log_error(
"reindexing of database \"%s\" failed: %s",
565 pg_log_error(
"reindexing of index \"%s\" in database \"%s\" failed: %s",
569 pg_log_error(
"reindexing of schema \"%s\" in database \"%s\" failed: %s",
573 pg_log_error(
"reindexing of system catalogs in database \"%s\" failed: %s",
577 pg_log_error(
"reindexing of table \"%s\" in database \"%s\" failed: %s",
619 Assert(user_list == NULL);
621 "SELECT c.relname, ns.nspname\n"
622 " FROM pg_catalog.pg_class c\n"
623 " JOIN pg_catalog.pg_namespace ns"
624 " ON c.relnamespace = ns.oid\n"
625 " WHERE ns.nspname != 'pg_catalog'\n"
626 " AND c.relkind IN ("
629 " ORDER BY c.relpages DESC;");
635 bool nsp_listed =
false;
637 Assert(user_list != NULL);
644 "SELECT c.relname, ns.nspname\n"
645 " FROM pg_catalog.pg_class c\n"
646 " JOIN pg_catalog.pg_namespace ns"
647 " ON c.relnamespace = ns.oid\n"
648 " WHERE c.relkind IN ("
651 " AND ns.nspname IN (");
653 for (cell = user_list->
head; cell; cell = cell->
next)
655 const char *nspname = cell->
val;
666 " ORDER BY c.relpages DESC;");
695 for (
i = 0;
i < ntups;
i++)
713 bool concurrently,
int concurrentCons,
722 "SELECT datname FROM pg_database WHERE datallowconn AND datconnlimit <> -2 ORDER BY 1;",
753 printf(
_(
" -a, --all reindex all databases\n"));
754 printf(
_(
" --concurrently reindex concurrently\n"));
755 printf(
_(
" -d, --dbname=DBNAME database to reindex\n"));
756 printf(
_(
" -e, --echo show the commands being sent to the server\n"));
757 printf(
_(
" -i, --index=INDEX recreate specific index(es) only\n"));
758 printf(
_(
" -j, --jobs=NUM use this many concurrent connections to reindex\n"));
759 printf(
_(
" -q, --quiet don't write any messages\n"));
760 printf(
_(
" -s, --system reindex system catalogs only\n"));
761 printf(
_(
" -S, --schema=SCHEMA reindex specific schema(s) only\n"));
762 printf(
_(
" -t, --table=TABLE reindex specific table(s) only\n"));
763 printf(
_(
" --tablespace=TABLESPACE tablespace where indexes are rebuilt\n"));
764 printf(
_(
" -v, --verbose write a lot of output\n"));
765 printf(
_(
" -V, --version output version information, then exit\n"));
766 printf(
_(
" -?, --help show this help, then exit\n"));
767 printf(
_(
"\nConnection options:\n"));
768 printf(
_(
" -h, --host=HOSTNAME database server host or socket directory\n"));
769 printf(
_(
" -p, --port=PORT database server port\n"));
770 printf(
_(
" -U, --username=USERNAME user name to connect as\n"));
771 printf(
_(
" -w, --no-password never prompt for password\n"));
772 printf(
_(
" -W, --password force password prompt\n"));
773 printf(
_(
" --maintenance-db=DBNAME alternate maintenance database\n"));
774 printf(
_(
"\nRead the description of the SQL command REINDEX for details.\n"));
775 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
776 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
static void setup_cancel_handler(void)
void appendQualifiedRelation(PQExpBuffer buf, const char *spec, PGconn *conn, bool echo)
#define PG_TEXTDOMAIN(domain)
volatile sig_atomic_t CancelRequested
void set_pglocale_pgservice(const char *argv0, const char *app)
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
static void PGresult * res
int PQserverVersion(const PGconn *conn)
char * PQdb(const PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
void PQfinish(PGconn *conn)
int PQntuples(const PGresult *res)
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
int PQsendQuery(PGconn *conn, const char *query)
void * pg_malloc0(size_t size)
char * pg_strdup(const char *in)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
static void const char fflush(stdout)
Assert(fmt[strlen(fmt) - 1] !='\n')
void pg_logging_init(const char *argv0)
#define pg_log_error(...)
#define pg_log_error_hint(...)
void pfree(void *pointer)
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
ParallelSlot * ParallelSlotsGetIdle(ParallelSlotArray *sa, const char *dbname)
bool ParallelSlotsWaitCompletion(ParallelSlotArray *sa)
ParallelSlotArray * ParallelSlotsSetup(int numslots, ConnParams *cparams, const char *progname, bool echo, const char *initcmd)
bool TableCommandResultHandler(PGresult *res, PGconn *conn, void *context)
void ParallelSlotsTerminate(ParallelSlotArray *sa)
void ParallelSlotsAdoptConn(ParallelSlotArray *sa, PGconn *conn)
static void ParallelSlotSetHandler(ParallelSlot *slot, ParallelSlotResultHandler handler, void *context)
static PGconn * connectDatabase(const char *dbname, const char *connection_string, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error)
static PGresult * executeQuery(PGconn *conn, const char *query)
PGDLLIMPORT char * optarg
const char * get_progname(const char *argv0)
void initPQExpBuffer(PQExpBuffer str)
void resetPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void termPQExpBuffer(PQExpBuffer str)
bool executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
static void reindex_all_databases(ConnParams *cparams, const char *progname, bool echo, bool quiet, bool verbose, bool concurrently, int concurrentCons, const char *tablespace)
int main(int argc, char *argv[])
static void help(const char *progname)
static void reindex_one_database(ConnParams *cparams, ReindexType type, SimpleStringList *user_list, const char *progname, bool echo, bool verbose, bool concurrently, int concurrentCons, const char *tablespace)
static SimpleStringList * get_parallel_object_list(PGconn *conn, ReindexType type, SimpleStringList *user_list, bool echo)
static void run_reindex_command(PGconn *conn, ReindexType type, const char *name, bool echo, bool verbose, bool concurrently, bool async, const char *tablespace)
void simple_string_list_append(SimpleStringList *list, const char *val)
void simple_string_list_destroy(SimpleStringList *list)
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
const char * fmtId(const char *rawid)
const char * fmtQualifiedId(const char *schema, const char *id)
char val[FLEXIBLE_ARRAY_MEMBER]
struct SimpleStringListCell * next
SimpleStringListCell * head
enum trivalue prompt_password
const char * get_user_name_or_exit(const char *progname)