Skip to content

Commit

Permalink
pgstat: Infrastructure for more detailed IO statistics
Browse files Browse the repository at this point in the history
This commit adds the infrastructure for more detailed IO statistics. The calls
to actually count IOs, a system view to access the new statistics,
documentation and tests will be added in subsequent commits, to make review
easier.

While we already had some IO statistics, e.g. in pg_stat_bgwriter and
pg_stat_database, they did not provide sufficient detail to understand what
the main sources of IO are, or whether configuration changes could avoid
IO. E.g., pg_stat_bgwriter.buffers_backend does contain the number of buffers
written out by a backend, but as that includes extending relations (always
done by backends) and writes triggered by the use of buffer access strategies,
it cannot easily be used to tune background writer or checkpointer. Similarly,
pg_stat_database.blks_read cannot easily be used to tune shared_buffers /
compute a cache hit ratio, as the use of buffer access strategies will often
prevent a large fraction of the read blocks to end up in shared_buffers.

The new IO statistics count IO operations (evict, extend, fsync, read, reuse,
and write), and are aggregated for each combination of backend type (backend,
autovacuum worker, bgwriter, etc), target object of the IO (relations, temp
relations) and context of the IO (normal, vacuum, bulkread, bulkwrite).

What is tracked in this series of patches, is sufficient to perform the
aforementioned analyses. Further details, e.g. tracking the number of buffer
hits, would make that even easier, but was left out for now, to keep the scope
of the already large patchset manageable.

Bumps PGSTAT_FILE_FORMAT_ID.

Author: Melanie Plageman <[email protected]>
Reviewed-by: Andres Freund <[email protected]>
Reviewed-by: Justin Pryzby <[email protected]>
Reviewed-by: Kyotaro Horiguchi <[email protected]>
Discussion: https://postgr.es/m/[email protected]
  • Loading branch information
anarazel committed Feb 9, 2023
1 parent 49c2c5f commit 28e626b
Show file tree
Hide file tree
Showing 15 changed files with 569 additions and 8 deletions.
2 changes: 2 additions & 0 deletions doc/src/sgml/monitoring.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -5444,6 +5444,8 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i
the <structname>pg_stat_bgwriter</structname>
view, <literal>archiver</literal> to reset all the counters shown in
the <structname>pg_stat_archiver</structname> view,
<literal>io</literal> to reset all the counters shown in the
<structname>pg_stat_io</structname> view,
<literal>wal</literal> to reset all the counters shown in the
<structname>pg_stat_wal</structname> view or
<literal>recovery_prefetch</literal> to reset all the counters shown
Expand Down
1 change: 1 addition & 0 deletions src/backend/utils/activity/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ OBJS = \
pgstat_checkpointer.o \
pgstat_database.o \
pgstat_function.o \
pgstat_io.o \
pgstat_relation.o \
pgstat_replslot.o \
pgstat_shmem.o \
Expand Down
1 change: 1 addition & 0 deletions src/backend/utils/activity/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ backend_sources += files(
'pgstat_checkpointer.c',
'pgstat_database.c',
'pgstat_function.c',
'pgstat_io.c',
'pgstat_relation.c',
'pgstat_replslot.c',
'pgstat_shmem.c',
Expand Down
26 changes: 26 additions & 0 deletions src/backend/utils/activity/pgstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
* - pgstat_checkpointer.c
* - pgstat_database.c
* - pgstat_function.c
* - pgstat_io.c
* - pgstat_relation.c
* - pgstat_replslot.c
* - pgstat_slru.c
Expand Down Expand Up @@ -359,6 +360,15 @@ static const PgStat_KindInfo pgstat_kind_infos[PGSTAT_NUM_KINDS] = {
.snapshot_cb = pgstat_checkpointer_snapshot_cb,
},

[PGSTAT_KIND_IO] = {
.name = "io",

.fixed_amount = true,

.reset_all_cb = pgstat_io_reset_all_cb,
.snapshot_cb = pgstat_io_snapshot_cb,
},

[PGSTAT_KIND_SLRU] = {
.name = "slru",

Expand Down Expand Up @@ -582,6 +592,7 @@ pgstat_report_stat(bool force)

/* Don't expend a clock check if nothing to do */
if (dlist_is_empty(&pgStatPending) &&
!have_iostats &&
!have_slrustats &&
!pgstat_have_pending_wal())
{
Expand Down Expand Up @@ -628,6 +639,9 @@ pgstat_report_stat(bool force)
/* flush database / relation / function / ... stats */
partial_flush |= pgstat_flush_pending_entries(nowait);

/* flush IO stats */
partial_flush |= pgstat_flush_io(nowait);

/* flush wal stats */
partial_flush |= pgstat_flush_wal(nowait);

Expand Down Expand Up @@ -1322,6 +1336,12 @@ pgstat_write_statsfile(void)
pgstat_build_snapshot_fixed(PGSTAT_KIND_CHECKPOINTER);
write_chunk_s(fpout, &pgStatLocal.snapshot.checkpointer);

/*
* Write IO stats struct
*/
pgstat_build_snapshot_fixed(PGSTAT_KIND_IO);
write_chunk_s(fpout, &pgStatLocal.snapshot.io);

/*
* Write SLRU stats struct
*/
Expand Down Expand Up @@ -1496,6 +1516,12 @@ pgstat_read_statsfile(void)
if (!read_chunk_s(fpin, &shmem->checkpointer.stats))
goto error;

/*
* Read IO stats struct
*/
if (!read_chunk_s(fpin, &shmem->io.stats))
goto error;

/*
* Read SLRU stats struct
*/
Expand Down
7 changes: 6 additions & 1 deletion src/backend/utils/activity/pgstat_bgwriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PgStat_BgWriterStats PendingBgWriterStats = {0};


/*
* Report bgwriter statistics
* Report bgwriter and IO statistics
*/
void
pgstat_report_bgwriter(void)
Expand Down Expand Up @@ -56,6 +56,11 @@ pgstat_report_bgwriter(void)
* Clear out the statistics buffer, so it can be re-used.
*/
MemSet(&PendingBgWriterStats, 0, sizeof(PendingBgWriterStats));

/*
* Report IO statistics
*/
pgstat_flush_io(false);
}

/*
Expand Down
7 changes: 6 additions & 1 deletion src/backend/utils/activity/pgstat_checkpointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PgStat_CheckpointerStats PendingCheckpointerStats = {0};


/*
* Report checkpointer statistics
* Report checkpointer and IO statistics
*/
void
pgstat_report_checkpointer(void)
Expand Down Expand Up @@ -62,6 +62,11 @@ pgstat_report_checkpointer(void)
* Clear out the statistics buffer, so it can be re-used.
*/
MemSet(&PendingCheckpointerStats, 0, sizeof(PendingCheckpointerStats));

/*
* Report IO statistics
*/
pgstat_flush_io(false);
}

/*
Expand Down
Loading

0 comments on commit 28e626b

Please sign in to comment.