From a9bc7c62131c13ae99b7f62811f67485129f04ad Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Mon, 11 Aug 2025 11:23:15 +0000
Subject: [PATCH v1 10/10] Adding last_analyze to pg_stat_backend

Adding the last time that a backend triggered a manual analyze.

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          | 7 ++++++-
 src/include/catalog/pg_proc.dat              | 6 +++---
 src/include/pgstat.h                         | 3 +++
 src/test/regress/expected/rules.out          | 3 ++-
 8 files changed, 29 insertions(+), 5 deletions(-)
  23.1% doc/src/sgml/
  12.2% src/backend/utils/activity/
  17.2% src/backend/utils/adt/
  25.3% src/include/catalog/
   9.2% src/include/
  10.6% src/test/regress/expected/

diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml
index dd4e9bb486e..ed83c36188e 100644
--- a/doc/src/sgml/monitoring.sgml
+++ b/doc/src/sgml/monitoring.sgml
@@ -1294,6 +1294,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>last_analyze</structfield> <type>timestamp with time zone</type>
+      </para>
+      <para>
+       Last time at which this backend triggered a manual analyze.
+      </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 62af8b4f069..ffb9bdbe92c 100644
--- a/src/backend/catalog/system_views.sql
+++ b/src/backend/catalog/system_views.sql
@@ -922,6 +922,7 @@ CREATE VIEW pg_stat_backend AS
             S.vacuum_count,
             S.last_vacuum,
             S.analyze_count,
+            S.last_analyze,
             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 cdd36160a4c..50d08e58e4a 100644
--- a/src/backend/utils/activity/pgstat_backend.c
+++ b/src/backend/utils/activity/pgstat_backend.c
@@ -300,6 +300,7 @@ pgstat_flush_backend_entry_rel(PgStat_EntryRef *entry_ref)
 		(shbackendent->stats.stat = PendingBackendStats.pending_backendrel.stat)
 
 	BACKENDREL_SET(last_vacuum);
+	BACKENDREL_SET(last_analyze);
 #undef BACKENDREL_SET
 
 	/*
@@ -512,3 +513,6 @@ CppConcat(pgstat_set_backend_rel_,stat)(TimestampTz ts)		\
 
 /* pgstat_set_backend_rel_last_vacuum */
 PGSTAT_SETTS_BACKEND_FUNC(last_vacuum)
+
+/* pgstat_set_backend_rel_last_analyze */
+PGSTAT_SETTS_BACKEND_FUNC(last_analyze)
diff --git a/src/backend/utils/activity/pgstat_relation.c b/src/backend/utils/activity/pgstat_relation.c
index 24313c3a5ec..d9fafd6980f 100644
--- a/src/backend/utils/activity/pgstat_relation.c
+++ b/src/backend/utils/activity/pgstat_relation.c
@@ -361,6 +361,7 @@ pgstat_report_analyze(Relation rel,
 		tabentry->analyze_count++;
 		tabentry->total_analyze_time += elapsedtime;
 		pgstat_count_backend_rel_analyze_count();
+		pgstat_set_backend_rel_last_analyze(ts);
 	}
 
 	pgstat_unlock_entry(entry_ref);
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index c47af5c1b11..ae56bcf878f 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	10
+#define PG_STAT_GET_BACKEND_STATS_COLS	11
 	int			num_backends = pgstat_fetch_stat_numbackends();
 	int			curr_backend;
 	int			pid = PG_ARGISNULL(0) ? -1 : PG_GETARG_INT32(0);
@@ -739,6 +739,11 @@ pg_stat_get_backend_statistics(PG_FUNCTION_ARGS)
 
 		values[i++] = Int64GetDatum(backend_stats->analyze_count);
 
+		if (backend_stats->last_analyze != 0)
+			values[i++] = TimestampTzGetDatum(backend_stats->last_analyze);
+		else
+			nulls[i++] = true;
+
 		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 eb6fb292bd5..330c9d93440 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,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}',
+  proallargtypes => '{int4,int4,int8,int8,int8,int8,int8,int8,timestamptz,int8,timestamptz,timestamptz}',
+  proargmodes => '{i,o,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,last_analyze,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 16a17fdbcad..c8cd04af909 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -498,6 +498,7 @@ typedef struct PgStat_Backend
 	PgStat_Counter vacuum_count;
 	TimestampTz last_vacuum;
 	PgStat_Counter analyze_count;
+	TimestampTz last_analyze;
 } PgStat_Backend;
 
 typedef struct PgStat_BackendRelPending
@@ -510,6 +511,7 @@ typedef struct PgStat_BackendRelPending
 	PgStat_Counter vacuum_count;
 	TimestampTz last_vacuum;
 	PgStat_Counter analyze_count;
+	TimestampTz last_analyze;
 } PgStat_BackendRelPending;
 
 /* ---------
@@ -597,6 +599,7 @@ 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);
+extern void pgstat_set_backend_rel_last_analyze(TimestampTz ts);
 
 /*
  * Functions in pgstat_bgwriter.c
diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out
index 32efc5d33e1..b31f1bc06a0 100644
--- a/src/test/regress/expected/rules.out
+++ b/src/test/regress/expected/rules.out
@@ -1856,8 +1856,9 @@ pg_stat_backend| SELECT pid,
     vacuum_count,
     last_vacuum,
     analyze_count,
+    last_analyze,
     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);
+   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, last_analyze, 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

