PostgreSQL Source Code  git master
nodeAgg.h File Reference
#include "access/parallel.h"
#include "nodes/execnodes.h"
Include dependency graph for nodeAgg.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  AggStatePerTransData
 
struct  AggStatePerAggData
 
struct  AggStatePerGroupData
 
struct  AggStatePerPhaseData
 
struct  AggStatePerHashData
 

Macros

#define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUE   0
 
#define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUEISNULL   1
 
#define FIELDNO_AGGSTATEPERGROUPDATA_NOTRANSVALUE   2
 

Typedefs

typedef struct AggStatePerTransData AggStatePerTransData
 
typedef struct AggStatePerAggData AggStatePerAggData
 
typedef struct AggStatePerGroupData AggStatePerGroupData
 
typedef struct AggStatePerPhaseData AggStatePerPhaseData
 
typedef struct AggStatePerHashData AggStatePerHashData
 

Functions

AggStateExecInitAgg (Agg *node, EState *estate, int eflags)
 
void ExecEndAgg (AggState *node)
 
void ExecReScanAgg (AggState *node)
 
Size hash_agg_entry_size (int numTrans, Size tupleWidth, Size transitionSpace)
 
void hash_agg_set_limits (double hashentrysize, double input_groups, int used_bits, Size *mem_limit, uint64 *ngroups_limit, int *num_partitions)
 
void ExecAggEstimate (AggState *node, ParallelContext *pcxt)
 
void ExecAggInitializeDSM (AggState *node, ParallelContext *pcxt)
 
void ExecAggInitializeWorker (AggState *node, ParallelWorkerContext *pwcxt)
 
void ExecAggRetrieveInstrumentation (AggState *node)
 

Macro Definition Documentation

◆ FIELDNO_AGGSTATEPERGROUPDATA_NOTRANSVALUE

#define FIELDNO_AGGSTATEPERGROUPDATA_NOTRANSVALUE   2

Definition at line 257 of file nodeAgg.h.

◆ FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUE

#define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUE   0

Definition at line 252 of file nodeAgg.h.

◆ FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUEISNULL

#define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUEISNULL   1

Definition at line 254 of file nodeAgg.h.

Typedef Documentation

◆ AggStatePerAggData

◆ AggStatePerGroupData

◆ AggStatePerHashData

◆ AggStatePerPhaseData

◆ AggStatePerTransData

Function Documentation

◆ ExecAggEstimate()

void ExecAggEstimate ( AggState node,
ParallelContext pcxt 
)

Definition at line 4694 of file nodeAgg.c.

4695 {
4696  Size size;
4697 
4698  /* don't need this if not instrumenting or no workers */
4699  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
4700  return;
4701 
4702  size = mul_size(pcxt->nworkers, sizeof(AggregateInstrumentation));
4703  size = add_size(size, offsetof(SharedAggInfo, sinstrument));
4704  shm_toc_estimate_chunk(&pcxt->estimator, size);
4705  shm_toc_estimate_keys(&pcxt->estimator, 1);
4706 }
size_t Size
Definition: c.h:594
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
ScanState ss
Definition: execnodes.h:2379
shm_toc_estimator estimator
Definition: parallel.h:42
Instrumentation * instrument
Definition: execnodes.h:1047
PlanState ps
Definition: execnodes.h:1474

References add_size(), ParallelContext::estimator, PlanState::instrument, mul_size(), ParallelContext::nworkers, ScanState::ps, shm_toc_estimate_chunk, shm_toc_estimate_keys, and AggState::ss.

Referenced by ExecParallelEstimate().

◆ ExecAggInitializeDSM()

void ExecAggInitializeDSM ( AggState node,
ParallelContext pcxt 
)

Definition at line 4715 of file nodeAgg.c.

4716 {
4717  Size size;
4718 
4719  /* don't need this if not instrumenting or no workers */
4720  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
4721  return;
4722 
4723  size = offsetof(SharedAggInfo, sinstrument)
4724  + pcxt->nworkers * sizeof(AggregateInstrumentation);
4725  node->shared_info = shm_toc_allocate(pcxt->toc, size);
4726  /* ensure any unfilled slots will contain zeroes */
4727  memset(node->shared_info, 0, size);
4728  node->shared_info->num_workers = pcxt->nworkers;
4729  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
4730  node->shared_info);
4731 }
struct AggregateInstrumentation AggregateInstrumentation
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
SharedAggInfo * shared_info
Definition: execnodes.h:2451
shm_toc * toc
Definition: parallel.h:45
Plan * plan
Definition: execnodes.h:1037
int plan_node_id
Definition: plannodes.h:152

References PlanState::instrument, SharedAggInfo::num_workers, ParallelContext::nworkers, PlanState::plan, Plan::plan_node_id, ScanState::ps, AggState::shared_info, shm_toc_allocate(), shm_toc_insert(), AggState::ss, and ParallelContext::toc.

Referenced by ExecParallelInitializeDSM().

◆ ExecAggInitializeWorker()

void ExecAggInitializeWorker ( AggState node,
ParallelWorkerContext pwcxt 
)

Definition at line 4740 of file nodeAgg.c.

4741 {
4742  node->shared_info =
4743  shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
4744 }
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

References PlanState::plan, Plan::plan_node_id, ScanState::ps, AggState::shared_info, shm_toc_lookup(), AggState::ss, and ParallelWorkerContext::toc.

Referenced by ExecParallelInitializeWorker().

◆ ExecAggRetrieveInstrumentation()

void ExecAggRetrieveInstrumentation ( AggState node)

Definition at line 4753 of file nodeAgg.c.

4754 {
4755  Size size;
4756  SharedAggInfo *si;
4757 
4758  if (node->shared_info == NULL)
4759  return;
4760 
4761  size = offsetof(SharedAggInfo, sinstrument)
4763  si = palloc(size);
4764  memcpy(si, node->shared_info, size);
4765  node->shared_info = si;
4766 }
void * palloc(Size size)
Definition: mcxt.c:1226

References SharedAggInfo::num_workers, palloc(), and AggState::shared_info.

Referenced by ExecParallelRetrieveInstrumentation().

◆ ExecEndAgg()

void ExecEndAgg ( AggState node)

Definition at line 4305 of file nodeAgg.c.

4306 {
4308  int transno;
4309  int numGroupingSets = Max(node->maxsets, 1);
4310  int setno;
4311 
4312  /*
4313  * When ending a parallel worker, copy the statistics gathered by the
4314  * worker back into shared memory so that it can be picked up by the main
4315  * process to report in EXPLAIN ANALYZE.
4316  */
4317  if (node->shared_info && IsParallelWorker())
4318  {
4320 
4321  Assert(ParallelWorkerNumber <= node->shared_info->num_workers);
4324  si->hash_disk_used = node->hash_disk_used;
4325  si->hash_mem_peak = node->hash_mem_peak;
4326  }
4327 
4328  /* Make sure we have closed any open tuplesorts */
4329 
4330  if (node->sort_in)
4331  tuplesort_end(node->sort_in);
4332  if (node->sort_out)
4333  tuplesort_end(node->sort_out);
4334 
4336 
4337  if (node->hash_metacxt != NULL)
4338  {
4340  node->hash_metacxt = NULL;
4341  }
4342 
4343  for (transno = 0; transno < node->numtrans; transno++)
4344  {
4345  AggStatePerTrans pertrans = &node->pertrans[transno];
4346 
4347  for (setno = 0; setno < numGroupingSets; setno++)
4348  {
4349  if (pertrans->sortstates[setno])
4350  tuplesort_end(pertrans->sortstates[setno]);
4351  }
4352  }
4353 
4354  /* And ensure any agg shutdown callbacks have been called */
4355  for (setno = 0; setno < numGroupingSets; setno++)
4356  ReScanExprContext(node->aggcontexts[setno]);
4357  if (node->hashcontext)
4359 
4360  /*
4361  * We don't actually free any ExprContexts here (see comment in
4362  * ExecFreeExprContext), just unlinking the output one from the plan node
4363  * suffices.
4364  */
4365  ExecFreeExprContext(&node->ss.ps);
4366 
4367  /* clean up tuple table */
4369 
4370  outerPlan = outerPlanState(node);
4372 }
int ParallelWorkerNumber
Definition: parallel.c:114
#define Max(x, y)
Definition: c.h:987
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:557
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:446
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:658
#define outerPlanState(node)
Definition: execnodes.h:1133
#define IsParallelWorker()
Definition: parallel.h:61
Assert(fmt[strlen(fmt) - 1] !='\n')
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:403
static void hashagg_reset_spill_state(AggState *aggstate)
Definition: nodeAgg.c:3134
#define outerPlan(node)
Definition: plannodes.h:183
Tuplesortstate ** sortstates
Definition: nodeAgg.h:162
MemoryContext hash_metacxt
Definition: execnodes.h:2421
Tuplesortstate * sort_out
Definition: execnodes.h:2412
uint64 hash_disk_used
Definition: execnodes.h:2439
AggStatePerTrans pertrans
Definition: execnodes.h:2389
int numtrans
Definition: execnodes.h:2382
ExprContext * hashcontext
Definition: execnodes.h:2390
Size hash_mem_peak
Definition: execnodes.h:2436
int hash_batches_used
Definition: execnodes.h:2440
Tuplesortstate * sort_in
Definition: execnodes.h:2411
int maxsets
Definition: execnodes.h:2409
ExprContext ** aggcontexts
Definition: execnodes.h:2391
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1477
AggregateInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:2355
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:969
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:432

