From 5f3d4c5b4877b31bb95caa6086d1939aef4f3d12 Mon Sep 17 00:00:00 2001 From: Ilya Gladyshev Date: Sat, 20 Jul 2024 14:08:33 +0100 Subject: [PATCH v2 1/2] make REINDEX track partition progress --- doc/src/sgml/monitoring.sgml | 8 ++---- src/backend/catalog/index.c | 11 ++++++-- src/backend/commands/indexcmds.c | 48 ++++++++++++++++++++++++++++---- src/include/catalog/index.h | 1 + 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 55417a6fa9..00bb423288 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -5888,7 +5888,7 @@ FROM pg_stat_get_backend_idset() AS backendid; relid oid - OID of the table on which the index is being created. + OID of the table on which the command was run. @@ -5992,8 +5992,7 @@ FROM pg_stat_get_backend_idset() AS backendid; Total number of partitions on which the index is to be created or attached, including both direct and indirect partitions. - 0 during a REINDEX, or when - the index is not partitioned. + 0 when the index is not partitioned. @@ -6004,8 +6003,7 @@ FROM pg_stat_get_backend_idset() AS backendid; Number of partitions on which the index has already been created or attached, including both direct and indirect partitions. - 0 during a REINDEX, or when - the index is not partitioned. + 0 when the index is not partitioned. diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index a819b4197c..90488f9140 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -3560,6 +3560,7 @@ reindex_index(const ReindexStmt *stmt, Oid indexId, volatile bool skipped_constraint = false; PGRUsage ru0; bool progress = ((params->options & REINDEXOPT_REPORT_PROGRESS) != 0); + bool partition = ((params->options & REINDEXOPT_PARTITION) != 0); bool set_tablespace = false; pg_rusage_init(&ru0); @@ -3605,8 +3606,9 @@ reindex_index(const ReindexStmt *stmt, Oid indexId, indexId }; - pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, - heapId); + if (!partition) + pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, + heapId); pgstat_progress_update_multi_param(2, progress_cols, progress_vals); } @@ -3846,8 +3848,11 @@ reindex_index(const ReindexStmt *stmt, Oid indexId, index_close(iRel, NoLock); table_close(heapRelation, NoLock); - if (progress) + if (progress && !partition) + { + /* progress for partitions is tracked in the caller */ pgstat_progress_end_command(); + } } /* diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index c5a56c75f6..d4c1811f50 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -3217,9 +3217,16 @@ ReindexPartitions(const ReindexStmt *stmt, Oid relid, const ReindexParams *param ListCell *lc; ErrorContextCallback errcallback; ReindexErrorInfo errinfo; + ReindexParams newparams; + Oid heapId = relid; Assert(RELKIND_HAS_PARTITIONS(relkind)); + if (relkind == RELKIND_PARTITIONED_INDEX) + { + heapId = IndexGetRelation(relid, true); + } + /* * Check if this runs in a transaction block, with an error callback to * provide more context under which a problem happens. @@ -3278,11 +3285,33 @@ ReindexPartitions(const ReindexStmt *stmt, Oid relid, const ReindexParams *param MemoryContextSwitchTo(old_context); } + + { + int progress_params[3] = { + PROGRESS_CREATEIDX_COMMAND, + PROGRESS_CREATEIDX_PHASE, + PROGRESS_CREATEIDX_PARTITIONS_TOTAL + }; + int64 progress_values[3]; + + progress_values[0] = (params->options & REINDEXOPT_CONCURRENTLY) != 0 ? + PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY : + PROGRESS_CREATEIDX_COMMAND_REINDEX; + progress_values[1] = 0; + progress_values[2] = list_length(partitions); + pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, heapId); + pgstat_progress_update_multi_param(3, progress_params, progress_values); + } + /* * Process each partition listed in a separate transaction. Note that * this commits and then starts a new transaction immediately. */ - ReindexMultipleInternal(stmt, partitions, params); + newparams = *params; + newparams.options |= REINDEXOPT_PARTITION; + ReindexMultipleInternal(stmt, partitions, &newparams); + + pgstat_progress_end_command(); /* * Clean up working storage --- note we must do this after @@ -3303,6 +3332,7 @@ static void ReindexMultipleInternal(const ReindexStmt *stmt, const List *relids, const ReindexParams *params) { ListCell *l; + bool partitions = ((params->options & REINDEXOPT_PARTITION) != 0); PopActiveSnapshot(); CommitTransactionCommand(); @@ -3396,6 +3426,9 @@ ReindexMultipleInternal(const ReindexStmt *stmt, const List *relids, const Reind } CommitTransactionCommand(); + + if (partitions) + pgstat_progress_incr_param(PROGRESS_CREATEIDX_PARTITIONS_DONE, 1); } StartTransactionCommand(); @@ -3448,6 +3481,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein char *relationName = NULL; char *relationNamespace = NULL; PGRUsage ru0; + bool partition = ((params->options & REINDEXOPT_PARTITION) != 0); const int progress_index[] = { PROGRESS_CREATEIDX_COMMAND, PROGRESS_CREATEIDX_PHASE, @@ -3791,7 +3825,8 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein if (indexRel->rd_rel->relpersistence == RELPERSISTENCE_TEMP) elog(ERROR, "cannot reindex a temporary table concurrently"); - pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, idx->tableId); + if (!partition) + pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, idx->tableId); progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY; progress_vals[1] = 0; /* initializing */ @@ -3966,7 +4001,8 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein * Update progress for the index to build, with the correct parent * table involved. */ - pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, newidx->tableId); + if (!partition) + pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, newidx->tableId); progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY; progress_vals[1] = PROGRESS_CREATEIDX_PHASE_BUILD; progress_vals[2] = newidx->indexId; @@ -4030,7 +4066,8 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein * Update progress for the index to build, with the correct parent * table involved. */ - pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, newidx->tableId); + if (!partition) + pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, newidx->tableId); progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY; progress_vals[1] = PROGRESS_CREATEIDX_PHASE_VALIDATE_IDXSCAN; progress_vals[2] = newidx->indexId; @@ -4265,7 +4302,8 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein MemoryContextDelete(private_context); - pgstat_progress_end_command(); + if (!partition) + pgstat_progress_end_command(); return true; } diff --git a/src/include/catalog/index.h b/src/include/catalog/index.h index 7d434f8e65..ccba65fbbf 100644 --- a/src/include/catalog/index.h +++ b/src/include/catalog/index.h @@ -42,6 +42,7 @@ typedef struct ReindexParams #define REINDEXOPT_REPORT_PROGRESS 0x02 /* report pgstat progress */ #define REINDEXOPT_MISSING_OK 0x04 /* skip missing relations */ #define REINDEXOPT_CONCURRENTLY 0x08 /* concurrent mode */ +#define REINDEXOPT_PARTITION 0x10 /* reindexing is done as part of partitioned table/index reindex */ /* state info for validate_index bulkdelete callback */ typedef struct ValidateIndexState -- 2.39.3 (Apple Git-146)