@@ -121,7 +121,6 @@ static void SetReindexProcessing(Oid heapOid, Oid indexOid);
121
121
static void ResetReindexProcessing (void );
122
122
static void SetReindexPending (List * indexes );
123
123
static void RemoveReindexPending (Oid indexOid );
124
- static void ResetReindexPending (void );
125
124
126
125
127
126
/*
@@ -3380,27 +3379,18 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence,
3380
3379
indexInfo -> ii_ExclusionStrats = NULL ;
3381
3380
}
3382
3381
3383
- /* ensure SetReindexProcessing state isn't leaked */
3384
- PG_TRY ();
3385
- {
3386
- /* Suppress use of the target index while rebuilding it */
3387
- SetReindexProcessing (heapId , indexId );
3382
+ /* Suppress use of the target index while rebuilding it */
3383
+ SetReindexProcessing (heapId , indexId );
3388
3384
3389
- /* Create a new physical relation for the index */
3390
- RelationSetNewRelfilenode (iRel , persistence , InvalidTransactionId ,
3391
- InvalidMultiXactId );
3385
+ /* Create a new physical relation for the index */
3386
+ RelationSetNewRelfilenode (iRel , persistence , InvalidTransactionId ,
3387
+ InvalidMultiXactId );
3392
3388
3393
- /* Initialize the index and rebuild */
3394
- /* Note: we do not need to re-establish pkey setting */
3395
- index_build (heapRelation , iRel , indexInfo , false, true);
3396
- }
3397
- PG_CATCH ();
3398
- {
3399
- /* Make sure flag gets cleared on error exit */
3400
- ResetReindexProcessing ();
3401
- PG_RE_THROW ();
3402
- }
3403
- PG_END_TRY ();
3389
+ /* Initialize the index and rebuild */
3390
+ /* Note: we do not need to re-establish pkey setting */
3391
+ index_build (heapRelation , iRel , indexInfo , false, true);
3392
+
3393
+ /* Re-allow use of target index */
3404
3394
ResetReindexProcessing ();
3405
3395
3406
3396
/*
@@ -3529,7 +3519,9 @@ reindex_relation(Oid relid, int flags, int options)
3529
3519
Relation rel ;
3530
3520
Oid toast_relid ;
3531
3521
List * indexIds ;
3522
+ char persistence ;
3532
3523
bool result ;
3524
+ ListCell * indexId ;
3533
3525
3534
3526
/*
3535
3527
* Open and lock the relation. ShareLock is sufficient since we only need
@@ -3547,56 +3539,42 @@ reindex_relation(Oid relid, int flags, int options)
3547
3539
*/
3548
3540
indexIds = RelationGetIndexList (rel );
3549
3541
3550
- PG_TRY ();
3542
+ if ( flags & REINDEX_REL_SUPPRESS_INDEX_USE )
3551
3543
{
3552
- ListCell * indexId ;
3553
- char persistence ;
3554
-
3555
- if (flags & REINDEX_REL_SUPPRESS_INDEX_USE )
3556
- {
3557
- /* Suppress use of all the indexes until they are rebuilt */
3558
- SetReindexPending (indexIds );
3559
-
3560
- /*
3561
- * Make the new heap contents visible --- now things might be
3562
- * inconsistent!
3563
- */
3564
- CommandCounterIncrement ();
3565
- }
3544
+ /* Suppress use of all the indexes until they are rebuilt */
3545
+ SetReindexPending (indexIds );
3566
3546
3567
3547
/*
3568
- * Compute persistence of indexes: same as that of owning rel, unless
3569
- * caller specified otherwise.
3548
+ * Make the new heap contents visible --- now things might be
3549
+ * inconsistent!
3570
3550
*/
3571
- if (flags & REINDEX_REL_FORCE_INDEXES_UNLOGGED )
3572
- persistence = RELPERSISTENCE_UNLOGGED ;
3573
- else if (flags & REINDEX_REL_FORCE_INDEXES_PERMANENT )
3574
- persistence = RELPERSISTENCE_PERMANENT ;
3575
- else
3576
- persistence = rel -> rd_rel -> relpersistence ;
3551
+ CommandCounterIncrement ();
3552
+ }
3577
3553
3578
- /* Reindex all the indexes. */
3579
- foreach (indexId , indexIds )
3580
- {
3581
- Oid indexOid = lfirst_oid (indexId );
3554
+ /*
3555
+ * Compute persistence of indexes: same as that of owning rel, unless
3556
+ * caller specified otherwise.
3557
+ */
3558
+ if (flags & REINDEX_REL_FORCE_INDEXES_UNLOGGED )
3559
+ persistence = RELPERSISTENCE_UNLOGGED ;
3560
+ else if (flags & REINDEX_REL_FORCE_INDEXES_PERMANENT )
3561
+ persistence = RELPERSISTENCE_PERMANENT ;
3562
+ else
3563
+ persistence = rel -> rd_rel -> relpersistence ;
3582
3564
3583
- reindex_index (indexOid , !(flags & REINDEX_REL_CHECK_CONSTRAINTS ),
3584
- persistence , options );
3565
+ /* Reindex all the indexes. */
3566
+ foreach (indexId , indexIds )
3567
+ {
3568
+ Oid indexOid = lfirst_oid (indexId );
3585
3569
3586
- CommandCounterIncrement ();
3570
+ reindex_index (indexOid , !(flags & REINDEX_REL_CHECK_CONSTRAINTS ),
3571
+ persistence , options );
3587
3572
3588
- /* Index should no longer be in the pending list */
3589
- Assert (!ReindexIsProcessingIndex (indexOid ));
3590
- }
3591
- }
3592
- PG_CATCH ();
3593
- {
3594
- /* Make sure list gets cleared on error exit */
3595
- ResetReindexPending ();
3596
- PG_RE_THROW ();
3573
+ CommandCounterIncrement ();
3574
+
3575
+ /* Index should no longer be in the pending list */
3576
+ Assert (!ReindexIsProcessingIndex (indexOid ));
3597
3577
}
3598
- PG_END_TRY ();
3599
- ResetReindexPending ();
3600
3578
3601
3579
/*
3602
3580
* Close rel, but continue to hold the lock.
@@ -3629,6 +3607,7 @@ reindex_relation(Oid relid, int flags, int options)
3629
3607
static Oid currentlyReindexedHeap = InvalidOid ;
3630
3608
static Oid currentlyReindexedIndex = InvalidOid ;
3631
3609
static List * pendingReindexedIndexes = NIL ;
3610
+ static int reindexingNestLevel = 0 ;
3632
3611
3633
3612
/*
3634
3613
* ReindexIsProcessingHeap
@@ -3665,8 +3644,6 @@ ReindexIsProcessingIndex(Oid indexOid)
3665
3644
/*
3666
3645
* SetReindexProcessing
3667
3646
* Set flag that specified heap/index are being reindexed.
3668
- *
3669
- * NB: caller must use a PG_TRY block to ensure ResetReindexProcessing is done.
3670
3647
*/
3671
3648
static void
3672
3649
SetReindexProcessing (Oid heapOid , Oid indexOid )
@@ -3679,6 +3656,8 @@ SetReindexProcessing(Oid heapOid, Oid indexOid)
3679
3656
currentlyReindexedIndex = indexOid ;
3680
3657
/* Index is no longer "pending" reindex. */
3681
3658
RemoveReindexPending (indexOid );
3659
+ /* This may have been set already, but in case it isn't, do so now. */
3660
+ reindexingNestLevel = GetCurrentTransactionNestLevel ();
3682
3661
}
3683
3662
3684
3663
/*
@@ -3690,14 +3669,14 @@ ResetReindexProcessing(void)
3690
3669
{
3691
3670
currentlyReindexedHeap = InvalidOid ;
3692
3671
currentlyReindexedIndex = InvalidOid ;
3672
+ /* reindexingNestLevel remains set till end of (sub)transaction */
3693
3673
}
3694
3674
3695
3675
/*
3696
3676
* SetReindexPending
3697
3677
* Mark the given indexes as pending reindex.
3698
3678
*
3699
- * NB: caller must use a PG_TRY block to ensure ResetReindexPending is done.
3700
- * Also, we assume that the current memory context stays valid throughout.
3679
+ * NB: we assume that the current memory context stays valid throughout.
3701
3680
*/
3702
3681
static void
3703
3682
SetReindexPending (List * indexes )
@@ -3706,6 +3685,7 @@ SetReindexPending(List *indexes)
3706
3685
if (pendingReindexedIndexes )
3707
3686
elog (ERROR , "cannot reindex while reindexing" );
3708
3687
pendingReindexedIndexes = list_copy (indexes );
3688
+ reindexingNestLevel = GetCurrentTransactionNestLevel ();
3709
3689
}
3710
3690
3711
3691
/*
@@ -3720,11 +3700,30 @@ RemoveReindexPending(Oid indexOid)
3720
3700
}
3721
3701
3722
3702
/*
3723
- * ResetReindexPending
3724
- * Unset reindex-pending status .
3703
+ * ResetReindexState
3704
+ * Clear all reindexing state during (sub)transaction abort .
3725
3705
*/
3726
- static void
3727
- ResetReindexPending ( void )
3706
+ void
3707
+ ResetReindexState ( int nestLevel )
3728
3708
{
3729
- pendingReindexedIndexes = NIL ;
3709
+ /*
3710
+ * Because reindexing is not re-entrant, we don't need to cope with nested
3711
+ * reindexing states. We just need to avoid messing up the outer-level
3712
+ * state in case a subtransaction fails within a REINDEX. So checking the
3713
+ * current nest level against that of the reindex operation is sufficient.
3714
+ */
3715
+ if (reindexingNestLevel >= nestLevel )
3716
+ {
3717
+ currentlyReindexedHeap = InvalidOid ;
3718
+ currentlyReindexedIndex = InvalidOid ;
3719
+
3720
+ /*
3721
+ * We needn't try to release the contents of pendingReindexedIndexes;
3722
+ * that list should be in a transaction-lifespan context, so it will
3723
+ * go away automatically.
3724
+ */
3725
+ pendingReindexedIndexes = NIL ;
3726
+
3727
+ reindexingNestLevel = 0 ;
3728
+ }
3730
3729
}
0 commit comments