Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * instrument_node.h
4 : : * Definitions for node-specific support for parallel query instrumentation
5 : : *
6 : : * These structs purposely contain no pointers because they are copied
7 : : * across processes during parallel query execution. Each worker copies its
8 : : * individual information into the container struct at executor shutdown time,
9 : : * to allow the leader to display the information in EXPLAIN ANALYZE.
10 : : *
11 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
12 : : * Portions Copyright (c) 1994, Regents of the University of California
13 : : *
14 : : * src/include/executor/instrument_node.h
15 : : *
16 : : *-------------------------------------------------------------------------
17 : : */
18 : : #ifndef INSTRUMENT_NODE_H
19 : : #define INSTRUMENT_NODE_H
20 : :
21 : : /*
22 : : * Offset added to plan_node_id to create a second TOC key for per-worker scan
23 : : * instrumentation. Instrumentation and parallel-awareness are independent, so
24 : : * separate DSM chunks let each be allocated and initialized only when needed.
25 : : * In the future, if nodes need more DSM allocations, we would need a more
26 : : * robust system.
27 : : */
28 : : #define PARALLEL_KEY_SCAN_INSTRUMENT_OFFSET UINT64CONST(0xD000000000000000)
29 : :
30 : : /* ---------------------
31 : : * Instrumentation information for aggregate function execution
32 : : * ---------------------
33 : : */
34 : : typedef struct AggregateInstrumentation
35 : : {
36 : : Size hash_mem_peak; /* peak hash table memory usage */
37 : : uint64 hash_disk_used; /* kB of disk space used */
38 : : int hash_batches_used; /* batches used during entire execution */
39 : : } AggregateInstrumentation;
40 : :
41 : : /*
42 : : * Shared memory container for per-worker aggregate information
43 : : */
44 : : typedef struct SharedAggInfo
45 : : {
46 : : int num_workers;
47 : : pg_attribute_counted_by(num_workers) AggregateInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
48 : : } SharedAggInfo;
49 : :
50 : :
51 : : /* ---------------------
52 : : * Instrumentation information about read streams and I/O
53 : : * ---------------------
54 : : */
55 : : typedef struct IOStats
56 : : {
57 : : /* number of buffers returned to consumer (for averaging distance) */
58 : : uint64 prefetch_count;
59 : :
60 : : /* sum of pinned_buffers sampled at each buffer return */
61 : : uint64 distance_sum;
62 : :
63 : : /* maximum actual pinned_buffers observed during the scan */
64 : : int16 distance_max;
65 : :
66 : : /* maximum possible look-ahead distance (max_pinned_buffers) */
67 : : int16 distance_capacity;
68 : :
69 : : /* number of waits for a read (for the I/O) */
70 : : uint64 wait_count;
71 : :
72 : : /* I/O stats */
73 : : uint64 io_count; /* number of I/Os */
74 : : uint64 io_nblocks; /* sum of blocks for all I/Os */
75 : : uint64 io_in_progress; /* sum of in-progress I/Os */
76 : : } IOStats;
77 : :
78 : : typedef struct TableScanInstrumentation
79 : : {
80 : : IOStats io;
81 : : } TableScanInstrumentation;
82 : :
83 : : /* merge IO statistics from 'src' into 'dst' */
84 : : static inline void
85 : 0 : AccumulateIOStats(IOStats *dst, IOStats *src)
86 : : {
87 : 0 : dst->prefetch_count += src->prefetch_count;
88 : 0 : dst->distance_sum += src->distance_sum;
89 [ # # ]: 0 : if (src->distance_max > dst->distance_max)
90 : 0 : dst->distance_max = src->distance_max;
91 [ # # ]: 0 : if (src->distance_capacity > dst->distance_capacity)
92 : 0 : dst->distance_capacity = src->distance_capacity;
93 : 0 : dst->wait_count += src->wait_count;
94 : 0 : dst->io_count += src->io_count;
95 : 0 : dst->io_nblocks += src->io_nblocks;
96 : 0 : dst->io_in_progress += src->io_in_progress;
97 : 0 : }
98 : :
99 : :
100 : : /* ---------------------
101 : : * Instrumentation information for index scans (used by all AM interfaces)
102 : : * ---------------------
103 : : */
104 : : typedef struct IndexScanInstrumentation
105 : : {
106 : : /* Index search count (incremented with pgstat_count_index_scan call) */
107 : : uint64 nsearches;
108 : :
109 : : /* Table tuples fetched count (incremented during index-only scans) */
110 : : uint64 ntabletuplefetches;
111 : : } IndexScanInstrumentation;
112 : :
113 : : /*
114 : : * Shared memory container for per-worker information
115 : : */
116 : : typedef struct SharedIndexScanInstrumentation
117 : : {
118 : : int num_workers;
119 : : pg_attribute_counted_by(num_workers) IndexScanInstrumentation winstrument[FLEXIBLE_ARRAY_MEMBER];
120 : : } SharedIndexScanInstrumentation;
121 : :
122 : :
123 : : /* ---------------------
124 : : * Instrumentation information for bitmap heap scans
125 : : *
126 : : * exact_pages total number of exact pages retrieved
127 : : * lossy_pages total number of lossy pages retrieved
128 : : * ---------------------
129 : : */
130 : : typedef struct BitmapHeapScanInstrumentation
131 : : {
132 : : uint64 exact_pages;
133 : : uint64 lossy_pages;
134 : : TableScanInstrumentation stats;
135 : : } BitmapHeapScanInstrumentation;
136 : :
137 : : /*
138 : : * Shared memory container for per-worker information
139 : : */
140 : : typedef struct SharedBitmapHeapInstrumentation
141 : : {
142 : : int num_workers;
143 : : pg_attribute_counted_by(num_workers) BitmapHeapScanInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
144 : : } SharedBitmapHeapInstrumentation;
145 : :
146 : :
147 : : /* ---------------------
148 : : * Instrumentation information for Memoize
149 : : * ---------------------
150 : : */
151 : : typedef struct MemoizeInstrumentation
152 : : {
153 : : uint64 cache_hits; /* number of rescans where we've found the
154 : : * scan parameters values to be cached */
155 : : uint64 cache_misses; /* number of rescans where we've not found the
156 : : * scan parameters values to be cached */
157 : : uint64 cache_evictions; /* number of cache entries removed due to
158 : : * the need to free memory */
159 : : uint64 cache_overflows; /* number of times we've had to bypass the
160 : : * cache when filling it due to not being
161 : : * able to free enough space to store the
162 : : * current scan's tuples */
163 : : uint64 mem_peak; /* peak memory usage in bytes */
164 : : } MemoizeInstrumentation;
165 : :
166 : : /*
167 : : * Shared memory container for per-worker memoize information
168 : : */
169 : : typedef struct SharedMemoizeInfo
170 : : {
171 : : int num_workers;
172 : : pg_attribute_counted_by(num_workers) MemoizeInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
173 : : } SharedMemoizeInfo;
174 : :
175 : :
176 : : /* ---------------------
177 : : * Instrumentation information for Material
178 : : * ---------------------
179 : : */
180 : : typedef struct MaterialInstrumentation
181 : : {
182 : : int64 maxSpaceUsed; /* peak memory/disk usage in bytes, or 0 if no
183 : : * tuplestore was created */
184 : : bool usedDisk; /* did the tuplestore spill to disk? */
185 : : } MaterialInstrumentation;
186 : :
187 : : /*
188 : : * Shared memory container for per-worker material information
189 : : */
190 : : typedef struct SharedMaterialInfo
191 : : {
192 : : int num_workers;
193 : : pg_attribute_counted_by(num_workers) MaterialInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
194 : : } SharedMaterialInfo;
195 : :
196 : :
197 : : /* ---------------------
198 : : * Instrumentation information for FunctionScan
199 : : * ---------------------
200 : : */
201 : : typedef struct FunctionScanInstrumentation
202 : : {
203 : : int64 maxSpaceUsed; /* peak memory/disk usage in bytes, or 0 if no
204 : : * tuplestore was created */
205 : : bool usedDisk; /* did any tuplestore spill to disk? */
206 : : } FunctionScanInstrumentation;
207 : :
208 : : /*
209 : : * Shared memory container for per-worker function scan information
210 : : */
211 : : typedef struct SharedFunctionScanInfo
212 : : {
213 : : int num_workers;
214 : : pg_attribute_counted_by(num_workers) FunctionScanInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
215 : : } SharedFunctionScanInfo;
216 : :
217 : :
218 : : /* ---------------------
219 : : * Instrumentation information for Sorts.
220 : : * ---------------------
221 : : */
222 : :
223 : : typedef enum
224 : : {
225 : : SORT_SPACE_TYPE_DISK,
226 : : SORT_SPACE_TYPE_MEMORY,
227 : : } TuplesortSpaceType;
228 : :
229 : : /*
230 : : * The parallel-sort infrastructure relies on having a zero TuplesortMethod
231 : : * to indicate that a worker never did anything, so we assign zero to
232 : : * SORT_TYPE_STILL_IN_PROGRESS. The other values of this enum can be
233 : : * OR'ed together to represent a situation where different workers used
234 : : * different methods, so we need a separate bit for each one. Keep the
235 : : * NUM_TUPLESORTMETHODS constant in sync with the number of bits!
236 : : */
237 : : typedef enum
238 : : {
239 : : SORT_TYPE_STILL_IN_PROGRESS = 0,
240 : : SORT_TYPE_TOP_N_HEAPSORT = 1 << 0,
241 : : SORT_TYPE_QUICKSORT = 1 << 1,
242 : : SORT_TYPE_EXTERNAL_SORT = 1 << 2,
243 : : SORT_TYPE_EXTERNAL_MERGE = 1 << 3,
244 : : } TuplesortMethod;
245 : : #define NUM_TUPLESORTMETHODS 4
246 : :
247 : : typedef struct TuplesortInstrumentation
248 : : {
249 : : TuplesortMethod sortMethod; /* sort algorithm used */
250 : : TuplesortSpaceType spaceType; /* type of space spaceUsed represents */
251 : : int64 spaceUsed; /* space consumption, in kB */
252 : : } TuplesortInstrumentation;
253 : :
254 : : /*
255 : : * Shared memory container for per-worker sort information
256 : : */
257 : : typedef struct SharedSortInfo
258 : : {
259 : : int num_workers;
260 : : pg_attribute_counted_by(num_workers) TuplesortInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
261 : : } SharedSortInfo;
262 : :
263 : :
264 : : /* ---------------------
265 : : * Instrumentation information for nodeHash.c
266 : : * ---------------------
267 : : */
268 : : typedef struct HashInstrumentation
269 : : {
270 : : int nbuckets; /* number of buckets at end of execution */
271 : : int nbuckets_original; /* planned number of buckets */
272 : : int nbatch; /* number of batches at end of execution */
273 : : int nbatch_original; /* planned number of batches */
274 : : Size space_peak; /* peak memory usage in bytes */
275 : : } HashInstrumentation;
276 : :
277 : : /*
278 : : * Shared memory container for per-worker information
279 : : */
280 : : typedef struct SharedHashInfo
281 : : {
282 : : int num_workers;
283 : : pg_attribute_counted_by(num_workers) HashInstrumentation hinstrument[FLEXIBLE_ARRAY_MEMBER];
284 : : } SharedHashInfo;
285 : :
286 : :
287 : : /* ---------------------
288 : : * Instrumentation information for IncrementalSort
289 : : * ---------------------
290 : : */
291 : : typedef struct IncrementalSortGroupInfo
292 : : {
293 : : int64 groupCount;
294 : : int64 maxDiskSpaceUsed;
295 : : int64 totalDiskSpaceUsed;
296 : : int64 maxMemorySpaceUsed;
297 : : int64 totalMemorySpaceUsed;
298 : : uint32 sortMethods; /* bitmask of TuplesortMethod */
299 : : } IncrementalSortGroupInfo;
300 : :
301 : : typedef struct IncrementalSortInfo
302 : : {
303 : : IncrementalSortGroupInfo fullsortGroupInfo;
304 : : IncrementalSortGroupInfo prefixsortGroupInfo;
305 : : } IncrementalSortInfo;
306 : :
307 : : /* Shared memory container for per-worker incremental sort information */
308 : : typedef struct SharedIncrementalSortInfo
309 : : {
310 : : int num_workers;
311 : : pg_attribute_counted_by(num_workers) IncrementalSortInfo sinfo[FLEXIBLE_ARRAY_MEMBER];
312 : : } SharedIncrementalSortInfo;
313 : :
314 : :
315 : : /* ---------------------
316 : : * Instrumentation information for sequential scans
317 : : * ---------------------
318 : : */
319 : : typedef struct SeqScanInstrumentation
320 : : {
321 : : TableScanInstrumentation stats;
322 : : } SeqScanInstrumentation;
323 : :
324 : : /*
325 : : * Shared memory container for per-worker information
326 : : */
327 : : typedef struct SharedSeqScanInstrumentation
328 : : {
329 : : int num_workers;
330 : : pg_attribute_counted_by(num_workers) SeqScanInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
331 : : } SharedSeqScanInstrumentation;
332 : :
333 : :
334 : : /*
335 : : * Instrumentation information for TID range scans
336 : : */
337 : : typedef struct TidRangeScanInstrumentation
338 : : {
339 : : TableScanInstrumentation stats;
340 : : } TidRangeScanInstrumentation;
341 : :
342 : : /*
343 : : * Shared memory container for per-worker information
344 : : */
345 : : typedef struct SharedTidRangeScanInstrumentation
346 : : {
347 : : int num_workers;
348 : : pg_attribute_counted_by(num_workers) TidRangeScanInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
349 : : } SharedTidRangeScanInstrumentation;
350 : :
351 : : #endif /* INSTRUMENT_NODE_H */
|