PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pgstat_backend.c File Reference
#include "postgres.h"
#include "access/xlog.h"
#include "storage/bufmgr.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/memutils.h"
#include "utils/pgstat_internal.h"
Include dependency graph for pgstat_backend.c:

Go to the source code of this file.

Macros

#define WALSTAT_ACC(fld, var_to_add)    (bktype_shstats->fld += var_to_add.fld)
 

Functions

void pgstat_count_backend_io_op_time (IOObject io_object, IOContext io_context, IOOp io_op, instr_time io_time)
 
void pgstat_count_backend_io_op (IOObject io_object, IOContext io_context, IOOp io_op, uint32 cnt, uint64 bytes)
 
PgStat_Backendpgstat_fetch_stat_backend (ProcNumber procNumber)
 
PgStat_Backendpgstat_fetch_stat_backend_by_pid (int pid, BackendType *bktype)
 
static void pgstat_flush_backend_entry_io (PgStat_EntryRef *entry_ref)
 
static bool pgstat_backend_wal_have_pending (void)
 
static void pgstat_flush_backend_entry_wal (PgStat_EntryRef *entry_ref)
 
bool pgstat_flush_backend (bool nowait, bits32 flags)
 
bool pgstat_backend_have_pending_cb (void)
 
bool pgstat_backend_flush_cb (bool nowait)
 
void pgstat_create_backend (ProcNumber procnum)
 
bool pgstat_tracks_backend_bktype (BackendType bktype)
 
void pgstat_backend_reset_timestamp_cb (PgStatShared_Common *header, TimestampTz ts)
 

Variables

static PgStat_BackendPending PendingBackendStats
 
static bool backend_has_iostats = false
 
static WalUsage prevBackendWalUsage
 

Macro Definition Documentation

◆ WALSTAT_ACC

#define WALSTAT_ACC (   fld,
  var_to_add 
)     (bktype_shstats->fld += var_to_add.fld)

Function Documentation

◆ pgstat_backend_flush_cb()

bool pgstat_backend_flush_cb ( bool  nowait)

Definition at line 322 of file pgstat_backend.c.

323{
325}
bool pgstat_flush_backend(bool nowait, bits32 flags)
#define PGSTAT_BACKEND_FLUSH_ALL

References PGSTAT_BACKEND_FLUSH_ALL, and pgstat_flush_backend().

◆ pgstat_backend_have_pending_cb()

bool pgstat_backend_have_pending_cb ( void  )

Definition at line 308 of file pgstat_backend.c.

309{
311 return false;
312
314}
BackendType MyBackendType
Definition: miscinit.c:64
bool pgstat_tracks_backend_bktype(BackendType bktype)
static bool pgstat_backend_wal_have_pending(void)
static bool backend_has_iostats

References backend_has_iostats, MyBackendType, pgstat_backend_wal_have_pending(), and pgstat_tracks_backend_bktype().

◆ pgstat_backend_reset_timestamp_cb()

void pgstat_backend_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)

Definition at line 409 of file pgstat_backend.c.

410{
411 ((PgStatShared_Backend *) header)->stats.stat_reset_timestamp = ts;
412}

◆ pgstat_backend_wal_have_pending()

static bool pgstat_backend_wal_have_pending ( void  )
inlinestatic

Definition at line 213 of file pgstat_backend.c.

214{
216}
WalUsage pgWalUsage
Definition: instrument.c:22
static WalUsage prevBackendWalUsage
int64 wal_records
Definition: instrument.h:53

References pgWalUsage, prevBackendWalUsage, and WalUsage::wal_records.

Referenced by pgstat_backend_have_pending_cb(), pgstat_flush_backend(), and pgstat_flush_backend_entry_wal().

◆ pgstat_count_backend_io_op()

void pgstat_count_backend_io_op ( IOObject  io_object,
IOContext  io_context,
IOOp  io_op,
uint32  cnt,
uint64  bytes 
)

Definition at line 72 of file pgstat_backend.c.

74{
76 return;
77
78 Assert(pgstat_tracks_io_op(MyBackendType, io_object, io_context, io_op));
79
80 PendingBackendStats.pending_io.counts[io_object][io_context][io_op] += cnt;
81 PendingBackendStats.pending_io.bytes[io_object][io_context][io_op] += bytes;
82
84}
Assert(PointerIsAligned(start, uint64))
static PgStat_BackendPending PendingBackendStats
bool pgstat_tracks_io_op(BackendType bktype, IOObject io_object, IOContext io_context, IOOp io_op)
Definition: pgstat_io.c:485
PgStat_PendingIO pending_io
Definition: pgstat.h:504
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:332
uint64 bytes[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:331

References Assert(), backend_has_iostats, PgStat_PendingIO::bytes, PgStat_PendingIO::counts, MyBackendType, PgStat_BackendPending::pending_io, PendingBackendStats, pgstat_tracks_backend_bktype(), and pgstat_tracks_io_op().

Referenced by pgstat_count_io_op().

◆ pgstat_count_backend_io_op_time()

void pgstat_count_backend_io_op_time ( IOObject  io_object,
IOContext  io_context,
IOOp  io_op,
instr_time  io_time 
)

Definition at line 55 of file pgstat_backend.c.

57{
59
61 return;
62
63 Assert(pgstat_tracks_io_op(MyBackendType, io_object, io_context, io_op));
64
65 INSTR_TIME_ADD(PendingBackendStats.pending_io.pending_times[io_object][io_context][io_op],
66 io_time);
67
69}
bool track_io_timing
Definition: bufmgr.c:147
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:178
instr_time pending_times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:333
bool track_wal_io_timing
Definition: xlog.c:137

References Assert(), backend_has_iostats, INSTR_TIME_ADD, MyBackendType, PgStat_BackendPending::pending_io, PgStat_PendingIO::pending_times, PendingBackendStats, pgstat_tracks_backend_bktype(), pgstat_tracks_io_op(), track_io_timing, and track_wal_io_timing.

Referenced by pgstat_count_io_op_time().

◆ pgstat_create_backend()

void pgstat_create_backend ( ProcNumber  procnum)

Definition at line 331 of file pgstat_backend.c.

332{
333 PgStat_EntryRef *entry_ref;
334 PgStatShared_Backend *shstatent;
335
337 MyProcNumber, false);
338 shstatent = (PgStatShared_Backend *) entry_ref->shared_stats;
339
340 /*
341 * NB: need to accept that there might be stats from an older backend,
342 * e.g. if we previously used this proc number.
343 */
344 memset(&shstatent->stats, 0, sizeof(shstatent->stats));
345 pgstat_unlock_entry(entry_ref);
346
348 backend_has_iostats = false;
349
350 /*
351 * Initialize prevBackendWalUsage with pgWalUsage so that
352 * pgstat_backend_flush_cb() can calculate how much pgWalUsage counters
353 * are increased by subtracting prevBackendWalUsage from pgWalUsage.
354 */
356}
#define MemSet(start, val, len)
Definition: c.h:991
ProcNumber MyProcNumber
Definition: globals.c:91
#define PGSTAT_KIND_BACKEND
Definition: pgstat_kind.h:32
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:684
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
Definition: pgstat_shmem.c:693
#define InvalidOid
Definition: postgres_ext.h:35
PgStat_Backend stats
PgStatShared_Common * shared_stats

References backend_has_iostats, InvalidOid, MemSet, MyProcNumber, PendingBackendStats, pgstat_get_entry_ref_locked(), PGSTAT_KIND_BACKEND, pgstat_unlock_entry(), pgWalUsage, prevBackendWalUsage, PgStat_EntryRef::shared_stats, and PgStatShared_Backend::stats.

Referenced by pgstat_bestart_final().

◆ pgstat_fetch_stat_backend()

PgStat_Backend * pgstat_fetch_stat_backend ( ProcNumber  procNumber)

Definition at line 90 of file pgstat_backend.c.

91{
92 PgStat_Backend *backend_entry;
93
95 InvalidOid, procNumber);
96
97 return backend_entry;
98}
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
Definition: pgstat.c:949

References InvalidOid, pgstat_fetch_entry(), and PGSTAT_KIND_BACKEND.

Referenced by pgstat_fetch_stat_backend_by_pid().

◆ pgstat_fetch_stat_backend_by_pid()

PgStat_Backend * pgstat_fetch_stat_backend_by_pid ( int  pid,
BackendType bktype 
)

Definition at line 108 of file pgstat_backend.c.

109{
110 PGPROC *proc;
111 PgBackendStatus *beentry;
112 ProcNumber procNumber;
113 PgStat_Backend *backend_stats;
114
115 proc = BackendPidGetProc(pid);
116 if (bktype)
117 *bktype = B_INVALID;
118
119 /* this could be an auxiliary process */
120 if (!proc)
121 proc = AuxiliaryPidGetProc(pid);
122
123 if (!proc)
124 return NULL;
125
126 procNumber = GetNumberFromPGProc(proc);
127
128 beentry = pgstat_get_beentry_by_proc_number(procNumber);
129 if (!beentry)
130 return NULL;
131
132 /* check if the backend type tracks statistics */
134 return NULL;
135
136 /* if PID does not match, leave */
137 if (beentry->st_procpid != pid)
138 return NULL;
139
140 if (bktype)
141 *bktype = beentry->st_backendType;
142
143 /*
144 * Retrieve the entry. Note that "beentry" may be freed depending on the
145 * value of stats_fetch_consistency, so do not access it from this point.
146 */
147 backend_stats = pgstat_fetch_stat_backend(procNumber);
148 if (!backend_stats)
149 {
150 if (bktype)
151 *bktype = B_INVALID;
152 return NULL;
153 }
154
155 return backend_stats;
156}
PgBackendStatus * pgstat_get_beentry_by_proc_number(ProcNumber procNumber)
@ B_INVALID
Definition: miscadmin.h:339
PgStat_Backend * pgstat_fetch_stat_backend(ProcNumber procNumber)
#define GetNumberFromPGProc(proc)
Definition: proc.h:425
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3196
int ProcNumber
Definition: procnumber.h:24
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:1092
Definition: proc.h:163
BackendType st_backendType

References AuxiliaryPidGetProc(), B_INVALID, BackendPidGetProc(), GetNumberFromPGProc, pgstat_fetch_stat_backend(), pgstat_get_beentry_by_proc_number(), pgstat_tracks_backend_bktype(), PgBackendStatus::st_backendType, and PgBackendStatus::st_procpid.

Referenced by pg_stat_get_backend_io(), and pg_stat_get_backend_wal().

◆ pgstat_flush_backend()

bool pgstat_flush_backend ( bool  nowait,
bits32  flags 
)

Definition at line 267 of file pgstat_backend.c.

268{
269 PgStat_EntryRef *entry_ref;
270 bool has_pending_data = false;
271
273 return false;
274
275 /* Some IO data pending? */
277 has_pending_data = true;
278
279 /* Some WAL data pending? */
280 if ((flags & PGSTAT_BACKEND_FLUSH_WAL) &&
282 has_pending_data = true;
283
284 if (!has_pending_data)
285 return false;
286
288 MyProcNumber, nowait);
289 if (!entry_ref)
290 return true;
291
292 /* Flush requested statistics */
293 if (flags & PGSTAT_BACKEND_FLUSH_IO)
295
296 if (flags & PGSTAT_BACKEND_FLUSH_WAL)
298
299 pgstat_unlock_entry(entry_ref);
300
301 return false;
302}
static void pgstat_flush_backend_entry_wal(PgStat_EntryRef *entry_ref)
static void pgstat_flush_backend_entry_io(PgStat_EntryRef *entry_ref)
#define PGSTAT_BACKEND_FLUSH_IO
#define PGSTAT_BACKEND_FLUSH_WAL

References backend_has_iostats, InvalidOid, MyBackendType, MyProcNumber, PGSTAT_BACKEND_FLUSH_IO, PGSTAT_BACKEND_FLUSH_WAL, pgstat_backend_wal_have_pending(), pgstat_flush_backend_entry_io(), pgstat_flush_backend_entry_wal(), pgstat_get_entry_ref_locked(), PGSTAT_KIND_BACKEND, pgstat_tracks_backend_bktype(), and pgstat_unlock_entry().

Referenced by pgstat_backend_flush_cb(), pgstat_report_analyze(), pgstat_report_vacuum(), pgstat_report_wal(), WalSndLoop(), and WalSndWaitForWal().

◆ pgstat_flush_backend_entry_io()

static void pgstat_flush_backend_entry_io ( PgStat_EntryRef entry_ref)
static

Definition at line 163 of file pgstat_backend.c.

164{
165 PgStatShared_Backend *shbackendent;
166 PgStat_BktypeIO *bktype_shstats;
167 PgStat_PendingIO pending_io;
168
169 /*
170 * This function can be called even if nothing at all has happened for IO
171 * statistics. In this case, avoid unnecessarily modifying the stats
172 * entry.
173 */
175 return;
176
177 shbackendent = (PgStatShared_Backend *) entry_ref->shared_stats;
178 bktype_shstats = &shbackendent->stats.io_stats;
179 pending_io = PendingBackendStats.pending_io;
180
181 for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
182 {
183 for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
184 {
185 for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
186 {
187 instr_time time;
188
189 bktype_shstats->counts[io_object][io_context][io_op] +=
190 pending_io.counts[io_object][io_context][io_op];
191 bktype_shstats->bytes[io_object][io_context][io_op] +=
192 pending_io.bytes[io_object][io_context][io_op];
193 time = pending_io.pending_times[io_object][io_context][io_op];
194
195 bktype_shstats->times[io_object][io_context][io_op] +=
197 }
198 }
199 }
200
201 /*
202 * Clear out the statistics buffer, so it can be re-used.
203 */
205
206 backend_has_iostats = false;
207}
for(;;)
#define INSTR_TIME_GET_MICROSEC(t)
Definition: instr_time.h:194
#define IOOP_NUM_TYPES
Definition: pgstat.h:316
#define IOCONTEXT_NUM_TYPES
Definition: pgstat.h:290
#define IOOBJECT_NUM_TYPES
Definition: pgstat.h:279
PgStat_BktypeIO io_stats
Definition: pgstat.h:491
PgStat_Counter times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:326
uint64 bytes[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:324
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:325

References backend_has_iostats, PgStat_BktypeIO::bytes, PgStat_PendingIO::bytes, PgStat_BktypeIO::counts, PgStat_PendingIO::counts, for(), INSTR_TIME_GET_MICROSEC, PgStat_Backend::io_stats, IOCONTEXT_NUM_TYPES, IOOBJECT_NUM_TYPES, IOOP_NUM_TYPES, MemSet, PgStat_BackendPending::pending_io, PgStat_PendingIO::pending_times, PendingBackendStats, PgStat_EntryRef::shared_stats, PgStatShared_Backend::stats, and PgStat_BktypeIO::times.

Referenced by pgstat_flush_backend().

◆ pgstat_flush_backend_entry_wal()

static void pgstat_flush_backend_entry_wal ( PgStat_EntryRef entry_ref)
static

Definition at line 223 of file pgstat_backend.c.

224{
225 PgStatShared_Backend *shbackendent;
226 PgStat_WalCounters *bktype_shstats;
227 WalUsage wal_usage_diff = {0};
228
229 /*
230 * This function can be called even if nothing at all has happened for WAL
231 * statistics. In this case, avoid unnecessarily modifying the stats
232 * entry.
233 */
235 return;
236
237 shbackendent = (PgStatShared_Backend *) entry_ref->shared_stats;
238 bktype_shstats = &shbackendent->stats.wal_counters;
239
240 /*
241 * Calculate how much WAL usage counters were increased by subtracting the
242 * previous counters from the current ones.
243 */
245
246#define WALSTAT_ACC(fld, var_to_add) \
247 (bktype_shstats->fld += var_to_add.fld)
248 WALSTAT_ACC(wal_buffers_full, wal_usage_diff);
249 WALSTAT_ACC(wal_records, wal_usage_diff);
250 WALSTAT_ACC(wal_fpi, wal_usage_diff);
251 WALSTAT_ACC(wal_bytes, wal_usage_diff);
252#undef WALSTAT_ACC
253
254 /*
255 * Save the current counters for the subsequent calculation of WAL usage.
256 */
258}
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition: instrument.c:287
#define WALSTAT_ACC(fld, var_to_add)
PgStat_WalCounters wal_counters
Definition: pgstat.h:492

References pgstat_backend_wal_have_pending(), pgWalUsage, prevBackendWalUsage, PgStat_EntryRef::shared_stats, PgStatShared_Backend::stats, PgStat_Backend::wal_counters, WALSTAT_ACC, and WalUsageAccumDiff().

Referenced by pgstat_flush_backend().

◆ pgstat_tracks_backend_bktype()

bool pgstat_tracks_backend_bktype ( BackendType  bktype)

Definition at line 374 of file pgstat_backend.c.

375{
376 /*
377 * List every type so that new backend types trigger a warning about
378 * needing to adjust this switch.
379 */
380 switch (bktype)
381 {
382 case B_INVALID:
385 case B_ARCHIVER:
386 case B_LOGGER:
387 case B_BG_WRITER:
388 case B_CHECKPOINTER:
389 case B_IO_WORKER:
390 case B_STARTUP:
391 return false;
392
393 case B_AUTOVAC_WORKER:
394 case B_BACKEND:
395 case B_BG_WORKER:
398 case B_WAL_RECEIVER:
399 case B_WAL_SENDER:
400 case B_WAL_SUMMARIZER:
401 case B_WAL_WRITER:
402 return true;
403 }
404
405 return false;
406}
@ B_WAL_SUMMARIZER
Definition: miscadmin.h:367
@ B_WAL_WRITER
Definition: miscadmin.h:368
@ B_WAL_RECEIVER
Definition: miscadmin.h:366
@ B_CHECKPOINTER
Definition: miscadmin.h:363
@ B_WAL_SENDER
Definition: miscadmin.h:347
@ B_IO_WORKER
Definition: miscadmin.h:364
@ B_LOGGER
Definition: miscadmin.h:374
@ B_STARTUP
Definition: miscadmin.h:365
@ B_BG_WORKER
Definition: miscadmin.h:346
@ B_STANDALONE_BACKEND
Definition: miscadmin.h:350
@ B_BG_WRITER
Definition: miscadmin.h:362
@ B_BACKEND
Definition: miscadmin.h:342
@ B_ARCHIVER
Definition: miscadmin.h:361
@ B_AUTOVAC_LAUNCHER
Definition: miscadmin.h:344
@ B_SLOTSYNC_WORKER
Definition: miscadmin.h:348
@ B_DEAD_END_BACKEND
Definition: miscadmin.h:343
@ B_AUTOVAC_WORKER
Definition: miscadmin.h:345

References B_ARCHIVER, B_AUTOVAC_LAUNCHER, B_AUTOVAC_WORKER, B_BACKEND, B_BG_WORKER, B_BG_WRITER, B_CHECKPOINTER, B_DEAD_END_BACKEND, B_INVALID, B_IO_WORKER, B_LOGGER, B_SLOTSYNC_WORKER, B_STANDALONE_BACKEND, B_STARTUP, B_WAL_RECEIVER, B_WAL_SENDER, B_WAL_SUMMARIZER, and B_WAL_WRITER.

Referenced by pg_stat_reset_backend_stats(), pgstat_backend_have_pending_cb(), pgstat_bestart_final(), pgstat_count_backend_io_op(), pgstat_count_backend_io_op_time(), pgstat_fetch_stat_backend_by_pid(), and pgstat_flush_backend().

Variable Documentation

◆ backend_has_iostats

◆ PendingBackendStats

◆ prevBackendWalUsage

WalUsage prevBackendWalUsage
static