From 1d83c84ff849afbec324f5d21820b185b1104166 Mon Sep 17 00:00:00 2001
From: Shinya Kato <shinya11.kato@gmail.com>
Date: Fri, 2 May 2025 15:41:00 +0900
Subject: [PATCH v2 4/9] Make pg_stat_reset_backend_stats() return the reset
 time

---
 doc/src/sgml/monitoring.sgml        |  5 +++--
 src/backend/utils/adt/pgstatfuncs.c | 11 ++++++-----
 src/include/catalog/pg_proc.dat     |  2 +-
 src/test/regress/expected/stats.out |  9 ++++++++-
 src/test/regress/sql/stats.sql      |  4 +++-
 5 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml
index 99fdc8aabac..f48aefd054c 100644
--- a/doc/src/sgml/monitoring.sgml
+++ b/doc/src/sgml/monitoring.sgml
@@ -5114,11 +5114,12 @@ description | Waiting for a newly initialized WAL file to reach durable storage
          <primary>pg_stat_reset_backend_stats</primary>
         </indexterm>
         <function>pg_stat_reset_backend_stats</function> ( <type>integer</type> )
-        <returnvalue>void</returnvalue>
+        <returnvalue>timestamp with time zone</returnvalue>
        </para>
        <para>
         Resets statistics for a single backend with the specified process ID
-        to zero.
+        to zero, and returns the time of the reset or <literal>NULL</literal>
+        if an invalid backend process ID is specified.
        </para>
        <para>
         This function is restricted to superusers by default, but other users
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index bc985f6eaad..8350233dcaf 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -1948,6 +1948,7 @@ pg_stat_reset_backend_stats(PG_FUNCTION_ARGS)
 	PGPROC	   *proc;
 	PgBackendStatus *beentry;
 	ProcNumber	procNumber;
+	TimestampTz ts;
 	int			backend_pid = PG_GETARG_INT32(0);
 
 	proc = BackendPidGetProc(backend_pid);
@@ -1957,21 +1958,21 @@ pg_stat_reset_backend_stats(PG_FUNCTION_ARGS)
 		proc = AuxiliaryPidGetProc(backend_pid);
 
 	if (!proc)
-		PG_RETURN_VOID();
+		PG_RETURN_NULL();
 
 	procNumber = GetNumberFromPGProc(proc);
 
 	beentry = pgstat_get_beentry_by_proc_number(procNumber);
 	if (!beentry)
-		PG_RETURN_VOID();
+		PG_RETURN_NULL();
 
 	/* Check if the backend type tracks statistics */
 	if (!pgstat_tracks_backend_bktype(beentry->st_backendType))
-		PG_RETURN_VOID();
+		PG_RETURN_NULL();
 
-	pgstat_reset(PGSTAT_KIND_BACKEND, InvalidOid, procNumber);
+	ts = pgstat_reset(PGSTAT_KIND_BACKEND, InvalidOid, procNumber);
 
-	PG_RETURN_VOID();
+	PG_RETURN_TIMESTAMPTZ(ts);
 }
 
 /* Reset SLRU counters (a specific one or all of them). */
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 64f0a3d2d05..f4fe2626ef3 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -6170,7 +6170,7 @@
   prosrc => 'pg_stat_reset_single_function_counters' },
 { oid => '6387', descr => 'statistics: reset statistics for a single backend',
   proname => 'pg_stat_reset_backend_stats', provolatile => 'v',
-  prorettype => 'void', proargtypes => 'int4',
+  prorettype => 'timestamptz', proargtypes => 'int4',
   prosrc => 'pg_stat_reset_backend_stats' },
 { oid => '2307',
   descr => 'statistics: reset collected statistics for a single SLRU',
diff --git a/src/test/regress/expected/stats.out b/src/test/regress/expected/stats.out
index 983930e5009..91b3be1a8af 100644
--- a/src/test/regress/expected/stats.out
+++ b/src/test/regress/expected/stats.out
@@ -1720,7 +1720,14 @@ SELECT :my_io_stats_pre_reset <= :my_io_stats_post_reset;
 (1 row)
 
 -- but pg_stat_reset_backend_stats() does
-SELECT pg_stat_reset_backend_stats(pg_backend_pid());
+SELECT pg_stat_reset_backend_stats(pg_backend_pid()) IS NOT NULL AS t;
+ t 
+---
+ t
+(1 row)
+
+-- Invalid backend process ID is specified
+SELECT pg_stat_reset_backend_stats(0);
  pg_stat_reset_backend_stats 
 -----------------------------
  
diff --git a/src/test/regress/sql/stats.sql b/src/test/regress/sql/stats.sql
index 1801b0fec9a..77d3e1a845d 100644
--- a/src/test/regress/sql/stats.sql
+++ b/src/test/regress/sql/stats.sql
@@ -822,7 +822,9 @@ SELECT sum(evictions) + sum(reuses) + sum(extends) + sum(fsyncs) + sum(reads) +
 -- pg_stat_reset_shared() did not reset backend IO stats
 SELECT :my_io_stats_pre_reset <= :my_io_stats_post_reset;
 -- but pg_stat_reset_backend_stats() does
-SELECT pg_stat_reset_backend_stats(pg_backend_pid());
+SELECT pg_stat_reset_backend_stats(pg_backend_pid()) IS NOT NULL AS t;
+-- Invalid backend process ID is specified
+SELECT pg_stat_reset_backend_stats(0);
 SELECT sum(evictions) + sum(reuses) + sum(extends) + sum(fsyncs) + sum(reads) + sum(writes) + sum(writebacks) + sum(hits) AS my_io_stats_post_backend_reset
   FROM pg_stat_get_backend_io(pg_backend_pid()) \gset
 SELECT :my_io_stats_pre_reset > :my_io_stats_post_backend_reset;
-- 
2.47.3

