Skip to content

Commit 1361959

Browse files
committed
pgstat: split different types of stats into separate files.
pgstat.c is very long, and it's hard to find an order that makes sense and is likely to be maintained over time. Splitting the different pieces into separate files makes that a lot easier. With a few exceptions, this commit just moves code around. Those exceptions are: - adding file headers for new files - removing 'static' from functions - adapting pgstat_assert_is_up() to work across TUs - minor comment adjustments git diff --color-moved=dimmed-zebra is very helpful separating code movement from code changes. The next commit in this series will reorder pgstat.[ch] contents to be a bit more coherent. Earlier revisions of this patch had "global" statistics (archiver, bgwriter, checkpointer, replication slots, SLRU, WAL) in one file, because each seemed small enough. However later commits will increase their size and their aggregate size is not insubstantial. It also just seems easier to split each type of statistic into its own file. Author: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/20220303021600.hs34ghqcw6zcokdh@alap3.anarazel.de
1 parent cb02fcb commit 1361959

13 files changed

+2408
-2040
lines changed

src/backend/postmaster/pgstat.c

+73-2,040
Large diffs are not rendered by default.

src/backend/utils/activity/Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ include $(top_builddir)/src/Makefile.global
1616
OBJS = \
1717
backend_progress.o \
1818
backend_status.o \
19+
pgstat_archiver.o \
20+
pgstat_bgwriter.o \
21+
pgstat_checkpointer.o \
22+
pgstat_database.o \
23+
pgstat_function.o \
24+
pgstat_relation.o \
25+
pgstat_replslot.o \
26+
pgstat_subscription.o \
27+
pgstat_wal.o \
28+
pgstat_slru.o \
1929
wait_event.o
2030

