1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-04 20:11:56 +03:00

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
This commit is contained in:
Michael Paquier
2025-02-28 11:20:31 +09:00
parent 2a083ab807
commit c2a50ac678
3 changed files with 56 additions and 29 deletions

View File

@@ -1576,46 +1576,18 @@ pg_stat_get_backend_io(PG_FUNCTION_ARGS)
ReturnSetInfo *rsinfo;
BackendType bktype;
int pid;
PGPROC *proc;
ProcNumber procNumber;
PgStat_Backend *backend_stats;
PgStat_BktypeIO *bktype_stats;
PgBackendStatus *beentry;
InitMaterializedSRF(fcinfo, 0);
rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
pid = PG_GETARG_INT32(0);
proc = BackendPidGetProc(pid);
backend_stats = pgstat_fetch_stat_backend_by_pid(pid, &bktype);
/*
* 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 (Datum) 0;
procNumber = GetNumberFromPGProc(proc);
beentry = pgstat_get_beentry_by_proc_number(procNumber);
if (!beentry)
return (Datum) 0;
backend_stats = pgstat_fetch_stat_backend(procNumber);
if (!backend_stats)
return (Datum) 0;
bktype = beentry->st_backendType;
/* if PID does not match, leave */
if (beentry->st_procpid != pid)
return (Datum) 0;
/* backend may be gone, so recheck in case */
if (bktype == B_INVALID)
return (Datum) 0;
bktype_stats = &backend_stats->io_stats;
/*