References AggState::aggcontexts, Assert(), ExecClearTuple(), ExecEndNode(), ExecFreeExprContext(), AggregateInstrumentation::hash_batches_used, AggState::hash_batches_used, AggregateInstrumentation::hash_disk_used, AggState::hash_disk_used, AggregateInstrumentation::hash_mem_peak, AggState::hash_mem_peak, AggState::hash_metacxt, hashagg_reset_spill_state(), AggState::hashcontext, IsParallelWorker, Max, AggState::maxsets, MemoryContextDelete(), AggState::numtrans, outerPlan, outerPlanState, ParallelWorkerNumber, AggState::pertrans, ScanState::ps, ReScanExprContext(), AggState::shared_info, SharedAggInfo::sinstrument, AggState::sort_in, AggState::sort_out, AggStatePerTransData::sortstates, AggState::ss, ScanState::ss_ScanTupleSlot, and tuplesort_end().

Referenced by ExecEndNode().

◆ ExecInitAgg()

AggState* ExecInitAgg ( Agg node,
EState estate,
int  eflags 
)

Definition at line 3174 of file nodeAgg.c.

3175 {
3176  AggState *aggstate;
3177  AggStatePerAgg peraggs;
3178  AggStatePerTrans pertransstates;
3179  AggStatePerGroup *pergroups;
3180  Plan *outerPlan;
3181  ExprContext *econtext;
3182  TupleDesc scanDesc;
3183  int max_aggno;
3184  int max_transno;
3185  int numaggrefs;
3186  int numaggs;
3187  int numtrans;
3188  int phase;
3189  int phaseidx;
3190  ListCell *l;
3191  Bitmapset *all_grouped_cols = NULL;
3192  int numGroupingSets = 1;
3193  int numPhases;
3194  int numHashes;
3195  int i = 0;
3196  int j = 0;
3197  bool use_hashing = (node->aggstrategy == AGG_HASHED ||
3198  node->aggstrategy == AGG_MIXED);
3199 
3200  /* check for unsupported flags */
3201  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
3202 
3203  /*
3204  * create state structure
3205  */
3206  aggstate = makeNode(AggState);
3207  aggstate->ss.ps.plan = (Plan *) node;
3208  aggstate->ss.ps.state = estate;
3209  aggstate->ss.ps.ExecProcNode = ExecAgg;
3210 
3211  aggstate->aggs = NIL;
3212  aggstate->numaggs = 0;
3213  aggstate->numtrans = 0;
3214  aggstate->aggstrategy = node->aggstrategy;
3215  aggstate->aggsplit = node->aggsplit;
3216  aggstate->maxsets = 0;
3217  aggstate->projected_set = -1;
3218  aggstate->current_set = 0;
3219  aggstate->peragg = NULL;
3220  aggstate->pertrans = NULL;
3221  aggstate->curperagg = NULL;
3222  aggstate->curpertrans = NULL;
3223  aggstate->input_done = false;
3224  aggstate->agg_done = false;
3225  aggstate->pergroups = NULL;
3226  aggstate->grp_firstTuple = NULL;
3227  aggstate->sort_in = NULL;
3228  aggstate->sort_out = NULL;
3229 
3230  /*
3231  * phases[0] always exists, but is dummy in sorted/plain mode
3232  */
3233  numPhases = (use_hashing ? 1 : 2);
3234  numHashes = (use_hashing ? 1 : 0);
3235 
3236  /*
3237  * Calculate the maximum number of grouping sets in any phase; this
3238  * determines the size of some allocations. Also calculate the number of
3239  * phases, since all hashed/mixed nodes contribute to only a single phase.
3240  */
3241  if (node->groupingSets)
3242  {
3243  numGroupingSets = list_length(node->groupingSets);
3244 
3245  foreach(l, node->chain)
3246  {
3247  Agg *agg = lfirst(l);
3248 
3249  numGroupingSets = Max(numGroupingSets,
3250  list_length(agg->groupingSets));
3251 
3252  /*
3253  * additional AGG_HASHED aggs become part of phase 0, but all
3254  * others add an extra phase.
3255  */
3256  if (agg->aggstrategy != AGG_HASHED)
3257  ++numPhases;
3258  else
3259  ++numHashes;
3260  }
3261  }
3262 
3263  aggstate->maxsets = numGroupingSets;
3264  aggstate->numphases = numPhases;
3265 
3266  aggstate->aggcontexts = (ExprContext **)
3267  palloc0(sizeof(ExprContext *) * numGroupingSets);
3268 
3269  /*
3270  * Create expression contexts. We need three or more, one for
3271  * per-input-tuple processing, one for per-output-tuple processing, one
3272  * for all the hashtables, and one for each grouping set. The per-tuple
3273  * memory context of the per-grouping-set ExprContexts (aggcontexts)
3274  * replaces the standalone memory context formerly used to hold transition
3275  * values. We cheat a little by using ExecAssignExprContext() to build
3276  * all of them.
3277  *
3278  * NOTE: the details of what is stored in aggcontexts and what is stored
3279  * in the regular per-query memory context are driven by a simple
3280  * decision: we want to reset the aggcontext at group boundaries (if not
3281  * hashing) and in ExecReScanAgg to recover no-longer-wanted space.
3282  */
3283  ExecAssignExprContext(estate, &aggstate->ss.ps);
3284  aggstate->tmpcontext = aggstate->ss.ps.ps_ExprContext;
3285 
3286  for (i = 0; i < numGroupingSets; ++i)
3287  {
3288  ExecAssignExprContext(estate, &aggstate->ss.ps);
3289  aggstate->aggcontexts[i] = aggstate->ss.ps.ps_ExprContext;
3290  }
3291 
3292  if (use_hashing)
3293  aggstate->hashcontext = CreateWorkExprContext(estate);
3294 
3295  ExecAssignExprContext(estate, &aggstate->ss.ps);
3296 
3297  /*
3298  * Initialize child nodes.
3299  *
3300  * If we are doing a hashed aggregation then the child plan does not need
3301  * to handle REWIND efficiently; see ExecReScanAgg.
3302  */
3303  if (node->aggstrategy == AGG_HASHED)
3304  eflags &= ~EXEC_FLAG_REWIND;
3305  outerPlan = outerPlan(node);
3306  outerPlanState(aggstate) = ExecInitNode(outerPlan, estate, eflags);
3307 
3308  /*
3309  * initialize source tuple type.
3310  */
3311  aggstate->ss.ps.outerops =
3313  &aggstate->ss.ps.outeropsfixed);
3314  aggstate->ss.ps.outeropsset = true;
3315 
3316  ExecCreateScanSlotFromOuterPlan(estate, &aggstate->ss,
3317  aggstate->ss.ps.outerops);
3318  scanDesc = aggstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
3319 
3320  /*
3321  * If there are more than two phases (including a potential dummy phase
3322  * 0), input will be resorted using tuplesort. Need a slot for that.
3323  */
3324  if (numPhases > 2)
3325  {
3326  aggstate->sort_slot = ExecInitExtraTupleSlot(estate, scanDesc,
3328 
3329  /*
3330  * The output of the tuplesort, and the output from the outer child
3331  * might not use the same type of slot. In most cases the child will
3332  * be a Sort, and thus return a TTSOpsMinimalTuple type slot - but the
3333  * input can also be presorted due an index, in which case it could be
3334  * a different type of slot.
3335  *
3336  * XXX: For efficiency it would be good to instead/additionally
3337  * generate expressions with corresponding settings of outerops* for
3338  * the individual phases - deforming is often a bottleneck for
3339  * aggregations with lots of rows per group. If there's multiple
3340  * sorts, we know that all but the first use TTSOpsMinimalTuple (via
3341  * the nodeAgg.c internal tuplesort).
3342  */
3343  if (aggstate->ss.ps.outeropsfixed &&
3344  aggstate->ss.ps.outerops != &TTSOpsMinimalTuple)
3345  aggstate->ss.ps.outeropsfixed = false;
3346  }
3347 
3348  /*
3349  * Initialize result type, slot and projection.
3350  */
3352  ExecAssignProjectionInfo(&aggstate->ss.ps, NULL);
3353 
3354  /*
3355  * initialize child expressions
3356  *
3357  * We expect the parser to have checked that no aggs contain other agg
3358  * calls in their arguments (and just to be sure, we verify it again while
3359  * initializing the plan node). This would make no sense under SQL
3360  * semantics, and it's forbidden by the spec. Because it is true, we
3361  * don't need to worry about evaluating the aggs in any particular order.
3362  *
3363  * Note: execExpr.c finds Aggrefs for us, and adds them to aggstate->aggs.
3364  * Aggrefs in the qual are found here; Aggrefs in the targetlist are found
3365  * during ExecAssignProjectionInfo, above.
3366  */
3367  aggstate->ss.ps.qual =
3368  ExecInitQual(node->plan.qual, (PlanState *) aggstate);
3369 
3370  /*
3371  * We should now have found all Aggrefs in the targetlist and quals.
3372  */
3373  numaggrefs = list_length(aggstate->aggs);
3374  max_aggno = -1;
3375  max_transno = -1;
3376  foreach(l, aggstate->aggs)
3377  {
3378  Aggref *aggref = (Aggref *) lfirst(l);
3379 
3380  max_aggno = Max(max_aggno, aggref->aggno);
3381  max_transno = Max(max_transno, aggref->aggtransno);
3382  }
3383  numaggs = max_aggno + 1;
3384  numtrans = max_transno + 1;
3385 
3386  /*
3387  * For each phase, prepare grouping set data and fmgr lookup data for
3388  * compare functions. Accumulate all_grouped_cols in passing.
3389  */
3390  aggstate->phases = palloc0(numPhases * sizeof(AggStatePerPhaseData));
3391 
3392  aggstate->num_hashes = numHashes;
3393  if (numHashes)
3394  {
3395  aggstate->perhash = palloc0(sizeof(AggStatePerHashData) * numHashes);
3396  aggstate->phases[0].numsets = 0;
3397  aggstate->phases[0].gset_lengths = palloc(numHashes * sizeof(int));
3398  aggstate->phases[0].grouped_cols = palloc(numHashes * sizeof(Bitmapset *));
3399  }
3400 
3401  phase = 0;
3402  for (phaseidx = 0; phaseidx <= list_length(node->chain); ++phaseidx)
3403  {
3404  Agg *aggnode;
3405  Sort *sortnode;
3406 
3407  if (phaseidx > 0)
3408  {
3409  aggnode = list_nth_node(Agg, node->chain, phaseidx - 1);
3410  sortnode = castNode(Sort, outerPlan(aggnode));
3411  }
3412  else
3413  {
3414  aggnode = node;
3415  sortnode = NULL;
3416  }
3417 
3418  Assert(phase <= 1 || sortnode);
3419 
3420  if (aggnode->aggstrategy == AGG_HASHED
3421  || aggnode->aggstrategy == AGG_MIXED)
3422  {
3423  AggStatePerPhase phasedata = &aggstate->phases[0];
3424  AggStatePerHash perhash;
3425  Bitmapset *cols = NULL;
3426 
3427  Assert(phase == 0);
3428  i = phasedata->numsets++;
3429  perhash = &aggstate->perhash[i];
3430 
3431  /* phase 0 always points to the "real" Agg in the hash case */
3432  phasedata->aggnode = node;
3433  phasedata->aggstrategy = node->aggstrategy;
3434 
3435  /* but the actual Agg node representing this hash is saved here */
3436  perhash->aggnode = aggnode;
3437 
3438  phasedata->gset_lengths[i] = perhash->numCols = aggnode->numCols;
3439 
3440  for (j = 0; j < aggnode->numCols; ++j)
3441  cols = bms_add_member(cols, aggnode->grpColIdx[j]);
3442 
3443  phasedata->grouped_cols[i] = cols;
3444 
3445  all_grouped_cols = bms_add_members(all_grouped_cols, cols);
3446  continue;
3447  }
3448  else
3449  {
3450  AggStatePerPhase phasedata = &aggstate->phases[++phase];
3451  int num_sets;
3452 
3453  phasedata->numsets = num_sets = list_length(aggnode->groupingSets);
3454 
3455  if (num_sets)
3456  {
3457  phasedata->gset_lengths = palloc(num_sets * sizeof(int));
3458  phasedata->grouped_cols = palloc(num_sets * sizeof(Bitmapset *));
3459 
3460  i = 0;
3461  foreach(l, aggnode->groupingSets)
3462  {
3463  int current_length = list_length(lfirst(l));
3464  Bitmapset *cols = NULL;
3465 
3466  /* planner forces this to be correct */
3467  for (j = 0; j < current_length; ++j)
3468  cols = bms_add_member(cols, aggnode->grpColIdx[j]);
3469 
3470  phasedata->grouped_cols[i] = cols;
3471  phasedata->gset_lengths[i] = current_length;
3472 
3473  ++i;
3474  }
3475 
3476  all_grouped_cols = bms_add_members(all_grouped_cols,
3477  phasedata->grouped_cols[0]);
3478  }
3479  else
3480  {
3481  Assert(phaseidx == 0);
3482 
3483  phasedata->gset_lengths = NULL;
3484  phasedata->grouped_cols = NULL;
3485  }
3486 
3487  /*
3488  * If we are grouping, precompute fmgr lookup data for inner loop.
3489  */
3490  if (aggnode->aggstrategy == AGG_SORTED)
3491  {
3492  /*
3493  * Build a separate function for each subset of columns that
3494  * need to be compared.
3495  */
3496  phasedata->eqfunctions =
3497  (ExprState **) palloc0(aggnode->numCols * sizeof(ExprState *));
3498 
3499  /* for each grouping set */
3500  for (int k = 0; k < phasedata->numsets; k++)
3501  {
3502  int length = phasedata->gset_lengths[k];
3503 
3504  /* nothing to do for empty grouping set */
3505  if (length == 0)
3506  continue;
3507 
3508  /* if we already had one of this length, it'll do */
3509  if (phasedata->eqfunctions[length - 1] != NULL)
3510  continue;
3511 
3512  phasedata->eqfunctions[length - 1] =
3513  execTuplesMatchPrepare(scanDesc,
3514  length,
3515  aggnode->grpColIdx,
3516  aggnode->grpOperators,
3517  aggnode->grpCollations,
3518  (PlanState *) aggstate);
3519  }
3520 
3521  /* and for all grouped columns, unless already computed */
3522  if (aggnode->numCols > 0 &&
3523  phasedata->eqfunctions[aggnode->numCols - 1] == NULL)
3524  {
3525  phasedata->eqfunctions[aggnode->numCols - 1] =
3526  execTuplesMatchPrepare(scanDesc,
3527  aggnode->numCols,
3528  aggnode->grpColIdx,
3529  aggnode->grpOperators,
3530  aggnode->grpCollations,
3531  (PlanState *) aggstate);
3532  }
3533  }
3534 
3535  phasedata->aggnode = aggnode;
3536  phasedata->aggstrategy = aggnode->aggstrategy;
3537  phasedata->sortnode = sortnode;
3538  }
3539  }
3540 
3541  /*
3542  * Convert all_grouped_cols to a descending-order list.
3543  */
3544  i = -1;
3545  while ((i = bms_next_member(all_grouped_cols, i)) >= 0)
3546  aggstate->all_grouped_cols = lcons_int(i, aggstate->all_grouped_cols);
3547 
3548  /*
3549  * Set up aggregate-result storage in the output expr context, and also
3550  * allocate my private per-agg working storage
3551  */
3552  econtext = aggstate->ss.ps.ps_ExprContext;
3553  econtext->ecxt_aggvalues = (Datum *) palloc0(sizeof(Datum) * numaggs);
3554  econtext->ecxt_aggnulls = (bool *) palloc0(sizeof(bool) * numaggs);
3555 
3556  peraggs = (AggStatePerAgg) palloc0(sizeof(AggStatePerAggData) * numaggs);
3557  pertransstates = (AggStatePerTrans) palloc0(sizeof(AggStatePerTransData) * numtrans);
3558 
3559  aggstate->peragg = peraggs;
3560  aggstate->pertrans = pertransstates;
3561 
3562 
3563  aggstate->all_pergroups =
3565  * (numGroupingSets + numHashes));
3566  pergroups = aggstate->all_pergroups;
3567 
3568  if (node->aggstrategy != AGG_HASHED)
3569  {
3570  for (i = 0; i < numGroupingSets; i++)
3571  {
3572  pergroups[i] = (AggStatePerGroup) palloc0(sizeof(AggStatePerGroupData)
3573  * numaggs);
3574  }
3575 
3576  aggstate->pergroups = pergroups;
3577  pergroups += numGroupingSets;
3578  }
3579 
3580  /*
3581  * Hashing can only appear in the initial phase.
3582  */
3583  if (use_hashing)
3584  {
3585  Plan *outerplan = outerPlan(node);
3586  uint64 totalGroups = 0;
3587 
3588  aggstate->hash_metacxt = AllocSetContextCreate(aggstate->ss.ps.state->es_query_cxt,
3589  "HashAgg meta context",
3591  aggstate->hash_spill_rslot = ExecInitExtraTupleSlot(estate, scanDesc,
3593  aggstate->hash_spill_wslot = ExecInitExtraTupleSlot(estate, scanDesc,
3594  &TTSOpsVirtual);
3595 
3596  /* this is an array of pointers, not structures */
3597  aggstate->hash_pergroup = pergroups;
3598 
3599  aggstate->hashentrysize = hash_agg_entry_size(aggstate->numtrans,
3600  outerplan->plan_width,
3601  node->transitionSpace);
3602 
3603  /*
3604  * Consider all of the grouping sets together when setting the limits
3605  * and estimating the number of partitions. This can be inaccurate
3606  * when there is more than one grouping set, but should still be
3607  * reasonable.
3608  */
3609  for (int k = 0; k < aggstate->num_hashes; k++)
3610  totalGroups += aggstate->perhash[k].aggnode->numGroups;
3611 
3612  hash_agg_set_limits(aggstate->hashentrysize, totalGroups, 0,
3613  &aggstate->hash_mem_limit,
3614  &aggstate->hash_ngroups_limit,
3615  &aggstate->hash_planned_partitions);
3616  find_hash_columns(aggstate);
3617 
3618  /* Skip massive memory allocation if we are just doing EXPLAIN */
3619  if (!(eflags & EXEC_FLAG_EXPLAIN_ONLY))
3620  build_hash_tables(aggstate);
3621 
3622  aggstate->table_filled = false;
3623 
3624  /* Initialize this to 1, meaning nothing spilled, yet */
3625  aggstate->hash_batches_used = 1;
3626  }
3627 
3628  /*
3629  * Initialize current phase-dependent values to initial phase. The initial
3630  * phase is 1 (first sort pass) for all strategies that use sorting (if
3631  * hashing is being done too, then phase 0 is processed last); but if only
3632  * hashing is being done, then phase 0 is all there is.
3633  */
3634  if (node->aggstrategy == AGG_HASHED)
3635  {
3636  aggstate->current_phase = 0;
3637  initialize_phase(aggstate, 0);
3638  select_current_set(aggstate, 0, true);
3639  }
3640  else
3641  {
3642  aggstate->current_phase = 1;
3643  initialize_phase(aggstate, 1);
3644  select_current_set(aggstate, 0, false);
3645  }
3646 
3647  /*
3648  * Perform lookups of aggregate function info, and initialize the
3649  * unchanging fields of the per-agg and per-trans data.
3650  */
3651  foreach(l, aggstate->aggs)
3652  {
3653  Aggref *aggref = lfirst(l);
3654  AggStatePerAgg peragg;
3655  AggStatePerTrans pertrans;
3656  Oid aggTransFnInputTypes[FUNC_MAX_ARGS];
3657  int numAggTransFnArgs;
3658  int numDirectArgs;
3659  HeapTuple aggTuple;
3660  Form_pg_aggregate aggform;
3661  AclResult aclresult;
3662  Oid finalfn_oid;
3663  Oid serialfn_oid,
3664  deserialfn_oid;
3665  Oid aggOwner;
3666  Expr *finalfnexpr;
3667  Oid aggtranstype;
3668 
3669  /* Planner should have assigned aggregate to correct level */
3670  Assert(aggref->agglevelsup == 0);
3671  /* ... and the split mode should match */
3672  Assert(aggref->aggsplit == aggstate->aggsplit);
3673 
3674  peragg = &peraggs[aggref->aggno];
3675 
3676  /* Check if we initialized the state for this aggregate already. */
3677  if (peragg->aggref != NULL)
3678  continue;
3679 
3680  peragg->aggref = aggref;
3681  peragg->transno = aggref->aggtransno;
3682 
3683  /* Fetch the pg_aggregate row */
3684  aggTuple = SearchSysCache1(AGGFNOID,
3685  ObjectIdGetDatum(aggref->aggfnoid));
3686  if (!HeapTupleIsValid(aggTuple))
3687  elog(ERROR, "cache lookup failed for aggregate %u",
3688  aggref->aggfnoid);
3689  aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
3690 
3691  /* Check permission to call aggregate function */
3692  aclresult = object_aclcheck(ProcedureRelationId, aggref->aggfnoid, GetUserId(),
3693  ACL_EXECUTE);
3694  if (aclresult != ACLCHECK_OK)
3695  aclcheck_error(aclresult, OBJECT_AGGREGATE,
3696  get_func_name(aggref->aggfnoid));
3698 
3699  /* planner recorded transition state type in the Aggref itself */
3700  aggtranstype = aggref->aggtranstype;
3701  Assert(OidIsValid(aggtranstype));
3702 
3703  /* Final function only required if we're finalizing the aggregates */
3704  if (DO_AGGSPLIT_SKIPFINAL(aggstate->aggsplit))
3705  peragg->finalfn_oid = finalfn_oid = InvalidOid;
3706  else
3707  peragg->finalfn_oid = finalfn_oid = aggform->aggfinalfn;
3708 
3709  serialfn_oid = InvalidOid;
3710  deserialfn_oid = InvalidOid;
3711 
3712  /*
3713  * Check if serialization/deserialization is required. We only do it
3714  * for aggregates that have transtype INTERNAL.
3715  */
3716  if (aggtranstype == INTERNALOID)
3717  {
3718  /*
3719  * The planner should only have generated a serialize agg node if
3720  * every aggregate with an INTERNAL state has a serialization
3721  * function. Verify that.
3722  */
3723  if (DO_AGGSPLIT_SERIALIZE(aggstate->aggsplit))
3724  {
3725  /* serialization only valid when not running finalfn */
3727 
3728  if (!OidIsValid(aggform->aggserialfn))
3729  elog(ERROR, "serialfunc not provided for serialization aggregation");
3730  serialfn_oid = aggform->aggserialfn;
3731  }
3732 
3733  /* Likewise for deserialization functions */
3734  if (DO_AGGSPLIT_DESERIALIZE(aggstate->aggsplit))
3735  {
3736  /* deserialization only valid when combining states */
3737  Assert(DO_AGGSPLIT_COMBINE(aggstate->aggsplit));
3738 
3739  if (!OidIsValid(aggform->aggdeserialfn))
3740  elog(ERROR, "deserialfunc not provided for deserialization aggregation");
3741  deserialfn_oid = aggform->aggdeserialfn;
3742  }
3743  }
3744 
3745  /* Check that aggregate owner has permission to call component fns */
3746  {
3747  HeapTuple procTuple;
3748 
3749  procTuple = SearchSysCache1(PROCOID,
3750  ObjectIdGetDatum(aggref->aggfnoid));
3751  if (!HeapTupleIsValid(procTuple))
3752  elog(ERROR, "cache lookup failed for function %u",
3753  aggref->aggfnoid);
3754  aggOwner = ((Form_pg_proc) GETSTRUCT(procTuple))->proowner;
3755  ReleaseSysCache(procTuple);
3756 
3757  if (OidIsValid(finalfn_oid))
3758  {
3759  aclresult = object_aclcheck(ProcedureRelationId, finalfn_oid, aggOwner,
3760  ACL_EXECUTE);
3761  if (aclresult != ACLCHECK_OK)
3762  aclcheck_error(aclresult, OBJECT_FUNCTION,
3763  get_func_name(finalfn_oid));
3764  InvokeFunctionExecuteHook(finalfn_oid);
3765  }
3766  if (OidIsValid(serialfn_oid))
3767  {
3768  aclresult = object_aclcheck(ProcedureRelationId, serialfn_oid, aggOwner,
3769  ACL_EXECUTE);
3770  if (aclresult != ACLCHECK_OK)
3771  aclcheck_error(aclresult, OBJECT_FUNCTION,
3772  get_func_name(serialfn_oid));
3773  InvokeFunctionExecuteHook(serialfn_oid);
3774  }
3775  if (OidIsValid(deserialfn_oid))
3776  {
3777  aclresult = object_aclcheck(ProcedureRelationId, deserialfn_oid, aggOwner,
3778  ACL_EXECUTE);
3779  if (aclresult != ACLCHECK_OK)
3780  aclcheck_error(aclresult, OBJECT_FUNCTION,
3781  get_func_name(deserialfn_oid));
3782  InvokeFunctionExecuteHook(deserialfn_oid);
3783  }
3784  }
3785 
3786  /*
3787  * Get actual datatypes of the (nominal) aggregate inputs. These
3788  * could be different from the agg's declared input types, when the
3789  * agg accepts ANY or a polymorphic type.
3790  */
3791  numAggTransFnArgs = get_aggregate_argtypes(aggref,
3792  aggTransFnInputTypes);
3793 
3794  /* Count the "direct" arguments, if any */
3795  numDirectArgs = list_length(aggref->aggdirectargs);
3796 
3797  /* Detect how many arguments to pass to the finalfn */
3798  if (aggform->aggfinalextra)
3799  peragg->numFinalArgs = numAggTransFnArgs + 1;
3800  else
3801  peragg->numFinalArgs = numDirectArgs + 1;
3802 
3803  /* Initialize any direct-argument expressions */
3804  peragg->aggdirectargs = ExecInitExprList(aggref->aggdirectargs,
3805  (PlanState *) aggstate);
3806 
3807  /*
3808  * build expression trees using actual argument & result types for the
3809  * finalfn, if it exists and is required.
3810  */
3811  if (OidIsValid(finalfn_oid))
3812  {
3813  build_aggregate_finalfn_expr(aggTransFnInputTypes,
3814  peragg->numFinalArgs,
3815  aggtranstype,
3816  aggref->aggtype,
3817  aggref->inputcollid,
3818  finalfn_oid,
3819  &finalfnexpr);
3820  fmgr_info(finalfn_oid, &peragg->finalfn);
3821  fmgr_info_set_expr((Node *) finalfnexpr, &peragg->finalfn);
3822  }
3823 
3824  /* get info about the output value's datatype */
3825  get_typlenbyval(aggref->aggtype,
3826  &peragg->resulttypeLen,
3827  &peragg->resulttypeByVal);
3828 
3829  /*
3830  * Build working state for invoking the transition function, if we
3831  * haven't done it already.
3832  */
3833  pertrans = &pertransstates[aggref->aggtransno];
3834  if (pertrans->aggref == NULL)
3835  {
3836  Datum textInitVal;
3837  Datum initValue;
3838  bool initValueIsNull;
3839  Oid transfn_oid;
3840 
3841  /*
3842  * If this aggregation is performing state combines, then instead
3843  * of using the transition function, we'll use the combine
3844  * function.
3845  */
3846  if (DO_AGGSPLIT_COMBINE(aggstate->aggsplit))
3847  {
3848  transfn_oid = aggform->aggcombinefn;
3849 
3850  /* If not set then the planner messed up */
3851  if (!OidIsValid(transfn_oid))
3852  elog(ERROR, "combinefn not set for aggregate function");
3853  }
3854  else
3855  transfn_oid = aggform->aggtransfn;
3856 
3857  aclresult = object_aclcheck(ProcedureRelationId, transfn_oid, aggOwner, ACL_EXECUTE);
3858  if (aclresult != ACLCHECK_OK)
3859  aclcheck_error(aclresult, OBJECT_FUNCTION,
3860  get_func_name(transfn_oid));
3861  InvokeFunctionExecuteHook(transfn_oid);
3862 
3863  /*
3864  * initval is potentially null, so don't try to access it as a
3865  * struct field. Must do it the hard way with SysCacheGetAttr.
3866  */
3867  textInitVal = SysCacheGetAttr(AGGFNOID, aggTuple,
3868  Anum_pg_aggregate_agginitval,
3869  &initValueIsNull);
3870  if (initValueIsNull)
3871  initValue = (Datum) 0;
3872  else
3873  initValue = GetAggInitVal(textInitVal, aggtranstype);
3874 
3875  if (DO_AGGSPLIT_COMBINE(aggstate->aggsplit))
3876  {
3877  Oid combineFnInputTypes[] = {aggtranstype,
3878  aggtranstype};
3879 
3880  /*
3881  * When combining there's only one input, the to-be-combined
3882  * transition value. The transition value is not counted
3883  * here.
3884  */
3885  pertrans->numTransInputs = 1;
3886 
3887  /* aggcombinefn always has two arguments of aggtranstype */
3888  build_pertrans_for_aggref(pertrans, aggstate, estate,
3889  aggref, transfn_oid, aggtranstype,
3890  serialfn_oid, deserialfn_oid,
3891  initValue, initValueIsNull,
3892  combineFnInputTypes, 2);
3893 
3894  /*
3895  * Ensure that a combine function to combine INTERNAL states
3896  * is not strict. This should have been checked during CREATE
3897  * AGGREGATE, but the strict property could have been changed
3898  * since then.
3899  */
3900  if (pertrans->transfn.fn_strict && aggtranstype == INTERNALOID)
3901  ereport(ERROR,
3902  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
3903  errmsg("combine function with transition type %s must not be declared STRICT",
3904  format_type_be(aggtranstype))));
3905  }
3906  else
3907  {
3908  /* Detect how many arguments to pass to the transfn */
3909  if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
3910  pertrans->numTransInputs = list_length(aggref->args);
3911  else
3912  pertrans->numTransInputs = numAggTransFnArgs;
3913 
3914  build_pertrans_for_aggref(pertrans, aggstate, estate,
3915  aggref, transfn_oid, aggtranstype,
3916  serialfn_oid, deserialfn_oid,
3917  initValue, initValueIsNull,
3918  aggTransFnInputTypes,
3919  numAggTransFnArgs);
3920 
3921  /*
3922  * If the transfn is strict and the initval is NULL, make sure
3923  * input type and transtype are the same (or at least
3924  * binary-compatible), so that it's OK to use the first
3925  * aggregated input value as the initial transValue. This
3926  * should have been checked at agg definition time, but we
3927  * must check again in case the transfn's strictness property
3928  * has been changed.
3929  */
3930  if (pertrans->transfn.fn_strict && pertrans->initValueIsNull)
3931  {
3932  if (numAggTransFnArgs <= numDirectArgs ||
3933  !IsBinaryCoercible(aggTransFnInputTypes[numDirectArgs],
3934  aggtranstype))
3935  ereport(ERROR,
3936  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
3937  errmsg("aggregate %u needs to have compatible input type and transition type",
3938  aggref->aggfnoid)));
3939  }
3940  }
3941  }
3942  else
3943  pertrans->aggshared = true;
3944  ReleaseSysCache(aggTuple);
3945  }
3946 
3947  /*
3948  * Update aggstate->numaggs to be the number of unique aggregates found.
3949  * Also set numstates to the number of unique transition states found.
3950  */
3951  aggstate->numaggs = numaggs;
3952  aggstate->numtrans = numtrans;
3953 
3954  /*
3955  * Last, check whether any more aggregates got added onto the node while
3956  * we processed the expressions for the aggregate arguments (including not
3957  * only the regular arguments and FILTER expressions handled immediately
3958  * above, but any direct arguments we might've handled earlier). If so,
3959  * we have nested aggregate functions, which is semantically nonsensical,
3960  * so complain. (This should have been caught by the parser, so we don't
3961  * need to work hard on a helpful error message; but we defend against it
3962  * here anyway, just to be sure.)
3963  */
3964  if (numaggrefs != list_length(aggstate->aggs))
3965  ereport(ERROR,
3966  (errcode(ERRCODE_GROUPING_ERROR),
3967  errmsg("aggregate function calls cannot be nested")));
3968 
3969  /*
3970  * Build expressions doing all the transition work at once. We build a
3971  * different one for each phase, as the number of transition function
3972  * invocation can differ between phases. Note this'll work both for
3973  * transition and combination functions (although there'll only be one
3974  * phase in the latter case).
3975  */
3976  for (phaseidx = 0; phaseidx < aggstate->numphases; phaseidx++)
3977  {
3978  AggStatePerPhase phase = &aggstate->phases[phaseidx];
3979  bool dohash = false;
3980  bool dosort = false;
3981 
3982  /* phase 0 doesn't necessarily exist */
3983  if (!phase->aggnode)
3984  continue;
3985 
3986  if (aggstate->aggstrategy == AGG_MIXED && phaseidx == 1)
3987  {
3988  /*
3989  * Phase one, and only phase one, in a mixed agg performs both
3990  * sorting and aggregation.
3991  */
3992  dohash = true;
3993  dosort = true;
3994  }
3995  else if (aggstate->aggstrategy == AGG_MIXED && phaseidx == 0)
3996  {
3997  /*
3998  * No need to compute a transition function for an AGG_MIXED phase
3999  * 0 - the contents of the hashtables will have been computed
4000  * during phase 1.
4001  */
4002  continue;
4003  }
4004  else if (phase->aggstrategy == AGG_PLAIN ||
4005  phase->aggstrategy == AGG_SORTED)
4006  {
4007  dohash = false;
4008  dosort = true;
4009  }
4010  else if (phase->aggstrategy == AGG_HASHED)
4011  {
4012  dohash = true;
4013  dosort = false;
4014  }
4015  else
4016  Assert(false);
4017 
4018  phase->evaltrans = ExecBuildAggTrans(aggstate, phase, dosort, dohash,
4019  false);
4020 
4021  /* cache compiled expression for outer slot without NULL check */
4022  phase->evaltrans_cache[0][0] = phase->evaltrans;
4023  }
4024 
4025  return aggstate;
4026 }
AclResult
Definition: acl.h:181
@ ACLCHECK_OK
Definition: acl.h:182
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2669
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3760
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1106
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:753
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:835
#define OidIsValid(objectId)
Definition: c.h:764
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
List * ExecInitExprList(List *nodes, PlanState *parent)
Definition: execExpr.c:323
ExprState * ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase, bool doSort, bool doHash, bool nullcheck)
Definition: execExpr.c:3451
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:214
ExprState * execTuplesMatchPrepare(TupleDesc desc, int numCols, const AttrNumber *keyColIdx, const Oid *eqOperators, const Oid *collations, PlanState *parent)
Definition: execGrouping.c:59
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1832
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1800
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
ExprContext * CreateWorkExprContext(EState *estate)
Definition: execUtils.c:324
const TupleTableSlotOps * ExecGetResultSlotOps(PlanState *planstate, bool *isfixed)
Definition: execUtils.c:507
void ExecCreateScanSlotFromOuterPlan(EState *estate, ScanState *scanstate, const TupleTableSlotOps *tts_ops)
Definition: execUtils.c:690
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:488
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:543
struct AggStatePerGroupData * AggStatePerGroup
Definition: execnodes.h:2373
struct AggStatePerTransData * AggStatePerTrans
Definition: execnodes.h:2372
struct AggStatePerAggData * AggStatePerAgg
Definition: execnodes.h:2371
#define EXEC_FLAG_BACKWARD
Definition: executor.h:68
#define EXEC_FLAG_REWIND
Definition: executor.h:67
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:65
#define EXEC_FLAG_MARK
Definition: executor.h:69
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
#define fmgr_info_set_expr(expr, finfo)
Definition: fmgr.h:135
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
static int initValue(long lng_val)
Definition: informix.c:677
int j
Definition: isn.c:74
int i
Definition: isn.c:73
List * lcons_int(int datum, List *list)
Definition: list.c:512
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2233
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1612
void * palloc0(Size size)
Definition: mcxt.c:1257
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
Oid GetUserId(void)
Definition: miscinit.c:509
static void find_hash_columns(AggState *aggstate)
Definition: nodeAgg.c:1564
static Datum GetAggInitVal(Datum textInitVal, Oid transtype)
Definition: nodeAgg.c:4289
static TupleTableSlot * ExecAgg(PlanState *pstate)
Definition: nodeAgg.c:2159
static void initialize_phase(AggState *aggstate, int newphase)
Definition: nodeAgg.c:478
Size hash_agg_entry_size(int numTrans, Size tupleWidth, Size transitionSpace)
Definition: nodeAgg.c:1695
static void build_pertrans_for_aggref(AggStatePerTrans pertrans, AggState *aggstate, EState *estate, Aggref *aggref, Oid transfn_oid, Oid aggtranstype, Oid aggserialfn, Oid aggdeserialfn, Datum initValue, bool initValueIsNull, Oid *inputTypes, int numArguments)
Definition: nodeAgg.c:4039
static void build_hash_tables(AggState *aggstate)
Definition: nodeAgg.c:1469
static void select_current_set(AggState *aggstate, int setno, bool is_hash)
Definition: nodeAgg.c:456
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:1799
#define DO_AGGSPLIT_SKIPFINAL(as)
Definition: nodes.h:396
#define DO_AGGSPLIT_DESERIALIZE(as)
Definition: nodes.h:398
#define DO_AGGSPLIT_COMBINE(as)
Definition: nodes.h:395
@ AGG_SORTED
Definition: nodes.h:365
@ AGG_HASHED
Definition: nodes.h:366
@ AGG_MIXED
Definition: nodes.h:367
@ AGG_PLAIN
Definition: nodes.h:364
#define DO_AGGSPLIT_SERIALIZE(as)
Definition: nodes.h:397
#define makeNode(_type_)
Definition: nodes.h:176
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
#define InvokeFunctionExecuteHook(objectId)
Definition: objectaccess.h:213
void build_aggregate_finalfn_expr(Oid *agg_input_types, int num_finalfn_inputs, Oid agg_state_type, Oid agg_result_type, Oid agg_input_collation, Oid finalfn_oid, Expr **finalfnexpr)
Definition: parse_agg.c:2122
int get_aggregate_argtypes(Aggref *aggref, Oid *inputTypes)
Definition: parse_agg.c:1894
bool IsBinaryCoercible(Oid srctype, Oid targettype)
@ OBJECT_AGGREGATE
Definition: parsenodes.h:2121
@ OBJECT_FUNCTION
Definition: parsenodes.h:2139
#define ACL_EXECUTE
Definition: parsenodes.h:90
FormData_pg_aggregate * Form_pg_aggregate
Definition: pg_aggregate.h:109
#define FUNC_MAX_ARGS
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define list_nth_node(type, list, n)
Definition: pg_list.h:327
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
#define InvalidOid
Definition: postgres_ext.h:36
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
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
FmgrInfo transfn
Definition: nodeAgg.h:86
Aggref * aggref
Definition: nodeAgg.h:44
AggStatePerGroup * all_pergroups
Definition: execnodes.h:2448
AggStatePerGroup * hash_pergroup
Definition: execnodes.h:2443
List * aggs
Definition: execnodes.h:2380
ExprContext * tmpcontext
Definition: execnodes.h:2392
int hash_planned_partitions
Definition: execnodes.h:2433
HeapTuple grp_firstTuple
Definition: execnodes.h:2417
Size hash_mem_limit
Definition: execnodes.h:2431
AggStatePerTrans curpertrans
Definition: execnodes.h:2397
bool table_filled
Definition: execnodes.h:2419
int current_set
Definition: execnodes.h:2402
AggStrategy aggstrategy
Definition: execnodes.h:2383
AggSplit aggsplit
Definition: execnodes.h:2384
int projected_set
Definition: execnodes.h:2400
uint64 hash_ngroups_limit
Definition: execnodes.h:2432
bool input_done
Definition: execnodes.h:2398
AggStatePerPhase phases
Definition: execnodes.h:2410
List * all_grouped_cols
Definition: execnodes.h:2404
AggStatePerGroup * pergroups
Definition: execnodes.h:2415
AggStatePerHash perhash
Definition: execnodes.h:2442
double hashentrysize
Definition: execnodes.h:2435
int numphases
Definition: execnodes.h:2386
TupleTableSlot * hash_spill_wslot
Definition: execnodes.h:2426
AggStatePerAgg curperagg
Definition: execnodes.h:2395
TupleTableSlot * sort_slot
Definition: execnodes.h:2413
int numaggs
Definition: execnodes.h:2381
int num_hashes
Definition: execnodes.h:2420
AggStatePerAgg peragg
Definition: execnodes.h:2388
TupleTableSlot * hash_spill_rslot
Definition: execnodes.h:2425
int current_phase
Definition: execnodes.h:2387
bool agg_done
Definition: execnodes.h:2399
Definition: plannodes.h:995
AggSplit aggsplit
Definition: plannodes.h:1002
List * chain
Definition: plannodes.h:1029
long numGroups
Definition: plannodes.h:1015
List * groupingSets
Definition: plannodes.h:1026
Plan plan
Definition: plannodes.h:996
int numCols
Definition: plannodes.h:1005
uint64 transitionSpace
Definition: plannodes.h:1018
AggStrategy aggstrategy
Definition: plannodes.h:999
Oid aggfnoid
Definition: primnodes.h:422
List * aggdirectargs
Definition: primnodes.h:443
List * args
Definition: primnodes.h:446
MemoryContext es_query_cxt
Definition: execnodes.h:659
Datum * ecxt_aggvalues
Definition: execnodes.h:268
bool * ecxt_aggnulls
Definition: execnodes.h:270
bool fn_strict
Definition: fmgr.h:61
Definition: nodes.h:129
bool outeropsset
Definition: execnodes.h:1120
const TupleTableSlotOps * outerops
Definition: execnodes.h:1112
ExprState * qual
Definition: execnodes.h:1058
bool outeropsfixed
Definition: execnodes.h:1116
EState * state
Definition: execnodes.h:1039
ExprContext * ps_ExprContext
Definition: execnodes.h:1076
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1043
List * qual
Definition: plannodes.h:154
int plan_width
Definition: plannodes.h:136
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:123
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:868
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:820
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1081
@ AGGFNOID
Definition: syscache.h:34
@ PROCOID
Definition: syscache.h:79

