PostgreSQL Source Code  git master
nodeAgg.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/parallel.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "common/hashfn.h"
#include "executor/execExpr.h"
#include "executor/executor.h"
#include "executor/nodeAgg.h"
#include "lib/hyperloglog.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
#include "parser/parse_agg.h"
#include "parser/parse_coerce.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/dynahash.h"
#include "utils/expandeddatum.h"
#include "utils/logtape.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/syscache.h"
#include "utils/tuplesort.h"
Include dependency graph for nodeAgg.c:

Go to the source code of this file.

Data Structures

struct  HashAggSpill
 
struct  HashAggBatch
 
struct  FindColsContext
 

Macros

#define HASHAGG_PARTITION_FACTOR   1.50
 
#define HASHAGG_MIN_PARTITIONS   4
 
#define HASHAGG_MAX_PARTITIONS   1024
 
#define HASHAGG_READ_BUFFER_SIZE   BLCKSZ
 
#define HASHAGG_WRITE_BUFFER_SIZE   BLCKSZ
 
#define HASHAGG_HLL_BIT_WIDTH   5
 
#define CHUNKHDRSZ   16
 

Typedefs

typedef struct HashAggSpill HashAggSpill
 
typedef struct HashAggBatch HashAggBatch
 
typedef struct FindColsContext FindColsContext
 

Functions

static void select_current_set (AggState *aggstate, int setno, bool is_hash)
 
static void initialize_phase (AggState *aggstate, int newphase)
 
static TupleTableSlotfetch_input_tuple (AggState *aggstate)
 
static void initialize_aggregates (AggState *aggstate, AggStatePerGroup *pergroups, int numReset)
 
static void advance_transition_function (AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroupstate)
 
static void advance_aggregates (AggState *aggstate)
 
static void process_ordered_aggregate_single (AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroupstate)
 
static void process_ordered_aggregate_multi (AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroupstate)
 
static void finalize_aggregate (AggState *aggstate, AggStatePerAgg peragg, AggStatePerGroup pergroupstate, Datum *resultVal, bool *resultIsNull)
 
static void finalize_partialaggregate (AggState *aggstate, AggStatePerAgg peragg, AggStatePerGroup pergroupstate, Datum *resultVal, bool *resultIsNull)
 
static void prepare_hash_slot (AggStatePerHash perhash, TupleTableSlot *inputslot, TupleTableSlot *hashslot)
 
static void prepare_projection_slot (AggState *aggstate, TupleTableSlot *slot, int currentSet)
 
static void finalize_aggregates (AggState *aggstate, AggStatePerAgg peragg, AggStatePerGroup pergroup)
 
static TupleTableSlotproject_aggregates (AggState *aggstate)
 
static void find_cols (AggState *aggstate, Bitmapset **aggregated, Bitmapset **unaggregated)
 
static bool find_cols_walker (Node *node, FindColsContext *context)
 
static void build_hash_tables (AggState *aggstate)
 
static void build_hash_table (AggState *aggstate, int setno, long nbuckets)
 
static void hashagg_recompile_expressions (AggState *aggstate, bool minslot, bool nullcheck)
 
static long hash_choose_num_buckets (double hashentrysize, long estimated_nbuckets, Size memory)
 
static int hash_choose_num_partitions (double input_groups, double hashentrysize, int used_bits, int *log2_npartittions)
 
static void initialize_hash_entry (AggState *aggstate, TupleHashTable hashtable, TupleHashEntry entry)
 
static void lookup_hash_entries (AggState *aggstate)
 
static TupleTableSlotagg_retrieve_direct (AggState *aggstate)
 
static void agg_fill_hash_table (AggState *aggstate)
 
static bool agg_refill_hash_table (AggState *aggstate)
 
static TupleTableSlotagg_retrieve_hash_table (AggState *aggstate)
 
static TupleTableSlotagg_retrieve_hash_table_in_memory (AggState *aggstate)
 
static void hash_agg_check_limits (AggState *aggstate)
 
static void hash_agg_enter_spill_mode (AggState *aggstate)
 
static void hash_agg_update_metrics (AggState *aggstate, bool from_tape, int npartitions)
 
static void hashagg_finish_initial_spills (AggState *aggstate)
 
static void hashagg_reset_spill_state (AggState *aggstate)
 
static HashAggBatchhashagg_batch_new (LogicalTape *input_tape, int setno, int64 input_tuples, double input_card, int used_bits)
 
static MinimalTuple hashagg_batch_read (HashAggBatch *batch, uint32 *hashp)
 
static void hashagg_spill_init (HashAggSpill *spill, LogicalTapeSet *lts, int used_bits, double input_groups, double hashentrysize)
 
static Size hashagg_spill_tuple (AggState *aggstate, HashAggSpill *spill, TupleTableSlot *slot, uint32 hash)
 
static void hashagg_spill_finish (AggState *aggstate, HashAggSpill *spill, int setno)
 
static Datum GetAggInitVal (Datum textInitVal, Oid transtype)
 
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)
 
static void initialize_aggregate (AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroupstate)
 
static void find_hash_columns (AggState *aggstate)
 
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)
 
static TupleTableSlotExecAgg (PlanState *pstate)
 
AggStateExecInitAgg (Agg *node, EState *estate, int eflags)
 
void ExecEndAgg (AggState *node)
 
void ExecReScanAgg (AggState *node)
 
int AggCheckCallContext (FunctionCallInfo fcinfo, MemoryContext *aggcontext)
 
AggrefAggGetAggref (FunctionCallInfo fcinfo)
 
MemoryContext AggGetTempMemoryContext (FunctionCallInfo fcinfo)
 
bool AggStateIsShared (FunctionCallInfo fcinfo)
 
void AggRegisterCallback (FunctionCallInfo fcinfo, ExprContextCallbackFunction func, Datum arg)
 
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

◆ CHUNKHDRSZ

#define CHUNKHDRSZ   16

Definition at line 321 of file nodeAgg.c.

Referenced by hash_agg_entry_size().

◆ HASHAGG_HLL_BIT_WIDTH

#define HASHAGG_HLL_BIT_WIDTH   5

Definition at line 315 of file nodeAgg.c.

Referenced by hashagg_spill_init().

◆ HASHAGG_MAX_PARTITIONS

#define HASHAGG_MAX_PARTITIONS   1024

Definition at line 298 of file nodeAgg.c.

Referenced by hash_choose_num_partitions().

◆ HASHAGG_MIN_PARTITIONS

#define HASHAGG_MIN_PARTITIONS   4

Definition at line 297 of file nodeAgg.c.

Referenced by hash_choose_num_partitions().

◆ HASHAGG_PARTITION_FACTOR

#define HASHAGG_PARTITION_FACTOR   1.50

Definition at line 296 of file nodeAgg.c.

Referenced by hash_choose_num_partitions().

◆ HASHAGG_READ_BUFFER_SIZE

#define HASHAGG_READ_BUFFER_SIZE   BLCKSZ

◆ HASHAGG_WRITE_BUFFER_SIZE

#define HASHAGG_WRITE_BUFFER_SIZE   BLCKSZ

Typedef Documentation

◆ FindColsContext

◆ HashAggBatch

typedef struct HashAggBatch HashAggBatch

◆ HashAggSpill

typedef struct HashAggSpill HashAggSpill

Function Documentation

◆ advance_aggregates()

static void advance_aggregates ( AggState aggstate)
static

Definition at line 820 of file nodeAgg.c.

References AggStatePerPhaseData::evaltrans, ExecEvalExprSwitchContext(), AggState::phase, and AggState::tmpcontext.

Referenced by agg_fill_hash_table(), agg_refill_hash_table(), and agg_retrieve_direct().

821 {
822  bool dummynull;
823 
825  aggstate->tmpcontext,
826  &dummynull);
827 }
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:331
ExprState * evaltrans
Definition: nodeAgg.h:283
ExprContext * tmpcontext
Definition: execnodes.h:2290
AggStatePerPhase phase
Definition: execnodes.h:2283

◆ advance_transition_function()

static void advance_transition_function ( AggState aggstate,
AggStatePerTrans  pertrans,
AggStatePerGroup  pergroupstate 
)
static

Definition at line 708 of file nodeAgg.c.

References FunctionCallInfoBaseData::args, AggState::curaggcontext, AggState::curpertrans, datumCopy(), DatumGetPointer, ExprContext::ecxt_per_tuple_memory, ExecAggTransReparent(), FmgrInfo::fn_strict, FunctionCallInvoke, i, FunctionCallInfoBaseData::isnull, NullableDatum::isnull, MemoryContextSwitchTo(), AggStatePerGroupData::noTransValue, AggStatePerTransData::numTransInputs, AggState::tmpcontext, AggStatePerTransData::transfn, AggStatePerTransData::transfn_fcinfo, AggStatePerTransData::transtypeByVal, AggStatePerTransData::transtypeLen, AggStatePerGroupData::transValue, AggStatePerGroupData::transValueIsNull, and NullableDatum::value.

Referenced by process_ordered_aggregate_multi(), and process_ordered_aggregate_single().

711 {
712  FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
713  MemoryContext oldContext;
714  Datum newVal;
715 
716  if (pertrans->transfn.fn_strict)
717  {
718  /*
719  * For a strict transfn, nothing happens when there's a NULL input; we
720  * just keep the prior transValue.
721  */
722  int numTransInputs = pertrans->numTransInputs;
723  int i;
724 
725  for (i = 1; i <= numTransInputs; i++)
726  {
727  if (fcinfo->args[i].isnull)
728  return;
729  }
730  if (pergroupstate->noTransValue)
731  {
732  /*
733  * transValue has not been initialized. This is the first non-NULL
734  * input value. We use it as the initial value for transValue. (We
735  * already checked that the agg's input type is binary-compatible
736  * with its transtype, so straight copy here is OK.)
737  *
738  * We must copy the datum into aggcontext if it is pass-by-ref. We
739  * do not need to pfree the old transValue, since it's NULL.
740  */
742  pergroupstate->transValue = datumCopy(fcinfo->args[1].value,
743  pertrans->transtypeByVal,
744  pertrans->transtypeLen);
745  pergroupstate->transValueIsNull = false;
746  pergroupstate->noTransValue = false;
747  MemoryContextSwitchTo(oldContext);
748  return;
749  }
750  if (pergroupstate->transValueIsNull)
751  {
752  /*
753  * Don't call a strict function with NULL inputs. Note it is
754  * possible to get here despite the above tests, if the transfn is
755  * strict *and* returned a NULL on a prior cycle. If that happens
756  * we will propagate the NULL all the way to the end.
757  */
758  return;
759  }
760  }
761 
762  /* We run the transition functions in per-input-tuple memory context */
763  oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
764 
765  /* set up aggstate->curpertrans for AggGetAggref() */
766  aggstate->curpertrans = pertrans;
767 
768  /*
769  * OK to call the transition function
770  */
771  fcinfo->args[0].value = pergroupstate->transValue;
772  fcinfo->args[0].isnull = pergroupstate->transValueIsNull;
773  fcinfo->isnull = false; /* just in case transfn doesn't set it */
774 
775  newVal = FunctionCallInvoke(fcinfo);
776 
777  aggstate->curpertrans = NULL;
778 
779  /*
780  * If pass-by-ref datatype, must copy the new value into aggcontext and
781  * free the prior transValue. But if transfn returned a pointer to its
782  * first input, we don't need to do anything. Also, if transfn returned a
783  * pointer to a R/W expanded object that is already a child of the
784  * aggcontext, assume we can adopt that value without copying it.
785  *
786  * It's safe to compare newVal with pergroup->transValue without regard
787  * for either being NULL, because ExecAggTransReparent() takes care to set
788  * transValue to 0 when NULL. Otherwise we could end up accidentally not
789  * reparenting, when the transValue has the same numerical value as
790  * newValue, despite being NULL. This is a somewhat hot path, making it
791  * undesirable to instead solve this with another branch for the common
792  * case of the transition function returning its (modified) input
793  * argument.
794  */
795  if (!pertrans->transtypeByVal &&
796  DatumGetPointer(newVal) != DatumGetPointer(pergroupstate->transValue))
797  newVal = ExecAggTransReparent(aggstate, pertrans,
798  newVal, fcinfo->isnull,
799  pergroupstate->transValue,
800  pergroupstate->transValueIsNull);
801 
802  pergroupstate->transValue = newVal;
803  pergroupstate->transValueIsNull = fcinfo->isnull;
804 
805  MemoryContextSwitchTo(oldContext);
806 }
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:235
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
FunctionCallInfo transfn_fcinfo
Definition: nodeAgg.h:162
Datum ExecAggTransReparent(AggState *aggstate, AggStatePerTrans pertrans, Datum newValue, bool newValueIsNull, Datum oldValue, bool oldValueIsNull)
ExprContext * tmpcontext
Definition: execnodes.h:2290
FmgrInfo transfn
Definition: nodeAgg.h:81
bool fn_strict
Definition: fmgr.h:61
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
Datum value
Definition: postgres.h:422
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131
ExprContext * curaggcontext
Definition: execnodes.h:2292
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetPointer(X)
Definition: postgres.h:593
int i
AggStatePerTrans curpertrans
Definition: execnodes.h:2295

◆ agg_fill_hash_table()

static void agg_fill_hash_table ( AggState aggstate)
static

Definition at line 2522 of file nodeAgg.c.

References advance_aggregates(), ExprContext::ecxt_outertuple, fetch_input_tuple(), hashagg_finish_initial_spills(), AggStatePerHashData::hashiter, AggStatePerHashData::hashtable, lookup_hash_entries(), AggState::perhash, ResetExprContext, ResetTupleHashIterator, select_current_set(), AggState::table_filled, AggState::tmpcontext, and TupIsNull.

Referenced by ExecAgg().

2523 {
2524  TupleTableSlot *outerslot;
2525  ExprContext *tmpcontext = aggstate->tmpcontext;
2526 
2527  /*
2528  * Process each outer-plan tuple, and then fetch the next one, until we
2529  * exhaust the outer plan.
2530  */
2531  for (;;)
2532  {
2533  outerslot = fetch_input_tuple(aggstate);
2534  if (TupIsNull(outerslot))
2535  break;
2536 
2537  /* set up for lookup_hash_entries and advance_aggregates */
2538  tmpcontext->ecxt_outertuple = outerslot;
2539 
2540  /* Find or build hashtable entries */
2541  lookup_hash_entries(aggstate);
2542 
2543  /* Advance the aggregates (or combine functions) */
2544  advance_aggregates(aggstate);
2545 
2546  /*
2547  * Reset per-input-tuple context after each tuple, but note that the
2548  * hash lookups do this too
2549  */
2550  ResetExprContext(aggstate->tmpcontext);
2551  }
2552 
2553  /* finalize spills, if any */
2555 
2556  aggstate->table_filled = true;
2557  /* Initialize to walk the first hash table */
2558  select_current_set(aggstate, 0, true);
2560  &aggstate->perhash[0].hashiter);
2561 }
static TupleTableSlot * fetch_input_tuple(AggState *aggstate)
Definition: nodeAgg.c:549
static void select_current_set(AggState *aggstate, int setno, bool is_hash)
Definition: nodeAgg.c:457
static void hashagg_finish_initial_spills(AggState *aggstate)
Definition: nodeAgg.c:3040
static void advance_aggregates(AggState *aggstate)
Definition: nodeAgg.c:820
ExprContext * tmpcontext
Definition: execnodes.h:2290
bool table_filled
Definition: execnodes.h:2317
#define TupIsNull(slot)
Definition: tuptable.h:292
AggStatePerHash perhash
Definition: execnodes.h:2340
TupleHashIterator hashiter
Definition: nodeAgg.h:304
#define ResetTupleHashIterator(htable, iter)
Definition: execnodes.h:770
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:231
TupleHashTable hashtable
Definition: nodeAgg.h:303
static void lookup_hash_entries(AggState *aggstate)
Definition: nodeAgg.c:2077
#define ResetExprContext(econtext)
Definition: executor.h:527

◆ agg_refill_hash_table()

static bool agg_refill_hash_table ( AggState aggstate)
static

Definition at line 2576 of file nodeAgg.c.

References TupleHashEntryData::additional, advance_aggregates(), AGG_MIXED, AggStatePerPhaseData::aggstrategy, Assert, CHECK_FOR_INTERRUPTS, AggState::current_phase, AggState::current_set, ExprContext::ecxt_outertuple, ExecStoreMinimalTuple(), hash(), hash_agg_set_limits(), hash_agg_update_metrics(), AggState::hash_batches, AggState::hash_mem_limit, AggState::hash_ngroups_current, AggState::hash_ngroups_limit, AggState::hash_pergroup, AggState::hash_spill_mode, AggState::hash_spill_rslot, AggState::hash_tapeset, hashagg_batch_read(), hashagg_recompile_expressions(), hashagg_spill_finish(), hashagg_spill_init(), hashagg_spill_tuple(), AggState::hashcontext, AggState::hashentrysize, AggStatePerHashData::hashiter, AggStatePerHashData::hashslot, AggStatePerHashData::hashtable, initialize_hash_entry(), HashAggBatch::input_card, HashAggBatch::input_tape, linitial, list_delete_first(), LogicalTapeClose(), LookupTupleHashEntryHash(), MemSet, NIL, HashAggSpill::npartitions, AggState::num_hashes, AggState::perhash, pfree(), AggState::phase, AggState::phases, prepare_hash_slot(), ReScanExprContext(), ResetExprContext, ResetTupleHashIterator, ResetTupleHashTable(), select_current_set(), HashAggBatch::setno, AggState::tmpcontext, and HashAggBatch::used_bits.

