PostgreSQL Source Code git master
Loading...
Searching...
No Matches
instrument.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * instrument.h
4 * definitions for run-time statistics collection
5 *
6 *
7 * Copyright (c) 2001-2026, PostgreSQL Global Development Group
8 *
9 * src/include/executor/instrument.h
10 *
11 *-------------------------------------------------------------------------
12 */
13#ifndef INSTRUMENT_H
14#define INSTRUMENT_H
15
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 */
24typedef 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 */
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 */
51typedef 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 uint64 wal_fpi_bytes; /* size of WAL full page images produced */
57 int64 wal_buffers_full; /* # of times the WAL buffers became full */
59
60/* Flag bits included in InstrAlloc's instrument_options bitmask */
61typedef enum InstrumentOption
62{
63 INSTRUMENT_TIMER = 1 << 0, /* needs timer (and row counts) */
64 INSTRUMENT_BUFFERS = 1 << 1, /* needs buffer usage */
65 INSTRUMENT_ROWS = 1 << 2, /* needs row count */
66 INSTRUMENT_WAL = 1 << 3, /* needs WAL usage */
67 INSTRUMENT_IO = 1 << 4, /* needs IO usage */
70
71/*
72 * General purpose instrumentation that can capture time and WAL/buffer usage
73 *
74 * Initialized through InstrAlloc, followed by one or more calls to a pair of
75 * InstrStart/InstrStop (activity is measured in between).
76 */
77typedef struct Instrumentation
78{
79 /* Parameters set at creation: */
80 bool need_timer; /* true if we need timer data */
81 bool need_bufusage; /* true if we need buffer usage data */
82 bool need_walusage; /* true if we need WAL usage data */
83 /* Internal state keeping: */
84 instr_time starttime; /* start time of last InstrStart */
85 BufferUsage bufusage_start; /* buffer usage at start */
86 WalUsage walusage_start; /* WAL usage at start */
87 /* Accumulated statistics: */
88 instr_time total; /* total runtime */
89 BufferUsage bufusage; /* total buffer usage */
90 WalUsage walusage; /* total WAL usage */
92
93/*
94 * Specialized instrumentation for per-node execution statistics
95 */
96typedef struct NodeInstrumentation
97{
99 /* Parameters set at node creation: */
100 bool async_mode; /* true if node is in async mode */
101 /* Info about current plan cycle: */
102 bool running; /* true if we've completed first tuple */
103 instr_time counter; /* accumulated runtime for this node */
104 instr_time firsttuple; /* time for first tuple of this cycle */
105 double tuplecount; /* # of tuples emitted so far this cycle */
106 /* Accumulated statistics across all completed cycles: */
107 instr_time startup; /* total startup time */
108 double ntuples; /* total tuples produced */
109 double ntuples2; /* secondary node-specific tuple counter */
110 double nloops; /* # of run cycles for this node */
111 double nfiltered1; /* # of tuples removed by scanqual or joinqual */
112 double nfiltered2; /* # of tuples removed by "other" quals */
114
120
122{
124 int64 firings; /* number of times the instrumented trigger
125 * was fired */
127
130
131extern Instrumentation *InstrAlloc(int instrument_options);
132extern void InstrInitOptions(Instrumentation *instr, int instrument_options);
133extern void InstrStart(Instrumentation *instr);
134extern void InstrStop(Instrumentation *instr);
135
136extern NodeInstrumentation *InstrAllocNode(int instrument_options,
137 bool async_mode);
138extern void InstrInitNode(NodeInstrumentation *instr, int instrument_options,
139 bool async_mode);
140extern void InstrStartNode(NodeInstrumentation *instr);
141extern void InstrStopNode(NodeInstrumentation *instr, double nTuples);
142extern void InstrUpdateTupleCount(NodeInstrumentation *instr, double nTuples);
143extern void InstrEndLoop(NodeInstrumentation *instr);
145
146extern TriggerInstrumentation *InstrAllocTrigger(int n, int instrument_options);
149
150extern void InstrStartParallelQuery(void);
151extern void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage);
152extern void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage);
154 const BufferUsage *add, const BufferUsage *sub);
155extern void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add,
156 const WalUsage *sub);
157
158#endif /* INSTRUMENT_H */
#define PGDLLIMPORT
Definition c.h:1421
#define PG_INT32_MAX
Definition c.h:673
int64_t int64
Definition c.h:621
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:558
uint64_t uint64
Definition c.h:625
void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition instrument.c:297
void InstrStart(Instrumentation *instr)
Definition instrument.c:53
PGDLLIMPORT WalUsage pgWalUsage
Definition instrument.c:27
void InstrUpdateTupleCount(NodeInstrumentation *instr, double nTuples)
Definition instrument.c:196
void InstrInitNode(NodeInstrumentation *instr, int instrument_options, bool async_mode)
Definition instrument.c:123
void InstrStop(Instrumentation *instr)
Definition instrument.c:103
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition instrument.c:287
InstrumentOption
Definition instrument.h:62
@ INSTRUMENT_ALL
Definition instrument.h:68
@ INSTRUMENT_TIMER
Definition instrument.h:63
@ INSTRUMENT_IO
Definition instrument.h:67
@ INSTRUMENT_BUFFERS
Definition instrument.h:64
@ INSTRUMENT_WAL
Definition instrument.h:66
@ INSTRUMENT_ROWS
Definition instrument.h:65
void InstrStartNode(NodeInstrumentation *instr)
Definition instrument.c:132
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition instrument.c:367
void InstrStartParallelQuery(void)
Definition instrument.c:279
void InstrInitOptions(Instrumentation *instr, int instrument_options)
Definition instrument.c:45
Instrumentation * InstrAlloc(int instrument_options)
Definition instrument.c:36
TriggerInstrumentation * InstrAllocTrigger(int n, int instrument_options)
Definition instrument.c:253
PGDLLIMPORT BufferUsage pgBufferUsage
Definition instrument.c:25
void InstrStopNode(NodeInstrumentation *instr, double nTuples)
Definition instrument.c:139
void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub)
Definition instrument.c:327
void InstrEndLoop(NodeInstrumentation *instr)
Definition instrument.c:204
NodeInstrumentation * InstrAllocNode(int instrument_options, bool async_mode)
Definition instrument.c:112
void InstrStartTrigger(TriggerInstrumentation *tginstr)
Definition instrument.c:265
void InstrAggNode(NodeInstrumentation *dst, NodeInstrumentation *add)
Definition instrument.c:232
void InstrStopTrigger(TriggerInstrumentation *tginstr, int64 firings)
Definition instrument.c:271
static int fb(int x)
instr_time local_blk_read_time
Definition instrument.h:38
int64 shared_blks_dirtied
Definition instrument.h:28
int64 local_blks_hit
Definition instrument.h:30
instr_time temp_blk_write_time
Definition instrument.h:41
instr_time shared_blk_read_time
Definition instrument.h:36
instr_time shared_blk_write_time
Definition instrument.h:37
int64 local_blks_written
Definition instrument.h:33
instr_time temp_blk_read_time
Definition instrument.h:40
instr_time local_blk_write_time
Definition instrument.h:39
int64 temp_blks_read
Definition instrument.h:34
int64 shared_blks_read
Definition instrument.h:27
int64 shared_blks_written
Definition instrument.h:29
int64 temp_blks_written
Definition instrument.h:35
int64 local_blks_read
Definition instrument.h:31
int64 local_blks_dirtied
Definition instrument.h:32
int64 shared_blks_hit
Definition instrument.h:26
BufferUsage bufusage_start
Definition instrument.h:85
WalUsage walusage
Definition instrument.h:90
instr_time total
Definition instrument.h:88
BufferUsage bufusage
Definition instrument.h:89
instr_time starttime
Definition instrument.h:84
WalUsage walusage_start
Definition instrument.h:86
Instrumentation instr
Definition instrument.h:98
instr_time firsttuple
Definition instrument.h:104
Instrumentation instr
Definition instrument.h:123
int64 wal_buffers_full
Definition instrument.h:57
uint64 wal_bytes
Definition instrument.h:55
int64 wal_fpi
Definition instrument.h:54
uint64 wal_fpi_bytes
Definition instrument.h:56
int64 wal_records
Definition instrument.h:53
NodeInstrumentation instrument[FLEXIBLE_ARRAY_MEMBER]
Definition instrument.h:118