PostgreSQL Source Code git master
pgstat_io.c File Reference
#include "postgres.h"
#include "executor/instrument.h"
#include "storage/bufmgr.h"
#include "utils/pgstat_internal.h"
Include dependency graph for pgstat_io.c:

Go to the source code of this file.

Functions

bool pgstat_bktype_io_stats_valid (PgStat_BktypeIO *backend_io, BackendType bktype)
 
void pgstat_count_io_op (IOObject io_object, IOContext io_context, IOOp io_op, uint32 cnt, uint64 bytes)
 
instr_time pgstat_prepare_io_time (bool track_io_guc)
 
void pgstat_count_io_op_time (IOObject io_object, IOContext io_context, IOOp io_op, instr_time start_time, uint32 cnt, uint64 bytes)
 
PgStat_IOpgstat_fetch_stat_io (void)
 
bool pgstat_io_have_pending_cb (void)
 
void pgstat_flush_io (bool nowait)
 
bool pgstat_io_flush_cb (bool nowait)
 
const char * pgstat_get_io_context_name (IOContext io_context)
 
const char * pgstat_get_io_object_name (IOObject io_object)
 
void pgstat_io_init_shmem_cb (void *stats)
 
void pgstat_io_reset_all_cb (TimestampTz ts)
 
void pgstat_io_snapshot_cb (void)
 
bool pgstat_tracks_io_bktype (BackendType bktype)
 
bool pgstat_tracks_io_object (BackendType bktype, IOObject io_object, IOContext io_context)
 
bool pgstat_tracks_io_op (BackendType bktype, IOObject io_object, IOContext io_context, IOOp io_op)
 

Variables

static PgStat_PendingIO PendingIOStats
 
static bool have_iostats = false
 

Function Documentation

◆ pgstat_bktype_io_stats_valid()

bool pgstat_bktype_io_stats_valid ( PgStat_BktypeIO backend_io,
BackendType  bktype 
)

Definition at line 37 of file pgstat_io.c.

39{
40 for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
41 {
42 for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
43 {
44 for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
45 {
46 /* we do track it */
47 if (pgstat_tracks_io_op(bktype, io_object, io_context, io_op))
48 {
49 /* ensure that if IO times are non-zero, counts are > 0 */
50 if (backend_io->times[io_object][io_context][io_op] != 0 &&
51 backend_io->counts[io_object][io_context][io_op] <= 0)
52 return false;
53
54 continue;
55 }
56
57 /* we don't track it, and it is not 0 */
58 if (backend_io->counts[io_object][io_context][io_op] != 0)
59 return false;
60 }
61 }
62 }
63
64 return true;
65}
#define IOOP_NUM_TYPES
Definition: pgstat.h:317
#define IOCONTEXT_NUM_TYPES
Definition: pgstat.h:291
#define IOOBJECT_NUM_TYPES
Definition: pgstat.h:280
bool pgstat_tracks_io_op(BackendType bktype, IOObject io_object, IOContext io_context, IOOp io_op)
Definition: pgstat_io.c:473
PgStat_Counter times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:327
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:326

References PgStat_BktypeIO::counts, IOCONTEXT_NUM_TYPES, IOOBJECT_NUM_TYPES, IOOP_NUM_TYPES, pgstat_tracks_io_op(), and PgStat_BktypeIO::times.

Referenced by pg_stat_get_backend_io(), pg_stat_get_io(), and pgstat_io_flush_cb().

◆ pgstat_count_io_op()

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

Definition at line 68 of file pgstat_io.c.

70{
71 Assert((unsigned int) io_object < IOOBJECT_NUM_TYPES);
72 Assert((unsigned int) io_context < IOCONTEXT_NUM_TYPES);
73 Assert(pgstat_is_ioop_tracked_in_bytes(io_op) || bytes == 0);
74 Assert(pgstat_tracks_io_op(MyBackendType, io_object, io_context, io_op));
75
76 PendingIOStats.counts[io_object][io_context][io_op] += cnt;
77 PendingIOStats.bytes[io_object][io_context][io_op] += bytes;
78
79 /* Add the per-backend counts */
80 pgstat_count_backend_io_op(io_object, io_context, io_op, cnt, bytes);
81
82 have_iostats = true;
83}
#define Assert(condition)
Definition: c.h:815
BackendType MyBackendType
Definition: miscinit.c:64
#define pgstat_is_ioop_tracked_in_bytes(io_op)
Definition: pgstat.h:319
void pgstat_count_backend_io_op(IOObject io_object, IOContext io_context, IOOp io_op, uint32 cnt, uint64 bytes)
static PgStat_PendingIO PendingIOStats
Definition: pgstat_io.c:23
static bool have_iostats
Definition: pgstat_io.c:24
PgStat_Counter counts[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:333
uint64 bytes[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:332

References Assert, PgStat_PendingIO::bytes, PgStat_PendingIO::counts, have_iostats, IOCONTEXT_NUM_TYPES, IOOBJECT_NUM_TYPES, MyBackendType, PendingIOStats, pgstat_count_backend_io_op(), pgstat_is_ioop_tracked_in_bytes, and pgstat_tracks_io_op().

Referenced by GetLocalVictimBuffer(), GetVictimBuffer(), pgstat_count_io_op_time(), and PinBufferForBlock().

◆ pgstat_count_io_op_time()

void pgstat_count_io_op_time ( IOObject  io_object,
IOContext  io_context,
IOOp  io_op,
instr_time  start_time,
uint32  cnt,
uint64  bytes 
)

Definition at line 120 of file pgstat_io.c.

122{
123 if (track_io_timing)
124 {
125 instr_time io_time;
126
127 INSTR_TIME_SET_CURRENT(io_time);
129
130 if (io_object != IOOBJECT_WAL)
131 {
132 if (io_op == IOOP_WRITE || io_op == IOOP_EXTEND)
133 {
135 if (io_object == IOOBJECT_RELATION)
137 else if (io_object == IOOBJECT_TEMP_RELATION)
139 }
140 else if (io_op == IOOP_READ)
141 {
143 if (io_object == IOOBJECT_RELATION)
145 else if (io_object == IOOBJECT_TEMP_RELATION)
147 }
148 }
149
150 INSTR_TIME_ADD(PendingIOStats.pending_times[io_object][io_context][io_op],
151 io_time);
152
153 /* Add the per-backend count */
154 pgstat_count_backend_io_op_time(io_object, io_context, io_op,
155 io_time);
156 }
157
158 pgstat_count_io_op(io_object, io_context, io_op, cnt, bytes);
159}
bool track_io_timing
Definition: bufmgr.c:143
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:122
#define INSTR_TIME_ADD(x, y)
Definition: instr_time.h:178
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:181
#define INSTR_TIME_GET_MICROSEC(t)
Definition: instr_time.h:194
BufferUsage pgBufferUsage
Definition: instrument.c:20
static time_t start_time
Definition: pg_ctl.c:95
#define pgstat_count_buffer_read_time(n)
Definition: pgstat.h:617
@ IOOBJECT_RELATION
Definition: pgstat.h:275
@ IOOBJECT_WAL
Definition: pgstat.h:277
@ IOOBJECT_TEMP_RELATION
Definition: pgstat.h:276
#define pgstat_count_buffer_write_time(n)
Definition: pgstat.h:619
@ IOOP_EXTEND
Definition: pgstat.h:312
@ IOOP_READ
Definition: pgstat.h:313
@ IOOP_WRITE
Definition: pgstat.h:314
void pgstat_count_backend_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op, instr_time io_time)
void pgstat_count_io_op(IOObject io_object, IOContext io_context, IOOp io_op, uint32 cnt, uint64 bytes)
Definition: pgstat_io.c:68
instr_time local_blk_read_time
Definition: instrument.h:38
instr_time shared_blk_read_time
Definition: instrument.h:36
instr_time shared_blk_write_time
Definition: instrument.h:37
instr_time local_blk_write_time
Definition: instrument.h:39
instr_time pending_times[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:334

References INSTR_TIME_ADD, INSTR_TIME_GET_MICROSEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, IOOBJECT_RELATION, IOOBJECT_TEMP_RELATION, IOOBJECT_WAL, IOOP_EXTEND, IOOP_READ, IOOP_WRITE, BufferUsage::local_blk_read_time, BufferUsage::local_blk_write_time, PgStat_PendingIO::pending_times, PendingIOStats, pgBufferUsage, pgstat_count_backend_io_op_time(), pgstat_count_buffer_read_time, pgstat_count_buffer_write_time, pgstat_count_io_op(), BufferUsage::shared_blk_read_time, BufferUsage::shared_blk_write_time, start_time, and track_io_timing.

Referenced by ExtendBufferedRelLocal(), ExtendBufferedRelShared(), FlushBuffer(), FlushRelationBuffers(), GetLocalVictimBuffer(), issue_xlog_fsync(), IssuePendingWritebacks(), mdsyncfiletag(), register_dirty_segment(), WaitReadBuffers(), WALRead(), XLogFileInitInternal(), XLogPageRead(), and XLogWrite().

◆ pgstat_fetch_stat_io()

PgStat_IO * pgstat_fetch_stat_io ( void  )

Definition at line 162 of file pgstat_io.c.

163{
165
166 return &pgStatLocal.snapshot.io;
167}
void pgstat_snapshot_fixed(PgStat_Kind kind)
Definition: pgstat.c:1079
PgStat_LocalState pgStatLocal
Definition: pgstat.c:213
#define PGSTAT_KIND_IO
Definition: pgstat_kind.h:38
PgStat_Snapshot snapshot

References PgStat_Snapshot::io, PGSTAT_KIND_IO, pgstat_snapshot_fixed(), pgStatLocal, and PgStat_LocalState::snapshot.

Referenced by pg_stat_get_io().

◆ pgstat_flush_io()

void pgstat_flush_io ( bool  nowait)

Definition at line 182 of file pgstat_io.c.

183{
184 (void) pgstat_io_flush_cb(nowait);
185}
bool pgstat_io_flush_cb(bool nowait)
Definition: pgstat_io.c:196

References pgstat_io_flush_cb().

Referenced by pgstat_report_analyze(), pgstat_report_bgwriter(), pgstat_report_checkpointer(), pgstat_report_vacuum(), and pgstat_report_wal().

◆ pgstat_get_io_context_name()

const char * pgstat_get_io_context_name ( IOContext  io_context)

Definition at line 247 of file pgstat_io.c.

248{
249 switch (io_context)
250 {
252 return "bulkread";
254 return "bulkwrite";
255 case IOCONTEXT_INIT:
256 return "init";
257 case IOCONTEXT_NORMAL:
258 return "normal";
259 case IOCONTEXT_VACUUM:
260 return "vacuum";
261 }
262
263 elog(ERROR, "unrecognized IOContext value: %d", io_context);
265}
#define pg_unreachable()
Definition: c.h:318
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
@ IOCONTEXT_INIT
Definition: pgstat.h:286
@ IOCONTEXT_NORMAL
Definition: pgstat.h:287
@ IOCONTEXT_VACUUM
Definition: pgstat.h:288
@ IOCONTEXT_BULKREAD
Definition: pgstat.h:284
@ IOCONTEXT_BULKWRITE
Definition: pgstat.h:285

References elog, ERROR, IOCONTEXT_BULKREAD, IOCONTEXT_BULKWRITE, IOCONTEXT_INIT, IOCONTEXT_NORMAL, IOCONTEXT_VACUUM, and pg_unreachable.

Referenced by pg_stat_io_build_tuples().

◆ pgstat_get_io_object_name()

const char * pgstat_get_io_object_name ( IOObject  io_object)

Definition at line 268 of file pgstat_io.c.

269{
270 switch (io_object)
271 {
273 return "relation";
275 return "temp relation";
276 case IOOBJECT_WAL:
277 return "wal";
278 }
279
280 elog(ERROR, "unrecognized IOObject value: %d", io_object);
282}

References elog, ERROR, IOOBJECT_RELATION, IOOBJECT_TEMP_RELATION, IOOBJECT_WAL, and pg_unreachable.

Referenced by pg_stat_io_build_tuples().

◆ pgstat_io_flush_cb()

bool pgstat_io_flush_cb ( bool  nowait)

Definition at line 196 of file pgstat_io.c.

197{
198 LWLock *bktype_lock;
199 PgStat_BktypeIO *bktype_shstats;
200
201 if (!have_iostats)
202 return false;
203
204 bktype_lock = &pgStatLocal.shmem->io.locks[MyBackendType];
205 bktype_shstats =
207
208 if (!nowait)
209 LWLockAcquire(bktype_lock, LW_EXCLUSIVE);
210 else if (!LWLockConditionalAcquire(bktype_lock, LW_EXCLUSIVE))
211 return true;
212
213 for (int io_object = 0; io_object < IOOBJECT_NUM_TYPES; io_object++)
214 {
215 for (int io_context = 0; io_context < IOCONTEXT_NUM_TYPES; io_context++)
216 {
217 for (int io_op = 0; io_op < IOOP_NUM_TYPES; io_op++)
218 {
219 instr_time time;
220
221 bktype_shstats->counts[io_object][io_context][io_op] +=
222 PendingIOStats.counts[io_object][io_context][io_op];
223
224 bktype_shstats->bytes[io_object][io_context][io_op] +=
225 PendingIOStats.bytes[io_object][io_context][io_op];
226
227 time = PendingIOStats.pending_times[io_object][io_context][io_op];
228
229 bktype_shstats->times[io_object][io_context][io_op] +=
231 }
232 }
233 }
234
236
237 LWLockRelease(bktype_lock);
238
239 memset(&PendingIOStats, 0, sizeof(PendingIOStats));
240
241 have_iostats = false;
242
243 return false;
244}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1339
@ LW_EXCLUSIVE
Definition: lwlock.h:114
bool pgstat_bktype_io_stats_valid(PgStat_BktypeIO *backend_io, BackendType bktype)
Definition: pgstat_io.c:37
Definition: lwlock.h:42
LWLock locks[BACKEND_NUM_TYPES]
uint64 bytes[IOOBJECT_NUM_TYPES][IOCONTEXT_NUM_TYPES][IOOP_NUM_TYPES]
Definition: pgstat.h:325
PgStat_BktypeIO stats[BACKEND_NUM_TYPES]
Definition: pgstat.h:340
PgStat_ShmemControl * shmem
PgStatShared_IO io

References Assert, PgStat_BktypeIO::bytes, PgStat_PendingIO::bytes, PgStat_BktypeIO::counts, PgStat_PendingIO::counts, have_iostats, INSTR_TIME_GET_MICROSEC, PgStat_ShmemControl::io, IOCONTEXT_NUM_TYPES, IOOBJECT_NUM_TYPES, IOOP_NUM_TYPES, PgStatShared_IO::locks, LW_EXCLUSIVE, LWLockAcquire(), LWLockConditionalAcquire(), LWLockRelease(), MyBackendType, PgStat_PendingIO::pending_times, PendingIOStats, pgstat_bktype_io_stats_valid(), pgStatLocal, PgStat_LocalState::shmem, PgStat_IO::stats, PgStatShared_IO::stats, and PgStat_BktypeIO::times.

Referenced by pgstat_flush_io().

◆ pgstat_io_have_pending_cb()

bool pgstat_io_have_pending_cb ( void  )

Definition at line 173 of file pgstat_io.c.

174{
175 return have_iostats;
176}

References have_iostats.

◆ pgstat_io_init_shmem_cb()

void pgstat_io_init_shmem_cb ( void *  stats)

Definition at line 285 of file pgstat_io.c.

286{
287 PgStatShared_IO *stat_shmem = (PgStatShared_IO *) stats;
288
289 for (int i = 0; i < BACKEND_NUM_TYPES; i++)
291}
int i
Definition: isn.c:72
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:707
@ LWTRANCHE_PGSTATS_DATA
Definition: lwlock.h:205
#define BACKEND_NUM_TYPES
Definition: miscadmin.h:375

References BACKEND_NUM_TYPES, i, PgStatShared_IO::locks, LWLockInitialize(), and LWTRANCHE_PGSTATS_DATA.

◆ pgstat_io_reset_all_cb()

void pgstat_io_reset_all_cb ( TimestampTz  ts)

Definition at line 294 of file pgstat_io.c.

295{
296 for (int i = 0; i < BACKEND_NUM_TYPES; i++)
297 {
298 LWLock *bktype_lock = &pgStatLocal.shmem->io.locks[i];
299 PgStat_BktypeIO *bktype_shstats = &pgStatLocal.shmem->io.stats.stats[i];
300
301 LWLockAcquire(bktype_lock, LW_EXCLUSIVE);
302
303 /*
304 * Use the lock in the first BackendType's PgStat_BktypeIO to protect
305 * the reset timestamp as well.
306 */
307 if (i == 0)
309
310 memset(bktype_shstats, 0, sizeof(*bktype_shstats));
311 LWLockRelease(bktype_lock);
312 }
313}
TimestampTz stat_reset_timestamp
Definition: pgstat.h:339

References BACKEND_NUM_TYPES, i, PgStat_ShmemControl::io, PgStatShared_IO::locks, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shmem, PgStat_IO::stat_reset_timestamp, PgStat_IO::stats, and PgStatShared_IO::stats.

◆ pgstat_io_snapshot_cb()

void pgstat_io_snapshot_cb ( void  )

Definition at line 316 of file pgstat_io.c.

317{
318 for (int i = 0; i < BACKEND_NUM_TYPES; i++)
319 {
320 LWLock *bktype_lock = &pgStatLocal.shmem->io.locks[i];
321 PgStat_BktypeIO *bktype_shstats = &pgStatLocal.shmem->io.stats.stats[i];
322 PgStat_BktypeIO *bktype_snap = &pgStatLocal.snapshot.io.stats[i];
323
324 LWLockAcquire(bktype_lock, LW_SHARED);
325
326 /*
327 * Use the lock in the first BackendType's PgStat_BktypeIO to protect
328 * the reset timestamp as well.
329 */
330 if (i == 0)
333
334 /* using struct assignment due to better type safety */
335 *bktype_snap = *bktype_shstats;
336 LWLockRelease(bktype_lock);
337 }
338}
@ LW_SHARED
Definition: lwlock.h:115

References BACKEND_NUM_TYPES, i, PgStat_ShmemControl::io, PgStat_Snapshot::io, PgStatShared_IO::locks, LW_SHARED, LWLockAcquire(), LWLockRelease(), pgStatLocal, PgStat_LocalState::shmem, PgStat_LocalState::snapshot, PgStat_IO::stat_reset_timestamp, PgStat_IO::stats, and PgStatShared_IO::stats.

◆ pgstat_prepare_io_time()

instr_time pgstat_prepare_io_time ( bool  track_io_guc)

Definition at line 90 of file pgstat_io.c.

91{
92 instr_time io_start;
93
94 if (track_io_guc)
95 INSTR_TIME_SET_CURRENT(io_start);
96 else
97 {
98 /*
99 * There is no need to set io_start when an IO timing GUC is disabled,
100 * still initialize it to zero to avoid compiler warnings.
101 */
102 INSTR_TIME_SET_ZERO(io_start);
103 }
104
105 return io_start;
106}
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:172

References INSTR_TIME_SET_CURRENT, and INSTR_TIME_SET_ZERO.

Referenced by ExtendBufferedRelLocal(), ExtendBufferedRelShared(), FlushBuffer(), FlushRelationBuffers(), GetLocalVictimBuffer(), issue_xlog_fsync(), IssuePendingWritebacks(), mdsyncfiletag(), register_dirty_segment(), WaitReadBuffers(), WALRead(), XLogFileInitInternal(), XLogPageRead(), and XLogWrite().

◆ pgstat_tracks_io_bktype()

bool pgstat_tracks_io_bktype ( BackendType  bktype)

Definition at line 358 of file pgstat_io.c.

359{
360 /*
361 * List every type so that new backend types trigger a warning about
362 * needing to adjust this switch.
363 */
364 switch (bktype)
365 {
366 case B_INVALID:
368 case B_ARCHIVER:
369 case B_LOGGER:
370 return false;
371
373 case B_AUTOVAC_WORKER:
374 case B_BACKEND:
375 case B_BG_WORKER:
376 case B_BG_WRITER:
377 case B_CHECKPOINTER:
380 case B_STARTUP:
381 case B_WAL_RECEIVER:
382 case B_WAL_SENDER:
383 case B_WAL_SUMMARIZER:
384 case B_WAL_WRITER:
385 return true;
386 }
387
388 return false;
389}
@ B_WAL_SUMMARIZER
Definition: miscadmin.h:365
@ B_WAL_WRITER
Definition: miscadmin.h:366
@ B_WAL_RECEIVER
Definition: miscadmin.h:364
@ B_CHECKPOINTER
Definition: miscadmin.h:362
@ B_WAL_SENDER
Definition: miscadmin.h:346
@ B_LOGGER
Definition: miscadmin.h:372
@ B_STARTUP
Definition: miscadmin.h:363
@ B_BG_WORKER
Definition: miscadmin.h:345
@ B_INVALID
Definition: miscadmin.h:338
@ B_STANDALONE_BACKEND
Definition: miscadmin.h:349
@ B_BG_WRITER
Definition: miscadmin.h:361
@ B_BACKEND
Definition: miscadmin.h:341
@ B_ARCHIVER
Definition: miscadmin.h:360
@ B_AUTOVAC_LAUNCHER
Definition: miscadmin.h:343
@ B_SLOTSYNC_WORKER
Definition: miscadmin.h:347
@ B_DEAD_END_BACKEND
Definition: miscadmin.h:342
@ B_AUTOVAC_WORKER
Definition: miscadmin.h:344

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_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_get_io(), and pgstat_tracks_io_object().

◆ pgstat_tracks_io_object()

bool pgstat_tracks_io_object ( BackendType  bktype,
IOObject  io_object,
IOContext  io_context 
)

Definition at line 399 of file pgstat_io.c.

401{
402 bool no_temp_rel;
403
404 /*
405 * Some BackendTypes should never track IO statistics.
406 */
407 if (!pgstat_tracks_io_bktype(bktype))
408 return false;
409
410 /*
411 * Currently, IO on IOOBJECT_WAL objects can only occur in the
412 * IOCONTEXT_NORMAL and IOCONTEXT_INIT IOContexts.
413 */
414 if (io_object == IOOBJECT_WAL &&
415 (io_context != IOCONTEXT_NORMAL &&
416 io_context != IOCONTEXT_INIT))
417 return false;
418
419 /*
420 * Currently, IO on temporary relations can only occur in the
421 * IOCONTEXT_NORMAL IOContext.
422 */
423 if (io_context != IOCONTEXT_NORMAL &&
424 io_object == IOOBJECT_TEMP_RELATION)
425 return false;
426
427 /*
428 * In core Postgres, only regular backends and WAL Sender processes
429 * executing queries will use local buffers and operate on temporary
430 * relations. Parallel workers will not use local buffers (see
431 * InitLocalBuffers()); however, extensions leveraging background workers
432 * have no such limitation, so track IO on IOOBJECT_TEMP_RELATION for
433 * BackendType B_BG_WORKER.
434 */
435 no_temp_rel = bktype == B_AUTOVAC_LAUNCHER || bktype == B_BG_WRITER ||
436 bktype == B_CHECKPOINTER || bktype == B_AUTOVAC_WORKER ||
437 bktype == B_STANDALONE_BACKEND || bktype == B_STARTUP;
438
439 if (no_temp_rel && io_context == IOCONTEXT_NORMAL &&
440 io_object == IOOBJECT_TEMP_RELATION)
441 return false;
442
443 /*
444 * Some BackendTypes do not currently perform any IO in certain
445 * IOContexts, and, while it may not be inherently incorrect for them to
446 * do so, excluding those rows from the view makes the view easier to use.
447 */
448 if ((bktype == B_CHECKPOINTER || bktype == B_BG_WRITER) &&
449 (io_context == IOCONTEXT_BULKREAD ||
450 io_context == IOCONTEXT_BULKWRITE ||
451 io_context == IOCONTEXT_VACUUM))
452 return false;
453
454 if (bktype == B_AUTOVAC_LAUNCHER && io_context == IOCONTEXT_VACUUM)
455 return false;
456
457 if ((bktype == B_AUTOVAC_WORKER || bktype == B_AUTOVAC_LAUNCHER) &&
458 io_context == IOCONTEXT_BULKWRITE)
459 return false;
460
461 return true;
462}
bool pgstat_tracks_io_bktype(BackendType bktype)
Definition: pgstat_io.c:358

References B_AUTOVAC_LAUNCHER, B_AUTOVAC_WORKER, B_BG_WRITER, B_CHECKPOINTER, B_STANDALONE_BACKEND, B_STARTUP, IOCONTEXT_BULKREAD, IOCONTEXT_BULKWRITE, IOCONTEXT_INIT, IOCONTEXT_NORMAL, IOCONTEXT_VACUUM, IOOBJECT_TEMP_RELATION, IOOBJECT_WAL, and pgstat_tracks_io_bktype().

Referenced by pg_stat_io_build_tuples(), and pgstat_tracks_io_op().

◆ pgstat_tracks_io_op()

bool pgstat_tracks_io_op ( BackendType  bktype,
IOObject  io_object,
IOContext  io_context,
IOOp  io_op 
)

Definition at line 473 of file pgstat_io.c.

475{
476 bool strategy_io_context;
477
478 /* if (io_context, io_object) will never collect stats, we're done */
479 if (!pgstat_tracks_io_object(bktype, io_object, io_context))
480 return false;
481
482 /*
483 * Some BackendTypes will not do certain IOOps.
484 */
485 if (bktype == B_BG_WRITER &&
486 (io_op == IOOP_READ || io_op == IOOP_EVICT || io_op == IOOP_HIT))
487 return false;
488
489 if (bktype == B_CHECKPOINTER &&
490 ((io_object != IOOBJECT_WAL && io_op == IOOP_READ) ||
491 (io_op == IOOP_EVICT || io_op == IOOP_HIT)))
492 return false;
493
494 if ((bktype == B_AUTOVAC_LAUNCHER || bktype == B_BG_WRITER ||
495 bktype == B_CHECKPOINTER) && io_op == IOOP_EXTEND)
496 return false;
497
498 /*
499 * Some BackendTypes do not perform reads with IOOBJECT_WAL.
500 */
501 if (io_object == IOOBJECT_WAL && io_op == IOOP_READ &&
502 (bktype == B_WAL_RECEIVER || bktype == B_BG_WRITER ||
503 bktype == B_AUTOVAC_WORKER || bktype == B_AUTOVAC_WORKER ||
504 bktype == B_WAL_WRITER))
505 return false;
506
507 /*
508 * Temporary tables are not logged and thus do not require fsync'ing.
509 * Writeback is not requested for temporary tables.
510 */
511 if (io_object == IOOBJECT_TEMP_RELATION &&
512 (io_op == IOOP_FSYNC || io_op == IOOP_WRITEBACK))
513 return false;
514
515 /*
516 * Some IOOps are not valid in certain IOContexts and some IOOps are only
517 * valid in certain contexts.
518 */
519 if (io_context == IOCONTEXT_BULKREAD && io_op == IOOP_EXTEND)
520 return false;
521
522 strategy_io_context = io_context == IOCONTEXT_BULKREAD ||
523 io_context == IOCONTEXT_BULKWRITE || io_context == IOCONTEXT_VACUUM;
524
525 /*
526 * IOOP_REUSE is only relevant when a BufferAccessStrategy is in use.
527 */
528 if (!strategy_io_context && io_op == IOOP_REUSE)
529 return false;
530
531 /*
532 * IOOBJECT_WAL IOObject will not do certain IOOps depending on IOContext.
533 */
534 if (io_object == IOOBJECT_WAL && io_context == IOCONTEXT_INIT &&
535 !(io_op == IOOP_WRITE || io_op == IOOP_FSYNC))
536 return false;
537
538 if (io_object == IOOBJECT_WAL && io_context == IOCONTEXT_NORMAL &&
539 !(io_op == IOOP_WRITE || io_op == IOOP_READ || io_op == IOOP_FSYNC))
540 return false;
541
542 /*
543 * IOOP_FSYNC IOOps done by a backend using a BufferAccessStrategy are
544 * counted in the IOCONTEXT_NORMAL IOContext. See comment in
545 * register_dirty_segment() for more details.
546 */
547 if (strategy_io_context && io_op == IOOP_FSYNC)
548 return false;
549
550
551 return true;
552}
@ IOOP_FSYNC
Definition: pgstat.h:306
@ IOOP_WRITEBACK
Definition: pgstat.h:309
@ IOOP_HIT
Definition: pgstat.h:307
@ IOOP_EVICT
Definition: pgstat.h:305
@ IOOP_REUSE
Definition: pgstat.h:308
bool pgstat_tracks_io_object(BackendType bktype, IOObject io_object, IOContext io_context)
Definition: pgstat_io.c:399

References B_AUTOVAC_LAUNCHER, B_AUTOVAC_WORKER, B_BG_WRITER, B_CHECKPOINTER, B_WAL_RECEIVER, B_WAL_WRITER, IOCONTEXT_BULKREAD, IOCONTEXT_BULKWRITE, IOCONTEXT_INIT, IOCONTEXT_NORMAL, IOCONTEXT_VACUUM, IOOBJECT_TEMP_RELATION, IOOBJECT_WAL, IOOP_EVICT, IOOP_EXTEND, IOOP_FSYNC, IOOP_HIT, IOOP_READ, IOOP_REUSE, IOOP_WRITE, IOOP_WRITEBACK, and pgstat_tracks_io_object().

Referenced by pg_stat_io_build_tuples(), pgstat_bktype_io_stats_valid(), pgstat_count_backend_io_op(), pgstat_count_backend_io_op_time(), and pgstat_count_io_op().

Variable Documentation

◆ have_iostats

bool have_iostats = false
static

Definition at line 24 of file pgstat_io.c.

Referenced by pgstat_count_io_op(), pgstat_io_flush_cb(), and pgstat_io_have_pending_cb().

◆ PendingIOStats

PgStat_PendingIO PendingIOStats
static

Definition at line 23 of file pgstat_io.c.

Referenced by pgstat_count_io_op(), pgstat_count_io_op_time(), and pgstat_io_flush_cb().