Hi,

While working on a PG12-instance I noticed that the progress reporting of
concurrent index creation for non-index relations fails to update the
index/relation OIDs that it's currently working on in the
pg_stat_progress_create_index view after creating the indexes. Please find
attached a patch which properly sets these values at the appropriate places.

Any thoughts?

Matthias van de Meent
From f41da096b1f36118917fe345e2a6fc89530a40c9 Mon Sep 17 00:00:00 2001
From: Matthias van de Meent <boekew...@gmail.com>
Date: Thu, 24 Sep 2020 20:41:10 +0200
Subject: [PATCH] Report the active index for reindex table concurrently

The pgstat_progress reporting for the multi-index path of reindexing
failed to set the index and relation OIDs correctly for various stages,
which resulted in a pg_stat_progress_create_index view that did not
accurately represent the index that the progress was being reported on.

This commit tags the correct index and relation for each of the concurrent
index creation stages.

Signed-off-by: Matthias van de Meent <boekew...@gmail.com>
---
 src/backend/commands/indexcmds.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f1b5f87e6a..b2f04012a4 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -3431,6 +3431,15 @@ ReindexRelationConcurrently(Oid relationOid, int options)
 		heapId = indexRel->rd_index->indrelid;
 		index_close(indexRel, NoLock);
 
+		/*
+		 * Configure progress reporting to report for this index.
+		 * While we're at it, reset the phase as it is cleared by start_command.
+		 */
+		pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, heapId);
+		pgstat_progress_update_param(PROGRESS_CREATEIDX_INDEX_OID, newIndexId);
+		pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
+									 PROGRESS_CREATEIDX_PHASE_WAIT_1);
+
 		/* Perform concurrent build of new index */
 		index_concurrently_build(heapId, newIndexId);
 
@@ -3477,6 +3486,15 @@ ReindexRelationConcurrently(Oid relationOid, int options)
 		snapshot = RegisterSnapshot(GetTransactionSnapshot());
 		PushActiveSnapshot(snapshot);
 
+		/*
+		 * Configure progress reporting to report for this index.
+		 * While we're at it, reset the phase as it is cleared by start_command.
+		 */
+		pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, heapId);
+		pgstat_progress_update_param(PROGRESS_CREATEIDX_INDEX_OID, newIndexId);
+		pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
+									 PROGRESS_CREATEIDX_PHASE_WAIT_2);
+
 		validate_index(heapId, newIndexId, snapshot);
 
 		/*
-- 
2.20.1

Reply via email to