From f0cfbb67614b83848077cfc7c7c24a2b25f3cd7c 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 v3 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 | 24 ++++++++++++------------
 src/test/regress/sql/stats.sql      |  6 +++---
 7 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml
index e5edee97e94..f70ddc13f33 100644
--- a/doc/src/sgml/monitoring.sgml
+++ b/doc/src/sgml/monitoring.sgml
@@ -5155,11 +5155,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 1fcd140a589..5b7166ee554 100644
--- a/src/backend/utils/activity/pgstat.c
+++ b/src/backend/utils/activity/pgstat.c
@@ -853,7 +853,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);
@@ -867,6 +867,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 4a84575e7a7..b87f99dee19 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -1943,10 +1943,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 df291d21ef7..5fea29d7b27 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -6169,7 +6169,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 a91984f086f..cac2cef060d 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -530,7 +530,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 9b93d257992..a3f9e04c555 100644
--- a/src/test/regress/expected/stats.out
+++ b/src/test/regress/expected/stats.out
@@ -673,10 +673,10 @@ SELECT stats_reset IS NOT NULL AS has_stats_reset
  f
 (1 row)
 
-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, stats_reset IS NOT NULL AS has_stats_reset
@@ -868,10 +868,10 @@ SELECT idx_scan, :'test_last_idx' < last_idx_scan AS idx_ok,
 (1 row)
 
 -- check that the stats in pg_stat_all_indexes are reset
-SELECT pg_stat_reset_single_table_counters('test_last_scan_pkey'::regclass);
- pg_stat_reset_single_table_counters 
--------------------------------------
- 
+SELECT pg_stat_reset_single_table_counters('test_last_scan_pkey'::regclass) IS NOT NULL AS t;
+ t 
+---
+ t
 (1 row)
 
 SELECT idx_scan, stats_reset IS NOT NULL AS has_stats_reset
@@ -908,10 +908,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 07fcff8215a..50ba7354b7e 100644
--- a/src/test/regress/sql/stats.sql
+++ b/src/test/regress/sql/stats.sql
@@ -314,7 +314,7 @@ COMMIT;
 
 SELECT stats_reset IS NOT NULL AS has_stats_reset
   FROM pg_stat_all_tables WHERE relid = 'test_last_scan'::regclass;
-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, stats_reset IS NOT NULL AS has_stats_reset
   FROM pg_stat_all_tables WHERE relid = 'test_last_scan'::regclass;
 
@@ -391,7 +391,7 @@ SELECT idx_scan, :'test_last_idx' < last_idx_scan AS idx_ok,
   FROM pg_stat_all_indexes WHERE indexrelid = 'test_last_scan_pkey'::regclass;
 
 -- check that the stats in pg_stat_all_indexes are reset
-SELECT pg_stat_reset_single_table_counters('test_last_scan_pkey'::regclass);
+SELECT pg_stat_reset_single_table_counters('test_last_scan_pkey'::regclass) IS NOT NULL AS t;
 
 SELECT idx_scan, stats_reset IS NOT NULL AS has_stats_reset
   FROM pg_stat_all_indexes WHERE indexrelid = 'test_last_scan_pkey'::regclass;
@@ -416,7 +416,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.3