References ACL_EXECUTE, aclcheck_error(), ACLCHECK_OK, AggState::agg_done, AGG_HASHED, AGG_MIXED, AGG_PLAIN, AGG_SORTED, AggState::aggcontexts, AggStatePerAggData::aggdirectargs, Aggref::aggdirectargs, Aggref::aggfnoid, AGGFNOID, AggStatePerPhaseData::aggnode, AggStatePerHashData::aggnode, AggStatePerTransData::aggref, AggStatePerAggData::aggref, AggState::aggs, AggStatePerTransData::aggshared, AggState::aggsplit, Agg::aggsplit, AggStatePerPhaseData::aggstrategy, AggState::aggstrategy, Agg::aggstrategy, AggState::all_grouped_cols, AggState::all_pergroups, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Aggref::args, Assert(), bms_add_member(), bms_add_members(), bms_next_member(), build_aggregate_finalfn_expr(), build_hash_tables(), build_pertrans_for_aggref(), castNode, Agg::chain, CreateWorkExprContext(), AggState::curperagg, AggState::curpertrans, AggState::current_phase, AggState::current_set, DO_AGGSPLIT_COMBINE, DO_AGGSPLIT_DESERIALIZE, DO_AGGSPLIT_SERIALIZE, DO_AGGSPLIT_SKIPFINAL, ExprContext::ecxt_aggnulls, ExprContext::ecxt_aggvalues, elog(), AggStatePerPhaseData::eqfunctions, ereport, errcode(), errmsg(), ERROR, EState::es_query_cxt, AggStatePerPhaseData::evaltrans, AggStatePerPhaseData::evaltrans_cache, EXEC_FLAG_BACKWARD, EXEC_FLAG_EXPLAIN_ONLY, EXEC_FLAG_MARK, EXEC_FLAG_REWIND, ExecAgg(), ExecAssignExprContext(), ExecAssignProjectionInfo(), ExecBuildAggTrans(), ExecCreateScanSlotFromOuterPlan(), ExecGetResultSlotOps(), ExecInitExprList(), ExecInitExtraTupleSlot(), ExecInitNode(), ExecInitQual(), ExecInitResultTupleSlotTL(), PlanState::ExecProcNode, execTuplesMatchPrepare(), AggStatePerAggData::finalfn, AggStatePerAggData::finalfn_oid, find_hash_columns(), fmgr_info(), fmgr_info_set_expr, FmgrInfo::fn_strict, format_type_be(), FUNC_MAX_ARGS, get_aggregate_argtypes(), get_func_name(), get_typlenbyval(), GetAggInitVal(), GETSTRUCT, GetUserId(), AggStatePerPhaseData::grouped_cols, Agg::groupingSets, AggState::grp_firstTuple, AggStatePerPhaseData::gset_lengths, hash_agg_entry_size(), hash_agg_set_limits(), AggState::hash_batches_used, AggState::hash_mem_limit, AggState::hash_metacxt, AggState::hash_ngroups_limit, AggState::hash_pergroup, AggState::hash_planned_partitions, AggState::hash_spill_rslot, AggState::hash_spill_wslot, AggState::hashcontext, AggState::hashentrysize, HeapTupleIsValid, i, initialize_phase(), initValue(), AggStatePerTransData::initValueIsNull, AggState::input_done, InvalidOid, InvokeFunctionExecuteHook, IsBinaryCoercible(), j, lcons_int(), lfirst, list_length(), list_nth_node, makeNode, Max, AggState::maxsets, NIL, AggState::num_hashes, AggState::numaggs, AggStatePerHashData::numCols, Agg::numCols, AggStatePerAggData::numFinalArgs, Agg::numGroups, AggState::numphases, AggStatePerPhaseData::numsets, AggState::numtrans, AggStatePerTransData::numTransInputs, object_aclcheck(), OBJECT_AGGREGATE, OBJECT_FUNCTION, ObjectIdGetDatum(), OidIsValid, PlanState::outerops, PlanState::outeropsfixed, PlanState::outeropsset, outerPlan, outerPlanState, palloc(), palloc0(), AggState::peragg, AggState::pergroups, AggState::perhash, AggState::pertrans, AggState::phases, PlanState::plan, Agg::plan, Plan::plan_width, PROCOID, AggState::projected_set, ScanState::ps, PlanState::ps_ExprContext, PlanState::qual, Plan::qual, ReleaseSysCache(), AggStatePerAggData::resulttypeByVal, AggStatePerAggData::resulttypeLen, SearchSysCache1(), select_current_set(), AggState::sort_in, AggState::sort_out, AggState::sort_slot, AggStatePerPhaseData::sortnode, AggState::ss, ScanState::ss_ScanTupleSlot, PlanState::state, SysCacheGetAttr(), AggState::table_filled, AggState::tmpcontext, AggStatePerTransData::transfn, Agg::transitionSpace, AggStatePerAggData::transno, TupleTableSlot::tts_tupleDescriptor, TTSOpsMinimalTuple, and TTSOpsVirtual.

