From 7de8f575533c9751894916183e74f37111d73a5f Mon Sep 17 00:00:00 2001
From: Shinya Kato <shinya11.kato@gmail.com>
Date: Fri, 2 May 2025 18:48:28 +0900
Subject: [PATCH v3 8/9] Make pg_stat_reset_subscription_stats() return the
 reset time

---
 doc/src/sgml/monitoring.sgml               |  8 ++++----
 src/backend/utils/adt/pgstatfuncs.c        |  7 ++++---
 src/include/catalog/pg_proc.dat            |  2 +-
 src/test/regress/expected/subscription.out | 16 ++++++++--------
 src/test/regress/sql/subscription.sql      |  4 ++--
 5 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml
index 38838755eed..fe561c01da1 100644
--- a/doc/src/sgml/monitoring.sgml
+++ b/doc/src/sgml/monitoring.sgml
@@ -5263,13 +5263,13 @@ description | Waiting for a newly initialized WAL file to reach durable storage
           <primary>pg_stat_reset_subscription_stats</primary>
         </indexterm>
         <function>pg_stat_reset_subscription_stats</function> ( <type>oid</type> )
-        <returnvalue>void</returnvalue>
+        <returnvalue>timestamp with time zone</returnvalue>
        </para>
        <para>
         Resets statistics for a single subscription shown in the
-        <structname>pg_stat_subscription_stats</structname> view to zero. If
-        the argument is <literal>NULL</literal>, reset statistics for all
-        subscriptions.
+        <structname>pg_stat_subscription_stats</structname> view to zero, and
+        returns the time of the reset. If the argument is
+        <literal>NULL</literal>, reset statistics for all subscriptions.
        </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 ce6630f5eac..b4ffc401626 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -2041,11 +2041,12 @@ Datum
 pg_stat_reset_subscription_stats(PG_FUNCTION_ARGS)
 {
 	Oid			subid;
+	TimestampTz ts;
 
 	if (PG_ARGISNULL(0))
 	{
 		/* Clear all subscription stats */
-		pgstat_reset_of_kind(PGSTAT_KIND_SUBSCRIPTION);
+		ts = pgstat_reset_of_kind(PGSTAT_KIND_SUBSCRIPTION);
 	}
 	else
 	{
@@ -2055,10 +2056,10 @@ pg_stat_reset_subscription_stats(PG_FUNCTION_ARGS)
 			ereport(ERROR,
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 					 errmsg("invalid subscription OID %u", subid)));
-		pgstat_reset(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid);
+		ts = pgstat_reset(PGSTAT_KIND_SUBSCRIPTION, InvalidOid, subid);
 	}
 
-	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 941cb8f9cf6..e9b56688032 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -6193,7 +6193,7 @@
 { oid => '6232',
   descr => 'statistics: reset collected statistics for a single subscription',
   proname => 'pg_stat_reset_subscription_stats', proisstrict => 'f',
-  provolatile => 'v', prorettype => 'void', proargtypes => 'oid',
+  provolatile => 'v', prorettype => 'timestamptz', proargtypes => 'oid',
   prosrc => 'pg_stat_reset_subscription_stats' },
 
 { oid => '3163', descr => 'current trigger depth',
diff --git a/src/test/regress/expected/subscription.out b/src/test/regress/expected/subscription.out
index 327d1e7731f..dca7fa66767 100644
--- a/src/test/regress/expected/subscription.out
+++ b/src/test/regress/expected/subscription.out
@@ -47,10 +47,10 @@ SELECT subname, stats_reset IS NULL stats_reset_is_null FROM pg_stat_subscriptio
  regress_testsub | t
 (1 row)
 
-SELECT pg_stat_reset_subscription_stats(oid) FROM pg_subscription WHERE subname = 'regress_testsub';
- pg_stat_reset_subscription_stats 
-----------------------------------
- 
+SELECT pg_stat_reset_subscription_stats(oid) IS NOT NULL AS t FROM pg_subscription WHERE subname = 'regress_testsub';
+ t 
+---
+ t
 (1 row)
 
 SELECT subname, stats_reset IS NULL stats_reset_is_null FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub';
@@ -61,10 +61,10 @@ SELECT subname, stats_reset IS NULL stats_reset_is_null FROM pg_stat_subscriptio
 
 -- Reset the stats again and check if the new reset_stats is updated.
 SELECT stats_reset as prev_stats_reset FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub' \gset
-SELECT pg_stat_reset_subscription_stats(oid) FROM pg_subscription WHERE subname = 'regress_testsub';
- pg_stat_reset_subscription_stats 
-----------------------------------
- 
+SELECT pg_stat_reset_subscription_stats(oid) IS NOT NULL AS t FROM pg_subscription WHERE subname = 'regress_testsub';
+ t 
+---
+ t
 (1 row)
 
 SELECT :'prev_stats_reset' < stats_reset FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub';
diff --git a/src/test/regress/sql/subscription.sql b/src/test/regress/sql/subscription.sql
index ef0c298d2df..8f4aa760fe2 100644
--- a/src/test/regress/sql/subscription.sql
+++ b/src/test/regress/sql/subscription.sql
@@ -34,12 +34,12 @@ SELECT obj_description(s.oid, 'pg_subscription') FROM pg_subscription s;
 -- Check if the subscription stats are created and stats_reset is updated
 -- by pg_stat_reset_subscription_stats().
 SELECT subname, stats_reset IS NULL stats_reset_is_null FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub';
-SELECT pg_stat_reset_subscription_stats(oid) FROM pg_subscription WHERE subname = 'regress_testsub';
+SELECT pg_stat_reset_subscription_stats(oid) IS NOT NULL AS t FROM pg_subscription WHERE subname = 'regress_testsub';
 SELECT subname, stats_reset IS NULL stats_reset_is_null FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub';
 
 -- Reset the stats again and check if the new reset_stats is updated.
 SELECT stats_reset as prev_stats_reset FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub' \gset
-SELECT pg_stat_reset_subscription_stats(oid) FROM pg_subscription WHERE subname = 'regress_testsub';
+SELECT pg_stat_reset_subscription_stats(oid) IS NOT NULL AS t FROM pg_subscription WHERE subname = 'regress_testsub';
 SELECT :'prev_stats_reset' < stats_reset FROM pg_stat_subscription_stats WHERE subname = 'regress_testsub';
 
 -- fail - name already exists
-- 
2.47.3