Referenced by agg_retrieve_hash_table().

2577 {
2578  HashAggBatch *batch;
2579  AggStatePerHash perhash;
2580  HashAggSpill spill;
2581  LogicalTapeSet *tapeset = aggstate->hash_tapeset;
2582  bool spill_initialized = false;
2583 
2584  if (aggstate->hash_batches == NIL)
2585  return false;
2586 
2587  batch = linitial(aggstate->hash_batches);
2588  aggstate->hash_batches = list_delete_first(aggstate->hash_batches);
2589 
2590  hash_agg_set_limits(aggstate->hashentrysize, batch->input_card,
2591  batch->used_bits, &aggstate->hash_mem_limit,
2592  &aggstate->hash_ngroups_limit, NULL);
2593 
2594  /*
2595  * Each batch only processes one grouping set; set the rest to NULL so
2596  * that advance_aggregates() knows to ignore them. We don't touch
2597  * pergroups for sorted grouping sets here, because they will be needed if
2598  * we rescan later. The expressions for sorted grouping sets will not be
2599  * evaluated after we recompile anyway.
2600  */
2601  MemSet(aggstate->hash_pergroup, 0,
2602  sizeof(AggStatePerGroup) * aggstate->num_hashes);
2603 
2604  /* free memory and reset hash tables */
2605  ReScanExprContext(aggstate->hashcontext);
2606  for (int setno = 0; setno < aggstate->num_hashes; setno++)
2607  ResetTupleHashTable(aggstate->perhash[setno].hashtable);
2608 
2609  aggstate->hash_ngroups_current = 0;
2610 
2611  /*
2612  * In AGG_MIXED mode, hash aggregation happens in phase 1 and the output
2613  * happens in phase 0. So, we switch to phase 1 when processing a batch,
2614  * and back to phase 0 after the batch is done.
2615  */
2616  Assert(aggstate->current_phase == 0);
2617  if (aggstate->phase->aggstrategy == AGG_MIXED)
2618  {
2619  aggstate->current_phase = 1;
2620  aggstate->phase = &aggstate->phases[aggstate->current_phase];
2621  }
2622 
2623  select_current_set(aggstate, batch->setno, true);
2624 
2625  perhash = &aggstate->perhash[aggstate->current_set];
2626 
2627  /*
2628  * Spilled tuples are always read back as MinimalTuples, which may be
2629  * different from the outer plan, so recompile the aggregate expressions.
2630  *
2631  * We still need the NULL check, because we are only processing one
2632  * grouping set at a time and the rest will be NULL.
2633  */
2634  hashagg_recompile_expressions(aggstate, true, true);
2635 
2636  for (;;)
2637  {
2638  TupleTableSlot *spillslot = aggstate->hash_spill_rslot;
2639  TupleTableSlot *hashslot = perhash->hashslot;
2640  TupleHashEntry entry;
2641  MinimalTuple tuple;
2642  uint32 hash;
2643  bool isnew = false;
2644  bool *p_isnew = aggstate->hash_spill_mode ? NULL : &isnew;
2645 
2647 
2648  tuple = hashagg_batch_read(batch, &hash);
2649  if (tuple == NULL)
2650  break;
2651 
2652  ExecStoreMinimalTuple(tuple, spillslot, true);
2653  aggstate->tmpcontext->ecxt_outertuple = spillslot;
2654 
2655  prepare_hash_slot(perhash,
2656  aggstate->tmpcontext->ecxt_outertuple,
2657  hashslot);
2658  entry = LookupTupleHashEntryHash(
2659  perhash->hashtable, hashslot, p_isnew, hash);
2660 
2661  if (entry != NULL)
2662  {
2663  if (isnew)
2664  initialize_hash_entry(aggstate, perhash->hashtable, entry);
2665  aggstate->hash_pergroup[batch->setno] = entry->additional;
2666  advance_aggregates(aggstate);
2667  }
2668  else
2669  {
2670  if (!spill_initialized)
2671  {
2672  /*
2673  * Avoid initializing the spill until we actually need it so
2674  * that we don't assign tapes that will never be used.
2675  */
2676  spill_initialized = true;
2677  hashagg_spill_init(&spill, tapeset, batch->used_bits,
2678  batch->input_card, aggstate->hashentrysize);
2679  }
2680  /* no memory for a new group, spill */
2681  hashagg_spill_tuple(aggstate, &spill, spillslot, hash);
2682 
2683  aggstate->hash_pergroup[batch->setno] = NULL;
2684  }
2685 
2686  /*
2687  * Reset per-input-tuple context after each tuple, but note that the
2688  * hash lookups do this too
2689  */
2690  ResetExprContext(aggstate->tmpcontext);
2691  }
2692 
2693  LogicalTapeClose(batch->input_tape);
2694 
2695  /* change back to phase 0 */
2696  aggstate->current_phase = 0;
2697  aggstate->phase = &aggstate->phases[aggstate->current_phase];
2698 
2699  if (spill_initialized)
2700  {
2701  hashagg_spill_finish(aggstate, &spill, batch->setno);
2702  hash_agg_update_metrics(aggstate, true, spill.npartitions);
2703  }
2704  else
2705  hash_agg_update_metrics(aggstate, true, 0);
2706 
2707  aggstate->hash_spill_mode = false;
2708 
2709  /* prepare to walk the first hash table */
2710  select_current_set(aggstate, batch->setno, true);
2711  ResetTupleHashIterator(aggstate->perhash[batch->setno].hashtable,
2712  &aggstate->perhash[batch->setno].hashiter);
2713 
2714  pfree(batch);
2715 
2716  return true;
2717 }
AggStatePerGroup * hash_pergroup
Definition: execnodes.h:2341
#define NIL
Definition: pg_list.h:65
static void select_current_set(AggState *aggstate, int setno, bool is_hash)
Definition: nodeAgg.c:457
AggStatePerPhase phases
Definition: execnodes.h:2308
double hashentrysize
Definition: execnodes.h:2333
static void hash_agg_update_metrics(AggState *aggstate, bool from_tape, int npartitions)
Definition: nodeAgg.c:1899
TupleHashEntry LookupTupleHashEntryHash(TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew, uint32 hash)
Definition: execGrouping.c:361
uint64 hash_ngroups_limit
Definition: execnodes.h:2330
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1446
static void hashagg_spill_finish(AggState *aggstate, HashAggSpill *spill, int setno)
Definition: nodeAgg.c:3074
int current_phase
Definition: execnodes.h:2285
#define MemSet(start, val, len)
Definition: c.h:1008
TupleTableSlot * hash_spill_rslot
Definition: execnodes.h:2323
static void advance_aggregates(AggState *aggstate)
Definition: nodeAgg.c:820
int current_set
Definition: execnodes.h:2300
void LogicalTapeClose(LogicalTape *lt)
Definition: logtape.c:742
void ResetTupleHashTable(TupleHashTable hashtable)
Definition: execGrouping.c:285
ExprContext * tmpcontext
Definition: execnodes.h:2290
bool hash_spill_mode
Definition: execnodes.h:2327
List * hash_batches
Definition: execnodes.h:2325
void pfree(void *pointer)
Definition: mcxt.c:1169
#define linitial(l)
Definition: pg_list.h:174
struct LogicalTapeSet * hash_tapeset
Definition: execnodes.h:2320
int npartitions
Definition: nodeAgg.c:334
static Size hashagg_spill_tuple(AggState *aggstate, HashAggSpill *spill, TupleTableSlot *slot, uint32 hash)
Definition: nodeAgg.c:2906
int used_bits
Definition: nodeAgg.c:354
unsigned int uint32
Definition: c.h:441
AggStatePerHash perhash
Definition: execnodes.h:2340
AggStrategy aggstrategy
Definition: nodeAgg.h:274
static void hashagg_spill_init(HashAggSpill *spill, LogicalTapeSet *lts, int used_bits, double input_groups, double hashentrysize)
Definition: nodeAgg.c:2875
double input_card
Definition: nodeAgg.c:357
TupleHashIterator hashiter
Definition: nodeAgg.h:304
int num_hashes
Definition: execnodes.h:2318
ExprContext * hashcontext
Definition: execnodes.h:2288
AggStatePerPhase phase
Definition: execnodes.h:2283
#define ResetTupleHashIterator(htable, iter)
Definition: execnodes.h:770
static void hashagg_recompile_expressions(AggState *aggstate, bool minslot, bool nullcheck)
Definition: nodeAgg.c:1723
static void prepare_hash_slot(AggStatePerHash perhash, TupleTableSlot *inputslot, TupleTableSlot *hashslot)
Definition: nodeAgg.c:1201
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:231
#define Assert(condition)
Definition: c.h:804
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:438
Size hash_mem_limit
Definition: execnodes.h:2329
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:1780
static MinimalTuple hashagg_batch_read(HashAggBatch *batch, uint32 *hashp)
Definition: nodeAgg.c:2991
uint64 hash_ngroups_current
Definition: execnodes.h:2335
static void initialize_hash_entry(AggState *aggstate, TupleHashTable hashtable, TupleHashEntry entry)
Definition: nodeAgg.c:2027
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
TupleTableSlot * hashslot
Definition: nodeAgg.h:305
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:715
TupleHashTable hashtable
Definition: nodeAgg.h:303
#define ResetExprContext(econtext)
Definition: executor.h:527
List * list_delete_first(List *list)
Definition: list.c:875
LogicalTape * input_tape
Definition: nodeAgg.c:355

◆ agg_retrieve_direct()

static TupleTableSlot * agg_retrieve_direct ( AggState aggstate)
static

Definition at line 2176 of file nodeAgg.c.

References advance_aggregates(), AggState::agg_done, AGG_MIXED, AGG_PLAIN, agg_retrieve_hash_table(), AggState::aggcontexts, AggStatePerPhaseData::aggnode, Agg::aggstrategy, AggState::aggstrategy, Assert, AggState::current_phase, ExprContext::ecxt_innertuple, ExprContext::ecxt_outertuple, AggStatePerPhaseData::eqfunctions, ExecCopySlotHeapTuple(), ExecForceStoreHeapTuple(), ExecQual(), ExecQualAndReset(), fetch_input_tuple(), finalize_aggregates(), AggState::grp_firstTuple, AggStatePerPhaseData::gset_lengths, hashagg_finish_initial_spills(), AggStatePerHashData::hashiter, AggStatePerHashData::hashtable, i, initialize_aggregates(), initialize_phase(), AggState::input_done, lookup_hash_entries(), Max, Agg::numCols, AggState::numphases, AggStatePerPhaseData::numsets, AggState::peragg, AggState::pergroups, AggState::perhash, AggState::phase, prepare_projection_slot(), project_aggregates(), AggState::projected_set, ScanState::ps, PlanState::ps_ExprContext, ReScanExprContext(), ResetExprContext, ResetTupleHashIterator, select_current_set(), AggState::ss, ScanState::ss_ScanTupleSlot, AggState::table_filled, AggState::tmpcontext, and TupIsNull.

Referenced by ExecAgg().

2177 {
2178  Agg *node = aggstate->phase->aggnode;
2179  ExprContext *econtext;
2180  ExprContext *tmpcontext;
2181  AggStatePerAgg peragg;
2182  AggStatePerGroup *pergroups;
2183  TupleTableSlot *outerslot;
2184  TupleTableSlot *firstSlot;
2185  TupleTableSlot *result;
2186  bool hasGroupingSets = aggstate->phase->numsets > 0;
2187  int numGroupingSets = Max(aggstate->phase->numsets, 1);
2188  int currentSet;
2189  int nextSetSize;
2190  int numReset;
2191  int i;
2192 
2193  /*
2194  * get state info from node
2195  *
2196  * econtext is the per-output-tuple expression context
2197  *
2198  * tmpcontext is the per-input-tuple expression context
2199  */
2200  econtext = aggstate->ss.ps.ps_ExprContext;
2201  tmpcontext = aggstate->tmpcontext;
2202 
2203  peragg = aggstate->peragg;
2204  pergroups = aggstate->pergroups;
2205  firstSlot = aggstate->ss.ss_ScanTupleSlot;
2206 
2207  /*
2208  * We loop retrieving groups until we find one matching
2209  * aggstate->ss.ps.qual
2210  *
2211  * For grouping sets, we have the invariant that aggstate->projected_set
2212  * is either -1 (initial call) or the index (starting from 0) in
2213  * gset_lengths for the group we just completed (either by projecting a
2214  * row or by discarding it in the qual).
2215  */
2216  while (!aggstate->agg_done)
2217  {
2218  /*
2219  * Clear the per-output-tuple context for each group, as well as
2220  * aggcontext (which contains any pass-by-ref transvalues of the old
2221  * group). Some aggregate functions store working state in child
2222  * contexts; those now get reset automatically without us needing to
2223  * do anything special.
2224  *
2225  * We use ReScanExprContext not just ResetExprContext because we want
2226  * any registered shutdown callbacks to be called. That allows
2227  * aggregate functions to ensure they've cleaned up any non-memory
2228  * resources.
2229  */
2230  ReScanExprContext(econtext);
2231 
2232  /*
2233  * Determine how many grouping sets need to be reset at this boundary.
2234  */
2235  if (aggstate->projected_set >= 0 &&
2236  aggstate->projected_set < numGroupingSets)
2237  numReset = aggstate->projected_set + 1;
2238  else
2239  numReset = numGroupingSets;
2240 
2241  /*
2242  * numReset can change on a phase boundary, but that's OK; we want to
2243  * reset the contexts used in _this_ phase, and later, after possibly
2244  * changing phase, initialize the right number of aggregates for the
2245  * _new_ phase.
2246  */
2247 
2248  for (i = 0; i < numReset; i++)
2249  {
2250  ReScanExprContext(aggstate->aggcontexts[i]);
2251  }
2252 
2253  /*
2254  * Check if input is complete and there are no more groups to project
2255  * in this phase; move to next phase or mark as done.
2256  */
2257  if (aggstate->input_done == true &&
2258  aggstate->projected_set >= (numGroupingSets - 1))
2259  {
2260  if (aggstate->current_phase < aggstate->numphases - 1)
2261  {
2262  initialize_phase(aggstate, aggstate->current_phase + 1);
2263  aggstate->input_done = false;
2264  aggstate->projected_set = -1;
2265  numGroupingSets = Max(aggstate->phase->numsets, 1);
2266  node = aggstate->phase->aggnode;
2267  numReset = numGroupingSets;
2268  }
2269  else if (aggstate->aggstrategy == AGG_MIXED)
2270  {
2271  /*
2272  * Mixed mode; we've output all the grouped stuff and have
2273  * full hashtables, so switch to outputting those.
2274  */
2275  initialize_phase(aggstate, 0);
2276  aggstate->table_filled = true;
2278  &aggstate->perhash[0].hashiter);
2279  select_current_set(aggstate, 0, true);
2280  return agg_retrieve_hash_table(aggstate);
2281  }
2282  else
2283  {
2284  aggstate->agg_done = true;
2285  break;
2286  }
2287  }
2288 
2289  /*
2290  * Get the number of columns in the next grouping set after the last
2291  * projected one (if any). This is the number of columns to compare to
2292  * see if we reached the boundary of that set too.
2293  */
2294  if (aggstate->projected_set >= 0 &&
2295  aggstate->projected_set < (numGroupingSets - 1))
2296  nextSetSize = aggstate->phase->gset_lengths[aggstate->projected_set + 1];
2297  else
2298  nextSetSize = 0;
2299 
2300  /*----------
2301  * If a subgroup for the current grouping set is present, project it.
2302  *
2303  * We have a new group if:
2304  * - we're out of input but haven't projected all grouping sets
2305  * (checked above)
2306  * OR
2307  * - we already projected a row that wasn't from the last grouping
2308  * set
2309  * AND
2310  * - the next grouping set has at least one grouping column (since
2311  * empty grouping sets project only once input is exhausted)
2312  * AND
2313  * - the previous and pending rows differ on the grouping columns
2314  * of the next grouping set
2315  *----------
2316  */
2317  tmpcontext->ecxt_innertuple = econtext->ecxt_outertuple;
2318  if (aggstate->input_done ||
2319  (node->aggstrategy != AGG_PLAIN &&
2320  aggstate->projected_set != -1 &&
2321  aggstate->projected_set < (numGroupingSets - 1) &&
2322  nextSetSize > 0 &&
2323  !ExecQualAndReset(aggstate->phase->eqfunctions[nextSetSize - 1],
2324  tmpcontext)))
2325  {
2326  aggstate->projected_set += 1;
2327 
2328  Assert(aggstate->projected_set < numGroupingSets);
2329  Assert(nextSetSize > 0 || aggstate->input_done);
2330  }
2331  else
2332  {
2333  /*
2334  * We no longer care what group we just projected, the next
2335  * projection will always be the first (or only) grouping set
2336  * (unless the input proves to be empty).
2337  */
2338  aggstate->projected_set = 0;
2339 
2340  /*
2341  * If we don't already have the first tuple of the new group,
2342  * fetch it from the outer plan.
2343  */
2344  if (aggstate->grp_firstTuple == NULL)
2345  {
2346  outerslot = fetch_input_tuple(aggstate);
2347  if (!TupIsNull(outerslot))
2348  {
2349  /*
2350  * Make a copy of the first input tuple; we will use this
2351  * for comparisons (in group mode) and for projection.
2352  */
2353  aggstate->grp_firstTuple = ExecCopySlotHeapTuple(outerslot);
2354  }
2355  else
2356  {
2357  /* outer plan produced no tuples at all */
2358  if (hasGroupingSets)
2359  {
2360  /*
2361  * If there was no input at all, we need to project
2362  * rows only if there are grouping sets of size 0.
2363  * Note that this implies that there can't be any
2364  * references to ungrouped Vars, which would otherwise
2365  * cause issues with the empty output slot.
2366  *
2367  * XXX: This is no longer true, we currently deal with
2368  * this in finalize_aggregates().
2369  */
2370  aggstate->input_done = true;
2371 
2372  while (aggstate->phase->gset_lengths[aggstate->projected_set] > 0)
2373  {
2374  aggstate->projected_set += 1;
2375  if (aggstate->projected_set >= numGroupingSets)
2376  {
2377  /*
2378  * We can't set agg_done here because we might
2379  * have more phases to do, even though the
2380  * input is empty. So we need to restart the
2381  * whole outer loop.
2382  */
2383  break;
2384  }
2385  }
2386 
2387  if (aggstate->projected_set >= numGroupingSets)
2388  continue;
2389  }
2390  else
2391  {
2392  aggstate->agg_done = true;
2393  /* If we are grouping, we should produce no tuples too */
2394  if (node->aggstrategy != AGG_PLAIN)
2395  return NULL;
2396  }
2397  }
2398  }
2399 
2400  /*
2401  * Initialize working state for a new input tuple group.
2402  */
2403  initialize_aggregates(aggstate, pergroups, numReset);
2404 
2405  if (aggstate->grp_firstTuple != NULL)
2406  {
2407  /*
2408  * Store the copied first input tuple in the tuple table slot
2409  * reserved for it. The tuple will be deleted when it is
2410  * cleared from the slot.
2411  */
2413  firstSlot, true);
2414  aggstate->grp_firstTuple = NULL; /* don't keep two pointers */
2415 
2416  /* set up for first advance_aggregates call */
2417  tmpcontext->ecxt_outertuple = firstSlot;
2418 
2419  /*
2420  * Process each outer-plan tuple, and then fetch the next one,
2421  * until we exhaust the outer plan or cross a group boundary.
2422  */
2423  for (;;)
2424  {
2425  /*
2426  * During phase 1 only of a mixed agg, we need to update
2427  * hashtables as well in advance_aggregates.
2428  */
2429  if (aggstate->aggstrategy == AGG_MIXED &&
2430  aggstate->current_phase == 1)
2431  {
2432  lookup_hash_entries(aggstate);
2433  }
2434 
2435  /* Advance the aggregates (or combine functions) */
2436  advance_aggregates(aggstate);
2437 
2438  /* Reset per-input-tuple context after each tuple */
2439  ResetExprContext(tmpcontext);
2440 
2441  outerslot = fetch_input_tuple(aggstate);
2442  if (TupIsNull(outerslot))
2443  {
2444  /* no more outer-plan tuples available */
2445 
2446  /* if we built hash tables, finalize any spills */
2447  if (aggstate->aggstrategy == AGG_MIXED &&
2448  aggstate->current_phase == 1)
2450 
2451  if (hasGroupingSets)
2452  {
2453  aggstate->input_done = true;
2454  break;
2455  }
2456  else
2457  {
2458  aggstate->agg_done = true;
2459  break;
2460  }
2461  }
2462  /* set up for next advance_aggregates call */
2463  tmpcontext->ecxt_outertuple = outerslot;
2464 
2465  /*
2466  * If we are grouping, check whether we've crossed a group
2467  * boundary.
2468  */
2469  if (node->aggstrategy != AGG_PLAIN)
2470  {
2471  tmpcontext->ecxt_innertuple = firstSlot;
2472  if (!ExecQual(aggstate->phase->eqfunctions[node->numCols - 1],
2473  tmpcontext))
2474  {
2475  aggstate->grp_firstTuple = ExecCopySlotHeapTuple(outerslot);
2476  break;
2477  }
2478  }
2479  }
2480  }
2481 
2482  /*
2483  * Use the representative input tuple for any references to
2484  * non-aggregated input columns in aggregate direct args, the node
2485  * qual, and the tlist. (If we are not grouping, and there are no
2486  * input rows at all, we will come here with an empty firstSlot
2487  * ... but if not grouping, there can't be any references to
2488  * non-aggregated input columns, so no problem.)
2489  */
2490  econtext->ecxt_outertuple = firstSlot;
2491  }
2492 
2493  Assert(aggstate->projected_set >= 0);
2494 
2495  currentSet = aggstate->projected_set;
2496 
2497  prepare_projection_slot(aggstate, econtext->ecxt_outertuple, currentSet);
2498 
2499  select_current_set(aggstate, currentSet, false);
2500 
2501  finalize_aggregates(aggstate,
2502  peragg,
2503  pergroups[currentSet]);
2504 
2505  /*
2506  * If there's no row to project right now, we must continue rather
2507  * than returning a null since there might be more groups.
2508  */
2509  result = project_aggregates(aggstate);
2510  if (result)
2511  return result;
2512  }
2513 
2514  /* No more groups */
2515  return NULL;
2516 }
ExprState ** eqfunctions
Definition: nodeAgg.h:278
static TupleTableSlot * fetch_input_tuple(AggState *aggstate)
Definition: nodeAgg.c:549
static void select_current_set(AggState *aggstate, int setno, bool is_hash)
Definition: nodeAgg.c:457
int numCols
Definition: plannodes.h:865
bool agg_done
Definition: execnodes.h:2297
ScanState ss
Definition: execnodes.h:2277
ExprContext * ps_ExprContext
Definition: execnodes.h:1006
int current_phase
Definition: execnodes.h:2285
static void hashagg_finish_initial_spills(AggState *aggstate)
Definition: nodeAgg.c:3040
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1381
int projected_set
Definition: execnodes.h:2298
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:396
HeapTuple grp_firstTuple
Definition: execnodes.h:2315
static TupleTableSlot * project_aggregates(AggState *aggstate)
Definition: nodeAgg.c:1354
static void advance_aggregates(AggState *aggstate)
Definition: nodeAgg.c:820
void ExecForceStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1469
ExprContext * tmpcontext
Definition: execnodes.h:2290
static void prepare_projection_slot(AggState *aggstate, TupleTableSlot *slot, int currentSet)
Definition: nodeAgg.c:1246
PlanState ps
Definition: execnodes.h:1378
AggStrategy aggstrategy
Definition: plannodes.h:863
bool table_filled
Definition: execnodes.h:2317
AggStrategy aggstrategy
Definition: execnodes.h:2281
static TupleTableSlot * agg_retrieve_hash_table(AggState *aggstate)
Definition: nodeAgg.c:2727
static void finalize_aggregates(AggState *aggstate, AggStatePerAgg peragg, AggStatePerGroup pergroup)
Definition: nodeAgg.c:1291
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:229
#define TupIsNull(slot)
Definition: tuptable.h:292
AggStatePerHash perhash
Definition: execnodes.h:2340
static void initialize_aggregates(AggState *aggstate, AggStatePerGroup *pergroups, int numReset)
Definition: nodeAgg.c:667
TupleHashIterator hashiter
Definition: nodeAgg.h:304
bool input_done
Definition: execnodes.h:2296
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
Definition: executor.h:423
AggStatePerPhase phase
Definition: execnodes.h:2283
static void initialize_phase(AggState *aggstate, int newphase)
Definition: nodeAgg.c:479
#define ResetTupleHashIterator(htable, iter)
Definition: execnodes.h:770
static HeapTuple ExecCopySlotHeapTuple(TupleTableSlot *slot)
Definition: tuptable.h:452
#define Max(x, y)
Definition: c.h:980
ExprContext ** aggcontexts
Definition: execnodes.h:2289
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:231
#define Assert(condition)
Definition: c.h:804
AggStatePerGroup * pergroups
Definition: execnodes.h:2313
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:438
int numphases
Definition: execnodes.h:2284
AggStatePerAgg peragg
Definition: execnodes.h:2286
int i
Definition: plannodes.h:860
TupleHashTable hashtable
Definition: nodeAgg.h:303
static void lookup_hash_entries(AggState *aggstate)
Definition: nodeAgg.c:2077
#define ResetExprContext(econtext)
Definition: executor.h:527

◆ agg_retrieve_hash_table()

static TupleTableSlot * agg_retrieve_hash_table ( AggState aggstate)
static

Definition at line 2727 of file nodeAgg.c.

References AggState::agg_done, agg_refill_hash_table(), and agg_retrieve_hash_table_in_memory().

Referenced by agg_retrieve_direct(), and ExecAgg().

2728 {
2729  TupleTableSlot *result = NULL;
2730 
2731  while (result == NULL)
2732  {
2733  result = agg_retrieve_hash_table_in_memory(aggstate);
2734  if (result == NULL)
2735  {
2736  if (!agg_refill_hash_table(aggstate))
2737  {
2738  aggstate->agg_done = true;
2739  break;
2740  }
2741  }
2742  }
2743 
2744  return result;
2745 }
bool agg_done
Definition: execnodes.h:2297
static bool agg_refill_hash_table(AggState *aggstate)
Definition: nodeAgg.c:2576
static TupleTableSlot * agg_retrieve_hash_table_in_memory(AggState *aggstate)
Definition: nodeAgg.c:2752

◆ agg_retrieve_hash_table_in_memory()

static TupleTableSlot * agg_retrieve_hash_table_in_memory ( AggState aggstate)
static

Definition at line 2752 of file nodeAgg.c.

References TupleHashEntryData::additional, CHECK_FOR_INTERRUPTS, AggState::current_set, ExprContext::ecxt_outertuple, ExecClearTuple(), ExecStoreMinimalTuple(), ExecStoreVirtualTuple(), finalize_aggregates(), TupleHashEntryData::firstTuple, AggStatePerHashData::hashGrpColIdxInput, AggStatePerHashData::hashiter, AggStatePerHashData::hashslot, AggStatePerHashData::hashtable, i, TupleDescData::natts, AggStatePerHashData::numhashGrpCols, AggState::peragg, AggState::perhash, prepare_projection_slot(), project_aggregates(), ScanState::ps, PlanState::ps_ExprContext, ResetExprContext, ResetTupleHashIterator, ScanTupleHashTable, select_current_set(), slot_getallattrs(), AggState::ss, ScanState::ss_ScanTupleSlot, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, and TupleTableSlot::tts_values.

Referenced by agg_retrieve_hash_table().

2753 {
2754  ExprContext *econtext;
2755  AggStatePerAgg peragg;
2756  AggStatePerGroup pergroup;
2757  TupleHashEntryData *entry;
2758  TupleTableSlot *firstSlot;
2759  TupleTableSlot *result;
2760  AggStatePerHash perhash;
2761 
2762  /*
2763  * get state info from node.
2764  *
2765  * econtext is the per-output-tuple expression context.
2766  */
2767  econtext = aggstate->ss.ps.ps_ExprContext;
2768  peragg = aggstate->peragg;
2769  firstSlot = aggstate->ss.ss_ScanTupleSlot;
2770 
2771  /*
2772  * Note that perhash (and therefore anything accessed through it) can
2773  * change inside the loop, as we change between grouping sets.
2774  */
2775  perhash = &aggstate->perhash[aggstate->current_set];
2776 
2777  /*
2778  * We loop retrieving groups until we find one satisfying
2779  * aggstate->ss.ps.qual
2780  */
2781  for (;;)
2782  {
2783  TupleTableSlot *hashslot = perhash->hashslot;
2784  int i;
2785 
2787 
2788  /*
2789  * Find the next entry in the hash table
2790  */
2791  entry = ScanTupleHashTable(perhash->hashtable, &perhash->hashiter);
2792  if (entry == NULL)
2793  {
2794  int nextset = aggstate->current_set + 1;
2795 
2796  if (nextset < aggstate->num_hashes)
2797  {
2798  /*
2799  * Switch to next grouping set, reinitialize, and restart the
2800  * loop.
2801  */
2802  select_current_set(aggstate, nextset, true);
2803 
2804  perhash = &aggstate->perhash[aggstate->current_set];
2805 
2806  ResetTupleHashIterator(perhash->hashtable, &perhash->hashiter);
2807 
2808  continue;
2809  }
2810  else
2811  {
2812  return NULL;
2813  }
2814  }
2815 
2816  /*
2817  * Clear the per-output-tuple context for each group
2818  *
2819  * We intentionally don't use ReScanExprContext here; if any aggs have
2820  * registered shutdown callbacks, they mustn't be called yet, since we
2821  * might not be done with that agg.
2822  */
2823  ResetExprContext(econtext);
2824 
2825  /*
2826  * Transform representative tuple back into one with the right
2827  * columns.
2828  */
2829  ExecStoreMinimalTuple(entry->firstTuple, hashslot, false);
2830  slot_getallattrs(hashslot);
2831 
2832  ExecClearTuple(firstSlot);
2833  memset(firstSlot->tts_isnull, true,
2834  firstSlot->tts_tupleDescriptor->natts * sizeof(bool));
2835 
2836  for (i = 0; i < perhash->numhashGrpCols; i++)
2837  {
2838  int varNumber = perhash->hashGrpColIdxInput[i] - 1;
2839 
2840  firstSlot->tts_values[varNumber] = hashslot->tts_values[i];
2841  firstSlot->tts_isnull[varNumber] = hashslot->tts_isnull[i];
2842  }
2843  ExecStoreVirtualTuple(firstSlot);
2844 
2845  pergroup = (AggStatePerGroup) entry->additional;
2846 
2847  /*
2848  * Use the representative input tuple for any references to
2849  * non-aggregated input columns in the qual and tlist.
2850  */
2851  econtext->ecxt_outertuple = firstSlot;
2852 
2853  prepare_projection_slot(aggstate,
2854  econtext->ecxt_outertuple,
2855  aggstate->current_set);
2856 
2857  finalize_aggregates(aggstate, peragg, pergroup);
2858 
2859  result = project_aggregates(aggstate);
2860  if (result)
2861  return result;
2862  }
2863 
2864  /* No more groups */
2865  return NULL;
2866 }
struct AggStatePerGroupData * AggStatePerGroup
Definition: execnodes.h:2271
#define ScanTupleHashTable(htable, iter)
Definition: execnodes.h:772
static void select_current_set(AggState *aggstate, int setno, bool is_hash)
Definition: nodeAgg.c:457
AttrNumber * hashGrpColIdxInput
Definition: nodeAgg.h:311
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1446
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
ScanState ss
Definition: execnodes.h:2277
ExprContext * ps_ExprContext
Definition: execnodes.h:1006
MinimalTuple firstTuple
Definition: execnodes.h:725
Datum * tts_values
Definition: tuptable.h:126
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1381
static TupleTableSlot * project_aggregates(AggState *aggstate)
Definition: nodeAgg.c:1354
int current_set
Definition: execnodes.h:2300
static void prepare_projection_slot(AggState *aggstate, TupleTableSlot *slot, int currentSet)
Definition: nodeAgg.c:1246
PlanState ps
Definition: execnodes.h:1378
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:354
static void finalize_aggregates(AggState *aggstate, AggStatePerAgg peragg, AggStatePerGroup pergroup)
Definition: nodeAgg.c:1291
bool * tts_isnull
Definition: tuptable.h:128
AggStatePerHash perhash
Definition: execnodes.h:2340
TupleHashIterator hashiter
Definition: nodeAgg.h:304
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
#define ResetTupleHashIterator(htable, iter)
Definition: execnodes.h:770
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:231
AggStatePerAgg peragg
Definition: execnodes.h:2286
int i
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
TupleTableSlot * hashslot
Definition: nodeAgg.h:305
TupleHashTable hashtable
Definition: nodeAgg.h:303
#define ResetExprContext(econtext)
Definition: executor.h:527
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1552

◆ AggCheckCallContext()

int AggCheckCallContext ( FunctionCallInfo  fcinfo,
MemoryContext aggcontext 
)

Definition at line 4495 of file nodeAgg.c.

References AGG_CONTEXT_AGGREGATE, AGG_CONTEXT_WINDOW, FunctionCallInfoBaseData::context, AggState::curaggcontext, ExprContext::ecxt_per_tuple_memory, and IsA.

Referenced by array_agg_array_finalfn(), array_agg_array_transfn(), array_agg_finalfn(), array_agg_transfn(), bytea_string_agg_finalfn(), fetch_array_arg_replace_nulls(), float4_accum(), float8_accum(), float8_combine(), float8_regr_accum(), float8_regr_combine(), hypothetical_dense_rank_final(), hypothetical_rank_common(), int2_avg_accum(), int2_avg_accum_inv(), int2_sum(), int4_avg_accum(), int4_avg_accum_inv(), int4_avg_combine(), int4_sum(), int8_avg_combine(), int8_avg_deserialize(), int8_avg_serialize(), int8dec(), int8inc(), json_agg_finalfn(), json_agg_transfn(), json_object_agg_finalfn(), json_object_agg_transfn(), jsonb_agg_finalfn(), jsonb_agg_transfn(), jsonb_object_agg_finalfn(), jsonb_object_agg_transfn(), makeBoolAggState(), makeNumericAggState(), makeStringAggState(), mode_final(), multirange_intersect_agg_transfn(), numeric_accum_inv(), numeric_avg_combine(), numeric_avg_deserialize(), numeric_avg_serialize(), numeric_combine(), numeric_deserialize(), numeric_poly_combine(), numeric_poly_deserialize(), numeric_poly_serialize(), numeric_serialize(), ordered_set_startup(), percentile_cont_final_common(), percentile_cont_multi_final_common(), percentile_disc_final(), percentile_disc_multi_final(), range_agg_finalfn(), range_agg_transfn(), range_intersect_agg_transfn(), and string_agg_finalfn().