Referenced by ExecInitNode().

◆ ExecReScanAgg()

void ExecReScanAgg ( AggState node)

Definition at line 4375 of file nodeAgg.c.

4376 {
4377  ExprContext *econtext = node->ss.ps.ps_ExprContext;
4379  Agg *aggnode = (Agg *) node->ss.ps.plan;
4380  int transno;
4381  int numGroupingSets = Max(node->maxsets, 1);
4382  int setno;
4383 
4384  node->agg_done = false;
4385 
4386  if (node->aggstrategy == AGG_HASHED)
4387  {
4388  /*
4389  * In the hashed case, if we haven't yet built the hash table then we
4390  * can just return; nothing done yet, so nothing to undo. If subnode's
4391  * chgParam is not NULL then it will be re-scanned by ExecProcNode,
4392  * else no reason to re-scan it at all.
4393  */
4394  if (!node->table_filled)
4395  return;
4396 
4397  /*
4398  * If we do have the hash table, and it never spilled, and the subplan
4399  * does not have any parameter changes, and none of our own parameter
4400  * changes affect input expressions of the aggregated functions, then
4401  * we can just rescan the existing hash table; no need to build it
4402  * again.
4403  */
4404  if (outerPlan->chgParam == NULL && !node->hash_ever_spilled &&
4405  !bms_overlap(node->ss.ps.chgParam, aggnode->aggParams))
4406  {
4408  &node->perhash[0].hashiter);
4409  select_current_set(node, 0, true);
4410  return;
4411  }
4412  }
4413 
4414  /* Make sure we have closed any open tuplesorts */
4415  for (transno = 0; transno < node->numtrans; transno++)
4416  {
4417  for (setno = 0; setno < numGroupingSets; setno++)
4418  {
4419  AggStatePerTrans pertrans = &node->pertrans[transno];
4420 
4421  if (pertrans->sortstates[setno])
4422  {
4423  tuplesort_end(pertrans->sortstates[setno]);
4424  pertrans->sortstates[setno] = NULL;
4425  }
4426  }
4427  }
4428 
4429  /*
4430  * We don't need to ReScanExprContext the output tuple context here;
4431  * ExecReScan already did it. But we do need to reset our per-grouping-set
4432  * contexts, which may have transvalues stored in them. (We use rescan
4433  * rather than just reset because transfns may have registered callbacks
4434  * that need to be run now.) For the AGG_HASHED case, see below.
4435  */
4436 
4437  for (setno = 0; setno < numGroupingSets; setno++)
4438  {
4439  ReScanExprContext(node->aggcontexts[setno]);
4440  }
4441 
4442  /* Release first tuple of group, if we have made a copy */
4443  if (node->grp_firstTuple != NULL)
4444  {
4446  node->grp_firstTuple = NULL;
4447  }
4449 
4450  /* Forget current agg values */
4451  MemSet(econtext->ecxt_aggvalues, 0, sizeof(Datum) * node->numaggs);
4452  MemSet(econtext->ecxt_aggnulls, 0, sizeof(bool) * node->numaggs);
4453 
4454  /*
4455  * With AGG_HASHED/MIXED, the hash table is allocated in a sub-context of
4456  * the hashcontext. This used to be an issue, but now, resetting a context
4457  * automatically deletes sub-contexts too.
4458  */
4459  if (node->aggstrategy == AGG_HASHED || node->aggstrategy == AGG_MIXED)
4460  {
4462 
4463  node->hash_ever_spilled = false;
4464  node->hash_spill_mode = false;
4465  node->hash_ngroups_current = 0;
4466 
4468  /* Rebuild an empty hash table */
4469  build_hash_tables(node);
4470  node->table_filled = false;
4471  /* iterator will be reset when the table is filled */
4472 
4473  hashagg_recompile_expressions(node, false, false);
4474  }
4475 
4476  if (node->aggstrategy != AGG_HASHED)
4477  {
4478  /*
4479  * Reset the per-group state (in particular, mark transvalues null)
4480  */
4481  for (setno = 0; setno < numGroupingSets; setno++)
4482  {
4483  MemSet(node->pergroups[setno], 0,
4484  sizeof(AggStatePerGroupData) * node->numaggs);
4485  }
4486 
4487  /* reset to phase 1 */
4488  initialize_phase(node, 1);
4489 
4490  node->input_done = false;
4491  node->projected_set = -1;
4492  }
4493 
4494  if (outerPlan->chgParam == NULL)
4496 }
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:527
#define MemSet(start, val, len)
Definition: c.h:1009
void ExecReScan(PlanState *node)
Definition: execAmi.c:78
#define ResetTupleHashIterator(htable, iter)
Definition: execnodes.h:838
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1426
static void hashagg_recompile_expressions(AggState *aggstate, bool minslot, bool nullcheck)
Definition: nodeAgg.c:1742
TupleHashTable hashtable
Definition: nodeAgg.h:311
TupleHashIterator hashiter
Definition: nodeAgg.h:312
bool hash_spill_mode
Definition: execnodes.h:2429
uint64 hash_ngroups_current
Definition: execnodes.h:2437
bool hash_ever_spilled
Definition: execnodes.h:2428
Bitmapset * aggParams
Definition: plannodes.h:1021
Bitmapset * chgParam
Definition: execnodes.h:1069

