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 peraggs, 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 ngroups, Size memory)
 
static int hash_choose_num_partitions (double input_groups, double hashentrysize, int used_bits, int *log2_npartitions)
 
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 *tapeset, int used_bits, double input_groups, double hashentrysize)
 
static Size hashagg_spill_tuple (AggState *aggstate, HashAggSpill *spill, TupleTableSlot *inputslot, 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.

◆ HASHAGG_HLL_BIT_WIDTH

#define HASHAGG_HLL_BIT_WIDTH   5

Definition at line 315 of file nodeAgg.c.

◆ HASHAGG_MAX_PARTITIONS

#define HASHAGG_MAX_PARTITIONS   1024

Definition at line 298 of file nodeAgg.c.

◆ HASHAGG_MIN_PARTITIONS

#define HASHAGG_MIN_PARTITIONS   4

Definition at line 297 of file nodeAgg.c.

◆ HASHAGG_PARTITION_FACTOR

#define HASHAGG_PARTITION_FACTOR   1.50

Definition at line 296 of file nodeAgg.c.

◆ HASHAGG_READ_BUFFER_SIZE

#define HASHAGG_READ_BUFFER_SIZE   BLCKSZ

Definition at line 306 of file nodeAgg.c.

◆ HASHAGG_WRITE_BUFFER_SIZE

#define HASHAGG_WRITE_BUFFER_SIZE   BLCKSZ

Definition at line 307 of file nodeAgg.c.

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

818 {
819  bool dummynull;
820 
822  aggstate->tmpcontext,
823  &dummynull);
824 }
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:347
ExprState * evaltrans
Definition: nodeAgg.h:291
AggStatePerPhase phase
Definition: execnodes.h:2385
ExprContext * tmpcontext
Definition: execnodes.h:2392

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

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

◆ advance_transition_function()

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

Definition at line 707 of file nodeAgg.c.

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

References FunctionCallInfoBaseData::args, AggState::curaggcontext, AggState::curpertrans, datumCopy(), DatumGetPointer(), ExprContext::ecxt_per_tuple_memory, ExecAggCopyTransValue(), 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().

◆ agg_fill_hash_table()

static void agg_fill_hash_table ( AggState aggstate)
static

Definition at line 2541 of file nodeAgg.c.

2542 {
2543  TupleTableSlot *outerslot;
2544  ExprContext *tmpcontext = aggstate->tmpcontext;
2545 
2546  /*
2547  * Process each outer-plan tuple, and then fetch the next one, until we
2548  * exhaust the outer plan.
2549  */
2550  for (;;)
2551  {
2552  outerslot = fetch_input_tuple(aggstate);
2553  if (TupIsNull(outerslot))
2554  break;
2555 
2556  /* set up for lookup_hash_entries and advance_aggregates */
2557  tmpcontext->ecxt_outertuple = outerslot;
2558 
2559  /* Find or build hashtable entries */
2560  lookup_hash_entries(aggstate);
2561 
2562  /* Advance the aggregates (or combine functions) */
2563  advance_aggregates(aggstate);
2564 
2565  /*
2566  * Reset per-input-tuple context after each tuple, but note that the
2567  * hash lookups do this too
2568  */
2569  ResetExprContext(aggstate->tmpcontext);
2570  }
2571 
2572  /* finalize spills, if any */
2574 
2575  aggstate->table_filled = true;
2576  /* Initialize to walk the first hash table */
2577  select_current_set(aggstate, 0, true);
2579  &aggstate->perhash[0].hashiter);
2580 }
#define ResetTupleHashIterator(htable, iter)
Definition: execnodes.h:838
#define ResetExprContext(econtext)
Definition: executor.h:543
static void hashagg_finish_initial_spills(AggState *aggstate)
Definition: nodeAgg.c:3060
static TupleTableSlot * fetch_input_tuple(AggState *aggstate)
Definition: nodeAgg.c:548
static void lookup_hash_entries(AggState *aggstate)
Definition: nodeAgg.c:2096
static void advance_aggregates(AggState *aggstate)
Definition: nodeAgg.c:817
static void select_current_set(AggState *aggstate, int setno, bool is_hash)
Definition: nodeAgg.c:456
TupleHashTable hashtable
Definition: nodeAgg.h:311
TupleHashIterator hashiter
Definition: nodeAgg.h:312
bool table_filled
Definition: execnodes.h:2419
AggStatePerHash perhash
Definition: execnodes.h:2442
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:253
#define TupIsNull(slot)
Definition: tuptable.h:299

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

◆ agg_refill_hash_table()

static bool agg_refill_hash_table ( AggState aggstate)
static

Definition at line 2595 of file nodeAgg.c.

2596 {
2597  HashAggBatch *batch;
2598  AggStatePerHash perhash;
2599  HashAggSpill spill;
2600  LogicalTapeSet *tapeset = aggstate->hash_tapeset;
2601  bool spill_initialized = false;
2602 
2603  if (aggstate->hash_batches == NIL)
2604  return false;
2605 
2606  /* hash_batches is a stack, with the top item at the end of the list */
2607  batch = llast(aggstate->hash_batches);
2608  aggstate->hash_batches = list_delete_last(aggstate->hash_batches);
2609 
2610  hash_agg_set_limits(aggstate->hashentrysize, batch->input_card,
2611  batch->used_bits, &aggstate->hash_mem_limit,
2612  &aggstate->hash_ngroups_limit, NULL);
2613 
2614  /*
2615  * Each batch only processes one grouping set; set the rest to NULL so
2616  * that advance_aggregates() knows to ignore them. We don't touch
2617  * pergroups for sorted grouping sets here, because they will be needed if
2618  * we rescan later. The expressions for sorted grouping sets will not be
2619  * evaluated after we recompile anyway.
2620  */
2621  MemSet(aggstate->hash_pergroup, 0,
2622  sizeof(AggStatePerGroup) * aggstate->num_hashes);
2623 
2624  /* free memory and reset hash tables */
2625  ReScanExprContext(aggstate->hashcontext);
2626  for (int setno = 0; setno < aggstate->num_hashes; setno++)
2627  ResetTupleHashTable(aggstate->perhash[setno].hashtable);
2628 
2629  aggstate->hash_ngroups_current = 0;
2630 
2631  /*
2632  * In AGG_MIXED mode, hash aggregation happens in phase 1 and the output
2633  * happens in phase 0. So, we switch to phase 1 when processing a batch,
2634  * and back to phase 0 after the batch is done.
2635  */
2636  Assert(aggstate->current_phase == 0);
2637  if (aggstate->phase->aggstrategy == AGG_MIXED)
2638  {
2639  aggstate->current_phase = 1;
2640  aggstate->phase = &aggstate->phases[aggstate->current_phase];
2641  }
2642 
2643  select_current_set(aggstate, batch->setno, true);
2644 
2645  perhash = &aggstate->perhash[aggstate->current_set];
2646 
2647  /*
2648  * Spilled tuples are always read back as MinimalTuples, which may be
2649  * different from the outer plan, so recompile the aggregate expressions.
2650  *
2651  * We still need the NULL check, because we are only processing one
2652  * grouping set at a time and the rest will be NULL.
2653  */
2654  hashagg_recompile_expressions(aggstate, true, true);
2655 
2656  for (;;)
2657  {
2658  TupleTableSlot *spillslot = aggstate->hash_spill_rslot;
2659  TupleTableSlot *hashslot = perhash->hashslot;
2660  TupleHashEntry entry;
2661  MinimalTuple tuple;
2662  uint32 hash;
2663  bool isnew = false;
2664  bool *p_isnew = aggstate->hash_spill_mode ? NULL : &isnew;
2665 
2667 
2668  tuple = hashagg_batch_read(batch, &hash);
2669  if (tuple == NULL)
2670  break;
2671 
2672  ExecStoreMinimalTuple(tuple, spillslot, true);
2673  aggstate->tmpcontext->ecxt_outertuple = spillslot;
2674 
2675  prepare_hash_slot(perhash,
2676  aggstate->tmpcontext->ecxt_outertuple,
2677  hashslot);
2678  entry = LookupTupleHashEntryHash(perhash->hashtable, hashslot,
2679  p_isnew, hash);
2680 
2681  if (entry != NULL)
2682  {
2683  if (isnew)
2684  initialize_hash_entry(aggstate, perhash->hashtable, entry);
2685  aggstate->hash_pergroup[batch->setno] = entry->additional;
2686  advance_aggregates(aggstate);
2687  }
2688  else
2689  {
2690  if (!spill_initialized)
2691  {
2692  /*
2693  * Avoid initializing the spill until we actually need it so
2694  * that we don't assign tapes that will never be used.
2695  */
2696  spill_initialized = true;
2697  hashagg_spill_init(&spill, tapeset, batch->used_bits,
2698  batch->input_card, aggstate->hashentrysize);
2699  }
2700  /* no memory for a new group, spill */
2701  hashagg_spill_tuple(aggstate, &spill, spillslot, hash);
2702 
2703  aggstate->hash_pergroup[batch->setno] = NULL;
2704  }
2705 
2706  /*
2707  * Reset per-input-tuple context after each tuple, but note that the
2708  * hash lookups do this too
2709  */
2710  ResetExprContext(aggstate->tmpcontext);
2711  }
2712 
2713  LogicalTapeClose(batch->input_tape);
2714 
2715  /* change back to phase 0 */
2716  aggstate->current_phase = 0;
2717  aggstate->phase = &aggstate->phases[aggstate->current_phase];
2718 
2719  if (spill_initialized)
2720  {
2721  hashagg_spill_finish(aggstate, &spill, batch->setno);
2722  hash_agg_update_metrics(aggstate, true, spill.npartitions);
2723  }
2724  else
2725  hash_agg_update_metrics(aggstate, true, 0);
2726 
2727  aggstate->hash_spill_mode = false;
2728 
2729  /* prepare to walk the first hash table */
2730  select_current_set(aggstate, batch->setno, true);
2731  ResetTupleHashIterator(aggstate->perhash[batch->setno].hashtable,
2732  &aggstate->perhash[batch->setno].hashiter);
2733 
2734  pfree(batch);
2735 
2736  return true;
2737 }
unsigned int uint32
Definition: c.h:495
#define MemSet(start, val, len)
Definition: c.h:1009
TupleHashEntry LookupTupleHashEntryHash(TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew, uint32 hash)
Definition: execGrouping.c:361
void ResetTupleHashTable(TupleHashTable hashtable)
Definition: execGrouping.c:285
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1447
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:446
Assert(fmt[strlen(fmt) - 1] !='\n')
List * list_delete_last(List *list)
Definition: list.c:956
void LogicalTapeClose(LogicalTape *lt)
Definition: logtape.c:733
void pfree(void *pointer)
Definition: mcxt.c:1456
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static void initialize_hash_entry(AggState *aggstate, TupleHashTable hashtable, TupleHashEntry entry)
Definition: nodeAgg.c:2046
static void hashagg_spill_finish(AggState *aggstate, HashAggSpill *spill, int setno)
Definition: nodeAgg.c:3094
static MinimalTuple hashagg_batch_read(HashAggBatch *batch, uint32 *hashp)
Definition: nodeAgg.c:3011
static void hash_agg_update_metrics(AggState *aggstate, bool from_tape, int npartitions)
Definition: nodeAgg.c:1918
static void hashagg_recompile_expressions(AggState *aggstate, bool minslot, bool nullcheck)
Definition: nodeAgg.c:1742
static void prepare_hash_slot(AggStatePerHash perhash, TupleTableSlot *inputslot, TupleTableSlot *hashslot)
Definition: nodeAgg.c:1205
static Size hashagg_spill_tuple(AggState *aggstate, HashAggSpill *spill, TupleTableSlot *inputslot, uint32 hash)
Definition: nodeAgg.c:2926
static void hashagg_spill_init(HashAggSpill *spill, LogicalTapeSet *tapeset, int used_bits, double input_groups, double hashentrysize)
Definition: nodeAgg.c:2895
void hash_agg_set_limits(double hashentrysize, double input_groups, int used_bits, Size *mem_limit, uint64 *ngroups_limit, int *num_partitions)
Definition: nodeAgg.c:1799
@ AGG_MIXED
Definition: nodes.h:367
#define llast(l)
Definition: pg_list.h:198
#define NIL
Definition: pg_list.h:68
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:715
TupleTableSlot * hashslot
Definition: nodeAgg.h:313
AggStrategy aggstrategy
Definition: nodeAgg.h:282
AggStatePerGroup * hash_pergroup
Definition: execnodes.h:2443
Size hash_mem_limit
Definition: execnodes.h:2431
int current_set
Definition: execnodes.h:2402
struct LogicalTapeSet * hash_tapeset
Definition: execnodes.h:2422
ExprContext * hashcontext
Definition: execnodes.h:2390
uint64 hash_ngroups_limit
Definition: execnodes.h:2432
AggStatePerPhase phases
Definition: execnodes.h:2410
bool hash_spill_mode
Definition: execnodes.h:2429
double hashentrysize
Definition: execnodes.h:2435
uint64 hash_ngroups_current
Definition: execnodes.h:2437
int num_hashes
Definition: execnodes.h:2420
List * hash_batches
Definition: execnodes.h:2427
TupleTableSlot * hash_spill_rslot
Definition: execnodes.h:2425
int current_phase
Definition: execnodes.h:2387
int used_bits
Definition: nodeAgg.c:354
double input_card
Definition: nodeAgg.c:357
LogicalTape * input_tape
Definition: nodeAgg.c:355
int npartitions
Definition: nodeAgg.c:334

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, list_delete_last(), llast, 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().

◆ agg_retrieve_direct()

static TupleTableSlot * agg_retrieve_direct ( AggState aggstate)
static

Definition at line 2195 of file nodeAgg.c.

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

References advance_aggregates(), AggState::agg_done, AGG_MIXED, AGG_PLAIN, agg_retrieve_hash_table(), AggState::aggcontexts, AggStatePerPhaseData::aggnode, AggState::aggstrategy, Agg::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().

◆ agg_retrieve_hash_table()

static TupleTableSlot * agg_retrieve_hash_table ( AggState aggstate)
static

Definition at line 2747 of file nodeAgg.c.

2748 {
2749  TupleTableSlot *result = NULL;
2750 
2751  while (result == NULL)
2752  {
2753  result = agg_retrieve_hash_table_in_memory(aggstate);
2754  if (result == NULL)
2755  {
2756  if (!agg_refill_hash_table(aggstate))
2757  {
2758  aggstate->agg_done = true;
2759  break;
2760  }
2761  }
2762  }
2763 
2764  return result;
2765 }
static bool agg_refill_hash_table(AggState *aggstate)
Definition: nodeAgg.c:2595
static TupleTableSlot * agg_retrieve_hash_table_in_memory(AggState *aggstate)
Definition: nodeAgg.c:2772

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

Referenced by agg_retrieve_direct(), and ExecAgg().

◆ agg_retrieve_hash_table_in_memory()

static TupleTableSlot * agg_retrieve_hash_table_in_memory ( AggState aggstate)
static

Definition at line 2772 of file nodeAgg.c.

2773 {
2774  ExprContext *econtext;
2775  AggStatePerAgg peragg;
2776  AggStatePerGroup pergroup;
2777  TupleHashEntryData *entry;
2778  TupleTableSlot *firstSlot;
2779  TupleTableSlot *result;
2780  AggStatePerHash perhash;
2781 
2782  /*
2783  * get state info from node.
2784  *
2785  * econtext is the per-output-tuple expression context.
2786  */
2787  econtext = aggstate->ss.ps.ps_ExprContext;
2788  peragg = aggstate->peragg;
2789  firstSlot = aggstate->ss.ss_ScanTupleSlot;
2790 
2791  /*
2792  * Note that perhash (and therefore anything accessed through it) can
2793  * change inside the loop, as we change between grouping sets.
2794  */
2795  perhash = &aggstate->perhash[aggstate->current_set];
2796 
2797  /*
2798  * We loop retrieving groups until we find one satisfying
2799  * aggstate->ss.ps.qual
2800  */
2801  for (;;)
2802  {
2803  TupleTableSlot *hashslot = perhash->hashslot;
2804  int i;
2805 
2807 
2808  /*
2809  * Find the next entry in the hash table
2810  */
2811  entry = ScanTupleHashTable(perhash->hashtable, &perhash->hashiter);
2812  if (entry == NULL)
2813  {
2814  int nextset = aggstate->current_set + 1;
2815 
2816  if (nextset < aggstate->num_hashes)
2817  {
2818  /*
2819  * Switch to next grouping set, reinitialize, and restart the
2820  * loop.
2821  */
2822  select_current_set(aggstate, nextset, true);
2823 
2824  perhash = &aggstate->perhash[aggstate->current_set];
2825 
2826  ResetTupleHashIterator(perhash->hashtable, &perhash->hashiter);
2827 
2828  continue;
2829  }
2830  else
2831  {
2832  return NULL;
2833  }
2834  }
2835 
2836  /*
2837  * Clear the per-output-tuple context for each group
2838  *
2839  * We intentionally don't use ReScanExprContext here; if any aggs have
2840  * registered shutdown callbacks, they mustn't be called yet, since we
2841  * might not be done with that agg.
2842  */
2843  ResetExprContext(econtext);
2844 
2845  /*
2846  * Transform representative tuple back into one with the right
2847  * columns.
2848  */
2849  ExecStoreMinimalTuple(entry->firstTuple, hashslot, false);
2850  slot_getallattrs(hashslot);
2851 
2852  ExecClearTuple(firstSlot);
2853  memset(firstSlot->tts_isnull, true,
2854  firstSlot->tts_tupleDescriptor->natts * sizeof(bool));
2855 
2856  for (i = 0; i < perhash->numhashGrpCols; i++)
2857  {
2858  int varNumber = perhash->hashGrpColIdxInput[i] - 1;
2859 
2860  firstSlot->tts_values[varNumber] = hashslot->tts_values[i];
2861  firstSlot->tts_isnull[varNumber] = hashslot->tts_isnull[i];
2862  }
2863  ExecStoreVirtualTuple(firstSlot);
2864 
2865  pergroup = (AggStatePerGroup) entry->additional;
2866 
2867  /*
2868  * Use the representative input tuple for any references to
2869  * non-aggregated input columns in the qual and tlist.
2870  */
2871  econtext->ecxt_outertuple = firstSlot;
2872 
2873  prepare_projection_slot(aggstate,
2874  econtext->ecxt_outertuple,
2875  aggstate->current_set);
2876 
2877  finalize_aggregates(aggstate, peragg, pergroup);
2878 
2879  result = project_aggregates(aggstate);
2880  if (result)
2881  return result;
2882  }
2883 
2884  /* No more groups */
2885  return NULL;
2886 }
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1553
#define ScanTupleHashTable(htable, iter)
Definition: execnodes.h:840
struct AggStatePerGroupData * AggStatePerGroup
Definition: execnodes.h:2373
AttrNumber * hashGrpColIdxInput
Definition: nodeAgg.h:319
MinimalTuple firstTuple
Definition: execnodes.h:793
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:123
bool * tts_isnull
Definition: tuptable.h:127
Datum * tts_values
Definition: tuptable.h:125
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:432
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:361

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

◆ AggCheckCallContext()

int AggCheckCallContext ( FunctionCallInfo  fcinfo,
MemoryContext aggcontext 
)

Definition at line 4522 of file nodeAgg.c.

4523 {
4524  if (fcinfo->context && IsA(fcinfo->context, AggState))
4525  {
4526  if (aggcontext)
4527  {
4528  AggState *aggstate = ((AggState *) fcinfo->context);
4529  ExprContext *cxt = aggstate->curaggcontext;
4530 
4531  *aggcontext = cxt->ecxt_per_tuple_memory;
4532  }
4533  return AGG_CONTEXT_AGGREGATE;
4534  }
4535  if (fcinfo->context && IsA(fcinfo->context, WindowAggState))
4536  {
4537  if (aggcontext)
4538  *aggcontext = ((WindowAggState *) fcinfo->context)->curaggcontext;
4539  return AGG_CONTEXT_WINDOW;
4540  }
4541 
4542  /* this is just to prevent "uninitialized variable" warnings */
4543  if (aggcontext)
4544  *aggcontext = NULL;
4545  return 0;
4546 }
#define AGG_CONTEXT_WINDOW
Definition: fmgr.h:762
#define AGG_CONTEXT_AGGREGATE
Definition: fmgr.h:761
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
fmNodePtr context
Definition: fmgr.h:88

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

Referenced by array_agg_array_combine(), array_agg_array_deserialize(), array_agg_array_finalfn(), array_agg_array_serialize(), array_agg_array_transfn(), array_agg_combine(), array_agg_deserialize(), array_agg_finalfn(), array_agg_serialize(), 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_worker(), json_object_agg_finalfn(), json_object_agg_transfn_worker(), jsonb_agg_finalfn(), jsonb_agg_transfn_worker(), jsonb_object_agg_finalfn(), jsonb_object_agg_transfn_worker(), makeBoolAggState(), makeNumericAggState(), makeStringAggState(), mode_final(), multirange_agg_transfn(), multirange_intersect_agg_transfn(), 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(), string_agg_combine(), string_agg_deserialize(), string_agg_finalfn(), and string_agg_serialize().

◆ AggGetAggref()

Aggref* AggGetAggref ( FunctionCallInfo  fcinfo)

Definition at line 4566 of file nodeAgg.c.

