Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c2a50ac

Browse files
committed
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
1 parent 2a083ab commit c2a50ac

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

src/backend/utils/activity/pgstat_backend.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include "access/xlog.h"
2828
#include "storage/bufmgr.h"
29+
#include "storage/proc.h"
30+
#include "storage/procarray.h"
2931
#include "utils/memutils.h"
3032
#include "utils/pgstat_internal.h"
3133

@@ -82,6 +84,57 @@ pgstat_fetch_stat_backend(ProcNumber procNumber)
8284
return backend_entry;
8385
}
8486

87+
/*
88+
* Returns statistics of a backend by pid.
89+
*
90+
* This routine includes sanity checks to ensire that the backend exists and
91+
* is running. "bktype" can be optionally defined to return the BackendType
92+
* of the backend whose statistics are returned.
93+
*/
94+
PgStat_Backend *
95+
pgstat_fetch_stat_backend_by_pid(int pid, BackendType *bktype)
96+
{
97+
PGPROC *proc;
98+
PgBackendStatus *beentry;
99+
ProcNumber procNumber;
100+
PgStat_Backend *backend_stats;
101+
102+
proc = BackendPidGetProc(pid);
103+
if (bktype)
104+
*bktype = B_INVALID;
105+
106+
/*
107+
* This could be an auxiliary process but these do not report backend
108+
* statistics due to pgstat_tracks_backend_bktype(), so there is no need
109+
* for an extra call to AuxiliaryPidGetProc().
110+
*/
111+
if (!proc)
112+
return NULL;
113+
114+
procNumber = GetNumberFromPGProc(proc);
115+
116+
beentry = pgstat_get_beentry_by_proc_number(procNumber);
117+
if (!beentry)
118+
return NULL;
119+
120+
backend_stats = pgstat_fetch_stat_backend(procNumber);
121+
if (!backend_stats)
122+
return NULL;
123+
124+
/* if PID does not match, leave */
125+
if (beentry->st_procpid != pid)
126+
return NULL;
127+
128+
/* backend may be gone, so recheck in case */
129+
if (beentry->st_backendType == B_INVALID)
130+
return NULL;
131+
132+
if (bktype)
133+
*bktype = beentry->st_backendType;
134+
135+
return backend_stats;
136+
}
137+
85138
/*
86139
* Flush out locally pending backend IO statistics. Locking is managed
87140
* by the caller.

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,46 +1576,18 @@ pg_stat_get_backend_io(PG_FUNCTION_ARGS)
15761576
ReturnSetInfo *rsinfo;
15771577
BackendType bktype;
15781578
int pid;
1579-
PGPROC *proc;
1580-
ProcNumber procNumber;
15811579
PgStat_Backend *backend_stats;
15821580
PgStat_BktypeIO *bktype_stats;
1583-
PgBackendStatus *beentry;
15841581

15851582
InitMaterializedSRF(fcinfo, 0);
15861583
rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
15871584

15881585
pid = PG_GETARG_INT32(0);
1589-
proc = BackendPidGetProc(pid);
1590-
1591-
/*
1592-
* This could be an auxiliary process but these do not report backend
1593-
* statistics due to pgstat_tracks_backend_bktype(), so there is no need
1594-
* for an extra call to AuxiliaryPidGetProc().
1595-
*/
1596-
if (!proc)
1597-
return (Datum) 0;
1598-
1599-
procNumber = GetNumberFromPGProc(proc);
1586+
backend_stats = pgstat_fetch_stat_backend_by_pid(pid, &bktype);
16001587

1601-
beentry = pgstat_get_beentry_by_proc_number(procNumber);
1602-
if (!beentry)
1603-
return (Datum) 0;
1604-
1605-
backend_stats = pgstat_fetch_stat_backend(procNumber);
16061588
if (!backend_stats)
16071589
return (Datum) 0;
16081590

1609-
bktype = beentry->st_backendType;
1610-
1611-
/* if PID does not match, leave */
1612-
if (beentry->st_procpid != pid)
1613-
return (Datum) 0;
1614-
1615-
/* backend may be gone, so recheck in case */
1616-
if (bktype == B_INVALID)
1617-
return (Datum) 0;
1618-
16191591
bktype_stats = &backend_stats->io_stats;
16201592

16211593
/*

src/include/pgstat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ extern void pgstat_count_backend_io_op(IOObject io_object,
554554
IOOp io_op, uint32 cnt,
555555
uint64 bytes);
556556
extern PgStat_Backend *pgstat_fetch_stat_backend(ProcNumber procNumber);
557+
extern PgStat_Backend *pgstat_fetch_stat_backend_by_pid(int pid,
558+
BackendType *bktype);
557559
extern bool pgstat_tracks_backend_bktype(BackendType bktype);
558560
extern void pgstat_create_backend(ProcNumber procnum);
559561

0 commit comments

Comments
 (0)