Skip to content

Commit c6c4b37

Browse files
committed
Invent struct ReindexIndexInfo
This struct is used by ReindexRelationConcurrently to keep track of the relations to process. This saves having to obtain some data repeatedly, and has future uses as well. Reviewed-by: Dmitry Dolgov <[email protected]> Reviewed-by: Hamid Akhtar <[email protected]> Reviewed-by: Masahiko Sawada <[email protected]> Discussion: https://postgr.es/m/[email protected]
1 parent 9eabfe3 commit c6c4b37

File tree

2 files changed

+66
-61
lines changed

2 files changed

+66
-61
lines changed

src/backend/commands/indexcmds.c

+65-61
Original file line numberDiff line numberDiff line change
@@ -3061,6 +3061,12 @@ ReindexMultipleInternal(List *relids, int options)
30613061
static bool
30623062
ReindexRelationConcurrently(Oid relationOid, int options)
30633063
{
3064+
typedef struct ReindexIndexInfo
3065+
{
3066+
Oid indexId;
3067+
Oid tableId;
3068+
Oid amId;
3069+
} ReindexIndexInfo;
30643070
List *heapRelationIds = NIL;
30653071
List *indexIds = NIL;
30663072
List *newIndexIds = NIL;
@@ -3170,10 +3176,16 @@ ReindexRelationConcurrently(Oid relationOid, int options)
31703176
get_rel_name(cellOid))));
31713177
else
31723178
{
3179+
ReindexIndexInfo *idx;
3180+
31733181
/* Save the list of relation OIDs in private context */
31743182
oldcontext = MemoryContextSwitchTo(private_context);
31753183

3176-
indexIds = lappend_oid(indexIds, cellOid);
3184+
idx = palloc(sizeof(ReindexIndexInfo));
3185+
idx->indexId = cellOid;
3186+
/* other fields set later */
3187+
3188+
indexIds = lappend(indexIds, idx);
31773189

31783190
MemoryContextSwitchTo(oldcontext);
31793191
}
@@ -3210,13 +3222,18 @@ ReindexRelationConcurrently(Oid relationOid, int options)
32103222
get_rel_name(cellOid))));
32113223
else
32123224
{
3225+
ReindexIndexInfo *idx;
3226+
32133227
/*
32143228
* Save the list of relation OIDs in private
32153229
* context
32163230
*/
32173231
oldcontext = MemoryContextSwitchTo(private_context);
32183232

3219-
indexIds = lappend_oid(indexIds, cellOid);
3233+
idx = palloc(sizeof(ReindexIndexInfo));
3234+
idx->indexId = cellOid;
3235+
indexIds = lappend(indexIds, idx);
3236+
/* other fields set later */
32203237

32213238
MemoryContextSwitchTo(oldcontext);
32223239
}
@@ -3235,6 +3252,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
32353252
Oid heapId = IndexGetRelation(relationOid,
32363253
(options & REINDEXOPT_MISSING_OK) != 0);
32373254
Relation heapRelation;
3255+
ReindexIndexInfo *idx;
32383256

32393257
/* if relation is missing, leave */
32403258
if (!OidIsValid(heapId))
@@ -3285,7 +3303,10 @@ ReindexRelationConcurrently(Oid relationOid, int options)
32853303
* Save the list of relation OIDs in private context. Note
32863304
* that invalid indexes are allowed here.
32873305
*/
3288-
indexIds = lappend_oid(indexIds, relationOid);
3306+
idx = palloc(sizeof(ReindexIndexInfo));
3307+
idx->indexId = relationOid;
3308+
indexIds = lappend(indexIds, idx);
3309+
/* other fields set later */
32893310

32903311
MemoryContextSwitchTo(oldcontext);
32913312
break;
@@ -3344,39 +3365,44 @@ ReindexRelationConcurrently(Oid relationOid, int options)
33443365
foreach(lc, indexIds)
33453366
{
33463367
char *concurrentName;
3347-
Oid indexId = lfirst_oid(lc);
3368+
ReindexIndexInfo *idx = lfirst(lc);
3369+
ReindexIndexInfo *newidx;
33483370
Oid newIndexId;
33493371
Relation indexRel;
33503372
Relation heapRel;
33513373
Relation newIndexRel;
33523374
LockRelId *lockrelid;
33533375

3354-
indexRel = index_open(indexId, ShareUpdateExclusiveLock);
3376+
indexRel = index_open(idx->indexId, ShareUpdateExclusiveLock);
33553377
heapRel = table_open(indexRel->rd_index->indrelid,
33563378
ShareUpdateExclusiveLock);
33573379

3380+
idx->tableId = RelationGetRelid(heapRel);
3381+
idx->amId = indexRel->rd_rel->relam;
3382+
33583383
/* This function shouldn't be called for temporary relations. */
33593384
if (indexRel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
33603385
elog(ERROR, "cannot reindex a temporary table concurrently");
33613386

33623387
pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX,
3363-
RelationGetRelid(heapRel));
3388+
idx->tableId);
3389+
33643390
progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY;
33653391
progress_vals[1] = 0; /* initializing */
3366-
progress_vals[2] = indexId;
3367-
progress_vals[3] = indexRel->rd_rel->relam;
3392+
progress_vals[2] = idx->indexId;
3393+
progress_vals[3] = idx->amId;
33683394
pgstat_progress_update_multi_param(4, progress_index, progress_vals);
33693395

