Skip to content

Commit 7e40cdc

Browse files
committed
Add pg_stat_reset_shared('bgwriter') to reset the cluster-wide shared
statistics of the bgwriter. Greg Smith
1 parent 4f15699 commit 7e40cdc

File tree

6 files changed

+111
-7
lines changed

6 files changed

+111
-7
lines changed

doc/src/sgml/monitoring.sgml

Lines changed: 12 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.73 2009/11/29 18:14:30 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.74 2010/01/19 14:11:30 mha Exp $ -->
22

33
<chapter id="monitoring">
44
<title>Monitoring Database Activity</title>
@@ -918,6 +918,17 @@ postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <re
918918
(requires superuser privileges)
919919
</entry>
920920
</row>
921+
922+
<row>
923+
<entry><literal><function>pg_stat_reset_shared</function>(text)</literal></entry>
924+
<entry><type>void</type></entry>
925+
<entry>
926+
Reset some of the shared statistics counters for the database cluster to
927+
zero (requires superuser privileges). Calling
928+
<literal>pg_stat_reset_shared('bgwriter')</> will zero all the values shown by
929+
<structname>pg_stat_bgwriter</>.
930+
</entry>
931+
</row>
921932
</tbody>
922933
</tgroup>
923934
</table>

src/backend/postmaster/pgstat.c

Lines changed: 61 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.197 2010/01/10 14:16:07 mha Exp $
16+
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.198 2010/01/19 14:11:30 mha Exp $
1717
* ----------
1818
*/
1919
#include "postgres.h"
@@ -270,6 +270,7 @@ static void pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len);
270270
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);
273+
static void pgstat_recv_resetsharedcounter(PgStat_MsgResetsharedcounter *msg, int len);
273274
static void pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len);
274275
static void pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len);
275276
static void pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len);
@@ -1153,6 +1154,38 @@ pgstat_reset_counters(void)
11531154
pgstat_send(&msg, sizeof(msg));
11541155
}
11551156

1157+
/* ----------
1158+
* pgstat_reset_shared_counters() -
1159+
*
1160+
* Tell the statistics collector to reset cluster-wide shared counters.
1161+
* ----------
1162+
*/
1163+
void
1164+
pgstat_reset_shared_counters(const char *target)
1165+
{
1166+
PgStat_MsgResetsharedcounter msg;
1167+
1168+
if (pgStatSock < 0)
1169+
return;
1170+
1171+
if (!superuser())
1172+
ereport(ERROR,
1173+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1174+
errmsg("must be superuser to reset statistics counters")));
1175+
1176+
if (strcmp(target, "bgwriter") == 0)
1177+
msg.m_resettarget = RESET_BGWRITER;
1178+
else
1179+
{
1180+
ereport(ERROR,
1181+
(errcode(ERRCODE_SYNTAX_ERROR),
1182+
errmsg("unrecognized reset target: '%s'", target),
1183+
errhint("allowed targets are 'bgwriter'.")));
1184+
}
1185+
1186+
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETSHAREDCOUNTER);
1187+
pgstat_send(&msg, sizeof(msg));
1188+
}
11561189

11571190
/* ----------
11581191
* pgstat_report_autovac() -
@@ -2915,6 +2948,12 @@ PgstatCollectorMain(int argc, char *argv[])
29152948
len);
29162949
break;
29172950

2951+
case PGSTAT_MTYPE_RESETSHAREDCOUNTER:
2952+
pgstat_recv_resetsharedcounter(
2953+
(PgStat_MsgResetsharedcounter *) &msg,
2954+
len);
2955+
break;
2956+
29182957
case PGSTAT_MTYPE_AUTOVAC_START:
29192958
pgstat_recv_autovac((PgStat_MsgAutovacStart *) &msg, len);
29202959
break;
@@ -3868,6 +3907,27 @@ pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len)
38683907
HASH_ELEM | HASH_FUNCTION);
38693908
}
38703909

3910+
/* ----------
3911+
* pgstat_recv_resetshared() -
3912+
*
3913+
* Reset some shared statistics of the cluster.
3914+
* ----------
3915+
*/
3916+
static void
3917+
pgstat_recv_resetsharedcounter(PgStat_MsgResetsharedcounter *msg, int len)
3918+
{
3919+
if (msg->m_resettarget==RESET_BGWRITER)
3920+
{
3921+
/* Reset the global background writer statistics for the cluster. */
3922+
memset(&globalStats, 0, sizeof(globalStats));
3923+
}
3924+
3925+
/*
3926+
* Presumably the sender of this message validated the target, don't
3927+
* complain here if it's not valid
3928+
*/
3929+
}
3930+
38713931
/* ----------
38723932
* pgstat_recv_autovac() -
38733933
*

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 13 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.57 2010/01/02 16:57:54 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.58 2010/01/19 14:11:31 mha Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -78,6 +78,7 @@ extern Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS);
7878

7979
extern Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS);
8080
extern Datum pg_stat_reset(PG_FUNCTION_ARGS);
81+
extern Datum pg_stat_reset_shared(PG_FUNCTION_ARGS);
8182

8283
/* Global bgwriter statistics, from bgwriter.c */
8384
extern PgStat_MsgBgWriter bgwriterStats;
@@ -1108,3 +1109,14 @@ pg_stat_reset(PG_FUNCTION_ARGS)
11081109