4567 {
4568  if (fcinfo->context && IsA(fcinfo->context, AggState))
4569  {
4570  AggState *aggstate = (AggState *) fcinfo->context;
4571  AggStatePerAgg curperagg;
4572  AggStatePerTrans curpertrans;
4573 
4574  /* check curperagg (valid when in a final function) */
4575  curperagg = aggstate->curperagg;
4576 
4577  if (curperagg)
4578  return curperagg->aggref;
4579 
4580  /* check curpertrans (valid when in a transition function) */
4581  curpertrans = aggstate->curpertrans;
4582 
4583  if (curpertrans)
4584  return curpertrans->aggref;
4585  }
4586  return NULL;
4587 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Aggref * aggref
Definition: nodeAgg.h:44
AggStatePerAgg curperagg
Definition: execnodes.h:2395

References AggStatePerTransData::aggref, FunctionCallInfoBaseData::context, AggState::curperagg, AggState::curpertrans, if(), and IsA.

Referenced by ordered_set_startup().

◆ AggGetTempMemoryContext()

MemoryContext AggGetTempMemoryContext ( FunctionCallInfo  fcinfo)

Definition at line 4600 of file nodeAgg.c.

4601 {
4602  if (fcinfo->context && IsA(fcinfo->context, AggState))
4603  {
4604  AggState *aggstate = (AggState *) fcinfo->context;
4605 
4606  return aggstate->tmpcontext->ecxt_per_tuple_memory;
4607  }
4608  return NULL;
4609 }

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

◆ AggRegisterCallback()

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

Definition at line 4665 of file nodeAgg.c.

4668 {
4669  if (fcinfo->context && IsA(fcinfo->context, AggState))
4670  {
4671  AggState *aggstate = (AggState *) fcinfo->context;
4672  ExprContext *cxt = aggstate->curaggcontext;
4673 
4674  RegisterExprContextCallback(cxt, func, arg);
4675 
4676  return;
4677  }
4678  elog(ERROR, "aggregate function cannot register a callback in this context");
4679 }
#define ERROR
Definition: elog.h:39
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:928
void * arg

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

Referenced by ordered_set_startup().

◆ AggStateIsShared()

bool AggStateIsShared ( FunctionCallInfo  fcinfo)

Definition at line 4626 of file nodeAgg.c.

4627 {
4628  if (fcinfo->context && IsA(fcinfo->context, AggState))
4629  {
4630  AggState *aggstate = (AggState *) fcinfo->context;
4631  AggStatePerAgg curperagg;
4632  AggStatePerTrans curpertrans;
4633 
4634  /* check curperagg (valid when in a final function) */
4635  curperagg = aggstate->curperagg;
4636 
4637  if (curperagg)
4638  return aggstate->pertrans[curperagg->transno].aggshared;
4639 
4640  /* check curpertrans (valid when in a transition function) */
4641  curpertrans = aggstate->curpertrans;
4642 
4643  if (curpertrans)
4644  return curpertrans->aggshared;
4645  }
4646  return true;
4647 }
AggStatePerTrans pertrans
Definition: execnodes.h:2389

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

Referenced by ordered_set_startup().

◆ build_hash_table()

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

Definition at line 1504 of file nodeAgg.c.

1505 {
1506  AggStatePerHash perhash = &aggstate->perhash[setno];
1507  MemoryContext metacxt = aggstate->hash_metacxt;
1508  MemoryContext hashcxt = aggstate->hashcontext->ecxt_per_tuple_memory;
1509  MemoryContext tmpcxt = aggstate->tmpcontext->ecxt_per_tuple_memory;
1510  Size additionalsize;
1511 
1512  Assert(aggstate->aggstrategy == AGG_HASHED ||
1513  aggstate->aggstrategy == AGG_MIXED);
1514 
1515  /*
1516  * Used to make sure initial hash table allocation does not exceed
1517  * hash_mem. Note that the estimate does not include space for
1518  * pass-by-reference transition data values, nor for the representative
1519  * tuple of each group.
1520  */
1521  additionalsize = aggstate->numtrans * sizeof(AggStatePerGroupData);
1522 
1523  perhash->hashtable = BuildTupleHashTableExt(&aggstate->ss.ps,
1524  perhash->hashslot->tts_tupleDescriptor,
1525  perhash->numCols,
1526  perhash->hashGrpColIdxHash,
1527  perhash->eqfuncoids,
1528  perhash->hashfunctions,
1529  perhash->aggnode->grpCollations,
1530  nbuckets,
1531  additionalsize,
1532  metacxt,
1533  hashcxt,
1534  tmpcxt,
1535  DO_AGGSPLIT_SKIPFINAL(aggstate->aggsplit));
1536 }
size_t Size
Definition: c.h:594
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
struct AggStatePerGroupData AggStatePerGroupData
#define DO_AGGSPLIT_SKIPFINAL(as)
Definition: nodes.h:396
@ AGG_HASHED
Definition: nodes.h:366
FmgrInfo * hashfunctions
Definition: nodeAgg.h:314
AttrNumber * hashGrpColIdxHash
Definition: nodeAgg.h:320
MemoryContext hash_metacxt
Definition: execnodes.h:2421
int numtrans
Definition: execnodes.h:2382
AggSplit aggsplit
Definition: execnodes.h:2384

References AGG_HASHED, AGG_MIXED, AggStatePerHashData::aggnode, AggState::aggsplit, AggState::aggstrategy, Assert(), BuildTupleHashTableExt(), DO_AGGSPLIT_SKIPFINAL, ExprContext::ecxt_per_tuple_memory, AggStatePerHashData::eqfuncoids, 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().

◆ build_hash_tables()

static void build_hash_tables ( AggState aggstate)
static

Definition at line 1469 of file nodeAgg.c.

1470 {
1471  int setno;
1472 
1473  for (setno = 0; setno < aggstate->num_hashes; ++setno)
1474  {
1475  AggStatePerHash perhash = &aggstate->perhash[setno];
1476  long nbuckets;
1477  Size memory;
1478 
1479  if (perhash->hashtable != NULL)
1480  {
1481  ResetTupleHashTable(perhash->hashtable);
1482  continue;
1483  }
1484 
1485  Assert(perhash->aggnode->numGroups > 0);
1486 
1487  memory = aggstate->hash_mem_limit / aggstate->num_hashes;
1488 
1489  /* choose reasonable number of buckets per hashtable */
1490  nbuckets = hash_choose_num_buckets(aggstate->hashentrysize,
1491  perhash->aggnode->numGroups,
1492  memory);
1493 
1494  build_hash_table(aggstate, setno, nbuckets);
1495  }
1496 
1497  aggstate->hash_ngroups_current = 0;
1498 }
static long hash_choose_num_buckets(double hashentrysize, long ngroups, Size memory)
Definition: nodeAgg.c:1967
static void build_hash_table(AggState *aggstate, int setno, long nbuckets)
Definition: nodeAgg.c:1504
long numGroups
Definition: plannodes.h:1015

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

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

4046 {
4047  int numGroupingSets = Max(aggstate->maxsets, 1);
4048  Expr *transfnexpr;
4049  int numTransArgs;
4050  Expr *serialfnexpr = NULL;
4051  Expr *deserialfnexpr = NULL;
4052  ListCell *lc;
4053  int numInputs;
4054  int numDirectArgs;
4055  List *sortlist;
4056  int numSortCols;
4057  int numDistinctCols;
4058  int i;
4059 
4060  /* Begin filling in the pertrans data */
4061  pertrans->aggref = aggref;
4062  pertrans->aggshared = false;
4063  pertrans->aggCollation = aggref->inputcollid;
4064  pertrans->transfn_oid = transfn_oid;
4065  pertrans->serialfn_oid = aggserialfn;
4066  pertrans->deserialfn_oid = aggdeserialfn;
4067  pertrans->initValue = initValue;
4068  pertrans->initValueIsNull = initValueIsNull;
4069 
4070  /* Count the "direct" arguments, if any */
4071  numDirectArgs = list_length(aggref->aggdirectargs);
4072 
4073  /* Count the number of aggregated input columns */
4074  pertrans->numInputs = numInputs = list_length(aggref->args);
4075 
4076  pertrans->aggtranstype = aggtranstype;
4077 
4078  /* account for the current transition state */
4079  numTransArgs = pertrans->numTransInputs + 1;
4080 
4081  /*
4082  * Set up infrastructure for calling the transfn. Note that invtransfn is
4083  * not needed here.
4084  */
4085  build_aggregate_transfn_expr(inputTypes,
4086  numArguments,
4087  numDirectArgs,
4088  aggref->aggvariadic,
4089  aggtranstype,
4090  aggref->inputcollid,
4091  transfn_oid,
4092  InvalidOid,
4093  &transfnexpr,
4094  NULL);
4095 
4096  fmgr_info(transfn_oid, &pertrans->transfn);
4097  fmgr_info_set_expr((Node *) transfnexpr, &pertrans->transfn);
4098 
4099  pertrans->transfn_fcinfo =
4102  &pertrans->transfn,
4103  numTransArgs,
4104  pertrans->aggCollation,
4105  (void *) aggstate, NULL);
4106 
4107  /* get info about the state value's datatype */
4108  get_typlenbyval(aggtranstype,
4109  &pertrans->transtypeLen,
4110  &pertrans->transtypeByVal);
4111 
4112  if (OidIsValid(aggserialfn))
4113  {
4114  build_aggregate_serialfn_expr(aggserialfn,
4115  &serialfnexpr);
4116  fmgr_info(aggserialfn, &pertrans->serialfn);
4117  fmgr_info_set_expr((Node *) serialfnexpr, &pertrans->serialfn);
4118 
4119  pertrans->serialfn_fcinfo =
4122  &pertrans->serialfn,
4123  1,
4124  InvalidOid,
4125  (void *) aggstate, NULL);
4126  }
4127 
4128  if (OidIsValid(aggdeserialfn))
4129  {
4130  build_aggregate_deserialfn_expr(aggdeserialfn,
4131  &deserialfnexpr);
4132  fmgr_info(aggdeserialfn, &pertrans->deserialfn);
4133  fmgr_info_set_expr((Node *) deserialfnexpr, &pertrans->deserialfn);
4134 
4135  pertrans->deserialfn_fcinfo =
4138  &pertrans->deserialfn,
4139  2,
4140  InvalidOid,
4141  (void *) aggstate, NULL);
4142  }
4143 
4144  /*
4145  * If we're doing either DISTINCT or ORDER BY for a plain agg, then we
4146  * have a list of SortGroupClause nodes; fish out the data in them and
4147  * stick them into arrays. We ignore ORDER BY for an ordered-set agg,
4148  * however; the agg's transfn and finalfn are responsible for that.
4149  *
4150  * When the planner has set the aggpresorted flag, the input to the
4151  * aggregate is already correctly sorted. For ORDER BY aggregates we can
4152  * simply treat these as normal aggregates. For presorted DISTINCT
4153  * aggregates an extra step must be added to remove duplicate consecutive
4154  * inputs.
4155  *
4156  * Note that by construction, if there is a DISTINCT clause then the ORDER
4157  * BY clause is a prefix of it (see transformDistinctClause).
4158  */
4159  if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
4160  {
4161  sortlist = NIL;
4162  numSortCols = numDistinctCols = 0;
4163  pertrans->aggsortrequired = false;
4164  }
4165  else if (aggref->aggpresorted && aggref->aggdistinct == NIL)
4166  {
4167  sortlist = NIL;
4168  numSortCols = numDistinctCols = 0;
4169  pertrans->aggsortrequired = false;
4170  }
4171  else if (aggref->aggdistinct)
4172  {
4173  sortlist = aggref->aggdistinct;
4174  numSortCols = numDistinctCols = list_length(sortlist);
4175  Assert(numSortCols >= list_length(aggref->aggorder));
4176  pertrans->aggsortrequired = !aggref->aggpresorted;
4177  }
4178  else
4179  {
4180  sortlist = aggref->aggorder;
4181  numSortCols = list_length(sortlist);
4182  numDistinctCols = 0;
4183  pertrans->aggsortrequired = (numSortCols > 0);
4184  }
4185 
4186  pertrans->numSortCols = numSortCols;
4187  pertrans->numDistinctCols = numDistinctCols;
4188 
4189  /*
4190  * If we have either sorting or filtering to do, create a tupledesc and
4191  * slot corresponding to the aggregated inputs (including sort
4192  * expressions) of the agg.
4193  */
4194  if (numSortCols > 0 || aggref->aggfilter)
4195  {
4196  pertrans->sortdesc = ExecTypeFromTL(aggref->args);
4197  pertrans->sortslot =
4198  ExecInitExtraTupleSlot(estate, pertrans->sortdesc,
4200  }
4201 
4202  if (numSortCols > 0)
4203  {
4204  /*
4205  * We don't implement DISTINCT or ORDER BY aggs in the HASHED case
4206  * (yet)
4207  */
4208  Assert(aggstate->aggstrategy != AGG_HASHED && aggstate->aggstrategy != AGG_MIXED);
4209 
4210  /* ORDER BY aggregates are not supported with partial aggregation */
4211  Assert(!DO_AGGSPLIT_COMBINE(aggstate->aggsplit));
4212 
4213  /* If we have only one input, we need its len/byval info. */
4214  if (numInputs == 1)
4215  {
4216  get_typlenbyval(inputTypes[numDirectArgs],
4217  &pertrans->inputtypeLen,
4218  &pertrans->inputtypeByVal);
4219  }
4220  else if (numDistinctCols > 0)
4221  {
4222  /* we will need an extra slot to store prior values */
4223  pertrans->uniqslot =
4224  ExecInitExtraTupleSlot(estate, pertrans->sortdesc,
4226  }
4227 
4228  /* Extract the sort information for use later */
4229  pertrans->sortColIdx =
4230  (AttrNumber *) palloc(numSortCols * sizeof(AttrNumber));
4231  pertrans->sortOperators =
4232  (Oid *) palloc(numSortCols * sizeof(Oid));
4233  pertrans->sortCollations =
4234  (Oid *) palloc(numSortCols * sizeof(Oid));
4235  pertrans->sortNullsFirst =
4236  (bool *) palloc(numSortCols * sizeof(bool));
4237 
4238  i = 0;
4239  foreach(lc, sortlist)
4240  {
4241  SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc);
4242  TargetEntry *tle = get_sortgroupclause_tle(sortcl, aggref->args);
4243 
4244  /* the parser should have made sure of this */
4245  Assert(OidIsValid(sortcl->sortop));
4246 
4247  pertrans->sortColIdx[i] = tle->resno;
4248  pertrans->sortOperators[i] = sortcl->sortop;
4249  pertrans->sortCollations[i] = exprCollation((Node *) tle->expr);
4250  pertrans->sortNullsFirst[i] = sortcl->nulls_first;
4251  i++;
4252  }
4253  Assert(i == numSortCols);
4254  }
4255 
4256  if (aggref->aggdistinct)
4257  {
4258  Oid *ops;
4259 
4260  Assert(numArguments > 0);
4261  Assert(list_length(aggref->aggdistinct) == numDistinctCols);
4262 
4263  ops = palloc(numDistinctCols * sizeof(Oid));
4264 
4265  i = 0;
4266  foreach(lc, aggref->aggdistinct)
4267  ops[i++] = ((SortGroupClause *) lfirst(lc))->eqop;
4268 
4269  /* lookup / build the necessary comparators */
4270  if (numDistinctCols == 1)
4271  fmgr_info(get_opcode(ops[0]), &pertrans->equalfnOne);
4272  else
4273  pertrans->equalfnMulti =
4274  execTuplesMatchPrepare(pertrans->sortdesc,
4275  numDistinctCols,
4276  pertrans->sortColIdx,
4277  ops,
4278  pertrans->sortCollations,
4279  &aggstate->ss.ps);
4280  pfree(ops);
4281  }
4282 
4283  pertrans->sortstates = (Tuplesortstate **)
4284  palloc0(sizeof(Tuplesortstate *) * numGroupingSets);
4285 }
int16 AttrNumber
Definition: attnum.h:21
#define OidIsValid(objectId)
Definition: c.h:764
ExprState * execTuplesMatchPrepare(TupleDesc desc, int numCols, const AttrNumber *keyColIdx, const Oid *eqOperators, const Oid *collations, PlanState *parent)
Definition: execGrouping.c:59
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1832
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:1939
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
#define SizeForFunctionCallInfo(nargs)
Definition: fmgr.h:102
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
struct FunctionCallInfoBaseData * FunctionCallInfo
Definition: fmgr.h:38
#define fmgr_info_set_expr(expr, finfo)
Definition: fmgr.h:135
static int initValue(long lng_val)
Definition: informix.c:677
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2233
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1289
void * palloc0(Size size)
Definition: mcxt.c:1257
void * palloc(Size size)
Definition: mcxt.c:1226
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:786
#define DO_AGGSPLIT_COMBINE(as)
Definition: nodes.h:395
void build_aggregate_deserialfn_expr(Oid deserialfn_oid, Expr **deserialfnexpr)
Definition: parse_agg.c:2098
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:2014
void build_aggregate_serialfn_expr(Oid serialfn_oid, Expr **serialfnexpr)
Definition: parse_agg.c:2075
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
bool * sortNullsFirst
Definition: nodeAgg.h:108
FmgrInfo serialfn
Definition: nodeAgg.h:89
FmgrInfo equalfnOne
Definition: nodeAgg.h:115
TupleDesc sortdesc
Definition: nodeAgg.h:143
TupleTableSlot * sortslot
Definition: nodeAgg.h:141
ExprState * equalfnMulti
Definition: nodeAgg.h:116
Tuplesortstate ** sortstates
Definition: nodeAgg.h:162
TupleTableSlot * uniqslot
Definition: nodeAgg.h:142
FmgrInfo deserialfn
Definition: nodeAgg.h:92
FunctionCallInfo deserialfn_fcinfo
Definition: nodeAgg.h:175
AttrNumber * sortColIdx
Definition: nodeAgg.h:105
FunctionCallInfo serialfn_fcinfo
Definition: nodeAgg.h:173
int maxsets
Definition: execnodes.h:2409
List * aggdistinct
Definition: primnodes.h:452
List * aggdirectargs
Definition: primnodes.h:443
List * args
Definition: primnodes.h:446
Expr * aggfilter
Definition: primnodes.h:455
List * aggorder
Definition: primnodes.h:449
Definition: pg_list.h:54
Definition: nodes.h:129
Expr * expr
Definition: primnodes.h:1895
AttrNumber resno
Definition: primnodes.h:1897
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:367

References AGG_HASHED, AGG_MIXED, AggStatePerTransData::aggCollation, Aggref::aggdirectargs, Aggref::aggdistinct, Aggref::aggfilter, Aggref::aggorder, AggStatePerTransData::aggref, AggStatePerTransData::aggshared, AggStatePerTransData::aggsortrequired, AggState::aggsplit, AggState::aggstrategy, AggStatePerTransData::aggtranstype, 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, 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(), ScanState::ps, 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, AggState::ss, AggStatePerTransData::transfn, AggStatePerTransData::transfn_fcinfo, AggStatePerTransData::transfn_oid, AggStatePerTransData::transtypeByVal, AggStatePerTransData::transtypeLen, TTSOpsMinimalTuple, and AggStatePerTransData::uniqslot.

Referenced by ExecInitAgg().

◆ ExecAgg()

static TupleTableSlot* ExecAgg ( PlanState pstate)
static

Definition at line 2159 of file nodeAgg.c.

2160 {
2161  AggState *node = castNode(AggState, pstate);
2162  TupleTableSlot *result = NULL;
2163 
2165 
2166  if (!node->agg_done)
2167  {
2168  /* Dispatch based on strategy */
2169  switch (node->phase->aggstrategy)
2170  {
2171  case AGG_HASHED:
2172  if (!node->table_filled)
2173  agg_fill_hash_table(node);
2174  /* FALLTHROUGH */
2175  case AGG_MIXED:
2176  result = agg_retrieve_hash_table(node);
2177  break;
2178  case AGG_PLAIN:
2179  case AGG_SORTED:
2180  result = agg_retrieve_direct(node);
2181  break;
2182  }
2183 
2184  if (!TupIsNull(result))
2185  return result;
2186  }
2187 
2188  return NULL;
2189 }
static void agg_fill_hash_table(AggState *aggstate)
Definition: nodeAgg.c:2541
static TupleTableSlot * agg_retrieve_direct(AggState *aggstate)
Definition: nodeAgg.c:2195
@ AGG_SORTED
Definition: nodes.h:365
#define castNode(_type_, nodeptr)
Definition: nodes.h:197

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

◆ ExecAggEstimate()

void ExecAggEstimate ( AggState node,
ParallelContext pcxt 
)

Definition at line 4694 of file nodeAgg.c.

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

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

Referenced by ExecParallelEstimate().

◆ ExecAggInitializeDSM()

void ExecAggInitializeDSM ( AggState node,
ParallelContext pcxt 
)

Definition at line 4715 of file nodeAgg.c.

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

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

Referenced by ExecParallelInitializeDSM().

◆ ExecAggInitializeWorker()

void ExecAggInitializeWorker ( AggState node,
ParallelWorkerContext pwcxt 
)

Definition at line 4740 of file nodeAgg.c.

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

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

Referenced by ExecParallelInitializeWorker().

◆ ExecAggRetrieveInstrumentation()

void ExecAggRetrieveInstrumentation ( AggState node)

Definition at line 4753 of file nodeAgg.c.

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

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

Referenced by ExecParallelRetrieveInstrumentation().

◆ ExecEndAgg()

void ExecEndAgg ( AggState node)

Definition at line 4305 of file nodeAgg.c.

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

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

Referenced by ExecEndNode().

◆ ExecInitAgg()

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

Definition at line 3174 of file nodeAgg.c.

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

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

Referenced by ExecInitNode().

◆ ExecReScanAgg()

void ExecReScanAgg ( AggState node)

Definition at line 4375 of file nodeAgg.c.

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

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

Referenced by ExecReScan().

◆ fetch_input_tuple()

static TupleTableSlot * fetch_input_tuple ( AggState aggstate)
static

Definition at line 548 of file nodeAgg.c.

549 {
550  TupleTableSlot *slot;
551 
552  if (aggstate->sort_in)
553  {
554  /* make sure we check for interrupts in either path through here */
556  if (!tuplesort_gettupleslot(aggstate->sort_in, true, false,
557  aggstate->sort_slot, NULL))
558  return NULL;
559  slot = aggstate->sort_slot;
560  }
561  else
562  slot = ExecProcNode(outerPlanState(aggstate));
563 
564  if (!TupIsNull(slot) && aggstate->sort_out)
565  tuplesort_puttupleslot(aggstate->sort_out, slot);
566 
567  return slot;
568 }
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:268
void tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward, bool copy, TupleTableSlot *slot, Datum *abbrev)

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

◆ finalize_aggregate()

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

Definition at line 1047 of file nodeAgg.c.

1051 {
1052  LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
1053  bool anynull = false;
1054  MemoryContext oldContext;
1055  int i;
1056  ListCell *lc;
1057  AggStatePerTrans pertrans = &aggstate->pertrans[peragg->transno];
1058 
1060 
1061  /*
1062  * Evaluate any direct arguments. We do this even if there's no finalfn
1063  * (which is unlikely anyway), so that side-effects happen as expected.
1064  * The direct arguments go into arg positions 1 and up, leaving position 0
1065  * for the transition state value.
1066  */
1067  i = 1;
1068  foreach(lc, peragg->aggdirectargs)
1069  {
1070  ExprState *expr = (ExprState *) lfirst(lc);
1071 
1072  fcinfo->args[i].value = ExecEvalExpr(expr,
1073  aggstate->ss.ps.ps_ExprContext,
1074  &fcinfo->args[i].isnull);
1075  anynull |= fcinfo->args[i].isnull;
1076  i++;
1077  }
1078 
1079  /*
1080  * Apply the agg's finalfn if one is provided, else return transValue.
1081  */
1082  if (OidIsValid(peragg->finalfn_oid))
1083  {
1084  int numFinalArgs = peragg->numFinalArgs;
1085 
1086  /* set up aggstate->curperagg for AggGetAggref() */
1087  aggstate->curperagg = peragg;
1088 
1089  InitFunctionCallInfoData(*fcinfo, &peragg->finalfn,
1090  numFinalArgs,
1091  pertrans->aggCollation,
1092  (void *) aggstate, NULL);
1093 
1094  /* Fill in the transition state value */
1095  fcinfo->args[0].value =
1096  MakeExpandedObjectReadOnly(pergroupstate->transValue,
1097  pergroupstate->transValueIsNull,
1098  pertrans->transtypeLen);
1099  fcinfo->args[0].isnull = pergroupstate->transValueIsNull;
1100  anynull |= pergroupstate->transValueIsNull;
1101 
1102  /* Fill any remaining argument positions with nulls */
1103  for (; i < numFinalArgs; i++)
1104  {
1105  fcinfo->args[i].value = (Datum) 0;
1106  fcinfo->args[i].isnull = true;
1107  anynull = true;
1108  }
1109 
1110  if (fcinfo->flinfo->fn_strict && anynull)
1111  {
1112  /* don't call a strict function with NULL inputs */
1113  *resultVal = (Datum) 0;
1114  *resultIsNull = true;
1115  }
1116  else
1117  {
1118  Datum result;
1119 
1120  result = FunctionCallInvoke(fcinfo);
1121  *resultIsNull = fcinfo->isnull;
1122  *resultVal = MakeExpandedObjectReadOnly(result,
1123  fcinfo->isnull,
1124  peragg->resulttypeLen);
1125  }
1126  aggstate->curperagg = NULL;
1127  }
1128  else
1129  {
1130  *resultVal =
1131  MakeExpandedObjectReadOnly(pergroupstate->transValue,
1132  pergroupstate->transValueIsNull,
1133  pertrans->transtypeLen);
1134  *resultIsNull = pergroupstate->transValueIsNull;
1135  }
1136 
1137  MemoryContextSwitchTo(oldContext);
1138 }
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:332
#define MakeExpandedObjectReadOnly(d, isnull, typlen)
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110

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

Referenced by finalize_aggregates().

◆ finalize_aggregates()

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

Definition at line 1295 of file nodeAgg.c.

1298 {
1299  ExprContext *econtext = aggstate->ss.ps.ps_ExprContext;
1300  Datum *aggvalues = econtext->ecxt_aggvalues;
1301  bool *aggnulls = econtext->ecxt_aggnulls;
1302  int aggno;
1303 
1304  /*
1305  * If there were any DISTINCT and/or ORDER BY aggregates, sort their
1306  * inputs and run the transition functions.
1307  */
1308  for (int transno = 0; transno < aggstate->numtrans; transno++)
1309  {
1310  AggStatePerTrans pertrans = &aggstate->pertrans[transno];
1311  AggStatePerGroup pergroupstate;
1312 
1313  pergroupstate = &pergroup[transno];
1314 
1315  if (pertrans->aggsortrequired)
1316  {
1317  Assert(aggstate->aggstrategy != AGG_HASHED &&
1318  aggstate->aggstrategy != AGG_MIXED);
1319 
1320  if (pertrans->numInputs == 1)
1322  pertrans,
1323  pergroupstate);
1324  else
1326  pertrans,
1327  pergroupstate);
1328  }
1329  else if (pertrans->numDistinctCols > 0 && pertrans->haslast)
1330  {
1331  pertrans->haslast = false;
1332 
1333  if (pertrans->numDistinctCols == 1)
1334  {
1335  if (!pertrans->inputtypeByVal && !pertrans->lastisnull)
1336  pfree(DatumGetPointer(pertrans->lastdatum));
1337 
1338  pertrans->lastisnull = false;
1339  pertrans->lastdatum = (Datum) 0;
1340  }
1341  else
1342  ExecClearTuple(pertrans->uniqslot);
1343  }
1344  }
1345 
1346  /*
1347  * Run the final functions.
1348  */
1349  for (aggno = 0; aggno < aggstate->numaggs; aggno++)
1350  {
1351  AggStatePerAgg peragg = &peraggs[aggno];
1352  int transno = peragg->transno;
1353  AggStatePerGroup pergroupstate;
1354 
1355  pergroupstate = &pergroup[transno];
1356 
1357  if (DO_AGGSPLIT_SKIPFINAL(aggstate->aggsplit))
1358  finalize_partialaggregate(aggstate, peragg, pergroupstate,
1359  &aggvalues[aggno], &aggnulls[aggno]);
1360  else
1361  finalize_aggregate(aggstate, peragg, pergroupstate,
1362  &aggvalues[aggno], &aggnulls[aggno]);
1363  }
1364 }
static void process_ordered_aggregate_multi(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroupstate)
Definition: nodeAgg.c:950
static void finalize_aggregate(AggState *aggstate, AggStatePerAgg peragg, AggStatePerGroup pergroupstate, Datum *resultVal, bool *resultIsNull)
Definition: nodeAgg.c:1047
static void process_ordered_aggregate_single(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroupstate)
Definition: nodeAgg.c:849
static void finalize_partialaggregate(AggState *aggstate, AggStatePerAgg peragg, AggStatePerGroup pergroupstate, Datum *resultVal, bool *resultIsNull)
Definition: nodeAgg.c:1147

References AGG_HASHED, AGG_MIXED, AggStatePerTransData::aggsortrequired, AggState::aggsplit, AggState::aggstrategy, Assert(), DatumGetPointer(), DO_AGGSPLIT_SKIPFINAL, ExprContext::ecxt_aggnulls, ExprContext::ecxt_aggvalues, ExecClearTuple(), finalize_aggregate(), finalize_partialaggregate(), AggStatePerTransData::haslast, AggStatePerTransData::inputtypeByVal, AggStatePerTransData::lastdatum, AggStatePerTransData::lastisnull, AggState::numaggs, AggStatePerTransData::numDistinctCols, AggStatePerTransData::numInputs, AggState::numtrans, AggState::pertrans, pfree(), process_ordered_aggregate_multi(), process_ordered_aggregate_single(), ScanState::ps, PlanState::ps_ExprContext, AggState::ss, AggStatePerAggData::transno, and AggStatePerTransData::uniqslot.

