Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 335feca

Browse files
committed
Add some instrumentation to the bgwriter, through the stats collector.
New view pg_stat_bgwriter, and the functions required to build it.
1 parent f9ce21f commit 335feca

File tree

10 files changed

+349
-12
lines changed

10 files changed

+349
-12
lines changed

doc/src/sgml/monitoring.sgml

Lines changed: 76 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.47 2007/03/16 17:57:35 mha Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/monitoring.sgml,v 1.48 2007/03/30 18:34:55 mha Exp $ -->
22

33
<chapter id="monitoring">
44
<title>Monitoring Database Activity</title>
@@ -260,6 +260,16 @@ postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <re
260260
</entry>
261261
</row>
262262

263+
<row>
264+
<entry><structname>pg_stat_bgwriter</></entry>
265+
<entry>One row only, showing cluster-wide statistics from the
266+
background writer: number of scheduled checkpoints, requested
267+
checkpoints, buffers written by checkpoints, lru-scans and all-scans,
268+
and the number of times the bgwriter aborted a round because it had
269+
written too many buffers during lru-scans and all-scans.
270+
</entry>
271+
</row>
272+
263273
<row>
264274
<entry><structname>pg_stat_database</></entry>
265275
<entry>One row per database, showing database OID, database name,
@@ -751,6 +761,71 @@ postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <re
751761
</entry>
752762
</row>
753763

764+
<row>
765+
<entry><literal><function>pg_stat_get_bgwriter_timed_checkpoints</function>()</literal></entry>
766+
<entry><type>bigint</type></entry>
767+
<entry>
768+
The number of times the bgwriter has started timed checkpoints
769+
(because the <varname>checkpoint_timeout</varname> time has expired).
770+
</entry>
771+
</row>
772+
773+
<row>
774+
<entry><literal><function>pg_stat_get_bgwriter_requested_checkpoints</function>()</literal></entry>
775+
<entry><type>bigint</type></entry>
776+
<entry>
777+
The number of times the bgwriter has started checkpoints based on
778+
requests from backends because the <varname>checkpoint_segments</varname>
779+
has been exceeded or because the CHECKPOINT command has been issued.
780+
</entry>
781+
</row>
782+
783+
<row>
784+
<entry><literal><function>pg_stat_get_bgwriter_buf_written_checkpoints</function>()</literal></entry>
785+
<entry><type>bigint</type></entry>
786+
<entry>
787+
The number of buffers written by the bgwriter during checkpoints.
788+
</entry>
789+
</row>
790+
791+
<row>
792+
<entry><literal><function>pg_stat_get_bgwriter_buf_written_lru</function>()</literal></entry>
793+
<entry><type>bigint</type></entry>
794+
<entry>
795+
The number of buffers written by the bgwriter when performing a
796+
LRU scan of the buffer cache.
797+
</entry>
798+
</row>
799+
800+
<row>
801+
<entry><literal><function>pg_stat_get_bgwriter_buf_written_all</function>()</literal></entry>
802+
<entry><type>bigint</type></entry>
803+
<entry>
804+
The number of buffers written by the bgwriter when performing a
805+
scan of all the buffer cache.
806+
</entry>
807+
</row>
808+
809+
<row>
810+
<entry><literal><function>pg_stat_get_bgwriter_maxwritten_lru</function>()</literal></entry>
811+
<entry><type>bigint</type></entry>
812+
<entry>
813+
The number of times the bgwriter has stopped its LRU round because
814+
it has written more buffers than specified in the
815+
<varname>bgwriter_lru_maxpages</varname> parameter.
816+
</entry>
817+
</row>
818+
819+
<row>
820+
<entry><literal><function>pg_stat_get_bgwriter_maxwritten_all</function>()</literal></entry>
821+
<entry><type>bigint</type></entry>
822+
<entry>
823+
The number of times the bgwriter has stopped its all-buffer round
824+
because it has written more buffers than specified in the
825+
<varname>bgwriter_all_maxpages</varname> parameter.
826+
</entry>
827+
</row>
828+
754829
<row>
755830
<entry><literal><function>pg_stat_clear_snapshot</function>()</literal></entry>
756831
<entry><type>void</type></entry>

src/backend/catalog/system_views.sql

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 1996-2007, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/backend/catalog/system_views.sql,v 1.36 2007/03/16 17:57:36 mha Exp $
6+
* $PostgreSQL: pgsql/src/backend/catalog/system_views.sql,v 1.37 2007/03/30 18:34:55 mha Exp $
77
*/
88

