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

Commit 3f1db99

Browse files
committed
Handle auxiliary processes in SQL functions of backend statistics
This commit impacts the following SQL functions, authorizing the access to the PGPROC entries of auxiliary processes when attempting to fetch or reset backend-level pgstats entries: - pg_stat_reset_backend_stats() - pg_stat_get_backend_io() This is relevant since a051e71 for at least the WAL summarizer, WAL receiver and WAL writer processes, that has changed the backend statistics to authorize these three following the addition of WAL I/O statistics in pg_stat_io and backend statistics. The code is more flexible with future changes written this way, adapting automatically to any updates done in pgstat_tracks_backend_bktype(). While on it, pgstat_report_wal() gains a call to pgstat_flush_backend(), making sure that backend I/O statistics are updated when calling this routine. This makes the statistics report correctly for the WAL writer. WAL receiver and WAL summarizer do not call pgstat_report_wal() yet (spoiler: both should). It should be possible to lift some of the existing restrictions for other auxiliary processes, as well, but this is left as future work. Reported-by: Rahila Syed <rahilasyed90@gmail.com> Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Discussion: https://postgr.es/m/CAH2L28v9BwN8_y0k6FQ591=0g2Hj_esHLGj3bP38c9nmVykoiA@mail.gmail.com
1 parent fe186bd commit 3f1db99

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/backend/utils/activity/pgstat_backend.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,10 @@ pgstat_fetch_stat_backend_by_pid(int pid, BackendType *bktype)
103103
if (bktype)
104104
*bktype = B_INVALID;
105105

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-
*/
106+
/* this could be an auxiliary process */
107+
if (!proc)
108+
proc = AuxiliaryPidGetProc(pid);
109+
111110
if (!proc)
112111
return NULL;
113112

@@ -117,6 +116,10 @@ pgstat_fetch_stat_backend_by_pid(int pid, BackendType *bktype)
117116
if (!beentry)
118117
return NULL;
119118

119+
/* check if the backend type tracks statistics */
120+
if (!pgstat_tracks_backend_bktype(beentry->st_backendType))
121+
return NULL;
122+
120123
backend_stats = pgstat_fetch_stat_backend(procNumber);
121124
if (!backend_stats)
122125
return NULL;
@@ -125,10 +128,6 @@ pgstat_fetch_stat_backend_by_pid(int pid, BackendType *bktype)
125128
if (beentry->st_procpid != pid)
126129
return NULL;
127130

128-
/* backend may be gone, so recheck in case */
129-
if (beentry->st_backendType == B_INVALID)
130-
return NULL;
131-
132131
if (bktype)
133132
*bktype = beentry->st_backendType;
134133

src/backend/utils/activity/pgstat_wal.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pgstat_report_wal(bool force)
5555

5656
/* flush IO stats */
5757
pgstat_flush_io(nowait);
58+
(void) pgstat_flush_backend(nowait, PGSTAT_BACKEND_FLUSH_IO);
5859
}
5960

6061
/*

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,19 +1919,30 @@ Datum
19191919
pg_stat_reset_backend_stats(PG_FUNCTION_ARGS)
19201920
{
19211921
PGPROC *proc;
1922+
PgBackendStatus *beentry;
1923+
ProcNumber procNumber;
19221924
int backend_pid = PG_GETARG_INT32(0);
19231925

19241926
proc = BackendPidGetProc(backend_pid);
19251927

1926-
/*
1927-
* This could be an auxiliary process but these do not report backend
1928-
* statistics due to pgstat_tracks_backend_bktype(), so there is no need
1929-
* for an extra call to AuxiliaryPidGetProc().
1930-
*/
1928+
/* This could be an auxiliary process */
1929+
if (!proc)
1930+
proc = AuxiliaryPidGetProc(backend_pid);
1931+
19311932
if (!proc)
19321933
PG_RETURN_VOID();
19331934

1934-
pgstat_reset(PGSTAT_KIND_BACKEND, InvalidOid, GetNumberFromPGProc(proc));
1935+
procNumber = GetNumberFromPGProc(proc);
1936+
1937+
beentry = pgstat_get_beentry_by_proc_number(procNumber);
1938+
if (!beentry)
1939+
PG_RETURN_VOID();
1940+
1941+
/* Check if the backend type tracks statistics */
1942+
if (!pgstat_tracks_backend_bktype(beentry->st_backendType))
1943+
PG_RETURN_VOID();
1944+
1945+
pgstat_reset(PGSTAT_KIND_BACKEND, InvalidOid, procNumber);
19351946

19361947
PG_RETURN_VOID();
19371948
}

0 commit comments

Comments
 (0)