References AggState::agg_done, AGG_HASHED, AGG_MIXED, AggState::aggcontexts, Agg::aggParams, AggState::aggstrategy, bms_overlap(), build_hash_tables(), PlanState::chgParam, ExprContext::ecxt_aggnulls, ExprContext::ecxt_aggvalues, ExecClearTuple(), ExecReScan(), AggState::grp_firstTuple, AggState::hash_ever_spilled, AggState::hash_ngroups_current, AggState::hash_spill_mode, hashagg_recompile_expressions(), hashagg_reset_spill_state(), AggState::hashcontext, AggStatePerHashData::hashiter, AggStatePerHashData::hashtable, heap_freetuple(), initialize_phase(), AggState::input_done, Max, AggState::maxsets, MemSet, AggState::numaggs, AggState::numtrans, outerPlan, outerPlanState, AggState::pergroups, AggState::perhash, AggState::pertrans, PlanState::plan, AggState::projected_set, ScanState::ps, PlanState::ps_ExprContext, ReScanExprContext(), ResetTupleHashIterator, select_current_set(), AggStatePerTransData::sortstates, AggState::ss, ScanState::ss_ScanTupleSlot, AggState::table_filled, and tuplesort_end().

Referenced by ExecReScan().

◆ hash_agg_entry_size()

