Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc2a50ac

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 pgstatsentries while having the benefits of the various sanity checksrefactored here. As per its name, this retrieves backend statisticsbased on a PID, with the option of retrieving a BackendType if given ininput.Currently, this is used for the backend-level IO statistics. The nextmove 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 parent2a083ab commitc2a50ac

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
returnbackend_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(intpid,BackendType*bktype)
96+
{
97+
PGPROC*proc;
98+
PgBackendStatus*beentry;
99+
ProcNumberprocNumber;
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+
returnNULL;
113+
114+
procNumber=GetNumberFromPGProc(proc);
115+
116+
beentry=pgstat_get_beentry_by_proc_number(procNumber);
117+
if (!beentry)
118+
returnNULL;
119+
120+
backend_stats=pgstat_fetch_stat_backend(procNumber);
121+
if (!backend_stats)
122+
returnNULL;
123+
124+
/* if PID does not match, leave */
125+
if (beentry->st_procpid!=pid)
126+
returnNULL;
127+
128+
/* backend may be gone, so recheck in case */
129+
if (beentry->st_backendType==B_INVALID)
130+
returnNULL;
131+
132+
if (bktype)
133+
*bktype=beentry->st_backendType;
134+
135+
returnbackend_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
BackendTypebktype;
15781578
intpid;
1579-
PGPROC*proc;
1580-
ProcNumberprocNumber;
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
IOOpio_op,uint32cnt,
555555
uint64bytes);
556556
externPgStat_Backend*pgstat_fetch_stat_backend(ProcNumberprocNumber);
557+
externPgStat_Backend*pgstat_fetch_stat_backend_by_pid(intpid,
558+
BackendType*bktype);
557559
externboolpgstat_tracks_backend_bktype(BackendTypebktype);
558560
externvoidpgstat_create_backend(ProcNumberprocnum);
559561

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp