Skip to content

Commit 2c14037

Browse files
committed
Refactor some code related to backend statistics
This commit changes the way pending backend statistics are tracked by moving them into a new structure called PgStat_BackendPending, removing PgStat_BackendPendingIO. PgStat_BackendPending currently only includes PgStat_PendingIO for the pending I/O stats. pgstat_flush_backend() is extended with a "flags" argument to control which parts of the stats of a backend should be flushed. With this refactoring, it becomes easier to plug into backend statistics more data. A patch to add information related to WAL in this stats kind is under discussion. Author: Bertrand Drouvot Discussion: https://postgr.es/m/Z3zqc4o09dM/Ezyz@ip-10-97-1-34.eu-west-3.compute.internal
1 parent 39e3bca commit 2c14037

File tree

8 files changed

+76
-35
lines changed

8 files changed

+76
-35
lines changed

src/backend/utils/activity/pgstat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
370370
.shared_size = sizeof(PgStatShared_Backend),
371371
.shared_data_off = offsetof(PgStatShared_Backend, stats),
372372
.shared_data_len = sizeof(((PgStatShared_Backend *) 0)->stats),
373-
.pending_size = sizeof(PgStat_BackendPendingIO),
373+
.pending_size = sizeof(PgStat_BackendPending),
374374

375375
.flush_pending_cb = pgstat_backend_flush_cb,
376376
.reset_timestamp_cb = pgstat_backend_reset_timestamp_cb,

src/backend/utils/activity/pgstat_backend.c

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,21 @@ pgstat_fetch_stat_backend(ProcNumber procNumber)
3939
}
4040

4141
/*
42-
* Flush out locally pending backend statistics
43-
*
44-
* If no stats have been recorded, this function returns false.
42+
* Flush out locally pending backend IO statistics. Locking is managed
43+
* by the caller.
4544
*/
46-
bool
47-
pgstat_backend_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
45+
static void
46+
pgstat_flush_backend_entry_io(PgStat_EntryRef *entry_ref)
4847
{
49-
PgStatShared_Backend *shbackendioent;
50-
PgStat_BackendPendingIO *pendingent;
48+
PgStatShared_Backend *shbackendent;
49+
PgStat_BackendPending *pendingent;
5150
PgStat_BktypeIO *bktype_shstats;
51+
PgStat_PendingIO *pending_io;
5252

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;
53+
shbackendent = (PgStatShared_Backend *) entry_ref->shared_stats;
54+
pendingent = (PgStat_BackendPending *) entry_ref->pending;
55+
bktype_shstats = &shbackendent->stats.io_stats;
56+
pending_io = &pendingent->pending_io;
5957

6058
for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
6159
{
@@ -66,26 +64,57 @@ pgstat_backend_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
6664
instr_time time;
6765

6866
bktype_shstats->counts[io_object][io_context][io_op] +=
69-
pendingent->counts[io_object][io_context][io_op];
67+
pending_io->counts[io_object][io_context][io_op];
7068

71-
time = pendingent->pending_times[io_object][io_context][io_op];
69+
time = pending_io->pending_times[io_object][io_context][io_op];
7270

7371
bktype_shstats->times[io_object][io_context][io_op] +=
7472
INSTR_TIME_GET_MICROSEC(time);
7573
}
7674
}
7775
}
76+
}
77+
78+
/*
79+
* Wrapper routine to flush backend statistics.
80+
*/
81+
static bool
82+
pgstat_flush_backend_entry(PgStat_EntryRef *entry_ref, bool nowait,
83+
bits32 flags)
84+
{
85+
if (!pgstat_tracks_backend_bktype(MyBackendType))
86+
return false;
87+
88+
if (!pgstat_lock_entry(entry_ref, nowait))
89+
return false;
90+
91+
/* Flush requested statistics */
92+
if (flags & PGSTAT_BACKEND_FLUSH_IO)
93+
pgstat_flush_backend_entry_io(entry_ref);
7894

7995
pgstat_unlock_entry(entry_ref);
8096

8197
return true;
8298
}
8399

84100
/*
85-
* Simpler wrapper of pgstat_backend_flush_cb()
101+
* Callback to flush out locally pending backend statistics.
102+
*
103+
* If no stats have been recorded, this function returns false.
104+
*/
105+
bool
106+
pgstat_backend_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
107+
{
108+
return pgstat_flush_backend_entry(entry_ref, nowait, PGSTAT_BACKEND_FLUSH_ALL);
109+
}
110+
111+
/*
112+
* Flush out locally pending backend statistics
113+
*
114+
* "flags" parameter controls which statistics to flush.
86115
*/
87116
void
88-
pgstat_flush_backend(bool nowait)
117+
pgstat_flush_backend(bool nowait, bits32 flags)
89118
{
90119
PgStat_EntryRef *entry_ref;
91120

@@ -94,7 +123,7 @@ pgstat_flush_backend(bool nowait)
94123

95124
entry_ref = pgstat_get_entry_ref(PGSTAT_KIND_BACKEND, InvalidOid,
96125
MyProcNumber, false, NULL);
97-
(void) pgstat_backend_flush_cb(entry_ref, nowait);
126+
(void) pgstat_flush_backend_entry(entry_ref, nowait, flags);
98127
}
99128

100129
/*
@@ -119,9 +148,9 @@ pgstat_create_backend(ProcNumber procnum)
119148
}
120149

121150
/*
122-
* Find or create a local PgStat_BackendPendingIO entry for proc number.
151+
* Find or create a local PgStat_BackendPending entry for proc number.
123152
*/
124-
PgStat_BackendPendingIO *
153+
PgStat_BackendPending *
125154
pgstat_prep_backend_pending(ProcNumber procnum)
126155
{
127156
PgStat_EntryRef *entry_ref;

src/backend/utils/activity/pgstat_io.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ pgstat_count_io_op_n(IOObject io_object, IOContext io_context, IOOp io_op, uint3
8181

8282
if (pgstat_tracks_backend_bktype(MyBackendType))
8383
{
84-
PgStat_PendingIO *entry_ref;
84+
PgStat_BackendPending *entry_ref;
8585

8686
entry_ref = pgstat_prep_backend_pending(MyProcNumber);
87-
entry_ref->counts[io_object][io_context][io_op] += cnt;
87+
entry_ref->pending_io.counts[io_object][io_context][io_op] += cnt;
8888
}
8989

9090
PendingIOStats.counts[io_object][io_context][io_op] += cnt;
@@ -151,10 +151,10 @@ pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op,
151151

152152
if (pgstat_tracks_backend_bktype(MyBackendType))
153153
{
154-
PgStat_PendingIO *entry_ref;
154+
PgStat_BackendPending *entry_ref;
155155

156156
entry_ref = pgstat_prep_backend_pending(MyProcNumber);
157-
INSTR_TIME_ADD(entry_ref->pending_times[io_object][io_context][io_op],
157+
INSTR_TIME_ADD(entry_ref->pending_io.pending_times[io_object][io_context][io_op],
158158
io_time);
159159
}
160160
}

src/backend/utils/activity/pgstat_relation.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +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);
267+
pgstat_flush_backend(false, PGSTAT_BACKEND_FLUSH_IO);
268268
}
269269

270270
/*
@@ -351,7 +351,7 @@ pgstat_report_analyze(Relation rel,
351351

352352
/* see pgstat_report_vacuum() */
353353
pgstat_flush_io(false);
354-
pgstat_flush_backend(false);
354+
pgstat_flush_backend(false, PGSTAT_BACKEND_FLUSH_IO);
355355
}
356356

357357
/*

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ pg_stat_get_backend_io(PG_FUNCTION_ARGS)
15441544
if (bktype == B_INVALID)
15451545
return (Datum) 0;
15461546

1547-
bktype_stats = &backend_stats->stats;
1547+
bktype_stats = &backend_stats->io_stats;
15481548

15491549
/*
15501550
* In Assert builds, we can afford an extra loop through all of the

src/include/pgstat.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,24 @@ typedef struct PgStat_IO
375375
PgStat_BktypeIO stats[BACKEND_NUM_TYPES];
376376
} PgStat_IO;
377377

378-
/* Backend statistics store the same amount of IO data as PGSTAT_KIND_IO */
379-
typedef PgStat_PendingIO PgStat_BackendPendingIO;
380-
381378
typedef struct PgStat_Backend
382379
{
383380
TimestampTz stat_reset_timestamp;
384-
PgStat_BktypeIO stats;
381+
PgStat_BktypeIO io_stats;
385382
} PgStat_Backend;
386383

384+
/* ---------
385+
* PgStat_BackendPending Non-flushed backend stats.
386+
* ---------
387+
*/
388+
typedef struct PgStat_BackendPending
389+
{
390+
/*
391+
* Backend statistics store the same amount of IO data as PGSTAT_KIND_IO.
392+
*/
393+
PgStat_PendingIO pending_io;
394+
} PgStat_BackendPending;
395+
387396
typedef struct PgStat_StatDBEntry
388397
{
389398
PgStat_Counter xact_commit;

src/include/utils/pgstat_internal.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,9 +613,12 @@ extern void pgstat_archiver_snapshot_cb(void);
613613
* Functions in pgstat_backend.c
614614
*/
615615

616-
extern void pgstat_flush_backend(bool nowait);
616+
/* flags for pgstat_flush_backend() */
617+
#define PGSTAT_BACKEND_FLUSH_IO (1 << 0) /* Flush I/O statistics */
618+
#define PGSTAT_BACKEND_FLUSH_ALL (PGSTAT_BACKEND_FLUSH_IO)
617619

618-
extern PgStat_BackendPendingIO *pgstat_prep_backend_pending(ProcNumber procnum);
620+
extern void pgstat_flush_backend(bool nowait, bits32 flags);
621+
extern PgStat_BackendPending *pgstat_prep_backend_pending(ProcNumber procnum);
619622
extern bool pgstat_backend_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
620623
extern void pgstat_backend_reset_timestamp_cb(PgStatShared_Common *header, TimestampTz ts);
621624

src/tools/pgindent/typedefs.list

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2140,7 +2140,7 @@ PgStatShared_Subscription
21402140
PgStatShared_Wal
21412141
PgStat_ArchiverStats
21422142
PgStat_Backend
2143-
PgStat_BackendPendingIO
2143+
PgStat_BackendPending
21442144
PgStat_BackendSubEntry
21452145
PgStat_BgWriterStats
21462146
PgStat_BktypeIO

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy