From 6aa74e9f59d343523bf7c0ecea0301a0d85f7f1e Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Fri, 8 Aug 2025 07:21:35 +0000
Subject: [PATCH v1 09/10] Adding analyze_count to pg_stat_backend

Adding per backend number of manual analyzes triggered.

XXX: Bump catversion.
---
 doc/src/sgml/monitoring.sgml                 | 9 +++++++++
 src/backend/catalog/system_views.sql         | 1 +
 src/backend/utils/activity/pgstat_backend.c  | 4 ++++
 src/backend/utils/activity/pgstat_relation.c | 1 +
 src/backend/utils/adt/pgstatfuncs.c          | 4 +++-
 src/include/catalog/pg_proc.dat              | 6 +++---
 src/include/pgstat.h                         | 3 +++
 src/test/regress/expected/rules.out          | 3 ++-
 8 files changed, 26 insertions(+), 5 deletions(-)
  23.6% doc/src/sgml/
  14.8% src/backend/utils/activity/
  13.1% src/backend/utils/adt/
  25.0% src/include/catalog/
  10.8% src/include/
   9.7% src/test/regress/expected/

diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml
index d27369a4d21..dd4e9bb486e 100644
--- a/doc/src/sgml/monitoring.sgml
+++ b/doc/src/sgml/monitoring.sgml
@@ -1285,6 +1285,15 @@ description | Waiting for a newly initialized WAL file to reach durable storage
       </para></entry>
      </row>
 
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>analyze_count</structfield> <type>bigint</type>
+      </para>
+      <para>
+       The number of manual analyzes triggered.
+      </para></entry>
+     </row>
+
      <row>
       <entry role="catalog_table_entry"><para role="column_definition">
         <structfield>stats_reset</structfield> <type>timestamp with time zone</type>
diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql
index 5450d60a084..62af8b4f069 100644
--- a/src/backend/catalog/system_views.sql
+++ b/src/backend/catalog/system_views.sql
@@ -921,6 +921,7 @@ CREATE VIEW pg_stat_backend AS
             S.idx_tup_read,
             S.vacuum_count,
             S.last_vacuum,
+            S.analyze_count,
             S.stats_reset
     FROM pg_stat_get_backend_statistics(NULL) AS S;
 
diff --git a/src/backend/utils/activity/pgstat_backend.c b/src/backend/utils/activity/pgstat_backend.c
index c248da52bc2..cdd36160a4c 100644
--- a/src/backend/utils/activity/pgstat_backend.c
+++ b/src/backend/utils/activity/pgstat_backend.c
@@ -292,6 +292,7 @@ pgstat_flush_backend_entry_rel(PgStat_EntryRef *entry_ref)
 	BACKENDREL_ACC(idx_scan);
 	BACKENDREL_ACC(idx_tup_read);
 	BACKENDREL_ACC(vacuum_count);
+	BACKENDREL_ACC(analyze_count);
 #undef BACKENDREL_AC
 
 #define BACKENDREL_SET(stat) \
@@ -484,6 +485,9 @@ PGSTAT_COUNT_BACKEND_FUNC(idx_scan)
 /* pgstat_count_backend_rel_vacuum_count */
 PGSTAT_COUNT_BACKEND_FUNC(vacuum_count)
 
