diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c index 665864fd22..5726a15ce5 100644 --- a/src/bin/scripts/reindexdb.c +++ b/src/bin/scripts/reindexdb.c @@ -33,7 +33,11 @@ typedef enum ReindexType } ReindexType; -static SimpleStringList *get_parallel_object_list(PGconn *conn, +static SimpleStringList *get_parallel_tables_list(PGconn *conn, + ReindexType type, + SimpleStringList *user_list, + bool echo); +static SimpleStringList *get_parallel_tabidx_list(PGconn *conn, ReindexType type, SimpleStringList *user_list, bool echo); @@ -333,26 +337,32 @@ reindex_one_database(ConnParams *cparams, ReindexType type, case REINDEX_DATABASE: /* Build a list of relations from the database */ - process_list = get_parallel_object_list(conn, process_type, + process_list = get_parallel_tables_list(conn, process_type, user_list, echo); process_type = REINDEX_TABLE; /* Bail out if nothing to process */ if (process_list == NULL) + { + PQfinish(conn); return; + } break; case REINDEX_SCHEMA: Assert(user_list != NULL); /* Build a list of relations from all the schemas */ - process_list = get_parallel_object_list(conn, process_type, + process_list = get_parallel_tables_list(conn, process_type, user_list, echo); process_type = REINDEX_TABLE; /* Bail out if nothing to process */ if (process_list == NULL) + { + PQfinish(conn); return; + } break; case REINDEX_INDEX: @@ -362,21 +372,16 @@ reindex_one_database(ConnParams *cparams, ReindexType type, * Build a list of relations from the indices. This will * accordingly reorder the list of indices too. */ - indices_tables_list = get_parallel_object_list(conn, process_type, + indices_tables_list = get_parallel_tabidx_list(conn, process_type, user_list, echo); - /* - * Bail out if nothing to process. 'user_list' was modified - * in-place, so check if it has at least one cell. - */ - if (user_list->head == NULL) + /* Bail out if nothing to process */ + if (indices_tables_list == NULL) + { + PQfinish(conn); return; + } - /* - * Assuming 'user_list' is not empty, 'indices_tables_list' - * shouldn't be empty as well. - */ - Assert(indices_tables_list != NULL); indices_tables_cell = indices_tables_list->head; break; @@ -613,7 +618,7 @@ run_reindex_command(PGconn *conn, ReindexType type, const char *name, } /* - * Prepare the list of objects to process by querying the catalogs. + * Prepare the list of tables to process by querying the catalogs. * * This function will return a SimpleStringList object containing the entire * list of tables in the given database that should be processed by a parallel @@ -621,15 +626,13 @@ run_reindex_command(PGconn *conn, ReindexType type, const char *name, * table. */ static SimpleStringList * -get_parallel_object_list(PGconn *conn, ReindexType type, +get_parallel_tables_list(PGconn *conn, ReindexType type, SimpleStringList *user_list, bool echo) { PQExpBufferData catalog_query; - PQExpBufferData buf; PGresult *res; SimpleStringList *tables; - int ntups, - i; + int ntups; initPQExpBuffer(&catalog_query); @@ -658,7 +661,6 @@ get_parallel_object_list(PGconn *conn, ReindexType type, case REINDEX_SCHEMA: { SimpleStringListCell *cell; - bool nsp_listed = false; Assert(user_list != NULL); @@ -680,14 +682,10 @@ get_parallel_object_list(PGconn *conn, ReindexType type, for (cell = user_list->head; cell; cell = cell->next) { - const char *nspname = cell->val; - - if (nsp_listed) + if (cell != user_list->head) appendPQExpBufferStr(&catalog_query, ", "); - else - nsp_listed = true; - appendStringLiteralConn(&catalog_query, nspname, conn); + appendStringLiteralConn(&catalog_query, cell->val, conn); } appendPQExpBufferStr(&catalog_query, ")\n" @@ -695,6 +693,69 @@ get_parallel_object_list(PGconn *conn, ReindexType type, } break; + case REINDEX_INDEX: + case REINDEX_SYSTEM: + case REINDEX_TABLE: + Assert(false); + break; + } + + res = executeQuery(conn, catalog_query.data, echo); + termPQExpBuffer(&catalog_query); + + /* + * If no rows are returned, there are no matching tables, so we are done. + */ + ntups = PQntuples(res); + if (ntups == 0) + { + PQclear(res); + return NULL; + } + + tables = pg_malloc0(sizeof(SimpleStringList)); + + /* Build qualified identifiers for each table */ + for (int i = 0; i < ntups; i++) + { + simple_string_list_append(tables, + fmtQualifiedIdEnc(PQgetvalue(res, i, 1), + PQgetvalue(res, i, 0), + PQclientEncoding(conn))); + } + PQclear(res); + + return tables; +} + +/* + * Prepare the list of tables to process by querying the catalogs. + * + * This function will return a SimpleStringList object containing the entire + * list of tables in the given database that should be processed by a parallel + * database-wide reindex (excluding system tables), or NULL if there's no such + * table. + */ +static SimpleStringList * +get_parallel_tabidx_list(PGconn *conn, ReindexType type, + SimpleStringList *user_list, bool echo) +{ + PQExpBufferData catalog_query; + PGresult *res; + SimpleStringList *tables; + int ntups; + + initPQExpBuffer(&catalog_query); + + /* + * The queries here are using a safe search_path, so there's no need to + * fully qualify everything. + */ + switch (type) + { + case REINDEX_DATABASE: + Assert(false); + break; case REINDEX_INDEX: { SimpleStringListCell *cell; @@ -765,40 +826,28 @@ get_parallel_object_list(PGconn *conn, ReindexType type, if (ntups == 0) { PQclear(res); - PQfinish(conn); return NULL; } tables = pg_malloc0(sizeof(SimpleStringList)); - /* Build qualified identifiers for each table */ - initPQExpBuffer(&buf); - for (i = 0; i < ntups; i++) + /* Build qualified identifiers for each index */ + for (int i = 0; i < ntups; i++) { - appendPQExpBufferStr(&buf, + simple_string_list_append(tables, fmtQualifiedIdEnc(PQgetvalue(res, i, 1), PQgetvalue(res, i, 0), PQclientEncoding(conn))); - simple_string_list_append(tables, buf.data); - resetPQExpBuffer(&buf); - - if (type == REINDEX_INDEX) - { - /* - * For index-level REINDEX, rebuild the list of indexes to match - * the order of tables list. - */ - appendPQExpBufferStr(&buf, - fmtQualifiedIdEnc(PQgetvalue(res, i, 1), - PQgetvalue(res, i, 2), - PQclientEncoding(conn))); - - simple_string_list_append(user_list, buf.data); - resetPQExpBuffer(&buf); - } + /* + * For index-level REINDEX, rebuild the list of indices to match + * the order of tables list. + */ + simple_string_list_append(user_list, + fmtQualifiedIdEnc(PQgetvalue(res, i, 1), + PQgetvalue(res, i, 2), + PQclientEncoding(conn))); } - termPQExpBuffer(&buf); PQclear(res); return tables;