@@ -3061,6 +3061,12 @@ ReindexMultipleInternal(List *relids, int options)
3061
3061
static bool
3062
3062
ReindexRelationConcurrently (Oid relationOid , int options )
3063
3063
{
3064
+ typedef struct ReindexIndexInfo
3065
+ {
3066
+ Oid indexId ;
3067
+ Oid tableId ;
3068
+ Oid amId ;
3069
+ } ReindexIndexInfo ;
3064
3070
List * heapRelationIds = NIL ;
3065
3071
List * indexIds = NIL ;
3066
3072
List * newIndexIds = NIL ;
@@ -3170,10 +3176,16 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3170
3176
get_rel_name (cellOid ))));
3171
3177
else
3172
3178
{
3179
+ ReindexIndexInfo * idx ;
3180
+
3173
3181
/* Save the list of relation OIDs in private context */
3174
3182
oldcontext = MemoryContextSwitchTo (private_context );
3175
3183
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 );
3177
3189
3178
3190
MemoryContextSwitchTo (oldcontext );
3179
3191
}
@@ -3210,13 +3222,18 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3210
3222
get_rel_name (cellOid ))));
3211
3223
else
3212
3224
{
3225
+ ReindexIndexInfo * idx ;
3226
+
3213
3227
/*
3214
3228
* Save the list of relation OIDs in private
3215
3229
* context
3216
3230
*/
3217
3231
oldcontext = MemoryContextSwitchTo (private_context );
3218
3232
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 */
3220
3237
3221
3238
MemoryContextSwitchTo (oldcontext );
3222
3239
}
@@ -3235,6 +3252,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3235
3252
Oid heapId = IndexGetRelation (relationOid ,
3236
3253
(options & REINDEXOPT_MISSING_OK ) != 0 );
3237
3254
Relation heapRelation ;
3255
+ ReindexIndexInfo * idx ;
3238
3256
3239
3257
/* if relation is missing, leave */
3240
3258
if (!OidIsValid (heapId ))
@@ -3285,7 +3303,10 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3285
3303
* Save the list of relation OIDs in private context. Note
3286
3304
* that invalid indexes are allowed here.
3287
3305
*/
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 */
3289
3310
3290
3311
MemoryContextSwitchTo (oldcontext );
3291
3312
break ;
@@ -3344,39 +3365,44 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3344
3365
foreach (lc , indexIds )
3345
3366
{
3346
3367
char * concurrentName ;
3347
- Oid indexId = lfirst_oid (lc );
3368
+ ReindexIndexInfo * idx = lfirst (lc );
3369
+ ReindexIndexInfo * newidx ;
3348
3370
Oid newIndexId ;
3349
3371
Relation indexRel ;
3350
3372
Relation heapRel ;
3351
3373
Relation newIndexRel ;
3352
3374
LockRelId * lockrelid ;
3353
3375
3354
- indexRel = index_open (indexId , ShareUpdateExclusiveLock );
3376
+ indexRel = index_open (idx -> indexId , ShareUpdateExclusiveLock );
3355
3377
heapRel = table_open (indexRel -> rd_index -> indrelid ,
3356
3378
ShareUpdateExclusiveLock );
3357
3379
3380
+ idx -> tableId = RelationGetRelid (heapRel );
3381
+ idx -> amId = indexRel -> rd_rel -> relam ;
3382
+
3358
3383
/* This function shouldn't be called for temporary relations. */
3359
3384
if (indexRel -> rd_rel -> relpersistence == RELPERSISTENCE_TEMP )
3360
3385
elog (ERROR , "cannot reindex a temporary table concurrently" );
3361
3386
3362
3387
pgstat_progress_start_command (PROGRESS_COMMAND_CREATE_INDEX ,
3363
- RelationGetRelid (heapRel ));
3388
+ idx -> tableId );
3389
+
3364
3390
progress_vals [0 ] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY ;
3365
3391
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 ;
3368
3394
pgstat_progress_update_multi_param (4 , progress_index , progress_vals );
3369
3395
3370
3396
/* Choose a temporary relation name for the new index */
3371
- concurrentName = ChooseRelationName (get_rel_name (indexId ),
3397
+ concurrentName = ChooseRelationName (get_rel_name (idx -> indexId ),
3372
3398
NULL ,
3373
3399
"ccnew" ,
3374
3400
get_rel_namespace (indexRel -> rd_index -> indrelid ),
3375
3401
false);
3376
3402
3377
3403
/* Create new index definition based on given index */
3378
3404
newIndexId = index_concurrently_create_copy (heapRel ,
3379
- indexId ,
3405
+ idx -> indexId ,
3380
3406
concurrentName );
3381
3407
3382
3408
/*
@@ -3390,7 +3416,12 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3390
3416
*/
3391
3417
oldcontext = MemoryContextSwitchTo (private_context );
3392
3418
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 );
3394
3425
3395
3426
/*
3396
3427
* Save lockrelid to protect each relation from drop then close
@@ -3471,10 +3502,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3471
3502
3472
3503
foreach (lc , newIndexIds )
3473
3504
{
3474
- Relation newIndexRel ;
3475
- Oid newIndexId = lfirst_oid (lc );
3476
- Oid heapId ;
3477
- Oid indexam ;
3505
+ ReindexIndexInfo * newidx = lfirst (lc );
3478
3506
3479
3507
/* Start new transaction for this index's concurrent build */
3480
3508
StartTransactionCommand ();
@@ -3489,28 +3517,19 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3489
3517
/* Set ActiveSnapshot since functions in the indexes may need it */
3490
3518
PushActiveSnapshot (GetTransactionSnapshot ());
3491
3519
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
-
3501
3520
/*
3502
3521
* Update progress for the index to build, with the correct parent
3503
3522
* table involved.
3504
3523
*/
3505
- pgstat_progress_start_command (PROGRESS_COMMAND_CREATE_INDEX , heapId );
3524
+ pgstat_progress_start_command (PROGRESS_COMMAND_CREATE_INDEX , newidx -> tableId );
3506
3525
progress_vals [0 ] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY ;
3507
3526
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 ;
3510
3529
pgstat_progress_update_multi_param (4 , progress_index , progress_vals );
3511
3530
3512
3531
/* Perform concurrent build of new index */
3513
- index_concurrently_build (heapId , newIndexId );
3532
+ index_concurrently_build (newidx -> tableId , newidx -> indexId );
3514
3533
3515
3534
PopActiveSnapshot ();
3516
3535
CommitTransactionCommand ();
@@ -3532,12 +3551,9 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3532
3551
3533
3552
foreach (lc , newIndexIds )
3534
3553
{
3535
- Oid newIndexId = lfirst_oid (lc );
3536
- Oid heapId ;
3554
+ ReindexIndexInfo * newidx = lfirst (lc );
3537
3555
TransactionId limitXmin ;
3538
3556
Snapshot snapshot ;
3539
- Relation newIndexRel ;
3540
- Oid indexam ;
3541
3557
3542
3558
StartTransactionCommand ();
3543
3559
@@ -3555,27 +3571,19 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3555
3571
snapshot = RegisterSnapshot (GetTransactionSnapshot ());
3556
3572
PushActiveSnapshot (snapshot );
3557
3573
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
-
3567
3574
/*
3568
3575
* Update progress for the index to build, with the correct parent
3569
3576
* table involved.
3570
3577
*/
3571
- pgstat_progress_start_command (PROGRESS_COMMAND_CREATE_INDEX , heapId );
3578
+ pgstat_progress_start_command (PROGRESS_COMMAND_CREATE_INDEX ,
3579
+ newidx -> tableId );
3572
3580
progress_vals [0 ] = PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY ;
3573
3581
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 ;
3576
3584
pgstat_progress_update_multi_param (4 , progress_index , progress_vals );
3577
3585
3578
- validate_index (heapId , newIndexId , snapshot );
3586
+ validate_index (newidx -> tableId , newidx -> indexId , snapshot );
3579
3587
3580
3588
/*
3581
3589
* We can now do away with our active snapshot, we still need to save
@@ -3622,10 +3630,9 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3622
3630
3623
3631
forboth (lc , indexIds , lc2 , newIndexIds )
3624
3632
{
3633
+ ReindexIndexInfo * oldidx = lfirst (lc );
3634
+ ReindexIndexInfo * newidx = lfirst (lc2 );
3625
3635
char * oldName ;
3626
- Oid oldIndexId = lfirst_oid (lc );
3627
- Oid newIndexId = lfirst_oid (lc2 );
3628
- Oid heapId ;
3629
3636
3630
3637
/*
3631
3638
* Check for user-requested abort. This is inside a transaction so as
@@ -3634,27 +3641,25 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3634
3641
*/
3635
3642
CHECK_FOR_INTERRUPTS ();
3636
3643
3637
- heapId = IndexGetRelation (oldIndexId , false);
3638
-
3639
3644
/* Choose a relation name for old index */
3640
- oldName = ChooseRelationName (get_rel_name (oldIndexId ),
3645
+ oldName = ChooseRelationName (get_rel_name (oldidx -> indexId ),
3641
3646
NULL ,
3642
3647
"ccold" ,
3643
- get_rel_namespace (heapId ),
3648
+ get_rel_namespace (oldidx -> tableId ),
3644
3649
false);
3645
3650
3646
3651
/*
3647
3652
* Swap old index with the new one. This also marks the new one as
3648
3653
* valid and the old one as not valid.
3649
3654
*/
3650
- index_concurrently_swap (newIndexId , oldIndexId , oldName );
3655
+ index_concurrently_swap (newidx -> indexId , oldidx -> indexId , oldName );
3651
3656
3652
3657
/*
3653
3658
* Invalidate the relcache for the table, so that after this commit
3654
3659
* all sessions will refresh any cached plans that might reference the
3655
3660
* index.
3656
3661
*/
3657
- CacheInvalidateRelcacheByRelid (heapId );
3662
+ CacheInvalidateRelcacheByRelid (oldidx -> tableId );
3658
3663
3659
3664
/*
3660
3665
* CCI here so that subsequent iterations see the oldName in the
@@ -3684,8 +3689,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3684
3689
3685
3690
foreach (lc , indexIds )
3686
3691
{
3687
- Oid oldIndexId = lfirst_oid (lc );
3688
- Oid heapId ;
3692
+ ReindexIndexInfo * oldidx = lfirst (lc );
3689
3693
3690
3694
/*
3691
3695
* Check for user-requested abort. This is inside a transaction so as
@@ -3694,8 +3698,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3694
3698
*/
3695
3699
CHECK_FOR_INTERRUPTS ();
3696
3700
3697
- heapId = IndexGetRelation (oldIndexId , false);
3698
- index_concurrently_set_dead (heapId , oldIndexId );
3701
+ index_concurrently_set_dead (oldidx -> tableId , oldidx -> indexId );
3699
3702
}
3700
3703
3701
3704
/* Commit this transaction to make the updates visible. */
@@ -3719,11 +3722,11 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3719
3722
3720
3723
foreach (lc , indexIds )
3721
3724
{
3722
- Oid oldIndexId = lfirst_oid (lc );
3725
+ ReindexIndexInfo * idx = lfirst (lc );
3723
3726
ObjectAddress object ;
3724
3727
3725
3728
object .classId = RelationRelationId ;
3726
- object .objectId = oldIndexId ;
3729
+ object .objectId = idx -> indexId ;
3727
3730
object .objectSubId = 0 ;
3728
3731
3729
3732
add_exact_object_address (& object , objects );
@@ -3766,7 +3769,8 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3766
3769
{
3767
3770
foreach (lc , newIndexIds )
3768
3771
{
3769
- Oid indOid = lfirst_oid (lc );
3772
+ ReindexIndexInfo * idx = lfirst (lc );
3773
+ Oid indOid = idx -> indexId ;
3770
3774
3771
3775
ereport (INFO ,
3772
3776
(errmsg ("index \"%s.%s\" was reindexed" ,
0 commit comments