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

Commit9aea73f

Browse files
committed
Add backend-level statistics to pgstats
This adds a new variable-numbered statistics kind in pgstats, where theobject ID key of the stats entries is based on the proc number of thebackends. This acts as an upper-bound for the number of stats entriesthat can exist at once. The entries are created when a backend startsafter authentication succeeds, and are removed when the backend exits,making the stats entry exist for as long as their backend is up andrunning. These are not written to the pgstats file at shutdown (notethat write_to_file is disabled, as a safety measure).Currently, these stats include only information about the I/O generatedby a backend, using the same layer as pg_stat_io, except that it is nowpossible to know how much activity is happening in each backend ratherthan an overall aggregate of all the activity. A function calledpg_stat_get_backend_io() is added to access this data depending on thePID of a backend. The existing structure could be expanded in thefuture to add more information about other statistics related tobackends, depending on requirements or ideas.Auxiliary processes are not included in this set of statistics. Theseare less interesting to have than normal backends as they have dedicatedentries in pg_stat_io, and stats kinds of their own.This commit includes also pg_stat_reset_backend_stats(), function ableto reset all the stats associated to a single backend.Bump catalog version and PGSTAT_FILE_FORMAT_ID.Author: Bertrand DrouvotReviewed-by: Álvaro Herrera, Kyotaro Horiguchi, Michael Paquier, NazirBilal YavuzDiscussion:https://postgr.es/m/ZtXR+CtkEVVE/LHF@ip-10-97-1-34.eu-west-3.compute.internal
1 parentff7c40d commit9aea73f

File tree

18 files changed

+553
-29
lines changed

18 files changed

+553
-29
lines changed

‎doc/src/sgml/config.sgml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8403,9 +8403,11 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
84038403
displayed in <link linkend="monitoring-pg-stat-database-view">
84048404
<structname>pg_stat_database</structname></link>,
84058405
<link linkend="monitoring-pg-stat-io-view">
8406-
<structname>pg_stat_io</structname></link>, in the output of
8407-
<xref linkend="sql-explain"/> when the <literal>BUFFERS</literal> option
8408-
is used, in the output of <xref linkend="sql-vacuum"/> when
8406+
<structname>pg_stat_io</structname></link>, in the output of the
8407+
<link linkend="pg-stat-get-backend-io">
8408+
<function>pg_stat_get_backend_io()</function></link> function, in the
8409+
output of <xref linkend="sql-explain"/> when the <literal>BUFFERS</literal>
8410+
option is used, in the output of <xref linkend="sql-vacuum"/> when
84098411
the <literal>VERBOSE</literal> option is used, by autovacuum
84108412
for auto-vacuums and auto-analyzes, when <xref
84118413
linkend="guc-log-autovacuum-min-duration"/> is set and by

‎doc/src/sgml/monitoring.sgml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4790,6 +4790,27 @@ description | Waiting for a newly initialized WAL file to reach durable storage
47904790
</para></entry>
47914791
</row>
47924792

4793+
<row>
4794+
<entry id="pg-stat-get-backend-io" role="func_table_entry"><para role="func_signature">
4795+
<indexterm>
4796+
<primary>pg_stat_get_backend_io</primary>
4797+
</indexterm>
4798+
<function>pg_stat_get_backend_io</function> ( <type>integer</type> )
4799+
<returnvalue>setof record</returnvalue>
4800+
</para>
4801+
<para>
4802+
Returns I/O statistics about the backend with the specified
4803+
process ID. The output fields are exactly the same as the ones in the
4804+
<structname>pg_stat_io</structname> view.
4805+
</para>
4806+
<para>
4807+
The function does not return I/O statistics for the checkpointer,
4808+
the background writer, the startup process and the autovacuum launcher
4809+
as they are already visible in the <structname>pg_stat_io</structname>
4810+
view and there is only one of each.
4811+
</para></entry>
4812+
</row>
4813+
47934814
<row>
47944815
<entry role="func_table_entry"><para role="func_signature">
47954816
<indexterm>
@@ -4971,6 +4992,24 @@ description | Waiting for a newly initialized WAL file to reach durable storage
49714992
</para></entry>
49724993
</row>
49734994

4995+
<row>
4996+
<entry role="func_table_entry"><para role="func_signature">
4997+
<indexterm>
4998+
<primary>pg_stat_reset_backend_stats</primary>
4999+
</indexterm>
5000+
<function>pg_stat_reset_backend_stats</function> ( <type>integer</type> )
5001+
<returnvalue>void</returnvalue>
5002+
</para>
5003+
<para>
5004+
Resets statistics for a single backend with the specified process ID
5005+
to zero.
5006+
</para>
5007+
<para>
5008+
This function is restricted to superusers by default, but other users
5009+
can be granted EXECUTE to run the function.
5010+
</para></entry>
5011+
</row>
5012+
49745013
<row>
49755014
<entry role="func_table_entry"><para role="func_signature">
49765015
<indexterm>

‎src/backend/catalog/system_functions.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,8 @@ REVOKE EXECUTE ON FUNCTION pg_stat_reset_single_table_counters(oid) FROM public;
711711

712712
REVOKE EXECUTEON FUNCTION pg_stat_reset_single_function_counters(oid)FROM public;
713713

714+
REVOKE EXECUTEON FUNCTION pg_stat_reset_backend_stats(integer)FROM public;
715+
714716
REVOKE EXECUTEON FUNCTION pg_stat_reset_replication_slot(text)FROM public;
715717

716718
REVOKE EXECUTEON FUNCTION pg_stat_have_stats(text,oid, int8)FROM public;

‎src/backend/utils/activity/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ OBJS = \
2020
backend_status.o\
2121
pgstat.o\
2222
pgstat_archiver.o\
23+
pgstat_backend.o\
2324
pgstat_bgwriter.o\
2425
pgstat_checkpointer.o\
2526
pgstat_database.o\

‎src/backend/utils/activity/backend_status.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ pgstat_bestart(void)
426426

427427
PGSTAT_END_WRITE_ACTIVITY(vbeentry);
428428

429+
/* Create the backend statistics entry */
430+
if (pgstat_tracks_backend_bktype(MyBackendType))
431+
pgstat_create_backend(MyProcNumber);
432+
429433
/* Update app name to current GUC setting */
430434
if (application_name)
431435
pgstat_report_appname(application_name);

‎src/backend/utils/activity/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ backend_sources += files(
55
'backend_status.c',
66
'pgstat.c',
77
'pgstat_archiver.c',
8+
'pgstat_backend.c',
89
'pgstat_bgwriter.c',
910
'pgstat_checkpointer.c',
1011
'pgstat_database.c',

‎src/backend/utils/activity/pgstat.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
*
7878
* Each statistics kind is handled in a dedicated file:
7979
* - pgstat_archiver.c
80+
* - pgstat_backend.c
8081
* - pgstat_bgwriter.c
8182
* - pgstat_checkpointer.c
8283
* - pgstat_database.c
@@ -358,6 +359,22 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
358359
.reset_timestamp_cb=pgstat_subscription_reset_timestamp_cb,
359360
},
360361

362+
[PGSTAT_KIND_BACKEND]= {
363+
.name="backend",
364+
365+
.fixed_amount= false,
366+
.write_to_file= false,
367+
368+
.accessed_across_databases= true,
369+
370+
.shared_size=sizeof(PgStatShared_Backend),
371+
.shared_data_off= offsetof(PgStatShared_Backend,stats),
372+
.shared_data_len=sizeof(((PgStatShared_Backend*)0)->stats),
373+
.pending_size=sizeof(PgStat_BackendPendingIO),
374+
375+
.flush_pending_cb=pgstat_backend_flush_cb,
376+
.reset_timestamp_cb=pgstat_backend_reset_timestamp_cb,
377+
},
361378

362379
/* stats for fixed-numbered (mostly 1) objects */
363380

@@ -602,6 +619,10 @@ pgstat_shutdown_hook(int code, Datum arg)
602619
Assert(dlist_is_empty(&pgStatPending));
603620
dlist_init(&pgStatPending);
604621

622+
/* drop the backend stats entry */
623+
if (!pgstat_drop_entry(PGSTAT_KIND_BACKEND,InvalidOid,MyProcNumber))
624+
pgstat_request_entry_refs_gc();
625+
605626
pgstat_detach_shmem();
606627