4496 {
4497  if (fcinfo->context && IsA(fcinfo->context, AggState))
4498  {
4499  if (aggcontext)
4500  {
4501  AggState *aggstate = ((AggState *) fcinfo->context);
4502  ExprContext *cxt = aggstate->curaggcontext;
4503 
4504  *aggcontext = cxt->ecxt_per_tuple_memory;
4505  }
4506  return AGG_CONTEXT_AGGREGATE;
4507  }
4508  if (fcinfo->context && IsA(fcinfo->context, WindowAggState))
4509  {
4510  if (aggcontext)
4511  *aggcontext = ((WindowAggState *) fcinfo->context)->curaggcontext;
4512  return AGG_CONTEXT_WINDOW;
4513  }
4514 
4515  /* this is just to prevent "uninitialized variable" warnings */
4516  if (aggcontext)
4517  *aggcontext = NULL;
4518  return 0;
4519 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:235
fmNodePtr context
Definition: fmgr.h:88
#define AGG_CONTEXT_AGGREGATE
Definition: fmgr.h:738
ExprContext * curaggcontext
Definition: execnodes.h:2292
#define AGG_CONTEXT_WINDOW
Definition: fmgr.h:739

◆ AggGetAggref()

Aggref* AggGetAggref ( FunctionCallInfo  fcinfo)

Definition at line 4539 of file nodeAgg.c.

References AggStatePerTransData::aggref, AggStatePerAggData::aggref, FunctionCallInfoBaseData::context, AggState::curperagg, AggState::curpertrans, and IsA.

Referenced by ordered_set_startup().

4540 {
4541  if (fcinfo->context && IsA(fcinfo->context, AggState))
4542  {
4543  AggState *aggstate = (AggState *) fcinfo->context;
4544  AggStatePerAgg curperagg;
4545  AggStatePerTrans curpertrans;
4546 
4547  /* check curperagg (valid when in a final function) */
4548  curperagg = aggstate->curperagg;
4549 
4550  if (curperagg)
4551  return curperagg->aggref;
4552 
4553  /* check curpertrans (valid when in a transition function) */
4554  curpertrans = aggstate->curpertrans;
4555 
4556  if (curpertrans)
4557  return curpertrans->aggref;
4558  }
4559  return NULL;
4560 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
fmNodePtr context
Definition: fmgr.h:88
Aggref * aggref
Definition: nodeAgg.h:187
Aggref * aggref
Definition: nodeAgg.h:44
AggStatePerAgg curperagg
Definition: execnodes.h:2293
AggStatePerTrans curpertrans
Definition: execnodes.h:2295

◆ AggGetTempMemoryContext()

MemoryContext AggGetTempMemoryContext ( FunctionCallInfo  fcinfo)

Definition at line 4573 of file nodeAgg.c.

References FunctionCallInfoBaseData::context, ExprContext::ecxt_per_tuple_memory, IsA, and AggState::tmpcontext.

4574 {
4575  if (fcinfo->context && IsA(fcinfo->context, AggState))
4576  {
4577  AggState *aggstate = (AggState *) fcinfo->context;
4578 
4579  return aggstate->tmpcontext->ecxt_per_tuple_memory;
4580  }
4581  return NULL;
4582 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:235
fmNodePtr context
Definition: fmgr.h:88
ExprContext * tmpcontext
Definition: execnodes.h:2290

◆ AggRegisterCallback()

void AggRegisterCallback ( FunctionCallInfo  fcinfo,
ExprContextCallbackFunction  func,
Datum  arg 
)

Definition at line 4638 of file nodeAgg.c.

References FunctionCallInfoBaseData::context, AggState::curaggcontext, elog, ERROR, IsA, and RegisterExprContextCallback().

Referenced by ordered_set_startup().

4641 {
4642  if (fcinfo->context && IsA(fcinfo->context, AggState))
4643  {
4644  AggState *aggstate = (AggState *) fcinfo->context;
4645  ExprContext *cxt = aggstate->curaggcontext;
4646 
4647  RegisterExprContextCallback(cxt, func, arg);
4648 
4649  return;
4650  }
4651  elog(ERROR, "aggregate function cannot register a callback in this context");
4652 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
fmNodePtr context
Definition: fmgr.h:88
#define ERROR
Definition: elog.h:46
ExprContext * curaggcontext
Definition: execnodes.h:2292
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:925
#define elog(elevel,...)
Definition: elog.h:232
void * arg

◆ AggStateIsShared()

bool AggStateIsShared ( FunctionCallInfo  fcinfo)

Definition at line 4599 of file nodeAgg.c.

References AggStatePerTransData::aggshared, FunctionCallInfoBaseData::context, AggState::curperagg, AggState::curpertrans, IsA, and AggState::pertrans.

Referenced by ordered_set_startup().

4600 {
4601  if (fcinfo->context && IsA(fcinfo->context, AggState))
4602  {
4603  AggState *aggstate = (AggState *) fcinfo->context;
4604  AggStatePerAgg curperagg;
4605  AggStatePerTrans curpertrans;
4606 
4607  /* check curperagg (valid when in a final function) */
4608  curperagg = aggstate->curperagg;
4609 
4610  if (curperagg)
4611  return aggstate->pertrans[curperagg->transno].aggshared;
4612 
4613  /* check curpertrans (valid when in a transition function) */
4614  curpertrans = aggstate->curpertrans;
4615 
4616  if (curpertrans)
4617  return curpertrans->aggshared;
4618  }
4619  return true;
4620 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
fmNodePtr context
Definition: fmgr.h:88
AggStatePerTrans pertrans
Definition: execnodes.h:2287
AggStatePerAgg curperagg
Definition: execnodes.h:2293
AggStatePerTrans curpertrans
Definition: execnodes.h:2295

◆ build_hash_table()

static void build_hash_table ( AggState aggstate,
int  setno,
long  nbuckets 
)
static

Definition at line 1486 of file nodeAgg.c.

References AGG_HASHED, AGG_MIXED, AggStatePerHashData::aggnode, AggState::aggsplit, AggState::aggstrategy, Assert, BuildTupleHashTableExt(), DO_AGGSPLIT_SKIPFINAL, ExprContext::ecxt_per_tuple_memory, AggStatePerHashData::eqfuncoids, Agg::grpCollations, AggState::hash_metacxt, AggState::hashcontext, AggStatePerHashData::hashfunctions, AggStatePerHashData::hashGrpColIdxHash, AggStatePerHashData::hashslot, AggStatePerHashData::hashtable, AggStatePerHashData::numCols, AggState::numtrans, AggState::perhash, ScanState::ps, AggState::ss, AggState::tmpcontext, and TupleTableSlot::tts_tupleDescriptor.

Referenced by build_hash_tables().

1487 {
1488  AggStatePerHash perhash = &aggstate->perhash[setno];
1489  MemoryContext metacxt = aggstate->hash_metacxt;
1490  MemoryContext hashcxt = aggstate->hashcontext->ecxt_per_tuple_memory;
1491  MemoryContext tmpcxt = aggstate->tmpcontext->ecxt_per_tuple_memory;
1492  Size additionalsize;
1493 
1494  Assert(aggstate->aggstrategy == AGG_HASHED ||
1495  aggstate->aggstrategy == AGG_MIXED);
1496 
1497  /*
1498  * Used to make sure initial hash table allocation does not exceed
1499  * hash_mem. Note that the estimate does not include space for
1500  * pass-by-reference transition data values, nor for the representative
1501  * tuple of each group.
1502  */
1503  additionalsize = aggstate->numtrans * sizeof(AggStatePerGroupData);
1504 
1505  perhash->hashtable = BuildTupleHashTableExt(&aggstate->ss.ps,
1506  perhash->hashslot->tts_tupleDescriptor,
1507  perhash->numCols,
1508  perhash->hashGrpColIdxHash,
1509  perhash->eqfuncoids,
1510  perhash->hashfunctions,
1511  perhash->aggnode->grpCollations,
1512  nbuckets,
1513  additionalsize,
1514  metacxt,
1515  hashcxt,
1516  tmpcxt,
1517  DO_AGGSPLIT_SKIPFINAL(aggstate->aggsplit));
1518 }
TupleHashTable BuildTupleHashTableExt(PlanState *parent, TupleDesc inputDesc, int numCols, AttrNumber *keyColIdx, const Oid *eqfuncoids, FmgrInfo *hashfunctions, Oid *collations, long nbuckets, Size additionalsize, MemoryContext metacxt, MemoryContext tablecxt, MemoryContext tempcxt, bool use_variable_hash_iv)
Definition: execGrouping.c:154
Oid * grpCollations
Definition: plannodes.h:868
ScanState ss
Definition: execnodes.h:2277
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:235
AggSplit aggsplit
Definition: execnodes.h:2282
int numtrans
Definition: execnodes.h:2280
ExprContext * tmpcontext
Definition: execnodes.h:2290
PlanState ps
Definition: execnodes.h:1378
AggStrategy aggstrategy
Definition: execnodes.h:2281
MemoryContext hash_metacxt
Definition: execnodes.h:2319
struct AggStatePerGroupData AggStatePerGroupData
AggStatePerHash perhash
Definition: execnodes.h:2340
AttrNumber * hashGrpColIdxHash
Definition: nodeAgg.h:312
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
ExprContext * hashcontext
Definition: execnodes.h:2288
FmgrInfo * hashfunctions
Definition: nodeAgg.h:306
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
#define DO_AGGSPLIT_SKIPFINAL(as)
Definition: nodes.h:800
TupleTableSlot * hashslot
Definition: nodeAgg.h:305
TupleHashTable hashtable
Definition: nodeAgg.h:303

◆ build_hash_tables()

static void build_hash_tables ( AggState aggstate)
static

Definition at line 1451 of file nodeAgg.c.

References AggStatePerHashData::aggnode, Assert, build_hash_table(), hash_choose_num_buckets(), AggState::hash_mem_limit, AggState::hash_ngroups_current, AggState::hashentrysize, AggStatePerHashData::hashtable, AggState::num_hashes, Agg::numGroups, AggState::perhash, and ResetTupleHashTable().

Referenced by ExecInitAgg(), and ExecReScanAgg().

1452 {
1453  int setno;
1454 
1455  for (setno = 0; setno < aggstate->num_hashes; ++setno)
1456  {
1457  AggStatePerHash perhash = &aggstate->perhash[setno];
1458  long nbuckets;
1459  Size memory;
1460 
1461  if (perhash->hashtable != NULL)
1462  {
1463  ResetTupleHashTable(perhash->hashtable);
1464  continue;
1465  }
1466 
1467  Assert(perhash->aggnode->numGroups > 0);
1468 
1469  memory = aggstate->hash_mem_limit / aggstate->num_hashes;
1470 
1471  /* choose reasonable number of buckets per hashtable */
1472  nbuckets = hash_choose_num_buckets(aggstate->hashentrysize,
1473  perhash->aggnode->numGroups,
1474  memory);
1475 
1476  build_hash_table(aggstate, setno, nbuckets);
1477  }
1478 
1479  aggstate->hash_ngroups_current = 0;
1480 }
double hashentrysize
Definition: execnodes.h:2333
static long hash_choose_num_buckets(double hashentrysize, long estimated_nbuckets, Size memory)
Definition: nodeAgg.c:1948
void ResetTupleHashTable(TupleHashTable hashtable)
Definition: execGrouping.c:285
static void build_hash_table(AggState *aggstate, int setno, long nbuckets)
Definition: nodeAgg.c:1486
AggStatePerHash perhash
Definition: execnodes.h:2340
int num_hashes
Definition: execnodes.h:2318
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
long numGroups
Definition: plannodes.h:869
Size hash_mem_limit
Definition: execnodes.h:2329
uint64 hash_ngroups_current
Definition: execnodes.h:2335
TupleHashTable hashtable
Definition: nodeAgg.h:303

◆ build_pertrans_for_aggref()

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 
)
static

Definition at line 4026 of file nodeAgg.c.

References AGG_HASHED, AGG_MIXED, AggStatePerTransData::aggCollation, Aggref::aggdirectargs, Aggref::aggdistinct, Aggref::aggfilter, Aggref::aggkind, Aggref::aggorder, AggStatePerTransData::aggref, AggStatePerTransData::aggshared, AggStatePerTransData::aggtranstype, Aggref::aggvariadic, Aggref::args, Assert, build_aggregate_deserialfn_expr(), build_aggregate_serialfn_expr(), build_aggregate_transfn_expr(), AggStatePerTransData::deserialfn, AggStatePerTransData::deserialfn_fcinfo, AggStatePerTransData::deserialfn_oid, DO_AGGSPLIT_COMBINE, AggStatePerTransData::equalfnMulti, AggStatePerTransData::equalfnOne, ExecInitExtraTupleSlot(), execTuplesMatchPrepare(), ExecTypeFromTL(), TargetEntry::expr, exprCollation(), fmgr_info(), fmgr_info_set_expr, get_opcode(), get_sortgroupclause_tle(), get_typlenbyval(), i, InitFunctionCallInfoData, AggStatePerTransData::initValue, initValue(), AggStatePerTransData::initValueIsNull, Aggref::inputcollid, AggStatePerTransData::inputtypeByVal, AggStatePerTransData::inputtypeLen, InvalidOid, lfirst, list_length(), Max, AggState::maxsets, NIL, SortGroupClause::nulls_first, AggStatePerTransData::numDistinctCols, AggStatePerTransData::numInputs, AggStatePerTransData::numSortCols, AggStatePerTransData::numTransInputs, OidIsValid, palloc(), palloc0(), pfree(), TargetEntry::resno, AggStatePerTransData::serialfn, AggStatePerTransData::serialfn_fcinfo, AggStatePerTransData::serialfn_oid, SizeForFunctionCallInfo, AggStatePerTransData::sortColIdx, AggStatePerTransData::sortCollations, AggStatePerTransData::sortdesc, AggStatePerTransData::sortNullsFirst, SortGroupClause::sortop, AggStatePerTransData::sortOperators, AggStatePerTransData::sortslot, AggStatePerTransData::sortstates, AggStatePerTransData::transfn, AggStatePerTransData::transfn_fcinfo, AggStatePerTransData::transfn_oid, AggStatePerTransData::transtypeByVal, AggStatePerTransData::transtypeLen, TTSOpsMinimalTuple, and AggStatePerTransData::uniqslot.

Referenced by ExecInitAgg().

4033 {
4034  int numGroupingSets = Max(aggstate->maxsets, 1);
4035  Expr *transfnexpr;
4036  int numTransArgs;
4037  Expr *serialfnexpr = NULL;
4038  Expr *deserialfnexpr = NULL;
4039  ListCell *lc;
4040  int numInputs;
4041  int numDirectArgs;
4042  List *sortlist;
4043  int numSortCols;
4044  int numDistinctCols;
4045  int i;
4046 
4047  /* Begin filling in the pertrans data */
4048  pertrans->aggref = aggref;
4049  pertrans->aggshared = false;
4050  pertrans->aggCollation = aggref->inputcollid;
4051  pertrans->transfn_oid = transfn_oid;
4052  pertrans->serialfn_oid = aggserialfn;
4053  pertrans->deserialfn_oid = aggdeserialfn;
4054  pertrans->initValue = initValue;
4055  pertrans->initValueIsNull = initValueIsNull;
4056 
4057  /* Count the "direct" arguments, if any */
4058  numDirectArgs = list_length(aggref->aggdirectargs);
4059 
4060  /* Count the number of aggregated input columns */
4061  pertrans->numInputs = numInputs = list_length(aggref->args);
4062 
4063  pertrans->aggtranstype = aggtranstype;
4064 
4065  /* account for the current transition state */
4066  numTransArgs = pertrans->numTransInputs + 1;
4067 
4068  /*
4069  * Set up infrastructure for calling the transfn. Note that invtrans is
4070  * not needed here.
4071  */
4072  build_aggregate_transfn_expr(inputTypes,
4073  numArguments,
4074  numDirectArgs,
4075  aggref->aggvariadic,
4076  aggtranstype,
4077  aggref->inputcollid,
4078  transfn_oid,
4079  InvalidOid,
4080  &transfnexpr,
4081  NULL);
4082 
4083  fmgr_info(transfn_oid, &pertrans->transfn);
4084  fmgr_info_set_expr((Node *) transfnexpr, &pertrans->transfn);
4085 
4086  pertrans->transfn_fcinfo =
4089  &pertrans->transfn,
4090  numTransArgs,
4091  pertrans->aggCollation,
4092  (void *) aggstate, NULL);
4093 
4094  /* get info about the state value's datatype */
4095  get_typlenbyval(aggtranstype,
4096  &pertrans->transtypeLen,
4097  &pertrans->transtypeByVal);
4098 
4099  if (OidIsValid(aggserialfn))
4100  {
4101  build_aggregate_serialfn_expr(aggserialfn,
4102  &serialfnexpr);
4103  fmgr_info(aggserialfn, &pertrans->serialfn);
4104  fmgr_info_set_expr((Node *) serialfnexpr, &pertrans->serialfn);
4105 
4106  pertrans->serialfn_fcinfo =
4109  &pertrans->serialfn,
4110  1,
4111  InvalidOid,
4112  (void *) aggstate, NULL);
4113  }
4114 
4115  if (OidIsValid(aggdeserialfn))
4116  {
4117  build_aggregate_deserialfn_expr(aggdeserialfn,
4118  &deserialfnexpr);
4119  fmgr_info(aggdeserialfn, &pertrans->deserialfn);
4120  fmgr_info_set_expr((Node *) deserialfnexpr, &pertrans->deserialfn);
4121 
4122  pertrans->deserialfn_fcinfo =
4125  &pertrans->deserialfn,
4126  2,
4127  InvalidOid,
4128  (void *) aggstate, NULL);
4129 
4130  }
4131 
4132  /*
4133  * If we're doing either DISTINCT or ORDER BY for a plain agg, then we
4134  * have a list of SortGroupClause nodes; fish out the data in them and
4135  * stick them into arrays. We ignore ORDER BY for an ordered-set agg,
4136  * however; the agg's transfn and finalfn are responsible for that.
4137  *
4138  * Note that by construction, if there is a DISTINCT clause then the ORDER
4139  * BY clause is a prefix of it (see transformDistinctClause).
4140  */
4141  if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
4142  {
4143  sortlist = NIL;
4144  numSortCols = numDistinctCols = 0;
4145  }
4146  else if (aggref->aggdistinct)
4147  {
4148  sortlist = aggref->aggdistinct;
4149  numSortCols = numDistinctCols = list_length(sortlist);
4150  Assert(numSortCols >= list_length(aggref->aggorder));
4151  }
4152  else
4153  {
4154  sortlist = aggref->aggorder;
4155  numSortCols = list_length(sortlist);
4156  numDistinctCols = 0;
4157  }
4158 
4159  pertrans->numSortCols = numSortCols;
4160  pertrans->numDistinctCols = numDistinctCols;
4161 
4162  /*
4163  * If we have either sorting or filtering to do, create a tupledesc and
4164  * slot corresponding to the aggregated inputs (including sort
4165  * expressions) of the agg.
4166  */
4167  if (numSortCols > 0 || aggref->aggfilter)
4168  {
4169  pertrans->sortdesc = ExecTypeFromTL(aggref->args);
4170  pertrans->sortslot =
4171  ExecInitExtraTupleSlot(estate, pertrans->sortdesc,
4173  }
4174 
4175  if (numSortCols > 0)
4176  {
4177  /*
4178  * We don't implement DISTINCT or ORDER BY aggs in the HASHED case
4179  * (yet)
4180  */
4181  Assert(aggstate->aggstrategy != AGG_HASHED && aggstate->aggstrategy != AGG_MIXED);
4182 
4183  /* ORDER BY aggregates are not supported with partial aggregation */
4184  Assert(!DO_AGGSPLIT_COMBINE(aggstate->aggsplit));
4185 
4186  /* If we have only one input, we need its len/byval info. */
4187  if (numInputs == 1)
4188  {
4189  get_typlenbyval(inputTypes[numDirectArgs],
4190  &pertrans->inputtypeLen,
4191  &pertrans->inputtypeByVal);
4192  }
4193  else if (numDistinctCols > 0)
4194  {
4195  /* we will need an extra slot to store prior values */
4196  pertrans->uniqslot =
4197  ExecInitExtraTupleSlot(estate, pertrans->sortdesc,
4199  }
4200 
4201  /* Extract the sort information for use later */
4202  pertrans->sortColIdx =
4203  (AttrNumber *) palloc(numSortCols * sizeof(AttrNumber));
4204  pertrans->sortOperators =
4205  (Oid *) palloc(numSortCols * sizeof(Oid));
4206  pertrans->sortCollations =
4207  (Oid *) palloc(numSortCols * sizeof(Oid));
4208  pertrans->sortNullsFirst =
4209  (bool *) palloc(numSortCols * sizeof(bool));
4210 
4211  i = 0;
4212  foreach(lc, sortlist)
4213  {
4214  SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc);
4215  TargetEntry *tle = get_sortgroupclause_tle(sortcl, aggref->args);
4216 
4217  /* the parser should have made sure of this */
4218  Assert(OidIsValid(sortcl->sortop));
4219 
4220  pertrans->sortColIdx[i] = tle->resno;
4221  pertrans->sortOperators[i] = sortcl->sortop;
4222  pertrans->sortCollations[i] = exprCollation((Node *) tle->expr);
4223  pertrans->sortNullsFirst[i] = sortcl->nulls_first;
4224  i++;
4225  }
4226  Assert(i == numSortCols);
4227  }
4228 
4229  if (aggref->aggdistinct)
4230  {
4231  Oid *ops;
4232 
4233  Assert(numArguments > 0);
4234  Assert(list_length(aggref->aggdistinct) == numDistinctCols);
4235 
4236  ops = palloc(numDistinctCols * sizeof(Oid));
4237 
4238  i = 0;
4239  foreach(lc, aggref->aggdistinct)
4240  ops[i++] = ((SortGroupClause *) lfirst(lc))->eqop;
4241 
4242  /* lookup / build the necessary comparators */
4243  if (numDistinctCols == 1)
4244  fmgr_info(get_opcode(ops[0]), &pertrans->equalfnOne);
4245  else
4246  pertrans->equalfnMulti =
4247  execTuplesMatchPrepare(pertrans->sortdesc,
4248  numDistinctCols,
4249  pertrans->sortColIdx,
4250  ops,
4251  pertrans->sortCollations,
4252  &aggstate->ss.ps);
4253  pfree(ops);
4254  }
4255 
4256  pertrans->sortstates = (Tuplesortstate **)
4257  palloc0(sizeof(Tuplesortstate *) * numGroupingSets);
4258 }
List * aggdistinct
Definition: primnodes.h:332
#define NIL
Definition: pg_list.h:65
bool aggvariadic
Definition: primnodes.h:335
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:356
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1831
ScanState ss
Definition: execnodes.h:2277
FmgrInfo equalfnOne
Definition: nodeAgg.h:110
#define SizeForFunctionCallInfo(nargs)
Definition: fmgr.h:102
Oid inputcollid
Definition: primnodes.h:326
Definition: nodes.h:536
AggSplit aggsplit
Definition: execnodes.h:2282
List * args
Definition: primnodes.h:330
void build_aggregate_deserialfn_expr(Oid deserialfn_oid, Expr **deserialfnexpr)
Definition: parse_agg.c:2050
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define DO_AGGSPLIT_COMBINE(as)
Definition: nodes.h:799
FunctionCallInfo transfn_fcinfo
Definition: nodeAgg.h:162
TupleDesc sortdesc
Definition: nodeAgg.h:138
FmgrInfo transfn
Definition: nodeAgg.h:81
Aggref * aggref
Definition: nodeAgg.h:44
PlanState ps
Definition: execnodes.h:1378
int maxsets
Definition: execnodes.h:2307
void pfree(void *pointer)
Definition: mcxt.c:1169
AggStrategy aggstrategy
Definition: execnodes.h:2281
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
ExprState * equalfnMulti
Definition: nodeAgg.h:111
Tuplesortstate ** sortstates
Definition: nodeAgg.h:154
static int initValue(long lng_val)
Definition: informix.c:677
List * aggorder
Definition: primnodes.h:331
#define fmgr_info_set_expr(expr, finfo)
Definition: fmgr.h:135
AttrNumber resno
Definition: primnodes.h:1456
List * aggdirectargs
Definition: primnodes.h:329
AttrNumber * sortColIdx
Definition: nodeAgg.h:100
void * palloc0(Size size)
Definition: mcxt.c:1093
struct FunctionCallInfoBaseData * FunctionCallInfo
Definition: fmgr.h:38
FmgrInfo deserialfn
Definition: nodeAgg.h:87
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1256
#define Max(x, y)
Definition: c.h:980
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
FmgrInfo serialfn
Definition: nodeAgg.h:84
ExprState * execTuplesMatchPrepare(TupleDesc desc, int numCols, const AttrNumber *keyColIdx, const Oid *eqOperators, const Oid *collations, PlanState *parent)
Definition: execGrouping.c:59
FunctionCallInfo deserialfn_fcinfo
Definition: nodeAgg.h:167
void build_aggregate_serialfn_expr(Oid serialfn_oid, Expr **serialfnexpr)
Definition: parse_agg.c:2027
Expr * expr
Definition: primnodes.h:1455
void build_aggregate_transfn_expr(Oid *agg_input_types, int agg_num_inputs, int agg_num_direct_inputs, bool agg_variadic, Oid agg_state_type, Oid agg_input_collation, Oid transfn_oid, Oid invtransfn_oid, Expr **transfnexpr, Expr **invtransfnexpr)
Definition: parse_agg.c:1966
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
FunctionCallInfo serialfn_fcinfo
Definition: nodeAgg.h:165
static int list_length(const List *l)
Definition: pg_list.h:149
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2198
Expr * aggfilter
Definition: primnodes.h:333
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:1938
TupleTableSlot * uniqslot
Definition: nodeAgg.h:137
void * palloc(Size size)
Definition: mcxt.c:1062
int i
char aggkind
Definition: primnodes.h:337
TupleTableSlot * sortslot
Definition: nodeAgg.h:136
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
bool * sortNullsFirst
Definition: nodeAgg.h:103

◆ ExecAgg()

static TupleTableSlot* ExecAgg ( PlanState pstate)
static

Definition at line 2140 of file nodeAgg.c.

References AggState::agg_done, agg_fill_hash_table(), AGG_HASHED, AGG_MIXED, AGG_PLAIN, agg_retrieve_direct(), agg_retrieve_hash_table(), AGG_SORTED, AggStatePerPhaseData::aggstrategy, castNode, CHECK_FOR_INTERRUPTS, AggState::phase, AggState::table_filled, and TupIsNull.

Referenced by ExecInitAgg().

2141 {
2142  AggState *node = castNode(AggState, pstate);
2143  TupleTableSlot *result = NULL;
2144 
2146 
2147  if (!node->agg_done)
2148  {
2149  /* Dispatch based on strategy */
2150  switch (node->phase->aggstrategy)
2151  {
2152  case AGG_HASHED:
2153  if (!node->table_filled)
2154  agg_fill_hash_table(node);
2155  /* FALLTHROUGH */
2156  case AGG_MIXED:
2157  result = agg_retrieve_hash_table(node);
2158  break;
2159  case AGG_PLAIN:
2160  case AGG_SORTED:
2161  result = agg_retrieve_direct(node);
2162  break;
2163  }
2164 
2165  if (!TupIsNull(result))
2166  return result;
2167  }
2168 
2169  return NULL;
2170 }
static void agg_fill_hash_table(AggState *aggstate)
Definition: nodeAgg.c:2522
bool agg_done
Definition: execnodes.h:2297
#define castNode(_type_, nodeptr)
Definition: nodes.h:605
bool table_filled
Definition: execnodes.h:2317
static TupleTableSlot * agg_retrieve_hash_table(AggState *aggstate)
Definition: nodeAgg.c:2727
static TupleTableSlot * agg_retrieve_direct(AggState *aggstate)
Definition: nodeAgg.c:2176
#define TupIsNull(slot)
Definition: tuptable.h:292
AggStrategy aggstrategy
Definition: nodeAgg.h:274
AggStatePerPhase phase
Definition: execnodes.h:2283
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120

◆ ExecAggEstimate()

void ExecAggEstimate ( AggState node,
ParallelContext pcxt 
)

Definition at line 4667 of file nodeAgg.c.

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

Referenced by ExecParallelEstimate().

4668 {
4669  Size size;
4670 
4671  /* don't need this if not instrumenting or no workers */
4672  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
4673  return;
4674 
4675  size = mul_size(pcxt->nworkers, sizeof(AggregateInstrumentation));
4676  size = add_size(size, offsetof(SharedAggInfo, sinstrument));
4677  shm_toc_estimate_chunk(&pcxt->estimator, size);
4678  shm_toc_estimate_keys(&pcxt->estimator, 1);
4679 }
Instrumentation * instrument
Definition: execnodes.h:977
ScanState ss
Definition: execnodes.h:2277
shm_toc_estimator estimator
Definition: parallel.h:42
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
PlanState ps
Definition: execnodes.h:1378
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
size_t Size
Definition: c.h:540
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define offsetof(type, field)
Definition: c.h:727

◆ ExecAggInitializeDSM()

void ExecAggInitializeDSM ( AggState node,
ParallelContext pcxt 
)

Definition at line 4688 of file nodeAgg.c.

References PlanState::instrument, SharedAggInfo::num_workers, ParallelContext::nworkers, offsetof, 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().

4689 {
4690  Size size;
4691 
4692  /* don't need this if not instrumenting or no workers */
4693  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
4694  return;
4695 
4696  size = offsetof(SharedAggInfo, sinstrument)
4697  + pcxt->nworkers * sizeof(AggregateInstrumentation);
4698  node->shared_info = shm_toc_allocate(pcxt->toc, size);
4699  /* ensure any unfilled slots will contain zeroes */
4700  memset(node->shared_info, 0, size);
4701  node->shared_info->num_workers = pcxt->nworkers;
4702  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
4703  node->shared_info);
4704 }
Instrumentation * instrument
Definition: execnodes.h:977
ScanState ss
Definition: execnodes.h:2277
int plan_node_id
Definition: plannodes.h:140
PlanState ps
Definition: execnodes.h:1378
struct AggregateInstrumentation AggregateInstrumentation
Plan * plan
Definition: execnodes.h:967
size_t Size
Definition: c.h:540
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
SharedAggInfo * shared_info
Definition: execnodes.h:2349
#define offsetof(type, field)
Definition: c.h:727
shm_toc * toc
Definition: parallel.h:45

◆ ExecAggInitializeWorker()

void ExecAggInitializeWorker ( AggState node,
ParallelWorkerContext pwcxt 
)

Definition at line 4713 of file nodeAgg.c.

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

Referenced by ExecParallelInitializeWorker().

4714 {
4715  node->shared_info =
4716  shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
4717 }
ScanState ss
Definition: execnodes.h:2277
int plan_node_id
Definition: plannodes.h:140
PlanState ps
Definition: execnodes.h:1378
Plan * plan
Definition: execnodes.h:967
SharedAggInfo * shared_info
Definition: execnodes.h:2349
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

◆ ExecAggRetrieveInstrumentation()

void ExecAggRetrieveInstrumentation ( AggState node)

Definition at line 4726 of file nodeAgg.c.

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

Referenced by ExecParallelRetrieveInstrumentation().

4727 {
4728  Size size;
4729  SharedAggInfo *si;
4730 
4731  if (node->shared_info == NULL)
4732  return;
4733 
4734  size = offsetof(SharedAggInfo, sinstrument)
4736  si = palloc(size);
4737  memcpy(si, node->shared_info, size);
4738  node->shared_info = si;
4739 }
struct AggregateInstrumentation AggregateInstrumentation
size_t Size
Definition: c.h:540
void * palloc(Size size)
Definition: mcxt.c:1062
SharedAggInfo * shared_info
Definition: execnodes.h:2349
#define offsetof(type, field)
Definition: c.h:727

◆ ExecEndAgg()

void ExecEndAgg ( AggState node)

Definition at line 4278 of file nodeAgg.c.

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().

4279 {
4281  int transno;
4282  int numGroupingSets = Max(node->maxsets, 1);
4283  int setno;
4284 
4285  /*
4286  * When ending a parallel worker, copy the statistics gathered by the
4287  * worker back into shared memory so that it can be picked up by the main
4288  * process to report in EXPLAIN ANALYZE.
4289  */
4290  if (node->shared_info && IsParallelWorker())
4291  {
4293 
4294  Assert(ParallelWorkerNumber <= node->shared_info->num_workers);
4297  si->hash_disk_used = node->hash_disk_used;
4298  si->hash_mem_peak = node->hash_mem_peak;
4299  }
4300 
4301  /* Make sure we have closed any open tuplesorts */
4302 
4303  if (node->sort_in)
4304  tuplesort_end(node->sort_in);
4305  if (node->sort_out)
4306  tuplesort_end(node->sort_out);
4307 
4309 
4310  if (node->hash_metacxt != NULL)
4311  {
4313  node->hash_metacxt = NULL;
4314  }
4315 
4316  for (transno = 0; transno < node->numtrans; transno++)
4317  {
4318  AggStatePerTrans pertrans = &node->pertrans[transno];
4319 
4320  for (setno = 0; setno < numGroupingSets; setno++)
4321  {
4322  if (pertrans->sortstates[setno])
4323  tuplesort_end(pertrans->sortstates[setno]);
4324  }
4325  }
4326 
4327  /* And ensure any agg shutdown callbacks have been called */
4328  for (setno = 0; setno < numGroupingSets; setno++)
4329  ReScanExprContext(node->aggcontexts[setno]);
4330  if (node->hashcontext)
4332 
4333  /*
4334  * We don't actually free any ExprContexts here (see comment in
4335  * ExecFreeExprContext), just unlinking the output one from the plan node
4336  * suffices.
4337  */
4338  ExecFreeExprContext(&node->ss.ps);
4339 
4340  /* clean up tuple table */
4342 
4343  outerPlan = outerPlanState(node);
4344  ExecEndNode(outerPlan);
4345 }
static void hashagg_reset_spill_state(AggState *aggstate)
Definition: nodeAgg.c:3114
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:556
Tuplesortstate * sort_out
Definition: execnodes.h:2310
ScanState ss
Definition: execnodes.h:2277
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1381
AggregateInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:2253
AggStatePerTrans pertrans
Definition: execnodes.h:2287
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:650
int numtrans
Definition: execnodes.h:2280
PlanState ps
Definition: execnodes.h:1378
int maxsets
Definition: execnodes.h:2307
MemoryContext hash_metacxt
Definition: execnodes.h:2319
Tuplesortstate * sort_in
Definition: execnodes.h:2309
#define outerPlanState(node)
Definition: execnodes.h:1063
Tuplesortstate ** sortstates
Definition: nodeAgg.h:154
int ParallelWorkerNumber
Definition: parallel.c:112
#define IsParallelWorker()
Definition: parallel.h:61
int hash_batches_used
Definition: execnodes.h:2338
#define outerPlan(node)
Definition: plannodes.h:171
ExprContext * hashcontext
Definition: execnodes.h:2288
uint64 hash_disk_used
Definition: execnodes.h:2337
#define Max(x, y)
Definition: c.h:980
ExprContext ** aggcontexts
Definition: execnodes.h:2289
#define Assert(condition)
Definition: c.h:804
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:438
Size hash_mem_peak
Definition: execnodes.h:2334
SharedAggInfo * shared_info
Definition: execnodes.h:2349
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1467

◆ ExecInitAgg()

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

Definition at line 3162 of file nodeAgg.c.

References ACL_EXECUTE, aclcheck_error(), ACLCHECK_OK, AggState::agg_done, AGG_HASHED, AGG_MIXED, AGG_PLAIN, AGG_SORTED, AggState::aggcontexts, AggStatePerAggData::aggdirectargs, Aggref::aggdirectargs, AGGFNOID, Aggref::aggfnoid, Aggref::aggkind, Aggref::agglevelsup, Aggref::aggno, AggStatePerPhaseData::aggnode, AggStatePerHashData::aggnode, AggStatePerTransData::aggref, AggStatePerAggData::aggref, AggState::aggs, AggStatePerTransData::aggshared, Aggref::aggsplit, Agg::aggsplit, AggState::aggsplit, AggStatePerPhaseData::aggstrategy, Agg::aggstrategy, AggState::aggstrategy, Aggref::aggtransno, Aggref::aggtranstype, Aggref::aggtype, 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_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, Agg::grpColIdx, Agg::grpCollations, Agg::grpOperators, AggStatePerPhaseData::gset_lengths, hash_agg_entry_size(), hash_agg_set_limits(), AggState::hashcontext, HeapTupleIsValid, i, initialize_phase(), initValue(), AggStatePerTransData::initValueIsNull, AggState::input_done, Aggref::inputcollid, InvalidOid, InvokeFunctionExecuteHook, IsBinaryCoercible(), lcons_int(), Plan::lefttree, lfirst, list_length(), list_nth_node, makeNode, Max, AggState::maxsets, NIL, AggState::numaggs, AggStatePerHashData::numCols, Agg::numCols, AggStatePerAggData::numFinalArgs, AggState::numphases, AggStatePerPhaseData::numsets, AggState::numtrans, AggStatePerTransData::numTransInputs, OBJECT_AGGREGATE, OBJECT_FUNCTION, ObjectIdGetDatum, OidIsValid, PlanState::outerops, PlanState::outeropsfixed, PlanState::outeropsset, outerPlan, outerPlanState, palloc(), palloc0(), AggState::peragg, AggState::pergroups, AggState::pertrans, pg_proc_aclcheck(), Agg::plan, PlanState::plan, Plan::plan_width, PROCOID, AggState::projected_set, ScanState::ps, PlanState::ps_ExprContext, Plan::qual, PlanState::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::tmpcontext, AggStatePerTransData::transfn, Agg::transitionSpace, AggStatePerAggData::transno, TupleTableSlot::tts_tupleDescriptor, TTSOpsMinimalTuple, and TTSOpsVirtual.

Referenced by ExecInitNode().

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

◆ ExecReScanAgg()

void ExecReScanAgg ( AggState node)

Definition at line 4348 of file nodeAgg.c.

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().

4349 {
4350  ExprContext *econtext = node->ss.ps.ps_ExprContext;
4352  Agg *aggnode = (Agg *) node->ss.ps.plan;
4353  int transno;
4354  int numGroupingSets = Max(node->maxsets, 1);
4355  int setno;
4356 
4357  node->agg_done = false;
4358 
4359  if (node->aggstrategy == AGG_HASHED)
4360  {
4361  /*
4362  * In the hashed case, if we haven't yet built the hash table then we
4363  * can just return; nothing done yet, so nothing to undo. If subnode's
4364  * chgParam is not NULL then it will be re-scanned by ExecProcNode,
4365  * else no reason to re-scan it at all.
4366  */
4367  if (!node->table_filled)
4368  return;
4369 
4370  /*
4371  * If we do have the hash table, and it never spilled, and the subplan
4372  * does not have any parameter changes, and none of our own parameter
4373  * changes affect input expressions of the aggregated functions, then
4374  * we can just rescan the existing hash table; no need to build it
4375  * again.
4376  */
4377  if (outerPlan->chgParam == NULL && !node->hash_ever_spilled &&
4378  !bms_overlap(node->ss.ps.chgParam, aggnode->aggParams))
4379  {
4381  &node->perhash[0].hashiter);
4382  select_current_set(node, 0, true);
4383  return;
4384  }
4385  }
4386 
4387  /* Make sure we have closed any open tuplesorts */
4388  for (transno = 0; transno < node->numtrans; transno++)
4389  {
4390  for (setno = 0; setno < numGroupingSets; setno++)
4391  {
4392  AggStatePerTrans pertrans = &node->pertrans[transno];
4393 
4394  if (pertrans->sortstates[setno])
4395  {
4396  tuplesort_end(pertrans->sortstates[setno]);
4397  pertrans->sortstates[setno] = NULL;
4398  }
4399  }
4400  }
4401 
4402  /*
4403  * We don't need to ReScanExprContext the output tuple context here;
4404  * ExecReScan already did it. But we do need to reset our per-grouping-set
4405  * contexts, which may have transvalues stored in them. (We use rescan
4406  * rather than just reset because transfns may have registered callbacks
4407  * that need to be run now.) For the AGG_HASHED case, see below.
4408  */
4409 
4410  for (setno = 0; setno < numGroupingSets; setno++)
4411  {
4412  ReScanExprContext(node->aggcontexts[setno]);
4413  }
4414 
4415  /* Release first tuple of group, if we have made a copy */
4416  if (node->grp_firstTuple != NULL)
4417  {
4419  node->grp_firstTuple = NULL;
4420  }
4422 
4423  /* Forget current agg values */
4424  MemSet(econtext->ecxt_aggvalues, 0, sizeof(Datum) * node->numaggs);
4425  MemSet(econtext->ecxt_aggnulls, 0, sizeof(bool) * node->numaggs);
4426 
4427  /*
4428  * With AGG_HASHED/MIXED, the hash table is allocated in a sub-context of
4429  * the hashcontext. This used to be an issue, but now, resetting a context
4430  * automatically deletes sub-contexts too.
4431  */
4432  if (node->aggstrategy == AGG_HASHED || node->aggstrategy == AGG_MIXED)
4433  {
4435 
4436  node->hash_ever_spilled = false;
4437  node->hash_spill_mode = false;
4438  node->hash_ngroups_current = 0;
4439 
4441  /* Rebuild an empty hash table */
4442  build_hash_tables(node);
4443  node->table_filled = false;
4444  /* iterator will be reset when the table is filled */
4445 
4446  hashagg_recompile_expressions(node, false, false);
4447  }
4448 
4449  if (node->aggstrategy != AGG_HASHED)
4450  {
4451  /*
4452  * Reset the per-group state (in particular, mark transvalues null)
4453  */
4454  for (setno = 0; setno < numGroupingSets; setno++)
4455  {
4456  MemSet(node->pergroups[setno], 0,
4457  sizeof(AggStatePerGroupData) * node->numaggs);
4458  }
4459 
4460  /* reset to phase 1 */
4461  initialize_phase(node, 1);
4462 
4463  node->input_done = false;
4464  node->projected_set = -1;
4465  }
4466 
4467  if (outerPlan->chgParam == NULL)
4468  ExecReScan(outerPlan);
4469 }
static void hashagg_reset_spill_state(AggState *aggstate)
Definition: nodeAgg.c:3114
static void select_current_set(AggState *aggstate, int setno, bool is_hash)
Definition: nodeAgg.c:457
Datum * ecxt_aggvalues
Definition: execnodes.h:246
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
int numaggs
Definition: execnodes.h:2279
bool agg_done
Definition: execnodes.h:2297
ScanState ss
Definition: execnodes.h:2277
ExprContext * ps_ExprContext
Definition: execnodes.h:1006
void ExecReScan(PlanState *node)
Definition: execAmi.c:78
#define MemSet(start, val, len)
Definition: c.h:1008
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1381
AggStatePerTrans pertrans
Definition: execnodes.h:2287
int projected_set
Definition: execnodes.h:2298
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
HeapTuple grp_firstTuple
Definition: execnodes.h:2315
int numtrans
Definition: execnodes.h:2280
bool hash_spill_mode
Definition: execnodes.h:2327
PlanState ps
Definition: execnodes.h:1378
int maxsets
Definition: execnodes.h:2307
bool table_filled
Definition: execnodes.h:2317
AggStrategy aggstrategy
Definition: execnodes.h:2281
#define outerPlanState(node)
Definition: execnodes.h:1063
Tuplesortstate ** sortstates
Definition: nodeAgg.h:154
Bitmapset * aggParams
Definition: plannodes.h:871
AggStatePerHash perhash
Definition: execnodes.h:2340
Bitmapset * chgParam
Definition: execnodes.h:999
#define outerPlan(node)
Definition: plannodes.h:171
TupleHashIterator hashiter
Definition: nodeAgg.h:304
bool input_done
Definition: execnodes.h:2296
ExprContext * hashcontext
Definition: execnodes.h:2288
bool * ecxt_aggnulls
Definition: execnodes.h:248
uintptr_t Datum
Definition: postgres.h:411
static void initialize_phase(AggState *aggstate, int newphase)
Definition: nodeAgg.c:479
Plan * plan
Definition: execnodes.h:967
#define ResetTupleHashIterator(htable, iter)
Definition: execnodes.h:770
static void hashagg_recompile_expressions(AggState *aggstate, bool minslot, bool nullcheck)
Definition: nodeAgg.c:1723
#define Max(x, y)
Definition: c.h:980
ExprContext ** aggcontexts
Definition: execnodes.h:2289
bool hash_ever_spilled
Definition: execnodes.h:2326
AggStatePerGroup * pergroups
Definition: execnodes.h:2313
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:438
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
uint64 hash_ngroups_current
Definition: execnodes.h:2335
Definition: plannodes.h:860
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1467
TupleHashTable hashtable
Definition: nodeAgg.h:303
static void build_hash_tables(AggState *aggstate)
Definition: nodeAgg.c:1451