+/* pgstat_count_backend_rel_analayze_count */
+PGSTAT_COUNT_BACKEND_FUNC(analyze_count)
+
 void
 pgstat_count_backend_rel_idx_tup_read(PgStat_Counter n)
 {
diff --git a/src/backend/utils/activity/pgstat_relation.c b/src/backend/utils/activity/pgstat_relation.c
index d22ee2224a7..24313c3a5ec 100644
--- a/src/backend/utils/activity/pgstat_relation.c
+++ b/src/backend/utils/activity/pgstat_relation.c
@@ -360,6 +360,7 @@ pgstat_report_analyze(Relation rel,
 		tabentry->last_analyze_time = ts;
 		tabentry->analyze_count++;
 		tabentry->total_analyze_time += elapsedtime;
+		pgstat_count_backend_rel_analyze_count();
 	}
 
 	pgstat_unlock_entry(entry_ref);
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 8abf43185e5..c47af5c1b11 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -691,7 +691,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
 Datum
 pg_stat_get_backend_statistics(PG_FUNCTION_ARGS)
 {
-#define PG_STAT_GET_BACKEND_STATS_COLS	9
+#define PG_STAT_GET_BACKEND_STATS_COLS	10
 	int			num_backends = pgstat_fetch_stat_numbackends();
 	int			curr_backend;
 	int			pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
@@ -737,6 +737,8 @@ pg_stat_get_backend_statistics(PG_FUNCTION_ARGS)
 		else
 			nulls[i++] = true;
 
+		values[i++] = Int64GetDatum(backend_stats->analyze_count);
+
 		if (backend_stats->stat_reset_timestamp != 0)
 			values[i] = TimestampTzGetDatum(backend_stats->stat_reset_timestamp);
 		else
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 4c73c91bf84..eb6fb292bd5 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -5646,9 +5646,9 @@
   proname => 'pg_stat_get_backend_statistics', prorows => '100', proisstrict => 'f',
   proretset => 't', provolatile => 's', proparallel => 'r',
   prorettype => 'record', proargtypes => 'int4',
-  proallargtypes => '{int4,int4,int8,int8,int8,int8,int8,int8,timestamptz,timestamptz}',
-  proargmodes => '{i,o,o,o,o,o,o,o,o,o}',
-  proargnames => '{pid,pid,seq_scan,seq_tup_read,idx_tup_fetch,idx_scan,idx_tup_read,vacuum_count,last_vacuum,stats_reset}',
+  proallargtypes => '{int4,int4,int8,int8,int8,int8,int8,int8,timestamptz,int8,timestamptz}',
+  proargmodes => '{i,o,o,o,o,o,o,o,o,o,o}',
+  proargnames => '{pid,pid,seq_scan,seq_tup_read,idx_tup_fetch,idx_scan,idx_tup_read,vacuum_count,last_vacuum,analyze_count,stats_reset}',
   prosrc => 'pg_stat_get_backend_statistics' },
 { oid => '6318', descr => 'describe wait events',
   proname => 'pg_get_wait_events', procost => '10', prorows => '250',
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 5c1c49aaed0..16a17fdbcad 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -497,6 +497,7 @@ typedef struct PgStat_Backend
 	PgStat_Counter idx_tup_read;
 	PgStat_Counter vacuum_count;
 	TimestampTz last_vacuum;
+	PgStat_Counter analyze_count;
 } PgStat_Backend;
 
 typedef struct PgStat_BackendRelPending
@@ -508,6 +509,7 @@ typedef struct PgStat_BackendRelPending
 	PgStat_Counter idx_tup_read;
 	PgStat_Counter vacuum_count;
 	TimestampTz last_vacuum;
+	PgStat_Counter analyze_count;
 } PgStat_BackendRelPending;
 
 /* ---------
@@ -594,6 +596,7 @@ extern void pgstat_count_backend_rel_idx_scan(void);
 extern void pgstat_count_backend_rel_idx_tup_read(PgStat_Counter n);
 extern void pgstat_count_backend_rel_vacuum_count(void);
 extern void pgstat_set_backend_rel_last_vacuum(TimestampTz ts);
+extern void pgstat_count_backend_rel_analyze_count(void);
 
 /*
  * Functions in pgstat_bgwriter.c
diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out
index 89f03c7097c..32efc5d33e1 100644
--- a/src/test/regress/expected/rules.out
+++ b/src/test/regress/expected/rules.out
@@ -1855,8 +1855,9 @@ pg_stat_backend| SELECT pid,
     idx_tup_read,
     vacuum_count,
     last_vacuum,
+    analyze_count,
     stats_reset
-   FROM pg_stat_get_backend_statistics(NULL::integer) s(pid, seq_scan, seq_tup_read, idx_tup_fetch, idx_scan, idx_tup_read, vacuum_count, last_vacuum, stats_reset);
+   FROM pg_stat_get_backend_statistics(NULL::integer) s(pid, seq_scan, seq_tup_read, idx_tup_fetch, idx_scan, idx_tup_read, vacuum_count, last_vacuum, analyze_count, stats_reset);
 pg_stat_bgwriter| SELECT pg_stat_get_bgwriter_buf_written_clean() AS buffers_clean,
     pg_stat_get_bgwriter_maxwritten_clean() AS maxwritten_clean,
     pg_stat_get_buf_alloc() AS buffers_alloc,
-- 
2.34.1

