diff options
author | Michael Paquier <michael@paquier.xyz> | 2025-02-28 11:20:31 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2025-02-28 11:20:31 +0900 |
commit | c2a50ac678eb5ccee271aef3e7ed146ac395a32b (patch) | |
tree | 98145d57e043c33125f83fc1718c46b181e734dc /src/backend/utils/activity/pgstat_backend.c | |
parent | 2a083ab807db6d9e2e0e3aa82ee8f6ff9fc44c8d (diff) |
Invent pgstat_fetch_stat_backend_by_pid()
This code is extracted from pg_stat_get_backend_io() in pgstatfuncs.c,
so as it can be shared with other areas that need backend pgstats
entries while having the benefits of the various sanity checks
refactored here. As per its name, this retrieves backend statistics
based on a PID, with the option of retrieving a BackendType if given in
input.
Currently, this is used for the backend-level IO statistics. The next
move would be to reuse that for the backend-level WAL statistics.
Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Discussion: https://postgr.es/m/Z3zqc4o09dM/Ezyz@ip-10-97-1-34.eu-west-3.compute.internal
Diffstat (limited to 'src/backend/utils/activity/pgstat_backend.c')
-rw-r--r-- | src/backend/utils/activity/pgstat_backend.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/backend/utils/activity/pgstat_backend.c b/src/backend/utils/activity/pgstat_backend.c index 338da73a9a9..3c9ebbcd69c 100644 --- a/src/backend/utils/activity/pgstat_backend.c +++ b/src/backend/utils/activity/pgstat_backend.c @@ -26,6 +26,8 @@ #include "access/xlog.h" #include "storage/bufmgr.h" +#include "storage/proc.h" +#include "storage/procarray.h" #include "utils/memutils.h" #include "utils/pgstat_internal.h" @@ -83,6 +85,57 @@ pgstat_fetch_stat_backend(ProcNumber procNumber) } /* + * Returns statistics of a backend by pid. + * + * This routine includes sanity checks to ensire that the backend exists and + * is running. "bktype" can be optionally defined to return the BackendType + * of the backend whose statistics are returned. + */ +PgStat_Backend * +pgstat_fetch_stat_backend_by_pid(int pid, BackendType *bktype) +{ + PGPROC *proc; + PgBackendStatus *beentry; + ProcNumber procNumber; + PgStat_Backend *backend_stats; + + proc = BackendPidGetProc(pid); + if (bktype) + *bktype = B_INVALID; + + /* + * This could be an auxiliary process but these do not report backend + * statistics due to pgstat_tracks_backend_bktype(), so there is no need + * for an extra call to AuxiliaryPidGetProc(). + */ + if (!proc) + return NULL; + + procNumber = GetNumberFromPGProc(proc); + + beentry = pgstat_get_beentry_by_proc_number(procNumber); + if (!beentry) + return NULL; + + backend_stats = pgstat_fetch_stat_backend(procNumber); + if (!backend_stats) + return NULL; + + /* if PID does not match, leave */ + if (beentry->st_procpid != pid) + return NULL; + + /* backend may be gone, so recheck in case */ + if (beentry->st_backendType == B_INVALID) + return NULL; + + if (bktype) + *bktype = beentry->st_backendType; + + return backend_stats; +} + +/* * Flush out locally pending backend IO statistics. Locking is managed * by the caller. */ |