11091110
PG_RETURN_VOID();
11101111
}
1112+
1113+
/* Reset some shared cluster-wide counters */
1114+
Datum
1115+
pg_stat_reset_shared(PG_FUNCTION_ARGS)
1116+
{
1117+
char *target = text_to_cstring(PG_GETARG_TEXT_PP(0));
1118+
1119+
pgstat_reset_shared_counters(target);
1120+
1121+
PG_RETURN_VOID();
1122+
}

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.575 2010/01/19 05:50:18 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.576 2010/01/19 14:11:32 mha Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201001181
56+
#define CATALOG_VERSION_NO 201001191
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 3 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.563 2010/01/19 05:50:18 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.564 2010/01/19 14:11:32 mha Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.pl reads this file and generates .bki
@@ -3073,6 +3073,8 @@ DATA(insert OID = 2230 ( pg_stat_clear_snapshot PGNSP PGUID 12 1 0 0 f f f f f
30733073
DESCR("statistics: discard current transaction's statistics snapshot");
30743074
DATA(insert OID = 2274 ( pg_stat_reset PGNSP PGUID 12 1 0 0 f f f f f v 0 0 2278 "" _null_ _null_ _null_ _null_ pg_stat_reset _null_ _null_ _null_ ));
30753075
DESCR("statistics: reset collected statistics for current database");
3076+
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_ ));
3077+
DESCR("statistics: reset collected statistics shared across the cluster");
30763078

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

src/include/pgstat.h

Lines changed: 20 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.86 2010/01/02 16:58:00 momjian Exp $
8+
* $PostgreSQL: pgsql/src/include/pgstat.h,v 1.87 2010/01/19 14:11:31 mha Exp $
99
* ----------
1010
*/
1111
#ifndef PGSTAT_H
@@ -38,6 +38,7 @@ typedef enum StatMsgType
3838
PGSTAT_MTYPE_TABPURGE,
3939
PGSTAT_MTYPE_DROPDB,
4040
PGSTAT_MTYPE_RESETCOUNTER,
41+
PGSTAT_MTYPE_RESETSHAREDCOUNTER,
4142
PGSTAT_MTYPE_AUTOVAC_START,
4243
PGSTAT_MTYPE_VACUUM,
4344
PGSTAT_MTYPE_ANALYZE,
@@ -93,6 +94,12 @@ typedef struct PgStat_TableCounts
9394
PgStat_Counter t_blocks_hit;
9495
} PgStat_TableCounts;
9596

97+
/* Possible targets for resetting cluster-wide shared values */
98+
typedef enum PgStat_Shared_Reset_Target
99+
{
100+
RESET_BGWRITER
101+
} PgStat_Shared_Reset_Target;
102+
96103

97104
/* ------------------------------------------------------------
98105
* Structures kept in backend local memory while accumulating counts
@@ -260,6 +267,16 @@ typedef struct PgStat_MsgResetcounter
260267
Oid m_databaseid;
261268
} PgStat_MsgResetcounter;
262269

270+
/* ----------
271+
* PgStat_MsgResetsharedcounter Sent by the backend to tell the collector
272+
* to reset a shared counter
273+
* ----------
274+
*/
275+
typedef struct PgStat_MsgResetsharedcounter
276+
{
277+
PgStat_MsgHdr m_hdr;
278+
PgStat_Shared_Reset_Target m_resettarget;
279+
} PgStat_MsgResetsharedcounter;
263280

264281
/* ----------
265282
* PgStat_MsgAutovacStart Sent by the autovacuum daemon to signal
@@ -414,6 +431,7 @@ typedef union PgStat_Msg
414431
PgStat_MsgTabpurge msg_tabpurge;
415432
PgStat_MsgDropdb msg_dropdb;
416433
PgStat_MsgResetcounter msg_resetcounter;
434+
PgStat_MsgResetsharedcounter msg_resetsharedcounter;
417435
PgStat_MsgAutovacStart msg_autovacuum;
418436
PgStat_MsgVacuum msg_vacuum;
419437
PgStat_MsgAnalyze msg_analyze;
@@ -635,6 +653,7 @@ extern void pgstat_drop_database(Oid databaseid);
635653

636654
extern void pgstat_clear_snapshot(void);
637655
extern void pgstat_reset_counters(void);
656+
extern void pgstat_reset_shared_counters(const char *);
638657

639658
extern void pgstat_report_autovac(Oid dboid);
640659
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