]> granicus.if.org Git - postgresql/commitdiff
Add functions to reset the statistics counter for a single table/index or
authorMagnus Hagander <magnus@hagander.net>
Thu, 28 Jan 2010 14:25:41 +0000 (14:25 +0000)
committerMagnus Hagander <magnus@hagander.net>
Thu, 28 Jan 2010 14:25:41 +0000 (14:25 +0000)
a single function.

doc/src/sgml/monitoring.sgml
src/backend/postmaster/pgstat.c
src/backend/utils/adt/pgstatfuncs.c
src/include/catalog/catversion.h
src/include/catalog/pg_proc.h
src/include/pgstat.h

index 940b8b79ffe5940a0a8b9a9b38136d69c276d6c4..954dd1a2e4ba053617ad3c8d514b87796cdca00d 100644 (file)
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.74 2010/01/19 14:11:30 mha Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.75 2010/01/28 14:25:41 mha Exp $ -->
 
 <chapter id="monitoring">
  <title>Monitoring Database Activity</title>
@@ -929,6 +929,24 @@ postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <re
        <structname>pg_stat_bgwriter</>.
       </entry>
      </row>
+
+     <row>
+      <entry><literal><function>pg_stat_reset_single_table_counters</function>(oid)</literal></entry>
+      <entry><type>void</type></entry>
+      <entry>
+       Reset statistics for a single table or index in the current database to
+       zero (requires superuser privileges)
+      </entry>
+     </row>
+
+     <row>
+      <entry><literal><function>pg_stat_reset_single_function_counters</function>(oid)</literal></entry>
+      <entry><type>void</type></entry>
+      <entry>
+       Reset statistics for a single function in the current database to
+       zero (requires superuser privileges)
+      </entry>
+     </row>
     </tbody>
    </tgroup>
   </table>
index 2d716d8576382ba5141fc07cb1be315191a52fd6..8205c1d143b23104496a2629ea03dcf2d2129a03 100644 (file)
@@ -13,7 +13,7 @@
  *
  *     Copyright (c) 2001-2010, PostgreSQL Global Development Group
  *
- *     $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.198 2010/01/19 14:11:30 mha Exp $
+ *     $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.199 2010/01/28 14:25:41 mha Exp $
  * ----------
  */
 #include "postgres.h"
@@ -271,6 +271,7 @@ static void pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len);
 static void pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len);
 static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
 static void pgstat_recv_resetsharedcounter(PgStat_MsgResetsharedcounter *msg, int len);
+static void pgstat_recv_resetsinglecounter(PgStat_MsgResetsinglecounter *msg, int len);
 static void pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len);
 static void pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len);
 static void pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len);
@@ -1187,6 +1188,32 @@ pgstat_reset_shared_counters(const char *target)
        pgstat_send(&msg, sizeof(msg));
 }
 
+/* ----------
+ * pgstat_reset_single_counter() -
+ *
+ *     Tell the statistics collector to reset a single counter.
+ * ----------
+ */
+void pgstat_reset_single_counter(Oid objoid, PgStat_Single_Reset_Type type)
+{
+       PgStat_MsgResetsinglecounter msg;
+
+       if (pgStatSock < 0)
+               return;
+
+       if (!superuser())
+               ereport(ERROR,
+                               (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+                                errmsg("must be superuser to reset statistics counters")));
+
+       pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETSINGLECOUNTER);
+       msg.m_databaseid = MyDatabaseId;
+       msg.m_resettype = type;
+       msg.m_objectid = objoid;
+
+       pgstat_send(&msg, sizeof(msg));
+}
+
 /* ----------
  * pgstat_report_autovac() -
  *
@@ -2954,6 +2981,12 @@ PgstatCollectorMain(int argc, char *argv[])
                                                                                         len);
                                        break;
 
+                               case PGSTAT_MTYPE_RESETSINGLECOUNTER:
+                                       pgstat_recv_resetsinglecounter(
+                                                                                        (PgStat_MsgResetsinglecounter *) &msg,
+                                                                                        len);
+                                       break;
+
                                case PGSTAT_MTYPE_AUTOVAC_START:
                                        pgstat_recv_autovac((PgStat_MsgAutovacStart *) &msg, len);
                                        break;
@@ -3928,6 +3961,30 @@ pgstat_recv_resetsharedcounter(PgStat_MsgResetsharedcounter *msg, int len)
         */
 }
 
+/* ----------
+ * pgstat_recv_resetsinglecounter() -
+ *
+ *     Reset a statistics for a single object
+ * ----------
+ */
+static void
+pgstat_recv_resetsinglecounter(PgStat_MsgResetsinglecounter *msg, int len)
+{
+       PgStat_StatDBEntry *dbentry;
+
+       dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
+
+       if (!dbentry)
+               return;
+
+
+       /* Remove object if it exists, ignore it if not */
+       if (msg->m_resettype == RESET_TABLE)
+               (void) hash_search(dbentry->tables, (void *) &(msg->m_objectid), HASH_REMOVE, NULL);
+       else if (msg->m_resettype == RESET_FUNCTION)
+               (void) hash_search(dbentry->functions, (void *)&(msg->m_objectid), HASH_REMOVE, NULL);
+}
+
 /* ----------
  * pgstat_recv_autovac() -
  *
index 06dfd3bc93bfe3a9d61a1aab0dd5d2fe7e7e5965..93fad320c1600eef9e85ab00c39f6581b4c2b3cc 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.58 2010/01/19 14:11:31 mha Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.59 2010/01/28 14:25:41 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -79,6 +79,8 @@ extern Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS);
 extern Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS);
 extern Datum pg_stat_reset(PG_FUNCTION_ARGS);
 extern Datum pg_stat_reset_shared(PG_FUNCTION_ARGS);
+extern Datum pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS);
+extern Datum pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS);
 
 /* Global bgwriter statistics, from bgwriter.c */
 extern PgStat_MsgBgWriter bgwriterStats;
@@ -1120,3 +1122,24 @@ pg_stat_reset_shared(PG_FUNCTION_ARGS)
 
        PG_RETURN_VOID();
 }
+
+/* Reset a a single counter in the current database */
+Datum
+pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
+{
+       Oid             taboid = PG_GETARG_OID(0);
+
+       pgstat_reset_single_counter(taboid, RESET_TABLE);
+
+       PG_RETURN_VOID();
+}
+
+Datum
+pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS)
+{
+       Oid             funcoid = PG_GETARG_OID(0);
+
+       pgstat_reset_single_counter(funcoid, RESET_FUNCTION);
+
+       PG_RETURN_VOID();
+}
index 4c0fde84e39d839d91330bd7b0c548425dcbaf1d..4f6ccbcf0c313cc97a20091bc92f621ec0f0bde3 100644 (file)
@@ -37,7 +37,7 @@
  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.579 2010/01/25 20:55:32 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.580 2010/01/28 14:25:41 mha Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -53,6 +53,6 @@
  */
 
 /*                                                     yyyymmddN */
-#define CATALOG_VERSION_NO     201001251
+#define CATALOG_VERSION_NO     201001281
 
 #endif
index f4b816f128b52032c9bc1774c8305a2654865e54..a30e06975815b304291e1b765b2d549c8ea03e64 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.565 2010/01/25 20:55:32 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.566 2010/01/28 14:25:41 mha Exp $
  *
  * NOTES
  *       The script catalog/genbki.pl reads this file and generates .bki
@@ -3087,6 +3087,10 @@ DATA(insert OID = 2274 (  pg_stat_reset                                  PGNSP PGUID 12 1 0 0 f f f f f v 0 0
 DESCR("statistics: reset collected statistics for current database");
 DATA(insert OID = 3775 (  pg_stat_reset_shared                 PGNSP PGUID 12 1 0 0 f f f f f v 1 0 2278 "25" _null_ _null_ _null_ _null_      pg_stat_reset_shared _null_ _null_ _null_ ));
 DESCR("statistics: reset collected statistics shared across the cluster");
+DATA(insert OID = 3776 (  pg_stat_reset_single_table_counters  PGNSP PGUID 12 1 0 0 f f f f f v 1 0 2278 "26" _null_ _null_ _null_ _null_  pg_stat_reset_single_table_counters _null_ _null_ _null_ ));
+DESCR("statistics: reset collected statistics for a single table or index in the current database");
+DATA(insert OID = 3777 (  pg_stat_reset_single_function_counters       PGNSP PGUID 12 1 0 0 f f f f f v 1 0 2278 "26" _null_ _null_ _null_ _null_  pg_stat_reset_single_function_counters _null_ _null_ _null_ ));
+DESCR("statistics: reset collected statistics for a single function in the current database");
 
 DATA(insert OID = 1946 (  encode                                               PGNSP PGUID 12 1 0 0 f f f t f i 2 0 25 "17 25" _null_ _null_ _null_ _null_ binary_encode _null_ _null_ _null_ ));
 DESCR("convert bytea value into some ascii-only text string");
index acadef35a201e506c1336a17c367a1a68553fb55..6545ddf8588a996f93f8565a5b345a036c135494 100644 (file)
@@ -5,7 +5,7 @@
  *
  *     Copyright (c) 2001-2010, PostgreSQL Global Development Group
  *
- *     $PostgreSQL: pgsql/src/include/pgstat.h,v 1.87 2010/01/19 14:11:31 mha Exp $
+ *     $PostgreSQL: pgsql/src/include/pgstat.h,v 1.88 2010/01/28 14:25:41 mha Exp $
  * ----------
  */
 #ifndef PGSTAT_H
@@ -39,6 +39,7 @@ typedef enum StatMsgType
        PGSTAT_MTYPE_DROPDB,
        PGSTAT_MTYPE_RESETCOUNTER,
        PGSTAT_MTYPE_RESETSHAREDCOUNTER,
+       PGSTAT_MTYPE_RESETSINGLECOUNTER,
        PGSTAT_MTYPE_AUTOVAC_START,
        PGSTAT_MTYPE_VACUUM,
        PGSTAT_MTYPE_ANALYZE,
@@ -100,6 +101,12 @@ typedef enum PgStat_Shared_Reset_Target
     RESET_BGWRITER
 } PgStat_Shared_Reset_Target;
 
+/* Possible object types for resetting single counters */
+typedef enum PgStat_Single_Reset_Type
+{
+       RESET_TABLE,
+       RESET_FUNCTION
+} PgStat_Single_Reset_Type;
 
 /* ------------------------------------------------------------
  * Structures kept in backend local memory while accumulating counts
@@ -278,6 +285,19 @@ typedef struct PgStat_MsgResetsharedcounter
        PgStat_Shared_Reset_Target m_resettarget;
 } PgStat_MsgResetsharedcounter;
 
+/* ----------
+ * PgStat_MsgResetsinglecounter        Sent by the backend to tell the collector
+ *                                                             to reset a single counter
+ * ----------
+ */
+typedef struct PgStat_MsgResetsinglecounter
+{
+       PgStat_MsgHdr m_hdr;
+       Oid                     m_databaseid;
+       PgStat_Single_Reset_Type m_resettype;
+       Oid                     m_objectid;
+} PgStat_MsgResetsinglecounter;
+
 /* ----------
  * PgStat_MsgAutovacStart              Sent by the autovacuum daemon to signal
  *                                                             that a database is going to be processed
@@ -432,6 +452,7 @@ typedef union PgStat_Msg
        PgStat_MsgDropdb msg_dropdb;
        PgStat_MsgResetcounter msg_resetcounter;
        PgStat_MsgResetsharedcounter msg_resetsharedcounter;
+       PgStat_MsgResetsinglecounter msg_resetsinglecounter;
        PgStat_MsgAutovacStart msg_autovacuum;
        PgStat_MsgVacuum msg_vacuum;
        PgStat_MsgAnalyze msg_analyze;
@@ -654,6 +675,7 @@ extern void pgstat_drop_database(Oid databaseid);
 extern void pgstat_clear_snapshot(void);
 extern void pgstat_reset_counters(void);
 extern void pgstat_reset_shared_counters(const char *);
+extern void pgstat_reset_single_counter(Oid objectid, PgStat_Single_Reset_Type type);
 
 extern void pgstat_report_autovac(Oid dboid);
 extern void pgstat_report_vacuum(Oid tableoid, bool shared, bool adopt_counts,