◆ fetch_input_tuple()

static TupleTableSlot * fetch_input_tuple ( AggState aggstate)
static

Definition at line 549 of file nodeAgg.c.

References CHECK_FOR_INTERRUPTS, ExecProcNode(), outerPlanState, AggState::sort_in, AggState::sort_out, AggState::sort_slot, TupIsNull, tuplesort_gettupleslot(), and tuplesort_puttupleslot().

Referenced by agg_fill_hash_table(), and agg_retrieve_direct().

550 {
551  TupleTableSlot *slot;
552 
553  if (aggstate->sort_in)
554  {
555  /* make sure we check for interrupts in either path through here */
557  if (!tuplesort_gettupleslot(aggstate->sort_in, true, false,
558  aggstate->sort_slot, NULL))
559  return NULL;
560  slot = aggstate->sort_slot;
561  }
562  else
563  slot = ExecProcNode(outerPlanState(aggstate));
564 
565  if (!TupIsNull(slot) && aggstate->sort_out)
566  tuplesort_puttupleslot(aggstate->sort_out, slot);
567 
568  return slot;
569 }
TupleTableSlot * sort_slot
Definition: execnodes.h:2311
Tuplesortstate * sort_out
Definition: execnodes.h:2310
Tuplesortstate * sort_in
Definition: execnodes.h:2309
#define outerPlanState(node)
Definition: execnodes.h:1063
bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward, bool copy, TupleTableSlot *slot, Datum *abbrev)
Definition: tuplesort.c:2408
#define TupIsNull(slot)
Definition: tuptable.h:292
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:252
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
void tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
Definition: tuplesort.c:1687

