PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
nodeAgg.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * nodeAgg.h
4 * prototypes for nodeAgg.c
5 *
6 *
7 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/include/executor/nodeAgg.h
11 *
12 *-------------------------------------------------------------------------
13 */
14#ifndef NODEAGG_H
15#define NODEAGG_H
16
17#include "access/parallel.h"
18#include "nodes/execnodes.h"
19
20
21/*
22 * AggStatePerTransData - per aggregate state value information
23 *
24 * Working state for updating the aggregate's state value, by calling the
25 * transition function with an input row. This struct does not store the
26 * information needed to produce the final aggregate result from the transition
27 * state, that's stored in AggStatePerAggData instead. This separation allows
28 * multiple aggregate results to be produced from a single state value.
29 */
31{
32 /*
33 * These values are set up during ExecInitAgg() and do not change
34 * thereafter:
35 */
36
37 /*
38 * Link to an Aggref expr this state value is for.
39 *
40 * There can be multiple Aggref's sharing the same state value, so long as
41 * the inputs and transition functions are identical and the final
42 * functions are not read-write. This points to the first one of them.
43 */
45
46 /*
47 * Is this state value actually being shared by more than one Aggref?
48 */
50
51 /*
52 * True for ORDER BY and DISTINCT Aggrefs that are not aggpresorted.
53 */
55
56 /*
57 * Number of aggregated input columns. This includes ORDER BY expressions
58 * in both the plain-agg and ordered-set cases. Ordered-set direct args
59 * are not counted, though.
60 */
62
63 /*
64 * Number of aggregated input columns to pass to the transfn. This
65 * includes the ORDER BY columns for ordered-set aggs, but not for plain
66 * aggs. (This doesn't count the transition state value!)
67 */
69
70 /* Oid of the state transition or combine function */
72
73 /* Oid of the serialization function or InvalidOid */
75
76 /* Oid of the deserialization function or InvalidOid */
78
79 /* Oid of state value's datatype */
81
82 /*
83 * fmgr lookup data for transition function or combine function. Note in
84 * particular that the fn_strict flag is kept here.
85 */
87
88 /* fmgr lookup data for serialization function */
90
91 /* fmgr lookup data for deserialization function */
93
94 /* Input collation derived for aggregate */
96
97 /* number of sorting columns */
99
100 /* number of sorting columns to consider in DISTINCT comparisons */
101 /* (this is either zero or the same as numSortCols) */
103
104 /* deconstructed sorting information (arrays of length numSortCols) */
109
110 /*
111 * Comparators for input columns --- only set/used when aggregate has
112 * DISTINCT flag. equalfnOne version is used for single-column
113 * comparisons, equalfnMulti for the case of multiple columns.
114 */
117
118 /*
119 * initial value from pg_aggregate entry
120 */
123
124 /*
125 * We need the len and byval info for the agg's input and transition data
126 * types in order to know how to copy/delete values.
127 *
128 * Note that the info for the input type is used only when handling
129 * DISTINCT aggs with just one argument, so there is only one input type.
130 */
135
136 /*
137 * Slots for holding the evaluated input arguments. These are set up
138 * during ExecInitAgg() and then used for each input row requiring either
139 * FILTER or ORDER BY/DISTINCT processing.
140 */
141 TupleTableSlot *sortslot; /* current input tuple */
142 TupleTableSlot *uniqslot; /* used for multi-column DISTINCT */
143 TupleDesc sortdesc; /* descriptor of input tuples */
144 Datum lastdatum; /* used for single-column DISTINCT */
145 bool lastisnull; /* used for single-column DISTINCT */
146 bool haslast; /* got a last value for DISTINCT check */
147
148 /*
149 * These values are working state that is initialized at the start of an
150 * input tuple group and updated for each input tuple.
151 *
152 * For a simple (non DISTINCT/ORDER BY) aggregate, we just feed the input
153 * values straight to the transition function. If it's DISTINCT or
154 * requires ORDER BY, we pass the input values into a Tuplesort object;
155 * then at completion of the input tuple group, we scan the sorted values,
156 * eliminate duplicates if needed, and run the transition function on the
157 * rest.
158 *
159 * We need a separate tuplesort for each grouping set.
160 */
161
162 Tuplesortstate **sortstates; /* sort objects, if DISTINCT or ORDER BY */
163
164 /*
165 * This field is a pre-initialized FunctionCallInfo struct used for
166 * calling this aggregate's transfn. We save a few cycles per row by not
167 * re-initializing the unchanging fields; which isn't much, but it seems
168 * worth the extra space consumption.
169 */
171
172 /* Likewise for serialization and deserialization functions */
174
177
178/*
179 * AggStatePerAggData - per-aggregate information
180 *
181 * This contains the information needed to call the final function, to produce
182 * a final aggregate result from the state value. If there are multiple
183 * identical Aggrefs in the query, they can all share the same per-agg data.
184 *
185 * These values are set up during ExecInitAgg() and do not change thereafter.
186 */
187typedef struct AggStatePerAggData
188{
189 /*
190 * Link to an Aggref expr this state value is for.
191 *
192 * There can be multiple identical Aggref's sharing the same per-agg. This
193 * points to the first one of them.
194 */
196
197 /* index to the state value which this agg should use */
199
200 /* Optional Oid of final function (may be InvalidOid) */
202
203 /*
204 * fmgr lookup data for final function --- only valid when finalfn_oid is
205 * not InvalidOid.
206 */
208
209 /*
210 * Number of arguments to pass to the finalfn. This is always at least 1
211 * (the transition state value) plus any ordered-set direct args. If the
212 * finalfn wants extra args then we pass nulls corresponding to the
213 * aggregated input columns.
214 */
216
217 /* ExprStates for any direct-argument expressions */
219
220 /*
221 * We need the len and byval info for the agg's result data type in order
222 * to know how to copy/delete values.
223 */
226
227 /*
228 * "shareable" is false if this agg cannot share state values with other
229 * aggregates because the final function is read-write.
230 */
233
234/*
235 * AggStatePerGroupData - per-aggregate-per-group working state
236 *
237 * These values are working state that is initialized at the start of
238 * an input tuple group and updated for each input tuple.
239 *
240 * In AGG_PLAIN and AGG_SORTED modes, we have a single array of these
241 * structs (pointed to by aggstate->pergroup); we re-use the array for
242 * each input group, if it's AGG_SORTED mode. In AGG_HASHED mode, the
243 * hash table contains an array of these structs for each tuple group.
244 *
245 * Logically, the sortstate field belongs in this struct, but we do not
246 * keep it here for space reasons: we don't support DISTINCT aggregates
247 * in AGG_HASHED mode, so there's no reason to use up a pointer field
248 * in every entry of the hashtable.
249 */
251{
252#define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUE 0
253 Datum transValue; /* current transition value */
254#define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUEISNULL 1
256
257#define FIELDNO_AGGSTATEPERGROUPDATA_NOTRANSVALUE 2
258 bool noTransValue; /* true if transValue not set yet */
259
260 /*
261 * Note: noTransValue initially has the same value as transValueIsNull,
262 * and if true both are cleared to false at the same time. They are not
263 * the same though: if transfn later returns a NULL, we want to keep that
264 * NULL and not auto-replace it with a later input value. Only the first
265 * non-NULL input will be auto-substituted.
266 */
268
269/*
270 * AggStatePerPhaseData - per-grouping-set-phase state
271 *
272 * Grouping sets are divided into "phases", where a single phase can be
273 * processed in one pass over the input. If there is more than one phase, then
274 * at the end of input from the current phase, state is reset and another pass
275 * taken over the data which has been re-sorted in the mean time.
276 *
277 * Accordingly, each phase specifies a list of grouping sets and group clause
278 * information, plus each phase after the first also has a sort order.
279 */
281{
282 AggStrategy aggstrategy; /* strategy for this phase */
283 int numsets; /* number of grouping sets (or 0) */
284 int *gset_lengths; /* lengths of grouping sets */
285 Bitmapset **grouped_cols; /* column groupings for rollup */
286 ExprState **eqfunctions; /* expression returning equality, indexed by
287 * nr of cols to compare */
288 Agg *aggnode; /* Agg node for phase data */
289 Sort *sortnode; /* Sort node for input ordering for phase */
290
291 ExprState *evaltrans; /* evaluation of transition functions */
292
293 /*----------
294 * Cached variants of the compiled expression.
295 * first subscript: 0: outerops; 1: TTSOpsMinimalTuple
296 * second subscript: 0: no NULL check; 1: with NULL check
297 *----------
298 */
301
302/*
303 * AggStatePerHashData - per-hashtable state
304 *
305 * When doing grouping sets with hashing, we have one of these for each
306 * grouping set. (When doing hashing without grouping sets, we have just one of
307 * them.)
308 */
310{
311 TupleHashTable hashtable; /* hash table with one entry per group */
312 TupleHashIterator hashiter; /* for iterating through hash table */
313 TupleTableSlot *hashslot; /* slot for loading hash table */
314 FmgrInfo *hashfunctions; /* per-grouping-field hash fns */
315 Oid *eqfuncoids; /* per-grouping-field equality fns */
316 int numCols; /* number of hash key columns */
317 int numhashGrpCols; /* number of columns in hash table */
318 int largestGrpColIdx; /* largest col required for hashing */
319 AttrNumber *hashGrpColIdxInput; /* hash col indices in input slot */
320 AttrNumber *hashGrpColIdxHash; /* indices in hash table tuples */
321 Agg *aggnode; /* original Agg node, for numGroups etc. */
323
324
325extern AggState *ExecInitAgg(Agg *node, EState *estate, int eflags);
326extern void ExecEndAgg(AggState *node);
327extern void ExecReScanAgg(AggState *node);
328
329extern Size hash_agg_entry_size(int numTrans, Size tupleWidth,
330 Size transitionSpace);
331extern void hash_agg_set_limits(double hashentrysize, double input_groups,
332 int used_bits, Size *mem_limit,
333 uint64 *ngroups_limit, int *num_partitions);
334
335/* parallel instrumentation support */
336extern void ExecAggEstimate(AggState *node, ParallelContext *pcxt);
337extern void ExecAggInitializeDSM(AggState *node, ParallelContext *pcxt);
340
341#endif /* NODEAGG_H */
int16 AttrNumber
Definition: attnum.h:21
int16_t int16
Definition: c.h:480
uint64_t uint64
Definition: c.h:486
size_t Size
Definition: c.h:559
tuplehash_iterator TupleHashIterator
Definition: execnodes.h:847
void ExecAggEstimate(AggState *node, ParallelContext *pcxt)
Definition: nodeAgg.c:4683
struct AggStatePerAggData AggStatePerAggData
void ExecAggInitializeWorker(AggState *node, ParallelWorkerContext *pwcxt)
Definition: nodeAgg.c:4729
struct AggStatePerTransData AggStatePerTransData
void ExecAggRetrieveInstrumentation(AggState *node)
Definition: nodeAgg.c:4742
struct AggStatePerGroupData AggStatePerGroupData
void ExecReScanAgg(AggState *node)
Definition: nodeAgg.c:4364
Size hash_agg_entry_size(int numTrans, Size tupleWidth, Size transitionSpace)
Definition: nodeAgg.c:1694
void ExecAggInitializeDSM(AggState *node, ParallelContext *pcxt)
Definition: nodeAgg.c:4704
struct AggStatePerPhaseData AggStatePerPhaseData
struct AggStatePerHashData AggStatePerHashData
void ExecEndAgg(AggState *node)
Definition: nodeAgg.c:4304
AggState * ExecInitAgg(Agg *node, EState *estate, int eflags)
Definition: nodeAgg.c:3173
void hash_agg_set_limits(double hashentrysize, double input_groups, int used_bits, Size *mem_limit, uint64 *ngroups_limit, int *num_partitions)
Definition: nodeAgg.c:1798
AggStrategy
Definition: nodes.h:353
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
FmgrInfo finalfn
Definition: nodeAgg.h:207
bool resulttypeByVal
Definition: nodeAgg.h:225
List * aggdirectargs
Definition: nodeAgg.h:218
Aggref * aggref
Definition: nodeAgg.h:195
int16 resulttypeLen
Definition: nodeAgg.h:224
FmgrInfo * hashfunctions
Definition: nodeAgg.h:314
TupleHashTable hashtable
Definition: nodeAgg.h:311
TupleTableSlot * hashslot
Definition: nodeAgg.h:313
TupleHashIterator hashiter
Definition: nodeAgg.h:312
AttrNumber * hashGrpColIdxHash
Definition: nodeAgg.h:320
AttrNumber * hashGrpColIdxInput
Definition: nodeAgg.h:319
Bitmapset ** grouped_cols
Definition: nodeAgg.h:285
ExprState * evaltrans
Definition: nodeAgg.h:291
ExprState * evaltrans_cache[2][2]
Definition: nodeAgg.h:299
ExprState ** eqfunctions
Definition: nodeAgg.h:286
AggStrategy aggstrategy
Definition: nodeAgg.h:282
bool * sortNullsFirst
Definition: nodeAgg.h:108
FmgrInfo serialfn
Definition: nodeAgg.h:89
FmgrInfo equalfnOne
Definition: nodeAgg.h:115
TupleDesc sortdesc
Definition: nodeAgg.h:143
TupleTableSlot * sortslot
Definition: nodeAgg.h:141
FmgrInfo transfn
Definition: nodeAgg.h:86
Aggref * aggref
Definition: nodeAgg.h:44
ExprState * equalfnMulti
Definition: nodeAgg.h:116
Tuplesortstate ** sortstates
Definition: nodeAgg.h:162
TupleTableSlot * uniqslot
Definition: nodeAgg.h:142
FmgrInfo deserialfn
Definition: nodeAgg.h:92
FunctionCallInfo deserialfn_fcinfo
Definition: nodeAgg.h:175
AttrNumber * sortColIdx
Definition: nodeAgg.h:105
FunctionCallInfo serialfn_fcinfo
Definition: nodeAgg.h:173
FunctionCallInfo transfn_fcinfo
Definition: nodeAgg.h:170
Definition: plannodes.h:998
Definition: fmgr.h:57
Definition: pg_list.h:54