@@ -2529,26 +2529,29 @@ reindex_index(Oid indexId, bool skip_constraint_checks)
2529
2529
* reindex_relation - This routine is used to recreate all indexes
2530
2530
* of a relation (and optionally its toast relation too, if any).
2531
2531
*
2532
- * If heap_rebuilt is true, then the relation was just completely rebuilt by
2533
- * an operation such as VACUUM FULL or CLUSTER, and therefore its indexes are
2534
- * inconsistent with it. This makes things tricky if the relation is a system
2535
- * catalog that we might consult during the reindexing. To deal with that
2536
- * case, we mark all of the indexes as pending rebuild so that they won't be
2537
- * trusted until rebuilt. The caller is required to call us *without* having
2538
- * made the rebuilt versions visible by doing CommandCounterIncrement; we'll
2539
- * do CCI after having collected the index list. (This way we can still use
2540
- * catalog indexes while collecting the list.)
2532
+ * "flags" can include REINDEX_SUPPRESS_INDEX_USE and REINDEX_CHECK_CONSTRAINTS.
2541
2533
*
2542
- * We also skip rechecking uniqueness/exclusion constraint properties if
2543
- * heap_rebuilt is true. This avoids likely deadlock conditions when doing
2544
- * VACUUM FULL or CLUSTER on system catalogs. REINDEX should be used to
2545
- * rebuild an index if constraint inconsistency is suspected.
2534
+ * If flags has REINDEX_SUPPRESS_INDEX_USE, the relation was just completely
2535
+ * rebuilt by an operation such as VACUUM FULL or CLUSTER, and therefore its
2536
+ * indexes are inconsistent with it. This makes things tricky if the relation
2537
+ * is a system catalog that we might consult during the reindexing. To deal
2538
+ * with that case, we mark all of the indexes as pending rebuild so that they
2539
+ * won't be trusted until rebuilt. The caller is required to call us *without*
2540
+ * having made the rebuilt versions visible by doing CommandCounterIncrement;
2541
+ * we'll do CCI after having collected the index list. (This way we can still
2542
+ * use catalog indexes while collecting the list.)
2543
+ *
2544
+ * To avoid deadlocks, VACUUM FULL or CLUSTER on a system catalog must omit the
2545
+ * REINDEX_CHECK_CONSTRAINTS flag. REINDEX should be used to rebuild an index
2546
+ * if constraint inconsistency is suspected. For optimal performance, other
2547
+ * callers should include the flag only after transforming the data in a manner
2548
+ * that risks a change in constraint validity.
2546
2549
*
2547
2550
* Returns true if any indexes were rebuilt. Note that a
2548
2551
* CommandCounterIncrement will occur after each index rebuild.
2549
2552
*/
2550
2553
bool
2551
- reindex_relation (Oid relid , bool toast_too , bool heap_rebuilt )
2554
+ reindex_relation (Oid relid , bool toast_too , int flags )
2552
2555
{
2553
2556
Relation rel ;
2554
2557
Oid toast_relid ;
@@ -2604,7 +2607,7 @@ reindex_relation(Oid relid, bool toast_too, bool heap_rebuilt)
2604
2607
List * doneIndexes ;
2605
2608
ListCell * indexId ;
2606
2609
2607
- if (heap_rebuilt )
2610
+ if (flags & REINDEX_SUPPRESS_INDEX_USE )
2608
2611
{
2609
2612
/* Suppress use of all the indexes until they are rebuilt */
2610
2613
SetReindexPending (indexIds );
@@ -2625,11 +2628,11 @@ reindex_relation(Oid relid, bool toast_too, bool heap_rebuilt)
2625
2628
if (is_pg_class )
2626
2629
RelationSetIndexList (rel , doneIndexes , InvalidOid );
2627
2630
2628
- reindex_index (indexOid , heap_rebuilt );
2631
+ reindex_index (indexOid , !( flags & REINDEX_CHECK_CONSTRAINTS ) );
2629
2632
2630
2633
CommandCounterIncrement ();
2631
2634
2632
- if (heap_rebuilt )
2635
+ if (flags & REINDEX_SUPPRESS_INDEX_USE )
2633
2636
RemoveReindexPending (indexOid );
2634
2637
2635
2638
if (is_pg_class )
@@ -2657,10 +2660,12 @@ reindex_relation(Oid relid, bool toast_too, bool heap_rebuilt)
2657
2660
2658
2661
/*
2659
2662
* If the relation has a secondary toast rel, reindex that too while we
2660
- * still hold the lock on the master table.
2663
+ * still hold the lock on the master table. There's never a reason to
2664
+ * reindex the toast table right after rebuilding the heap.
2661
2665
*/
2666
+ Assert (!(toast_too && (flags & REINDEX_SUPPRESS_INDEX_USE )));
2662
2667
if (toast_too && OidIsValid (toast_relid ))
2663
- result |= reindex_relation (toast_relid , false, false );
2668
+ result |= reindex_relation (toast_relid , false, flags );
2664
2669
2665
2670
return result ;
2666
2671
}
0 commit comments