From eed371d2a5d5c7991045a210f5a7314526416f8f Mon Sep 17 00:00:00 2001
From: Sami Imseih <samimseih@gmail.com>
Date: Tue, 31 Mar 2026 18:33:18 +0000
Subject: [PATCH v6 2/4] Add elevel parameter to relation_needs_vacanalyze

Allow callers to control the log level for debug output
by passing an elevel parameter. Passing 0 suppresses
logging. This prepares the function for use by a future
view that should not emit debug messages when queried.

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

diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index f409a5d9028..f6e8722a17a 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -383,7 +383,8 @@ static void relation_needs_vacanalyze(Oid relid, AutoVacOpts *relopts,
 									  PgStat_StatTabEntry *tabentry,
 									  int effective_multixact_freeze_max_age,
 									  bool *dovacuum, bool *doanalyze, bool *wraparound,
-									  AutoVacuumScores *scores);
+									  AutoVacuumScores *scores,
+									  int elevel);
 
 static void autovacuum_do_vac_analyze(autovac_table *tab,
 									  BufferAccessStrategy bstrategy);
@@ -2080,7 +2081,7 @@ do_autovacuum(void)
 		relation_needs_vacanalyze(relid, relopts, classForm, tabentry,
 								  effective_multixact_freeze_max_age,
 								  &dovacuum, &doanalyze, &wraparound,
-								  &scores);
+								  &scores, DEBUG3);
 
 		/* Relations that need work are added to tables_to_process */
 		if (dovacuum || doanalyze)
@@ -2180,7 +2181,7 @@ do_autovacuum(void)
 		relation_needs_vacanalyze(relid, relopts, classForm, tabentry,
 								  effective_multixact_freeze_max_age,
 								  &dovacuum, &doanalyze, &wraparound,
-								  &scores);
+								  &scores, DEBUG3);
 
 		/* ignore analyze for toast tables */
 		if (dovacuum)
@@ -2998,7 +2999,7 @@ recheck_relation_needs_vacanalyze(Oid relid,
 	relation_needs_vacanalyze(relid, avopts, classForm, tabentry,
 							  effective_multixact_freeze_max_age,
 							  dovacuum, doanalyze, wraparound,
-							  &scores);
+							  &scores, DEBUG3);
 
 	/* Release tabentry to avoid leakage */
 	if (tabentry)
@@ -3088,6 +3089,8 @@ recheck_relation_needs_vacanalyze(Oid relid,
  * All fields in AutoVacuumScores are always computed regardless of autovacuum
  * settings.  The dovacuum and doanalyze output parameters are only set when
  * autovacuum is globally active and enabled for the relation.
+ *
+ * elevel controls the log level for debug output.  Pass 0 to suppress logging.
  */
 static void
 relation_needs_vacanalyze(Oid relid,
@@ -3099,7 +3102,8 @@ relation_needs_vacanalyze(Oid relid,
 						  bool *dovacuum,
 						  bool *doanalyze,
 						  bool *wraparound,
-						  AutoVacuumScores *scores)
+						  AutoVacuumScores *scores,
+						  int elevel)
 {
 	bool		force_vacuum;
 	bool		av_enabled;
@@ -3352,19 +3356,22 @@ relation_needs_vacanalyze(Oid relid,
 				*doanalyze = true;
 		}
 
-		if (vac_ins_base_thresh >= 0)
-			elog(DEBUG3, "%s: vac: %.0f (thresh %.0f, score %.2f), ins: %.0f (thresh %.0f, score %.2f), anl: %.0f (thresh %.0f, score %.2f), xid score: %.2f, mxid score: %.2f",
-				 NameStr(classForm->relname),
-				 vactuples, vacthresh, scores->vac,
-				 instuples, vacinsthresh, scores->vac_ins,
-				 anltuples, anlthresh, scores->anl,
-				 scores->xid, scores->mxid);
-		else
-			elog(DEBUG3, "%s: vac: %.0f (thresh %.0f, score %.2f), ins: (disabled), anl: %.0f (thresh %.0f, score %.2f), xid score: %.2f, mxid score: %.2f",
-				 NameStr(classForm->relname),
-				 vactuples, vacthresh, scores->vac,
-				 anltuples, anlthresh, scores->anl,
-				 scores->xid, scores->mxid);
+		if (elevel > 0)
+		{
+			if (vac_ins_base_thresh >= 0)
+				elog(elevel, "%s: vac: %.0f (thresh %.0f, score %.2f), ins: %.0f (thresh %.0f, score %.2f), anl: %.0f (thresh %.0f, score %.2f), xid score: %.2f, mxid score: %.2f",
+					 NameStr(classForm->relname),
+					 vactuples, vacthresh, scores->vac,
+					 instuples, vacinsthresh, scores->vac_ins,
+					 anltuples, anlthresh, scores->anl,
+					 scores->xid, scores->mxid);
+			else
+				elog(elevel, "%s: vac: %.0f (thresh %.0f, score %.2f), ins: (disabled), anl: %.0f (thresh %.0f, score %.2f), xid score: %.2f, mxid score: %.2f",
+					 NameStr(classForm->relname),
+					 vactuples, vacthresh, scores->vac,
+					 anltuples, anlthresh, scores->anl,
+					 scores->xid, scores->mxid);
+		}
 	}
 }
 
-- 
2.47.3