Referenced by agg_retrieve_direct(), and agg_retrieve_hash_table_in_memory().

◆ finalize_partialaggregate()

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

Definition at line 1147 of file nodeAgg.c.

1151 {
1152  AggStatePerTrans pertrans = &aggstate->pertrans[peragg->transno];
1153  MemoryContext oldContext;
1154 
1156 
1157  /*
1158  * serialfn_oid will be set if we must serialize the transvalue before
1159  * returning it
1160  */
1161  if (OidIsValid(pertrans->serialfn_oid))
1162  {
1163  /* Don't call a strict serialization function with NULL input. */
1164  if (pertrans->serialfn.fn_strict && pergroupstate->transValueIsNull)
1165  {
1166  *resultVal = (Datum) 0;
1167  *resultIsNull = true;
1168  }
1169  else
1170  {
1171  FunctionCallInfo fcinfo = pertrans->serialfn_fcinfo;
1172  Datum result;
1173 
1174  fcinfo->args[0].value =
1175  MakeExpandedObjectReadOnly(pergroupstate->transValue,
1176  pergroupstate->transValueIsNull,
1177  pertrans->transtypeLen);
1178  fcinfo->args[0].isnull = pergroupstate->transValueIsNull;
1179  fcinfo->isnull = false;
1180 
1181  result = FunctionCallInvoke(fcinfo);
1182  *resultIsNull = fcinfo->isnull;
1183  *resultVal = MakeExpandedObjectReadOnly(result,
1184  fcinfo->isnull,
1185  peragg->resulttypeLen);
1186  }
1187  }
1188  else
1189  {
1190  *resultVal =
1191  MakeExpandedObjectReadOnly(pergroupstate->transValue,
1192  pergroupstate->transValueIsNull,
1193  pertrans->transtypeLen);
1194  *resultIsNull = pergroupstate->transValueIsNull;
1195  }
1196 
1197  MemoryContextSwitchTo(oldContext);
1198 }

References FunctionCallInfoBaseData::args, ExprContext::ecxt_per_tuple_memory, FmgrInfo::fn_strict, FunctionCallInvoke, FunctionCallInfoBaseData::isnull, NullableDatum::isnull, MakeExpandedObjectReadOnly, MemoryContextSwitchTo(), OidIsValid, AggState::pertrans, ScanState::ps, PlanState::ps_ExprContext, 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().

◆ find_cols()

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

Definition at line 1398 of file nodeAgg.c.

1399 {
1400  Agg *agg = (Agg *) aggstate->ss.ps.plan;
1401  FindColsContext context;
1402 
1403  context.is_aggref = false;
1404  context.aggregated = NULL;
1405  context.unaggregated = NULL;
1406 
1407  /* Examine tlist and quals */
1408  (void) find_cols_walker((Node *) agg->plan.targetlist, &context);
1409  (void) find_cols_walker((Node *) agg->plan.qual, &context);
1410 
1411  /* In some cases, grouping columns will not appear in the tlist */
1412  for (int i = 0; i < agg->numCols; i++)
1413  context.unaggregated = bms_add_member(context.unaggregated,
1414  agg->grpColIdx[i]);
1415 
1416  *aggregated = context.aggregated;
1417  *unaggregated = context.unaggregated;
1418 }
static bool find_cols_walker(Node *node, FindColsContext *context)
Definition: nodeAgg.c:1421
bool is_aggref
Definition: nodeAgg.c:363
List * targetlist
Definition: plannodes.h:153

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

Referenced by find_hash_columns().

◆ find_cols_walker()

static bool find_cols_walker ( Node node,
FindColsContext context 
)
static

Definition at line 1421 of file nodeAgg.c.

1422 {
1423  if (node == NULL)
1424  return false;
1425  if (IsA(node, Var))
1426  {
1427  Var *var = (Var *) node;
1428 
1429  /* setrefs.c should have set the varno to OUTER_VAR */
1430  Assert(var->varno == OUTER_VAR);
1431  Assert(var->varlevelsup == 0);
1432  if (context->is_aggref)
1433  context->aggregated = bms_add_member(context->aggregated,
1434  var->varattno);
1435  else
1436  context->unaggregated = bms_add_member(context->unaggregated,
1437  var->varattno);
1438  return false;
1439  }
1440  if (IsA(node, Aggref))
1441  {
1442  Assert(!context->is_aggref);
1443  context->is_aggref = true;
1444  expression_tree_walker(node, find_cols_walker, (void *) context);
1445  context->is_aggref = false;
1446  return false;
1447  }
1449  (void *) context);
1450 }
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
#define OUTER_VAR
Definition: primnodes.h:215
Bitmapset * aggregated
Definition: nodeAgg.c:364
Bitmapset * unaggregated
Definition: nodeAgg.c:365
Definition: primnodes.h:226
AttrNumber varattno
Definition: primnodes.h:238
int varno
Definition: primnodes.h:233
Index varlevelsup
Definition: primnodes.h:258

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

◆ find_hash_columns()

static void find_hash_columns ( AggState aggstate)
static

Definition at line 1564 of file nodeAgg.c.

