From 2940d2a1b497ea6791918a6646690779c9b01065 Mon Sep 17 00:00:00 2001
From: Sami Imseih <samimseih@gmail.com>
Date: Mon, 30 Mar 2026 17:23:09 +0000
Subject: [PATCH v3 2/3] Refactor autovacuum score computation into
 compute_autovac_score

The pattern of fetching a relation's pgstat entry and
calling relation_needs_vacanalyze() is needed in multiple
places: table_recheck_autovac and a future view that will
emit a relation's autovacuum priority score.

Introduce compute_autovac_score() to consolidate this.
It accepts relopts and force_scores parameters, and
replaces recheck_relation_needs_vacanalyze() which did
the same thing with a narrower interface.

Discussion: https://postgr.es/m/CAA5RZ0s4xjMrB-VAnLccC7kY8d0-4806-Lsac-czJsdA1LXtAw%40mail.gmail.com
---
 src/backend/postmaster/autovacuum.c | 51 ++++++++++++++---------------
 1 file changed, 24 insertions(+), 27 deletions(-)

diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index f6b213852e3..fbe670d375f 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -370,10 +370,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, bool force_scores,
+								  bool *dovacuum, bool *doanalyze,
+								  bool *wraparound, AutoVacuumScores *scores);
 static void relation_needs_vacanalyze(Oid relid, AutoVacOpts *relopts,
 									  Form_pg_class classForm,
 									  PgStat_StatTabEntry *tabentry,
@@ -2834,6 +2835,7 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map,
 	bool		wraparound;
 	AutoVacOpts *avopts;
 	bool		free_avopts = false;
+	AutoVacuumScores scores;
 
 	/* fetch the relation's relcache entry */
 	classTup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
@@ -2859,9 +2861,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, false,
+						  &dovacuum, &doanalyze, &wraparound, &scores);
 
 	/* OK, it needs something done */
 	if (doanalyze || dovacuum)
@@ -2971,34 +2974,28 @@ 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, bool force_scores,
+					  bool *dovacuum, bool *doanalyze,
+					  bool *wraparound, AutoVacuumScores *scores)
 {
+	Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
 	PgStat_StatTabEntry *tabentry;
-	AutoVacuumScores scores;
 
 	/* fetch the pgstat table entry */
 	tabentry = pgstat_fetch_stat_tabentry_ext(classForm->relisshared,
-											  relid);
+											  classForm->oid);
 
-	relation_needs_vacanalyze(relid, avopts, classForm, tabentry,
-							  effective_multixact_freeze_max_age,
-							  false,
-							  dovacuum, doanalyze, wraparound,
-							  &scores);
+	relation_needs_vacanalyze(classForm->oid, relopts, classForm, tabentry,
+							  effective_multixact_freeze_max_age, force_scores,
+							  dovacuum, doanalyze, wraparound, scores);
 
 	/* Release tabentry to avoid leakage */
 	if (tabentry)
-- 
2.47.3