607628
#ifdefUSE_ASSERT_CHECKING
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/* -------------------------------------------------------------------------
2+
*
3+
* pgstat_backend.c
4+
* Implementation of backend statistics.
5+
*
6+
* This file contains the implementation of backend statistics. It is kept
7+
* separate from pgstat.c to enforce the line between the statistics access /
8+
* storage implementation and the details about individual types of
9+
* statistics.
10+
*
11+
* This statistics kind uses a proc number as object ID for the hash table
12+
* of pgstats. Entries are created each time a process is spawned, and are
13+
* dropped when the process exits. These are not written to the pgstats file
14+
* on disk.
15+
*
16+
* Copyright (c) 2001-2024, PostgreSQL Global Development Group
17+
*
18+
* IDENTIFICATION
19+
* src/backend/utils/activity/pgstat_backend.c
20+
* -------------------------------------------------------------------------
21+
*/
22+
23+
#include"postgres.h"
24+
25+
#include"utils/pgstat_internal.h"
26+
27+
/*
28+
* Returns statistics of a backend by proc number.
29+
*/
30+
PgStat_Backend*
31+
pgstat_fetch_stat_backend(ProcNumberprocNumber)
32+
{
33+
PgStat_Backend*backend_entry;
34+
35+
backend_entry= (PgStat_Backend*)pgstat_fetch_entry(PGSTAT_KIND_BACKEND,
36+
InvalidOid,procNumber);
37+
38+
returnbackend_entry;
39+
}
40+
41+
/*
42+
* Flush out locally pending backend statistics
43+
*
44+
* If no stats have been recorded, this function returns false.
45+
*/
46+
bool
47+
pgstat_backend_flush_cb(PgStat_EntryRef*entry_ref,boolnowait)
48+
{
49+
PgStatShared_Backend*shbackendioent;
50+
PgStat_BackendPendingIO*pendingent;
51+
PgStat_BktypeIO*bktype_shstats;
52+
53+
if (!pgstat_lock_entry(entry_ref,nowait))
54+
return false;
55+
56+
shbackendioent= (PgStatShared_Backend*)entry_ref->shared_stats;
57+
bktype_shstats=&shbackendioent->stats.stats;
58+
pendingent= (PgStat_BackendPendingIO*)entry_ref->pending;
59+
60+
for (intio_object=0;io_object<IOOBJECT_NUM_TYPES;io_object++)
61+
{
62+
for (intio_context=0;io_context<IOCONTEXT_NUM_TYPES;io_context++)
63+
{
64+
for (intio_op=0;io_op<IOOP_NUM_TYPES;io_op++)
65+
{
66+
instr_timetime;
67+
68+
bktype_shstats->counts[io_object][io_context][io_op]+=
69+
pendingent->counts[io_object][io_context][io_op];
70+
71+
time=pendingent->pending_times[io_object][io_context][io_op];
72+
73+
bktype_shstats->times[io_object][io_context][io_op]+=
74+
INSTR_TIME_GET_MICROSEC(time);
75+
}
76+
}
77+
}
78+
79+
pgstat_unlock_entry(entry_ref);
80+
81+
return true;
82+
}
83+
84+
/*
85+
* Simpler wrapper of pgstat_backend_flush_cb()
86+
*/
87+
void
88+
pgstat_flush_backend(boolnowait)
89+
{
90+
PgStat_EntryRef*entry_ref;
91+
92+
if (!pgstat_tracks_backend_bktype(MyBackendType))
93+
return;
94+
95+
entry_ref=pgstat_get_entry_ref(PGSTAT_KIND_BACKEND,InvalidOid,
96+
MyProcNumber, false,NULL);
97+
(void)pgstat_backend_flush_cb(entry_ref,nowait);
98+
}
99+
100+
/*
101+
* Create backend statistics entry for proc number.
102+
*/
103+
void
104+
pgstat_create_backend(ProcNumberprocnum)
105+
{
106+
PgStat_EntryRef*entry_ref;
107+
PgStatShared_Backend*shstatent;
108+
109+
entry_ref=pgstat_prep_pending_entry(PGSTAT_KIND_BACKEND,InvalidOid,
110+
procnum,NULL);
111+
112+
shstatent= (PgStatShared_Backend*)entry_ref->shared_stats;
113+
114+
/*
115+
* NB: need to accept that there might be stats from an older backend,
116+
* e.g. if we previously used this proc number.
117+
*/
118+
memset(&shstatent->stats,0,sizeof(shstatent->stats));
119+
}
120+
121+
/*
122+
* Find or create a local PgStat_BackendPendingIO entry for proc number.
123+
*/
124+
PgStat_BackendPendingIO*
125+
pgstat_prep_backend_pending(ProcNumberprocnum)
126+
{
127+
PgStat_EntryRef*entry_ref;
128+
129+
entry_ref=pgstat_prep_pending_entry(PGSTAT_KIND_BACKEND,InvalidOid,
130+
procnum,NULL);
131+
132+
returnentry_ref->pending;
133+
}
134+
135+
/*
136+
* Backend statistics are not collected for all BackendTypes.
137+
*
138+
* The following BackendTypes do not participate in the backend stats
139+
* subsystem:
140+
* - The same and for the same reasons as in pgstat_tracks_io_bktype().
141+
* - B_BG_WRITER, B_CHECKPOINTER, B_STARTUP and B_AUTOVAC_LAUNCHER because their
142+
* I/O stats are already visible in pg_stat_io and there is only one of those.
143+
*
144+
* Function returns true if BackendType participates in the backend stats
145+
* subsystem and false if it does not.
146+
*
147+
* When adding a new BackendType, also consider adding relevant restrictions to
148+
* pgstat_tracks_io_object() and pgstat_tracks_io_op().
149+
*/
150+
bool
151+
pgstat_tracks_backend_bktype(BackendTypebktype)
152+
{
153+
/*
154+
* List every type so that new backend types trigger a warning about
155+
* needing to adjust this switch.
156+
*/
157+
switch (bktype)
158+
{
159+
caseB_INVALID:
160+
caseB_AUTOVAC_LAUNCHER:
161+
caseB_DEAD_END_BACKEND:
162+
caseB_ARCHIVER:
163+
caseB_LOGGER:
164+
caseB_WAL_RECEIVER:
165+
caseB_WAL_WRITER:
166+
caseB_WAL_SUMMARIZER:
167+
caseB_BG_WRITER:
168+
caseB_CHECKPOINTER:
169+
caseB_STARTUP:
170+
return false;
171+
172+
caseB_AUTOVAC_WORKER:
173+
caseB_BACKEND:
174+
caseB_BG_WORKER:
175+
caseB_STANDALONE_BACKEND:
176+
caseB_SLOTSYNC_WORKER:
177+
caseB_WAL_SENDER:
178+
return true;
179+
}
180+
181+
return false;
182+
}
183+
184+
void
185+
pgstat_backend_reset_timestamp_cb(PgStatShared_Common*header,TimestampTzts)
186+
{
187+
((PgStatShared_Backend*)header)->stats.stat_reset_timestamp=ts;
188+
}

