From 4c4bad998e29d940216b45fdbfee8368384bf642 Mon Sep 17 00:00:00 2001 From: B Sadhu Prasad Patro Date: Tue, 10 Aug 2021 08:36:41 -0700 Subject: [PATCH v2] DB-1318: pg_stat_reset and pg_stat_reset_single_table_counters don't work for shared objects. This patch will help to reset the statistics of shared tables also. Code changes are done in pgstat_reset_counters and pgstat_recv_resetsinglecounter. --- doc/src/sgml/monitoring.sgml | 5 +++-- src/backend/postmaster/pgstat.c | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 74a58a9..8b154b4 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -5056,7 +5056,8 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i void - Resets all statistics counters for the current database to zero. + Resets all statistics counters for the current database and reset + for shared tables also to zero. This function is restricted to superusers by default, but other users @@ -5097,7 +5098,7 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i Resets statistics for a single table or index in the current database - to zero. + to zero. It supports shared tables also. 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 1b54ef7..fcc226c 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -1425,7 +1425,8 @@ pgstat_send_connstats(bool disconnect, TimestampTz last_report) /* ---------- * pgstat_reset_counters() - * - * Tell the statistics collector to reset counters for our database. + * Tell the statistics collector to reset counters for our database + * and for shared tables. * * Permission checking for this function is managed through the normal * GRANT system. @@ -1442,6 +1443,10 @@ pgstat_reset_counters(void) pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETCOUNTER); msg.m_databaseid = MyDatabaseId; pgstat_send(&msg, sizeof(msg)); + + /* Reset the stat counters for Shared tables also. */ + msg.m_databaseid = InvalidOid; + pgstat_send(&msg, sizeof(msg)); } /* ---------- @@ -5199,7 +5204,8 @@ pgstat_recv_resetsharedcounter(PgStat_MsgResetsharedcounter *msg, int len) /* ---------- * pgstat_recv_resetsinglecounter() - * - * Reset a statistics for a single object + * Reset statistics for a single object. It may also be called for a + * shared table. * ---------- */ static void @@ -5207,7 +5213,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