diff options
Diffstat (limited to 'src/backend/catalog/index.c')
-rw-r--r-- | src/backend/catalog/index.c | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index a3ac44cf79b..8af382b9c58 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -2508,26 +2508,29 @@ reindex_index(Oid indexId, bool skip_constraint_checks) * reindex_relation - This routine is used to recreate all indexes * of a relation (and optionally its toast relation too, if any). * - * If heap_rebuilt is true, then the relation was just completely rebuilt by - * an operation such as VACUUM FULL or CLUSTER, and therefore its indexes are - * inconsistent with it. This makes things tricky if the relation is a system - * catalog that we might consult during the reindexing. To deal with that - * case, we mark all of the indexes as pending rebuild so that they won't be - * trusted until rebuilt. The caller is required to call us *without* having - * made the rebuilt versions visible by doing CommandCounterIncrement; we'll - * do CCI after having collected the index list. (This way we can still use - * catalog indexes while collecting the list.) + * "flags" can include REINDEX_SUPPRESS_INDEX_USE and REINDEX_CHECK_CONSTRAINTS. * - * We also skip rechecking uniqueness/exclusion constraint properties if - * heap_rebuilt is true. This avoids likely deadlock conditions when doing - * VACUUM FULL or CLUSTER on system catalogs. REINDEX should be used to - * rebuild an index if constraint inconsistency is suspected. + * If flags has REINDEX_SUPPRESS_INDEX_USE, the relation was just completely + * rebuilt by an operation such as VACUUM FULL or CLUSTER, and therefore its + * indexes are inconsistent with it. This makes things tricky if the relation + * is a system catalog that we might consult during the reindexing. To deal + * with that case, we mark all of the indexes as pending rebuild so that they + * won't be trusted until rebuilt. The caller is required to call us *without* + * having made the rebuilt versions visible by doing CommandCounterIncrement; + * we'll do CCI after having collected the index list. (This way we can still + * use catalog indexes while collecting the list.) + * + * To avoid deadlocks, VACUUM FULL or CLUSTER on a system catalog must omit the + * REINDEX_CHECK_CONSTRAINTS flag. REINDEX should be used to rebuild an index + * if constraint inconsistency is suspected. For optimal performance, other + * callers should include the flag only after transforming the data in a manner + * that risks a change in constraint validity. * * Returns true if any indexes were rebuilt. Note that a * CommandCounterIncrement will occur after each index rebuild. */ bool -reindex_relation(Oid relid, bool toast_too, bool heap_rebuilt) +reindex_relation(Oid relid, bool toast_too, int flags) { Relation rel; Oid toast_relid; @@ -2583,7 +2586,7 @@ reindex_relation(Oid relid, bool toast_too, bool heap_rebuilt) List *doneIndexes; ListCell *indexId; - if (heap_rebuilt) + if (flags & REINDEX_SUPPRESS_INDEX_USE) { /* Suppress use of all the indexes until they are rebuilt */ SetReindexPending(indexIds); @@ -2604,11 +2607,11 @@ reindex_relation(Oid relid, bool toast_too, bool heap_rebuilt) if (is_pg_class) RelationSetIndexList(rel, doneIndexes, InvalidOid); - reindex_index(indexOid, heap_rebuilt); + reindex_index(indexOid, !(flags & REINDEX_CHECK_CONSTRAINTS)); CommandCounterIncrement(); - if (heap_rebuilt) + if (flags & REINDEX_SUPPRESS_INDEX_USE) RemoveReindexPending(indexOid); if (is_pg_class) @@ -2636,10 +2639,12 @@ reindex_relation(Oid relid, bool toast_too, bool heap_rebuilt) /* * If the relation has a secondary toast rel, reindex that too while we - * still hold the lock on the master table. + * still hold the lock on the master table. There's never a reason to + * reindex the toast table right after rebuilding the heap. */ + Assert(!(toast_too && (flags & REINDEX_SUPPRESS_INDEX_USE))); if (toast_too && OidIsValid(toast_relid)) - result |= reindex_relation(toast_relid, false, false); + result |= reindex_relation(toast_relid, false, flags); return result; } |