‎src/backend/utils/activity/pgstat_io.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@
2020
#include"storage/bufmgr.h"
2121
#include"utils/pgstat_internal.h"
2222

23-
24-
typedefstructPgStat_PendingIO
25-
{
26-
PgStat_Countercounts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES];
27-
instr_timepending_times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES];
28-
}PgStat_PendingIO;
29-
30-
3123
staticPgStat_PendingIOPendingIOStats;
3224
staticboolhave_iostats= false;
3325

@@ -87,6 +79,14 @@ pgstat_count_io_op_n(IOObject io_object, IOContext io_context, IOOp io_op, uint3
8779
Assert((unsignedint)io_op<IOOP_NUM_TYPES);
8880
Assert(pgstat_tracks_io_op(MyBackendType,io_object,io_context,io_op));
8981

82+
if (pgstat_tracks_backend_bktype(MyBackendType))
83+
{
84+
PgStat_PendingIO*entry_ref;
85+
86+
entry_ref=pgstat_prep_backend_pending(MyProcNumber);
87+
entry_ref->counts[io_object][io_context][io_op]+=cnt;
88+
}
89+
9090
PendingIOStats.counts[io_object][io_context][io_op]+=cnt;
9191

9292
have_iostats= true;
@@ -148,6 +148,15 @@ pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op,
148148

149149
INSTR_TIME_ADD(PendingIOStats.pending_times[io_object][io_context][io_op],
150150
io_time);
151+
152+
if (pgstat_tracks_backend_bktype(MyBackendType))
153+
{
154+
PgStat_PendingIO*entry_ref;
155+
156+
entry_ref=pgstat_prep_backend_pending(MyProcNumber);
157+
INSTR_TIME_ADD(entry_ref->pending_times[io_object][io_context][io_op],
158+
io_time);
159+
}
151160
}
152161

153162
pgstat_count_io_op_n(io_object,io_context,io_op,cnt);

‎src/backend/utils/activity/pgstat_relation.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ pgstat_report_vacuum(Oid tableoid, bool shared,
264264
* VACUUM command has processed all tables and committed.
265265
*/
266266
pgstat_flush_io(false);
267+
pgstat_flush_backend(false);
267268
}
268269

269270
/*
@@ -350,6 +351,7 @@ pgstat_report_analyze(Relation rel,
350351

351352
/* see pgstat_report_vacuum() */
352353
pgstat_flush_io(false);
354+
pgstat_flush_backend(false);
353355
}
354356

355357
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp