@@ -135,11 +135,12 @@ char *pgstat_stat_filename = NULL;
135
135
char * pgstat_stat_tmpname = NULL ;
136
136
137
137
/*
138
- * BgWriter global statistics counters (unused in other processes) .
139
- * Stored directly in a stats message structure so it can be sent
140
- * without needing to copy things around. We assume this inits to zeroes.
138
+ * BgWriter and WAL global statistics counters.
139
+ * Stored directly in a stats message structure so they can be sent
140
+ * without needing to copy things around. We assume these init to zeroes.
141
141
*/
142
142
PgStat_MsgBgWriter BgWriterStats ;
143
+ PgStat_MsgWal WalStats ;
143
144
144
145
/*
145
146
* List of SLRU names that we keep stats for. There is no central registry of
@@ -281,6 +282,7 @@ static int localNumBackends = 0;
281
282
*/
282
283
static PgStat_ArchiverStats archiverStats ;
283
284
static PgStat_GlobalStats globalStats ;
285
+ static PgStat_WalStats walStats ;
284
286
static PgStat_SLRUStats slruStats [SLRU_NUM_ELEMENTS ];
285
287
286
288
/*
@@ -353,6 +355,7 @@ static void pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len);
353
355
static void pgstat_recv_analyze (PgStat_MsgAnalyze * msg , int len );
354
356
static void pgstat_recv_archiver (PgStat_MsgArchiver * msg , int len );
355
357
static void pgstat_recv_bgwriter (PgStat_MsgBgWriter * msg , int len );
358
+ static void pgstat_recv_wal (PgStat_MsgWal * msg , int len );
356
359
static void pgstat_recv_slru (PgStat_MsgSLRU * msg , int len );
357
360
static void pgstat_recv_funcstat (PgStat_MsgFuncstat * msg , int len );
358
361
static void pgstat_recv_funcpurge (PgStat_MsgFuncpurge * msg , int len );
@@ -938,6 +941,9 @@ pgstat_report_stat(bool force)
938
941
/* Now, send function statistics */
939
942
pgstat_send_funcstats ();
940
943
944
+ /* Send WAL statistics */
945
+ pgstat_send_wal ();
946
+
941
947
/* Finally send SLRU statistics */
942
948
pgstat_send_slru ();
943
949
}
@@ -1370,11 +1376,13 @@ pgstat_reset_shared_counters(const char *target)
1370
1376
msg .m_resettarget = RESET_ARCHIVER ;
1371
1377
else if (strcmp (target , "bgwriter" ) == 0 )
1372
1378
msg .m_resettarget = RESET_BGWRITER ;
1379
+ else if (strcmp (target , "wal" ) == 0 )
1380
+ msg .m_resettarget = RESET_WAL ;
1373
1381
else
1374
1382
ereport (ERROR ,
1375
1383
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
1376
1384
errmsg ("unrecognized reset target: \"%s\"" , target ),
1377
- errhint ("Target must be \"archiver\" or \"bgwriter \"." )));
1385
+ errhint ("Target must be \"archiver\", \"bgwriter\" or \"wal \"." )));
1378
1386
1379
1387
pgstat_setheader (& msg .m_hdr , PGSTAT_MTYPE_RESETSHAREDCOUNTER );
1380
1388
pgstat_send (& msg , sizeof (msg ));
@@ -2674,6 +2682,21 @@ pgstat_fetch_global(void)
2674
2682
return & globalStats ;
2675
2683
}
2676
2684
2685
+ /*
2686
+ * ---------
2687
+ * pgstat_fetch_stat_wal() -
2688
+ *
2689
+ * Support function for the SQL-callable pgstat* functions. Returns
2690
+ * a pointer to the WAL statistics struct.
2691
+ * ---------
2692
+ */
2693
+ PgStat_WalStats *
2694
+ pgstat_fetch_stat_wal (void )
2695
+ {
2696
+ backend_read_statsfile ();
2697
+
2698
+ return & walStats ;
2699
+ }
2677
2700
2678
2701
/*
2679
2702
* ---------
@@ -4419,6 +4442,38 @@ pgstat_send_bgwriter(void)
4419
4442
MemSet (& BgWriterStats , 0 , sizeof (BgWriterStats ));
4420
4443
}
4421
4444
4445
+ /* ----------
4446
+ * pgstat_send_wal() -
4447
+ *
4448
+ * Send WAL statistics to the collector
4449
+ * ----------
4450
+ */
4451
+ void
4452
+ pgstat_send_wal (void )
4453
+ {
4454
+ /* We assume this initializes to zeroes */
4455
+ static const PgStat_MsgWal all_zeroes ;
4456
+
4457
+ /*
4458
+ * This function can be called even if nothing at all has happened. In
4459
+ * this case, avoid sending a completely empty message to the stats
4460
+ * collector.
4461
+ */
4462
+ if (memcmp (& WalStats , & all_zeroes , sizeof (PgStat_MsgWal )) == 0 )
4463
+ return ;
4464
+
4465
+ /*
4466
+ * Prepare and send the message
4467
+ */
4468
+ pgstat_setheader (& WalStats .m_hdr , PGSTAT_MTYPE_WAL );
4469
+ pgstat_send (& WalStats , sizeof (WalStats ));
4470
+
4471
+ /*
4472
+ * Clear out the statistics buffer, so it can be re-used.
4473
+ */
4474
+ MemSet (& WalStats , 0 , sizeof (WalStats ));
4475
+ }
4476
+
4422
4477
/* ----------
4423
4478
* pgstat_send_slru() -
4424
4479
*
@@ -4658,6 +4713,10 @@ PgstatCollectorMain(int argc, char *argv[])
4658
4713
pgstat_recv_bgwriter (& msg .msg_bgwriter , len );
4659
4714
break ;
4660
4715
4716
+ case PGSTAT_MTYPE_WAL :
4717
+ pgstat_recv_wal (& msg .msg_wal , len );
4718
+ break ;
4719
+
4661
4720
case PGSTAT_MTYPE_SLRU :
4662
4721
pgstat_recv_slru (& msg .msg_slru , len );
4663
4722
break ;
@@ -4927,6 +4986,12 @@ pgstat_write_statsfiles(bool permanent, bool allDbs)
4927
4986
rc = fwrite (& archiverStats , sizeof (archiverStats ), 1 , fpout );
4928
4987
(void ) rc ; /* we'll check for error with ferror */
4929
4988
4989
+ /*
4990
+ * Write WAL stats struct
4991
+ */
4992
+ rc = fwrite (& walStats , sizeof (walStats ), 1 , fpout );
4993
+ (void ) rc ; /* we'll check for error with ferror */
4994
+
4930
4995
/*
4931
4996
* Write SLRU stats struct
4932
4997
*/
@@ -5186,11 +5251,12 @@ pgstat_read_statsfiles(Oid onlydb, bool permanent, bool deep)
5186
5251
HASH_ELEM | HASH_BLOBS | HASH_CONTEXT );
5187
5252
5188
5253
/*
5189
- * Clear out global and archiver statistics so they start from zero in
5190
- * case we can't load an existing statsfile.
5254
+ * Clear out global, archiver, WAL and SLRU statistics so they start from
5255
+ * zero in case we can't load an existing statsfile.
5191
5256
*/
5192
5257
memset (& globalStats , 0 , sizeof (globalStats ));
5193
5258
memset (& archiverStats , 0 , sizeof (archiverStats ));
5259
+ memset (& walStats , 0 , sizeof (walStats ));
5194
5260
memset (& slruStats , 0 , sizeof (slruStats ));
5195
5261
5196
5262
/*
@@ -5199,6 +5265,7 @@ pgstat_read_statsfiles(Oid onlydb, bool permanent, bool deep)
5199
5265
*/
5200
5266
globalStats .stat_reset_timestamp = GetCurrentTimestamp ();
5201
5267
archiverStats .stat_reset_timestamp = globalStats .stat_reset_timestamp ;
5268
+ walStats .stat_reset_timestamp = globalStats .stat_reset_timestamp ;
5202
5269
5203
5270
/*
5204
5271
* Set the same reset timestamp for all SLRU items too.
@@ -5268,6 +5335,17 @@ pgstat_read_statsfiles(Oid onlydb, bool permanent, bool deep)
5268
5335
goto done ;
5269
5336
}
5270
5337
5338
+ /*
5339
+ * Read WAL stats struct
5340
+ */
5341
+ if (fread (& walStats , 1 , sizeof (walStats ), fpin ) != sizeof (walStats ))
5342
+ {
5343
+ ereport (pgStatRunningInCollector ? LOG : WARNING ,
5344
+ (errmsg ("corrupted statistics file \"%s\"" , statfile )));
5345
+ memset (& walStats , 0 , sizeof (walStats ));
5346
+ goto done ;
5347
+ }
5348
+
5271
5349
/*
5272
5350
* Read SLRU stats struct
5273
5351
*/
@@ -5578,6 +5656,7 @@ pgstat_read_db_statsfile_timestamp(Oid databaseid, bool permanent,
5578
5656
PgStat_StatDBEntry dbentry ;
5579
5657
PgStat_GlobalStats myGlobalStats ;
5580
5658
PgStat_ArchiverStats myArchiverStats ;
5659
+ PgStat_WalStats myWalStats ;
5581
5660
PgStat_SLRUStats mySLRUStats [SLRU_NUM_ELEMENTS ];
5582
5661
FILE * fpin ;
5583
5662
int32 format_id ;
@@ -5633,6 +5712,17 @@ pgstat_read_db_statsfile_timestamp(Oid databaseid, bool permanent,
5633
5712
return false;
5634
5713
}
5635
5714
5715
+ /*
5716
+ * Read WAL stats struct
5717
+ */
5718
+ if (fread (& myWalStats , 1 , sizeof (myWalStats ), fpin ) != sizeof (myWalStats ))
5719
+ {
5720
+ ereport (pgStatRunningInCollector ? LOG : WARNING ,
5721
+ (errmsg ("corrupted statistics file \"%s\"" , statfile )));
5722
+ FreeFile (fpin );
5723
+ return false;
5724
+ }
5725
+
5636
5726
/*
5637
5727
* Read SLRU stats struct
5638
5728
*/
@@ -6213,6 +6303,12 @@ pgstat_recv_resetsharedcounter(PgStat_MsgResetsharedcounter *msg, int len)
6213
6303
memset (& archiverStats , 0 , sizeof (archiverStats ));
6214
6304
archiverStats .stat_reset_timestamp = GetCurrentTimestamp ();
6215
6305
}
6306
+ else if (msg -> m_resettarget == RESET_WAL )
6307
+ {
6308
+ /* Reset the WAL statistics for the cluster. */
6309
+ memset (& walStats , 0 , sizeof (walStats ));
6310
+ walStats .stat_reset_timestamp = GetCurrentTimestamp ();
6311
+ }
6216
6312
6217
6313
/*
6218
6314
* Presumably the sender of this message validated the target, don't
@@ -6427,6 +6523,18 @@ pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len)
6427
6523
globalStats .buf_alloc += msg -> m_buf_alloc ;
6428
6524
}
6429
6525
6526
+ /* ----------
6527
+ * pgstat_recv_wal() -
6528
+ *
6529
+ * Process a WAL message.
6530
+ * ----------
6531
+ */
6532
+ static void
6533
+ pgstat_recv_wal (PgStat_MsgWal * msg , int len )
6534
+ {
6535
+ walStats .wal_buffers_full += msg -> m_wal_buffers_full ;
6536
+ }
6537
+
6430
6538
/* ----------
6431
6539
* pgstat_recv_slru() -
6432
6540
*
0 commit comments