◆ finalize_aggregate()

static void finalize_aggregate ( AggState aggstate,
AggStatePerAgg  peragg,
AggStatePerGroup  pergroupstate,
Datum resultVal,
bool resultIsNull 
)
static

Definition at line 1038 of file nodeAgg.c.

References AggStatePerTransData::aggCollation, AggStatePerAggData::aggdirectargs, AggState::curperagg, CurrentMemoryContext, datumCopy(), DatumGetPointer, ExprContext::ecxt_per_tuple_memory, ExecEvalExpr(), AggStatePerAggData::finalfn, AggStatePerAggData::finalfn_oid, FUNC_MAX_ARGS, FunctionCallInvoke, i, InitFunctionCallInfoData, lfirst, LOCAL_FCINFO, MakeExpandedObjectReadOnly, MemoryContextContains(), MemoryContextSwitchTo(), AggStatePerAggData::numFinalArgs, OidIsValid, AggState::pertrans, ScanState::ps, PlanState::ps_ExprContext, AggStatePerAggData::resulttypeByVal, AggStatePerAggData::resulttypeLen, AggState::ss, AggStatePerAggData::transno, AggStatePerTransData::transtypeLen, AggStatePerGroupData::transValue, and AggStatePerGroupData::transValueIsNull.

Referenced by finalize_aggregates().

1042 {
1043  LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
1044  bool anynull = false;
1045  MemoryContext oldContext;
1046  int i;
1047  ListCell *lc;
1048  AggStatePerTrans pertrans = &aggstate->pertrans[peragg->transno];
1049 
1051 
1052  /*
1053  * Evaluate any direct arguments. We do this even if there's no finalfn
1054  * (which is unlikely anyway), so that side-effects happen as expected.
1055  * The direct arguments go into arg positions 1 and up, leaving position 0
1056  * for the transition state value.
1057  */
1058  i = 1;
1059  foreach(lc, peragg->aggdirectargs)
1060  {
1061  ExprState *expr = (ExprState *) lfirst(lc);
1062 
1063  fcinfo->args[i].value = ExecEvalExpr(expr,
1064  aggstate->ss.ps.ps_ExprContext,
1065  &fcinfo->args[i].isnull);
1066  anynull |= fcinfo->args[i].isnull;
1067  i++;
1068  }
1069 
1070  /*
1071  * Apply the agg's finalfn if one is provided, else return transValue.
1072  */
1073  if (OidIsValid(peragg->finalfn_oid))
1074  {
1075  int numFinalArgs = peragg->numFinalArgs;
1076 
1077  /* set up aggstate->curperagg for AggGetAggref() */
1078  aggstate->curperagg = peragg;
1079 
1080  InitFunctionCallInfoData(*fcinfo, &peragg->finalfn,
1081  numFinalArgs,
1082  pertrans->aggCollation,
1083  (void *) aggstate, NULL);
1084 
1085  /* Fill in the transition state value */
1086  fcinfo->args[0].value =
1087  MakeExpandedObjectReadOnly(pergroupstate->transValue,
1088  pergroupstate->transValueIsNull,
1089  pertrans->transtypeLen);
1090  fcinfo->args[0].isnull = pergroupstate->transValueIsNull;
1091  anynull |= pergroupstate->transValueIsNull;
1092 
1093  /* Fill any remaining argument positions with nulls */
1094  for (; i < numFinalArgs; i++)
1095  {
1096  fcinfo->args[i].value = (Datum) 0;
1097  fcinfo->args[i].isnull = true;
1098  anynull = true;
1099  }
1100 
1101  if (fcinfo->flinfo->fn_strict && anynull)
1102  {
1103  /* don't call a strict function with NULL inputs */
1104  *resultVal = (Datum) 0;
1105  *resultIsNull = true;
1106  }
1107  else
1108  {
1109  *resultVal = FunctionCallInvoke(fcinfo);
1110  *resultIsNull = fcinfo->isnull;
1111  }
1112  aggstate->curperagg = NULL;
1113  }
1114  else
1115  {
1116  /* Don't need MakeExpandedObjectReadOnly; datumCopy will copy it */
1117  *resultVal = pergroupstate->transValue;
1118  *resultIsNull = pergroupstate->transValueIsNull;
1119  }
1120 
1121  /*
1122  * If result is pass-by-ref, make sure it is in the right context.
1123  */
1124  if (!peragg->resulttypeByVal && !*resultIsNull &&
1126  DatumGetPointer(*resultVal)))
1127  *resultVal = datumCopy(*resultVal,
1128  peragg->resulttypeByVal,
1129  peragg->resulttypeLen);
1130 
1131  MemoryContextSwitchTo(oldContext);
1132 }
ScanState ss
Definition: execnodes.h:2277
ExprContext * ps_ExprContext
Definition: execnodes.h:1006
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:235
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
AggStatePerTrans pertrans
Definition: execnodes.h:2287
#define OidIsValid(objectId)
Definition: c.h:710
#define FUNC_MAX_ARGS
PlanState ps
Definition: execnodes.h:1378
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:316
#define MakeExpandedObjectReadOnly(d, isnull, typlen)
AggStatePerAgg curperagg
Definition: execnodes.h:2293
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131
uintptr_t Datum
Definition: postgres.h:411
int16 resulttypeLen
Definition: nodeAgg.h:216
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define lfirst(lc)
Definition: pg_list.h:169
bool MemoryContextContains(MemoryContext context, void *pointer)
Definition: mcxt.c:758
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
#define DatumGetPointer(X)
Definition: postgres.h:593
int i
List * aggdirectargs
Definition: nodeAgg.h:210
bool resulttypeByVal
Definition: nodeAgg.h:217
FmgrInfo finalfn
Definition: nodeAgg.h:199

