PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeHash.h File Reference
#include "nodes/execnodes.h"
Include dependency graph for nodeHash.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

HashStateExecInitHash (Hash *node, EState *estate, int eflags)
 
TupleTableSlotExecHash (HashState *node)
 
NodeMultiExecHash (HashState *node)
 
void ExecEndHash (HashState *node)
 
void ExecReScanHash (HashState *node)
 
HashJoinTable ExecHashTableCreate (Hash *node, List *hashOperators, bool keepNulls)
 
void ExecHashTableDestroy (HashJoinTable hashtable)
 
void ExecHashTableInsert (HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue)
 
bool ExecHashGetHashValue (HashJoinTable hashtable, ExprContext *econtext, List *hashkeys, bool outer_tuple, bool keep_nulls, uint32 *hashvalue)
 
void ExecHashGetBucketAndBatch (HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
 
bool ExecScanHashBucket (HashJoinState *hjstate, ExprContext *econtext)
 
void ExecPrepHashTableForUnmatched (HashJoinState *hjstate)
 
bool ExecScanHashTableForUnmatched (HashJoinState *hjstate, ExprContext *econtext)
 
void ExecHashTableReset (HashJoinTable hashtable)
 
void ExecHashTableResetMatchFlags (HashJoinTable hashtable)
 
void ExecChooseHashTableSize (double ntuples, int tupwidth, bool useskew, int *numbuckets, int *numbatches, int *num_skew_mcvs)
 
int ExecHashGetSkewBucket (HashJoinTable hashtable, uint32 hashvalue)
 

Function Documentation

void ExecChooseHashTableSize ( double  ntuples,
int  tupwidth,
bool  useskew,
int *  numbuckets,
int *  numbatches,
int *  num_skew_mcvs 
)

Definition at line 400 of file nodeHash.c.

References Assert, HJTUPLE_OVERHEAD, Max, MAXALIGN, MaxAllocSize, Min, my_log2(), NTUP_PER_BUCKET, SizeofMinimalTupleHeader, SKEW_BUCKET_OVERHEAD, SKEW_WORK_MEM_PERCENT, and work_mem.

Referenced by ExecHashTableCreate(), and initial_cost_hashjoin().

404 {
405  int tupsize;
406  double inner_rel_bytes;
407  long bucket_bytes;
408  long hash_table_bytes;
409  long skew_table_bytes;
410  long max_pointers;
411  long mppow2;
412  int nbatch = 1;
413  int nbuckets;
414  double dbuckets;
415 
416  /* Force a plausible relation size if no info */
417  if (ntuples <= 0.0)
418  ntuples = 1000.0;
419 
420  /*
421  * Estimate tupsize based on footprint of tuple in hashtable... note this
422  * does not allow for any palloc overhead. The manipulations of spaceUsed
423  * don't count palloc overhead either.
424  */
425  tupsize = HJTUPLE_OVERHEAD +
427  MAXALIGN(tupwidth);
428  inner_rel_bytes = ntuples * tupsize;
429 
430  /*
431  * Target in-memory hashtable size is work_mem kilobytes.
432  */
433  hash_table_bytes = work_mem * 1024L;
434 
435  /*
436  * If skew optimization is possible, estimate the number of skew buckets
437  * that will fit in the memory allowed, and decrement the assumed space
438  * available for the main hash table accordingly.
439  *
440  * We make the optimistic assumption that each skew bucket will contain
441  * one inner-relation tuple. If that turns out to be low, we will recover
442  * at runtime by reducing the number of skew buckets.
443  *
444  * hashtable->skewBucket will have up to 8 times as many HashSkewBucket
445  * pointers as the number of MCVs we allow, since ExecHashBuildSkewHash
446  * will round up to the next power of 2 and then multiply by 4 to reduce
447  * collisions.
448  */
449  if (useskew)
450  {
451  skew_table_bytes = hash_table_bytes * SKEW_WORK_MEM_PERCENT / 100;
452 
453  /*----------
454  * Divisor is:
455  * size of a hash tuple +
456  * worst-case size of skewBucket[] per MCV +
457  * size of skewBucketNums[] entry +
458  * size of skew bucket struct itself
459  *----------
460  */
461  *num_skew_mcvs = skew_table_bytes / (tupsize +
462  (8 * sizeof(HashSkewBucket *)) +
463  sizeof(int) +
465  if (*num_skew_mcvs > 0)
466  hash_table_bytes -= skew_table_bytes;
467  }
468  else
469  *num_skew_mcvs = 0;
470 
471  /*
472  * Set nbuckets to achieve an average bucket load of NTUP_PER_BUCKET when
473  * memory is filled, assuming a single batch; but limit the value so that
474  * the pointer arrays we'll try to allocate do not exceed work_mem nor
475  * MaxAllocSize.
476  *
477  * Note that both nbuckets and nbatch must be powers of 2 to make
478  * ExecHashGetBucketAndBatch fast.
479  */
480  max_pointers = (work_mem * 1024L) / sizeof(HashJoinTuple);
481  max_pointers = Min(max_pointers, MaxAllocSize / sizeof(HashJoinTuple));
482  /* If max_pointers isn't a power of 2, must round it down to one */
483  mppow2 = 1L << my_log2(max_pointers);
484  if (max_pointers != mppow2)
485  max_pointers = mppow2 / 2;
486 
487  /* Also ensure we avoid integer overflow in nbatch and nbuckets */
488  /* (this step is redundant given the current value of MaxAllocSize) */
489  max_pointers = Min(max_pointers, INT_MAX / 2);
490 
491  dbuckets = ceil(ntuples / NTUP_PER_BUCKET);
492  dbuckets = Min(dbuckets, max_pointers);
493  nbuckets = (int) dbuckets;
494  /* don't let nbuckets be really small, though ... */
495  nbuckets = Max(nbuckets, 1024);
496  /* ... and force it to be a power of 2. */
497  nbuckets = 1 << my_log2(nbuckets);
498 
499  /*
500  * If there's not enough space to store the projected number of tuples and
501  * the required bucket headers, we will need multiple batches.
502  */
503  bucket_bytes = sizeof(HashJoinTuple) * nbuckets;
504  if (inner_rel_bytes + bucket_bytes > hash_table_bytes)
505  {
506  /* We'll need multiple batches */
507  long lbuckets;
508  double dbatch;
509  int minbatch;
510  long bucket_size;
511 
512  /*
513  * Estimate the number of buckets we'll want to have when work_mem is
514  * entirely full. Each bucket will contain a bucket pointer plus
515  * NTUP_PER_BUCKET tuples, whose projected size already includes
516  * overhead for the hash code, pointer to the next tuple, etc.
517  */
518  bucket_size = (tupsize * NTUP_PER_BUCKET + sizeof(HashJoinTuple));
519  lbuckets = 1L << my_log2(hash_table_bytes / bucket_size);
520  lbuckets = Min(lbuckets, max_pointers);
521  nbuckets = (int) lbuckets;
522  nbuckets = 1 << my_log2(nbuckets);
523  bucket_bytes = nbuckets * sizeof(HashJoinTuple);
524 
525  /*
526  * Buckets are simple pointers to hashjoin tuples, while tupsize
527  * includes the pointer, hash code, and MinimalTupleData. So buckets
528  * should never really exceed 25% of work_mem (even for
529  * NTUP_PER_BUCKET=1); except maybe for work_mem values that are not
530  * 2^N bytes, where we might get more because of doubling. So let's
531  * look for 50% here.
532  */
533  Assert(bucket_bytes <= hash_table_bytes / 2);
534 
535  /* Calculate required number of batches. */
536  dbatch = ceil(inner_rel_bytes / (hash_table_bytes - bucket_bytes));
537  dbatch = Min(dbatch, max_pointers);
538  minbatch = (int) dbatch;
539  nbatch = 2;
540  while (nbatch < minbatch)
541  nbatch <<= 1;
542  }
543 
544  Assert(nbuckets > 0);
545  Assert(nbatch > 0);
546 
547  *numbuckets = nbuckets;
548  *numbatches = nbatch;
549 }
#define SKEW_BUCKET_OVERHEAD
Definition: hashjoin.h:100
#define Min(x, y)
Definition: c.h:806
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1662
int my_log2(long num)
Definition: dynahash.c:1680
#define MaxAllocSize
Definition: memutils.h:40
#define SizeofMinimalTupleHeader
Definition: htup_details.h:650
#define NTUP_PER_BUCKET
Definition: nodeHash.c:397
int work_mem
Definition: globals.c:113
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:71
#define Max(x, y)
Definition: c.h:800
#define Assert(condition)
Definition: c.h:675
#define MAXALIGN(LEN)
Definition: c.h:588
#define SKEW_WORK_MEM_PERCENT
Definition: hashjoin.h:102
void ExecEndHash ( HashState node)

Definition at line 218 of file nodeHash.c.

References ExecEndNode(), ExecFreeExprContext(), outerPlan, outerPlanState, and HashState::ps.

Referenced by ExecEndNode().

219 {
221 
222  /*
223  * free exprcontext
224  */
225  ExecFreeExprContext(&node->ps);
226 
227  /*
228  * shut down the subplan
229  */
230  outerPlan = outerPlanState(node);
231  ExecEndNode(outerPlan);
232 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:654
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:516
#define outerPlanState(node)
Definition: execnodes.h:874
PlanState ps
Definition: execnodes.h:1935
#define outerPlan(node)
Definition: plannodes.h:174
TupleTableSlot* ExecHash ( HashState node)

Definition at line 60 of file nodeHash.c.

References elog, ERROR, and NULL.

Referenced by ExecProcNode().

61 {
62  elog(ERROR, "Hash node does not support ExecProcNode call convention");
63  return NULL;
64 }
#define ERROR
Definition: elog.h:43
#define NULL
Definition: c.h:229
#define elog
Definition: elog.h:219
void ExecHashGetBucketAndBatch ( HashJoinTable  hashtable,
uint32  hashvalue,
int *  bucketno,
int *  batchno 
)

Definition at line 1027 of file nodeHash.c.

References HashJoinTableData::log2_nbuckets, HashJoinTableData::nbatch, and HashJoinTableData::nbuckets.

Referenced by ExecHashIncreaseNumBatches(), ExecHashIncreaseNumBuckets(), ExecHashJoin(), ExecHashRemoveNextSkewBucket(), and ExecHashTableInsert().

1031 {
1032  uint32 nbuckets = (uint32) hashtable->nbuckets;
1033  uint32 nbatch = (uint32) hashtable->nbatch;
1034 
1035  if (nbatch > 1)
1036  {
1037  /* we can do MOD by masking, DIV by shifting */
1038  *bucketno = hashvalue & (nbuckets - 1);
1039  *batchno = (hashvalue >> hashtable->log2_nbuckets) & (nbatch - 1);
1040  }
1041  else
1042  {
1043  *bucketno = hashvalue & (nbuckets - 1);
1044  *batchno = 0;
1045  }
1046 }
unsigned int uint32
Definition: c.h:268
bool ExecHashGetHashValue ( HashJoinTable  hashtable,
ExprContext econtext,
List hashkeys,
bool  outer_tuple,
bool  keep_nulls,
uint32 hashvalue 
)

Definition at line 923 of file nodeHash.c.

References DatumGetUInt32, ExprContext::ecxt_per_tuple_memory, ExecEvalExpr(), FunctionCall1, HashJoinTableData::hashStrict, i, HashJoinTableData::inner_hashfunctions, lfirst, MemoryContextSwitchTo(), HashJoinTableData::outer_hashfunctions, and ResetExprContext.

Referenced by ExecHashJoinOuterGetTuple(), and MultiExecHash().

929 {
930  uint32 hashkey = 0;
931  FmgrInfo *hashfunctions;
932  ListCell *hk;
933  int i = 0;
934  MemoryContext oldContext;
935 
936  /*
937  * We reset the eval context each time to reclaim any memory leaked in the
938  * hashkey expressions.
939  */
940  ResetExprContext(econtext);
941 
942  oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
943 
944  if (outer_tuple)
945  hashfunctions = hashtable->outer_hashfunctions;
946  else
947  hashfunctions = hashtable->inner_hashfunctions;
948 
949  foreach(hk, hashkeys)
950  {
951  ExprState *keyexpr = (ExprState *) lfirst(hk);
952  Datum keyval;
953  bool isNull;
954 
955  /* rotate hashkey left 1 bit at each step */
956  hashkey = (hashkey << 1) | ((hashkey & 0x80000000) ? 1 : 0);
957 
958  /*
959  * Get the join attribute value of the tuple
960  */
961  keyval = ExecEvalExpr(keyexpr, econtext, &isNull);
962 
963  /*
964  * If the attribute is NULL, and the join operator is strict, then
965  * this tuple cannot pass the join qual so we can reject it
966  * immediately (unless we're scanning the outside of an outer join, in
967  * which case we must not reject it). Otherwise we act like the
968  * hashcode of NULL is zero (this will support operators that act like
969  * IS NOT DISTINCT, though not any more-random behavior). We treat
970  * the hash support function as strict even if the operator is not.
971  *
972  * Note: currently, all hashjoinable operators must be strict since
973  * the hash index AM assumes that. However, it takes so little extra
974  * code here to allow non-strict that we may as well do it.
975  */
976  if (isNull)
977  {
978  if (hashtable->hashStrict[i] && !keep_nulls)
979  {
980  MemoryContextSwitchTo(oldContext);
981  return false; /* cannot match */
982  }
983  /* else, leave hashkey unmodified, equivalent to hashcode 0 */
984  }
985  else
986  {
987  /* Compute the hash function */
988  uint32 hkey;
989 
990  hkey = DatumGetUInt32(FunctionCall1(&hashfunctions[i], keyval));
991  hashkey ^= hkey;
992  }
993 
994  i++;
995  }
996 
997  MemoryContextSwitchTo(oldContext);
998 
999  *hashvalue = hashkey;
1000  return true;
1001 }
#define DatumGetUInt32(X)
Definition: postgres.h:492
Definition: fmgr.h:56
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:203
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
FmgrInfo * inner_hashfunctions
Definition: hashjoin.h:174
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:266
unsigned int uint32
Definition: c.h:268
FmgrInfo * outer_hashfunctions
Definition: hashjoin.h:173
uintptr_t Datum
Definition: postgres.h:372
#define lfirst(lc)
Definition: pg_list.h:106
int i
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:602
bool * hashStrict
Definition: hashjoin.h:175
#define ResetExprContext(econtext)
Definition: executor.h:450
int ExecHashGetSkewBucket ( HashJoinTable  hashtable,
uint32  hashvalue 
)

Definition at line 1437 of file nodeHash.c.

References HashSkewBucket::hashvalue, INVALID_SKEW_BUCKET_NO, NULL, HashJoinTableData::skewBucket, HashJoinTableData::skewBucketLen, and HashJoinTableData::skewEnabled.

Referenced by ExecHashJoin(), and MultiExecHash().

1438 {
1439  int bucket;
1440 
1441  /*
1442  * Always return INVALID_SKEW_BUCKET_NO if not doing skew optimization (in
1443  * particular, this happens after the initial batch is done).
1444  */
1445  if (!hashtable->skewEnabled)
1446  return INVALID_SKEW_BUCKET_NO;
1447 
1448  /*
1449  * Since skewBucketLen is a power of 2, we can do a modulo by ANDing.
1450  */
1451  bucket = hashvalue & (hashtable->skewBucketLen - 1);
1452 
1453  /*
1454  * While we have not hit a hole in the hashtable and have not hit the
1455  * desired bucket, we have collided with some other hash value, so try the
1456  * next bucket location.
1457  */
1458  while (hashtable->skewBucket[bucket] != NULL &&
1459  hashtable->skewBucket[bucket]->hashvalue != hashvalue)
1460  bucket = (bucket + 1) & (hashtable->skewBucketLen - 1);
1461 
1462  /*
1463  * Found the desired bucket?
1464  */
1465  if (hashtable->skewBucket[bucket] != NULL)
1466  return bucket;
1467 
1468  /*
1469  * There must not be any hashtable entry for this hash value.
1470  */
1471  return INVALID_SKEW_BUCKET_NO;
1472 }
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:101
HashSkewBucket ** skewBucket
Definition: hashjoin.h:142
uint32 hashvalue
Definition: hashjoin.h:96
#define NULL
Definition: c.h:229
HashJoinTable ExecHashTableCreate ( Hash node,
List hashOperators,
bool  keepNulls 
)

Definition at line 242 of file nodeHash.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, HashJoinTableData::batchCxt, HashJoinTableData::buckets, HashJoinTableData::chunks, HashJoinTableData::curbatch, CurrentMemoryContext, elog, ERROR, ExecChooseHashTableSize(), ExecHashBuildSkewHash(), fmgr_info(), get_op_hash_functions(), HashJoinTableData::growEnabled, HashJoinTableData::hashCxt, HashJoinTableData::hashStrict, i, HashJoinTableData::inner_hashfunctions, HashJoinTableData::innerBatchFile, HashJoinTableData::keepNulls, lfirst_oid, list_length(), HashJoinTableData::log2_nbuckets, HashJoinTableData::log2_nbuckets_optimal, MemoryContextSwitchTo(), my_log2(), HashJoinTableData::nbatch, HashJoinTableData::nbatch_original, HashJoinTableData::nbatch_outstart, HashJoinTableData::nbuckets, HashJoinTableData::nbuckets_optimal, HashJoinTableData::nbuckets_original, HashJoinTableData::nSkewBuckets, NULL, OidIsValid, op_strict(), HashJoinTableData::outer_hashfunctions, HashJoinTableData::outerBatchFile, outerPlan, palloc(), palloc0(), Plan::plan_rows, Plan::plan_width, PrepareTempTablespaces(), SKEW_WORK_MEM_PERCENT, HashJoinTableData::skewBucket, HashJoinTableData::skewBucketLen, HashJoinTableData::skewBucketNums, HashJoinTableData::skewEnabled, Hash::skewTable, HashJoinTableData::skewTuples, HashJoinTableData::spaceAllowed, HashJoinTableData::spaceAllowedSkew, HashJoinTableData::spacePeak, HashJoinTableData::spaceUsed, HashJoinTableData::spaceUsedSkew, HashJoinTableData::totalTuples, and work_mem.

Referenced by ExecHashJoin().

243 {
244  HashJoinTable hashtable;
245  Plan *outerNode;
246  int nbuckets;
247  int nbatch;
248  int num_skew_mcvs;
249  int log2_nbuckets;
250  int nkeys;
251  int i;
252  ListCell *ho;
253  MemoryContext oldcxt;
254 
255  /*
256  * Get information about the size of the relation to be hashed (it's the
257  * "outer" subtree of this node, but the inner relation of the hashjoin).
258  * Compute the appropriate size of the hash table.
259  */
260  outerNode = outerPlan(node);
261 
262  ExecChooseHashTableSize(outerNode->plan_rows, outerNode->plan_width,
263  OidIsValid(node->skewTable),
264  &nbuckets, &nbatch, &num_skew_mcvs);
265 
266  /* nbuckets must be a power of 2 */
267  log2_nbuckets = my_log2(nbuckets);
268  Assert(nbuckets == (1 << log2_nbuckets));
269 
270  /*
271  * Initialize the hash table control block.
272  *
273  * The hashtable control block is just palloc'd from the executor's
274  * per-query memory context.
275  */
276  hashtable = (HashJoinTable) palloc(sizeof(HashJoinTableData));
277  hashtable->nbuckets = nbuckets;
278  hashtable->nbuckets_original = nbuckets;
279  hashtable->nbuckets_optimal = nbuckets;
280  hashtable->log2_nbuckets = log2_nbuckets;
281  hashtable->log2_nbuckets_optimal = log2_nbuckets;
282  hashtable->buckets = NULL;
283  hashtable->keepNulls = keepNulls;
284  hashtable->skewEnabled = false;
285  hashtable->skewBucket = NULL;
286  hashtable->skewBucketLen = 0;
287  hashtable->nSkewBuckets = 0;
288  hashtable->skewBucketNums = NULL;
289  hashtable->nbatch = nbatch;
290  hashtable->curbatch = 0;
291  hashtable->nbatch_original = nbatch;
292  hashtable->nbatch_outstart = nbatch;
293  hashtable->growEnabled = true;
294  hashtable->totalTuples = 0;
295  hashtable->skewTuples = 0;
296  hashtable->innerBatchFile = NULL;
297  hashtable->outerBatchFile = NULL;
298  hashtable->spaceUsed = 0;
299  hashtable->spacePeak = 0;
300  hashtable->spaceAllowed = work_mem * 1024L;
301  hashtable->spaceUsedSkew = 0;
302  hashtable->spaceAllowedSkew =
303  hashtable->spaceAllowed * SKEW_WORK_MEM_PERCENT / 100;
304  hashtable->chunks = NULL;
305 
306 #ifdef HJDEBUG
307  printf("Hashjoin %p: initial nbatch = %d, nbuckets = %d\n",
308  hashtable, nbatch, nbuckets);
309 #endif
310 
311  /*
312  * Get info about the hash functions to be used for each hash key. Also
313  * remember whether the join operators are strict.
314  */
315  nkeys = list_length(hashOperators);
316  hashtable->outer_hashfunctions =
317  (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
318  hashtable->inner_hashfunctions =
319  (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
320  hashtable->hashStrict = (bool *) palloc(nkeys * sizeof(bool));
321  i = 0;
322  foreach(ho, hashOperators)
323  {
324  Oid hashop = lfirst_oid(ho);
325  Oid left_hashfn;
326  Oid right_hashfn;
327 
328  if (!get_op_hash_functions(hashop, &left_hashfn, &right_hashfn))
329  elog(ERROR, "could not find hash function for hash operator %u",
330  hashop);
331  fmgr_info(left_hashfn, &hashtable->outer_hashfunctions[i]);
332  fmgr_info(right_hashfn, &hashtable->inner_hashfunctions[i]);
333  hashtable->hashStrict[i] = op_strict(hashop);
334  i++;
335  }
336 
337  /*
338  * Create temporary memory contexts in which to keep the hashtable working
339  * storage. See notes in executor/hashjoin.h.
340  */
342  "HashTableContext",
344 
345  hashtable->batchCxt = AllocSetContextCreate(hashtable->hashCxt,
346  "HashBatchContext",
348 
349  /* Allocate data that will live for the life of the hashjoin */
350 
351  oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
352 
353  if (nbatch > 1)
354  {
355  /*
356  * allocate and initialize the file arrays in hashCxt
357  */
358  hashtable->innerBatchFile = (BufFile **)
359  palloc0(nbatch * sizeof(BufFile *));
360  hashtable->outerBatchFile = (BufFile **)
361  palloc0(nbatch * sizeof(BufFile *));
362  /* The files will not be opened until needed... */
363  /* ... but make sure we have temp tablespaces established for them */
365  }
366 
367  /*
368  * Prepare context for the first-scan space allocations; allocate the
369  * hashbucket array therein, and set each bucket "empty".
370  */
371  MemoryContextSwitchTo(hashtable->batchCxt);
372 
373  hashtable->buckets = (HashJoinTuple *)
374  palloc0(nbuckets * sizeof(HashJoinTuple));
375 
376  /*
377  * Set up for skew optimization, if possible and there's a need for more
378  * than one batch. (In a one-batch join, there's no point in it.)
379  */
380  if (nbatch > 1)
381  ExecHashBuildSkewHash(hashtable, node, num_skew_mcvs);
382 
383  MemoryContextSwitchTo(oldcxt);
384 
385  return hashtable;
386 }
int log2_nbuckets_optimal
Definition: hashjoin.h:133
Oid skewTable
Definition: plannodes.h:865
double skewTuples
Definition: hashjoin.h:156
Definition: fmgr.h:56
double plan_rows
Definition: plannodes.h:131
bool op_strict(Oid opno)
Definition: lsyscache.c:1281
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:507
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:136
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
FmgrInfo * inner_hashfunctions
Definition: hashjoin.h:174
void ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew, int *numbuckets, int *numbatches, int *num_skew_mcvs)
Definition: nodeHash.c:400
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
static void ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
Definition: nodeHash.c:1283
int * skewBucketNums
Definition: hashjoin.h:145
#define ERROR
Definition: elog.h:43
void PrepareTempTablespaces(void)
Definition: tablespace.c:1287
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
BufFile ** outerBatchFile
Definition: hashjoin.h:166
Size spaceAllowedSkew
Definition: hashjoin.h:181
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
MemoryContext batchCxt
Definition: hashjoin.h:184
struct HashJoinTableData * HashJoinTable
Definition: execnodes.h:1663
int my_log2(long num)
Definition: dynahash.c:1680
#define outerPlan(node)
Definition: plannodes.h:174
FmgrInfo * outer_hashfunctions
Definition: hashjoin.h:173
HashSkewBucket ** skewBucket
Definition: hashjoin.h:142
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void * palloc0(Size size)
Definition: mcxt.c:878
int work_mem
Definition: globals.c:113
double totalTuples
Definition: hashjoin.h:155
int plan_width
Definition: plannodes.h:132
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
BufFile ** innerBatchFile
Definition: hashjoin.h:165
static int list_length(const List *l)
Definition: pg_list.h:89
HashMemoryChunk chunks
Definition: hashjoin.h:187
void * palloc(Size size)
Definition: mcxt.c:849
int i
bool * hashStrict
Definition: hashjoin.h:175
MemoryContext hashCxt
Definition: hashjoin.h:183
#define elog
Definition: elog.h:219
#define SKEW_WORK_MEM_PERCENT
Definition: hashjoin.h:102
#define lfirst_oid(lc)
Definition: pg_list.h:108
void ExecHashTableDestroy ( HashJoinTable  hashtable)

Definition at line 559 of file nodeHash.c.

References BufFileClose(), HashJoinTableData::hashCxt, i, HashJoinTableData::innerBatchFile, MemoryContextDelete(), HashJoinTableData::nbatch, HashJoinTableData::outerBatchFile, and pfree().

Referenced by ExecEndHashJoin(), and ExecReScanHashJoin().

560 {
561  int i;
562 
563  /*
564  * Make sure all the temp files are closed. We skip batch 0, since it
565  * can't have any temp files (and the arrays might not even exist if
566  * nbatch is only 1).
567  */
568  for (i = 1; i < hashtable->nbatch; i++)
569  {
570  if (hashtable->innerBatchFile[i])
571  BufFileClose(hashtable->innerBatchFile[i]);
572  if (hashtable->outerBatchFile[i])
573  BufFileClose(hashtable->outerBatchFile[i]);
574  }
575 
576  /* Release working memory (batchCxt is a child, so it goes away too) */
577  MemoryContextDelete(hashtable->hashCxt);
578 
579  /* And drop the control block */
580  pfree(hashtable);
581 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
void BufFileClose(BufFile *file)
Definition: buffile.c:203
void pfree(void *pointer)
Definition: mcxt.c:950
BufFile ** outerBatchFile
Definition: hashjoin.h:166
BufFile ** innerBatchFile
Definition: hashjoin.h:165
int i
MemoryContext hashCxt
Definition: hashjoin.h:183
void ExecHashTableInsert ( HashJoinTable  hashtable,
TupleTableSlot slot,
uint32  hashvalue 
)

Definition at line 829 of file nodeHash.c.

References Assert, HashJoinTableData::buckets, HashJoinTableData::curbatch, dense_alloc(), ExecFetchSlotMinimalTuple(), ExecHashGetBucketAndBatch(), ExecHashIncreaseNumBatches(), ExecHashJoinSaveTuple(), HashJoinTupleData::hashvalue, HeapTupleHeaderClearMatch, HJTUPLE_MINTUPLE, HJTUPLE_OVERHEAD, HashJoinTableData::innerBatchFile, HashJoinTableData::log2_nbuckets_optimal, MaxAllocSize, HashJoinTableData::nbatch, HashJoinTableData::nbuckets_optimal, HashJoinTupleData::next, NTUP_PER_BUCKET, HashJoinTableData::skewTuples, HashJoinTableData::spaceAllowed, HashJoinTableData::spacePeak, HashJoinTableData::spaceUsed, MinimalTupleData::t_len, and HashJoinTableData::totalTuples.

Referenced by ExecHashJoinNewBatch(), and MultiExecHash().

832 {
834  int bucketno;
835  int batchno;
836 
837  ExecHashGetBucketAndBatch(hashtable, hashvalue,
838  &bucketno, &batchno);
839 
840  /*
841  * decide whether to put the tuple in the hash table or a temp file
842  */
843  if (batchno == hashtable->curbatch)
844  {
845  /*
846  * put the tuple in hash table
847  */
848  HashJoinTuple hashTuple;
849  int hashTupleSize;
850  double ntuples = (hashtable->totalTuples - hashtable->skewTuples);
851 
852  /* Create the HashJoinTuple */
853  hashTupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
854  hashTuple = (HashJoinTuple) dense_alloc(hashtable, hashTupleSize);
855 
856  hashTuple->hashvalue = hashvalue;
857  memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
858 
859  /*
860  * We always reset the tuple-matched flag on insertion. This is okay
861  * even when reloading a tuple from a batch file, since the tuple
862  * could not possibly have been matched to an outer tuple before it
863  * went into the batch file.
864  */
866 
867  /* Push it onto the front of the bucket's list */
868  hashTuple->next = hashtable->buckets[bucketno];
869  hashtable->buckets[bucketno] = hashTuple;
870 
871  /*
872  * Increase the (optimal) number of buckets if we just exceeded the
873  * NTUP_PER_BUCKET threshold, but only when there's still a single
874  * batch.
875  */
876  if (hashtable->nbatch == 1 &&
877  ntuples > (hashtable->nbuckets_optimal * NTUP_PER_BUCKET))
878  {
879  /* Guard against integer overflow and alloc size overflow */
880  if (hashtable->nbuckets_optimal <= INT_MAX / 2 &&
881  hashtable->nbuckets_optimal * 2 <= MaxAllocSize / sizeof(HashJoinTuple))
882  {
883  hashtable->nbuckets_optimal *= 2;
884  hashtable->log2_nbuckets_optimal += 1;
885  }
886  }
887 
888  /* Account for space used, and back off if we've used too much */
889  hashtable->spaceUsed += hashTupleSize;
890  if (hashtable->spaceUsed > hashtable->spacePeak)
891  hashtable->spacePeak = hashtable->spaceUsed;
892  if (hashtable->spaceUsed +
893  hashtable->nbuckets_optimal * sizeof(HashJoinTuple)
894  > hashtable->spaceAllowed)
895  ExecHashIncreaseNumBatches(hashtable);
896  }
897  else
898  {
899  /*
900  * put the tuple into a temp file for later batches
901  */
902  Assert(batchno > hashtable->curbatch);
903  ExecHashJoinSaveTuple(tuple,
904  hashvalue,
905  &hashtable->innerBatchFile[batchno]);
906  }
907 }
int log2_nbuckets_optimal
Definition: hashjoin.h:133
double skewTuples
Definition: hashjoin.h:156
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:136
MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot)
Definition: execTuples.c:652
static void ExecHashIncreaseNumBatches(HashJoinTable hashtable)
Definition: nodeHash.c:589
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1027
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1662
#define MaxAllocSize
Definition: memutils.h:40
static void * dense_alloc(HashJoinTable hashtable, Size size)
Definition: nodeHash.c:1637
#define NTUP_PER_BUCKET
Definition: nodeHash.c:397
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:71
double totalTuples
Definition: hashjoin.h:155
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
#define Assert(condition)
Definition: c.h:675
BufFile ** innerBatchFile
Definition: hashjoin.h:165
struct HashJoinTupleData * next
Definition: hashjoin.h:66
#define HeapTupleHeaderClearMatch(tup)
Definition: htup_details.h:527
void ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr)
Definition: nodeHashjoin.c:815
uint32 hashvalue
Definition: hashjoin.h:67
void ExecHashTableReset ( HashJoinTable  hashtable)

Definition at line 1209 of file nodeHash.c.

References HashJoinTableData::batchCxt, HashJoinTableData::buckets, HashJoinTableData::chunks, MemoryContextReset(), MemoryContextSwitchTo(), HashJoinTableData::nbuckets, NULL, palloc0(), and HashJoinTableData::spaceUsed.

Referenced by ExecHashJoinNewBatch().

1210 {
1211  MemoryContext oldcxt;
1212  int nbuckets = hashtable->nbuckets;
1213 
1214  /*
1215  * Release all the hash buckets and tuples acquired in the prior pass, and
1216  * reinitialize the context for a new pass.
1217  */
1218  MemoryContextReset(hashtable->batchCxt);
1219  oldcxt = MemoryContextSwitchTo(hashtable->batchCxt);
1220 
1221  /* Reallocate and reinitialize the hash bucket headers. */
1222  hashtable->buckets = (HashJoinTuple *)
1223  palloc0(nbuckets * sizeof(HashJoinTuple));
1224 
1225  hashtable->spaceUsed = 0;
1226 
1227  MemoryContextSwitchTo(oldcxt);
1228 
1229  /* Forget the chunks (the memory was freed by the context reset above). */
1230  hashtable->chunks = NULL;
1231 }
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:136
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
MemoryContext batchCxt
Definition: hashjoin.h:184
void * palloc0(Size size)
Definition: mcxt.c:878
#define NULL
Definition: c.h:229
HashMemoryChunk chunks
Definition: hashjoin.h:187
void ExecHashTableResetMatchFlags ( HashJoinTable  hashtable)

Definition at line 1238 of file nodeHash.c.

References HashJoinTableData::buckets, HeapTupleHeaderClearMatch, HJTUPLE_MINTUPLE, i, HashJoinTableData::nbuckets, HashJoinTupleData::next, HashJoinTableData::nSkewBuckets, NULL, HashJoinTableData::skewBucket, HashJoinTableData::skewBucketNums, and HashSkewBucket::tuples.

Referenced by ExecReScanHashJoin().

1239 {
1240  HashJoinTuple tuple;
1241  int i;
1242 
1243  /* Reset all flags in the main table ... */
1244  for (i = 0; i < hashtable->nbuckets; i++)
1245  {
1246  for (tuple = hashtable->buckets[i]; tuple != NULL; tuple = tuple->next)
1248  }
1249 
1250  /* ... and the same for the skew buckets, if any */
1251  for (i = 0; i < hashtable->nSkewBuckets; i++)
1252  {
1253  int j = hashtable->skewBucketNums[i];
1254  HashSkewBucket *skewBucket = hashtable->skewBucket[j];
1255 
1256  for (tuple = skewBucket->tuples; tuple != NULL; tuple = tuple->next)
1258  }
1259 }
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:136
int * skewBucketNums
Definition: hashjoin.h:145
HashJoinTuple tuples
Definition: hashjoin.h:97
HashSkewBucket ** skewBucket
Definition: hashjoin.h:142
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
#define NULL
Definition: c.h:229
struct HashJoinTupleData * next
Definition: hashjoin.h:66
#define HeapTupleHeaderClearMatch(tup)
Definition: htup_details.h:527
int i
HashState* ExecInitHash ( Hash node,
EState estate,
int  eflags 
)

Definition at line 162 of file nodeHash.c.

References Assert, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecAssignResultTypeFromTL(), ExecInitNode(), ExecInitQual(), ExecInitResultTupleSlot(), HashState::hashkeys, HashState::hashtable, makeNode, NIL, NULL, outerPlan, outerPlanState, PlanState::plan, Hash::plan, HashState::ps, PlanState::ps_ProjInfo, Plan::qual, PlanState::qual, and PlanState::state.

Referenced by ExecInitNode().

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.qual =
194  ExecInitQual(node->plan.qual, (PlanState *) hashstate);
195 
196  /*
197  * initialize child nodes
198  */
199  outerPlanState(hashstate) = ExecInitNode(outerPlan(node), estate, eflags);
200 
201  /*
202  * initialize tuple type. no need to initialize projection info because
203  * this node doesn't do projections
204  */
205  ExecAssignResultTypeFromTL(&hashstate->ps);
206  hashstate->ps.ps_ProjInfo = NULL;
207 
208  return hashstate;
209 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:863
HashJoinTable hashtable
Definition: execnodes.h:1936
EState * state
Definition: execnodes.h:834
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:440
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:874
List * hashkeys
Definition: execnodes.h:1937
PlanState ps
Definition: execnodes.h:1935
#define outerPlan(node)
Definition: plannodes.h:174
Plan * plan
Definition: execnodes.h:832
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define EXEC_FLAG_MARK
Definition: executor.h:61
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:418
ExprState * qual
Definition: execnodes.h:846
Plan plan
Definition: plannodes.h:864
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:140
void ExecPrepHashTableForUnmatched ( HashJoinState hjstate)

Definition at line 1117 of file nodeHash.c.

References HashJoinState::hj_CurBucketNo, HashJoinState::hj_CurSkewBucketNo, HashJoinState::hj_CurTuple, and NULL.

Referenced by ExecHashJoin().

1118 {
1119  /*----------
1120  * During this scan we use the HashJoinState fields as follows:
1121  *
1122  * hj_CurBucketNo: next regular bucket to scan
1123  * hj_CurSkewBucketNo: next skew bucket (an index into skewBucketNums)
1124  * hj_CurTuple: last tuple returned, or NULL to start next bucket
1125  *----------
1126  */
1127  hjstate->hj_CurBucketNo = 0;
1128  hjstate->hj_CurSkewBucketNo = 0;
1129  hjstate->hj_CurTuple = NULL;
1130 }
int hj_CurSkewBucketNo
Definition: execnodes.h:1675
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1676
int hj_CurBucketNo
Definition: execnodes.h:1674
#define NULL
Definition: c.h:229
void ExecReScanHash ( HashState node)

Definition at line 1263 of file nodeHash.c.

References PlanState::chgParam, ExecReScan(), PlanState::lefttree, NULL, and HashState::ps.

Referenced by ExecReScan().

1264 {
1265  /*
1266  * if chgParam of subnode is not null then plan will be re-scanned by
1267  * first ExecProcNode.
1268  */
1269  if (node->ps.lefttree->chgParam == NULL)
1270  ExecReScan(node->ps.lefttree);
1271 }
void ExecReScan(PlanState *node)
Definition: execAmi.c:75
struct PlanState * lefttree
Definition: execnodes.h:847
PlanState ps
Definition: execnodes.h:1935
Bitmapset * chgParam
Definition: execnodes.h:856
#define NULL
Definition: c.h:229
bool ExecScanHashBucket ( HashJoinState hjstate,
ExprContext econtext 
)

Definition at line 1059 of file nodeHash.c.

References HashJoinTableData::buckets, ExprContext::ecxt_innertuple, ExecQual(), ExecStoreMinimalTuple(), HashJoinState::hashclauses, HashJoinTupleData::hashvalue, HashJoinState::hj_CurBucketNo, HashJoinState::hj_CurHashValue, HashJoinState::hj_CurSkewBucketNo, HashJoinState::hj_CurTuple, HashJoinState::hj_HashTable, HashJoinState::hj_HashTupleSlot, HJTUPLE_MINTUPLE, INVALID_SKEW_BUCKET_NO, HashJoinTupleData::next, NULL, ResetExprContext, HashJoinTableData::skewBucket, and HashSkewBucket::tuples.

Referenced by ExecHashJoin().

1061 {
1062  ExprState *hjclauses = hjstate->hashclauses;
1063  HashJoinTable hashtable = hjstate->hj_HashTable;
1064  HashJoinTuple hashTuple = hjstate->hj_CurTuple;
1065  uint32 hashvalue = hjstate->hj_CurHashValue;
1066 
1067  /*
1068  * hj_CurTuple is the address of the tuple last returned from the current
1069  * bucket, or NULL if it's time to start scanning a new bucket.
1070  *
1071  * If the tuple hashed to a skew bucket then scan the skew bucket
1072  * otherwise scan the standard hashtable bucket.
1073  */
1074  if (hashTuple != NULL)
1075  hashTuple = hashTuple->next;
1076  else if (hjstate->hj_CurSkewBucketNo != INVALID_SKEW_BUCKET_NO)
1077  hashTuple = hashtable->skewBucket[hjstate->hj_CurSkewBucketNo]->tuples;
1078  else
1079  hashTuple = hashtable->buckets[hjstate->hj_CurBucketNo];
1080 
1081  while (hashTuple != NULL)
1082  {
1083  if (hashTuple->hashvalue == hashvalue)
1084  {
1085  TupleTableSlot *inntuple;
1086 
1087  /* insert hashtable's tuple into exec slot so ExecQual sees it */
1088  inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
1089  hjstate->hj_HashTupleSlot,
1090  false); /* do not pfree */
1091  econtext->ecxt_innertuple = inntuple;
1092 
1093  /* reset temp memory each time to avoid leaks from qual expr */
1094  ResetExprContext(econtext);
1095 
1096  if (ExecQual(hjclauses, econtext))
1097  {
1098  hjstate->hj_CurTuple = hashTuple;
1099  return true;
1100  }
1101  }
1102 
1103  hashTuple = hashTuple->next;
1104  }
1105 
1106  /*
1107  * no match
1108  */
1109  return false;
1110 }
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:101
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:384
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:136
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:346
uint32 hj_CurHashValue
Definition: execnodes.h:1673
int hj_CurSkewBucketNo
Definition: execnodes.h:1675
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1676
HashJoinTuple tuples
Definition: hashjoin.h:97
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:198
unsigned int uint32
Definition: c.h:268
int hj_CurBucketNo
Definition: execnodes.h:1674
HashSkewBucket ** skewBucket
Definition: hashjoin.h:142
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
#define NULL
Definition: c.h:229
struct HashJoinTupleData * next
Definition: hashjoin.h:66
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1678
HashJoinTable hj_HashTable
Definition: execnodes.h:1672
uint32 hashvalue
Definition: hashjoin.h:67
ExprState * hashclauses
Definition: execnodes.h:1668
#define ResetExprContext(econtext)
Definition: executor.h:450
bool ExecScanHashTableForUnmatched ( HashJoinState hjstate,
ExprContext econtext 
)

Definition at line 1141 of file nodeHash.c.

References HashJoinTableData::buckets, ExprContext::ecxt_innertuple, ExecStoreMinimalTuple(), HeapTupleHeaderHasMatch, HashJoinState::hj_CurBucketNo, HashJoinState::hj_CurSkewBucketNo, HashJoinState::hj_CurTuple, HashJoinState::hj_HashTable, HashJoinState::hj_HashTupleSlot, HJTUPLE_MINTUPLE, HashJoinTableData::nbuckets, HashJoinTupleData::next, HashJoinTableData::nSkewBuckets, NULL, ResetExprContext, HashJoinTableData::skewBucket, HashJoinTableData::skewBucketNums, and HashSkewBucket::tuples.

Referenced by ExecHashJoin().

1142 {
1143  HashJoinTable hashtable = hjstate->hj_HashTable;
1144  HashJoinTuple hashTuple = hjstate->hj_CurTuple;
1145 
1146  for (;;)
1147  {
1148  /*
1149  * hj_CurTuple is the address of the tuple last returned from the
1150  * current bucket, or NULL if it's time to start scanning a new
1151  * bucket.
1152  */
1153  if (hashTuple != NULL)
1154  hashTuple = hashTuple->next;
1155  else if (hjstate->hj_CurBucketNo < hashtable->nbuckets)
1156  {
1157  hashTuple = hashtable->buckets[hjstate->hj_CurBucketNo];
1158  hjstate->hj_CurBucketNo++;
1159  }
1160  else if (hjstate->hj_CurSkewBucketNo < hashtable->nSkewBuckets)
1161  {
1162  int j = hashtable->skewBucketNums[hjstate->hj_CurSkewBucketNo];
1163 
1164  hashTuple = hashtable->skewBucket[j]->tuples;
1165  hjstate->hj_CurSkewBucketNo++;
1166  }
1167  else
1168  break; /* finished all buckets */
1169 
1170  while (hashTuple != NULL)
1171  {
1172  if (!HeapTupleHeaderHasMatch(HJTUPLE_MINTUPLE(hashTuple)))
1173  {
1174  TupleTableSlot *inntuple;
1175 
1176  /* insert hashtable's tuple into exec slot */
1177  inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
1178  hjstate->hj_HashTupleSlot,
1179  false); /* do not pfree */
1180  econtext->ecxt_innertuple = inntuple;
1181 
1182  /*
1183  * Reset temp memory each time; although this function doesn't
1184  * do any qual eval, the caller will, so let's keep it
1185  * parallel to ExecScanHashBucket.
1186  */
1187  ResetExprContext(econtext);
1188 
1189  hjstate->hj_CurTuple = hashTuple;
1190  return true;
1191  }
1192 
1193  hashTuple = hashTuple->next;
1194  }
1195  }
1196 
1197  /*
1198  * no more unmatched tuples
1199  */
1200  return false;
1201 }
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:384
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:136
int * skewBucketNums
Definition: hashjoin.h:145
int hj_CurSkewBucketNo
Definition: execnodes.h:1675
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1676
HashJoinTuple tuples
Definition: hashjoin.h:97
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:198
int hj_CurBucketNo
Definition: execnodes.h:1674
HashSkewBucket ** skewBucket
Definition: hashjoin.h:142
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
#define HeapTupleHeaderHasMatch(tup)
Definition: htup_details.h:517
#define NULL
Definition: c.h:229
struct HashJoinTupleData * next
Definition: hashjoin.h:66
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1678
HashJoinTable hj_HashTable
Definition: execnodes.h:1672
#define ResetExprContext(econtext)
Definition: executor.h:450
Node* MultiExecHash ( HashState node)

Definition at line 74 of file nodeHash.c.

References ExprContext::ecxt_innertuple, ExecHashGetHashValue(), ExecHashGetSkewBucket(), ExecHashIncreaseNumBuckets(), ExecHashSkewTableInsert(), ExecHashTableInsert(), ExecProcNode(), HashState::hashkeys, HashState::hashtable, InstrStartNode(), InstrStopNode(), PlanState::instrument, INVALID_SKEW_BUCKET_NO, HashJoinTableData::keepNulls, HashJoinTableData::nbuckets, HashJoinTableData::nbuckets_optimal, NULL, outerPlanState, HashState::ps, PlanState::ps_ExprContext, HashJoinTableData::skewTuples, HashJoinTableData::spacePeak, HashJoinTableData::spaceUsed, HashJoinTableData::totalTuples, and TupIsNull.

Referenced by MultiExecProcNode().

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 }
double skewTuples
Definition: hashjoin.h:156
void InstrStopNode(Instrumentation *instr, double nTuples)
Definition: instrument.c:80
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:101
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:398
Instrumentation * instrument
Definition: execnodes.h:838
ExprContext * ps_ExprContext
Definition: execnodes.h:862
HashJoinTable hashtable
Definition: execnodes.h:1936
int ExecHashGetSkewBucket(HashJoinTable hashtable, uint32 hashvalue)
Definition: nodeHash.c:1437
void ExecHashTableInsert(HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue)
Definition: nodeHash.c:829
static void ExecHashIncreaseNumBuckets(HashJoinTable hashtable)
Definition: nodeHash.c:758
void InstrStartNode(Instrumentation *instr)
Definition: instrument.c:63
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1662
#define outerPlanState(node)
Definition: execnodes.h:874
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:198
List * hashkeys
Definition: execnodes.h:1937
#define TupIsNull(slot)
Definition: tuptable.h:138
unsigned int uint32
Definition: c.h:268
PlanState ps
Definition: execnodes.h:1935
double totalTuples
Definition: hashjoin.h:155
#define NULL
Definition: c.h:229
static void ExecHashSkewTableInsert(HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue, int bucketNumber)
Definition: nodeHash.c:1483
bool ExecHashGetHashValue(HashJoinTable hashtable, ExprContext *econtext, List *hashkeys, bool outer_tuple, bool keep_nulls, uint32 *hashvalue)
Definition: nodeHash.c:923
Definition: pg_list.h:45