Size hash_agg_entry_size ( int  numTrans,
Size  tupleWidth,
Size  transitionSpace 
)

Definition at line 1695 of file nodeAgg.c.

1696 {
1697  Size tupleChunkSize;
1698  Size pergroupChunkSize;
1699  Size transitionChunkSize;
1700  Size tupleSize = (MAXALIGN(SizeofMinimalTupleHeader) +
1701  tupleWidth);
1702  Size pergroupSize = numTrans * sizeof(AggStatePerGroupData);
1703 
1704  tupleChunkSize = CHUNKHDRSZ + tupleSize;
1705 
1706  if (pergroupSize > 0)
1707  pergroupChunkSize = CHUNKHDRSZ + pergroupSize;
1708  else
1709  pergroupChunkSize = 0;
1710 
1711  if (transitionSpace > 0)
1712  transitionChunkSize = CHUNKHDRSZ + transitionSpace;
1713  else
1714  transitionChunkSize = 0;
1715 
1716  return
1717  sizeof(TupleHashEntryData) +
1718  tupleChunkSize +
1719  pergroupChunkSize +
1720  transitionChunkSize;
1721 }
#define MAXALIGN(LEN)
Definition: c.h:800
struct TupleHashEntryData TupleHashEntryData
#define SizeofMinimalTupleHeader
Definition: htup_details.h:647
#define CHUNKHDRSZ
Definition: nodeAgg.c:321
struct AggStatePerGroupData AggStatePerGroupData

