From f6a660d14b3649f046c2d54c810ccf28f51f65db Mon Sep 17 00:00:00 2001 From: pgaddict Date: Tue, 28 Nov 2023 00:05:50 +0800 Subject: [PATCH v6 3/4] refactor multiple functions to support tracking reindex using event trigger. --- src/backend/catalog/index.c | 8 ++--- src/backend/commands/cluster.c | 2 +- src/backend/commands/indexcmds.c | 60 +++++++++++++++++--------------- src/backend/commands/tablecmds.c | 2 +- src/include/catalog/index.h | 4 +-- 5 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 143fae01..88ff8c5a 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -3558,7 +3558,7 @@ IndexGetRelation(Oid indexId, bool missing_ok) * reindex_index - This routine is used to recreate a single index */ void -reindex_index(Oid indexId, bool skip_constraint_checks, char persistence, +reindex_index(const ReindexStmt *stmt, Oid indexId, bool skip_constraint_checks, char persistence, const ReindexParams *params) { Relation iRel, @@ -3865,7 +3865,7 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence, * index rebuild. */ bool -reindex_relation(Oid relid, int flags, const ReindexParams *params) +reindex_relation(const ReindexStmt *stmt, Oid relid, int flags, const ReindexParams *params) { Relation rel; Oid toast_relid; @@ -3953,7 +3953,7 @@ reindex_relation(Oid relid, int flags, const ReindexParams *params) continue; } - reindex_index(indexOid, !(flags & REINDEX_REL_CHECK_CONSTRAINTS), + reindex_index(stmt, indexOid, !(flags & REINDEX_REL_CHECK_CONSTRAINTS), persistence, params); CommandCounterIncrement(); @@ -3990,7 +3990,7 @@ reindex_relation(Oid relid, int flags, const ReindexParams *params) newparams.options &= ~(REINDEXOPT_MISSING_OK); newparams.tablespaceOid = InvalidOid; - result |= reindex_relation(toast_relid, flags, &newparams); + result |= reindex_relation(stmt, toast_relid, flags, &newparams); } return result; diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c index a3bef6ac..1f52d391 100644 --- a/src/backend/commands/cluster.c +++ b/src/backend/commands/cluster.c @@ -1518,7 +1518,7 @@ finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap, pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE, PROGRESS_CLUSTER_PHASE_REBUILD_INDEX); - reindex_relation(OIDOldHeap, reindex_flags, &reindex_params); + reindex_relation(NULL, OIDOldHeap, reindex_flags, &reindex_params); /* Report that we are now doing clean up */ pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE, diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 0b3b8e98..f47a4aa7 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -94,20 +94,19 @@ static char *ChooseIndexName(const char *tabname, Oid namespaceId, bool primary, bool isconstraint); static char *ChooseIndexNameAddition(const List *colnames); static List *ChooseIndexColumnNames(const List *indexElems); -static void ReindexIndex(const RangeVar *indexRelation, const ReindexParams *params, +static void ReindexIndex(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel); static void RangeVarCallbackForReindexIndex(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg); -static Oid ReindexTable(const RangeVar *relation, const ReindexParams *params, +static Oid ReindexTable(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel); -static void ReindexMultipleTables(const char *objectName, - ReindexObjectType objectKind, const ReindexParams *params); +static void ReindexMultipleTables(const ReindexStmt *stmt, const ReindexParams *params); static void reindex_error_callback(void *arg); -static void ReindexPartitions(Oid relid, const ReindexParams *params, +static void ReindexPartitions(const ReindexStmt *stmt, Oid relid, const ReindexParams *params, bool isTopLevel); -static void ReindexMultipleInternal(const List *relids, +static void ReindexMultipleInternal(const ReindexStmt *stmt, const List *relids, const ReindexParams *params); -static bool ReindexRelationConcurrently(Oid relationOid, +static bool ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const ReindexParams *params); static void update_relispartition(Oid relationId, bool newval); static inline void set_indexsafe_procflags(void); @@ -2729,10 +2728,10 @@ ExecReindex(ParseState *pstate, const ReindexStmt *stmt, bool isTopLevel) switch (stmt->kind) { case REINDEX_OBJECT_INDEX: - ReindexIndex(stmt->relation, ¶ms, isTopLevel); + ReindexIndex(stmt, ¶ms, isTopLevel); break; case REINDEX_OBJECT_TABLE: - ReindexTable(stmt->relation, ¶ms, isTopLevel); + ReindexTable(stmt, ¶ms, isTopLevel); break; case REINDEX_OBJECT_SCHEMA: case REINDEX_OBJECT_SYSTEM: @@ -2748,7 +2747,7 @@ ExecReindex(ParseState *pstate, const ReindexStmt *stmt, bool isTopLevel) (stmt->kind == REINDEX_OBJECT_SCHEMA) ? "REINDEX SCHEMA" : (stmt->kind == REINDEX_OBJECT_SYSTEM) ? "REINDEX SYSTEM" : "REINDEX DATABASE"); - ReindexMultipleTables(stmt->name, stmt->kind, ¶ms); + ReindexMultipleTables(stmt, ¶ms); break; default: elog(ERROR, "unrecognized object type: %d", @@ -2762,13 +2761,15 @@ ExecReindex(ParseState *pstate, const ReindexStmt *stmt, bool isTopLevel) * Recreate a specific index. */ static void -ReindexIndex(const RangeVar *indexRelation, const ReindexParams *params, bool isTopLevel) +ReindexIndex(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel) { + const RangeVar *indexRelation; struct ReindexIndexCallbackState state; Oid indOid; char persistence; char relkind; + indexRelation = stmt->relation; /* * Find and lock index, and check permissions on table; use callback to * obtain lock on table first, to avoid deadlock hazard. The lock level @@ -2796,16 +2797,16 @@ ReindexIndex(const RangeVar *indexRelation, const ReindexParams *params, bool is relkind = get_rel_relkind(indOid); if (relkind == RELKIND_PARTITIONED_INDEX) - ReindexPartitions(indOid, params, isTopLevel); + ReindexPartitions(stmt, indOid, params, isTopLevel); else if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 && persistence != RELPERSISTENCE_TEMP) - ReindexRelationConcurrently(indOid, params); + ReindexRelationConcurrently(stmt, indOid, params); else { ReindexParams newparams = *params; newparams.options |= REINDEXOPT_REPORT_PROGRESS; - reindex_index(indOid, false, persistence, &newparams); + reindex_index(stmt, indOid, false, persistence, &newparams); } } @@ -2885,11 +2886,12 @@ RangeVarCallbackForReindexIndex(const RangeVar *relation, * Recreate all indexes of a table (and of its toast table, if any) */ static Oid -ReindexTable(const RangeVar *relation, const ReindexParams *params, bool isTopLevel) +ReindexTable(const ReindexStmt *stmt, const ReindexParams *params, bool isTopLevel) { Oid heapOid; bool result; + const RangeVar *relation = stmt->relation; /* * The lock level used here should match reindex_relation(). * @@ -2905,11 +2907,11 @@ ReindexTable(const RangeVar *relation, const ReindexParams *params, bool isTopLe RangeVarCallbackOwnsTable, NULL); if (get_rel_relkind(heapOid) == RELKIND_PARTITIONED_TABLE) - ReindexPartitions(heapOid, params, isTopLevel); + ReindexPartitions(stmt, heapOid, params, isTopLevel); else if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 && get_rel_persistence(heapOid) != RELPERSISTENCE_TEMP) { - result = ReindexRelationConcurrently(heapOid, params); + result = ReindexRelationConcurrently(stmt, heapOid, params); if (!result) ereport(NOTICE, @@ -2921,7 +2923,7 @@ ReindexTable(const RangeVar *relation, const ReindexParams *params, bool isTopLe ReindexParams newparams = *params; newparams.options |= REINDEXOPT_REPORT_PROGRESS; - result = reindex_relation(heapOid, + result = reindex_relation(stmt, heapOid, REINDEX_REL_PROCESS_TOAST | REINDEX_REL_CHECK_CONSTRAINTS, &newparams); @@ -2943,9 +2945,9 @@ ReindexTable(const RangeVar *relation, const ReindexParams *params, bool isTopLe * That means this must not be called within a user transaction block! */ static void -ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind, - const ReindexParams *params) +ReindexMultipleTables(const ReindexStmt *stmt, const ReindexParams *params) { + Oid objectOid; Relation relationRelation; TableScanDesc scan; @@ -2957,6 +2959,8 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind, int num_keys; bool concurrent_warning = false; bool tablespace_warning = false; + const char *objectName = stmt->name; + const ReindexObjectType objectKind = stmt->kind; Assert(objectKind == REINDEX_OBJECT_SCHEMA || objectKind == REINDEX_OBJECT_SYSTEM || @@ -3152,7 +3156,7 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind, * Process each relation listed in a separate transaction. Note that this * commits and then starts a new transaction immediately. */ - ReindexMultipleInternal(relids, params); + ReindexMultipleInternal(stmt, relids, params); MemoryContextDelete(private_context); } @@ -3182,7 +3186,7 @@ reindex_error_callback(void *arg) * by the caller. */ static void -ReindexPartitions(Oid relid, const ReindexParams *params, bool isTopLevel) +ReindexPartitions(const ReindexStmt *stmt, Oid relid, const ReindexParams *params, bool isTopLevel) { List *partitions = NIL; char relkind = get_rel_relkind(relid); @@ -3258,7 +3262,7 @@ ReindexPartitions(Oid relid, const ReindexParams *params, bool isTopLevel) * Process each partition listed in a separate transaction. Note that * this commits and then starts a new transaction immediately. */ - ReindexMultipleInternal(partitions, params); + ReindexMultipleInternal(stmt, partitions, params); /* * Clean up working storage --- note we must do this after @@ -3276,7 +3280,7 @@ ReindexPartitions(Oid relid, const ReindexParams *params, bool isTopLevel) * and starts a new transaction when finished. */ static void -ReindexMultipleInternal(const List *relids, const ReindexParams *params) +ReindexMultipleInternal(const ReindexStmt *stmt, const List *relids, const ReindexParams *params) { ListCell *l; @@ -3335,7 +3339,7 @@ ReindexMultipleInternal(const List *relids, const ReindexParams *params) ReindexParams newparams = *params; newparams.options |= REINDEXOPT_MISSING_OK; - (void) ReindexRelationConcurrently(relid, &newparams); + (void) ReindexRelationConcurrently(stmt, relid, &newparams); /* ReindexRelationConcurrently() does the verbose output */ } else if (relkind == RELKIND_INDEX) @@ -3344,7 +3348,7 @@ ReindexMultipleInternal(const List *relids, const ReindexParams *params) newparams.options |= REINDEXOPT_REPORT_PROGRESS | REINDEXOPT_MISSING_OK; - reindex_index(relid, false, relpersistence, &newparams); + reindex_index(stmt, relid, false, relpersistence, &newparams); PopActiveSnapshot(); /* reindex_index() does the verbose output */ } @@ -3355,7 +3359,7 @@ ReindexMultipleInternal(const List *relids, const ReindexParams *params) newparams.options |= REINDEXOPT_REPORT_PROGRESS | REINDEXOPT_MISSING_OK; - result = reindex_relation(relid, + result = reindex_relation(stmt, relid, REINDEX_REL_PROCESS_TOAST | REINDEX_REL_CHECK_CONSTRAINTS, &newparams); @@ -3400,7 +3404,7 @@ ReindexMultipleInternal(const List *relids, const ReindexParams *params) * anyway, and a non-concurrent reindex is more efficient. */ static bool -ReindexRelationConcurrently(Oid relationOid, const ReindexParams *params) +ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const ReindexParams *params) { typedef struct ReindexIndexInfo { diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 323d9bf8..85ee7c63 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -2172,7 +2172,7 @@ ExecuteTruncateGuts(List *explicit_rels, /* * Reconstruct the indexes to match, and we're done. */ - reindex_relation(heap_relid, REINDEX_REL_PROCESS_TOAST, + reindex_relation(NULL, heap_relid, REINDEX_REL_PROCESS_TOAST, &reindex_params); } diff --git a/src/include/catalog/index.h b/src/include/catalog/index.h index a4770eaf..e1272662 100644 --- a/src/include/catalog/index.h +++ b/src/include/catalog/index.h @@ -149,7 +149,7 @@ extern void index_set_state_flags(Oid indexId, IndexStateFlagsAction action); extern Oid IndexGetRelation(Oid indexId, bool missing_ok); -extern void reindex_index(Oid indexId, bool skip_constraint_checks, +extern void reindex_index(const ReindexStmt *stmt, Oid indexId, bool skip_constraint_checks, char persistence, const ReindexParams *params); /* Flag bits for reindex_relation(): */ @@ -159,7 +159,7 @@ extern void reindex_index(Oid indexId, bool skip_constraint_checks, #define REINDEX_REL_FORCE_INDEXES_UNLOGGED 0x08 #define REINDEX_REL_FORCE_INDEXES_PERMANENT 0x10 -extern bool reindex_relation(Oid relid, int flags, const ReindexParams *params); +extern bool reindex_relation(const ReindexStmt *stmt, Oid relid, int flags, const ReindexParams *params); extern bool ReindexIsProcessingHeap(Oid heapOid); extern bool ReindexIsProcessingIndex(Oid indexOid); -- 2.34.1