PostgreSQL Source Code  git master
execGrouping.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * execGrouping.c
4  * executor utility routines for grouping, hashing, and aggregation
5  *
6  * Note: we currently assume that equality and hashing functions are not
7  * collation-sensitive, so the code in this file has no support for passing
8  * collation settings through from callers. That may have to change someday.
9  *
10  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  *
14  * IDENTIFICATION
15  * src/backend/executor/execGrouping.c
16  *
17  *-------------------------------------------------------------------------
18  */
19 #include "postgres.h"
20 
21 #include "access/parallel.h"
22 #include "executor/executor.h"
23 #include "miscadmin.h"
24 #include "utils/lsyscache.h"
25 #include "utils/hashutils.h"
26 #include "utils/memutils.h"
27 
28 static uint32 TupleHashTableHash(struct tuplehash_hash *tb, const MinimalTuple tuple);
29 static int TupleHashTableMatch(struct tuplehash_hash *tb, const MinimalTuple tuple1, const MinimalTuple tuple2);
30 
31 /*
32  * Define parameters for tuple hash table code generation. The interface is
33  * *also* declared in execnodes.h (to generate the types, which are externally
34  * visible).
35  */
36 #define SH_PREFIX tuplehash
37 #define SH_ELEMENT_TYPE TupleHashEntryData
38 #define SH_KEY_TYPE MinimalTuple
39 #define SH_KEY firstTuple
40 #define SH_HASH_KEY(tb, key) TupleHashTableHash(tb, key)
41 #define SH_EQUAL(tb, a, b) TupleHashTableMatch(tb, a, b) == 0
42 #define SH_SCOPE extern
43 #define SH_STORE_HASH
44 #define SH_GET_HASH(tb, a) a->hash
45 #define SH_DEFINE
46 #include "lib/simplehash.h"
47 
48 
49 /*****************************************************************************
50  * Utility routines for grouping tuples together
51  *****************************************************************************/
52 
53 /*
54  * execTuplesMatchPrepare
55  * Build expression that can be evaluated using ExecQual(), returning
56  * whether an ExprContext's inner/outer tuples are NOT DISTINCT
57  */
58 ExprState *
60  int numCols,
61  const AttrNumber *keyColIdx,
62  const Oid *eqOperators,
63  const Oid *collations,
64  PlanState *parent)
65 {
66  Oid *eqFunctions = (Oid *) palloc(numCols * sizeof(Oid));
67  int i;
68  ExprState *expr;
69 
70  if (numCols == 0)
71  return NULL;
72 
73  /* lookup equality functions */
74  for (i = 0; i < numCols; i++)
75  eqFunctions[i] = get_opcode(eqOperators[i]);
76 
77  /* build actual expression */
78  expr = ExecBuildGroupingEqual(desc, desc, NULL, NULL,
79  numCols, keyColIdx, eqFunctions, collations,
80  parent);
81 
82  return expr;
83 }
84 
85 /*
86  * execTuplesHashPrepare
87  * Look up the equality and hashing functions needed for a TupleHashTable.
88  *
89  * This is similar to execTuplesMatchPrepare, but we also need to find the
90  * hash functions associated with the equality operators. *eqFunctions and
91  * *hashFunctions receive the palloc'd result arrays.
92  *
93  * Note: we expect that the given operators are not cross-type comparisons.
94  */
95 void
97  const Oid *eqOperators,
98  Oid **eqFuncOids,
99  FmgrInfo **hashFunctions)
100 {
101  int i;
102 
103  *eqFuncOids = (Oid *) palloc(numCols * sizeof(Oid));
104  *hashFunctions = (FmgrInfo *) palloc(numCols * sizeof(FmgrInfo));
105 
106  for (i = 0; i < numCols; i++)
107  {
108  Oid eq_opr = eqOperators[i];
109  Oid eq_function;
110  Oid left_hash_function;
111  Oid right_hash_function;
112 
113  eq_function = get_opcode(eq_opr);
114  if (!get_op_hash_functions(eq_opr,
115  &left_hash_function, &right_hash_function))
116  elog(ERROR, "could not find hash function for hash operator %u",
117  eq_opr);
118  /* We're not supporting cross-type cases here */
119  Assert(left_hash_function == right_hash_function);
120  (*eqFuncOids)[i] = eq_function;
121  fmgr_info(right_hash_function, &(*hashFunctions)[i]);
122  }
123 }
124 
125 
126 /*****************************************************************************
127  * Utility routines for all-in-memory hash tables
128  *
129  * These routines build hash tables for grouping tuples together (eg, for
130  * hash aggregation). There is one entry for each not-distinct set of tuples
131  * presented.
132  *****************************************************************************/
133 
134 /*
135  * Construct an empty TupleHashTable
136  *
137  * numCols, keyColIdx: identify the tuple fields to use as lookup key
138  * eqfunctions: equality comparison functions to use
139  * hashfunctions: datatype-specific hashing functions to use
140  * nbuckets: initial estimate of hashtable size
141  * additionalsize: size of data stored in ->additional
142  * metacxt: memory context for long-lived allocation, but not per-entry data
143  * tablecxt: memory context in which to store table entries
144  * tempcxt: short-lived context for evaluation hash and comparison functions
145  *
146  * The function arrays may be made with execTuplesHashPrepare(). Note they
147  * are not cross-type functions, but expect to see the table datatype(s)
148  * on both sides.
149  *
150  * Note that keyColIdx, eqfunctions, and hashfunctions must be allocated in
151  * storage that will live as long as the hashtable does.
152  */
155  TupleDesc inputDesc,
156  int numCols, AttrNumber *keyColIdx,
157  const Oid *eqfuncoids,
158  FmgrInfo *hashfunctions,
159  Oid *collations,
160  long nbuckets, Size additionalsize,
161  MemoryContext metacxt,
162  MemoryContext tablecxt,
163  MemoryContext tempcxt,
164  bool use_variable_hash_iv)
165 {
166  TupleHashTable hashtable;
167  Size entrysize = sizeof(TupleHashEntryData) + additionalsize;
168  MemoryContext oldcontext;
169 
170  Assert(nbuckets > 0);
171 
172  /* Limit initial table size request to not more than work_mem */
173  nbuckets = Min(nbuckets, (long) ((work_mem * 1024L) / entrysize));
174 
175  oldcontext = MemoryContextSwitchTo(metacxt);
176 
177  hashtable = (TupleHashTable) palloc(sizeof(TupleHashTableData));
178 
179  hashtable->numCols = numCols;
180  hashtable->keyColIdx = keyColIdx;
181  hashtable->tab_hash_funcs = hashfunctions;
182  hashtable->tab_collations = collations;
183  hashtable->tablecxt = tablecxt;
184  hashtable->tempcxt = tempcxt;
185  hashtable->entrysize = entrysize;
186  hashtable->tableslot = NULL; /* will be made on first lookup */
187  hashtable->inputslot = NULL;
188  hashtable->in_hash_funcs = NULL;
189  hashtable->cur_eq_func = NULL;
190 
191  /*
192  * If parallelism is in use, even if the master backend is performing the
193  * scan itself, we don't want to create the hashtable exactly the same way
194  * in all workers. As hashtables are iterated over in keyspace-order,
195  * doing so in all processes in the same way is likely to lead to
196  * "unbalanced" hashtables when the table size initially is
197  * underestimated.
198  */
199  if (use_variable_hash_iv)
201  else
202  hashtable->hash_iv = 0;
203 
204  hashtable->hashtab = tuplehash_create(metacxt, nbuckets, hashtable);
205 
206  /*
207  * We copy the input tuple descriptor just for safety --- we assume all
208  * input tuples will have equivalent descriptors.
209  */
212 
213  /* build comparator for all columns */
214  /* XXX: should we support non-minimal tuples for the inputslot? */
215  hashtable->tab_eq_func = ExecBuildGroupingEqual(inputDesc, inputDesc,
217  numCols,
218  keyColIdx, eqfuncoids, collations,
219  NULL);
220 
221  /*
222  * While not pretty, it's ok to not shut down this context, but instead
223  * rely on the containing memory context being reset, as
224  * ExecBuildGroupingEqual() only builds a very simple expression calling
225  * functions (i.e. nothing that'd employ RegisterExprContextCallback()).
226  */
228 
229  MemoryContextSwitchTo(oldcontext);
230 
231  return hashtable;
232 }
233 
234 /*
235  * BuildTupleHashTable is a backwards-compatibilty wrapper for
236  * BuildTupleHashTableExt(), that allocates the hashtable's metadata in
237  * tablecxt. Note that hashtables created this way cannot be reset leak-free
238  * with ResetTupleHashTable().
239  */
242  TupleDesc inputDesc,
243  int numCols, AttrNumber *keyColIdx,
244  const Oid *eqfuncoids,
245  FmgrInfo *hashfunctions,
246  Oid *collations,
247  long nbuckets, Size additionalsize,
248  MemoryContext tablecxt,
249  MemoryContext tempcxt,
250  bool use_variable_hash_iv)
251 {
252  return BuildTupleHashTableExt(parent,
253  inputDesc,
254  numCols, keyColIdx,
255  eqfuncoids,
256  hashfunctions,
257  collations,
258  nbuckets, additionalsize,
259  tablecxt,
260  tablecxt,
261  tempcxt,
262  use_variable_hash_iv);
263 }
264 
265 /*
266  * Reset contents of the hashtable to be empty, preserving all the non-content
267  * state. Note that the tablecxt passed to BuildTupleHashTableExt() should
268  * also be reset, otherwise there will be leaks.
269  */
270 void
272 {
273  tuplehash_reset(hashtable->hashtab);
274 }
275 
276 /*
277  * Find or create a hashtable entry for the tuple group containing the
278  * given tuple. The tuple must be the same type as the hashtable entries.
279  *
280  * If isnew is NULL, we do not create new entries; we return NULL if no
281  * match is found.
282  *
283  * If isnew isn't NULL, then a new entry is created if no existing entry
284  * matches. On return, *isnew is true if the entry is newly created,
285  * false if it existed already. ->additional_data in the new entry has
286  * been zeroed.
287  */
290  bool *isnew)
291 {
292  TupleHashEntryData *entry;
293  MemoryContext oldContext;
294  bool found;
296 
297  /* Need to run the hash functions in short-lived context */
298  oldContext = MemoryContextSwitchTo(hashtable->tempcxt);
299 
300  /* set up data needed by hash and match functions */
301  hashtable->inputslot = slot;
302  hashtable->in_hash_funcs = hashtable->tab_hash_funcs;
303  hashtable->cur_eq_func = hashtable->tab_eq_func;
304 
305  key = NULL; /* flag to reference inputslot */
306 
307  if (isnew)
308  {
309  entry = tuplehash_insert(hashtable->hashtab, key, &found);
310 
311  if (found)
312  {
313  /* found pre-existing entry */
314  *isnew = false;
315  }
316  else
317  {
318  /* created new entry */
319  *isnew = true;
320  /* zero caller data */
321  entry->additional = NULL;
322  MemoryContextSwitchTo(hashtable->tablecxt);
323  /* Copy the first tuple into the table context */
324  entry->firstTuple = ExecCopySlotMinimalTuple(slot);
325  }
326  }
327  else
328  {
329  entry = tuplehash_lookup(hashtable->hashtab, key);
330  }
331 
332  MemoryContextSwitchTo(oldContext);
333 
334  return entry;
335 }
336 
337 /*
338  * Search for a hashtable entry matching the given tuple. No entry is
339  * created if there's not a match. This is similar to the non-creating
340  * case of LookupTupleHashEntry, except that it supports cross-type
341  * comparisons, in which the given tuple is not of the same type as the
342  * table entries. The caller must provide the hash functions to use for
343  * the input tuple, as well as the equality functions, since these may be
344  * different from the table's internal functions.
345  */
348  ExprState *eqcomp,
349  FmgrInfo *hashfunctions)
350 {
351  TupleHashEntry entry;
352  MemoryContext oldContext;
354 
355  /* Need to run the hash functions in short-lived context */
356  oldContext = MemoryContextSwitchTo(hashtable->tempcxt);
357 
358  /* Set up data needed by hash and match functions */
359  hashtable->inputslot = slot;
360  hashtable->in_hash_funcs = hashfunctions;
361  hashtable->cur_eq_func = eqcomp;
362 
363  /* Search the hash table */
364  key = NULL; /* flag to reference inputslot */
365  entry = tuplehash_lookup(hashtable->hashtab, key);
366  MemoryContextSwitchTo(oldContext);
367 
368  return entry;
369 }
370 
371 /*
372  * Compute the hash value for a tuple
373  *
374  * The passed-in key is a pointer to TupleHashEntryData. In an actual hash
375  * table entry, the firstTuple field points to a tuple (in MinimalTuple
376  * format). LookupTupleHashEntry sets up a dummy TupleHashEntryData with a
377  * NULL firstTuple field --- that cues us to look at the inputslot instead.
378  * This convention avoids the need to materialize virtual input tuples unless
379  * they actually need to get copied into the table.
380  *
381  * Also, the caller must select an appropriate memory context for running
382  * the hash functions. (dynahash.c doesn't change CurrentMemoryContext.)
383  */
384 static uint32
385 TupleHashTableHash(struct tuplehash_hash *tb, const MinimalTuple tuple)
386 {
387  TupleHashTable hashtable = (TupleHashTable) tb->private_data;
388  int numCols = hashtable->numCols;
389  AttrNumber *keyColIdx = hashtable->keyColIdx;
390  uint32 hashkey = hashtable->hash_iv;
391  TupleTableSlot *slot;
392  FmgrInfo *hashfunctions;
393  int i;
394 
395  if (tuple == NULL)
396  {
397  /* Process the current input tuple for the table */
398  slot = hashtable->inputslot;
399  hashfunctions = hashtable->in_hash_funcs;
400  }
401  else
402  {
403  /*
404  * Process a tuple already stored in the table.
405  *
406  * (this case never actually occurs due to the way simplehash.h is
407  * used, as the hash-value is stored in the entries)
408  */
409  slot = hashtable->tableslot;
410  ExecStoreMinimalTuple(tuple, slot, false);
411  hashfunctions = hashtable->tab_hash_funcs;
412  }
413 
414  for (i = 0; i < numCols; i++)
415  {
416  AttrNumber att = keyColIdx[i];
417  Datum attr;
418  bool isNull;
419 
420  /* rotate hashkey left 1 bit at each step */
421  hashkey = (hashkey << 1) | ((hashkey & 0x80000000) ? 1 : 0);
422 
423  attr = slot_getattr(slot, att, &isNull);
424 
425  if (!isNull) /* treat nulls as having hash key 0 */
426  {
427  uint32 hkey;
428 
429  hkey = DatumGetUInt32(FunctionCall1Coll(&hashfunctions[i],
430  hashtable->tab_collations[i],
431  attr));
432  hashkey ^= hkey;
433  }
434  }
435 
436  /*
437  * The way hashes are combined above, among each other and with the IV,
438  * doesn't lead to good bit perturbation. As the IV's goal is to lead to
439  * achieve that, perform a round of hashing of the combined hash -
440  * resulting in near perfect perturbation.
441  */
442  return murmurhash32(hashkey);
443 }
444 
445 /*
446  * See whether two tuples (presumably of the same hash value) match
447  *
448  * As above, the passed pointers are pointers to TupleHashEntryData.
449  */
450 static int
451 TupleHashTableMatch(struct tuplehash_hash *tb, const MinimalTuple tuple1, const MinimalTuple tuple2)
452 {
453  TupleTableSlot *slot1;
454  TupleTableSlot *slot2;
455  TupleHashTable hashtable = (TupleHashTable) tb->private_data;
456  ExprContext *econtext = hashtable->exprcontext;
457 
458  /*
459  * We assume that simplehash.h will only ever call us with the first
460  * argument being an actual table entry, and the second argument being
461  * LookupTupleHashEntry's dummy TupleHashEntryData. The other direction
462  * could be supported too, but is not currently required.
463  */
464  Assert(tuple1 != NULL);
465  slot1 = hashtable->tableslot;
466  ExecStoreMinimalTuple(tuple1, slot1, false);
467  Assert(tuple2 == NULL);
468  slot2 = hashtable->inputslot;
469 
470  /* For crosstype comparisons, the inputslot must be first */
471  econtext->ecxt_innertuple = slot2;
472  econtext->ecxt_outertuple = slot1;
473  return !ExecQualAndReset(hashtable->cur_eq_func, econtext);
474 }
ExprContext * exprcontext
Definition: execnodes.h:708
#define DatumGetUInt32(X)
Definition: postgres.h:486
ExprContext * CreateStandaloneExprContext(void)
Definition: execUtils.c:316
Definition: fmgr.h:56
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
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:507
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1411
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1203
ExprState * ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc, const TupleTableSlotOps *lops, const TupleTableSlotOps *rops, int numCols, const AttrNumber *keyColIdx, const Oid *eqfunctions, const Oid *collations, PlanState *parent)
Definition: execExpr.c:3316
TupleTableSlot * inputslot
Definition: execnodes.h:704
MinimalTuple firstTuple
Definition: execnodes.h:677
#define Min(x, y)
Definition: c.h:904
TupleHashEntry FindTupleHashEntry(TupleHashTable hashtable, TupleTableSlot *slot, ExprState *eqcomp, FmgrInfo *hashfunctions)
Definition: execGrouping.c:347
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
AttrNumber * keyColIdx
Definition: execnodes.h:695
ExprState * tab_eq_func
Definition: execnodes.h:697
ExprState * cur_eq_func
Definition: execnodes.h:706
unsigned int Oid
Definition: postgres_ext.h:31
void execTuplesHashPrepare(int numCols, const Oid *eqOperators, Oid **eqFuncOids, FmgrInfo **hashFunctions)
Definition: execGrouping.c:96
void ResetTupleHashTable(TupleHashTable hashtable)
Definition: execGrouping.c:271
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:696
static int TupleHashTableMatch(struct tuplehash_hash *tb, const MinimalTuple tuple1, const MinimalTuple tuple2)
Definition: execGrouping.c:451
FmgrInfo * in_hash_funcs
Definition: execnodes.h:705
#define ERROR
Definition: elog.h:43
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
struct TupleHashEntryData TupleHashEntryData
static MinimalTuple ExecCopySlotMinimalTuple(TupleTableSlot *slot)
Definition: tuptable.h:464
MemoryContext tablecxt
Definition: execnodes.h:699
struct TupleHashTableData * TupleHashTable
Definition: execnodes.h:673
int ParallelWorkerNumber
Definition: parallel.c:110
unsigned int uint32
Definition: c.h:358
TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew)
Definition: execGrouping.c:289
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
Definition: executor.h:392
uintptr_t Datum
Definition: postgres.h:367
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1130
int work_mem
Definition: globals.c:121
TupleHashTable BuildTupleHashTable(PlanState *parent, TupleDesc inputDesc, int numCols, AttrNumber *keyColIdx, const Oid *eqfuncoids, FmgrInfo *hashfunctions, Oid *collations, long nbuckets, Size additionalsize, MemoryContext tablecxt, MemoryContext tempcxt, bool use_variable_hash_iv)
Definition: execGrouping.c:241
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:382
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1092
tuplehash_hash * hashtab
Definition: execnodes.h:693
#define Assert(condition)
Definition: c.h:732
TupleTableSlot * tableslot
Definition: execnodes.h:702
ExprState * execTuplesMatchPrepare(TupleDesc desc, int numCols, const AttrNumber *keyColIdx, const Oid *eqOperators, const Oid *collations, PlanState *parent)
Definition: execGrouping.c:59
size_t Size
Definition: c.h:466
static uint32 TupleHashTableHash(struct tuplehash_hash *tb, const MinimalTuple tuple)
Definition: execGrouping.c:385
void * palloc(Size size)
Definition: mcxt.c:924
#define elog(elevel,...)
Definition: elog.h:226
int i
static uint32 murmurhash32(uint32 data)
Definition: hashutils.h:60
MemoryContext tempcxt
Definition: execnodes.h:700
int16 AttrNumber
Definition: attnum.h:21
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86