1565 {
1566  Bitmapset *base_colnos;
1567  Bitmapset *aggregated_colnos;
1568  TupleDesc scanDesc = aggstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
1569  List *outerTlist = outerPlanState(aggstate)->plan->targetlist;
1570  int numHashes = aggstate->num_hashes;
1571  EState *estate = aggstate->ss.ps.state;
1572  int j;
1573 
1574  /* Find Vars that will be needed in tlist and qual */
1575  find_cols(aggstate, &aggregated_colnos, &base_colnos);
1576  aggstate->colnos_needed = bms_union(base_colnos, aggregated_colnos);
1577  aggstate->max_colno_needed = 0;
1578  aggstate->all_cols_needed = true;
1579 
1580  for (int i = 0; i < scanDesc->natts; i++)
1581  {
1582  int colno = i + 1;
1583 
1584  if (bms_is_member(colno, aggstate->colnos_needed))
1585  aggstate->max_colno_needed = colno;
1586  else
1587  aggstate->all_cols_needed = false;
1588  }
1589 
1590  for (j = 0; j < numHashes; ++j)
1591  {
1592  AggStatePerHash perhash = &aggstate->perhash[j];
1593  Bitmapset *colnos = bms_copy(base_colnos);
1594  AttrNumber *grpColIdx = perhash->aggnode->grpColIdx;
1595  List *hashTlist = NIL;
1596  TupleDesc hashDesc;
1597  int maxCols;
1598  int i;
1599 
1600  perhash->largestGrpColIdx = 0;
1601 
1602  /*
1603  * If we're doing grouping sets, then some Vars might be referenced in
1604  * tlist/qual for the benefit of other grouping sets, but not needed
1605  * when hashing; i.e. prepare_projection_slot will null them out, so
1606  * there'd be no point storing them. Use prepare_projection_slot's
1607  * logic to determine which.
1608  */
1609  if (aggstate->phases[0].grouped_cols)
1610  {
1611  Bitmapset *grouped_cols = aggstate->phases[0].grouped_cols[j];
1612  ListCell *lc;
1613 
1614  foreach(lc, aggstate->all_grouped_cols)
1615  {
1616  int attnum = lfirst_int(lc);
1617 
1618  if (!bms_is_member(attnum, grouped_cols))
1619  colnos = bms_del_member(colnos, attnum);
1620  }
1621  }
1622 
1623  /*
1624  * Compute maximum number of input columns accounting for possible
1625  * duplications in the grpColIdx array, which can happen in some edge
1626  * cases where HashAggregate was generated as part of a semijoin or a
1627  * DISTINCT.
1628  */
1629  maxCols = bms_num_members(colnos) + perhash->numCols;
1630 
1631  perhash->hashGrpColIdxInput =
1632  palloc(maxCols * sizeof(AttrNumber));
1633  perhash->hashGrpColIdxHash =
1634  palloc(perhash->numCols * sizeof(AttrNumber));
1635 
1636  /* Add all the grouping columns to colnos */
1637  for (i = 0; i < perhash->numCols; i++)
1638  colnos = bms_add_member(colnos, grpColIdx[i]);
1639 
1640  /*
1641  * First build mapping for columns directly hashed. These are the
1642  * first, because they'll be accessed when computing hash values and
1643  * comparing tuples for exact matches. We also build simple mapping
1644  * for execGrouping, so it knows where to find the to-be-hashed /
1645  * compared columns in the input.
1646  */
1647  for (i = 0; i < perhash->numCols; i++)
1648  {
1649  perhash->hashGrpColIdxInput[i] = grpColIdx[i];
1650  perhash->hashGrpColIdxHash[i] = i + 1;
1651  perhash->numhashGrpCols++;
1652  /* delete already mapped columns */
1653  colnos = bms_del_member(colnos, grpColIdx[i]);
1654  }
1655 
1656  /* and add the remaining columns */
1657  i = -1;
1658  while ((i = bms_next_member(colnos, i)) >= 0)
1659  {
1660  perhash->hashGrpColIdxInput[perhash->numhashGrpCols] = i;
1661  perhash->numhashGrpCols++;
1662  }
1663 
1664  /* and build a tuple descriptor for the hashtable */
1665  for (i = 0; i < perhash->numhashGrpCols; i++)
1666  {
1667  int varNumber = perhash->hashGrpColIdxInput[i] - 1;
1668 
1669  hashTlist = lappend(hashTlist, list_nth(outerTlist, varNumber));
1670  perhash->largestGrpColIdx =
1671  Max(varNumber + 1, perhash->largestGrpColIdx);
1672  }
1673 
1674  hashDesc = ExecTypeFromTL(hashTlist);
1675 
1676  execTuplesHashPrepare(perhash->numCols,
1677  perhash->aggnode->grpOperators,
1678  &perhash->eqfuncoids,
1679  &perhash->hashfunctions);
1680  perhash->hashslot =
1681  ExecAllocTableSlot(&estate->es_tupleTable, hashDesc,
1683 
1684  list_free(hashTlist);
1685  bms_free(colnos);
1686  }
1687 
1688  bms_free(base_colnos);
1689 }
void bms_free(Bitmapset *a)
Definition: bitmapset.c:194
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:685
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:460
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:211
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:793
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:80
void execTuplesHashPrepare(int numCols, const Oid *eqOperators, Oid **eqFuncOids, FmgrInfo **hashFunctions)
Definition: execGrouping.c:96
TupleTableSlot * ExecAllocTableSlot(List **tupleTable, TupleDesc desc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1172
List * lappend(List *list, void *datum)
Definition: list.c:338
void list_free(List *list)
Definition: list.c:1545
static void find_cols(AggState *aggstate, Bitmapset **aggregated, Bitmapset **unaggregated)
Definition: nodeAgg.c:1398
int16 attnum
Definition: pg_attribute.h:74
#define lfirst_int(lc)
Definition: pg_list.h:173
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
int max_colno_needed
Definition: execnodes.h:2406
Bitmapset * colnos_needed
Definition: execnodes.h:2405
bool all_cols_needed
Definition: execnodes.h:2407
List * es_tupleTable
Definition: execnodes.h:661

References AggStatePerHashData::aggnode, AggState::all_cols_needed, AggState::all_grouped_cols, attnum, bms_add_member(), bms_copy(), bms_del_member(), bms_free(), bms_is_member(), bms_next_member(), bms_num_members(), bms_union(), AggState::colnos_needed, AggStatePerHashData::eqfuncoids, EState::es_tupleTable, ExecAllocTableSlot(), execTuplesHashPrepare(), ExecTypeFromTL(), find_cols(), AggStatePerPhaseData::grouped_cols, AggStatePerHashData::hashfunctions, AggStatePerHashData::hashGrpColIdxHash, AggStatePerHashData::hashGrpColIdxInput, AggStatePerHashData::hashslot, i, j, 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().

◆ GetAggInitVal()

static Datum GetAggInitVal ( Datum  textInitVal,
Oid  transtype 
)
static

Definition at line 4289 of file nodeAgg.c.

4290 {
4291  Oid typinput,
4292  typioparam;
4293  char *strInitVal;
4294  Datum initVal;
4295 
4296  getTypeInputInfo(transtype, &typinput, &typioparam);
4297  strInitVal = TextDatumGetCString(textInitVal);
4298  initVal = OidInputFunctionCall(typinput, strInitVal,
4299  typioparam, -1);
4300  pfree(strInitVal);
4301  return initVal;
4302 }
#define TextDatumGetCString(d)
Definition: builtins.h:95
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1737
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2856

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

Referenced by ExecInitAgg().

◆ hash_agg_check_limits()

static void hash_agg_check_limits ( AggState aggstate)
static

Definition at line 1857 of file nodeAgg.c.

1858 {
1859  uint64 ngroups = aggstate->hash_ngroups_current;
1860  Size meta_mem = MemoryContextMemAllocated(aggstate->hash_metacxt,
1861  true);
1863  true);
1864 
1865  /*
1866  * Don't spill unless there's at least one group in the hash table so we
1867  * can be sure to make progress even in edge cases.
1868  */
1869  if (aggstate->hash_ngroups_current > 0 &&
1870  (meta_mem + hashkey_mem > aggstate->hash_mem_limit ||
1871  ngroups > aggstate->hash_ngroups_limit))
1872  {
1873  hash_agg_enter_spill_mode(aggstate);
1874  }
1875 }
Size MemoryContextMemAllocated(MemoryContext context, bool recurse)
Definition: mcxt.c:671
static void hash_agg_enter_spill_mode(AggState *aggstate)
Definition: nodeAgg.c:1883

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

◆ hash_agg_enter_spill_mode()

static void hash_agg_enter_spill_mode ( AggState aggstate)
static

Definition at line 1883 of file nodeAgg.c.

1884 {
1885  aggstate->hash_spill_mode = true;
1886  hashagg_recompile_expressions(aggstate, aggstate->table_filled, true);
1887 
1888  if (!aggstate->hash_ever_spilled)
1889  {
1890  Assert(aggstate->hash_tapeset == NULL);
1891  Assert(aggstate->hash_spills == NULL);
1892 
1893  aggstate->hash_ever_spilled = true;
1894 
1895  aggstate->hash_tapeset = LogicalTapeSetCreate(true, NULL, -1);
1896 
1897  aggstate->hash_spills = palloc(sizeof(HashAggSpill) * aggstate->num_hashes);
1898 
1899  for (int setno = 0; setno < aggstate->num_hashes; setno++)
1900  {
1901  AggStatePerHash perhash = &aggstate->perhash[setno];
1902  HashAggSpill *spill = &aggstate->hash_spills[setno];
1903 
1904  hashagg_spill_init(spill, aggstate->hash_tapeset, 0,
1905  perhash->aggnode->numGroups,
1906  aggstate->hashentrysize);
1907  }
1908  }
1909 }
LogicalTapeSet * LogicalTapeSetCreate(bool preallocate, SharedFileSet *fileset, int worker)
Definition: logtape.c:556
struct HashAggSpill * hash_spills
Definition: execnodes.h:2423

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

◆ hash_agg_entry_size()

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

Definition at line 1695 of file nodeAgg.c.

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

References CHUNKHDRSZ, MAXALIGN, and SizeofMinimalTupleHeader.

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

◆ hash_agg_set_limits()

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

Definition at line 1799 of file nodeAgg.c.

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

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

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

◆ hash_agg_update_metrics()

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

Definition at line 1918 of file nodeAgg.c.

1919 {
1920  Size meta_mem;
1921  Size hashkey_mem;
1922  Size buffer_mem;
1923  Size total_mem;
1924 
1925  if (aggstate->aggstrategy != AGG_MIXED &&
1926  aggstate->aggstrategy != AGG_HASHED)
1927  return;
1928 
1929  /* memory for the hash table itself */
1930  meta_mem = MemoryContextMemAllocated(aggstate->hash_metacxt, true);
1931 
1932  /* memory for the group keys and transition states */
1933  hashkey_mem = MemoryContextMemAllocated(aggstate->hashcontext->ecxt_per_tuple_memory, true);
1934 
1935  /* memory for read/write tape buffers, if spilled */
1936  buffer_mem = npartitions * HASHAGG_WRITE_BUFFER_SIZE;
1937  if (from_tape)
1938  buffer_mem += HASHAGG_READ_BUFFER_SIZE;
1939 
1940  /* update peak mem */
1941  total_mem = meta_mem + hashkey_mem + buffer_mem;
1942  if (total_mem > aggstate->hash_mem_peak)
1943  aggstate->hash_mem_peak = total_mem;
1944 
1945  /* update disk usage */
1946  if (aggstate->hash_tapeset != NULL)
1947  {
1948  uint64 disk_used = LogicalTapeSetBlocks(aggstate->hash_tapeset) * (BLCKSZ / 1024);
1949 
1950  if (aggstate->hash_disk_used < disk_used)
1951  aggstate->hash_disk_used = disk_used;
1952  }
1953 
1954  /* update hashentrysize estimate based on contents */
1955  if (aggstate->hash_ngroups_current > 0)
1956  {
1957  aggstate->hashentrysize =
1958  sizeof(TupleHashEntryData) +
1959  (hashkey_mem / (double) aggstate->hash_ngroups_current);
1960  }
1961 }
long LogicalTapeSetBlocks(LogicalTapeSet *lts)
Definition: logtape.c:1183

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

◆ hash_choose_num_buckets()

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

Definition at line 1967 of file nodeAgg.c.

1968 {
1969  long max_nbuckets;
1970  long nbuckets = ngroups;
1971 
1972  max_nbuckets = memory / hashentrysize;
1973 
1974  /*
1975  * Underestimating is better than overestimating. Too many buckets crowd
1976  * out space for group keys and transition state values.
1977  */
1978  max_nbuckets >>= 1;
1979 
1980  if (nbuckets > max_nbuckets)
1981  nbuckets = max_nbuckets;
1982 
1983  return Max(nbuckets, 1);
1984 }

References Max.

Referenced by build_hash_tables().

◆ hash_choose_num_partitions()

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

Definition at line 1992 of file nodeAgg.c.

1994 {
1995  Size hash_mem_limit = get_hash_memory_limit();
1996  double partition_limit;
1997  double mem_wanted;
1998  double dpartitions;
1999  int npartitions;
2000  int partition_bits;
2001 
2002  /*
2003  * Avoid creating so many partitions that the memory requirements of the
2004  * open partition files are greater than 1/4 of hash_mem.
2005  */
2006  partition_limit =
2007  (hash_mem_limit * 0.25 - HASHAGG_READ_BUFFER_SIZE) /
2009 
2010  mem_wanted = HASHAGG_PARTITION_FACTOR * input_groups * hashentrysize;
2011 
2012  /* make enough partitions so that each one is likely to fit in memory */
2013  dpartitions = 1 + (mem_wanted / hash_mem_limit);
2014 
2015  if (dpartitions > partition_limit)
2016  dpartitions = partition_limit;
2017 
2018  if (dpartitions < HASHAGG_MIN_PARTITIONS)
2019  dpartitions = HASHAGG_MIN_PARTITIONS;
2020  if (dpartitions > HASHAGG_MAX_PARTITIONS)
2021  dpartitions = HASHAGG_MAX_PARTITIONS;
2022 
2023  /* HASHAGG_MAX_PARTITIONS limit makes this safe */
2024  npartitions = (int) dpartitions;
2025 
2026  /* ceil(log2(npartitions)) */
2027  partition_bits = my_log2(npartitions);
2028 
2029  /* make sure that we don't exhaust the hash bits */
2030  if (partition_bits + used_bits >= 32)
2031  partition_bits = 32 - used_bits;
2032 
2033  if (log2_npartitions != NULL)
2034  *log2_npartitions = partition_bits;
2035 
2036  /* number of partitions will be a power of two */
2037  npartitions = 1 << partition_bits;
2038 
2039  return npartitions;
2040 }
int my_log2(long num)
Definition: dynahash.c:1760
#define HASHAGG_MAX_PARTITIONS
Definition: nodeAgg.c:298
#define HASHAGG_MIN_PARTITIONS
Definition: nodeAgg.c:297
#define HASHAGG_PARTITION_FACTOR
Definition: nodeAgg.c:296

References get_hash_memory_limit(), HASHAGG_MAX_PARTITIONS, HASHAGG_MIN_PARTITIONS, HASHAGG_PARTITION_FACTOR, HASHAGG_READ_BUFFER_SIZE, HASHAGG_WRITE_BUFFER_SIZE, and my_log2().

Referenced by hash_agg_set_limits(), and hashagg_spill_init().

◆ hashagg_batch_new()

static HashAggBatch * hashagg_batch_new ( LogicalTape input_tape,
int  setno,
int64  input_tuples,
double  input_card,
int  used_bits 
)
static

Definition at line 2992 of file nodeAgg.c.

2994 {
2995  HashAggBatch *batch = palloc0(sizeof(HashAggBatch));
2996 
2997  batch->setno = setno;
2998  batch->used_bits = used_bits;
2999  batch->input_tape = input_tape;
3000  batch->input_tuples = input_tuples;
3001  batch->input_card = input_card;
3002 
3003  return batch;
3004 }
int64 input_tuples
Definition: nodeAgg.c:356

References HashAggBatch::input_card, HashAggBatch::input_tape, HashAggBatch::input_tuples, palloc0(), HashAggBatch::setno, and HashAggBatch::used_bits.

Referenced by hashagg_spill_finish().

◆ hashagg_batch_read()

static MinimalTuple hashagg_batch_read ( HashAggBatch batch,
uint32 hashp 
)
static

Definition at line 3011 of file nodeAgg.c.

3012 {
3013  LogicalTape *tape = batch->input_tape;
3014  MinimalTuple tuple;
3015  uint32 t_len;
3016  size_t nread;
3017  uint32 hash;
3018 
3019  nread = LogicalTapeRead(tape, &hash, sizeof(uint32));
3020  if (nread == 0)
3021  return NULL;
3022  if (nread != sizeof(uint32))
3023  ereport(ERROR,
3025  errmsg_internal("unexpected EOF for tape %p: requested %zu bytes, read %zu bytes",
3026  tape, sizeof(uint32), nread)));
3027  if (hashp != NULL)
3028  *hashp = hash;
3029 
3030  nread = LogicalTapeRead(tape, &t_len, sizeof(t_len));
3031  if (nread != sizeof(uint32))
3032  ereport(ERROR,
3034  errmsg_internal("unexpected EOF for tape %p: requested %zu bytes, read %zu bytes",
3035  tape, sizeof(uint32), nread)));
3036 
3037  tuple = (MinimalTuple) palloc(t_len);
3038  tuple->t_len = t_len;
3039 
3040  nread = LogicalTapeRead(tape,
3041  (char *) tuple + sizeof(uint32),
3042  t_len - sizeof(uint32));
3043  if (nread != t_len - sizeof(uint32))
3044  ereport(ERROR,
3046  errmsg_internal("unexpected EOF for tape %p: requested %zu bytes, read %zu bytes",
3047  tape, t_len - sizeof(uint32), nread)));
3048 
3049  return tuple;
3050 }
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
int errcode_for_file_access(void)
Definition: elog.c:881
MinimalTupleData * MinimalTuple
Definition: htup.h:27
size_t LogicalTapeRead(LogicalTape *lt, void *ptr, size_t size)
Definition: logtape.c:928

References ereport, errcode_for_file_access(), errmsg_internal(), ERROR, hash(), HashAggBatch::input_tape, LogicalTapeRead(), palloc(), and MinimalTupleData::t_len.

Referenced by agg_refill_hash_table().

◆ hashagg_finish_initial_spills()

static void hashagg_finish_initial_spills ( AggState aggstate)
static

Definition at line 3060 of file nodeAgg.c.

3061 {
3062  int setno;
3063  int total_npartitions = 0;
3064 
3065  if (aggstate->hash_spills != NULL)
3066  {
3067  for (setno = 0; setno < aggstate->num_hashes; setno++)
3068  {
3069  HashAggSpill *spill = &aggstate->hash_spills[setno];
3070 
3071  total_npartitions += spill->npartitions;
3072  hashagg_spill_finish(aggstate, spill, setno);
3073  }
3074 
3075  /*
3076  * We're not processing tuples from outer plan any more; only
3077  * processing batches of spilled tuples. The initial spill structures
3078  * are no longer needed.
3079  */
3080  pfree(aggstate->hash_spills);
3081  aggstate->hash_spills = NULL;
3082  }
3083 
3084  hash_agg_update_metrics(aggstate, false, total_npartitions);
3085  aggstate->hash_spill_mode = false;
3086 }

References hash_agg_update_metrics(), AggState::hash_spill_mode, AggState::hash_spills, hashagg_spill_finish(), HashAggSpill::npartitions, AggState::num_hashes, and pfree().

Referenced by agg_fill_hash_table(), and agg_retrieve_direct().

◆ hashagg_recompile_expressions()

static void hashagg_recompile_expressions ( AggState aggstate,
bool  minslot,
bool  nullcheck 
)
static

Definition at line 1742 of file nodeAgg.c.

1743 {
1744  AggStatePerPhase phase;
1745  int i = minslot ? 1 : 0;
1746  int j = nullcheck ? 1 : 0;
1747 
1748  Assert(aggstate->aggstrategy == AGG_HASHED ||
1749  aggstate->aggstrategy == AGG_MIXED);
1750 
1751  if (aggstate->aggstrategy == AGG_HASHED)
1752  phase = &aggstate->phases[0];
1753  else /* AGG_MIXED */
1754  phase = &aggstate->phases[1];
1755 
1756  if (phase->evaltrans_cache[i][j] == NULL)
1757  {
1758  const TupleTableSlotOps *outerops = aggstate->ss.ps.outerops;
1759  bool outerfixed = aggstate->ss.ps.outeropsfixed;
1760  bool dohash = true;
1761  bool dosort = false;
1762 
1763  /*
1764  * If minslot is true, that means we are processing a spilled batch
1765  * (inside agg_refill_hash_table()), and we must not advance the
1766  * sorted grouping sets.
1767  */
1768  if (aggstate->aggstrategy == AGG_MIXED && !minslot)
1769  dosort = true;
1770 
1771  /* temporarily change the outerops while compiling the expression */
1772  if (minslot)
1773  {
1774  aggstate->ss.ps.outerops = &TTSOpsMinimalTuple;
1775  aggstate->ss.ps.outeropsfixed = true;
1776  }
1777 
1778  phase->evaltrans_cache[i][j] = ExecBuildAggTrans(aggstate, phase,
1779  dosort, dohash,
1780  nullcheck);
1781 
1782  /* change back */
1783  aggstate->ss.ps.outerops = outerops;
1784  aggstate->ss.ps.outeropsfixed = outerfixed;
1785  }
1786 
1787  phase->evaltrans = phase->evaltrans_cache[i][j];
1788 }

References AGG_HASHED, AGG_MIXED, AggState::aggstrategy, Assert(), AggStatePerPhaseData::evaltrans, AggStatePerPhaseData::evaltrans_cache, ExecBuildAggTrans(), i, j, PlanState::outerops, PlanState::outeropsfixed, AggState::phases, ScanState::ps, AggState::ss, and TTSOpsMinimalTuple.

Referenced by agg_refill_hash_table(), ExecReScanAgg(), and hash_agg_enter_spill_mode().

◆ hashagg_reset_spill_state()

static void hashagg_reset_spill_state ( AggState aggstate)
static

Definition at line 3134 of file nodeAgg.c.

3135 {
3136  /* free spills from initial pass */
3137  if (aggstate->hash_spills != NULL)
3138  {
3139  int setno;
3140 
3141  for (setno = 0; setno < aggstate->num_hashes; setno++)
3142  {
3143  HashAggSpill *spill = &aggstate->hash_spills[setno];
3144 
3145  pfree(spill->ntuples);
3146  pfree(spill->partitions);
3147  }
3148  pfree(aggstate->hash_spills);
3149  aggstate->hash_spills = NULL;
3150  }
3151 
3152  /* free batches */
3153  list_free_deep(aggstate->hash_batches);
3154  aggstate->hash_batches = NIL;
3155 
3156  /* close tape set */
3157  if (aggstate->hash_tapeset != NULL)
3158  {
3159  LogicalTapeSetClose(aggstate->hash_tapeset);
3160  aggstate->hash_tapeset = NULL;
3161  }
3162 }
void list_free_deep(List *list)
Definition: list.c:1559
void LogicalTapeSetClose(LogicalTapeSet *lts)
Definition: logtape.c:667
int64 * ntuples
Definition: nodeAgg.c:336
LogicalTape ** partitions
Definition: nodeAgg.c:335

References AggState::hash_batches, AggState::hash_spills, AggState::hash_tapeset, list_free_deep(), LogicalTapeSetClose(), NIL, HashAggSpill::ntuples, AggState::num_hashes, HashAggSpill::partitions, and pfree().

Referenced by ExecEndAgg(), and ExecReScanAgg().

◆ hashagg_spill_finish()

static void hashagg_spill_finish ( AggState aggstate,
HashAggSpill spill,
int  setno 
)
static

Definition at line 3094 of file nodeAgg.c.

3095 {
3096  int i;
3097  int used_bits = 32 - spill->shift;
3098 
3099  if (spill->npartitions == 0)
3100  return; /* didn't spill */
3101 
3102  for (i = 0; i < spill->npartitions; i++)
3103  {
3104  LogicalTape *tape = spill->partitions[i];
3105  HashAggBatch *new_batch;
3106  double cardinality;
3107 
3108  /* if the partition is empty, don't create a new batch of work */
3109  if (spill->ntuples[i] == 0)
3110  continue;
3111 
3112  cardinality = estimateHyperLogLog(&spill->hll_card[i]);
3113  freeHyperLogLog(&spill->hll_card[i]);
3114 
3115  /* rewinding frees the buffer while not in use */
3117 
3118  new_batch = hashagg_batch_new(tape, setno,
3119  spill->ntuples[i], cardinality,
3120  used_bits);
3121  aggstate->hash_batches = lappend(aggstate->hash_batches, new_batch);
3122  aggstate->hash_batches_used++;
3123  }
3124 
3125  pfree(spill->ntuples);
3126  pfree(spill->hll_card);
3127  pfree(spill->partitions);
3128 }
double estimateHyperLogLog(hyperLogLogState *cState)
Definition: hyperloglog.c:186
void freeHyperLogLog(hyperLogLogState *cState)
Definition: hyperloglog.c:151
void LogicalTapeRewindForRead(LogicalTape *lt, size_t buffer_size)
Definition: logtape.c:846
static HashAggBatch * hashagg_batch_new(LogicalTape *input_tape, int setno, int64 input_tuples, double input_card, int used_bits)
Definition: nodeAgg.c:2992
hyperLogLogState * hll_card
Definition: nodeAgg.c:339

References estimateHyperLogLog(), freeHyperLogLog(), AggState::hash_batches, AggState::hash_batches_used, hashagg_batch_new(), HASHAGG_READ_BUFFER_SIZE, HashAggSpill::hll_card, i, lappend(), LogicalTapeRewindForRead(), HashAggSpill::npartitions, HashAggSpill::ntuples, HashAggSpill::partitions, pfree(), and HashAggSpill::shift.

Referenced by agg_refill_hash_table(), and hashagg_finish_initial_spills().

◆ hashagg_spill_init()

static void hashagg_spill_init ( HashAggSpill spill,
LogicalTapeSet tapeset,
int  used_bits,
double  input_groups,
double  hashentrysize 
)
static

Definition at line 2895 of file nodeAgg.c.

2897 {
2898  int npartitions;
2899  int partition_bits;
2900 
2901  npartitions = hash_choose_num_partitions(input_groups, hashentrysize,
2902  used_bits, &partition_bits);
2903 
2904  spill->partitions = palloc0(sizeof(LogicalTape *) * npartitions);
2905  spill->ntuples = palloc0(sizeof(int64) * npartitions);
2906  spill->hll_card = palloc0(sizeof(hyperLogLogState) * npartitions);
2907 
2908  for (int i = 0; i < npartitions; i++)
2909  spill->partitions[i] = LogicalTapeCreate(tapeset);
2910 
2911  spill->shift = 32 - used_bits - partition_bits;
2912  spill->mask = (npartitions - 1) << spill->shift;
2913  spill->npartitions = npartitions;
2914 
2915  for (int i = 0; i < npartitions; i++)
2917 }
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
Definition: hyperloglog.c:66
LogicalTape * LogicalTapeCreate(LogicalTapeSet *lts)
Definition: logtape.c:680
#define HASHAGG_HLL_BIT_WIDTH
Definition: nodeAgg.c:315
uint32 mask
Definition: nodeAgg.c:337

References hash_choose_num_partitions(), HASHAGG_HLL_BIT_WIDTH, HashAggSpill::hll_card, i, initHyperLogLog(), LogicalTapeCreate(), HashAggSpill::mask, HashAggSpill::npartitions, HashAggSpill::ntuples, palloc0(), HashAggSpill::partitions, and HashAggSpill::shift.

Referenced by agg_refill_hash_table(), hash_agg_enter_spill_mode(), and lookup_hash_entries().

◆ hashagg_spill_tuple()

static Size hashagg_spill_tuple ( AggState aggstate,
HashAggSpill spill,
TupleTableSlot inputslot,
uint32  hash 
)
static

Definition at line 2926 of file nodeAgg.c.

2928 {
2929  TupleTableSlot *spillslot;
2930  int partition;
2931  MinimalTuple tuple;
2932  LogicalTape *tape;
2933  int total_written = 0;
2934  bool shouldFree;
2935 
2936  Assert(spill->partitions != NULL);
2937 
2938  /* spill only attributes that we actually need */
2939  if (!aggstate->all_cols_needed)
2940  {
2941  spillslot = aggstate->hash_spill_wslot;
2942  slot_getsomeattrs(inputslot, aggstate->max_colno_needed);
2943  ExecClearTuple(spillslot);
2944  for (int i = 0; i < spillslot->tts_tupleDescriptor->natts; i++)
2945  {
2946  if (bms_is_member(i + 1, aggstate->colnos_needed))
2947  {
2948  spillslot->tts_values[i] = inputslot->tts_values[i];
2949  spillslot->tts_isnull[i] = inputslot->tts_isnull[i];
2950  }
2951  else
2952  spillslot->tts_isnull[i] = true;
2953  }
2954  ExecStoreVirtualTuple(spillslot);
2955  }
2956  else
2957  spillslot = inputslot;
2958 
2959  tuple = ExecFetchSlotMinimalTuple(spillslot, &shouldFree);
2960 
2961  partition = (hash & spill->mask) >> spill->shift;
2962  spill->ntuples[partition]++;
2963 
2964  /*
2965  * All hash values destined for a given partition have some bits in
2966  * common, which causes bad HLL cardinality estimates. Hash the hash to
2967  * get a more uniform distribution.
2968  */
2969  addHyperLogLog(&spill->hll_card[partition], hash_bytes_uint32(hash));
2970 
2971  tape = spill->partitions[partition];
2972 
2973  LogicalTapeWrite(tape, &hash, sizeof(uint32));
2974  total_written += sizeof(uint32);
2975 
2976  LogicalTapeWrite(tape, tuple, tuple->t_len);
2977  total_written += tuple->t_len;
2978 
2979  if (shouldFree)
2980  pfree(tuple);
2981 
2982  return total_written;
2983 }
MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot, bool *shouldFree)
Definition: execTuples.c:1693
uint32 hash_bytes_uint32(uint32 k)
Definition: hashfn.c:610
void addHyperLogLog(hyperLogLogState *cState, uint32 hash)
Definition: hyperloglog.c:167
void LogicalTapeWrite(LogicalTape *lt, const void *ptr, size_t size)
Definition: logtape.c:761
static void slot_getsomeattrs(TupleTableSlot *slot, int attnum)
Definition: tuptable.h:348

References addHyperLogLog(), AggState::all_cols_needed, Assert(), bms_is_member(), AggState::colnos_needed, ExecClearTuple(), ExecFetchSlotMinimalTuple(), ExecStoreVirtualTuple(), hash(), hash_bytes_uint32(), AggState::hash_spill_wslot, HashAggSpill::hll_card, i, LogicalTapeWrite(), HashAggSpill::mask, AggState::max_colno_needed, TupleDescData::natts, HashAggSpill::ntuples, HashAggSpill::partitions, pfree(), HashAggSpill::shift, slot_getsomeattrs(), MinimalTupleData::t_len, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, and TupleTableSlot::tts_values.

Referenced by agg_refill_hash_table(), and lookup_hash_entries().

◆ initialize_aggregate()

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

Definition at line 579 of file nodeAgg.c.

581 {
582  /*
583  * Start a fresh sort operation for each DISTINCT/ORDER BY aggregate.
584  */
585  if (pertrans->aggsortrequired)
586  {
587  /*
588  * In case of rescan, maybe there could be an uncompleted sort
589  * operation? Clean it up if so.
590  */
591  if (pertrans->sortstates[aggstate->current_set])
592  tuplesort_end(pertrans->sortstates[aggstate->current_set]);
593 
594 
595  /*
596  * We use a plain Datum sorter when there's a single input column;
597  * otherwise sort the full tuple. (See comments for
598  * process_ordered_aggregate_single.)
599  */
600  if (pertrans->numInputs == 1)
601  {
602  Form_pg_attribute attr = TupleDescAttr(pertrans->sortdesc, 0);
603 
604  pertrans->sortstates[aggstate->current_set] =
605  tuplesort_begin_datum(attr->atttypid,
606  pertrans->sortOperators[0],
607  pertrans->sortCollations[0],
608  pertrans->sortNullsFirst[0],
609  work_mem, NULL, TUPLESORT_NONE);
610  }
611  else
612  pertrans->sortstates[aggstate->current_set] =
613  tuplesort_begin_heap(pertrans->sortdesc,
614  pertrans->numSortCols,
615  pertrans->sortColIdx,
616  pertrans->sortOperators,
617  pertrans->sortCollations,
618  pertrans->sortNullsFirst,
619  work_mem, NULL, TUPLESORT_NONE);
620  }
621 
622  /*
623  * (Re)set transValue to the initial value.
624  *
625  * Note that when the initial value is pass-by-ref, we must copy it (into
626  * the aggcontext) since we will pfree the transValue later.
627  */
628  if (pertrans->initValueIsNull)
629  pergroupstate->transValue = pertrans->initValue;
630  else
631  {
632  MemoryContext oldContext;
633 
635  pergroupstate->transValue = datumCopy(pertrans->initValue,
636  pertrans->transtypeByVal,
637  pertrans->transtypeLen);
638  MemoryContextSwitchTo(oldContext);
639  }
640  pergroupstate->transValueIsNull = pertrans->initValueIsNull;
641 
642  /*
643  * If the initial value for the transition state doesn't exist in the
644  * pg_aggregate table then we will let the first non-NULL value returned
645  * from the outer procNode become the initial value. (This is useful for
646  * aggregates like max() and min().) The noTransValue flag signals that we
647  * still need to do this.
648  */
649  pergroupstate->noTransValue = pertrans->initValueIsNull;
650 }
int work_mem
Definition: globals.c:125
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define TUPLESORT_NONE
Definition: tuplesort.h:92
Tuplesortstate * tuplesort_begin_datum(Oid datumType, Oid sortOperator, Oid sortCollation, bool nullsFirstFlag, int workMem, SortCoordinate coordinate, int sortopt)
Tuplesortstate * tuplesort_begin_heap(TupleDesc tupDesc, int nkeys, AttrNumber *attNums, Oid *sortOperators, Oid *sortCollations, bool *nullsFirstFlags, int workMem, SortCoordinate coordinate, int sortopt)