◆ finalize_aggregates()

static void finalize_aggregates ( AggState aggstate,
AggStatePerAgg  peragg,
AggStatePerGroup  pergroup 
)
static

Definition at line 1291 of file nodeAgg.c.

References AGG_HASHED, AGG_MIXED, AggState::aggsplit, AggState::aggstrategy, Assert, DO_AGGSPLIT_SKIPFINAL, ExprContext::ecxt_aggnulls, ExprContext::ecxt_aggvalues, finalize_aggregate(), finalize_partialaggregate(), AggState::numaggs, AggStatePerTransData::numInputs, AggStatePerTransData::numSortCols, AggState::numtrans, AggState::pertrans, process_ordered_aggregate_multi(), process_ordered_aggregate_single(), ScanState::ps, PlanState::ps_ExprContext, AggState::ss, and AggStatePerAggData::transno.

Referenced by agg_retrieve_direct(), and agg_retrieve_hash_table_in_memory().

1294 {
1295  ExprContext *econtext = aggstate->ss.ps.ps_ExprContext;
1296  Datum *aggvalues = econtext->ecxt_aggvalues;
1297  bool *aggnulls = econtext->ecxt_aggnulls;
1298  int aggno;
1299  int transno;
1300 
1301  /*
1302  * If there were any DISTINCT and/or ORDER BY aggregates, sort their
1303  * inputs and run the transition functions.
1304  */
1305  for (transno = 0; transno < aggstate->numtrans; transno++)
1306  {
1307  AggStatePerTrans pertrans = &aggstate->pertrans[transno];
1308  AggStatePerGroup pergroupstate;
1309 
1310  pergroupstate = &pergroup[transno];
1311 
1312  if (pertrans->numSortCols > 0)
1313  {
1314  Assert(aggstate->aggstrategy != AGG_HASHED &&
1315  aggstate->aggstrategy != AGG_MIXED);
1316 
1317  if (pertrans->numInputs == 1)
1319  pertrans,
1320  pergroupstate);
1321  else
1323  pertrans,
1324  pergroupstate);
1325  }
1326  }
1327 
1328  /*
1329  * Run the final functions.
1330  */
1331  for (aggno = 0; aggno < aggstate->numaggs; aggno++)
1332  {
1333  AggStatePerAgg peragg = &peraggs[aggno];
1334  int transno = peragg->transno;
1335  AggStatePerGroup pergroupstate;
1336 
1337  pergroupstate = &pergroup[transno];
1338 
1339  if (DO_AGGSPLIT_SKIPFINAL(aggstate->aggsplit))
1340  finalize_partialaggregate(aggstate, peragg, pergroupstate,
1341  &aggvalues[aggno], &aggnulls[aggno]);
1342  else
1343  finalize_aggregate(aggstate, peragg, pergroupstate,
1344  &aggvalues[aggno], &aggnulls[aggno]);
1345  }
1346 }
Datum * ecxt_aggvalues
Definition: execnodes.h:246
int numaggs
Definition: execnodes.h:2279
static void finalize_partialaggregate(AggState *aggstate, AggStatePerAgg peragg, AggStatePerGroup pergroupstate, Datum *resultVal, bool *resultIsNull)
Definition: nodeAgg.c:1141
ScanState ss
Definition: execnodes.h:2277
ExprContext * ps_ExprContext
Definition: execnodes.h:1006
AggSplit aggsplit
Definition: execnodes.h:2282
static void finalize_aggregate(AggState *aggstate, AggStatePerAgg peragg, AggStatePerGroup pergroupstate, Datum *resultVal, bool *resultIsNull)
Definition: nodeAgg.c:1038
AggStatePerTrans pertrans
Definition: execnodes.h:2287
int numtrans
Definition: execnodes.h:2280
PlanState ps
Definition: execnodes.h:1378
AggStrategy aggstrategy
Definition: execnodes.h:2281
static void process_ordered_aggregate_multi(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroupstate)
Definition: nodeAgg.c:944
bool * ecxt_aggnulls
Definition: execnodes.h:248
uintptr_t Datum
Definition: postgres.h:411
#define Assert(condition)
Definition: c.h:804
#define DO_AGGSPLIT_SKIPFINAL(as)
Definition: nodes.h:800
static void process_ordered_aggregate_single(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroupstate)
Definition: nodeAgg.c:852

◆ finalize_partialaggregate()

static void finalize_partialaggregate ( AggState aggstate,
AggStatePerAgg  peragg,
AggStatePerGroup  pergroupstate,
Datum resultVal,
bool resultIsNull 
)
static

Definition at line 1141 of file nodeAgg.c.

References FunctionCallInfoBaseData::args, CurrentMemoryContext, datumCopy(), DatumGetPointer, ExprContext::ecxt_per_tuple_memory, FmgrInfo::fn_strict, FunctionCallInvoke, FunctionCallInfoBaseData::isnull, NullableDatum::isnull, MakeExpandedObjectReadOnly, MemoryContextContains(), MemoryContextSwitchTo(), OidIsValid, AggState::pertrans, ScanState::ps, PlanState::ps_ExprContext, AggStatePerAggData::resulttypeByVal, AggStatePerAggData::resulttypeLen, AggStatePerTransData::serialfn, AggStatePerTransData::serialfn_fcinfo, AggStatePerTransData::serialfn_oid, AggState::ss, AggStatePerAggData::transno, AggStatePerTransData::transtypeLen, AggStatePerGroupData::transValue, AggStatePerGroupData::transValueIsNull, and NullableDatum::value.

Referenced by finalize_aggregates().

1145 {
1146  AggStatePerTrans pertrans = &aggstate->pertrans[peragg->transno];
1147  MemoryContext oldContext;
1148 
1150 
1151  /*
1152  * serialfn_oid will be set if we must serialize the transvalue before
1153  * returning it
1154  */
1155  if (OidIsValid(pertrans->serialfn_oid))
1156  {
1157  /* Don't call a strict serialization function with NULL input. */
1158  if (pertrans->serialfn.fn_strict && pergroupstate->transValueIsNull)
1159  {
1160  *resultVal = (Datum) 0;
1161  *resultIsNull = true;
1162  }
1163  else
1164  {
1165  FunctionCallInfo fcinfo = pertrans->serialfn_fcinfo;
1166 
1167  fcinfo->args[0].value =
1168  MakeExpandedObjectReadOnly(pergroupstate->transValue,
1169  pergroupstate->transValueIsNull,
1170  pertrans->transtypeLen);
1171  fcinfo->args[0].isnull = pergroupstate->transValueIsNull;
1172  fcinfo->isnull = false;
1173 
1174  *resultVal = FunctionCallInvoke(fcinfo);
1175  *resultIsNull = fcinfo->isnull;
1176  }
1177  }
1178  else
1179  {
1180  /* Don't need MakeExpandedObjectReadOnly; datumCopy will copy it */
1181  *resultVal = pergroupstate->transValue;
1182  *resultIsNull = pergroupstate->transValueIsNull;
1183  }
1184 
1185  /* If result is pass-by-ref, make sure it is in the right context. */
1186  if (!peragg->resulttypeByVal && !*resultIsNull &&
1188  DatumGetPointer(*resultVal)))
1189  *resultVal = datumCopy(*resultVal,
1190  peragg->resulttypeByVal,
1191  peragg->resulttypeLen);
1192 
1193  MemoryContextSwitchTo(oldContext);
1194 }
ScanState ss
Definition: execnodes.h:2277
ExprContext * ps_ExprContext
Definition: execnodes.h:1006
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:235
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
AggStatePerTrans pertrans
Definition: execnodes.h:2287
#define OidIsValid(objectId)
Definition: c.h:710
PlanState ps
Definition: execnodes.h:1378
bool fn_strict
Definition: fmgr.h:61
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
#define MakeExpandedObjectReadOnly(d, isnull, typlen)
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
Datum value
Definition: postgres.h:422
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:131
uintptr_t Datum
Definition: postgres.h:411
int16 resulttypeLen
Definition: nodeAgg.h:216
FmgrInfo serialfn
Definition: nodeAgg.h:84
bool MemoryContextContains(MemoryContext context, void *pointer)
Definition: mcxt.c:758
FunctionCallInfo serialfn_fcinfo
Definition: nodeAgg.h:165
#define DatumGetPointer(X)
Definition: postgres.h:593
bool resulttypeByVal
Definition: nodeAgg.h:217

◆ find_cols()

static void find_cols ( AggState aggstate,
Bitmapset **  aggregated,
Bitmapset **  unaggregated 
)
static

Definition at line 1380 of file nodeAgg.c.

References bms_add_member(), find_cols_walker(), Agg::grpColIdx, i, FindColsContext::is_aggref, Agg::numCols, Agg::plan, PlanState::plan, ScanState::ps, Plan::qual, AggState::ss, and Plan::targetlist.

Referenced by find_hash_columns().

1381 {
1382  Agg *agg = (Agg *) aggstate->ss.ps.plan;
1383  FindColsContext context;
1384 
1385  context.is_aggref = false;
1386  context.aggregated = NULL;
1387  context.unaggregated = NULL;
1388 
1389  /* Examine tlist and quals */
1390  (void) find_cols_walker((Node *) agg->plan.targetlist, &context);
1391  (void) find_cols_walker((Node *) agg->plan.qual, &context);
1392 
1393  /* In some cases, grouping columns will not appear in the tlist */
1394  for (int i = 0; i < agg->numCols; i++)
1395  context.unaggregated = bms_add_member(context.unaggregated,
1396  agg->grpColIdx[i]);
1397 
1398  *aggregated = context.aggregated;
1399  *unaggregated = context.unaggregated;
1400 }
int numCols
Definition: plannodes.h:865
List * qual
Definition: plannodes.h:142
AttrNumber * grpColIdx
Definition: plannodes.h:866
ScanState ss
Definition: execnodes.h:2277
Definition: nodes.h:536
PlanState ps
Definition: execnodes.h:1378
static bool find_cols_walker(Node *node, FindColsContext *context)
Definition: nodeAgg.c:1403
Plan plan
Definition: plannodes.h:862
Plan * plan
Definition: execnodes.h:967
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
List * targetlist
Definition: plannodes.h:141
int i
Definition: plannodes.h:860
bool is_aggref
Definition: nodeAgg.c:363

◆ find_cols_walker()

static bool find_cols_walker ( Node node,
FindColsContext context 
)
static

Definition at line 1403 of file nodeAgg.c.

References FindColsContext::aggregated, Assert, bms_add_member(), expression_tree_walker(), FindColsContext::is_aggref, IsA, OUTER_VAR, FindColsContext::unaggregated, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by find_cols().

1404 {
1405  if (node == NULL)
1406  return false;
1407  if (IsA(node, Var))
1408  {
1409  Var *var = (Var *) node;
1410 
1411  /* setrefs.c should have set the varno to OUTER_VAR */
1412  Assert(var->varno == OUTER_VAR);
1413  Assert(var->varlevelsup == 0);
1414  if (context->is_aggref)
1415  context->aggregated = bms_add_member(context->aggregated,
1416  var->varattno);
1417  else
1418  context->unaggregated = bms_add_member(context->unaggregated,
1419  var->varattno);
1420  return false;
1421  }
1422  if (IsA(node, Aggref))
1423  {
1424  Assert(!context->is_aggref);
1425  context->is_aggref = true;
1426  expression_tree_walker(node, find_cols_walker, (void *) context);
1427  context->is_aggref = false;
1428  return false;
1429  }
1431  (void *) context);
1432 }
int varno
Definition: primnodes.h:189
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Index varlevelsup
Definition: primnodes.h:196
AttrNumber varattno
Definition: primnodes.h:191
Definition: primnodes.h:186
static bool find_cols_walker(Node *node, FindColsContext *context)
Definition: nodeAgg.c:1403
Bitmapset * unaggregated
Definition: nodeAgg.c:365
Bitmapset * aggregated
Definition: nodeAgg.c:364
#define Assert(condition)
Definition: c.h:804
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
bool is_aggref
Definition: nodeAgg.c:363
#define OUTER_VAR
Definition: primnodes.h:176

◆ find_hash_columns()

static void find_hash_columns ( AggState aggstate)
static

Definition at line 1546 of file nodeAgg.c.

References AggStatePerHashData::aggnode, AggState::all_cols_needed, AggState::all_grouped_cols, attnum, bms_add_member(), bms_copy(), bms_del_member(), bms_first_member(), bms_free(), bms_is_member(), bms_num_members(), bms_union(), AggState::colnos_needed, AggStatePerHashData::eqfuncoids, EState::es_tupleTable, ExecAllocTableSlot(), execTuplesHashPrepare(), ExecTypeFromTL(), find_cols(), AggStatePerPhaseData::grouped_cols, Agg::grpColIdx, Agg::grpOperators, AggStatePerHashData::hashfunctions, AggStatePerHashData::hashGrpColIdxHash, AggStatePerHashData::hashGrpColIdxInput, AggStatePerHashData::hashslot, i, lappend(), AggStatePerHashData::largestGrpColIdx, lfirst_int, list_free(), list_nth(), Max, AggState::max_colno_needed, TupleDescData::natts, NIL, AggState::num_hashes, AggStatePerHashData::numCols, AggStatePerHashData::numhashGrpCols, outerPlanState, palloc(), AggState::perhash, AggState::phases, ScanState::ps, AggState::ss, ScanState::ss_ScanTupleSlot, PlanState::state, TupleTableSlot::tts_tupleDescriptor, and TTSOpsMinimalTuple.

Referenced by ExecInitAgg().

