@@ -126,7 +126,6 @@ static void SetReindexProcessing(Oid heapOid, Oid indexOid);
126
126
static void ResetReindexProcessing (void );
127
127
static void SetReindexPending (List * indexes );
128
128
static void RemoveReindexPending (Oid indexOid );
129
- static void ResetReindexPending (void );
130
129
131
130
132
131
/*
@@ -3457,27 +3456,18 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence,
3457
3456
indexInfo -> ii_ExclusionStrats = NULL ;
3458
3457
}
3459
3458
3460
- /* ensure SetReindexProcessing state isn't leaked */
3461
- PG_TRY ();
3462
- {
3463
- /* Suppress use of the target index while rebuilding it */
3464
- SetReindexProcessing (heapId , indexId );
3459
+ /* Suppress use of the target index while rebuilding it */
3460
+ SetReindexProcessing (heapId , indexId );
3465
3461
3466
- /* Create a new physical relation for the index */
3467
- RelationSetNewRelfilenode (iRel , persistence , InvalidTransactionId ,
3468
- InvalidMultiXactId );
3462
+ /* Create a new physical relation for the index */
3463
+ RelationSetNewRelfilenode (iRel , persistence , InvalidTransactionId ,
3464
+ InvalidMultiXactId );
3469
3465
3470
- /* Initialize the index and rebuild */
3471
- /* Note: we do not need to re-establish pkey setting */
3472
- index_build (heapRelation , iRel , indexInfo , false, true);
3473
- }
3474
- PG_CATCH ();
3475
- {
3476
- /* Make sure flag gets cleared on error exit */
3477
- ResetReindexProcessing ();
3478
- PG_RE_THROW ();
3479
- }
3480
- PG_END_TRY ();
3466
+ /* Initialize the index and rebuild */
3467
+ /* Note: we do not need to re-establish pkey setting */
3468
+ index_build (heapRelation , iRel , indexInfo , false, true);
3469
+
3470
+ /* Re-allow use of target index */
3481
3471
ResetReindexProcessing ();
3482
3472
3483
3473
/*
@@ -3612,7 +3602,9 @@ reindex_relation(Oid relid, int flags, int options)
3612
3602
Relation rel ;
3613
3603
Oid toast_relid ;
3614
3604
List * indexIds ;
3605
+ char persistence ;
3615
3606
bool result ;
3607
+ ListCell * indexId ;
3616
3608
3617
3609
/*
3618
3610
* Open and lock the relation. ShareLock is sufficient since we only need
@@ -3630,56 +3622,42 @@ reindex_relation(Oid relid, int flags, int options)
3630
3622
*/
3631
3623
indexIds = RelationGetIndexList (rel );
3632
3624
3633
- PG_TRY ();
3625
+ if ( flags & REINDEX_REL_SUPPRESS_INDEX_USE )
3634
3626
{
3635
- ListCell * indexId ;
3636
- char persistence ;
3637
-
3638
- if (flags & REINDEX_REL_SUPPRESS_INDEX_USE )
3639
- {
3640
- /* Suppress use of all the indexes until they are rebuilt */
3641
- SetReindexPending (indexIds );
3642
-
3643
- /*
3644
- * Make the new heap contents visible --- now things might be
3645
- * inconsistent!
3646
- */
3647
- CommandCounterIncrement ();
3648
- }
3627
+ /* Suppress use of all the indexes until they are rebuilt */
3628
+ SetReindexPending (indexIds );
3649
3629
3650
3630
/*
3651
- * Compute persistence of indexes: same as that of owning rel, unless
3652
- * caller specified otherwise.
3631
+ * Make the new heap contents visible --- now things might be
3632
+ * inconsistent!
3653
3633
*/
3654
- if (flags & REINDEX_REL_FORCE_INDEXES_UNLOGGED )
3655
- persistence = RELPERSISTENCE_UNLOGGED ;
3656
- else if (flags & REINDEX_REL_FORCE_INDEXES_PERMANENT )
3657
- persistence = RELPERSISTENCE_PERMANENT ;
3658
- else
3659
- persistence = rel -> rd_rel -> relpersistence ;
3634
+ CommandCounterIncrement ();
3635
+ }
3660
3636
3661
- /* Reindex all the indexes. */
3662
- foreach (indexId , indexIds )
3663
- {
3664
- Oid indexOid = lfirst_oid (indexId );
3637
+ /*
3638
+ * Compute persistence of indexes: same as that of owning rel, unless
3639
+ * caller specified otherwise.
3640
+ */
3641
+ if (flags & REINDEX_REL_FORCE_INDEXES_UNLOGGED )
3642
+ persistence = RELPERSISTENCE_UNLOGGED ;
3643
+ else if (flags & REINDEX_REL_FORCE_INDEXES_PERMANENT )
3644
+ persistence = RELPERSISTENCE_PERMANENT ;
3645
+ else
3646
+ persistence = rel -> rd_rel -> relpersistence ;
3665
3647
3666
- reindex_index (indexOid , !(flags & REINDEX_REL_CHECK_CONSTRAINTS ),
3667
- persistence , options );
3648
+ /* Reindex all the indexes. */
3649
+ foreach (indexId , indexIds )
3650
+ {
3651
+ Oid indexOid = lfirst_oid (indexId );
3668
3652
3669
- CommandCounterIncrement ();
3653
+ reindex_index (indexOid , !(flags & REINDEX_REL_CHECK_CONSTRAINTS ),
3654
+ persistence , options );
3670
3655
3671
- /* Index should no longer be in the pending list */
3672
- Assert (!ReindexIsProcessingIndex (indexOid ));
3673
- }
3674
- }
3675
- PG_CATCH ();
3676
- {
3677
- /* Make sure list gets cleared on error exit */
3678
- ResetReindexPending ();
3679
- PG_RE_THROW ();
3656
+ CommandCounterIncrement ();
3657
+
3658
+ /* Index should no longer be in the pending list */
3659
+ Assert (!ReindexIsProcessingIndex (indexOid ));
3680
3660
}
3681
- PG_END_TRY ();
3682
- ResetReindexPending ();
3683
3661
3684
3662
/*
3685
3663
* Close rel, but continue to hold the lock.
@@ -3712,6 +3690,7 @@ reindex_relation(Oid relid, int flags, int options)
3712
3690
static Oid currentlyReindexedHeap = InvalidOid ;
3713
3691
static Oid currentlyReindexedIndex = InvalidOid ;
3714
3692
static List * pendingReindexedIndexes = NIL ;
3693
+ static int reindexingNestLevel = 0 ;
3715
3694
3716
3695
/*
3717
3696
* ReindexIsProcessingHeap
@@ -3748,8 +3727,6 @@ ReindexIsProcessingIndex(Oid indexOid)
3748
3727
/*
3749
3728
* SetReindexProcessing
3750
3729
* Set flag that specified heap/index are being reindexed.
3751
- *
3752
- * NB: caller must use a PG_TRY block to ensure ResetReindexProcessing is done.
3753
3730
*/
3754
3731
static void
3755
3732
SetReindexProcessing (Oid heapOid , Oid indexOid )
@@ -3762,6 +3739,8 @@ SetReindexProcessing(Oid heapOid, Oid indexOid)
3762
3739
currentlyReindexedIndex = indexOid ;
3763
3740
/* Index is no longer "pending" reindex. */
3764
3741
RemoveReindexPending (indexOid );
3742
+ /* This may have been set already, but in case it isn't, do so now. */
3743
+ reindexingNestLevel = GetCurrentTransactionNestLevel ();
3765
3744
}
3766
3745
3767
3746
/*
@@ -3773,14 +3752,14 @@ ResetReindexProcessing(void)
3773
3752
{
3774
3753
currentlyReindexedHeap = InvalidOid ;
3775
3754
currentlyReindexedIndex = InvalidOid ;
3755
+ /* reindexingNestLevel remains set till end of (sub)transaction */
3776
3756
}
3777
3757
3778
3758
/*
3779
3759
* SetReindexPending
3780
3760
* Mark the given indexes as pending reindex.
3781
3761
*
3782
- * NB: caller must use a PG_TRY block to ensure ResetReindexPending is done.
3783
- * Also, we assume that the current memory context stays valid throughout.
3762
+ * NB: we assume that the current memory context stays valid throughout.
3784
3763
*/
3785
3764
static void
3786
3765
SetReindexPending (List * indexes )
@@ -3789,6 +3768,7 @@ SetReindexPending(List *indexes)
3789
3768
if (pendingReindexedIndexes )
3790
3769
elog (ERROR , "cannot reindex while reindexing" );
3791
3770
pendingReindexedIndexes = list_copy (indexes );
3771
+ reindexingNestLevel = GetCurrentTransactionNestLevel ();
3792
3772
}
3793
3773
3794
3774
/*
@@ -3803,11 +3783,30 @@ RemoveReindexPending(Oid indexOid)
3803
3783
}
3804
3784
3805
3785
/*
3806
- * ResetReindexPending
3807
- * Unset reindex-pending status .
3786
+ * ResetReindexState
3787
+ * Clear all reindexing state during (sub)transaction abort .
3808
3788
*/
3809
- static void
3810
- ResetReindexPending ( void )
3789
+ void
3790
+ ResetReindexState ( int nestLevel )
3811
3791
{
3812
- pendingReindexedIndexes = NIL ;
3792
+ /*
3793
+ * Because reindexing is not re-entrant, we don't need to cope with nested
3794
+ * reindexing states. We just need to avoid messing up the outer-level
3795
+ * state in case a subtransaction fails within a REINDEX. So checking the
3796
+ * current nest level against that of the reindex operation is sufficient.
3797
+ */
3798
+ if (reindexingNestLevel >= nestLevel )
3799
+ {
3800
+ currentlyReindexedHeap = InvalidOid ;
3801
+ currentlyReindexedIndex = InvalidOid ;
3802
+
3803
+ /*
3804
+ * We needn't try to release the contents of pendingReindexedIndexes;
3805
+ * that list should be in a transaction-lifespan context, so it will
3806
+ * go away automatically.
3807
+ */
3808
+ pendingReindexedIndexes = NIL ;
3809
+
3810
+ reindexingNestLevel = 0 ;
3811
+ }
3813
3812
}
0 commit comments