33703396
/* Choose a temporary relation name for the new index */
3371-
concurrentName = ChooseRelationName(get_rel_name(indexId),
3397+
concurrentName = ChooseRelationName(get_rel_name(idx->indexId),
33723398
NULL,
33733399
"ccnew",
33743400
get_rel_namespace(indexRel->rd_index->indrelid),
33753401
false);
33763402

33773403
/* Create new index definition based on given index */
33783404
newIndexId = index_concurrently_create_copy(heapRel,
3379-
indexId,
3405+
idx->indexId,
33803406
concurrentName);
33813407

33823408
/*
@@ -3390,7 +3416,12 @@ ReindexRelationConcurrently(Oid relationOid, int options)
33903416
*/
33913417
oldcontext = MemoryContextSwitchTo(private_context);
33923418

3393-
newIndexIds = lappend_oid(newIndexIds, newIndexId);
3419+
newidx = palloc(sizeof(ReindexIndexInfo));
3420+
newidx->indexId = newIndexId;
3421+
newidx->tableId = idx->tableId;
3422+
newidx->amId = idx->amId;
3423+
3424+
newIndexIds = lappend(newIndexIds, newidx);
33943425

33953426
/*
33963427
* Save lockrelid to protect each relation from drop then close
@@ -3471,10 +3502,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
34713502

34723503
foreach(lc, newIndexIds)
34733504
{
3474-
Relation newIndexRel;
3475-
Oid newIndexId = lfirst_oid(lc);
3476-
Oid heapId;
3477-
Oid indexam;
3505+
ReindexIndexInfo *newidx = lfirst(lc);
34783506

34793507
/* Start new transaction for this index's concurrent build */
34803508
StartTransactionCommand();
@@ -3489,28 +3517,19 @@ ReindexRelationConcurrently(Oid relationOid, int options)
34893517
/* Set ActiveSnapshot since functions in the indexes may need it */
34903518
PushActiveSnapshot(GetTransactionSnapshot());
34913519

3492-
/*
3493-
* Index relation has been closed by previous commit, so reopen it to
3494-
* get its information.
3495-
*/
3496-
newIndexRel = index_open(newIndexId, ShareUpdateExclusiveLock);
3497-
heapId = newIndexRel->rd_index->indrelid;
3498-
indexam = newIndexRel->rd_rel->relam;
3499-
index_close(newIndexRel, NoLock);
3500-
35013520
/*
35023521
* Update progress for the index to build, with the correct parent
35033522
* table involved.
35043523
*/
3505-
pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, heapId);
3524+
pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, newidx->tableId);
35063525
progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY;
35073526
progress_vals[1] = PROGRESS_CREATEIDX_PHASE_BUILD;
3508-
progress_vals[2] = newIndexId;
3509-
progress_vals[3] = indexam;
3527+
progress_vals[2] = newidx->indexId;
3528+
progress_vals[3] = newidx->amId;
35103529
pgstat_progress_update_multi_param(4, progress_index, progress_vals);
35113530

35123531
/* Perform concurrent build of new index */
3513-
index_concurrently_build(heapId, newIndexId);
3532+
index_concurrently_build(newidx->tableId, newidx->indexId);
35143533

35153534
PopActiveSnapshot();
35163535
CommitTransactionCommand();
@@ -3532,12 +3551,9 @@ ReindexRelationConcurrently(Oid relationOid, int options)
35323551

35333552
foreach(lc, newIndexIds)
35343553
{
3535-
Oid newIndexId = lfirst_oid(lc);
3536-
Oid heapId;
3554+
ReindexIndexInfo *newidx = lfirst(lc);
35373555
TransactionId limitXmin;
35383556
Snapshot snapshot;
3539-
Relation newIndexRel;
3540-
Oid indexam;
35413557

35423558
StartTransactionCommand();
35433559

@@ -3555,27 +3571,19 @@ ReindexRelationConcurrently(Oid relationOid, int options)
35553571
snapshot = RegisterSnapshot(GetTransactionSnapshot());
35563572
PushActiveSnapshot(snapshot);
35573573

3558-
/*
3559-
* Index relation has been closed by previous commit, so reopen it to
3560-
* get its information.
3561-
*/
3562-
newIndexRel = index_open(newIndexId, ShareUpdateExclusiveLock);
3563-
heapId = newIndexRel->rd_index->indrelid;
3564-
indexam = newIndexRel->rd_rel->relam;
3565-
index_close(newIndexRel, NoLock);
3566-
35673574
/*
35683575
* Update progress for the index to build, with the correct parent
35693576
* table involved.
35703577
*/
3571-
pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, heapId);
3578+
pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX,
3579+
newidx->tableId);
35723580
progress_vals[0] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY;
35733581
progress_vals[1] = PROGRESS_CREATEIDX_PHASE_VALIDATE_IDXSCAN;
3574-
progress_vals[2] = newIndexId;
3575-
progress_vals[3] = indexam;
3582+
progress_vals[2] = newidx->indexId;
3583+
progress_vals[3] = newidx->amId;
35763584
pgstat_progress_update_multi_param(4, progress_index, progress_vals);
35773585

