| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* instrument.h |
| 4 |
* definitions for run-time statistics collection |
| 5 |
* |
| 6 |
* |
| 7 |
* Copyright (c) 2001-2025, PostgreSQL Global Development Group |
| 8 |
* |
| 9 |
* src/include/executor/instrument.h |
| 10 |
* |
| 11 |
*------------------------------------------------------------------------- |
| 12 |
*/ |
| 13 |
#ifndef INSTRUMENT_H |
| 14 |
#define INSTRUMENT_H |
| 15 |
|
| 16 |
#include "portability/instr_time.h" |
| 17 |
|
| 18 |
|
| 19 |
/* |
| 20 |
* BufferUsage and WalUsage counters keep being incremented infinitely, |
| 21 |
* i.e., must never be reset to zero, so that we can calculate how much |
| 22 |
* the counters are incremented in an arbitrary period. |
| 23 |
*/ |
| 24 |
typedef struct BufferUsage |
| 25 |
{ |
| 26 |
int64 shared_blks_hit; /* # of shared buffer hits */ |
| 27 |
int64 shared_blks_read; /* # of shared disk blocks read */ |
| 28 |
int64 shared_blks_dirtied; /* # of shared blocks dirtied */ |
| 29 |
int64 shared_blks_written; /* # of shared disk blocks written */ |
| 30 |
int64 local_blks_hit; /* # of local buffer hits */ |
| 31 |
int64 local_blks_read; /* # of local disk blocks read */ |
| 32 |
int64 local_blks_dirtied; /* # of local blocks dirtied */ |
| 33 |
int64 local_blks_written; /* # of local disk blocks written */ |
| 34 |
int64 temp_blks_read; /* # of temp blocks read */ |
| 35 |
int64 temp_blks_written; /* # of temp blocks written */ |
| 36 |
instr_time shared_blk_read_time; /* time spent reading shared blocks */ |
| 37 |
instr_time shared_blk_write_time; /* time spent writing shared blocks */ |
| 38 |
instr_time local_blk_read_time; /* time spent reading local blocks */ |
| 39 |
instr_time local_blk_write_time; /* time spent writing local blocks */ |
| 40 |
instr_time temp_blk_read_time; /* time spent reading temp blocks */ |
| 41 |
instr_time temp_blk_write_time; /* time spent writing temp blocks */ |
| 42 |
} BufferUsage; |
| 43 |
|
| 44 |
/* |
| 45 |
* WalUsage tracks only WAL activity like WAL records generation that |
| 46 |
* can be measured per query and is displayed by EXPLAIN command, |
| 47 |
* pg_stat_statements extension, etc. It does not track other WAL activity |
| 48 |
* like WAL writes that it's not worth measuring per query. That's tracked |
| 49 |
* by WAL global statistics counters in WalStats, instead. |
| 50 |
*/ |
| 51 |
typedef struct WalUsage |
| 52 |
{ |
| 53 |
int64 wal_records; /* # of WAL records produced */ |
| 54 |
int64 wal_fpi; /* # of WAL full page images produced */ |
| 55 |
uint64 wal_bytes; /* size of WAL records produced */ |
| 56 |
int64 wal_buffers_full; /* # of times the WAL buffers became full */ |
| 57 |
} WalUsage; |
| 58 |
|
| 59 |
/* Flag bits included in InstrAlloc's instrument_options bitmask */ |
| 60 |
typedef enum InstrumentOption |
| 61 |
{ |
| 62 |
INSTRUMENT_TIMER = 1 << 0, /* needs timer (and row counts) */ |
| 63 |
INSTRUMENT_BUFFERS = 1 << 1, /* needs buffer usage */ |
| 64 |
INSTRUMENT_ROWS = 1 << 2, /* needs row count */ |
| 65 |
INSTRUMENT_WAL = 1 << 3, /* needs WAL usage */ |
| 66 |
INSTRUMENT_ALL = PG_INT32_MAX |
| 67 |
} InstrumentOption; |
| 68 |
|
| 69 |
typedef struct Instrumentation |
| 70 |
{ |
| 71 |
/* Parameters set at node creation: */ |
| 72 |
bool need_timer; /* true if we need timer data */ |
| 73 |
bool need_bufusage; /* true if we need buffer usage data */ |
| 74 |
bool need_walusage; /* true if we need WAL usage data */ |
| 75 |
bool async_mode; /* true if node is in async mode */ |
| 76 |
/* Info about current plan cycle: */ |
| 77 |
bool running; /* true if we've completed first tuple */ |
| 78 |
instr_time starttime; /* start time of current iteration of node */ |
| 79 |
instr_time counter; /* accumulated runtime for this node */ |
| 80 |
double firsttuple; /* time for first tuple of this cycle */ |
| 81 |
double tuplecount; /* # of tuples emitted so far this cycle */ |
| 82 |
BufferUsage bufusage_start; /* buffer usage at start */ |
| 83 |
WalUsage walusage_start; /* WAL usage at start */ |
| 84 |
/* Accumulated statistics across all completed cycles: */ |
| 85 |
double startup; /* total startup time (in seconds) */ |
| 86 |
double total; /* total time (in seconds) */ |
| 87 |
double ntuples; /* total tuples produced */ |
| 88 |
double ntuples2; /* secondary node-specific tuple counter */ |
| 89 |
double nloops; /* # of run cycles for this node */ |
| 90 |
double nfiltered1; /* # of tuples removed by scanqual or joinqual */ |
| 91 |
double nfiltered2; /* # of tuples removed by "other" quals */ |
| 92 |
BufferUsage bufusage; /* total buffer usage */ |
| 93 |
WalUsage walusage; /* total WAL usage */ |
| 94 |
} Instrumentation; |
| 95 |
|
| 96 |
typedef struct WorkerInstrumentation |
| 97 |
{ |
| 98 |
int num_workers; /* # of structures that follow */ |
| 99 |
Instrumentation instrument[FLEXIBLE_ARRAY_MEMBER]; |
| 100 |
} WorkerInstrumentation; |
| 101 |
|
| 102 |
extern PGDLLIMPORT BufferUsage pgBufferUsage; |
| 103 |
extern PGDLLIMPORT WalUsage pgWalUsage; |
| 104 |
|
| 105 |
extern Instrumentation *InstrAlloc(int n, int instrument_options, |
| 106 |
bool async_mode); |
| 107 |
extern void InstrInit(Instrumentation *instr, int instrument_options); |
| 108 |
extern void InstrStartNode(Instrumentation *instr); |
| 109 |
extern void InstrStopNode(Instrumentation *instr, double nTuples); |
| 110 |
extern void InstrUpdateTupleCount(Instrumentation *instr, double nTuples); |
| 111 |
extern void InstrEndLoop(Instrumentation *instr); |
| 112 |
extern void InstrAggNode(Instrumentation *dst, Instrumentation *add); |
| 113 |
extern void InstrStartParallelQuery(void); |
| 114 |
extern void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage); |
| 115 |
extern void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage); |
| 116 |
extern void BufferUsageAccumDiff(BufferUsage *dst, |
| 117 |
const BufferUsage *add, const BufferUsage *sub); |
| 118 |
extern void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, |
| 119 |
const WalUsage *sub); |
| 120 |
|
| 121 |
#endif /* INSTRUMENT_H */ |