99
CREATE VIEW pg_roles AS
@@ -364,3 +364,13 @@ CREATE VIEW pg_stat_database AS
364364
pg_stat_get_db_tuples_updated(D.oid) AS tup_updated,
365365
pg_stat_get_db_tuples_deleted(D.oid) AS tup_deleted
366366
FROM pg_database D;
367+
368+
CREATE VIEW pg_stat_bgwriter AS
369+
SELECT
370+
pg_stat_get_bgwriter_timed_checkpoints() AS checkpoints_timed,
371+
pg_stat_get_bgwriter_requested_checkpoints() AS checkpoints_req,
372+
pg_stat_get_bgwriter_buf_written_checkpoints() AS buffers_checkpoint,
373+
pg_stat_get_bgwriter_buf_written_lru() AS buffers_lru,
374+
pg_stat_get_bgwriter_buf_written_all() AS buffers_all,
375+
pg_stat_get_bgwriter_maxwritten_lru() AS maxwritten_lru,
376+
pg_stat_get_bgwriter_maxwritten_all() AS maxwritten_all;

src/backend/postmaster/bgwriter.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.36 2007/01/17 16:25:01 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.37 2007/03/30 18:34:55 mha Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -50,6 +50,7 @@
5050
#include "access/xlog_internal.h"
5151
#include "libpq/pqsignal.h"
5252
#include "miscadmin.h"
53+
#include "pgstat.h"
5354
#include "postmaster/bgwriter.h"
5455
#include "storage/fd.h"
5556
#include "storage/freespace.h"
@@ -124,6 +125,13 @@ typedef struct
124125

125126
static BgWriterShmemStruct *BgWriterShmem;
126127

128+
/*
129+
* BgWriter statistics counters.
130+
* Stored directly in a stats message structure so it can be sent
131+
* without needing to copy things around.
132+
*/
133+
PgStat_MsgBgWriter BgWriterStats;
134+
127135
/*
128136
* GUC parameters
129137
*/
@@ -242,6 +250,11 @@ BackgroundWriterMain(void)
242250
ALLOCSET_DEFAULT_MAXSIZE);
243251
MemoryContextSwitchTo(bgwriter_context);
244252

253+
/*
254+
* Initialize statistics counters to zero
255+
*/
256+
memset(&BgWriterStats, 0, sizeof(BgWriterStats));
257+
245258
/*
246259
* If an exception is encountered, processing resumes here.
247260
*
@@ -354,6 +367,7 @@ BackgroundWriterMain(void)
354367
checkpoint_requested = false;
355368
do_checkpoint = true;
356369
force_checkpoint = true;
370+
BgWriterStats.m_requested_checkpoints++;
357371
}
358372
if (shutdown_requested)
359373
{
@@ -376,7 +390,11 @@ BackgroundWriterMain(void)
376390
now = time(NULL);
377391
elapsed_secs = now - last_checkpoint_time;
378392
if (elapsed_secs >= CheckPointTimeout)
393+
{
379394
do_checkpoint = true;
395+
if (!force_checkpoint)
396+
BgWriterStats.m_timed_checkpoints++;
397+
}
380398

381399
/*
382400
* Do a checkpoint if requested, otherwise do one cycle of
@@ -473,6 +491,11 @@ BackgroundWriterMain(void)
473491
}
474492
}
475493

494+
/*
495+
* Send off activity statistics to the stats collector
496+
*/
497+
pgstat_send_bgwriter();
498+
476499
/*
477500
* Nap for the configured time, or sleep for 10 seconds if there is no
478501
* bgwriter activity configured.

src/backend/postmaster/pgstat.c

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* Copyright (c) 2001-2007, PostgreSQL Global Development Group
1515
*
16-
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.151 2007/03/28 22:17:12 alvherre Exp $
16+
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.152 2007/03/30 18:34:55 mha Exp $
1717
* ----------
1818
*/
1919
#include "postgres.h"
@@ -135,6 +135,18 @@ static HTAB *pgStatDBHash = NULL;
135135
static PgBackendStatus *localBackendStatusTable = NULL;
136136
static int localNumBackends = 0;
137137

138+
/*
139+
* BgWriter global statistics counters, from bgwriter.c
140+
*/
141+
extern PgStat_MsgBgWriter BgWriterStats;
142+
143+
/*
144+
* Cluster wide statistics, kept in the stats collector.
145+
* Contains statistics that are not collected per database
146+
* or per table.
147+
*/
148+
static PgStat_GlobalStats globalStats;
149+
138150
static volatile bool need_exit = false;
139151
static volatile bool need_statwrite = false;
140152

@@ -171,6 +183,7 @@ static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
171183
static void pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len);
172184
static void pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len);
173185
static void pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len);
186+
static void pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len);
174187

175188

176189
/* ------------------------------------------------------------
@@ -1288,6 +1301,22 @@ pgstat_fetch_stat_numbackends(void)
12881301
return localNumBackends;
12891302
}
12901303

1304+
/*
1305+
* ---------
1306+
* pgstat_fetch_global() -
1307+
*
1308+
* Support function for the SQL-callable pgstat* functions. Returns
1309+
* a pointer to the global statistics struct.
1310+
* ---------
1311+
*/
1312+
PgStat_GlobalStats *
1313+
pgstat_fetch_global(void)
1314+
{
1315+
backend_read_statsfile();
1316+
1317+
return &globalStats;
1318+
}
1319+
12911320

