Skip to content

Commit 083e1b0

Browse files
committed
Add functions to reset the statistics counter for a single table/index or
a single function.
1 parent 21d3ae0 commit 083e1b0

File tree

6 files changed

+131
-7
lines changed

6 files changed

+131
-7
lines changed

doc/src/sgml/monitoring.sgml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.74 2010/01/19 14:11:30 mha Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.75 2010/01/28 14:25:41 mha Exp $ -->
22

33
<chapter id="monitoring">
44
<title>Monitoring Database Activity</title>
@@ -929,6 +929,24 @@ postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <re
929929
<structname>pg_stat_bgwriter</>.
930930
</entry>
931931
</row>
932+
933+
<row>
934+
<entry><literal><function>pg_stat_reset_single_table_counters</function>(oid)</literal></entry>
935+
<entry><type>void</type></entry>
936+
<entry>
937+
Reset statistics for a single table or index in the current database to
938+
zero (requires superuser privileges)
939+
</entry>
940+
</row>
941+
942+
<row>
943+
<entry><literal><function>pg_stat_reset_single_function_counters</function>(oid)</literal></entry>
944+
<entry><type>void</type></entry>
945+
<entry>
946+
Reset statistics for a single function in the current database to
947+
zero (requires superuser privileges)
948+
</entry>
949+
</row>
932950
</tbody>
933951
</tgroup>
934952
</table>

src/backend/postmaster/pgstat.c

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* Copyright (c) 2001-2010, PostgreSQL Global Development Group
1515
*
16-
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.198 2010/01/19 14:11:30 mha Exp $
16+
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.199 2010/01/28 14:25:41 mha Exp $
1717
* ----------
1818
*/
1919
#include "postgres.h"
@@ -271,6 +271,7 @@ static void pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len);
271271
static void pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len);
272272
static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
273273
static void pgstat_recv_resetsharedcounter(PgStat_MsgResetsharedcounter *msg, int len);
274+
static void pgstat_recv_resetsinglecounter(PgStat_MsgResetsinglecounter *msg, int len);
274275
static void pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len);
275276
static void pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len);
276277
static void pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len);
@@ -1187,6 +1188,32 @@ pgstat_reset_shared_counters(const char *target)
11871188
pgstat_send(&msg, sizeof(msg));
11881189
}
11891190

1191+
/* ----------
1192+
* pgstat_reset_single_counter() -
1193+
*
1194+
* Tell the statistics collector to reset a single counter.
1195+
* ----------
1196+
*/
1197+
void pgstat_reset_single_counter(Oid objoid, PgStat_Single_Reset_Type type)
1198+
{
1199+
PgStat_MsgResetsinglecounter msg;
1200+
1201+
if (pgStatSock < 0)
1202+
return;
1203+
1204+
if (!superuser())
1205+
ereport(ERROR,
1206+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1207+
errmsg("must be superuser to reset statistics counters")));
1208+
1209+
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETSINGLECOUNTER);
1210+
msg.m_databaseid = MyDatabaseId;
1211+
msg.m_resettype = type;
1212+
msg.m_objectid = objoid;
1213+
1214+
pgstat_send(&msg, sizeof(msg));
1215+
}
1216+
11901217
/* ----------
11911218
* pgstat_report_autovac() -
11921219
*
@@ -2954,6 +2981,12 @@ PgstatCollectorMain(int argc, char *argv[])
29542981
len);
29552982
break;
29562983

2984+
case PGSTAT_MTYPE_RESETSINGLECOUNTER:
2985+
pgstat_recv_resetsinglecounter(
2986+
(PgStat_MsgResetsinglecounter *) &msg,
2987+
len);
2988+
break;
2989+
29572990
case PGSTAT_MTYPE_AUTOVAC_START:
29582991
pgstat_recv_autovac((PgStat_MsgAutovacStart *) &msg, len);
29592992
break;
@@ -3928,6 +3961,30 @@ pgstat_recv_resetsharedcounter(PgStat_MsgResetsharedcounter *msg, int len)
39283961
*/
39293962
}
39303963

3964+
/* ----------
3965+
* pgstat_recv_resetsinglecounter() -
3966+
*
3967+
* Reset a statistics for a single object
3968+
* ----------
3969+
*/
3970+
static void
3971+
pgstat_recv_resetsinglecounter(PgStat_MsgResetsinglecounter *msg, int len)
3972+
{
3973+
PgStat_StatDBEntry *dbentry;
3974+
3975+
dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3976+
3977+
if (!dbentry)
3978+
return;
3979+
3980+
3981+
/* Remove object if it exists, ignore it if not */
3982+
if (msg->m_resettype == RESET_TABLE)
3983+
(void) hash_search(dbentry->tables, (void *) &(msg->m_objectid), HASH_REMOVE, NULL);
3984+
else if (msg->m_resettype == RESET_FUNCTION)
3985+
(void) hash_search(dbentry->functions, (void *)&(msg->m_objectid), HASH_REMOVE, NULL);
3986+
}
3987+
39313988
/* ----------
39323989
* pgstat_recv_autovac() -
39333990
*

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.58 2010/01/19 14:11:31 mha Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.59 2010/01/28 14:25:41 mha Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -79,6 +79,8 @@ extern Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS);
7979
extern Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS);
8080
extern Datum pg_stat_reset(PG_FUNCTION_ARGS);
8181
extern Datum pg_stat_reset_shared(PG_FUNCTION_ARGS);
82+
extern Datum pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS);
83+
extern Datum pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS);
8284

8385
/* Global bgwriter statistics, from bgwriter.c */
8486
extern PgStat_MsgBgWriter bgwriterStats;
@@ -1120,3 +1122,24 @@ pg_stat_reset_shared(PG_FUNCTION_ARGS)
11201122

11211123
PG_RETURN_VOID();
11221124
}
1125+
1126+
/* Reset a a single counter in the current database */
1127+
Datum
1128+
pg_stat_reset_single_table_counters(PG_FUNCTION_ARGS)
1129+
{
1130+
Oid taboid = PG_GETARG_OID(0);
1131+
1132+
pgstat_reset_single_counter(taboid, RESET_TABLE);
1133+
1134+
PG_RETURN_VOID();
1135+
}
1136+
1137+
Datum
1138+
pg_stat_reset_single_function_counters(PG_FUNCTION_ARGS)
1139+
{
1140+
Oid funcoid = PG_GETARG_OID(0);
1141+
1142+
pgstat_reset_single_counter(funcoid, RESET_FUNCTION);
1143+
1144+
PG_RETURN_VOID();
1145+
}

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.579 2010/01/25 20:55:32 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.580 2010/01/28 14:25:41 mha Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201001251
56+
#define CATALOG_VERSION_NO 201001281
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.565 2010/01/25 20:55:32 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.566 2010/01/28 14:25:41 mha Exp $
1111
*
1212
* NOTES
1313
* 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
30873087
DESCR("statistics: reset collected statistics for current database");
30883088
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_ ));
30893089
DESCR("statistics: reset collected statistics shared across the cluster");
3090+
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_ ));
3091+
DESCR("statistics: reset collected statistics for a single table or index in the current database");
3092+
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_ ));
3093+
DESCR("statistics: reset collected statistics for a single function in the current database");
30903094

30913095
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_ ));
30923096
DESCR("convert bytea value into some ascii-only text string");

src/include/pgstat.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 2001-2010, PostgreSQL Global Development Group
77
*
8-
* $PostgreSQL: pgsql/src/include/pgstat.h,v 1.87 2010/01/19 14:11:31 mha Exp $
8+
* $PostgreSQL: pgsql/src/include/pgstat.h,v 1.88 2010/01/28 14:25:41 mha Exp $
99
* ----------
1010
*/
1111
#ifndef PGSTAT_H
@@ -39,6 +39,7 @@ typedef enum StatMsgType
3939
PGSTAT_MTYPE_DROPDB,
4040
PGSTAT_MTYPE_RESETCOUNTER,
4141
PGSTAT_MTYPE_RESETSHAREDCOUNTER,
42+
PGSTAT_MTYPE_RESETSINGLECOUNTER,
4243
PGSTAT_MTYPE_AUTOVAC_START,
4344
PGSTAT_MTYPE_VACUUM,
4445
PGSTAT_MTYPE_ANALYZE,
@@ -100,6 +101,12 @@ typedef enum PgStat_Shared_Reset_Target
100101
RESET_BGWRITER
101102
} PgStat_Shared_Reset_Target;
102103

104+
/* Possible object types for resetting single counters */
105+
typedef enum PgStat_Single_Reset_Type
106+
{
107+
RESET_TABLE,
108+
RESET_FUNCTION
109+
} PgStat_Single_Reset_Type;
103110

104111
/* ------------------------------------------------------------
105112
* Structures kept in backend local memory while accumulating counts
@@ -278,6 +285,19 @@ typedef struct PgStat_MsgResetsharedcounter
278285
PgStat_Shared_Reset_Target m_resettarget;
279286
} PgStat_MsgResetsharedcounter;
280287

288+
/* ----------
289+
* PgStat_MsgResetsinglecounter Sent by the backend to tell the collector
290+
* to reset a single counter
291+
* ----------
292+
*/
293+
typedef struct PgStat_MsgResetsinglecounter
294+
{
295+
PgStat_MsgHdr m_hdr;
296+
Oid m_databaseid;
297+
PgStat_Single_Reset_Type m_resettype;
298+
Oid m_objectid;
299+
} PgStat_MsgResetsinglecounter;
300+
281301
/* ----------
282302
* PgStat_MsgAutovacStart Sent by the autovacuum daemon to signal
283303
* that a database is going to be processed
@@ -432,6 +452,7 @@ typedef union PgStat_Msg
432452
PgStat_MsgDropdb msg_dropdb;
433453
PgStat_MsgResetcounter msg_resetcounter;
434454
PgStat_MsgResetsharedcounter msg_resetsharedcounter;
455+
PgStat_MsgResetsinglecounter msg_resetsinglecounter;
435456
PgStat_MsgAutovacStart msg_autovacuum;
436457
PgStat_MsgVacuum msg_vacuum;
437458
PgStat_MsgAnalyze msg_analyze;
@@ -654,6 +675,7 @@ extern void pgstat_drop_database(Oid databaseid);
654675
extern void pgstat_clear_snapshot(void);
655676
extern void pgstat_reset_counters(void);
656677
extern void pgstat_reset_shared_counters(const char *);
678+
extern void pgstat_reset_single_counter(Oid objectid, PgStat_Single_Reset_Type type);
657679

658680
extern void pgstat_report_autovac(Oid dboid);
659681
extern void pgstat_report_vacuum(Oid tableoid, bool shared, bool adopt_counts,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy