1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-19 13:42:17 +03:00

Add backend-level statistics to pgstats

This adds a new variable-numbered statistics kind in pgstats, where the
object ID key of the stats entries is based on the proc number of the
backends.  This acts as an upper-bound for the number of stats entries
that can exist at once.  The entries are created when a backend starts
after authentication succeeds, and are removed when the backend exits,
making the stats entry exist for as long as their backend is up and
running.  These are not written to the pgstats file at shutdown (note
that write_to_file is disabled, as a safety measure).

Currently, these stats include only information about the I/O generated
by a backend, using the same layer as pg_stat_io, except that it is now
possible to know how much activity is happening in each backend rather
than an overall aggregate of all the activity.  A function called
pg_stat_get_backend_io() is added to access this data depending on the
PID of a backend.  The existing structure could be expanded in the
future to add more information about other statistics related to
backends, depending on requirements or ideas.

Auxiliary processes are not included in this set of statistics.  These
are less interesting to have than normal backends as they have dedicated
entries in pg_stat_io, and stats kinds of their own.

This commit includes also pg_stat_reset_backend_stats(), function able
to reset all the stats associated to a single backend.

Bump catalog version and PGSTAT_FILE_FORMAT_ID.

Author: Bertrand Drouvot
Reviewed-by: Álvaro Herrera, Kyotaro Horiguchi, Michael Paquier, Nazir
Bilal Yavuz
Discussion: https://postgr.es/m/ZtXR+CtkEVVE/LHF@ip-10-97-1-34.eu-west-3.compute.internal
This commit is contained in:
Michael Paquier
2024-12-19 13:19:22 +09:00
parent ff7c40d7fd
commit 9aea73fc61
18 changed files with 552 additions and 28 deletions

View File

@@ -0,0 +1,188 @@
/* -------------------------------------------------------------------------
*
* pgstat_backend.c
* Implementation of backend statistics.
*
* This file contains the implementation of backend statistics. It is kept
* separate from pgstat.c to enforce the line between the statistics access /
* storage implementation and the details about individual types of
* statistics.
*
* This statistics kind uses a proc number as object ID for the hash table
* of pgstats. Entries are created each time a process is spawned, and are
* dropped when the process exits. These are not written to the pgstats file
* on disk.
*
* Copyright (c) 2001-2024, PostgreSQL Global Development Group
*
* IDENTIFICATION
* src/backend/utils/activity/pgstat_backend.c
* -------------------------------------------------------------------------
*/
#include "postgres.h"
#include "utils/pgstat_internal.h"
/*
* Returns statistics of a backend by proc number.
*/
PgStat_Backend *
pgstat_fetch_stat_backend(ProcNumber procNumber)
{
PgStat_Backend *backend_entry;
backend_entry = (PgStat_Backend *) pgstat_fetch_entry(PGSTAT_KIND_BACKEND,
InvalidOid, procNumber);
return backend_entry;
}
/*
* Flush out locally pending backend statistics
*
* If no stats have been recorded, this function returns false.
*/
bool
pgstat_backend_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
{
PgStatShared_Backend *shbackendioent;
PgStat_BackendPendingIO *pendingent;
PgStat_BktypeIO *bktype_shstats;
if (!pgstat_lock_entry(entry_ref, nowait))
return false;
shbackendioent = (PgStatShared_Backend *) entry_ref->shared_stats;
bktype_shstats = &shbackendioent->stats.stats;
pendingent = (PgStat_BackendPendingIO *) entry_ref->pending;
for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
{
for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
{
for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
{
instr_time time;
bktype_shstats->counts[io_object][io_context][io_op] +=
pendingent->counts[io_object][io_context][io_op];
time = pendingent->pending_times[io_object][io_context][io_op];
bktype_shstats->times[io_object][io_context][io_op] +=
INSTR_TIME_GET_MICROSEC(time);
}
}
}
pgstat_unlock_entry(entry_ref);
return true;
}
/*
* Simpler wrapper of pgstat_backend_flush_cb()
*/
void
pgstat_flush_backend(bool nowait)
{
PgStat_EntryRef *entry_ref;
if (!pgstat_tracks_backend_bktype(MyBackendType))
return;
entry_ref = pgstat_get_entry_ref(PGSTAT_KIND_BACKEND, InvalidOid,
MyProcNumber, false, NULL);
(void) pgstat_backend_flush_cb(entry_ref, nowait);
}
/*
* Create backend statistics entry for proc number.
*/
void
pgstat_create_backend(ProcNumber procnum)
{
PgStat_EntryRef *entry_ref;
PgStatShared_Backend *shstatent;
entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_BACKEND, InvalidOid,
procnum, NULL);
shstatent = (PgStatShared_Backend *) entry_ref->shared_stats;
/*
* NB: need to accept that there might be stats from an older backend,
* e.g. if we previously used this proc number.
*/
memset(&shstatent->stats, 0, sizeof(shstatent->stats));
}
/*
* Find or create a local PgStat_BackendPendingIO entry for proc number.
*/
PgStat_BackendPendingIO *
pgstat_prep_backend_pending(ProcNumber procnum)
{
PgStat_EntryRef *entry_ref;
entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_BACKEND, InvalidOid,
procnum, NULL);
return entry_ref->pending;
}
/*
* Backend statistics are not collected for all BackendTypes.
*
* The following BackendTypes do not participate in the backend stats
* subsystem:
* - The same and for the same reasons as in pgstat_tracks_io_bktype().
* - B_BG_WRITER, B_CHECKPOINTER, B_STARTUP and B_AUTOVAC_LAUNCHER because their
* I/O stats are already visible in pg_stat_io and there is only one of those.
*
* Function returns true if BackendType participates in the backend stats
* subsystem and false if it does not.
*
* When adding a new BackendType, also consider adding relevant restrictions to
* pgstat_tracks_io_object() and pgstat_tracks_io_op().
*/
bool
pgstat_tracks_backend_bktype(BackendType bktype)
{
/*
* List every type so that new backend types trigger a warning about
* needing to adjust this switch.
*/
switch (bktype)
{
case B_INVALID:
case B_AUTOVAC_LAUNCHER:
case B_DEAD_END_BACKEND:
case B_ARCHIVER:
case B_LOGGER:
case B_WAL_RECEIVER:
case B_WAL_WRITER:
case B_WAL_SUMMARIZER:
case B_BG_WRITER:
case B_CHECKPOINTER:
case B_STARTUP:
return false;
case B_AUTOVAC_WORKER:
case B_BACKEND:
case B_BG_WORKER:
case B_STANDALONE_BACKEND:
case B_SLOTSYNC_WORKER:
case B_WAL_SENDER:
return true;
}
return false;
}
void
pgstat_backend_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts)
{
((PgStatShared_Backend *) header)->stats.stat_reset_timestamp = ts;
}