| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* nodeAgg.h |
| 4 |
* prototypes for nodeAgg.c |
| 5 |
* |
| 6 |
* |
| 7 |
* Portions Copyright (c) 1996-2025, 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 |
*/ |
| 30 |
typedef struct AggStatePerTransData |
| 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 |
*/ |
| 44 |
Aggref *aggref; |
| 45 |
|
| 46 |
/* |
| 47 |
* Is this state value actually being shared by more than one Aggref? |
| 48 |
*/ |
| 49 |
bool aggshared; |
| 50 |
|
| 51 |
/* |
| 52 |
* True for ORDER BY and DISTINCT Aggrefs that are not aggpresorted. |
| 53 |
*/ |
| 54 |
bool aggsortrequired; |
| 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 |
*/ |
| 61 |
int numInputs; |
| 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 |
*/ |
| 68 |
int numTransInputs; |
| 69 |
|
| 70 |
/* Oid of the state transition or combine function */ |
| 71 |
Oid transfn_oid; |
| 72 |
|
| 73 |
/* Oid of the serialization function or InvalidOid */ |
| 74 |
Oid serialfn_oid; |
| 75 |
|
| 76 |
/* Oid of the deserialization function or InvalidOid */ |
| 77 |
Oid deserialfn_oid; |
| 78 |
|
| 79 |
/* Oid of state value's datatype */ |
| 80 |
Oid aggtranstype; |
| 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 |
*/ |
| 86 |
FmgrInfo transfn; |
| 87 |
|
| 88 |
/* fmgr lookup data for serialization function */ |
| 89 |
FmgrInfo serialfn; |
| 90 |
|
| 91 |
/* fmgr lookup data for deserialization function */ |
| 92 |
FmgrInfo deserialfn; |
| 93 |
|
| 94 |
/* Input collation derived for aggregate */ |
| 95 |
Oid aggCollation; |
| 96 |
|
| 97 |
/* number of sorting columns */ |
| 98 |
int numSortCols; |
| 99 |
|
| 100 |
/* number of sorting columns to consider in DISTINCT comparisons */ |
| 101 |
/* (this is either zero or the same as numSortCols) */ |
| 102 |
int numDistinctCols; |
| 103 |
|
| 104 |
/* deconstructed sorting information (arrays of length numSortCols) */ |
| 105 |
AttrNumber *sortColIdx; |
| 106 |
Oid *sortOperators; |
| 107 |
Oid *sortCollations; |
| 108 |
bool *sortNullsFirst; |
| 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 |
*/ |
| 115 |
FmgrInfo equalfnOne; |
| 116 |
ExprState *equalfnMulti; |
| 117 |
|
| 118 |
/* |
| 119 |
* initial value from pg_aggregate entry |
| 120 |
*/ |
| 121 |
Datum initValue; |
| 122 |
bool initValueIsNull; |
| 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 |
*/ |
| 131 |
int16 inputtypeLen, |
| 132 |
transtypeLen; |
| 133 |
bool inputtypeByVal, |
| 134 |
transtypeByVal; |
| 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 |
*/ |
| 170 |
FunctionCallInfo transfn_fcinfo; |
| 171 |
|
| 172 |
/* Likewise for serialization and deserialization functions */ |
| 173 |
FunctionCallInfo serialfn_fcinfo; |
| 174 |
|
| 175 |
FunctionCallInfo deserialfn_fcinfo; |
| 176 |
} AggStatePerTransData; |
| 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 |
*/ |
| 187 |
typedef 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 |
*/ |
| 195 |
Aggref *aggref; |
| 196 |
|
| 197 |
/* index to the state value which this agg should use */ |
| 198 |
int transno; |
| 199 |
|
| 200 |
/* Optional Oid of final function (may be InvalidOid) */ |
| 201 |
Oid finalfn_oid; |
| 202 |
|
| 203 |
/* |
| 204 |
* fmgr lookup data for final function --- only valid when finalfn_oid is |
| 205 |
* not InvalidOid. |
| 206 |
*/ |
| 207 |
FmgrInfo finalfn; |
| 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 |
*/ |
| 215 |
int numFinalArgs; |
| 216 |
|
| 217 |
/* ExprStates for any direct-argument expressions */ |
| 218 |
List *aggdirectargs; |
| 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 |
*/ |
| 224 |
int16 resulttypeLen; |
| 225 |
bool resulttypeByVal; |
| 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 |
*/ |
| 231 |
bool shareable; |
| 232 |
} AggStatePerAggData; |
| 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 |
*/ |
| 250 |
typedef struct AggStatePerGroupData |
| 251 |
{ |
| 252 |
#define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUE 0 |
| 253 |
Datum transValue; /* current transition value */ |
| 254 |
#define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUEISNULL 1 |
| 255 |
bool transValueIsNull; |
| 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 |
*/ |
| 267 |
} AggStatePerGroupData; |
| 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 |
*/ |
| 280 |
typedef struct AggStatePerPhaseData |
| 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 |
*/ |
| 299 |
ExprState *evaltrans_cache[2][2]; |
| 300 |
} AggStatePerPhaseData; |
| 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 |
*/ |
| 309 |
typedef struct AggStatePerHashData |
| 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. */ |
| 322 |
} AggStatePerHashData; |
| 323 |
|
| 324 |
|
| 325 |
extern AggState *ExecInitAgg(Agg *node, EState *estate, int eflags); |
| 326 |
extern void ExecEndAgg(AggState *node); |
| 327 |
extern void ExecReScanAgg(AggState *node); |
| 328 |
|
| 329 |
extern Size hash_agg_entry_size(int numTrans, Size tupleWidth, |
| 330 |
Size transitionSpace); |
| 331 |
extern 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 */ |
| 336 |
extern void ExecAggEstimate(AggState *node, ParallelContext *pcxt); |
| 337 |
extern void ExecAggInitializeDSM(AggState *node, ParallelContext *pcxt); |
| 338 |
extern void ExecAggInitializeWorker(AggState *node, ParallelWorkerContext *pwcxt); |
| 339 |
extern void ExecAggRetrieveInstrumentation(AggState *node); |
| 340 |
|
| 341 |
#endif /* NODEAGG_H */ |