16 #include "catalog/pg_class_d.h"
44 bool echo,
bool verbose,
bool concurrently,
48 bool quiet,
bool verbose,
bool concurrently,
55 bool concurrently,
bool async,
61 main(
int argc,
char *argv[])
63 static struct option long_options[] = {
90 const char *maintenance_db = NULL;
91 const char *host = NULL;
92 const char *
port = NULL;
97 bool syscatalog =
false;
102 bool concurrently =
false;
106 int concurrentCons = 1;
115 while ((
c =
getopt_long(argc, argv,
"ad:eh:i:j:qp:sS:t:U:vwW", long_options, &optindex)) != -1)
194 pg_log_error(
"too many command-line arguments (first is \"%s\")",
209 if (concurrentCons > 1 && syscatalog)
210 pg_fatal(
"cannot use multiple jobs to reindex system catalogs");
215 pg_fatal(
"cannot reindex all databases and a specific one at the same time");
217 cparams.
dbname = maintenance_db;
221 syscatalog, &schemas, &tables, &indexes);
227 if (getenv(
"PGDATABASE"))
228 dbname = getenv(
"PGDATABASE");
229 else if (getenv(
"PGUSER"))
230 dbname = getenv(
"PGUSER");
242 if (schemas.
head != NULL)
247 if (indexes.
head != NULL)
252 if (tables.
head != NULL)
261 if (!syscatalog && indexes.
head == NULL &&
262 tables.
head == NULL && schemas.
head == NULL)
275 bool verbose,
bool concurrently,
int concurrentCons,
281 bool parallel = concurrentCons > 1;
288 char *prev_index_table_name = NULL;
296 pg_fatal(
"cannot use the \"%s\" option on server versions older than PostgreSQL %s",
297 "concurrently",
"12");
303 pg_fatal(
"cannot use the \"%s\" option on server versions older than PostgreSQL %s",
309 switch (process_type)
318 Assert(user_list == NULL);
326 Assert(user_list != NULL);
332 switch (process_type)
342 if (process_list == NULL)
347 Assert(user_list != NULL);
355 if (process_list == NULL)
360 Assert(user_list != NULL);
373 if (user_list->
head == NULL)
380 Assert(indices_tables_list != NULL);
381 indices_tables_cell = indices_tables_list->
head;
405 for (cell = process_list->
head; cell; cell = cell->
next)
410 if (items_count >= concurrentCons)
413 concurrentCons =
Min(concurrentCons, items_count);
414 Assert(concurrentCons > 0);
416 Assert(process_list != NULL);
421 cell = process_list->
head;
424 const char *objname = cell->
val;
425 bool need_new_slot =
true;
441 if (prev_index_table_name != NULL &&
442 strcmp(prev_index_table_name, indices_tables_cell->
val) == 0)
443 need_new_slot =
false;
444 prev_index_table_name = indices_tables_cell->
val;
445 indices_tables_cell = indices_tables_cell->
next;
464 }
while (cell != NULL);
470 if (process_list != user_list)
476 if (indices_tables_list)
491 bool echo,
bool verbose,
bool concurrently,
bool async,
494 const char *paren =
"(";
495 const char *
comma =
", ";
496 const char *sep = paren;
584 pg_log_error(
"reindexing of database \"%s\" failed: %s",
588 pg_log_error(
"reindexing of index \"%s\" in database \"%s\" failed: %s",
592 pg_log_error(
"reindexing of schema \"%s\" in database \"%s\" failed: %s",
596 pg_log_error(
"reindexing of system catalogs in database \"%s\" failed: %s",
600 pg_log_error(
"reindexing of table \"%s\" in database \"%s\" failed: %s",
642 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 ns.nspname != 'pg_catalog'\n"
649 " AND c.relkind IN ("
652 " ORDER BY c.relpages DESC;");
658 bool nsp_listed =
false;
660 Assert(user_list != NULL);
667 "SELECT c.relname, ns.nspname\n"
668 " FROM pg_catalog.pg_class c\n"
669 " JOIN pg_catalog.pg_namespace ns"
670 " ON c.relnamespace = ns.oid\n"
671 " WHERE c.relkind IN ("
674 " AND ns.nspname IN (");
676 for (cell = user_list->
head; cell; cell = cell->
next)
678 const char *nspname = cell->
val;
689 " ORDER BY c.relpages DESC;");
697 Assert(user_list != NULL);
712 "SELECT t.relname, n.nspname, i.relname\n"
713 "FROM pg_catalog.pg_index x\n"
714 "JOIN pg_catalog.pg_class t ON t.oid = x.indrelid\n"
715 "JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid\n"
716 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.relnamespace\n"
717 "WHERE x.indexrelid OPERATOR(pg_catalog.=) ANY(ARRAY['");
719 for (cell = user_list->
head; cell; cell = cell->
next)
721 if (cell != user_list->
head)
732 "']::pg_catalog.regclass[])\n"
733 "ORDER BY max(i.relpages) OVER \n"
734 " (PARTITION BY n.nspname, t.relname),\n"
735 " n.nspname, t.relname, i.relpages;\n");
743 user_list->
head = user_list->
tail = NULL;
771 for (
i = 0;
i < ntups;
i++)
803 bool concurrently,
int concurrentCons,
814 "SELECT datname FROM pg_database WHERE datallowconn AND datconnlimit <> -2 ORDER BY 1;",
835 if (schemas->
head != NULL)
840 if (indexes->
head != NULL)
845 if (tables->
head != NULL)
854 if (!syscatalog && indexes->
head == NULL &&
855 tables->
head == NULL && schemas->
head == NULL)
871 printf(
_(
" -a, --all reindex all databases\n"));
872 printf(
_(
" --concurrently reindex concurrently\n"));
873 printf(
_(
" -d, --dbname=DBNAME database to reindex\n"));
874 printf(
_(
" -e, --echo show the commands being sent to the server\n"));
875 printf(
_(
" -i, --index=INDEX recreate specific index(es) only\n"));
876 printf(
_(
" -j, --jobs=NUM use this many concurrent connections to reindex\n"));
877 printf(
_(
" -q, --quiet don't write any messages\n"));
878 printf(
_(
" -s, --system reindex system catalogs only\n"));
879 printf(
_(
" -S, --schema=SCHEMA reindex specific schema(s) only\n"));
880 printf(
_(
" -t, --table=TABLE reindex specific table(s) only\n"));
881 printf(
_(
" --tablespace=TABLESPACE tablespace where indexes are rebuilt\n"));
882 printf(
_(
" -v, --verbose write a lot of output\n"));
883 printf(
_(
" -V, --version output version information, then exit\n"));
884 printf(
_(
" -?, --help show this help, then exit\n"));
885 printf(
_(
"\nConnection options:\n"));
886 printf(
_(
" -h, --host=HOSTNAME database server host or socket directory\n"));
887 printf(
_(
" -p, --port=PORT database server port\n"));
888 printf(
_(
" -U, --username=USERNAME user name to connect as\n"));
889 printf(
_(
" -w, --no-password never prompt for password\n"));
890 printf(
_(
" -W, --password force password prompt\n"));
891 printf(
_(
" --maintenance-db=DBNAME alternate maintenance database\n"));
892 printf(
_(
"\nRead the description of the SQL command REINDEX for details.\n"));
893 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
894 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
void appendQualifiedRelation(PQExpBuffer buf, const char *spec, PGconn *conn, bool echo)
#define Assert(condition)
#define PG_TEXTDOMAIN(domain)
volatile sig_atomic_t CancelRequested
void setup_cancel_handler(void(*query_cancel_callback)(void))
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)
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)
int main(int argc, char *argv[])
static void reindex_all_databases(ConnParams *cparams, const char *progname, bool echo, bool quiet, bool verbose, bool concurrently, int concurrentCons, const char *tablespace, bool syscatalog, SimpleStringList *schemas, SimpleStringList *tables, SimpleStringList *indexes)
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
SimpleStringListCell * tail
enum trivalue prompt_password
const char * get_user_name_or_exit(const char *progname)