3578-
validate_index(heapId, newIndexId, snapshot);
3586+
validate_index(newidx->tableId, newidx->indexId, snapshot);
35793587

35803588
/*
35813589
* We can now do away with our active snapshot, we still need to save
@@ -3622,10 +3630,9 @@ ReindexRelationConcurrently(Oid relationOid, int options)
36223630

36233631
forboth(lc, indexIds, lc2, newIndexIds)
36243632
{
3633+
ReindexIndexInfo *oldidx = lfirst(lc);
3634+
ReindexIndexInfo *newidx = lfirst(lc2);
36253635
char *oldName;
3626-
Oid oldIndexId = lfirst_oid(lc);
3627-
Oid newIndexId = lfirst_oid(lc2);
3628-
Oid heapId;
36293636

36303637
/*
36313638
* Check for user-requested abort. This is inside a transaction so as
@@ -3634,27 +3641,25 @@ ReindexRelationConcurrently(Oid relationOid, int options)
36343641
*/
36353642
CHECK_FOR_INTERRUPTS();
36363643

3637-
heapId = IndexGetRelation(oldIndexId, false);
3638-
36393644
/* Choose a relation name for old index */
3640-
oldName = ChooseRelationName(get_rel_name(oldIndexId),
3645+
oldName = ChooseRelationName(get_rel_name(oldidx->indexId),
36413646
NULL,
36423647
"ccold",
3643-
get_rel_namespace(heapId),
3648+
get_rel_namespace(oldidx->tableId),
36443649
false);
36453650

36463651
/*
36473652
* Swap old index with the new one. This also marks the new one as
36483653
* valid and the old one as not valid.
36493654
*/
3650-
index_concurrently_swap(newIndexId, oldIndexId, oldName);
3655+
index_concurrently_swap(newidx->indexId, oldidx->indexId, oldName);
36513656

36523657
/*
36533658
* Invalidate the relcache for the table, so that after this commit
36543659
* all sessions will refresh any cached plans that might reference the
36553660
* index.
36563661
*/
3657-
CacheInvalidateRelcacheByRelid(heapId);
3662+
CacheInvalidateRelcacheByRelid(oldidx->tableId);
36583663

36593664
/*
36603665
* CCI here so that subsequent iterations see the oldName in the
@@ -3684,8 +3689,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
36843689

36853690
foreach(lc, indexIds)
36863691
{
3687-
Oid oldIndexId = lfirst_oid(lc);
3688-
Oid heapId;
3692+
ReindexIndexInfo *oldidx = lfirst(lc);
36893693

36903694
/*
36913695
* Check for user-requested abort. This is inside a transaction so as
@@ -3694,8 +3698,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
36943698
*/
36953699
CHECK_FOR_INTERRUPTS();
36963700

3697-
heapId = IndexGetRelation(oldIndexId, false);
3698-
index_concurrently_set_dead(heapId, oldIndexId);
3701+
index_concurrently_set_dead(oldidx->tableId, oldidx->indexId);
36993702
}
37003703

37013704
/* Commit this transaction to make the updates visible. */
@@ -3719,11 +3722,11 @@ ReindexRelationConcurrently(Oid relationOid, int options)
37193722

37203723
foreach(lc, indexIds)
37213724
{
3722-
Oid oldIndexId = lfirst_oid(lc);
3725+
ReindexIndexInfo *idx = lfirst(lc);
37233726
ObjectAddress object;
37243727

37253728
object.classId = RelationRelationId;
3726-
object.objectId = oldIndexId;
3729+
object.objectId = idx->indexId;
37273730
object.objectSubId = 0;
37283731

37293732
add_exact_object_address(&object, objects);
@@ -3766,7 +3769,8 @@ ReindexRelationConcurrently(Oid relationOid, int options)
37663769
{
37673770
foreach(lc, newIndexIds)
37683771
{
3769-
Oid indOid = lfirst_oid(lc);
3772+
ReindexIndexInfo *idx = lfirst(lc);
3773+
Oid indOid = idx->indexId;
37703774

37713775
ereport(INFO,
37723776
(errmsg("index \"%s.%s\" was reindexed",

src/tools/pgindent/typedefs.list

+1
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,7 @@ Regis
20612061
RegisNode
20622062
RegisteredBgWorker
20632063
ReindexErrorInfo
2064+
ReindexIndexInfo
20642065
ReindexObjectType
20652066
ReindexStmt
20662067
ReindexType

0 commit comments

Comments
 (0)