1547 {
1548  Bitmapset *base_colnos;
1549  Bitmapset *aggregated_colnos;
1550  TupleDesc scanDesc = aggstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
1551  List *outerTlist = outerPlanState(aggstate)->plan->targetlist;
1552  int numHashes = aggstate->num_hashes;
1553  EState *estate = aggstate->ss.ps.state;
1554  int j;
1555 
1556  /* Find Vars that will be needed in tlist and qual */
1557  find_cols(aggstate, &aggregated_colnos, &base_colnos);
1558  aggstate->colnos_needed = bms_union(base_colnos, aggregated_colnos);
1559  aggstate->max_colno_needed = 0;
1560  aggstate->all_cols_needed = true;
1561 
1562  for (int i = 0; i < scanDesc->natts; i++)
1563  {
1564  int colno = i + 1;
1565 
1566  if (bms_is_member(colno, aggstate->colnos_needed))
1567  aggstate->max_colno_needed = colno;
1568  else
1569  aggstate->all_cols_needed = false;
1570  }
1571 
1572  for (j = 0; j < numHashes; ++j)
1573  {
1574  AggStatePerHash perhash = &aggstate->perhash[j];
1575  Bitmapset *colnos = bms_copy(base_colnos);
1576  AttrNumber *grpColIdx = perhash->aggnode->grpColIdx;
1577  List *hashTlist = NIL;
1578  TupleDesc hashDesc;
1579  int maxCols;
1580  int i;
1581 
1582  perhash->largestGrpColIdx = 0;
1583 
1584  /*
1585  * If we're doing grouping sets, then some Vars might be referenced in
1586  * tlist/qual for the benefit of other grouping sets, but not needed
1587  * when hashing; i.e. prepare_projection_slot will null them out, so
1588  * there'd be no point storing them. Use prepare_projection_slot's
1589  * logic to determine which.
1590  */
1591  if (aggstate->phases[0].grouped_cols)
1592  {
1593  Bitmapset *grouped_cols = aggstate->phases[0].grouped_cols[j];
1594  ListCell *lc;
1595 
1596  foreach(lc, aggstate->all_grouped_cols)
1597  {
1598  int attnum = lfirst_int(lc);
1599 
1600  if (!bms_is_member(attnum, grouped_cols))
1601  colnos = bms_del_member(colnos, attnum);
1602  }
1603  }
1604 
1605  /*
1606  * Compute maximum number of input columns accounting for possible
1607  * duplications in the grpColIdx array, which can happen in some edge
1608  * cases where HashAggregate was generated as part of a semijoin or a
1609  * DISTINCT.
1610  */
1611  maxCols = bms_num_members(colnos) + perhash->numCols;
1612 
1613  perhash->hashGrpColIdxInput =
1614  palloc(maxCols * sizeof(AttrNumber));
1615  perhash->hashGrpColIdxHash =
1616  palloc(perhash->numCols * sizeof(AttrNumber));
1617 
1618  /* Add all the grouping columns to colnos */
1619  for (i = 0; i < perhash->numCols; i++)
1620  colnos = bms_add_member(colnos, grpColIdx[i]);
1621 
1622  /*
1623  * First build mapping for columns directly hashed. These are the
1624  * first, because they'll be accessed when computing hash values and
1625  * comparing tuples for exact matches. We also build simple mapping
1626  * for execGrouping, so it knows where to find the to-be-hashed /
1627  * compared columns in the input.
1628  */
1629  for (i = 0; i < perhash->numCols; i++)
1630  {
1631  perhash->hashGrpColIdxInput[i] = grpColIdx[i];
1632  perhash->hashGrpColIdxHash[i] = i + 1;
1633  perhash->numhashGrpCols++;
1634  /* delete already mapped columns */
1635  bms_del_member(colnos, grpColIdx[i]);
1636  }
1637 
1638  /* and add the remaining columns */
1639  while ((i = bms_first_member(colnos)) >= 0)
1640  {
1641  perhash->hashGrpColIdxInput[perhash->numhashGrpCols] = i;
1642  perhash->numhashGrpCols++;
1643  }
1644 
1645  /* and build a tuple descriptor for the hashtable */
1646  for (i = 0; i < perhash->numhashGrpCols; i++)
1647  {
1648  int varNumber = perhash->hashGrpColIdxInput[i] - 1;
1649 
1650  hashTlist = lappend(hashTlist, list_nth(outerTlist, varNumber));
1651  perhash->largestGrpColIdx =
1652  Max(varNumber + 1, perhash->largestGrpColIdx);
1653  }
1654 
1655  hashDesc = ExecTypeFromTL(hashTlist);
1656 
1657  execTuplesHashPrepare(perhash->numCols,
1658  perhash->aggnode->grpOperators,
1659  &perhash->eqfuncoids,
1660  &perhash->hashfunctions);
1661  perhash->hashslot =
1662  ExecAllocTableSlot(&estate->es_tupleTable, hashDesc,
1664 
1665  list_free(hashTlist);
1666  bms_free(colnos);
1667  }
1668 
1669  bms_free(base_colnos);
1670 }
#define NIL
Definition: pg_list.h:65
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:996
AggStatePerPhase phases
Definition: execnodes.h:2308
AttrNumber * hashGrpColIdxInput
Definition: nodeAgg.h:311
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
AttrNumber * grpColIdx
Definition: plannodes.h:866
Bitmapset * colnos_needed
Definition: execnodes.h:2303
List * all_grouped_cols
Definition: execnodes.h:2302
ScanState ss
Definition: execnodes.h:2277
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1381
bool all_cols_needed
Definition: execnodes.h:2305
EState * state
Definition: execnodes.h:969
void execTuplesHashPrepare(int numCols, const Oid *eqOperators, Oid **eqFuncOids, FmgrInfo **hashFunctions)
Definition: execGrouping.c:96
int max_colno_needed
Definition: execnodes.h:2304
Bitmapset ** grouped_cols
Definition: nodeAgg.h:277
PlanState ps
Definition: execnodes.h:1378
static void find_cols(AggState *aggstate, Bitmapset **aggregated, Bitmapset **unaggregated)
Definition: nodeAgg.c:1380
#define lfirst_int(lc)
Definition: pg_list.h:170
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
#define outerPlanState(node)
Definition: execnodes.h:1063
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
AggStatePerHash perhash
Definition: execnodes.h:2340
TupleTableSlot * ExecAllocTableSlot(List **tupleTable, TupleDesc desc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1171
List * lappend(List *list, void *datum)
Definition: list.c:336
int num_hashes
Definition: execnodes.h:2318
AttrNumber * hashGrpColIdxHash
Definition: nodeAgg.h:312
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
List * es_tupleTable
Definition: execnodes.h:603
int16 attnum
Definition: pg_attribute.h:83
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define Max(x, y)
Definition: c.h:980
FmgrInfo * hashfunctions
Definition: nodeAgg.h:306
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:1938
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
Oid * grpOperators
Definition: plannodes.h:867
void * palloc(Size size)
Definition: mcxt.c:1062
void list_free(List *list)
Definition: list.c:1391
int i
TupleTableSlot * hashslot
Definition: nodeAgg.h:305
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:773
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
int16 AttrNumber
Definition: attnum.h:21
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85

◆ GetAggInitVal()

static Datum GetAggInitVal ( Datum  textInitVal,
Oid  transtype 
)
static

Definition at line 4262 of file nodeAgg.c.

References getTypeInputInfo(), OidInputFunctionCall(), pfree(), and TextDatumGetCString.

Referenced by ExecInitAgg().

4263 {
4264  Oid typinput,
4265  typioparam;
4266  char *strInitVal;
4267  Datum initVal;
4268 
4269  getTypeInputInfo(transtype, &typinput, &typioparam);
4270  strInitVal = TextDatumGetCString(textInitVal);
4271  initVal = OidInputFunctionCall(typinput, strInitVal,
4272  typioparam, -1);
4273  pfree(strInitVal);
4274  return initVal;
4275 }
unsigned int Oid
Definition: postgres_ext.h:31
void pfree(void *pointer)
Definition: mcxt.c:1169
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2821
#define TextDatumGetCString(d)
Definition: builtins.h:87
uintptr_t Datum
Definition: postgres.h:411
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1644

◆ hash_agg_check_limits()

static void hash_agg_check_limits ( AggState aggstate)
static

Definition at line 1838 of file nodeAgg.c.

References ExprContext::ecxt_per_tuple_memory, hash_agg_enter_spill_mode(), AggState::hash_mem_limit, AggState::hash_metacxt, AggState::hash_ngroups_current, AggState::hash_ngroups_limit, AggState::hashcontext, and MemoryContextMemAllocated().

Referenced by initialize_hash_entry().

1839 {
1840  uint64 ngroups = aggstate->hash_ngroups_current;
1841  Size meta_mem = MemoryContextMemAllocated(aggstate->hash_metacxt,
1842  true);
1844  true);
1845 
1846  /*
1847  * Don't spill unless there's at least one group in the hash table so we
1848  * can be sure to make progress even in edge cases.
1849  */
1850  if (aggstate->hash_ngroups_current > 0 &&
1851  (meta_mem + hashkey_mem > aggstate->hash_mem_limit ||
1852  ngroups > aggstate->hash_ngroups_limit))
1853  {
1854  hash_agg_enter_spill_mode(aggstate);
1855  }
1856 }
uint64 hash_ngroups_limit
Definition: execnodes.h:2330
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:235
static void hash_agg_enter_spill_mode(AggState *aggstate)
Definition: nodeAgg.c:1864
MemoryContext hash_metacxt
Definition: execnodes.h:2319
ExprContext * hashcontext
Definition: execnodes.h:2288
Size MemoryContextMemAllocated(MemoryContext context, bool recurse)
Definition: mcxt.c:477
size_t Size
Definition: c.h:540
Size hash_mem_limit
Definition: execnodes.h:2329
uint64 hash_ngroups_current
Definition: execnodes.h:2335

◆ hash_agg_enter_spill_mode()

static void hash_agg_enter_spill_mode ( AggState aggstate)
static

Definition at line 1864 of file nodeAgg.c.

References AggStatePerHashData::aggnode, Assert, AggState::hash_ever_spilled, AggState::hash_spill_mode, AggState::hash_spills, AggState::hash_tapeset, hashagg_recompile_expressions(), hashagg_spill_init(), AggState::hashentrysize, LogicalTapeSetCreate(), AggState::num_hashes, Agg::numGroups, palloc(), AggState::perhash, and AggState::table_filled.

Referenced by hash_agg_check_limits().

1865 {
1866  aggstate->hash_spill_mode = true;
1867  hashagg_recompile_expressions(aggstate, aggstate->table_filled, true);
1868 
1869  if (!aggstate->hash_ever_spilled)
1870  {
1871  Assert(aggstate->hash_tapeset == NULL);
1872  Assert(aggstate->hash_spills == NULL);
1873 
1874  aggstate->hash_ever_spilled = true;
1875 
1876  aggstate->hash_tapeset = LogicalTapeSetCreate(true, NULL, -1);
1877 
1878  aggstate->hash_spills = palloc(sizeof(HashAggSpill) * aggstate->num_hashes);
1879 
1880  for (int setno = 0; setno < aggstate->num_hashes; setno++)
1881  {
1882  AggStatePerHash perhash = &aggstate->perhash[setno];
1883  HashAggSpill *spill = &aggstate->hash_spills[setno];
1884 
1885  hashagg_spill_init(spill, aggstate->hash_tapeset, 0,
1886  perhash->aggnode->numGroups,
1887  aggstate->hashentrysize);
1888  }
1889  }
1890 }
struct HashAggSpill * hash_spills
Definition: execnodes.h:2321
double hashentrysize
Definition: execnodes.h:2333
bool hash_spill_mode
Definition: execnodes.h:2327
bool table_filled
Definition: execnodes.h:2317
struct LogicalTapeSet * hash_tapeset
Definition: execnodes.h:2320
AggStatePerHash perhash
Definition: execnodes.h:2340
static void hashagg_spill_init(HashAggSpill *spill, LogicalTapeSet *lts, int used_bits, double input_groups, double hashentrysize)
Definition: nodeAgg.c:2875
int num_hashes
Definition: execnodes.h:2318
static void hashagg_recompile_expressions(AggState *aggstate, bool minslot, bool nullcheck)
Definition: nodeAgg.c:1723
#define Assert(condition)
Definition: c.h:804
bool hash_ever_spilled
Definition: execnodes.h:2326
long numGroups
Definition: plannodes.h:869
LogicalTapeSet * LogicalTapeSetCreate(bool preallocate, SharedFileSet *fileset, int worker)
Definition: logtape.c:565
void * palloc(Size size)
Definition: mcxt.c:1062

◆ hash_agg_entry_size()

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

Definition at line 1676 of file nodeAgg.c.

References CHUNKHDRSZ, MAXALIGN, and SizeofMinimalTupleHeader.

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

1677 {
1678  Size tupleChunkSize;
1679  Size pergroupChunkSize;
1680  Size transitionChunkSize;
1681  Size tupleSize = (MAXALIGN(SizeofMinimalTupleHeader) +
1682  tupleWidth);
1683  Size pergroupSize = numTrans * sizeof(AggStatePerGroupData);
1684 
1685  tupleChunkSize = CHUNKHDRSZ + tupleSize;
1686 
1687  if (pergroupSize > 0)
1688  pergroupChunkSize = CHUNKHDRSZ + pergroupSize;
1689  else
1690  pergroupChunkSize = 0;
1691 
1692  if (transitionSpace > 0)
1693  transitionChunkSize = CHUNKHDRSZ + transitionSpace;
1694  else
1695  transitionChunkSize = 0;
1696 
1697  return
1698  sizeof(TupleHashEntryData) +
1699  tupleChunkSize +
1700  pergroupChunkSize +
1701  transitionChunkSize;
1702 }
struct TupleHashEntryData TupleHashEntryData
struct AggStatePerGroupData AggStatePerGroupData
#define SizeofMinimalTupleHeader
Definition: htup_details.h:648
size_t Size
Definition: c.h:540
#define MAXALIGN(LEN)
Definition: c.h:757
#define CHUNKHDRSZ
Definition: nodeAgg.c:321

◆ 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 1780 of file nodeAgg.c.

References get_hash_memory_limit(), hash_choose_num_partitions(), HASHAGG_READ_BUFFER_SIZE, HASHAGG_WRITE_BUFFER_SIZE, and HashAggSpill::npartitions.

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

1783 {
1784  int npartitions;
1785  Size partition_mem;
1786  Size hash_mem_limit = get_hash_memory_limit();
1787 
1788  /* if not expected to spill, use all of hash_mem */
1789  if (input_groups * hashentrysize <= hash_mem_limit)
1790  {
1791  if (num_partitions != NULL)
1792  *num_partitions = 0;
1793  *mem_limit = hash_mem_limit;
1794  *ngroups_limit = hash_mem_limit / hashentrysize;
1795  return;
1796  }
1797 
1798  /*
1799  * Calculate expected memory requirements for spilling, which is the size
1800  * of the buffers needed for all the tapes that need to be open at once.
1801  * Then, subtract that from the memory available for holding hash tables.
1802  */
1803  npartitions = hash_choose_num_partitions(input_groups,
1804  hashentrysize,
1805  used_bits,
1806  NULL);
1807  if (num_partitions != NULL)
1808  *num_partitions = npartitions;
1809 
1810  partition_mem =
1812  HASHAGG_WRITE_BUFFER_SIZE * npartitions;
1813 
1814  /*
1815  * Don't set the limit below 3/4 of hash_mem. In that case, we are at the
1816  * minimum number of partitions, so we aren't going to dramatically exceed
1817  * work mem anyway.
1818  */
1819  if (hash_mem_limit > 4 * partition_mem)
1820  *mem_limit = hash_mem_limit - partition_mem;
1821  else
1822  *mem_limit = hash_mem_limit * 0.75;
1823 
1824  if (*mem_limit > hashentrysize)
1825  *ngroups_limit = *mem_limit / hashentrysize;
1826  else
1827  *ngroups_limit = 1;
1828 }
static int hash_choose_num_partitions(double input_groups, double hashentrysize, int used_bits, int *log2_npartittions)
Definition: nodeAgg.c:1973
#define HASHAGG_READ_BUFFER_SIZE
Definition: nodeAgg.c:306
size_t Size
Definition: c.h:540
size_t get_hash_memory_limit(void)
Definition: nodeHash.c:3401
#define HASHAGG_WRITE_BUFFER_SIZE
Definition: nodeAgg.c:307

◆ hash_agg_update_metrics()

static void hash_agg_update_metrics ( AggState aggstate,
bool  from_tape,
int  npartitions 
)
static

Definition at line 1899 of file nodeAgg.c.

References AGG_HASHED, AGG_MIXED, AggState::aggstrategy, ExprContext::ecxt_per_tuple_memory, AggState::hash_disk_used, AggState::hash_mem_peak, AggState::hash_metacxt, AggState::hash_ngroups_current, AggState::hash_tapeset, HASHAGG_READ_BUFFER_SIZE, HASHAGG_WRITE_BUFFER_SIZE, AggState::hashcontext, AggState::hashentrysize, LogicalTapeSetBlocks(), and MemoryContextMemAllocated().

Referenced by agg_refill_hash_table(), and hashagg_finish_initial_spills().

1900 {
1901  Size meta_mem;
1902  Size hashkey_mem;
1903  Size buffer_mem;
1904  Size total_mem;
1905 
1906  if (aggstate->aggstrategy != AGG_MIXED &&
1907  aggstate->aggstrategy != AGG_HASHED)
1908  return;
1909 
1910  /* memory for the hash table itself */
1911  meta_mem = MemoryContextMemAllocated(aggstate->hash_metacxt, true);
1912 
1913  /* memory for the group keys and transition states */
1914  hashkey_mem = MemoryContextMemAllocated(aggstate->hashcontext->ecxt_per_tuple_memory, true);
1915 
1916  /* memory for read/write tape buffers, if spilled */
1917  buffer_mem = npartitions * HASHAGG_WRITE_BUFFER_SIZE;
1918  if (from_tape)
1919  buffer_mem += HASHAGG_READ_BUFFER_SIZE;
1920 
1921  /* update peak mem */
1922  total_mem = meta_mem + hashkey_mem + buffer_mem;
1923  if (total_mem > aggstate->hash_mem_peak)
1924  aggstate->hash_mem_peak = total_mem;
1925 
1926  /* update disk usage */
1927  if (aggstate->hash_tapeset != NULL)
1928  {
1929  uint64 disk_used = LogicalTapeSetBlocks(aggstate->hash_tapeset) * (BLCKSZ / 1024);
1930 
1931  if (aggstate->hash_disk_used < disk_used)
1932  aggstate->hash_disk_used = disk_used;
1933  }
1934 
1935  /* update hashentrysize estimate based on contents */
1936  if (aggstate->hash_ngroups_current > 0)
1937  {
1938  aggstate->hashentrysize =
1939  sizeof(TupleHashEntryData) +
1940  (hashkey_mem / (double) aggstate->hash_ngroups_current);
1941  }
1942 }
double hashentrysize
Definition: execnodes.h:2333
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:235
AggStrategy aggstrategy
Definition: execnodes.h:2281
MemoryContext hash_metacxt
Definition: execnodes.h:2319
struct LogicalTapeSet * hash_tapeset
Definition: execnodes.h:2320
struct TupleHashEntryData TupleHashEntryData
ExprContext * hashcontext
Definition: execnodes.h:2288
#define HASHAGG_READ_BUFFER_SIZE
Definition: nodeAgg.c:306
uint64 hash_disk_used
Definition: execnodes.h:2337
Size MemoryContextMemAllocated(MemoryContext context, bool recurse)
Definition: mcxt.c:477
size_t Size
Definition: c.h:540
#define HASHAGG_WRITE_BUFFER_SIZE
Definition: nodeAgg.c:307
Size hash_mem_peak
Definition: execnodes.h:2334
uint64 hash_ngroups_current
Definition: execnodes.h:2335
long LogicalTapeSetBlocks(LogicalTapeSet *lts)
Definition: logtape.c:1192

◆ hash_choose_num_buckets()

static long hash_choose_num_buckets ( double  hashentrysize,
long  estimated_nbuckets,
Size  memory 
)
static

Definition at line 1948 of file nodeAgg.c.

References Max.

Referenced by build_hash_tables().

1949 {
1950  long max_nbuckets;
1951  long nbuckets = ngroups;
1952 
1953  max_nbuckets = memory / hashentrysize;
1954 
1955  /*
1956  * Underestimating is better than overestimating. Too many buckets crowd
1957  * out space for group keys and transition state values.
1958  */
1959  max_nbuckets >>= 1;
1960 
1961  if (nbuckets > max_nbuckets)
1962  nbuckets = max_nbuckets;
1963 
1964  return Max(nbuckets, 1);
1965 }
#define Max(x, y)
Definition: c.h:980

◆ hash_choose_num_partitions()

static int hash_choose_num_partitions ( double  input_groups,
double  hashentrysize,
int  used_bits,
int *  log2_npartittions 
)