From 7449c99e186d487da3e812ab093a52531bb64f5b Mon Sep 17 00:00:00 2001
From: Shinya Kato <shinya11.kato@gmail.com>
Date: Fri, 2 May 2025 15:30:39 +0900
Subject: [PATCH v1 3/9] Make pg_stat_reset_single_table_counters() return the
 reset time

---
 doc/src/sgml/monitoring.sgml        |  5 +++--
 src/backend/utils/activity/pgstat.c |  4 +++-
 src/backend/utils/adt/pgstatfuncs.c |  5 +++--
 src/include/catalog/pg_proc.dat     |  2 +-
 src/include/pgstat.h                |  2 +-
 src/test/regress/expected/stats.out | 16 ++++++++--------
 src/test/regress/sql/stats.sql      |  4 ++--
 7 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml
index 521ad113012..99fdc8aabac 100644
--- a/doc/src/sgml/monitoring.sgml
+++ b/doc/src/sgml/monitoring.sgml
@@ -5095,11 +5095,12 @@ description | Waiting for a newly initialized WAL file to reach durable storage
          <primary>pg_stat_reset_single_table_counters</primary>
         </indexterm>
         <function>pg_stat_reset_single_table_counters</function> ( <type>oid</type> )
-        <returnvalue>void</returnvalue>
+        <returnvalue>timestamp with time zone</returnvalue>
        </para>
        <para>
         Resets statistics for a single table or index in the current database
-        or shared across all databases in the cluster to zero.
+        or shared across all databases in the cluster to zero, and returns the
+        time of the reset.
        </para>
        <para>
         This function is restricted to superusers by default, but other users
diff --git a/src/backend/utils/activity/pgstat.c b/src/backend/utils/activity/pgstat.c
index 00b1ed31cef..aecf9fbdcd1 100644
--- a/src/backend/utils/activity/pgstat.c
+++ b/src/backend/utils/activity/pgstat.c
@@ -851,7 +851,7 @@ pgstat_reset_counters(void)
  * Permission checking for this function is managed through the normal
  * GRANT system.
  */
-void
+TimestampTz
 pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
 {
 	const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
@@ -865,6 +865,8 @@ pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
 
 	if (!kind_info->accessed_across_databases)
 		pgstat_reset_database_timestamp(dboid, ts);
+
+	return ts;
 }
 
 /*
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 36404fac4c6..bc985f6eaad 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -1922,10 +1922,11 @@ pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
 {
 	Oid			taboid = PG_GETARG_OID(0);
 	Oid			dboid = (IsSharedRelation(taboid) ? InvalidOid : MyDatabaseId);
+	TimestampTz ts;
 
-	pgstat_reset(PGSTAT_KIND_RELATION, dboid, taboid);
+	ts = pgstat_reset(PGSTAT_KIND_RELATION, dboid, taboid);
 
-	PG_RETURN_VOID();
+	PG_RETURN_TIMESTAMPTZ(ts);
 }
 
 Datum
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 9f86ad80ade..9077c313bfd 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -6149,7 +6149,7 @@
 { oid => '3776',
   descr => 'statistics: reset collected statistics for a single table or index in the current database or shared across all databases in the cluster',
   proname => 'pg_stat_reset_single_table_counters', provolatile => 'v',
-  prorettype => 'void', proargtypes => 'oid',
+  prorettype => 'timestamptz', proargtypes => 'oid',
   prosrc => 'pg_stat_reset_single_table_counters' },
 { oid => '3777',
   descr => 'statistics: reset collected statistics for a single function in the current database',
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index d89175927ee..ead16d7dad2 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -525,7 +525,7 @@ extern long pgstat_report_stat(bool force);
 extern void pgstat_force_next_flush(void);
 
 extern TimestampTz pgstat_reset_counters(void);
-extern void pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid);
+extern TimestampTz pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid);
 extern TimestampTz pgstat_reset_of_kind(PgStat_Kind kind);
 
 /* stats accessors */
diff --git a/src/test/regress/expected/stats.out b/src/test/regress/expected/stats.out
index 34a2351e29b..983930e5009 100644
--- a/src/test/regress/expected/stats.out
+++ b/src/test/regress/expected/stats.out
@@ -666,10 +666,10 @@ SELECT last_seq_scan, last_idx_scan FROM pg_stat_all_tables WHERE relid = 'test_
 (1 row)
 
 COMMIT;
-SELECT pg_stat_reset_single_table_counters('test_last_scan'::regclass);
- pg_stat_reset_single_table_counters 
--------------------------------------
- 
+SELECT pg_stat_reset_single_table_counters('test_last_scan'::regclass) IS NOT NULL AS t;
+ t 
+---
+ t
 (1 row)
 
 SELECT seq_scan, idx_scan FROM pg_stat_all_tables WHERE relid = 'test_last_scan'::regclass;
@@ -877,10 +877,10 @@ SELECT (n_tup_ins + n_tup_upd) > 0 AS has_data FROM pg_stat_all_tables
  t
 (1 row)
 
-SELECT pg_stat_reset_single_table_counters('pg_shdescription'::regclass);
- pg_stat_reset_single_table_counters 
--------------------------------------
- 
+SELECT pg_stat_reset_single_table_counters('pg_shdescription'::regclass) IS NOT NULL AS t;
+ t 
+---
+ t
 (1 row)
 
 SELECT (n_tup_ins + n_tup_upd) > 0 AS has_data FROM pg_stat_all_tables
diff --git a/src/test/regress/sql/stats.sql b/src/test/regress/sql/stats.sql
index 8249c2d4d89..1801b0fec9a 100644
--- a/src/test/regress/sql/stats.sql
+++ b/src/test/regress/sql/stats.sql
@@ -312,7 +312,7 @@ SELECT pg_stat_force_next_flush();
 SELECT last_seq_scan, last_idx_scan FROM pg_stat_all_tables WHERE relid = 'test_last_scan'::regclass;
 COMMIT;
 
-SELECT pg_stat_reset_single_table_counters('test_last_scan'::regclass);
+SELECT pg_stat_reset_single_table_counters('test_last_scan'::regclass) IS NOT NULL AS t;
 SELECT seq_scan, idx_scan FROM pg_stat_all_tables WHERE relid = 'test_last_scan'::regclass;
 
 -- ensure we start out with exactly one index and sequential scan
@@ -402,7 +402,7 @@ COMMIT;
 -- check that the stats are reset.
 SELECT (n_tup_ins + n_tup_upd) > 0 AS has_data FROM pg_stat_all_tables
   WHERE relid = 'pg_shdescription'::regclass;
-SELECT pg_stat_reset_single_table_counters('pg_shdescription'::regclass);
+SELECT pg_stat_reset_single_table_counters('pg_shdescription'::regclass) IS NOT NULL AS t;
 SELECT (n_tup_ins + n_tup_upd) > 0 AS has_data FROM pg_stat_all_tables
   WHERE relid = 'pg_shdescription'::regclass;
 
-- 
2.47.1

