From e36b8b1427757b1e09c32713b0b4912a0c806073 Mon Sep 17 00:00:00 2001 From: Sami Imseih Date: Tue, 31 Mar 2026 18:43:25 +0000 Subject: [PATCH v4 3/4] Refactor autovacuum score computation into compute_autovac_score Autovacuum priority score will be computed for several reasons: Autovacuum processing and a future monitoring view to report the scores per table. This consolidates this code into a new routine compute_autovac_score() and also removes the need for recheck_relation_needs_vacanalyze() which is doing the same thing. Discussion: https://postgr.es/m/CAA5RZ0s4xjMrB-VAnLccC7kY8d0-4806-Lsac-czJsdA1LXtAw%40mail.gmail.com --- src/backend/postmaster/autovacuum.c | 47 ++++++++++++++--------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c index 72ad30f6ae7..4b0d2526f5b 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -374,10 +374,11 @@ static void FreeWorkerInfo(int code, Datum arg); static autovac_table *table_recheck_autovac(Oid relid, HTAB *table_toast_map, TupleDesc pg_class_desc, int effective_multixact_freeze_max_age); -static void recheck_relation_needs_vacanalyze(Oid relid, AutoVacOpts *avopts, - Form_pg_class classForm, - int effective_multixact_freeze_max_age, - bool *dovacuum, bool *doanalyze, bool *wraparound); +static void compute_autovac_score(HeapTuple tuple, TupleDesc pg_class_desc, + int effective_multixact_freeze_max_age, + AutoVacOpts *relopts, int elevel, + bool *dovacuum, bool *doanalyze, + bool *wraparound, AutoVacuumPriority *priority); static void relation_needs_vacanalyze(Oid relid, AutoVacOpts *relopts, Form_pg_class classForm, PgStat_StatTabEntry *tabentry, @@ -2836,6 +2837,7 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map, bool wraparound; AutoVacOpts *avopts; bool free_avopts = false; + AutoVacuumPriority priority; /* fetch the relation's relcache entry */ classTup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid)); @@ -2861,9 +2863,10 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map, avopts = &hentry->ar_reloptions; } - recheck_relation_needs_vacanalyze(relid, avopts, classForm, - effective_multixact_freeze_max_age, - &dovacuum, &doanalyze, &wraparound); + compute_autovac_score(classTup, pg_class_desc, + effective_multixact_freeze_max_age, + avopts, DEBUG3, + &dovacuum, &doanalyze, &wraparound, &priority); /* OK, it needs something done */ if (doanalyze || dovacuum) @@ -2973,33 +2976,29 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map, } /* - * recheck_relation_needs_vacanalyze - * - * Subroutine for table_recheck_autovac. - * - * Fetch the pgstat of a relation and recheck whether a relation - * needs to be vacuumed or analyzed. + * compute_autovac_score + * Fetch the pgstat entry for a relation and call + * relation_needs_vacanalyze() to determine whether it needs + * vacuum or analyze and compute its priority scores. */ static void -recheck_relation_needs_vacanalyze(Oid relid, - AutoVacOpts *avopts, - Form_pg_class classForm, - int effective_multixact_freeze_max_age, - bool *dovacuum, - bool *doanalyze, - bool *wraparound) +compute_autovac_score(HeapTuple tuple, TupleDesc pg_class_desc, + int effective_multixact_freeze_max_age, + AutoVacOpts *relopts, int elevel, + bool *dovacuum, bool *doanalyze, + bool *wraparound, AutoVacuumPriority *priority) { + Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple); PgStat_StatTabEntry *tabentry; - AutoVacuumPriority priority; /* fetch the pgstat table entry */ tabentry = pgstat_fetch_stat_tabentry_ext(classForm->relisshared, - relid); + classForm->oid); - relation_needs_vacanalyze(relid, avopts, classForm, tabentry, + relation_needs_vacanalyze(classForm->oid, relopts, classForm, tabentry, effective_multixact_freeze_max_age, dovacuum, doanalyze, wraparound, - &priority, DEBUG3); + priority, elevel); /* Release tabentry to avoid leakage */ if (tabentry) -- 2.47.3