@@ -3061,6 +3061,12 @@ ReindexMultipleInternal(List *relids, int options)
30613061static bool
30623062ReindexRelationConcurrently (Oid relationOid ,int options )
30633063{
3064+ typedef struct ReindexIndexInfo
3065+ {
3066+ Oid indexId ;
3067+ Oid tableId ;
3068+ Oid amId ;
3069+ }ReindexIndexInfo ;
30643070List * heapRelationIds = NIL ;
30653071List * indexIds = NIL ;
30663072List * newIndexIds = NIL ;
@@ -3170,10 +3176,16 @@ ReindexRelationConcurrently(Oid relationOid, int options)
31703176get_rel_name (cellOid ))));
31713177else
31723178{
3179+ ReindexIndexInfo * idx ;
3180+
31733181/* Save the list of relation OIDs in private context */
31743182oldcontext = 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
31783190MemoryContextSwitchTo (oldcontext );
31793191}
@@ -3210,13 +3222,18 @@ ReindexRelationConcurrently(Oid relationOid, int options)
32103222get_rel_name (cellOid ))));
32113223else
32123224{
3225+ ReindexIndexInfo * idx ;
3226+
32133227/*
32143228 * Save the list of relation OIDs in private
32153229 * context
32163230 */
32173231oldcontext = 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
32213238MemoryContextSwitchTo (oldcontext );
32223239}
@@ -3235,6 +3252,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
32353252Oid heapId = IndexGetRelation (relationOid ,
32363253 (options & REINDEXOPT_MISSING_OK )!= 0 );
32373254Relation heapRelation ;
3255+ ReindexIndexInfo * idx ;
32383256
32393257/* if relation is missing, leave */
32403258if (!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
32903311MemoryContextSwitchTo (oldcontext );
32913312break ;
@@ -3344,39 +3365,44 @@ ReindexRelationConcurrently(Oid relationOid, int options)
33443365foreach (lc ,indexIds )
33453366{
33463367char * concurrentName ;
3347- Oid indexId = lfirst_oid (lc );
3368+ ReindexIndexInfo * idx = lfirst (lc );
3369+ ReindexIndexInfo * newidx ;
33483370Oid newIndexId ;
33493371Relation indexRel ;
33503372Relation heapRel ;
33513373Relation newIndexRel ;
33523374LockRelId * lockrelid ;
33533375
3354- indexRel = index_open (indexId ,ShareUpdateExclusiveLock );
3376+ indexRel = index_open (idx -> indexId ,ShareUpdateExclusiveLock );
33553377heapRel = table_open (indexRel -> rd_index -> indrelid ,
33563378ShareUpdateExclusiveLock );
33573379
3380+ idx -> tableId = RelationGetRelid (heapRel );
3381+ idx -> amId = indexRel -> rd_rel -> relam ;
3382+
33583383/* This function shouldn't be called for temporary relations. */
33593384if (indexRel -> rd_rel -> relpersistence == RELPERSISTENCE_TEMP )
33603385elog (ERROR ,"cannot reindex a temporary table concurrently" );
33613386
33623387pgstat_progress_start_command (PROGRESS_COMMAND_CREATE_INDEX ,
3363- RelationGetRelid (heapRel ));
3388+ idx -> tableId );
3389+
33643390progress_vals [0 ]= PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY ;
33653391progress_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 ;
33683394pgstat_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 ),
33723398NULL ,
33733399"ccnew" ,
33743400get_rel_namespace (indexRel -> rd_index -> indrelid ),
33753401false);
33763402
33773403/* Create new index definition based on given index */
33783404newIndexId = index_concurrently_create_copy (heapRel ,
3379- indexId ,
3405+ idx -> indexId ,
33803406concurrentName );
33813407
33823408/*
@@ -3390,7 +3416,12 @@ ReindexRelationConcurrently(Oid relationOid, int options)
33903416 */
33913417oldcontext = 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
34723503foreach (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 */
34803508StartTransactionCommand ();
@@ -3489,28 +3517,19 @@ ReindexRelationConcurrently(Oid relationOid, int options)
34893517/* Set ActiveSnapshot since functions in the indexes may need it */
34903518PushActiveSnapshot (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 );
35063525progress_vals [0 ]= PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY ;
35073526progress_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 ;
35103529pgstat_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
35153534PopActiveSnapshot ();
35163535CommitTransactionCommand ();
@@ -3532,12 +3551,9 @@ ReindexRelationConcurrently(Oid relationOid, int options)
35323551
35333552foreach (lc ,newIndexIds )
35343553{
3535- Oid newIndexId = lfirst_oid (lc );
3536- Oid heapId ;
3554+ ReindexIndexInfo * newidx = lfirst (lc );
35373555TransactionId limitXmin ;
35383556Snapshot snapshot ;
3539- Relation newIndexRel ;
3540- Oid indexam ;
35413557
35423558StartTransactionCommand ();
35433559
@@ -3555,27 +3571,19 @@ ReindexRelationConcurrently(Oid relationOid, int options)
35553571snapshot = RegisterSnapshot (GetTransactionSnapshot ());
35563572PushActiveSnapshot (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 );
35723580progress_vals [0 ]= PROGRESS_CREATEIDX_COMMAND_REINDEX_CONCURRENTLY ;
35733581progress_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 ;
35763584pgstat_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
36233631forboth (lc ,indexIds ,lc2 ,newIndexIds )
36243632{
3633+ ReindexIndexInfo * oldidx = lfirst (lc );
3634+ ReindexIndexInfo * newidx = lfirst (lc2 );
36253635char * 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 */
36353642CHECK_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 ),
36413646NULL ,
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
36853690foreach (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 */
36953699CHECK_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
37203723foreach (lc ,indexIds )
37213724{
3722- Oid oldIndexId = lfirst_oid (lc );
3725+ ReindexIndexInfo * idx = lfirst (lc );
37233726ObjectAddress object ;
37243727
37253728object .classId = RelationRelationId ;
3726- object .objectId = oldIndexId ;
3729+ object .objectId = idx -> indexId ;
37273730object .objectSubId = 0 ;
37283731
37293732add_exact_object_address (& object ,objects );
@@ -3766,7 +3769,8 @@ ReindexRelationConcurrently(Oid relationOid, int options)
37663769{
37673770foreach (lc ,newIndexIds )
37683771{
3769- Oid indOid = lfirst_oid (lc );
3772+ ReindexIndexInfo * idx = lfirst (lc );
3773+ Oid indOid = idx -> indexId ;
37703774
37713775ereport (INFO ,
37723776(errmsg ("index \"%s.%s\" was reindexed" ,