diff options
Diffstat (limited to 'src/bin/scripts/reindexdb.c')
-rw-r--r-- | src/bin/scripts/reindexdb.c | 50 |
1 files changed, 36 insertions, 14 deletions
diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c index 1cd1ccc951f..438500cb08a 100644 --- a/src/bin/scripts/reindexdb.c +++ b/src/bin/scripts/reindexdb.c @@ -19,16 +19,17 @@ static void reindex_one_database(const char *name, const char *dbname, const char *type, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, - bool echo, bool verbose); + bool echo, bool verbose, bool concurrently); static void reindex_all_databases(const char *maintenance_db, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, - bool quiet, bool verbose); + bool quiet, bool verbose, bool concurrently); static void reindex_system_catalogs(const char *dbname, const char *host, const char *port, const char *username, enum trivalue prompt_password, - const char *progname, bool echo, bool verbose); + const char *progname, bool echo, bool verbose, + bool concurrently); static void help(const char *progname); int @@ -49,6 +50,7 @@ main(int argc, char *argv[]) {"table", required_argument, NULL, 't'}, {"index", required_argument, NULL, 'i'}, {"verbose", no_argument, NULL, 'v'}, + {"concurrently", no_argument, NULL, 1}, {"maintenance-db", required_argument, NULL, 2}, {NULL, 0, NULL, 0} }; @@ -68,6 +70,7 @@ main(int argc, char *argv[]) bool echo = false; bool quiet = false; bool verbose = false; + bool concurrently = false; SimpleStringList indexes = {NULL, NULL}; SimpleStringList tables = {NULL, NULL}; SimpleStringList schemas = {NULL, NULL}; @@ -124,6 +127,9 @@ main(int argc, char *argv[]) case 'v': verbose = true; break; + case 1: + concurrently = true; + break; case 2: maintenance_db = pg_strdup(optarg); break; @@ -182,7 +188,7 @@ main(int argc, char *argv[]) } reindex_all_databases(maintenance_db, host, port, username, - prompt_password, progname, echo, quiet, verbose); + prompt_password, progname, echo, quiet, verbose, concurrently); } else if (syscatalog) { @@ -213,7 +219,7 @@ main(int argc, char *argv[]) } reindex_system_catalogs(dbname, host, port, username, prompt_password, - progname, echo, verbose); + progname, echo, verbose, concurrently); } else { @@ -234,7 +240,7 @@ main(int argc, char *argv[]) for (cell = schemas.head; cell; cell = cell->next) { reindex_one_database(cell->val, dbname, "SCHEMA", host, port, - username, prompt_password, progname, echo, verbose); + username, prompt_password, progname, echo, verbose, concurrently); } } @@ -245,7 +251,7 @@ main(int argc, char *argv[]) for (cell = indexes.head; cell; cell = cell->next) { reindex_one_database(cell->val, dbname, "INDEX", host, port, - username, prompt_password, progname, echo, verbose); + username, prompt_password, progname, echo, verbose, concurrently); } } if (tables.head != NULL) @@ -255,7 +261,7 @@ main(int argc, char *argv[]) for (cell = tables.head; cell; cell = cell->next) { reindex_one_database(cell->val, dbname, "TABLE", host, port, - username, prompt_password, progname, echo, verbose); + username, prompt_password, progname, echo, verbose, concurrently); } } @@ -265,7 +271,7 @@ main(int argc, char *argv[]) */ if (indexes.head == NULL && tables.head == NULL && schemas.head == NULL) reindex_one_database(NULL, dbname, "DATABASE", host, port, - username, prompt_password, progname, echo, verbose); + username, prompt_password, progname, echo, verbose, concurrently); } exit(0); @@ -275,7 +281,7 @@ static void reindex_one_database(const char *name, const char *dbname, const char *type, const char *host, const char *port, const char *username, enum trivalue prompt_password, const char *progname, bool echo, - bool verbose) + bool verbose, bool concurrently) { PQExpBufferData sql; @@ -284,6 +290,14 @@ reindex_one_database(const char *name, const char *dbname, const char *type, conn = connectDatabase(dbname, host, port, username, prompt_password, progname, echo, false, false); + if (concurrently && PQserverVersion(conn) < 120000) + { + PQfinish(conn); + fprintf(stderr, _("%s: cannot use the \"%s\" option on server versions older than PostgreSQL %s\n"), + progname, "concurrently", "12"); + exit(1); + } + initPQExpBuffer(&sql); appendPQExpBufferStr(&sql, "REINDEX "); @@ -293,6 +307,8 @@ reindex_one_database(const char *name, const char *dbname, const char *type, appendPQExpBufferStr(&sql, type); appendPQExpBufferChar(&sql, ' '); + if (concurrently) + appendPQExpBufferStr(&sql, "CONCURRENTLY "); if (strcmp(type, "TABLE") == 0 || strcmp(type, "INDEX") == 0) appendQualifiedRelation(&sql, name, conn, progname, echo); @@ -328,7 +344,8 @@ static void reindex_all_databases(const char *maintenance_db, 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) { PGconn *conn; PGresult *result; @@ -357,7 +374,7 @@ reindex_all_databases(const char *maintenance_db, reindex_one_database(NULL, connstr.data, "DATABASE", host, port, username, prompt_password, - progname, echo, verbose); + progname, echo, verbose, concurrently); } termPQExpBuffer(&connstr); @@ -367,7 +384,7 @@ reindex_all_databases(const char *maintenance_db, static void reindex_system_catalogs(const char *dbname, const char *host, const char *port, const char *username, enum trivalue prompt_password, - const char *progname, bool echo, bool verbose) + const char *progname, bool echo, bool verbose, bool concurrently) { PGconn *conn; PQExpBufferData sql; @@ -382,7 +399,11 @@ reindex_system_catalogs(const char *dbname, const char *host, const char *port, if (verbose) appendPQExpBuffer(&sql, " (VERBOSE)"); - appendPQExpBuffer(&sql, " SYSTEM %s;", fmtId(PQdb(conn))); + appendPQExpBufferStr(&sql, " SYSTEM "); + if (concurrently) + appendPQExpBuffer(&sql, "CONCURRENTLY "); + appendPQExpBufferStr(&sql, fmtId(PQdb(conn))); + appendPQExpBufferChar(&sql, ';'); if (!executeMaintenanceCommand(conn, sql.data, echo)) { @@ -403,6 +424,7 @@ help(const char *progname) printf(_(" %s [OPTION]... [DBNAME]\n"), progname); printf(_("\nOptions:\n")); printf(_(" -a, --all reindex all databases\n")); + printf(_(" --concurrently reindex concurrently\n")); printf(_(" -d, --dbname=DBNAME database to reindex\n")); printf(_(" -e, --echo show the commands being sent to the server\n")); printf(_(" -i, --index=INDEX recreate specific index(es) only\n")); |