From 2d3320d3d256eed7ea3deb8383348f2021e6a247 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 16 Apr 2011 17:26:41 -0400 Subject: [PATCH] Simplify reindex_relation's API. For what seem entirely historical reasons, a bitmask "flags" argument was recently added to reindex_relation without subsuming its existing boolean argument into that bitmask. This seems a bit bizarre, so fold them together. --- src/backend/catalog/index.c | 40 +++++++++++++++++--------------- src/backend/commands/cluster.c | 6 ++--- src/backend/commands/indexcmds.c | 4 ++-- src/backend/commands/tablecmds.c | 2 +- src/include/catalog/index.h | 9 ++++--- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index b74b2cc5c9..201f92998d 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -2841,29 +2841,33 @@ 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). * - * "flags" can include REINDEX_SUPPRESS_INDEX_USE and REINDEX_CHECK_CONSTRAINTS. + * "flags" is a bitmask that can include any combination of these bits: * - * If flags has REINDEX_SUPPRESS_INDEX_USE, the relation was just completely + * REINDEX_REL_PROCESS_TOAST: if true, process the toast table too (if any). + * + * REINDEX_REL_SUPPRESS_INDEX_USE: if true, 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; + * having made the rebuilt table 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. + * REINDEX_REL_CHECK_CONSTRAINTS: if true, recheck unique and exclusion + * constraint conditions, else don't. To avoid deadlocks, VACUUM FULL or + * CLUSTER on a system catalog must omit this 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. + * Returns true if any indexes were rebuilt (including toast table's index + * when relevant). Note that a CommandCounterIncrement will occur after each + * index rebuild. */ bool -reindex_relation(Oid relid, bool toast_too, int flags) +reindex_relation(Oid relid, int flags) { Relation rel; Oid toast_relid; @@ -2919,7 +2923,7 @@ reindex_relation(Oid relid, bool toast_too, int flags) List *doneIndexes; ListCell *indexId; - if (flags & REINDEX_SUPPRESS_INDEX_USE) + if (flags & REINDEX_REL_SUPPRESS_INDEX_USE) { /* Suppress use of all the indexes until they are rebuilt */ SetReindexPending(indexIds); @@ -2940,11 +2944,11 @@ reindex_relation(Oid relid, bool toast_too, int flags) if (is_pg_class) RelationSetIndexList(rel, doneIndexes, InvalidOid); - reindex_index(indexOid, !(flags & REINDEX_CHECK_CONSTRAINTS)); + reindex_index(indexOid, !(flags & REINDEX_REL_CHECK_CONSTRAINTS)); CommandCounterIncrement(); - if (flags & REINDEX_SUPPRESS_INDEX_USE) + if (flags & REINDEX_REL_SUPPRESS_INDEX_USE) RemoveReindexPending(indexOid); if (is_pg_class) @@ -2972,12 +2976,10 @@ reindex_relation(Oid relid, bool toast_too, int flags) /* * If the relation has a secondary toast rel, reindex that too while we - * still hold the lock on the master table. There's never a reason to - * reindex the toast table right after rebuilding the heap. + * still hold the lock on the master table. */ - Assert(!(toast_too && (flags & REINDEX_SUPPRESS_INDEX_USE))); - if (toast_too && OidIsValid(toast_relid)) - result |= reindex_relation(toast_relid, false, flags); + if ((flags & REINDEX_REL_PROCESS_TOAST) && OidIsValid(toast_relid)) + result |= reindex_relation(toast_relid, flags); return result; } diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c index 2cc2aaa8f6..10ec32c05d 100644 --- a/src/backend/commands/cluster.c +++ b/src/backend/commands/cluster.c @@ -1399,10 +1399,10 @@ finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap, * so no chance to reclaim disk space before commit. We do not need a * final CommandCounterIncrement() because reindex_relation does it. */ - reindex_flags = REINDEX_SUPPRESS_INDEX_USE; + reindex_flags = REINDEX_REL_SUPPRESS_INDEX_USE; if (check_constraints) - reindex_flags |= REINDEX_CHECK_CONSTRAINTS; - reindex_relation(OIDOldHeap, false, reindex_flags); + reindex_flags |= REINDEX_REL_CHECK_CONSTRAINTS; + reindex_relation(OIDOldHeap, reindex_flags); /* Destroy new heap with old filenode */ object.classId = RelationRelationId; diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 05e8234a0f..2f4e751100 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -1566,7 +1566,7 @@ ReindexTable(RangeVar *relation) ReleaseSysCache(tuple); - if (!reindex_relation(heapOid, true, 0)) + if (!reindex_relation(heapOid, REINDEX_REL_PROCESS_TOAST)) ereport(NOTICE, (errmsg("table \"%s\" has no indexes", relation->relname))); @@ -1679,7 +1679,7 @@ ReindexDatabase(const char *databaseName, bool do_system, bool do_user) StartTransactionCommand(); /* functions in indexes may want a snapshot set */ PushActiveSnapshot(GetTransactionSnapshot()); - if (reindex_relation(relid, true, 0)) + if (reindex_relation(relid, REINDEX_REL_PROCESS_TOAST)) ereport(NOTICE, (errmsg("table \"%s.%s\" was reindexed", get_namespace_name(get_rel_namespace(relid)), diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index fb39006e15..35929b20d5 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -1080,7 +1080,7 @@ ExecuteTruncate(TruncateStmt *stmt) /* * Reconstruct the indexes to match, and we're done. */ - reindex_relation(heap_relid, true, 0); + reindex_relation(heap_relid, REINDEX_REL_PROCESS_TOAST); } } diff --git a/src/include/catalog/index.h b/src/include/catalog/index.h index 2ce6806e50..cc7e785cc0 100644 --- a/src/include/catalog/index.h +++ b/src/include/catalog/index.h @@ -88,9 +88,12 @@ extern void validate_index(Oid heapId, Oid indexId, Snapshot snapshot); extern void reindex_index(Oid indexId, bool skip_constraint_checks); -#define REINDEX_CHECK_CONSTRAINTS 0x1 -#define REINDEX_SUPPRESS_INDEX_USE 0x2 -extern bool reindex_relation(Oid relid, bool toast_too, int flags); +/* Flag bits for reindex_relation(): */ +#define REINDEX_REL_PROCESS_TOAST 0x01 +#define REINDEX_REL_SUPPRESS_INDEX_USE 0x02 +#define REINDEX_REL_CHECK_CONSTRAINTS 0x04 + +extern bool reindex_relation(Oid relid, int flags); extern bool ReindexIsProcessingHeap(Oid heapOid); extern bool ReindexIsProcessingIndex(Oid indexOid);