2131
include $(top_srcdir)/src/backend/common.mk
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* -------------------------------------------------------------------------
2+
*
3+
* pgstat_archiver.c
4+
* Implementation of archiver statistics.
5+
*
6+
* This file contains the implementation of archiver statistics. It is kept
7+
* separate from pgstat.c to enforce the line between the statistics access /
8+
* storage implementation and the details about individual types of
9+
* statistics.
10+
*
11+
* Copyright (c) 2001-2022, PostgreSQL Global Development Group
12+
*
13+
* IDENTIFICATION
14+
* src/backend/utils/activity/pgstat_archiver.c
15+
* -------------------------------------------------------------------------
16+
*/
17+
18+
#include "postgres.h"
19+
20+
#include "utils/pgstat_internal.h"
21+
#include "utils/timestamp.h"
22+
23+
24+
/* ----------
25+
* pgstat_send_archiver() -
26+
*
27+
* Tell the collector about the WAL file that we successfully
28+
* archived or failed to archive.
29+
* ----------
30+
*/
31+
void
32+
pgstat_send_archiver(const char *xlog, bool failed)
33+
{
34+
PgStat_MsgArchiver msg;
35+
36+
/*
37+
* Prepare and send the message
38+
*/
39+
pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_ARCHIVER);
40+
msg.m_failed = failed;
41+
strlcpy(msg.m_xlog, xlog, sizeof(msg.m_xlog));
42+
msg.m_timestamp = GetCurrentTimestamp();
43+
pgstat_send(&msg, sizeof(msg));
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* -------------------------------------------------------------------------
2+
*
3+
* pgstat_bgwriter.c
4+
* Implementation of bgwriter statistics.
5+
*
6+
* This file contains the implementation of bgwriter statistics. It is kept
7+
* separate from pgstat.c to enforce the line between the statistics access /
8+
* storage implementation and the details about individual types of
9+
* statistics.
10+
*
11+
* Copyright (c) 2001-2022, PostgreSQL Global Development Group
12+
*
13+
* IDENTIFICATION
14+
* src/backend/utils/activity/pgstat_bgwriter.c
15+
* -------------------------------------------------------------------------
16+
*/
17+
18+
#include "postgres.h"
19+
20+
#include "utils/pgstat_internal.h"
21+
22+
23+
/*
24+
* BgWriter global statistics counters. Stored directly in a stats
25+
* message structure so they can be sent without needing to copy things
26+
* around. We assume this init to zeroes.
27+
*/
28+
PgStat_MsgBgWriter PendingBgWriterStats;
29+
30+
31+
/* ----------
32+
* pgstat_send_bgwriter() -
33+
*
34+
* Send bgwriter statistics to the collector
35+
* ----------
36+
*/
37+
void
38+
pgstat_send_bgwriter(void)
39+
{
40+
/* We assume this initializes to zeroes */
41+
static const PgStat_MsgBgWriter all_zeroes;
42+
43+
pgstat_assert_is_up();
44+
45+
/*
46+
* This function can be called even if nothing at all has happened. In
47+
* this case, avoid sending a completely empty message to the stats
48+
* collector.
49+
*/
50+
if (memcmp(&PendingBgWriterStats, &all_zeroes, sizeof(PgStat_MsgBgWriter)) == 0)
51+
return;
52+
53+
/*
54+
* Prepare and send the message
55+
*/
56+
pgstat_setheader(&PendingBgWriterStats.m_hdr, PGSTAT_MTYPE_BGWRITER);
57+
pgstat_send(&PendingBgWriterStats, sizeof(PendingBgWriterStats));
58+
59+
/*
60+
* Clear out the statistics buffer, so it can be re-used.
61+
*/
62+
MemSet(&PendingBgWriterStats, 0, sizeof(PendingBgWriterStats));
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* -------------------------------------------------------------------------
2+
*
3+
* pgstat_checkpointer.c
4+
* Implementation of checkpoint statistics.
5+
*
6+
* This file contains the implementation of checkpoint statistics. It is kept
7+
* separate from pgstat.c to enforce the line between the statistics access /
8+
* storage implementation and the details about individual types of
9+
* statistics.
10+
*
11+
* Copyright (c) 2001-2022, PostgreSQL Global Development Group
12+
*
13+
* IDENTIFICATION
14+
* src/backend/utils/activity/pgstat_checkpointer.c
15+
* -------------------------------------------------------------------------
16+
*/
17+
18+
#include "postgres.h"
19+
20+
#include "utils/pgstat_internal.h"
21+
22+
23+
/*
24+
* Checkpointer global statistics counters. Stored directly in a stats
25+
* message structure so they can be sent without needing to copy things
26+
* around. We assume this init to zeroes.
27+
*/
28+
PgStat_MsgCheckpointer PendingCheckpointerStats;
29+
30+
31+
/* ----------
32+
* pgstat_send_checkpointer() -
33+
*
34+
* Send checkpointer statistics to the collector
35+
* ----------
36+
*/
37+
void
38+
pgstat_send_checkpointer(void)
39+
{
40+
/* We assume this initializes to zeroes */
41+
static const PgStat_MsgCheckpointer all_zeroes;
42+
43+
/*
44+
* This function can be called even if nothing at all has happened. In
45+
* this case, avoid sending a completely empty message to the stats
46+
* collector.
47+
*/
48+
if (memcmp(&PendingCheckpointerStats, &all_zeroes, sizeof(PgStat_MsgCheckpointer)) == 0)
49+
return;
50+
51+
/*
52+
* Prepare and send the message
53+
*/
54+
pgstat_setheader(&PendingCheckpointerStats.m_hdr, PGSTAT_MTYPE_CHECKPOINTER);
55+
pgstat_send(&PendingCheckpointerStats, sizeof(PendingCheckpointerStats));
56+
57+
/*
58+
* Clear out the statistics buffer, so it can be re-used.
59+
*/
60+
MemSet(&PendingCheckpointerStats, 0, sizeof(PendingCheckpointerStats));
61+
}

0 commit comments

Comments
 (0)