References CHUNKHDRSZ, MAXALIGN, and SizeofMinimalTupleHeader.

Referenced by cost_agg(), estimate_hashagg_tablesize(), and ExecInitAgg().

◆ hash_agg_set_limits()

void hash_agg_set_limits ( double  hashentrysize,
double  input_groups,
int  used_bits,
Size mem_limit,
uint64 *  ngroups_limit,
int *  num_partitions 
)

Definition at line 1799 of file nodeAgg.c.

1802 {
1803  int npartitions;
1804  Size partition_mem;
1805  Size hash_mem_limit = get_hash_memory_limit();
1806 
1807  /* if not expected to spill, use all of hash_mem */
1808  if (input_groups * hashentrysize <= hash_mem_limit)
1809  {
1810  if (num_partitions != NULL)
1811  *num_partitions = 0;
1812  *mem_limit = hash_mem_limit;
1813  *ngroups_limit = hash_mem_limit / hashentrysize;
1814  return;
1815  }
1816 
1817  /*
1818  * Calculate expected memory requirements for spilling, which is the size
1819  * of the buffers needed for all the tapes that need to be open at once.
1820  * Then, subtract that from the memory available for holding hash tables.
1821  */
1822  npartitions = hash_choose_num_partitions(input_groups,
1823  hashentrysize,
1824  used_bits,
1825  NULL);
1826  if (num_partitions != NULL)
1827  *num_partitions = npartitions;
1828 
1829  partition_mem =
1831  HASHAGG_WRITE_BUFFER_SIZE * npartitions;
1832 
1833  /*
1834  * Don't set the limit below 3/4 of hash_mem. In that case, we are at the
1835  * minimum number of partitions, so we aren't going to dramatically exceed
1836  * work mem anyway.
1837  */
1838  if (hash_mem_limit > 4 * partition_mem)
1839  *mem_limit = hash_mem_limit - partition_mem;
1840  else
1841  *mem_limit = hash_mem_limit * 0.75;
1842 
1843  if (*mem_limit > hashentrysize)
1844  *ngroups_limit = *mem_limit / hashentrysize;
1845  else
1846  *ngroups_limit = 1;
1847 }
#define HASHAGG_READ_BUFFER_SIZE
Definition: nodeAgg.c:306
#define HASHAGG_WRITE_BUFFER_SIZE
Definition: nodeAgg.c:307
static int hash_choose_num_partitions(double input_groups, double hashentrysize, int used_bits, int *log2_npartitions)
Definition: nodeAgg.c:1992
size_t get_hash_memory_limit(void)
Definition: nodeHash.c:3592

References get_hash_memory_limit(), hash_choose_num_partitions(), HASHAGG_READ_BUFFER_SIZE, and HASHAGG_WRITE_BUFFER_SIZE.

Referenced by agg_refill_hash_table(), cost_agg(), and ExecInitAgg().