12921321
/* ------------------------------------------------------------
12931322
* Functions for management of the shared-memory PgBackendStatus array
@@ -1646,6 +1675,42 @@ pgstat_send(void *msg, int len)
16461675
#endif
16471676
}
16481677

1678+
/* ----------
1679+
* pgstat_send_bgwriter() -
1680+
*
1681+
* Send bgwriter statistics to the collector
1682+
* ----------
1683+
*/
1684+
void
1685+
pgstat_send_bgwriter(void)
1686+
{
1687+
/*
1688+
* This function can be called even if nothing at all has happened.
1689+
* In this case, avoid sending a completely empty message to
1690+
* the stats collector.
1691+
*/
1692+
if (BgWriterStats.m_timed_checkpoints == 0 &&
1693+
BgWriterStats.m_requested_checkpoints == 0 &&
1694+
BgWriterStats.m_buf_written_checkpoints == 0 &&
1695+
BgWriterStats.m_buf_written_lru == 0 &&
1696+
BgWriterStats.m_buf_written_all == 0 &&
1697+
BgWriterStats.m_maxwritten_lru == 0 &&
1698+
BgWriterStats.m_maxwritten_all == 0)
1699+
return;
1700+
1701+
/*
1702+
* Prepare and send the message
1703+
*/
1704+
pgstat_setheader(&BgWriterStats.m_hdr, PGSTAT_MTYPE_BGWRITER);
1705+
pgstat_send(&BgWriterStats, sizeof(BgWriterStats));
1706+
1707+
/*
1708+
* Clear out the bgwriter statistics buffer, so it can be
1709+
* re-used.
1710+
*/
1711+
memset(&BgWriterStats, 0, sizeof(BgWriterStats));
1712+
}
1713+
16491714

16501715
/* ----------
16511716
* PgstatCollectorMain() -
@@ -1892,6 +1957,10 @@ PgstatCollectorMain(int argc, char *argv[])
18921957
pgstat_recv_analyze((PgStat_MsgAnalyze *) &msg, len);
18931958
break;
18941959

1960+
case PGSTAT_MTYPE_BGWRITER:
1961+
pgstat_recv_bgwriter((PgStat_MsgBgWriter *) &msg, len);
1962+
break;
1963+
18951964
default:
18961965
break;
18971966
}
@@ -2030,6 +2099,11 @@ pgstat_write_statsfile(void)
20302099
format_id = PGSTAT_FILE_FORMAT_ID;
20312100
fwrite(&format_id, sizeof(format_id), 1, fpout);
20322101

2102+
/*
2103+
* Write global stats struct
2104+
*/
2105+
fwrite(&globalStats, sizeof(globalStats), 1, fpout);
2106+
20332107
/*
20342108
* Walk through the database table.
20352109
*/
@@ -2132,6 +2206,12 @@ pgstat_read_statsfile(Oid onlydb)
21322206
dbhash = hash_create("Databases hash", PGSTAT_DB_HASH_SIZE, &hash_ctl,
21332207
HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
21342208

2209+
/*
2210+
* Clear out global statistics so they start from zero in case we can't
2211+
* load an existing statsfile.
2212+
*/
2213+
memset(&globalStats, 0, sizeof(globalStats));
2214+
21352215
/*
21362216
* Try to open the status file. If it doesn't exist, the backends simply
21372217
* return zero for anything and the collector simply starts from scratch
@@ -2151,6 +2231,16 @@ pgstat_read_statsfile(Oid onlydb)
21512231
goto done;
21522232
}
21532233

2234+
/*
2235+
* Read global stats struct
2236+
*/
2237+
if (fread(&globalStats, 1, sizeof(globalStats), fpin) != sizeof(globalStats))
2238+
{
2239+
ereport(pgStatRunningInCollector ? LOG : WARNING,
2240+
(errmsg("corrupted pgstat.stat file")));
2241+
goto done;
2242+
}
2243+
21542244
/*
21552245
* We found an existing collector stats file. Read it and put all the
21562246
* hashtable entries into place.
@@ -2656,3 +2746,22 @@ pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len)
26562746
tabentry->n_dead_tuples = msg->m_dead_tuples;
26572747
tabentry->last_anl_tuples = msg->m_live_tuples + msg->m_dead_tuples;
26582748
}
2749+
2750+
2751+
/* ----------
2752+
* pgstat_recv_bgwriter() -
2753+
*
2754+
* Process a BGWRITER message.
2755+
* ----------
2756+
*/
2757+
static void
2758+
pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len)
2759+
{
2760+
globalStats.timed_checkpoints += msg->m_timed_checkpoints;
2761+
globalStats.requested_checkpoints += msg->m_requested_checkpoints;
2762+
globalStats.buf_written_checkpoints += msg->m_buf_written_checkpoints;
2763+
globalStats.buf_written_lru += msg->m_buf_written_lru;
2764+
globalStats.buf_written_all += msg->m_buf_written_all;
2765+
globalStats.maxwritten_lru += msg->m_maxwritten_lru;
2766+
globalStats.maxwritten_all += msg->m_maxwritten_all;
2767+
}

0 commit comments

Comments
 (0)