PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgstat_backend.c File Reference
#include "postgres.h"
#include "access/xlog.h"
#include "executor/instrument.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, uint32 flags)
 
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 315 of file pgstat_backend.c.

316{
318}
bool pgstat_flush_backend(bool nowait, uint32 flags)
#define PGSTAT_BACKEND_FLUSH_ALL

References PGSTAT_BACKEND_FLUSH_ALL, and pgstat_flush_backend().

◆ pgstat_backend_reset_timestamp_cb()

void pgstat_backend_reset_timestamp_cb ( PgStatShared_Common header,
TimestampTz  ts 
)

Definition at line 404 of file pgstat_backend.c.

405{
406 ((PgStatShared_Backend *) header)->stats.stat_reset_timestamp = ts;
407}

◆ pgstat_backend_wal_have_pending()

static bool pgstat_backend_wal_have_pending ( void  )
inlinestatic

Definition at line 217 of file pgstat_backend.c.

218{
220}
WalUsage pgWalUsage
Definition instrument.c:27
static WalUsage prevBackendWalUsage
int64 wal_records
Definition instrument.h:53

References pgWalUsage, prevBackendWalUsage, and WalUsage::wal_records.

Referenced by 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 74 of file pgstat_backend.c.

76{
78 return;
79
81
84
87}
#define Assert(condition)
Definition c.h:943
BackendType MyBackendType
Definition miscinit.c:65
bool pgstat_report_fixed
Definition pgstat.c:219
static PgStat_BackendPending PendingBackendStats
bool pgstat_tracks_backend_bktype(BackendType bktype)
static bool backend_has_iostats
bool pgstat_tracks_io_op(BackendType bktype, IOObject io_object, IOContext io_context, IOOp io_op)
Definition pgstat_io.c:479
static int fb(int x)
PgStat_PendingIO pending_io
Definition pgstat.h:537
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition pgstat.h:339
uint64 bytes[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition pgstat.h:338

References Assert, backend_has_iostats, PgStat_PendingIO::bytes, PgStat_PendingIO::counts, fb(), MyBackendType, PgStat_BackendPending::pending_io, PendingBackendStats, pgstat_report_fixed, 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 
)

◆ pgstat_create_backend()

void pgstat_create_backend ( ProcNumber  procnum)

Definition at line 324 of file pgstat_backend.c.

325{
326 PgStat_EntryRef *entry_ref;
328
330 procnum, false);
332
333 /*
334 * NB: need to accept that there might be stats from an older backend,
335 * e.g. if we previously used this proc number.
336 */
337 memset(&shstatent->stats, 0, sizeof(shstatent->stats));
338 pgstat_unlock_entry(entry_ref);
339
341 backend_has_iostats = false;
342
343 /*
344 * Initialize prevBackendWalUsage with pgWalUsage so that
345 * pgstat_backend_flush_cb() can calculate how much pgWalUsage counters
346 * are increased by subtracting prevBackendWalUsage from pgWalUsage.
347 */
349}
#define MemSet(start, val, len)
Definition c.h:1107
#define PGSTAT_KIND_BACKEND
Definition pgstat_kind.h:32
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid, bool nowait)
#define InvalidOid
PgStatShared_Common * shared_stats

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

Referenced by pgstat_bestart_final().

◆ pgstat_fetch_stat_backend()

PgStat_Backend * pgstat_fetch_stat_backend ( ProcNumber  procNumber)

Definition at line 93 of file pgstat_backend.c.

94{
96
98 InvalidOid, procNumber,
99 NULL);
100
101 return backend_entry;
102}
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *may_free)
Definition pgstat.c:962

References fb(), 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 112 of file pgstat_backend.c.

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

References AuxiliaryPidGetProc(), B_INVALID, BackendPidGetProc(), fb(), GetNumberFromPGProc, pgstat_fetch_stat_backend(), pgstat_get_beentry_by_proc_number(), and pgstat_tracks_backend_bktype().

Referenced by pg_stat_get_backend_io(), and pg_stat_get_backend_wal().

◆ pgstat_flush_backend()

bool pgstat_flush_backend ( bool  nowait,
uint32  flags 
)

Definition at line 272 of file pgstat_backend.c.

273{
274 PgStat_EntryRef *entry_ref;
275 bool has_pending_data = false;
276
278 return false;
279
280 /* Some IO data pending? */
282 has_pending_data = true;
283
284 /* Some WAL data pending? */
285 if ((flags & PGSTAT_BACKEND_FLUSH_WAL) &&
287 has_pending_data = true;
288
289 if (!has_pending_data)
290 return false;
291
293 MyProcNumber, nowait);
294 if (!entry_ref)
295 return true;
296
297 /* Flush requested statistics */
298 if (flags & PGSTAT_BACKEND_FLUSH_IO)
300
301 if (flags & PGSTAT_BACKEND_FLUSH_WAL)
303
304 pgstat_unlock_entry(entry_ref);
305
306 return false;
307}
ProcNumber MyProcNumber
Definition globals.c:92
static void pgstat_flush_backend_entry_wal(PgStat_EntryRef *entry_ref)
static void pgstat_flush_backend_entry_io(PgStat_EntryRef *entry_ref)
static bool pgstat_backend_wal_have_pending(void)
#define PGSTAT_BACKEND_FLUSH_IO
#define PGSTAT_BACKEND_FLUSH_WAL

References backend_has_iostats, fb(), 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 167 of file pgstat_backend.c.

168{
171 PgStat_PendingIO pending_io;
172
173 /*
174 * This function can be called even if nothing at all has happened for IO
175 * statistics. In this case, avoid unnecessarily modifying the stats
176 * entry.
177 */
179 return;
180
182 bktype_shstats = &shbackendent->stats.io_stats;
183 pending_io = PendingBackendStats.pending_io;
184
186 {
188 {
189 for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
190 {
191 instr_time time;
192
194 pending_io.counts[io_object][io_context][io_op];
196 pending_io.bytes[io_object][io_context][io_op];
197 time = pending_io.pending_times[io_object][io_context][io_op];
198
201 }
202 }
203 }
204
205 /*
206 * Clear out the statistics buffer, so it can be re-used.
207 */
209
210 backend_has_iostats = false;
211}
#define INSTR_TIME_GET_MICROSEC(t)
Definition instr_time.h:454
#define IOOP_NUM_TYPES
Definition pgstat.h:323
#define IOCONTEXT_NUM_TYPES
Definition pgstat.h:297
#define IOOBJECT_NUM_TYPES
Definition pgstat.h:286

References backend_has_iostats, PgStat_PendingIO::bytes, PgStat_PendingIO::counts, fb(), INSTR_TIME_GET_MICROSEC, IOCONTEXT_NUM_TYPES, IOOBJECT_NUM_TYPES, IOOP_NUM_TYPES, MemSet, PgStat_BackendPending::pending_io, PgStat_PendingIO::pending_times, PendingBackendStats, and PgStat_EntryRef::shared_stats.

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 227 of file pgstat_backend.c.

228{
232
233 /*
234 * This function can be called even if nothing at all has happened for WAL
235 * statistics. In this case, avoid unnecessarily modifying the stats
236 * entry.
237 */
239 return;
240
242 bktype_shstats = &shbackendent->stats.wal_counters;
243
244 /*
245 * Calculate how much WAL usage counters were increased by subtracting the
246 * previous counters from the current ones.
247 */
249
250#define WALSTAT_ACC(fld, var_to_add) \
251 (bktype_shstats->fld += var_to_add.fld)
252 WALSTAT_ACC(wal_buffers_full, wal_usage_diff);
253 WALSTAT_ACC(wal_records, wal_usage_diff);
254 WALSTAT_ACC(wal_fpi, wal_usage_diff);
255 WALSTAT_ACC(wal_bytes, wal_usage_diff);
256 WALSTAT_ACC(wal_fpi_bytes, wal_usage_diff);
257#undef WALSTAT_ACC
258
259 /*
260 * Save the current counters for the subsequent calculation of WAL usage.
261 */
263}
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition instrument.c:367
#define WALSTAT_ACC(fld, var_to_add)

References fb(), pgstat_backend_wal_have_pending(), pgWalUsage, prevBackendWalUsage, PgStat_EntryRef::shared_stats, WALSTAT_ACC, and WalUsageAccumDiff().

Referenced by pgstat_flush_backend().

◆ pgstat_tracks_backend_bktype()

bool pgstat_tracks_backend_bktype ( BackendType  bktype)

Definition at line 367 of file pgstat_backend.c.

368{
369 /*
370 * List every type so that new backend types trigger a warning about
371 * needing to adjust this switch.
372 */
373 switch (bktype)
374 {
375 case B_INVALID:
378 case B_ARCHIVER:
379 case B_LOGGER:
380 case B_BG_WRITER:
381 case B_CHECKPOINTER:
382 case B_IO_WORKER:
383 case B_STARTUP:
386 return false;
387
388 case B_AUTOVAC_WORKER:
389 case B_BACKEND:
390 case B_BG_WORKER:
393 case B_WAL_RECEIVER:
394 case B_WAL_SENDER:
395 case B_WAL_SUMMARIZER:
396 case B_WAL_WRITER:
397 return true;
398 }
399
400 return false;
401}
@ B_WAL_SUMMARIZER
Definition miscadmin.h:379
@ B_WAL_WRITER
Definition miscadmin.h:380
@ B_WAL_RECEIVER
Definition miscadmin.h:378
@ B_CHECKPOINTER
Definition miscadmin.h:375
@ B_DATACHECKSUMSWORKER_WORKER
Definition miscadmin.h:383
@ B_WAL_SENDER
Definition miscadmin.h:359
@ B_IO_WORKER
Definition miscadmin.h:376
@ B_LOGGER
Definition miscadmin.h:389
@ B_STARTUP
Definition miscadmin.h:377
@ B_DATACHECKSUMSWORKER_LAUNCHER
Definition miscadmin.h:382
@ B_BG_WORKER
Definition miscadmin.h:358
@ B_STANDALONE_BACKEND
Definition miscadmin.h:362
@ B_BG_WRITER
Definition miscadmin.h:374
@ B_BACKEND
Definition miscadmin.h:354
@ B_ARCHIVER
Definition miscadmin.h:373
@ B_AUTOVAC_LAUNCHER
Definition miscadmin.h:356
@ B_SLOTSYNC_WORKER
Definition miscadmin.h:360
@ B_DEAD_END_BACKEND
Definition miscadmin.h:355
@ B_AUTOVAC_WORKER
Definition miscadmin.h:357

References B_ARCHIVER, B_AUTOVAC_LAUNCHER, B_AUTOVAC_WORKER, B_BACKEND, B_BG_WORKER, B_BG_WRITER, B_CHECKPOINTER, B_DATACHECKSUMSWORKER_LAUNCHER, B_DATACHECKSUMSWORKER_WORKER, 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, B_WAL_WRITER, and fb().

Referenced by pg_stat_reset_backend_stats(), 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