From 21e927bd598fcdee5ae19f3fc64e9785f717be75 Mon Sep 17 00:00:00 2001
From: B Sadhu Prasad Patro <b.sadhuprasadp@enterprisedb.com>
Date: Thu, 19 Aug 2021 17:56:12 -0700
Subject: [PATCH v4] Enhancing pg_stat_reset_single_table_counters to support
 shared table as well.

This patch will help to reset the stats for shared tables also by existing
function pgstat_recv_resetsinglecounter.
---
 doc/src/sgml/monitoring.sgml    | 2 +-
 src/backend/postmaster/pgstat.c | 9 +++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml
index 74a58a9..7bdf371 100644
--- a/doc/src/sgml/monitoring.sgml
+++ b/doc/src/sgml/monitoring.sgml
@@ -5097,7 +5097,7 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i
        </para>
        <para>
         Resets statistics for a single table or index in the current database
-        to zero.
+        to zero. The input can be a shared table also.
        </para>
        <para>
         This function is restricted to superusers by default, but other users
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index a3c35bd..0cd7c43 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -40,6 +40,7 @@
 #include "access/xact.h"
 #include "catalog/pg_database.h"
 #include "catalog/pg_proc.h"
+#include "catalog/catalog.h"
 #include "common/ip.h"
 #include "executor/instrument.h"
 #include "libpq/libpq.h"
@@ -5135,7 +5136,8 @@ pgstat_recv_resetsharedcounter(PgStat_MsgResetsharedcounter *msg, int len)
 /* ----------
  * pgstat_recv_resetsinglecounter() -
  *
- *	Reset a statistics for a single object
+ *	Reset a statistics for a single object, which may also be a shared
+ *	table.
  * ----------
  */
 static void
@@ -5143,7 +5145,10 @@ pgstat_recv_resetsinglecounter(PgStat_MsgResetsinglecounter *msg, int len)
 {
 	PgStat_StatDBEntry *dbentry;
 
-	dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
+	if (!IsSharedRelation(msg->m_objectid))
+		dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
+	else
+		dbentry = pgstat_get_db_entry(InvalidOid, false);
 
 	if (!dbentry)
 		return;
-- 
1.8.3.1