References AggStatePerTransData::aggsortrequired, AggState::curaggcontext, AggState::current_set, datumCopy(), ExprContext::ecxt_per_tuple_memory, AggStatePerTransData::initValue, AggStatePerTransData::initValueIsNull, MemoryContextSwitchTo(), AggStatePerGroupData::noTransValue, AggStatePerTransData::numInputs, AggStatePerTransData::numSortCols, AggStatePerTransData::sortColIdx, AggStatePerTransData::sortCollations, AggStatePerTransData::sortdesc, AggStatePerTransData::sortNullsFirst, AggStatePerTransData::sortOperators, AggStatePerTransData::sortstates, AggStatePerTransData::transtypeByVal, AggStatePerTransData::transtypeLen, AggStatePerGroupData::transValue, AggStatePerGroupData::transValueIsNull, TupleDescAttr, tuplesort_begin_datum(), tuplesort_begin_heap(), tuplesort_end(), TUPLESORT_NONE, and work_mem.

Referenced by initialize_aggregates(), and initialize_hash_entry().

◆ initialize_aggregates()

static void initialize_aggregates ( AggState aggstate,
AggStatePerGroup pergroups,
int  numReset 
)
static

Definition at line 666 of file nodeAgg.c.

669 {
670  int transno;
671  int numGroupingSets = Max(aggstate->phase->numsets, 1);
672  int setno = 0;
673  int numTrans = aggstate->numtrans;
674  AggStatePerTrans transstates = aggstate->pertrans;
675 
676  if (numReset == 0)
677  numReset = numGroupingSets;
678 
679  for (setno = 0; setno < numReset; setno++)
680  {
681  AggStatePerGroup pergroup = pergroups[setno];
682 
683  select_current_set(aggstate, setno, false);
684 
685  for (transno = 0; transno < numTrans; transno++)
686  {
687  AggStatePerTrans pertrans = &transstates[transno];
688  AggStatePerGroup pergroupstate = &pergroup[transno];
689 
690  initialize_aggregate(aggstate, pertrans, pergroupstate);
691  }
692  }
693 }
static void initialize_aggregate(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroupstate)
Definition: nodeAgg.c:579

References initialize_aggregate(), Max, AggStatePerPhaseData::numsets, AggState::numtrans, AggState::pertrans, AggState::phase, and select_current_set().

Referenced by agg_retrieve_direct().

◆ initialize_hash_entry()

static void initialize_hash_entry ( AggState aggstate,
TupleHashTable  hashtable,
TupleHashEntry  entry 
)
static

Definition at line 2046 of file nodeAgg.c.

2048 {
2049  AggStatePerGroup pergroup;
2050  int transno;
2051 
2052  aggstate->hash_ngroups_current++;
2053  hash_agg_check_limits(aggstate);
2054 
2055  /* no need to allocate or initialize per-group state */
2056  if (aggstate->numtrans == 0)
2057  return;
2058 
2059  pergroup = (AggStatePerGroup)
2060  MemoryContextAlloc(hashtable->tablecxt,
2061  sizeof(AggStatePerGroupData) * aggstate->numtrans);
2062 
2063  entry->additional = pergroup;
2064 
2065  /*
2066  * Initialize aggregates for new tuple group, lookup_hash_entries()
2067  * already has selected the relevant grouping set.
2068  */
2069  for (transno = 0; transno < aggstate->numtrans; transno++)
2070  {
2071  AggStatePerTrans pertrans = &aggstate->pertrans[transno];
2072  AggStatePerGroup pergroupstate = &pergroup[transno];
2073 
2074  initialize_aggregate(aggstate, pertrans, pergroupstate);
2075  }
2076 }
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1021
static void hash_agg_check_limits(AggState *aggstate)
Definition: nodeAgg.c:1857
MemoryContext tablecxt
Definition: execnodes.h:815

References TupleHashEntryData::additional, hash_agg_check_limits(), AggState::hash_ngroups_current, initialize_aggregate(), MemoryContextAlloc(), AggState::numtrans, AggState::pertrans, and TupleHashTableData::tablecxt.

Referenced by agg_refill_hash_table(), and lookup_hash_entries().

◆ initialize_phase()

static void initialize_phase ( AggState aggstate,
int  newphase 
)
static

Definition at line 478 of file nodeAgg.c.

479 {
480  Assert(newphase <= 1 || newphase == aggstate->current_phase + 1);
481 
482  /*
483  * Whatever the previous state, we're now done with whatever input
484  * tuplesort was in use.
485  */
486  if (aggstate->sort_in)
487  {
488  tuplesort_end(aggstate->sort_in);
489  aggstate->sort_in = NULL;
490  }
491 
492  if (newphase <= 1)
493  {
494  /*
495  * Discard any existing output tuplesort.
496  */
497  if (aggstate->sort_out)
498  {
499  tuplesort_end(aggstate->sort_out);
500  aggstate->sort_out = NULL;
501  }
502  }
503  else
504  {
505  /*
506  * The old output tuplesort becomes the new input one, and this is the
507  * right time to actually sort it.
508  */
509  aggstate->sort_in = aggstate->sort_out;
510  aggstate->sort_out = NULL;
511  Assert(aggstate->sort_in);
512  tuplesort_performsort(aggstate->sort_in);
513  }
514 
515  /*
516  * If this isn't the last phase, we need to sort appropriately for the
517  * next phase in sequence.
518  */
519  if (newphase > 0 && newphase < aggstate->numphases - 1)
520  {
521  Sort *sortnode = aggstate->phases[newphase + 1].sortnode;
522  PlanState *outerNode = outerPlanState(aggstate);
523  TupleDesc tupDesc = ExecGetResultType(outerNode);
524 
525  aggstate->sort_out = tuplesort_begin_heap(tupDesc,
526  sortnode->numCols,
527  sortnode->sortColIdx,
528  sortnode->sortOperators,
529  sortnode->collations,
530  sortnode->nullsFirst,
531  work_mem,
532  NULL, TUPLESORT_NONE);
533  }
534 
535  aggstate->current_phase = newphase;
536  aggstate->phase = &aggstate->phases[newphase];
537 }
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:498
int numCols
Definition: plannodes.h:934
void tuplesort_performsort(Tuplesortstate *state)
Definition: tuplesort.c:1382

References Assert(), AggState::current_phase, ExecGetResultType(), Sort::numCols, outerPlanState, AggState::phase, AggState::phases, AggState::sort_in, AggState::sort_out, AggStatePerPhaseData::sortnode, tuplesort_begin_heap(), tuplesort_end(), TUPLESORT_NONE, tuplesort_performsort(), and work_mem.

Referenced by agg_retrieve_direct(), ExecInitAgg(), and ExecReScanAgg().

◆ lookup_hash_entries()

static void lookup_hash_entries ( AggState aggstate)
static

Definition at line 2096 of file nodeAgg.c.

2097 {
2098  AggStatePerGroup *pergroup = aggstate->hash_pergroup;
2099  TupleTableSlot *outerslot = aggstate->tmpcontext->ecxt_outertuple;
2100  int setno;
2101 
2102  for (setno = 0; setno < aggstate->num_hashes; setno++)
2103  {
2104  AggStatePerHash perhash = &aggstate->perhash[setno];
2105  TupleHashTable hashtable = perhash->hashtable;
2106  TupleTableSlot *hashslot = perhash->hashslot;
2107  TupleHashEntry entry;
2108  uint32 hash;
2109  bool isnew = false;
2110  bool *p_isnew;
2111 
2112  /* if hash table already spilled, don't create new entries */
2113  p_isnew = aggstate->hash_spill_mode ? NULL : &isnew;
2114 
2115  select_current_set(aggstate, setno, true);
2116  prepare_hash_slot(perhash,
2117  outerslot,
2118  hashslot);
2119 
2120  entry = LookupTupleHashEntry(hashtable, hashslot,
2121  p_isnew, &hash);
2122 
2123  if (entry != NULL)
2124  {
2125  if (isnew)
2126  initialize_hash_entry(aggstate, hashtable, entry);
2127  pergroup[setno] = entry->additional;
2128  }
2129  else
2130  {
2131  HashAggSpill *spill = &aggstate->hash_spills[setno];
2132  TupleTableSlot *slot = aggstate->tmpcontext->ecxt_outertuple;
2133 
2134  if (spill->partitions == NULL)
2135  hashagg_spill_init(spill, aggstate->hash_tapeset, 0,
2136  perhash->aggnode->numGroups,
2137  aggstate->hashentrysize);
2138 
2139  hashagg_spill_tuple(aggstate, spill, slot, hash);
2140  pergroup[setno] = NULL;
2141  }
2142  }
2143 }
TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew, uint32 *hash)
Definition: execGrouping.c:306

References TupleHashEntryData::additional, AggStatePerHashData::aggnode, ExprContext::ecxt_outertuple, hash(), AggState::hash_pergroup, AggState::hash_spill_mode, AggState::hash_spills, AggState::hash_tapeset, hashagg_spill_init(), hashagg_spill_tuple(), AggState::hashentrysize, AggStatePerHashData::hashslot, AggStatePerHashData::hashtable, initialize_hash_entry(), LookupTupleHashEntry(), AggState::num_hashes, Agg::numGroups, HashAggSpill::partitions, AggState::perhash, prepare_hash_slot(), select_current_set(), and AggState::tmpcontext.

Referenced by agg_fill_hash_table(), and agg_retrieve_direct().

◆ prepare_hash_slot()

static void prepare_hash_slot ( AggStatePerHash  perhash,
TupleTableSlot inputslot,
TupleTableSlot hashslot 
)
inlinestatic

Definition at line 1205 of file nodeAgg.c.

1208 {
1209  int i;
1210 
1211  /* transfer just the needed columns into hashslot */
1212  slot_getsomeattrs(inputslot, perhash->largestGrpColIdx);
1213  ExecClearTuple(hashslot);
1214 
1215  for (i = 0; i < perhash->numhashGrpCols; i++)
1216  {
1217  int varNumber = perhash->hashGrpColIdxInput[i] - 1;
1218 
1219  hashslot->tts_values[i] = inputslot->tts_values[varNumber];
1220  hashslot->tts_isnull[i] = inputslot->tts_isnull[varNumber];
1221  }
1222  ExecStoreVirtualTuple(hashslot);
1223 }

References ExecClearTuple(), ExecStoreVirtualTuple(), AggStatePerHashData::hashGrpColIdxInput, i, AggStatePerHashData::largestGrpColIdx, AggStatePerHashData::numhashGrpCols, slot_getsomeattrs(), TupleTableSlot::tts_isnull, and TupleTableSlot::tts_values.

Referenced by agg_refill_hash_table(), and lookup_hash_entries().

◆ prepare_projection_slot()

static void prepare_projection_slot ( AggState aggstate,
TupleTableSlot slot,
int  currentSet 
)
static

Definition at line 1250 of file nodeAgg.c.

1251 {
1252  if (aggstate->phase->grouped_cols)
1253  {
1254  Bitmapset *grouped_cols = aggstate->phase->grouped_cols[currentSet];
1255 
1256  aggstate->grouped_cols = grouped_cols;
1257 
1258  if (TTS_EMPTY(slot))
1259  {
1260  /*
1261  * Force all values to be NULL if working on an empty input tuple
1262  * (i.e. an empty grouping set for which no input rows were
1263  * supplied).
1264  */
1265  ExecStoreAllNullTuple(slot);
1266  }
1267  else if (aggstate->all_grouped_cols)
1268  {
1269  ListCell *lc;
1270 
1271  /* all_grouped_cols is arranged in desc order */
1273 
1274  foreach(lc, aggstate->all_grouped_cols)
1275  {
1276  int attnum = lfirst_int(lc);
1277 
1278  if (!bms_is_member(attnum, grouped_cols))
1279  slot->tts_isnull[attnum - 1] = true;
1280  }
1281  }
1282  }
1283 }
TupleTableSlot * ExecStoreAllNullTuple(TupleTableSlot *slot)
Definition: execTuples.c:1577
#define linitial_int(l)
Definition: pg_list.h:179