PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeHash.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodeHash.c
4  * Routines to hash relations for hashjoin
5  *
6  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/executor/nodeHash.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 /*
16  * INTERFACE ROUTINES
17  * MultiExecHash - generate an in-memory hash table of the relation
18  * ExecInitHash - initialize node and subnodes
19  * ExecEndHash - shutdown node and subnodes
20  */
21 
22 #include "postgres.h"
23 
24 #include <math.h>
25 #include <limits.h>
26 
27 #include "access/htup_details.h"
28 #include "catalog/pg_statistic.h"
29 #include "commands/tablespace.h"
30 #include "executor/execdebug.h"
31 #include "executor/hashjoin.h"
32 #include "executor/nodeHash.h"
33 #include "executor/nodeHashjoin.h"
34 #include "miscadmin.h"
35 #include "utils/dynahash.h"
36 #include "utils/memutils.h"
37 #include "utils/lsyscache.h"
38 #include "utils/syscache.h"
39 
40 
41 static void ExecHashIncreaseNumBatches(HashJoinTable hashtable);
42 static void ExecHashIncreaseNumBuckets(HashJoinTable hashtable);
43 static void ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node,
44  int mcvsToUse);
45 static void ExecHashSkewTableInsert(HashJoinTable hashtable,
46  TupleTableSlot *slot,
47  uint32 hashvalue,
48  int bucketNumber);
49 static void ExecHashRemoveNextSkewBucket(HashJoinTable hashtable);
50 
51 static void *dense_alloc(HashJoinTable hashtable, Size size);
52 
53 /* ----------------------------------------------------------------
54  * ExecHash
55  *
56  * stub for pro forma compliance
57  * ----------------------------------------------------------------
58  */
61 {
62  elog(ERROR, "Hash node does not support ExecProcNode call convention");
63  return NULL;
64 }
65 
66 /* ----------------------------------------------------------------
67  * MultiExecHash
68  *
69  * build hash table for hashjoin, doing partitioning if more
70  * than one batch is required.
71  * ----------------------------------------------------------------
72  */
73 Node *
75 {
76  PlanState *outerNode;
77  List *hashkeys;
78  HashJoinTable hashtable;
79  TupleTableSlot *slot;
80  ExprContext *econtext;
81  uint32 hashvalue;
82 
83  /* must provide our own instrumentation support */
84  if (node->ps.instrument)
86 
87  /*
88  * get state info from node
89  */
90  outerNode = outerPlanState(node);
91  hashtable = node->hashtable;
92 
93  /*
94  * set expression context
95  */
96  hashkeys = node->hashkeys;
97  econtext = node->ps.ps_ExprContext;
98 
99  /*
100  * get all inner tuples and insert into the hash table (or temp files)
101  */
102  for (;;)
103  {
104  slot = ExecProcNode(outerNode);
105  if (TupIsNull(slot))
106  break;
107  /* We have to compute the hash value */
108  econtext->ecxt_innertuple = slot;
109  if (ExecHashGetHashValue(hashtable, econtext, hashkeys,
110  false, hashtable->keepNulls,
111  &hashvalue))
112  {
113  int bucketNumber;
114 
115  bucketNumber = ExecHashGetSkewBucket(hashtable, hashvalue);
116  if (bucketNumber != INVALID_SKEW_BUCKET_NO)
117  {
118  /* It's a skew tuple, so put it into that hash table */
119  ExecHashSkewTableInsert(hashtable, slot, hashvalue,
120  bucketNumber);
121  hashtable->skewTuples += 1;
122  }
123  else
124  {
125  /* Not subject to skew optimization, so insert normally */
126  ExecHashTableInsert(hashtable, slot, hashvalue);
127  }
128  hashtable->totalTuples += 1;
129  }
130  }
131 
132  /* resize the hash table if needed (NTUP_PER_BUCKET exceeded) */
133  if (hashtable->nbuckets != hashtable->nbuckets_optimal)
134  ExecHashIncreaseNumBuckets(hashtable);
135 
136  /* Account for the buckets in spaceUsed (reported in EXPLAIN ANALYZE) */
137  hashtable->spaceUsed += hashtable->nbuckets * sizeof(HashJoinTuple);
138  if (hashtable->spaceUsed > hashtable->spacePeak)
139  hashtable->spacePeak = hashtable->spaceUsed;
140 
141  /* must provide our own instrumentation support */
142  if (node->ps.instrument)
143  InstrStopNode(node->ps.instrument, hashtable->totalTuples);
144 
145  /*
146  * We do not return the hash table directly because it's not a subtype of
147  * Node, and so would violate the MultiExecProcNode API. Instead, our
148  * parent Hashjoin node is expected to know how to fish it out of our node
149  * state. Ugly but not really worth cleaning up, since Hashjoin knows
150  * quite a bit more about Hash besides that.
151  */
152  return NULL;
153 }
154 
155 /* ----------------------------------------------------------------
156  * ExecInitHash
157  *
158  * Init routine for Hash node
159  * ----------------------------------------------------------------
160  */
161 HashState *
162 ExecInitHash(Hash *node, EState *estate, int eflags)
163 {
164  HashState *hashstate;
165 
166  /* check for unsupported flags */
167  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
168 
169  /*
170  * create state structure
171  */
172  hashstate = makeNode(HashState);
173  hashstate->ps.plan = (Plan *) node;
174  hashstate->ps.state = estate;
175  hashstate->hashtable = NULL;
176  hashstate->hashkeys = NIL; /* will be set by parent HashJoin */
177 
178  /*
179  * Miscellaneous initialization
180  *
181  * create expression context for node
182  */
183  ExecAssignExprContext(estate, &hashstate->ps);
184 
185  /*
186  * initialize our result slot
187  */
188  ExecInitResultTupleSlot(estate, &hashstate->ps);
189 
190  /*
191  * initialize child expressions
192  */
193  hashstate->ps.targetlist = (List *)
194  ExecInitExpr((Expr *) node->plan.targetlist,
195  (PlanState *) hashstate);
196  hashstate->ps.qual = (List *)
197  ExecInitExpr((Expr *) node->plan.qual,
198  (PlanState *) hashstate);
199 
200  /*
201  * initialize child nodes
202  */
203  outerPlanState(hashstate) = ExecInitNode(outerPlan(node), estate, eflags);
204 
205  /*
206  * initialize tuple type. no need to initialize projection info because
207  * this node doesn't do projections
208  */
209  ExecAssignResultTypeFromTL(&hashstate->ps);
210  hashstate->ps.ps_ProjInfo = NULL;
211 
212  return hashstate;
213 }
214 
215 /* ---------------------------------------------------------------
216  * ExecEndHash
217  *
218  * clean up routine for Hash node
219  * ----------------------------------------------------------------
220  */
221 void
223 {
225 
226  /*
227  * free exprcontext
228  */
229  ExecFreeExprContext(&node->ps);
230 
231  /*
232  * shut down the subplan
233  */
234  outerPlan = outerPlanState(node);
235  ExecEndNode(outerPlan);
236 }
237 
238 
239 /* ----------------------------------------------------------------
240  * ExecHashTableCreate
241  *
242  * create an empty hashtable data structure for hashjoin.
243  * ----------------------------------------------------------------
244  */
246 ExecHashTableCreate(Hash *node, List *hashOperators, bool keepNulls)
247 {
248  HashJoinTable hashtable;
249  Plan *outerNode;
250  int nbuckets;
251  int nbatch;
252  int num_skew_mcvs;
253  int log2_nbuckets;
254  int nkeys;
255  int i;
256  ListCell *ho;
257  MemoryContext oldcxt;
258 
259  /*
260  * Get information about the size of the relation to be hashed (it's the
261  * "outer" subtree of this node, but the inner relation of the hashjoin).
262  * Compute the appropriate size of the hash table.
263  */
264  outerNode = outerPlan(node);
265 
266  ExecChooseHashTableSize(outerNode->plan_rows, outerNode->plan_width,
267  OidIsValid(node->skewTable),
268  &nbuckets, &nbatch, &num_skew_mcvs);
269 
270  /* nbuckets must be a power of 2 */
271  log2_nbuckets = my_log2(nbuckets);
272  Assert(nbuckets == (1 << log2_nbuckets));
273 
274  /*
275  * Initialize the hash table control block.
276  *
277  * The hashtable control block is just palloc'd from the executor's
278  * per-query memory context.
279  */
280  hashtable = (HashJoinTable) palloc(sizeof(HashJoinTableData));
281  hashtable->nbuckets = nbuckets;
282  hashtable->nbuckets_original = nbuckets;
283  hashtable->nbuckets_optimal = nbuckets;
284  hashtable->log2_nbuckets = log2_nbuckets;
285  hashtable->log2_nbuckets_optimal = log2_nbuckets;
286  hashtable->buckets = NULL;
287  hashtable->keepNulls = keepNulls;
288  hashtable->skewEnabled = false;
289  hashtable->skewBucket = NULL;
290  hashtable->skewBucketLen = 0;
291  hashtable->nSkewBuckets = 0;
292  hashtable->skewBucketNums = NULL;
293  hashtable->nbatch = nbatch;
294  hashtable->curbatch = 0;
295  hashtable->nbatch_original = nbatch;
296  hashtable->nbatch_outstart = nbatch;
297  hashtable->growEnabled = true;
298  hashtable->totalTuples = 0;
299  hashtable->skewTuples = 0;
300  hashtable->innerBatchFile = NULL;
301  hashtable->outerBatchFile = NULL;
302  hashtable->spaceUsed = 0;
303  hashtable->spacePeak = 0;
304  hashtable->spaceAllowed = work_mem * 1024L;
305  hashtable->spaceUsedSkew = 0;
306  hashtable->spaceAllowedSkew =
307  hashtable->spaceAllowed * SKEW_WORK_MEM_PERCENT / 100;
308  hashtable->chunks = NULL;
309 
310 #ifdef HJDEBUG
311  printf("Hashjoin %p: initial nbatch = %d, nbuckets = %d\n",
312  hashtable, nbatch, nbuckets);
313 #endif
314 
315  /*
316  * Get info about the hash functions to be used for each hash key. Also
317  * remember whether the join operators are strict.
318  */
319  nkeys = list_length(hashOperators);
320  hashtable->outer_hashfunctions =
321  (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
322  hashtable->inner_hashfunctions =
323  (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
324  hashtable->hashStrict = (bool *) palloc(nkeys * sizeof(bool));
325  i = 0;
326  foreach(ho, hashOperators)
327  {
328  Oid hashop = lfirst_oid(ho);
329  Oid left_hashfn;
330  Oid right_hashfn;
331 
332  if (!get_op_hash_functions(hashop, &left_hashfn, &right_hashfn))
333  elog(ERROR, "could not find hash function for hash operator %u",
334  hashop);
335  fmgr_info(left_hashfn, &hashtable->outer_hashfunctions[i]);
336  fmgr_info(right_hashfn, &hashtable->inner_hashfunctions[i]);
337  hashtable->hashStrict[i] = op_strict(hashop);
338  i++;
339  }
340 
341  /*
342  * Create temporary memory contexts in which to keep the hashtable working
343  * storage. See notes in executor/hashjoin.h.
344  */
346  "HashTableContext",
350 
351  hashtable->batchCxt = AllocSetContextCreate(hashtable->hashCxt,
352  "HashBatchContext",
356 
357  /* Allocate data that will live for the life of the hashjoin */
358 
359  oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
360 
361  if (nbatch > 1)
362  {
363  /*
364  * allocate and initialize the file arrays in hashCxt
365  */
366  hashtable->innerBatchFile = (BufFile **)
367  palloc0(nbatch * sizeof(BufFile *));
368  hashtable->outerBatchFile = (BufFile **)
369  palloc0(nbatch * sizeof(BufFile *));
370  /* The files will not be opened until needed... */
371  /* ... but make sure we have temp tablespaces established for them */
373  }
374 
375  /*
376  * Prepare context for the first-scan space allocations; allocate the
377  * hashbucket array therein, and set each bucket "empty".
378  */
379  MemoryContextSwitchTo(hashtable->batchCxt);
380 
381  hashtable->buckets = (HashJoinTuple *)
382  palloc0(nbuckets * sizeof(HashJoinTuple));
383 
384  /*
385  * Set up for skew optimization, if possible and there's a need for more
386  * than one batch. (In a one-batch join, there's no point in it.)
387  */
388  if (nbatch > 1)
389  ExecHashBuildSkewHash(hashtable, node, num_skew_mcvs);
390 
391  MemoryContextSwitchTo(oldcxt);
392 
393  return hashtable;
394 }
395 
396 
397 /*
398  * Compute appropriate size for hashtable given the estimated size of the
399  * relation to be hashed (number of rows and average row width).
400  *
401  * This is exported so that the planner's costsize.c can use it.
402  */
403 
404 /* Target bucket loading (tuples per bucket) */
405 #define NTUP_PER_BUCKET 1
406 
407 void
408 ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
409  int *numbuckets,
410  int *numbatches,
411  int *num_skew_mcvs)
412 {
413  int tupsize;
414  double inner_rel_bytes;
415  long bucket_bytes;
416  long hash_table_bytes;
417  long skew_table_bytes;
418  long max_pointers;
419  long mppow2;
420  int nbatch = 1;
421  int nbuckets;
422  double dbuckets;
423 
424  /* Force a plausible relation size if no info */
425  if (ntuples <= 0.0)
426  ntuples = 1000.0;
427 
428  /*
429  * Estimate tupsize based on footprint of tuple in hashtable... note this
430  * does not allow for any palloc overhead. The manipulations of spaceUsed
431  * don't count palloc overhead either.
432  */
433  tupsize = HJTUPLE_OVERHEAD +
435  MAXALIGN(tupwidth);
436  inner_rel_bytes = ntuples * tupsize;
437 
438  /*
439  * Target in-memory hashtable size is work_mem kilobytes.
440  */
441  hash_table_bytes = work_mem * 1024L;
442 
443  /*
444  * If skew optimization is possible, estimate the number of skew buckets
445  * that will fit in the memory allowed, and decrement the assumed space
446  * available for the main hash table accordingly.
447  *
448  * We make the optimistic assumption that each skew bucket will contain
449  * one inner-relation tuple. If that turns out to be low, we will recover
450  * at runtime by reducing the number of skew buckets.
451  *
452  * hashtable->skewBucket will have up to 8 times as many HashSkewBucket
453  * pointers as the number of MCVs we allow, since ExecHashBuildSkewHash
454  * will round up to the next power of 2 and then multiply by 4 to reduce
455  * collisions.
456  */
457  if (useskew)
458  {
459  skew_table_bytes = hash_table_bytes * SKEW_WORK_MEM_PERCENT / 100;
460 
461  /*----------
462  * Divisor is:
463  * size of a hash tuple +
464  * worst-case size of skewBucket[] per MCV +
465  * size of skewBucketNums[] entry +
466  * size of skew bucket struct itself
467  *----------
468  */
469  *num_skew_mcvs = skew_table_bytes / (tupsize +
470  (8 * sizeof(HashSkewBucket *)) +
471  sizeof(int) +
473  if (*num_skew_mcvs > 0)
474  hash_table_bytes -= skew_table_bytes;
475  }
476  else
477  *num_skew_mcvs = 0;
478 
479  /*
480  * Set nbuckets to achieve an average bucket load of NTUP_PER_BUCKET when
481  * memory is filled, assuming a single batch; but limit the value so that
482  * the pointer arrays we'll try to allocate do not exceed work_mem nor
483  * MaxAllocSize.
484  *
485  * Note that both nbuckets and nbatch must be powers of 2 to make
486  * ExecHashGetBucketAndBatch fast.
487  */
488  max_pointers = (work_mem * 1024L) / sizeof(HashJoinTuple);
489  max_pointers = Min(max_pointers, MaxAllocSize / sizeof(HashJoinTuple));
490  /* If max_pointers isn't a power of 2, must round it down to one */
491  mppow2 = 1L << my_log2(max_pointers);
492  if (max_pointers != mppow2)
493  max_pointers = mppow2 / 2;
494 
495  /* Also ensure we avoid integer overflow in nbatch and nbuckets */
496  /* (this step is redundant given the current value of MaxAllocSize) */
497  max_pointers = Min(max_pointers, INT_MAX / 2);
498 
499  dbuckets = ceil(ntuples / NTUP_PER_BUCKET);
500  dbuckets = Min(dbuckets, max_pointers);
501  nbuckets = (int) dbuckets;
502  /* don't let nbuckets be really small, though ... */
503  nbuckets = Max(nbuckets, 1024);
504  /* ... and force it to be a power of 2. */
505  nbuckets = 1 << my_log2(nbuckets);
506 
507  /*
508  * If there's not enough space to store the projected number of tuples and
509  * the required bucket headers, we will need multiple batches.
510  */
511  bucket_bytes = sizeof(HashJoinTuple) * nbuckets;
512  if (inner_rel_bytes + bucket_bytes > hash_table_bytes)
513  {
514  /* We'll need multiple batches */
515  long lbuckets;
516  double dbatch;
517  int minbatch;
518  long bucket_size;
519 
520  /*
521  * Estimate the number of buckets we'll want to have when work_mem is
522  * entirely full. Each bucket will contain a bucket pointer plus
523  * NTUP_PER_BUCKET tuples, whose projected size already includes
524  * overhead for the hash code, pointer to the next tuple, etc.
525  */
526  bucket_size = (tupsize * NTUP_PER_BUCKET + sizeof(HashJoinTuple));
527  lbuckets = 1L << my_log2(hash_table_bytes / bucket_size);
528  lbuckets = Min(lbuckets, max_pointers);
529  nbuckets = (int) lbuckets;
530  nbuckets = 1 << my_log2(nbuckets);
531  bucket_bytes = nbuckets * sizeof(HashJoinTuple);
532 
533  /*
534  * Buckets are simple pointers to hashjoin tuples, while tupsize
535  * includes the pointer, hash code, and MinimalTupleData. So buckets
536  * should never really exceed 25% of work_mem (even for
537  * NTUP_PER_BUCKET=1); except maybe for work_mem values that are not
538  * 2^N bytes, where we might get more because of doubling. So let's
539  * look for 50% here.
540  */
541  Assert(bucket_bytes <= hash_table_bytes / 2);
542 
543  /* Calculate required number of batches. */
544  dbatch = ceil(inner_rel_bytes / (hash_table_bytes - bucket_bytes));
545  dbatch = Min(dbatch, max_pointers);
546  minbatch = (int) dbatch;
547  nbatch = 2;
548  while (nbatch < minbatch)
549  nbatch <<= 1;
550  }
551 
552  Assert(nbuckets > 0);
553  Assert(nbatch > 0);
554 
555  *numbuckets = nbuckets;
556  *numbatches = nbatch;
557 }
558 
559 
560 /* ----------------------------------------------------------------
561  * ExecHashTableDestroy
562  *
563  * destroy a hash table
564  * ----------------------------------------------------------------
565  */
566 void
568 {
569  int i;
570 
571  /*
572  * Make sure all the temp files are closed. We skip batch 0, since it
573  * can't have any temp files (and the arrays might not even exist if
574  * nbatch is only 1).
575  */
576  for (i = 1; i < hashtable->nbatch; i++)
577  {
578  if (hashtable->innerBatchFile[i])
579  BufFileClose(hashtable->innerBatchFile[i]);
580  if (hashtable->outerBatchFile[i])
581  BufFileClose(hashtable->outerBatchFile[i]);
582  }
583 
584  /* Release working memory (batchCxt is a child, so it goes away too) */
585  MemoryContextDelete(hashtable->hashCxt);
586 
587  /* And drop the control block */
588  pfree(hashtable);
589 }
590 
591 /*
592  * ExecHashIncreaseNumBatches
593  * increase the original number of batches in order to reduce
594  * current memory consumption
595  */
596 static void
598 {
599  int oldnbatch = hashtable->nbatch;
600  int curbatch = hashtable->curbatch;
601  int nbatch;
602  MemoryContext oldcxt;
603  long ninmemory;
604  long nfreed;
605  HashMemoryChunk oldchunks;
606 
607  /* do nothing if we've decided to shut off growth */
608  if (!hashtable->growEnabled)
609  return;
610 
611  /* safety check to avoid overflow */
612  if (oldnbatch > Min(INT_MAX / 2, MaxAllocSize / (sizeof(void *) * 2)))
613  return;
614 
615  nbatch = oldnbatch * 2;
616  Assert(nbatch > 1);
617 
618 #ifdef HJDEBUG
619  printf("Hashjoin %p: increasing nbatch to %d because space = %zu\n",
620  hashtable, nbatch, hashtable->spaceUsed);
621 #endif
622 
623  oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
624 
625  if (hashtable->innerBatchFile == NULL)
626  {
627  /* we had no file arrays before */
628  hashtable->innerBatchFile = (BufFile **)
629  palloc0(nbatch * sizeof(BufFile *));
630  hashtable->outerBatchFile = (BufFile **)
631  palloc0(nbatch * sizeof(BufFile *));
632  /* time to establish the temp tablespaces, too */
634  }
635  else
636  {
637  /* enlarge arrays and zero out added entries */
638  hashtable->innerBatchFile = (BufFile **)
639  repalloc(hashtable->innerBatchFile, nbatch * sizeof(BufFile *));
640  hashtable->outerBatchFile = (BufFile **)
641  repalloc(hashtable->outerBatchFile, nbatch * sizeof(BufFile *));
642  MemSet(hashtable->innerBatchFile + oldnbatch, 0,
643  (nbatch - oldnbatch) * sizeof(BufFile *));
644  MemSet(hashtable->outerBatchFile + oldnbatch, 0,
645  (nbatch - oldnbatch) * sizeof(BufFile *));
646  }
647 
648  MemoryContextSwitchTo(oldcxt);
649 
650  hashtable->nbatch = nbatch;
651 
652  /*
653  * Scan through the existing hash table entries and dump out any that are
654  * no longer of the current batch.
655  */
656  ninmemory = nfreed = 0;
657 
658  /* If know we need to resize nbuckets, we can do it while rebatching. */
659  if (hashtable->nbuckets_optimal != hashtable->nbuckets)
660  {
661  /* we never decrease the number of buckets */
662  Assert(hashtable->nbuckets_optimal > hashtable->nbuckets);
663 
664  hashtable->nbuckets = hashtable->nbuckets_optimal;
665  hashtable->log2_nbuckets = hashtable->log2_nbuckets_optimal;
666 
667  hashtable->buckets = repalloc(hashtable->buckets,
668  sizeof(HashJoinTuple) * hashtable->nbuckets);
669  }
670 
671  /*
672  * We will scan through the chunks directly, so that we can reset the
673  * buckets now and not have to keep track which tuples in the buckets have
674  * already been processed. We will free the old chunks as we go.
675  */
676  memset(hashtable->buckets, 0, sizeof(HashJoinTuple) * hashtable->nbuckets);
677  oldchunks = hashtable->chunks;
678  hashtable->chunks = NULL;
679 
680  /* so, let's scan through the old chunks, and all tuples in each chunk */
681  while (oldchunks != NULL)
682  {
683  HashMemoryChunk nextchunk = oldchunks->next;
684 
685  /* position within the buffer (up to oldchunks->used) */
686  size_t idx = 0;
687 
688  /* process all tuples stored in this chunk (and then free it) */
689  while (idx < oldchunks->used)
690  {
691  HashJoinTuple hashTuple = (HashJoinTuple) (oldchunks->data + idx);
692  MinimalTuple tuple = HJTUPLE_MINTUPLE(hashTuple);
693  int hashTupleSize = (HJTUPLE_OVERHEAD + tuple->t_len);
694  int bucketno;
695  int batchno;
696 
697  ninmemory++;
698  ExecHashGetBucketAndBatch(hashtable, hashTuple->hashvalue,
699  &bucketno, &batchno);
700 
701  if (batchno == curbatch)
702  {
703  /* keep tuple in memory - copy it into the new chunk */
704  HashJoinTuple copyTuple;
705 
706  copyTuple = (HashJoinTuple) dense_alloc(hashtable, hashTupleSize);
707  memcpy(copyTuple, hashTuple, hashTupleSize);
708 
709  /* and add it back to the appropriate bucket */
710  copyTuple->next = hashtable->buckets[bucketno];
711  hashtable->buckets[bucketno] = copyTuple;
712  }
713  else
714  {
715  /* dump it out */
716  Assert(batchno > curbatch);
718  hashTuple->hashvalue,
719  &hashtable->innerBatchFile[batchno]);
720 
721  hashtable->spaceUsed -= hashTupleSize;
722  nfreed++;
723  }
724 
725  /* next tuple in this chunk */
726  idx += MAXALIGN(hashTupleSize);
727  }
728 
729  /* we're done with this chunk - free it and proceed to the next one */
730  pfree(oldchunks);
731  oldchunks = nextchunk;
732  }
733 
734 #ifdef HJDEBUG
735  printf("Hashjoin %p: freed %ld of %ld tuples, space now %zu\n",
736  hashtable, nfreed, ninmemory, hashtable->spaceUsed);
737 #endif
738 
739  /*
740  * If we dumped out either all or none of the tuples in the table, disable
741  * further expansion of nbatch. This situation implies that we have
742  * enough tuples of identical hashvalues to overflow spaceAllowed.
743  * Increasing nbatch will not fix it since there's no way to subdivide the
744  * group any more finely. We have to just gut it out and hope the server
745  * has enough RAM.
746  */
747  if (nfreed == 0 || nfreed == ninmemory)
748  {
749  hashtable->growEnabled = false;
750 #ifdef HJDEBUG
751  printf("Hashjoin %p: disabling further increase of nbatch\n",
752  hashtable);
753 #endif
754  }
755 }
756 
757 /*
758  * ExecHashIncreaseNumBuckets
759  * increase the original number of buckets in order to reduce
760  * number of tuples per bucket
761  */
762 static void
764 {
765  HashMemoryChunk chunk;
766 
767  /* do nothing if not an increase (it's called increase for a reason) */
768  if (hashtable->nbuckets >= hashtable->nbuckets_optimal)
769  return;
770 
771 #ifdef HJDEBUG
772  printf("Hashjoin %p: increasing nbuckets %d => %d\n",
773  hashtable, hashtable->nbuckets, hashtable->nbuckets_optimal);
774 #endif
775 
776  hashtable->nbuckets = hashtable->nbuckets_optimal;
777  hashtable->log2_nbuckets = hashtable->log2_nbuckets_optimal;
778 
779  Assert(hashtable->nbuckets > 1);
780  Assert(hashtable->nbuckets <= (INT_MAX / 2));
781  Assert(hashtable->nbuckets == (1 << hashtable->log2_nbuckets));
782 
783  /*
784  * Just reallocate the proper number of buckets - we don't need to walk
785  * through them - we can walk the dense-allocated chunks (just like in
786  * ExecHashIncreaseNumBatches, but without all the copying into new
787  * chunks)
788  */
789  hashtable->buckets =
790  (HashJoinTuple *) repalloc(hashtable->buckets,
791  hashtable->nbuckets * sizeof(HashJoinTuple));
792 
793  memset(hashtable->buckets, 0, hashtable->nbuckets * sizeof(HashJoinTuple));
794 
795  /* scan through all tuples in all chunks to rebuild the hash table */
796  for (chunk = hashtable->chunks; chunk != NULL; chunk = chunk->next)
797  {
798  /* process all tuples stored in this chunk */
799  size_t idx = 0;
800 
801  while (idx < chunk->used)
802  {
803  HashJoinTuple hashTuple = (HashJoinTuple) (chunk->data + idx);
804  int bucketno;
805  int batchno;
806 
807  ExecHashGetBucketAndBatch(hashtable, hashTuple->hashvalue,
808  &bucketno, &batchno);
809 
810  /* add the tuple to the proper bucket */
811  hashTuple->next = hashtable->buckets[bucketno];
812  hashtable->buckets[bucketno] = hashTuple;
813 
814  /* advance index past the tuple */
815  idx += MAXALIGN(HJTUPLE_OVERHEAD +
816  HJTUPLE_MINTUPLE(hashTuple)->t_len);
817  }
818  }
819 }
820 
821 
822 /*
823  * ExecHashTableInsert
824  * insert a tuple into the hash table depending on the hash value
825  * it may just go to a temp file for later batches
826  *
827  * Note: the passed TupleTableSlot may contain a regular, minimal, or virtual
828  * tuple; the minimal case in particular is certain to happen while reloading
829  * tuples from batch files. We could save some cycles in the regular-tuple
830  * case by not forcing the slot contents into minimal form; not clear if it's
831  * worth the messiness required.
832  */
833 void
835  TupleTableSlot *slot,
836  uint32 hashvalue)
837 {
839  int bucketno;
840  int batchno;
841 
842  ExecHashGetBucketAndBatch(hashtable, hashvalue,
843  &bucketno, &batchno);
844 
845  /*
846  * decide whether to put the tuple in the hash table or a temp file
847  */
848  if (batchno == hashtable->curbatch)
849  {
850  /*
851  * put the tuple in hash table
852  */
853  HashJoinTuple hashTuple;
854  int hashTupleSize;
855  double ntuples = (hashtable->totalTuples - hashtable->skewTuples);
856 
857  /* Create the HashJoinTuple */
858  hashTupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
859  hashTuple = (HashJoinTuple) dense_alloc(hashtable, hashTupleSize);
860 
861  hashTuple->hashvalue = hashvalue;
862  memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
863 
864  /*
865  * We always reset the tuple-matched flag on insertion. This is okay
866  * even when reloading a tuple from a batch file, since the tuple
867  * could not possibly have been matched to an outer tuple before it
868  * went into the batch file.
869  */
871 
872  /* Push it onto the front of the bucket's list */
873  hashTuple->next = hashtable->buckets[bucketno];
874  hashtable->buckets[bucketno] = hashTuple;
875 
876  /*
877  * Increase the (optimal) number of buckets if we just exceeded the
878  * NTUP_PER_BUCKET threshold, but only when there's still a single
879  * batch.
880  */
881  if (hashtable->nbatch == 1 &&
882  ntuples > (hashtable->nbuckets_optimal * NTUP_PER_BUCKET))
883  {
884  /* Guard against integer overflow and alloc size overflow */
885  if (hashtable->nbuckets_optimal <= INT_MAX / 2 &&
886  hashtable->nbuckets_optimal * 2 <= MaxAllocSize / sizeof(HashJoinTuple))
887  {
888  hashtable->nbuckets_optimal *= 2;
889  hashtable->log2_nbuckets_optimal += 1;
890  }
891  }
892 
893  /* Account for space used, and back off if we've used too much */
894  hashtable->spaceUsed += hashTupleSize;
895  if (hashtable->spaceUsed > hashtable->spacePeak)
896  hashtable->spacePeak = hashtable->spaceUsed;
897  if (hashtable->spaceUsed +
898  hashtable->nbuckets_optimal * sizeof(HashJoinTuple)
899  > hashtable->spaceAllowed)
900  ExecHashIncreaseNumBatches(hashtable);
901  }
902  else
903  {
904  /*
905  * put the tuple into a temp file for later batches
906  */
907  Assert(batchno > hashtable->curbatch);
908  ExecHashJoinSaveTuple(tuple,
909  hashvalue,
910  &hashtable->innerBatchFile[batchno]);
911  }
912 }
913 
914 /*
915  * ExecHashGetHashValue
916  * Compute the hash value for a tuple
917  *
918  * The tuple to be tested must be in either econtext->ecxt_outertuple or
919  * econtext->ecxt_innertuple. Vars in the hashkeys expressions should have
920  * varno either OUTER_VAR or INNER_VAR.
921  *
922  * A TRUE result means the tuple's hash value has been successfully computed
923  * and stored at *hashvalue. A FALSE result means the tuple cannot match
924  * because it contains a null attribute, and hence it should be discarded
925  * immediately. (If keep_nulls is true then FALSE is never returned.)
926  */
927 bool
929  ExprContext *econtext,
930  List *hashkeys,
931  bool outer_tuple,
932  bool keep_nulls,
933  uint32 *hashvalue)
934 {
935  uint32 hashkey = 0;
936  FmgrInfo *hashfunctions;
937  ListCell *hk;
938  int i = 0;
939  MemoryContext oldContext;
940 
941  /*
942  * We reset the eval context each time to reclaim any memory leaked in the
943  * hashkey expressions.
944  */
945  ResetExprContext(econtext);
946 
947  oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
948 
949  if (outer_tuple)
950  hashfunctions = hashtable->outer_hashfunctions;
951  else
952  hashfunctions = hashtable->inner_hashfunctions;
953 
954  foreach(hk, hashkeys)
955  {
956  ExprState *keyexpr = (ExprState *) lfirst(hk);
957  Datum keyval;
958  bool isNull;
959 
960  /* rotate hashkey left 1 bit at each step */
961  hashkey = (hashkey << 1) | ((hashkey & 0x80000000) ? 1 : 0);
962 
963  /*
964  * Get the join attribute value of the tuple
965  */
966  keyval = ExecEvalExpr(keyexpr, econtext, &isNull, NULL);
967 
968  /*
969  * If the attribute is NULL, and the join operator is strict, then
970  * this tuple cannot pass the join qual so we can reject it
971  * immediately (unless we're scanning the outside of an outer join, in
972  * which case we must not reject it). Otherwise we act like the
973  * hashcode of NULL is zero (this will support operators that act like
974  * IS NOT DISTINCT, though not any more-random behavior). We treat
975  * the hash support function as strict even if the operator is not.
976  *
977  * Note: currently, all hashjoinable operators must be strict since
978  * the hash index AM assumes that. However, it takes so little extra
979  * code here to allow non-strict that we may as well do it.
980  */
981  if (isNull)
982  {
983  if (hashtable->hashStrict[i] && !keep_nulls)
984  {
985  MemoryContextSwitchTo(oldContext);
986  return false; /* cannot match */
987  }
988  /* else, leave hashkey unmodified, equivalent to hashcode 0 */
989  }
990  else
991  {
992  /* Compute the hash function */
993  uint32 hkey;
994 
995  hkey = DatumGetUInt32(FunctionCall1(&hashfunctions[i], keyval));
996  hashkey ^= hkey;
997  }
998 
999  i++;
1000  }
1001 
1002  MemoryContextSwitchTo(oldContext);
1003 
1004  *hashvalue = hashkey;
1005  return true;
1006 }
1007 
1008 /*
1009  * ExecHashGetBucketAndBatch
1010  * Determine the bucket number and batch number for a hash value
1011  *
1012  * Note: on-the-fly increases of nbatch must not change the bucket number
1013  * for a given hash code (since we don't move tuples to different hash
1014  * chains), and must only cause the batch number to remain the same or
1015  * increase. Our algorithm is
1016  * bucketno = hashvalue MOD nbuckets
1017  * batchno = (hashvalue DIV nbuckets) MOD nbatch
1018  * where nbuckets and nbatch are both expected to be powers of 2, so we can
1019  * do the computations by shifting and masking. (This assumes that all hash
1020  * functions are good about randomizing all their output bits, else we are
1021  * likely to have very skewed bucket or batch occupancy.)
1022  *
1023  * nbuckets and log2_nbuckets may change while nbatch == 1 because of dynamic
1024  * bucket count growth. Once we start batching, the value is fixed and does
1025  * not change over the course of the join (making it possible to compute batch
1026  * number the way we do here).
1027  *
1028  * nbatch is always a power of 2; we increase it only by doubling it. This
1029  * effectively adds one more bit to the top of the batchno.
1030  */
1031 void
1033  uint32 hashvalue,
1034  int *bucketno,
1035  int *batchno)
1036 {
1037  uint32 nbuckets = (uint32) hashtable->nbuckets;
1038  uint32 nbatch = (uint32) hashtable->nbatch;
1039 
1040  if (nbatch > 1)
1041  {
1042  /* we can do MOD by masking, DIV by shifting */
1043  *bucketno = hashvalue & (nbuckets - 1);
1044  *batchno = (hashvalue >> hashtable->log2_nbuckets) & (nbatch - 1);
1045  }
1046  else
1047  {
1048  *bucketno = hashvalue & (nbuckets - 1);
1049  *batchno = 0;
1050  }
1051 }
1052 
1053 /*
1054  * ExecScanHashBucket
1055  * scan a hash bucket for matches to the current outer tuple
1056  *
1057  * The current outer tuple must be stored in econtext->ecxt_outertuple.
1058  *
1059  * On success, the inner tuple is stored into hjstate->hj_CurTuple and
1060  * econtext->ecxt_innertuple, using hjstate->hj_HashTupleSlot as the slot
1061  * for the latter.
1062  */
1063 bool
1065  ExprContext *econtext)
1066 {
1067  List *hjclauses = hjstate->hashclauses;
1068  HashJoinTable hashtable = hjstate->hj_HashTable;
1069  HashJoinTuple hashTuple = hjstate->hj_CurTuple;
1070  uint32 hashvalue = hjstate->hj_CurHashValue;
1071 
1072  /*
1073  * hj_CurTuple is the address of the tuple last returned from the current
1074  * bucket, or NULL if it's time to start scanning a new bucket.
1075  *
1076  * If the tuple hashed to a skew bucket then scan the skew bucket
1077  * otherwise scan the standard hashtable bucket.
1078  */
1079  if (hashTuple != NULL)
1080  hashTuple = hashTuple->next;
1081  else if (hjstate->hj_CurSkewBucketNo != INVALID_SKEW_BUCKET_NO)
1082  hashTuple = hashtable->skewBucket[hjstate->hj_CurSkewBucketNo]->tuples;
1083  else
1084  hashTuple = hashtable->buckets[hjstate->hj_CurBucketNo];
1085 
1086  while (hashTuple != NULL)
1087  {
1088  if (hashTuple->hashvalue == hashvalue)
1089  {
1090  TupleTableSlot *inntuple;
1091 
1092  /* insert hashtable's tuple into exec slot so ExecQual sees it */
1093  inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
1094  hjstate->hj_HashTupleSlot,
1095  false); /* do not pfree */
1096  econtext->ecxt_innertuple = inntuple;
1097 
1098  /* reset temp memory each time to avoid leaks from qual expr */
1099  ResetExprContext(econtext);
1100 
1101  if (ExecQual(hjclauses, econtext, false))
1102  {
1103  hjstate->hj_CurTuple = hashTuple;
1104  return true;
1105  }
1106  }
1107 
1108  hashTuple = hashTuple->next;
1109  }
1110 
1111  /*
1112  * no match
1113  */
1114  return false;
1115 }
1116 
1117 /*
1118  * ExecPrepHashTableForUnmatched
1119  * set up for a series of ExecScanHashTableForUnmatched calls
1120  */
1121 void
1123 {
1124  /*----------
1125  * During this scan we use the HashJoinState fields as follows:
1126  *
1127  * hj_CurBucketNo: next regular bucket to scan
1128  * hj_CurSkewBucketNo: next skew bucket (an index into skewBucketNums)
1129  * hj_CurTuple: last tuple returned, or NULL to start next bucket
1130  *----------
1131  */
1132  hjstate->hj_CurBucketNo = 0;
1133  hjstate->hj_CurSkewBucketNo = 0;
1134  hjstate->hj_CurTuple = NULL;
1135 }
1136 
1137 /*
1138  * ExecScanHashTableForUnmatched
1139  * scan the hash table for unmatched inner tuples
1140  *
1141  * On success, the inner tuple is stored into hjstate->hj_CurTuple and
1142  * econtext->ecxt_innertuple, using hjstate->hj_HashTupleSlot as the slot
1143  * for the latter.
1144  */
1145 bool
1147 {
1148  HashJoinTable hashtable = hjstate->hj_HashTable;
1149  HashJoinTuple hashTuple = hjstate->hj_CurTuple;
1150 
1151  for (;;)
1152  {
1153  /*
1154  * hj_CurTuple is the address of the tuple last returned from the
1155  * current bucket, or NULL if it's time to start scanning a new
1156  * bucket.
1157  */
1158  if (hashTuple != NULL)
1159  hashTuple = hashTuple->next;
1160  else if (hjstate->hj_CurBucketNo < hashtable->nbuckets)
1161  {
1162  hashTuple = hashtable->buckets[hjstate->hj_CurBucketNo];
1163  hjstate->hj_CurBucketNo++;
1164  }
1165  else if (hjstate->hj_CurSkewBucketNo < hashtable->nSkewBuckets)
1166  {
1167  int j = hashtable->skewBucketNums[hjstate->hj_CurSkewBucketNo];
1168 
1169  hashTuple = hashtable->skewBucket[j]->tuples;
1170  hjstate->hj_CurSkewBucketNo++;
1171  }
1172  else
1173  break; /* finished all buckets */
1174 
1175  while (hashTuple != NULL)
1176  {
1177  if (!HeapTupleHeaderHasMatch(HJTUPLE_MINTUPLE(hashTuple)))
1178  {
1179  TupleTableSlot *inntuple;
1180 
1181  /* insert hashtable's tuple into exec slot */
1182  inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
1183  hjstate->hj_HashTupleSlot,
1184  false); /* do not pfree */
1185  econtext->ecxt_innertuple = inntuple;
1186 
1187  /*
1188  * Reset temp memory each time; although this function doesn't
1189  * do any qual eval, the caller will, so let's keep it
1190  * parallel to ExecScanHashBucket.
1191  */
1192  ResetExprContext(econtext);
1193 
1194  hjstate->hj_CurTuple = hashTuple;
1195  return true;
1196  }
1197 
1198  hashTuple = hashTuple->next;
1199  }
1200  }
1201 
1202  /*
1203  * no more unmatched tuples
1204  */
1205  return false;
1206 }
1207 
1208 /*
1209  * ExecHashTableReset
1210  *
1211  * reset hash table header for new batch
1212  */
1213 void
1215 {
1216  MemoryContext oldcxt;
1217  int nbuckets = hashtable->nbuckets;
1218 
1219  /*
1220  * Release all the hash buckets and tuples acquired in the prior pass, and
1221  * reinitialize the context for a new pass.
1222  */
1223  MemoryContextReset(hashtable->batchCxt);
1224  oldcxt = MemoryContextSwitchTo(hashtable->batchCxt);
1225 
1226  /* Reallocate and reinitialize the hash bucket headers. */
1227  hashtable->buckets = (HashJoinTuple *)
1228  palloc0(nbuckets * sizeof(HashJoinTuple));
1229 
1230  hashtable->spaceUsed = 0;
1231 
1232  MemoryContextSwitchTo(oldcxt);
1233 
1234  /* Forget the chunks (the memory was freed by the context reset above). */
1235  hashtable->chunks = NULL;
1236 }
1237 
1238 /*
1239  * ExecHashTableResetMatchFlags
1240  * Clear all the HeapTupleHeaderHasMatch flags in the table
1241  */
1242 void
1244 {
1245  HashJoinTuple tuple;
1246  int i;
1247 
1248  /* Reset all flags in the main table ... */
1249  for (i = 0; i < hashtable->nbuckets; i++)
1250  {
1251  for (tuple = hashtable->buckets[i]; tuple != NULL; tuple = tuple->next)
1253  }
1254 
1255  /* ... and the same for the skew buckets, if any */
1256  for (i = 0; i < hashtable->nSkewBuckets; i++)
1257  {
1258  int j = hashtable->skewBucketNums[i];
1259  HashSkewBucket *skewBucket = hashtable->skewBucket[j];
1260 
1261  for (tuple = skewBucket->tuples; tuple != NULL; tuple = tuple->next)
1263  }
1264 }
1265 
1266 
1267 void
1269 {
1270  /*
1271  * if chgParam of subnode is not null then plan will be re-scanned by
1272  * first ExecProcNode.
1273  */
1274  if (node->ps.lefttree->chgParam == NULL)
1275  ExecReScan(node->ps.lefttree);
1276 }
1277 
1278 
1279 /*
1280  * ExecHashBuildSkewHash
1281  *
1282  * Set up for skew optimization if we can identify the most common values
1283  * (MCVs) of the outer relation's join key. We make a skew hash bucket
1284  * for the hash value of each MCV, up to the number of slots allowed
1285  * based on available memory.
1286  */
1287 static void
1288 ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
1289 {
1290  HeapTupleData *statsTuple;
1291  Datum *values;
1292  int nvalues;
1293  float4 *numbers;
1294  int nnumbers;
1295 
1296  /* Do nothing if planner didn't identify the outer relation's join key */
1297  if (!OidIsValid(node->skewTable))
1298  return;
1299  /* Also, do nothing if we don't have room for at least one skew bucket */
1300  if (mcvsToUse <= 0)
1301  return;
1302 
1303  /*
1304  * Try to find the MCV statistics for the outer relation's join key.
1305  */
1306  statsTuple = SearchSysCache3(STATRELATTINH,
1307  ObjectIdGetDatum(node->skewTable),
1308  Int16GetDatum(node->skewColumn),
1309  BoolGetDatum(node->skewInherit));
1310  if (!HeapTupleIsValid(statsTuple))
1311  return;
1312 
1313  if (get_attstatsslot(statsTuple, node->skewColType, node->skewColTypmod,
1315  NULL,
1316  &values, &nvalues,
1317  &numbers, &nnumbers))
1318  {
1319  double frac;
1320  int nbuckets;
1321  FmgrInfo *hashfunctions;
1322  int i;
1323 
1324  if (mcvsToUse > nvalues)
1325  mcvsToUse = nvalues;
1326 
1327  /*
1328  * Calculate the expected fraction of outer relation that will
1329  * participate in the skew optimization. If this isn't at least
1330  * SKEW_MIN_OUTER_FRACTION, don't use skew optimization.
1331  */
1332  frac = 0;
1333  for (i = 0; i < mcvsToUse; i++)
1334  frac += numbers[i];
1335  if (frac < SKEW_MIN_OUTER_FRACTION)
1336  {
1338  values, nvalues, numbers, nnumbers);
1339  ReleaseSysCache(statsTuple);
1340  return;
1341  }
1342 
1343  /*
1344  * Okay, set up the skew hashtable.
1345  *
1346  * skewBucket[] is an open addressing hashtable with a power of 2 size
1347  * that is greater than the number of MCV values. (This ensures there
1348  * will be at least one null entry, so searches will always
1349  * terminate.)
1350  *
1351  * Note: this code could fail if mcvsToUse exceeds INT_MAX/8 or
1352  * MaxAllocSize/sizeof(void *)/8, but that is not currently possible
1353  * since we limit pg_statistic entries to much less than that.
1354  */
1355  nbuckets = 2;
1356  while (nbuckets <= mcvsToUse)
1357  nbuckets <<= 1;
1358  /* use two more bits just to help avoid collisions */
1359  nbuckets <<= 2;
1360 
1361  hashtable->skewEnabled = true;
1362  hashtable->skewBucketLen = nbuckets;
1363 
1364  /*
1365  * We allocate the bucket memory in the hashtable's batch context. It
1366  * is only needed during the first batch, and this ensures it will be
1367  * automatically removed once the first batch is done.
1368  */
1369  hashtable->skewBucket = (HashSkewBucket **)
1370  MemoryContextAllocZero(hashtable->batchCxt,
1371  nbuckets * sizeof(HashSkewBucket *));
1372  hashtable->skewBucketNums = (int *)
1373  MemoryContextAllocZero(hashtable->batchCxt,
1374  mcvsToUse * sizeof(int));
1375 
1376  hashtable->spaceUsed += nbuckets * sizeof(HashSkewBucket *)
1377  + mcvsToUse * sizeof(int);
1378  hashtable->spaceUsedSkew += nbuckets * sizeof(HashSkewBucket *)
1379  + mcvsToUse * sizeof(int);
1380  if (hashtable->spaceUsed > hashtable->spacePeak)
1381  hashtable->spacePeak = hashtable->spaceUsed;
1382 
1383  /*
1384  * Create a skew bucket for each MCV hash value.
1385  *
1386  * Note: it is very important that we create the buckets in order of
1387  * decreasing MCV frequency. If we have to remove some buckets, they
1388  * must be removed in reverse order of creation (see notes in
1389  * ExecHashRemoveNextSkewBucket) and we want the least common MCVs to
1390  * be removed first.
1391  */
1392  hashfunctions = hashtable->outer_hashfunctions;
1393 
1394  for (i = 0; i < mcvsToUse; i++)
1395  {
1396  uint32 hashvalue;
1397  int bucket;
1398 
1399  hashvalue = DatumGetUInt32(FunctionCall1(&hashfunctions[0],
1400  values[i]));
1401 
1402  /*
1403  * While we have not hit a hole in the hashtable and have not hit
1404  * the desired bucket, we have collided with some previous hash
1405  * value, so try the next bucket location. NB: this code must
1406  * match ExecHashGetSkewBucket.
1407  */
1408  bucket = hashvalue & (nbuckets - 1);
1409  while (hashtable->skewBucket[bucket] != NULL &&
1410  hashtable->skewBucket[bucket]->hashvalue != hashvalue)
1411  bucket = (bucket + 1) & (nbuckets - 1);
1412 
1413  /*
1414  * If we found an existing bucket with the same hashvalue, leave
1415  * it alone. It's okay for two MCVs to share a hashvalue.
1416  */
1417  if (hashtable->skewBucket[bucket] != NULL)
1418  continue;
1419 
1420  /* Okay, create a new skew bucket for this hashvalue. */
1421  hashtable->skewBucket[bucket] = (HashSkewBucket *)
1422  MemoryContextAlloc(hashtable->batchCxt,
1423  sizeof(HashSkewBucket));
1424  hashtable->skewBucket[bucket]->hashvalue = hashvalue;
1425  hashtable->skewBucket[bucket]->tuples = NULL;
1426  hashtable->skewBucketNums[hashtable->nSkewBuckets] = bucket;
1427  hashtable->nSkewBuckets++;
1428  hashtable->spaceUsed += SKEW_BUCKET_OVERHEAD;
1429  hashtable->spaceUsedSkew += SKEW_BUCKET_OVERHEAD;
1430  if (hashtable->spaceUsed > hashtable->spacePeak)
1431  hashtable->spacePeak = hashtable->spaceUsed;
1432  }
1433 
1435  values, nvalues, numbers, nnumbers);
1436  }
1437 
1438  ReleaseSysCache(statsTuple);
1439 }
1440 
1441 /*
1442  * ExecHashGetSkewBucket
1443  *
1444  * Returns the index of the skew bucket for this hashvalue,
1445  * or INVALID_SKEW_BUCKET_NO if the hashvalue is not
1446  * associated with any active skew bucket.
1447  */
1448 int
1450 {
1451  int bucket;
1452 
1453  /*
1454  * Always return INVALID_SKEW_BUCKET_NO if not doing skew optimization (in
1455  * particular, this happens after the initial batch is done).
1456  */
1457  if (!hashtable->skewEnabled)
1458  return INVALID_SKEW_BUCKET_NO;
1459 
1460  /*
1461  * Since skewBucketLen is a power of 2, we can do a modulo by ANDing.
1462  */
1463  bucket = hashvalue & (hashtable->skewBucketLen - 1);
1464 
1465  /*
1466  * While we have not hit a hole in the hashtable and have not hit the
1467  * desired bucket, we have collided with some other hash value, so try the
1468  * next bucket location.
1469  */
1470  while (hashtable->skewBucket[bucket] != NULL &&
1471  hashtable->skewBucket[bucket]->hashvalue != hashvalue)
1472  bucket = (bucket + 1) & (hashtable->skewBucketLen - 1);
1473 
1474  /*
1475  * Found the desired bucket?
1476  */
1477  if (hashtable->skewBucket[bucket] != NULL)
1478  return bucket;
1479 
1480  /*
1481  * There must not be any hashtable entry for this hash value.
1482  */
1483  return INVALID_SKEW_BUCKET_NO;
1484 }
1485 
1486 /*
1487  * ExecHashSkewTableInsert
1488  *
1489  * Insert a tuple into the skew hashtable.
1490  *
1491  * This should generally match up with the current-batch case in
1492  * ExecHashTableInsert.
1493  */
1494 static void
1496  TupleTableSlot *slot,
1497  uint32 hashvalue,
1498  int bucketNumber)
1499 {
1501  HashJoinTuple hashTuple;
1502  int hashTupleSize;
1503 
1504  /* Create the HashJoinTuple */
1505  hashTupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
1506  hashTuple = (HashJoinTuple) MemoryContextAlloc(hashtable->batchCxt,
1507  hashTupleSize);
1508  hashTuple->hashvalue = hashvalue;
1509  memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
1511 
1512  /* Push it onto the front of the skew bucket's list */
1513  hashTuple->next = hashtable->skewBucket[bucketNumber]->tuples;
1514  hashtable->skewBucket[bucketNumber]->tuples = hashTuple;
1515 
1516  /* Account for space used, and back off if we've used too much */
1517  hashtable->spaceUsed += hashTupleSize;
1518  hashtable->spaceUsedSkew += hashTupleSize;
1519  if (hashtable->spaceUsed > hashtable->spacePeak)
1520  hashtable->spacePeak = hashtable->spaceUsed;
1521  while (hashtable->spaceUsedSkew > hashtable->spaceAllowedSkew)
1522  ExecHashRemoveNextSkewBucket(hashtable);
1523 
1524  /* Check we are not over the total spaceAllowed, either */
1525  if (hashtable->spaceUsed > hashtable->spaceAllowed)
1526  ExecHashIncreaseNumBatches(hashtable);
1527 }
1528 
1529 /*
1530  * ExecHashRemoveNextSkewBucket
1531  *
1532  * Remove the least valuable skew bucket by pushing its tuples into
1533  * the main hash table.
1534  */
1535 static void
1537 {
1538  int bucketToRemove;
1539  HashSkewBucket *bucket;
1540  uint32 hashvalue;
1541  int bucketno;
1542  int batchno;
1543  HashJoinTuple hashTuple;
1544 
1545  /* Locate the bucket to remove */
1546  bucketToRemove = hashtable->skewBucketNums[hashtable->nSkewBuckets - 1];
1547  bucket = hashtable->skewBucket[bucketToRemove];
1548 
1549  /*
1550  * Calculate which bucket and batch the tuples belong to in the main
1551  * hashtable. They all have the same hash value, so it's the same for all
1552  * of them. Also note that it's not possible for nbatch to increase while
1553  * we are processing the tuples.
1554  */
1555  hashvalue = bucket->hashvalue;
1556  ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno, &batchno);
1557 
1558  /* Process all tuples in the bucket */
1559  hashTuple = bucket->tuples;
1560  while (hashTuple != NULL)
1561  {
1562  HashJoinTuple nextHashTuple = hashTuple->next;
1563  MinimalTuple tuple;
1564  Size tupleSize;
1565 
1566  /*
1567  * This code must agree with ExecHashTableInsert. We do not use
1568  * ExecHashTableInsert directly as ExecHashTableInsert expects a
1569  * TupleTableSlot while we already have HashJoinTuples.
1570  */
1571  tuple = HJTUPLE_MINTUPLE(hashTuple);
1572  tupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
1573 
1574  /* Decide whether to put the tuple in the hash table or a temp file */
1575  if (batchno == hashtable->curbatch)
1576  {
1577  /* Move the tuple to the main hash table */
1578  HashJoinTuple copyTuple;
1579 
1580  /*
1581  * We must copy the tuple into the dense storage, else it will not
1582  * be found by, eg, ExecHashIncreaseNumBatches.
1583  */
1584  copyTuple = (HashJoinTuple) dense_alloc(hashtable, tupleSize);
1585  memcpy(copyTuple, hashTuple, tupleSize);
1586  pfree(hashTuple);
1587 
1588  copyTuple->next = hashtable->buckets[bucketno];
1589  hashtable->buckets[bucketno] = copyTuple;
1590 
1591  /* We have reduced skew space, but overall space doesn't change */
1592  hashtable->spaceUsedSkew -= tupleSize;
1593  }
1594  else
1595  {
1596  /* Put the tuple into a temp file for later batches */
1597  Assert(batchno > hashtable->curbatch);
1598  ExecHashJoinSaveTuple(tuple, hashvalue,
1599  &hashtable->innerBatchFile[batchno]);
1600  pfree(hashTuple);
1601  hashtable->spaceUsed -= tupleSize;
1602  hashtable->spaceUsedSkew -= tupleSize;
1603  }
1604 
1605  hashTuple = nextHashTuple;
1606  }
1607 
1608  /*
1609  * Free the bucket struct itself and reset the hashtable entry to NULL.
1610  *
1611  * NOTE: this is not nearly as simple as it looks on the surface, because
1612  * of the possibility of collisions in the hashtable. Suppose that hash
1613  * values A and B collide at a particular hashtable entry, and that A was
1614  * entered first so B gets shifted to a different table entry. If we were
1615  * to remove A first then ExecHashGetSkewBucket would mistakenly start
1616  * reporting that B is not in the hashtable, because it would hit the NULL
1617  * before finding B. However, we always remove entries in the reverse
1618  * order of creation, so this failure cannot happen.
1619  */
1620  hashtable->skewBucket[bucketToRemove] = NULL;
1621  hashtable->nSkewBuckets--;
1622  pfree(bucket);
1623  hashtable->spaceUsed -= SKEW_BUCKET_OVERHEAD;
1624  hashtable->spaceUsedSkew -= SKEW_BUCKET_OVERHEAD;
1625 
1626  /*
1627  * If we have removed all skew buckets then give up on skew optimization.
1628  * Release the arrays since they aren't useful any more.
1629  */
1630  if (hashtable->nSkewBuckets == 0)
1631  {
1632  hashtable->skewEnabled = false;
1633  pfree(hashtable->skewBucket);
1634  pfree(hashtable->skewBucketNums);
1635  hashtable->skewBucket = NULL;
1636  hashtable->skewBucketNums = NULL;
1637  hashtable->spaceUsed -= hashtable->spaceUsedSkew;
1638  hashtable->spaceUsedSkew = 0;
1639  }
1640 }
1641 
1642 /*
1643  * Allocate 'size' bytes from the currently active HashMemoryChunk
1644  */
1645 static void *
1647 {
1648  HashMemoryChunk newChunk;
1649  char *ptr;
1650 
1651  /* just in case the size is not already aligned properly */
1652  size = MAXALIGN(size);
1653 
1654  /*
1655  * If tuple size is larger than of 1/4 of chunk size, allocate a separate
1656  * chunk.
1657  */
1658  if (size > HASH_CHUNK_THRESHOLD)
1659  {
1660  /* allocate new chunk and put it at the beginning of the list */
1661  newChunk = (HashMemoryChunk) MemoryContextAlloc(hashtable->batchCxt,
1662  offsetof(HashMemoryChunkData, data) + size);
1663  newChunk->maxlen = size;
1664  newChunk->used = 0;
1665  newChunk->ntuples = 0;
1666 
1667  /*
1668  * Add this chunk to the list after the first existing chunk, so that
1669  * we don't lose the remaining space in the "current" chunk.
1670  */
1671  if (hashtable->chunks != NULL)
1672  {
1673  newChunk->next = hashtable->chunks->next;
1674  hashtable->chunks->next = newChunk;
1675  }
1676  else
1677  {
1678  newChunk->next = hashtable->chunks;
1679  hashtable->chunks = newChunk;
1680  }
1681 
1682  newChunk->used += size;
1683  newChunk->ntuples += 1;
1684 
1685  return newChunk->data;
1686  }
1687 
1688  /*
1689  * See if we have enough space for it in the current chunk (if any). If
1690  * not, allocate a fresh chunk.
1691  */
1692  if ((hashtable->chunks == NULL) ||
1693  (hashtable->chunks->maxlen - hashtable->chunks->used) < size)
1694  {
1695  /* allocate new chunk and put it at the beginning of the list */
1696  newChunk = (HashMemoryChunk) MemoryContextAlloc(hashtable->batchCxt,
1698 
1699  newChunk->maxlen = HASH_CHUNK_SIZE;
1700  newChunk->used = size;
1701  newChunk->ntuples = 1;
1702 
1703  newChunk->next = hashtable->chunks;
1704  hashtable->chunks = newChunk;
1705 
1706  return newChunk->data;
1707  }
1708 
1709  /* There is enough space in the current chunk, let's add the tuple */
1710  ptr = hashtable->chunks->data + hashtable->chunks->used;
1711  hashtable->chunks->used += size;
1712  hashtable->chunks->ntuples += 1;
1713 
1714  /* return pointer to the start of the tuple memory */
1715  return ptr;
1716 }
int log2_nbuckets_optimal
Definition: hashjoin.h:134
Oid skewTable
Definition: plannodes.h:780
#define DatumGetUInt32(X)
Definition: postgres.h:494
double skewTuples
Definition: hashjoin.h:157
#define NIL
Definition: pg_list.h:69
void InstrStopNode(Instrumentation *instr, double nTuples)
Definition: instrument.c:80
Definition: fmgr.h:53
List * qual
Definition: plannodes.h:122
#define SKEW_BUCKET_OVERHEAD
Definition: hashjoin.h:100
double plan_rows
Definition: plannodes.h:109
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:101
bool op_strict(Oid opno)
Definition: lsyscache.c:1249
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:374
bool skewInherit
Definition: plannodes.h:782
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:203
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:507
HashJoinTable ExecHashTableCreate(Hash *node, List *hashOperators, bool keepNulls)
Definition: nodeHash.c:246
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:387
static void ExecHashRemoveNextSkewBucket(HashJoinTable hashtable)
Definition: nodeHash.c:1536
#define SKEW_MIN_OUTER_FRACTION
Definition: hashjoin.h:103
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:137
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1059
Instrumentation * instrument
Definition: execnodes.h:1033
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:614
bool ExecScanHashTableForUnmatched(HashJoinState *hjstate, ExprContext *econtext)
Definition: nodeHash.c:1146
MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot)
Definition: execTuples.c:655
#define HASH_CHUNK_SIZE
Definition: hashjoin.h:123
Definition: plannodes.h:96
void ExecPrepHashTableForUnmatched(HashJoinState *hjstate)
Definition: nodeHash.c:1122
ExprContext * ps_ExprContext
Definition: execnodes.h:1058
HashState * ExecInitHash(Hash *node, EState *estate, int eflags)
Definition: nodeHash.c:162
void ExecHashTableReset(HashJoinTable hashtable)
Definition: nodeHash.c:1214
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:128
HashJoinTable hashtable
Definition: execnodes.h:1972
#define Min(x, y)
Definition: c.h:798
void ExecReScan(PlanState *node)
Definition: execAmi.c:73
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define Int16GetDatum(X)
Definition: postgres.h:459
List * qual
Definition: execnodes.h:1042
Definition: nodes.h:491
Oid skewColType
Definition: plannodes.h:783
#define MemSet(start, val, len)
Definition: c.h:849
Node * MultiExecHash(HashState *node)
Definition: nodeHash.c:74
List * targetlist
Definition: execnodes.h:1041
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:138
FmgrInfo * inner_hashfunctions
Definition: hashjoin.h:175
bool get_attstatsslot(HeapTuple statstuple, Oid atttype, int32 atttypmod, int reqkind, Oid reqop, Oid *actualop, Datum **values, int *nvalues, float4 **numbers, int *nnumbers)
Definition: lsyscache.c:2854
static void ExecHashIncreaseNumBatches(HashJoinTable hashtable)
Definition: nodeHash.c:597
EState * state
Definition: execnodes.h:1029
void ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew, int *numbuckets, int *numbatches, int *num_skew_mcvs)
Definition: nodeHash.c:408
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:530
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:696
void BufFileClose(BufFile *file)
Definition: buffile.c:202
#define ALLOCSET_DEFAULT_MINSIZE
Definition: memutils.h:142
int ExecHashGetSkewBucket(HashJoinTable hashtable, uint32 hashvalue)
Definition: nodeHash.c:1449
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:435
static void ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
Definition: nodeHash.c:1288
struct PlanState * lefttree
Definition: execnodes.h:1043
int * skewBucketNums
Definition: hashjoin.h:146
void ExecHashTableInsert(HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue)
Definition: nodeHash.c:834
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1032
uint32 hj_CurHashValue
Definition: execnodes.h:1738
int hj_CurSkewBucketNo
Definition: execnodes.h:1740
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4452
void ExecReScanHash(HashState *node)
Definition: nodeHash.c:1268
void pfree(void *pointer)
Definition: mcxt.c:995
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
void PrepareTempTablespaces(void)
Definition: tablespace.c:1291
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:835
static void ExecHashIncreaseNumBuckets(HashJoinTable hashtable)
Definition: nodeHash.c:763
void InstrStartNode(Instrumentation *instr)
Definition: instrument.c:63
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:160
#define HASH_CHUNK_THRESHOLD
Definition: hashjoin.h:124
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1727
#define EXEC_FLAG_BACKWARD
Definition: executor.h:59
BufFile ** outerBatchFile
Definition: hashjoin.h:167
#define outerPlanState(node)
Definition: execnodes.h:1072
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1741
AttrNumber skewColumn
Definition: plannodes.h:781
bool ExecScanHashBucket(HashJoinState *hjstate, ExprContext *econtext)
Definition: nodeHash.c:1064
Size spaceAllowedSkew
Definition: hashjoin.h:182
HashJoinTuple tuples
Definition: hashjoin.h:97
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:123
bool ExecQual(List *qual, ExprContext *econtext, bool resultForNull)
Definition: execQual.c:5246
List * hashkeys
Definition: execnodes.h:1973
#define TupIsNull(slot)
Definition: tuptable.h:138
unsigned int uint32
Definition: c.h:265
PlanState ps
Definition: execnodes.h:1971
struct HashMemoryChunkData * next
Definition: hashjoin.h:115
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define STATISTIC_KIND_MCV
Definition: pg_statistic.h:203
MemoryContext batchCxt
Definition: hashjoin.h:185
struct HashJoinTableData * HashJoinTable
Definition: execnodes.h:1728
Bitmapset * chgParam
Definition: execnodes.h:1052
int my_log2(long num)
Definition: dynahash.c:1684
#define outerPlan(node)
Definition: plannodes.h:151
FmgrInfo * outer_hashfunctions
Definition: hashjoin.h:174
#define MaxAllocSize
Definition: memutils.h:40
int hj_CurBucketNo
Definition: execnodes.h:1739
float float4
Definition: c.h:376
#define SizeofMinimalTupleHeader
Definition: htup_details.h:625
HashSkewBucket ** skewBucket
Definition: hashjoin.h:143
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:436
void * palloc0(Size size)
Definition: mcxt.c:923
uintptr_t Datum
Definition: postgres.h:374
static void * dense_alloc(HashJoinTable hashtable, Size size)
Definition: nodeHash.c:1646
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:990
struct HashMemoryChunkData * HashMemoryChunk
Definition: hashjoin.h:121
#define NTUP_PER_BUCKET
Definition: nodeHash.c:405
int work_mem
Definition: globals.c:110
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:787
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:71
#define BoolGetDatum(X)
Definition: postgres.h:410
Plan * plan
Definition: execnodes.h:1027
int32 skewColTypmod
Definition: plannodes.h:784
#define InvalidOid
Definition: postgres_ext.h:36
double totalTuples
Definition: hashjoin.h:156
uint32 hashvalue
Definition: hashjoin.h:96
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
#define HeapTupleHeaderHasMatch(tup)
Definition: htup_details.h:492
#define Max(x, y)
Definition: c.h:792
#define makeNode(_type_)
Definition: nodes.h:539
int plan_width
Definition: plannodes.h:110
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:667
static void ExecHashSkewTableInsert(HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue, int bucketNumber)
Definition: nodeHash.c:1495
#define lfirst(lc)
Definition: pg_list.h:106
#define EXEC_FLAG_MARK
Definition: executor.h:60
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: hashjoin.h:118
size_t Size
Definition: c.h:352
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:413
BufFile ** innerBatchFile
Definition: hashjoin.h:166
struct HashJoinTupleData * next
Definition: hashjoin.h:66
static int list_length(const List *l)
Definition: pg_list.h:89
#define HeapTupleHeaderClearMatch(tup)
Definition: htup_details.h:502
#define MAXALIGN(LEN)
Definition: c.h:580
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1024
List * targetlist
Definition: plannodes.h:121
HashMemoryChunk chunks
Definition: hashjoin.h:188
List * hashclauses
Definition: execnodes.h:1733
static Datum values[MAXATTR]
Definition: bootstrap.c:160
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1743
Plan plan
Definition: plannodes.h:779
void * palloc(Size size)
Definition: mcxt.c:894
HashJoinTable hj_HashTable
Definition: execnodes.h:1737
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:752
#define SearchSysCache3(cacheId, key1, key2, key3)
Definition: syscache.h:145
#define ALLOCSET_DEFAULT_INITSIZE
Definition: memutils.h:143
int i
void ExecEndHash(HashState *node)
Definition: nodeHash.c:222
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:566
void ExecHashTableResetMatchFlags(HashJoinTable hashtable)
Definition: nodeHash.c:1243
bool ExecHashGetHashValue(HashJoinTable hashtable, ExprContext *econtext, List *hashkeys, bool outer_tuple, bool keep_nulls, uint32 *hashvalue)
Definition: nodeHash.c:928
bool * hashStrict
Definition: hashjoin.h:176
#define ALLOCSET_DEFAULT_MAXSIZE
Definition: memutils.h:144
MemoryContext hashCxt
Definition: hashjoin.h:184
#define elog
Definition: elog.h:218
#define SKEW_WORK_MEM_PERCENT
Definition: hashjoin.h:102
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:136
Definition: pg_list.h:45
void free_attstatsslot(Oid atttype, Datum *values, int nvalues, float4 *numbers, int nnumbers)
Definition: lsyscache.c:2978
TupleTableSlot * ExecHash(HashState *node)
Definition: nodeHash.c:60
void ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr)
Definition: nodeHashjoin.c:871
void ExecHashTableDestroy(HashJoinTable hashtable)
Definition: nodeHash.c:567
uint32 hashvalue
Definition: hashjoin.h:67
#define offsetof(type, field)
Definition: c.h:547
#define ResetExprContext(econtext)
Definition: executor.h:316
#define lfirst_oid(lc)
Definition: pg_list.h:108
#define ExecEvalExpr(expr, econtext, isNull, isDone)
Definition: executor.h:72