Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1aedaba

Browse files
committed
Fix progress reporting of REINDEX CONCURRENTLY
This addresses a couple of issues with the so-said subject: - Report the correct parent relation with the index actually being rebuilt or validated. Previously, the command status remained set to the last index created for the progress of the index build and validation, which would be incorrect when working on a table that has more than one index. - Use the correct phase when waiting before the drop of the old indexes. Previously, this was reported with the same status as when waiting before the old indexes are marked as dead. Author: Matthias van de Meent, Michael Paquier Discussion: https://postgr.es/m/CAEze2WhqFgcwe1_tv=sFYhLWV2AdpfukumotJ6JNcAOQs3jufg@mail.gmail.com Backpatch-through: 12
1 parent 67b2cee commit 1aedaba

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

src/backend/commands/indexcmds.c

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,6 +2837,13 @@ ReindexRelationConcurrently(Oid relationOid, int options)
28372837
char *relationName = NULL;
28382838
char *relationNamespace = NULL;
28392839
PGRUsage ru0;
2840+
const int progress_index[] = {
2841+
PROGRESS_CREATEIDX_COMMAND,
2842+
PROGRESS_CREATEIDX_PHASE,
2843+
PROGRESS_CREATEIDX_INDEX_OID,
2844+
PROGRESS_CREATEIDX_ACCESS_METHOD_OID
2845+
};
2846+
int64 progress_vals[4];
28402847

28412848
/*
28422849
* Create a memory context that will survive forced transaction commits we
@@ -3079,12 +3086,11 @@ ReindexRelationConcurrently(Oid relationOid, int options)
30793086

30803087
pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX,
30813088
RelationGetRelid(heapRel));
3082-
pgstat_progress_update_param(PROGRESS_CREATEIDX_COMMAND,
3083-
PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY);
3084-
pgstat_progress_update_param(PROGRESS_CREATEIDX_INDEX_OID,
3085-
indexId);
3086-
pgstat_progress_update_param(PROGRESS_CREATEIDX_ACCESS_METHOD_OID,
3087-
indexRel->rd_rel->relam);
3089+
progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY;
3090+
progress_vals[1] = 0; /* initializing */
3091+
progress_vals[2] = indexId;
3092+
progress_vals[3] = indexRel->rd_rel->relam;
3093+
pgstat_progress_update_multi_param(4, progress_index, progress_vals);
30883094

30893095
/* Choose a temporary relation name for the new index */
30903096
concurrentName = ChooseRelationName(get_rel_name(indexId),
@@ -3188,12 +3194,12 @@ ReindexRelationConcurrently(Oid relationOid, int options)
31883194
WaitForLockersMultiple(lockTags, ShareLock, true);
31893195
CommitTransactionCommand();
31903196

3191-
forboth(lc, indexIds, lc2, newIndexIds)
3197+
foreach(lc, newIndexIds)
31923198
{
3193-
Relation indexRel;
3194-
Oid oldIndexId = lfirst_oid(lc);
3195-
Oid newIndexId = lfirst_oid(lc2);
3199+
Relation newIndexRel;
3200+
Oid newIndexId = lfirst_oid(lc);
31963201
Oid heapId;
3202+
Oid indexam;
31973203

31983204
/* Start new transaction for this index's concurrent build */
31993205
StartTransactionCommand();
@@ -3212,9 +3218,21 @@ ReindexRelationConcurrently(Oid relationOid, int options)
32123218
* Index relation has been closed by previous commit, so reopen it to
32133219
* get its information.
32143220
*/
3215-
indexRel = index_open(oldIndexId, ShareUpdateExclusiveLock);
3216-
heapId = indexRel->rd_index->indrelid;
3217-
index_close(indexRel, NoLock);
3221+
newIndexRel = index_open(newIndexId, ShareUpdateExclusiveLock);
3222+
heapId = newIndexRel->rd_index->indrelid;
3223+
indexam = newIndexRel->rd_rel->relam;
3224+
index_close(newIndexRel, NoLock);
3225+
3226+
/*
3227+
* Update progress for the index to build, with the correct parent
3228+
* table involved.
3229+
*/
3230+
pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, heapId);
3231+
progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY;
3232+
progress_vals[1] = PROGRESS_CREATEIDX_PHASE_BUILD;
3233+
progress_vals[2] = newIndexId;
3234+
progress_vals[3] = indexam;
3235+
pgstat_progress_update_multi_param(4, progress_index, progress_vals);
32183236

32193237
/* Perform concurrent build of new index */
32203238
index_concurrently_build(heapId, newIndexId);
@@ -3243,6 +3261,8 @@ ReindexRelationConcurrently(Oid relationOid, int options)
32433261
Oid heapId;
32443262
TransactionId limitXmin;
32453263
Snapshot snapshot;
3264+
Relation newIndexRel;
3265+
Oid indexam;
32463266

32473267
StartTransactionCommand();
32483268

@@ -3253,15 +3273,33 @@ ReindexRelationConcurrently(Oid relationOid, int options)
32533273
*/
32543274
CHECK_FOR_INTERRUPTS();
32553275

3256-
heapId = IndexGetRelation(newIndexId, false);
3257-
32583276
/*
32593277
* Take the "reference snapshot" that will be used by validate_index()
32603278
* to filter candidate tuples.
32613279
*/
32623280
snapshot = RegisterSnapshot(GetTransactionSnapshot());
32633281
PushActiveSnapshot(snapshot);
32643282

3283+
/*
3284+
* Index relation has been closed by previous commit, so reopen it to
3285+
* get its information.
3286+
*/
3287+
newIndexRel = index_open(newIndexId, ShareUpdateExclusiveLock);
3288+
heapId = newIndexRel->rd_index->indrelid;
3289+
indexam = newIndexRel->rd_rel->relam;
3290+
index_close(newIndexRel, NoLock);
3291+
3292+
/*
3293+
* Update progress for the index to build, with the correct parent
3294+
* table involved.
3295+
*/
3296+
pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, heapId);
3297+
progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY;
3298+
progress_vals[1] = PROGRESS_CREATEIDX_PHASE_VALIDATE_IDXSCAN;
3299+
progress_vals[2] = newIndexId;
3300+
progress_vals[3] = indexam;
3301+
pgstat_progress_update_multi_param(4, progress_index, progress_vals);
3302+
32653303
validate_index(heapId, newIndexId, snapshot);
32663304

32673305
/*
@@ -3396,7 +3434,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
33963434
*/
33973435

33983436
pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
3399-
PROGRESS_CREATEIDX_PHASE_WAIT_4);
3437+
PROGRESS_CREATEIDX_PHASE_WAIT_5);
34003438
WaitForLockersMultiple(lockTags, AccessExclusiveLock, true);
34013439

34023440
PushActiveSnapshot(GetTransactionSnapshot());

0 commit comments

Comments
 (0)