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

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

Definition at line 222 of file nodeHash.c.

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

Referenced by ExecEndNode().

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 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:624
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:685
#define outerPlanState(node)
Definition: execnodes.h:1090
PlanState ps
Definition: execnodes.h:2021
#define outerPlan(node)
Definition: plannodes.h:159
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:226
#define elog
Definition: elog.h:219
void ExecHashGetBucketAndBatch ( HashJoinTable  hashtable,
uint32  hashvalue,
int *  bucketno,
int *  batchno 
)

Definition at line 1031 of file nodeHash.c.

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

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

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

Definition at line 927 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().

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

Definition at line 1448 of file nodeHash.c.

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

Referenced by ExecHashJoin(), and MultiExecHash().

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

Definition at line 246 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().

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",
348 
349  hashtable->batchCxt = AllocSetContextCreate(hashtable->hashCxt,
350  "HashBatchContext",
352 
353  /* Allocate data that will live for the life of the hashjoin */
354 
355  oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
356 
357  if (nbatch > 1)
358  {
359  /*
360  * allocate and initialize the file arrays in hashCxt
361  */
362  hashtable->innerBatchFile = (BufFile **)
363  palloc0(nbatch * sizeof(BufFile *));
364  hashtable->outerBatchFile = (BufFile **)
365  palloc0(nbatch * sizeof(BufFile *));
366  /* The files will not be opened until needed... */
367  /* ... but make sure we have temp tablespaces established for them */
369  }
370 
371  /*
372  * Prepare context for the first-scan space allocations; allocate the
373  * hashbucket array therein, and set each bucket "empty".
374  */
375  MemoryContextSwitchTo(hashtable->batchCxt);
376 
377  hashtable->buckets = (HashJoinTuple *)
378  palloc0(nbuckets * sizeof(HashJoinTuple));
379 
380  /*
381  * Set up for skew optimization, if possible and there's a need for more
382  * than one batch. (In a one-batch join, there's no point in it.)
383  */
384  if (nbatch > 1)
385  ExecHashBuildSkewHash(hashtable, node, num_skew_mcvs);
386 
387  MemoryContextSwitchTo(oldcxt);
388 
389  return hashtable;
390 }
int log2_nbuckets_optimal
Definition: hashjoin.h:134
Oid skewTable
Definition: plannodes.h:800
double skewTuples
Definition: hashjoin.h:157
Definition: fmgr.h:53
double plan_rows
Definition: plannodes.h:117
bool op_strict(Oid opno)
Definition: lsyscache.c:1249
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:507
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:137
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
FmgrInfo * inner_hashfunctions
Definition: hashjoin.h:175
void ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew, int *numbuckets, int *numbatches, int *num_skew_mcvs)
Definition: nodeHash.c:404
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
static void ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
Definition: nodeHash.c:1287
int * skewBucketNums
Definition: hashjoin.h:146
#define ERROR
Definition: elog.h:43
void PrepareTempTablespaces(void)
Definition: tablespace.c:1287
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:151
BufFile ** outerBatchFile
Definition: hashjoin.h:167
Size spaceAllowedSkew
Definition: hashjoin.h:182
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
MemoryContext batchCxt
Definition: hashjoin.h:185
struct HashJoinTableData * HashJoinTable
Definition: execnodes.h:1773
int my_log2(long num)
Definition: dynahash.c:1682
#define outerPlan(node)
Definition: plannodes.h:159
FmgrInfo * outer_hashfunctions
Definition: hashjoin.h:174
HashSkewBucket ** skewBucket
Definition: hashjoin.h:143
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:329
void * palloc0(Size size)
Definition: mcxt.c:920
int work_mem
Definition: globals.c:112
double totalTuples
Definition: hashjoin.h:156
int plan_width
Definition: plannodes.h:118
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
BufFile ** innerBatchFile
Definition: hashjoin.h:166
static int list_length(const List *l)
Definition: pg_list.h:89
HashMemoryChunk chunks
Definition: hashjoin.h:188
void * palloc(Size size)
Definition: mcxt.c:891
int i
bool * hashStrict
Definition: hashjoin.h:176
MemoryContext hashCxt
Definition: hashjoin.h:184
#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 563 of file nodeHash.c.

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

Referenced by ExecEndHashJoin(), and ExecReScanHashJoin().

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

Definition at line 833 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().

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

Definition at line 1213 of file nodeHash.c.

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

Referenced by ExecHashJoinNewBatch().

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

Definition at line 1242 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().

1243 {
1244  HashJoinTuple tuple;
1245  int i;
1246 
1247  /* Reset all flags in the main table ... */
1248  for (i = 0; i < hashtable->nbuckets; i++)
1249  {
1250  for (tuple = hashtable->buckets[i]; tuple != NULL; tuple = tuple->next)
1252  }
1253 
1254  /* ... and the same for the skew buckets, if any */
1255  for (i = 0; i < hashtable->nSkewBuckets; i++)
1256  {
1257  int j = hashtable->skewBucketNums[i];
1258  HashSkewBucket *skewBucket = hashtable->skewBucket[j];
1259 
1260  for (tuple = skewBucket->tuples; tuple != NULL; tuple = tuple->next)
1262  }
1263 }
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:137
int * skewBucketNums
Definition: hashjoin.h:146
HashJoinTuple tuples
Definition: hashjoin.h:97
HashSkewBucket ** skewBucket
Definition: hashjoin.h:143
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
#define NULL
Definition: c.h:226
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(), ExecInitExpr(), ExecInitNode(), ExecInitResultTupleSlot(), HashState::hashkeys, HashState::hashtable, makeNode, NIL, NULL, outerPlan, outerPlanState, Hash::plan, PlanState::plan, HashState::ps, PlanState::ps_ProjInfo, Plan::qual, PlanState::qual, PlanState::state, Plan::targetlist, and PlanState::targetlist.

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.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 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:130
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1079
HashJoinTable hashtable
Definition: execnodes.h:2022
List * qual
Definition: execnodes.h:1062
List * targetlist
Definition: execnodes.h:1061
EState * state
Definition: execnodes.h:1049
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:430
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4266
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:1090
List * hashkeys
Definition: execnodes.h:2023
PlanState ps
Definition: execnodes.h:2021
#define outerPlan(node)
Definition: plannodes.h:159
Plan * plan
Definition: execnodes.h:1047
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define EXEC_FLAG_MARK
Definition: executor.h:61
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:408
List * targetlist
Definition: plannodes.h:129
Plan plan
Definition: plannodes.h:799
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:137
Definition: pg_list.h:45
void ExecPrepHashTableForUnmatched ( HashJoinState hjstate)

Definition at line 1121 of file nodeHash.c.

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

Referenced by ExecHashJoin().

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

Definition at line 1267 of file nodeHash.c.

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

Referenced by ExecReScan().

1268 {
1269  /*
1270  * if chgParam of subnode is not null then plan will be re-scanned by
1271  * first ExecProcNode.
1272  */
1273  if (node->ps.lefttree->chgParam == NULL)
1274  ExecReScan(node->ps.lefttree);
1275 }
void ExecReScan(PlanState *node)
Definition: execAmi.c:73
struct PlanState * lefttree
Definition: execnodes.h:1063
PlanState ps
Definition: execnodes.h:2021
Bitmapset * chgParam
Definition: execnodes.h:1072
#define NULL
Definition: c.h:226
bool ExecScanHashBucket ( HashJoinState hjstate,
ExprContext econtext 
)

Definition at line 1063 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().

1065 {
1066  List *hjclauses = hjstate->hashclauses;
1067  HashJoinTable hashtable = hjstate->hj_HashTable;
1068  HashJoinTuple hashTuple = hjstate->hj_CurTuple;
1069  uint32 hashvalue = hjstate->hj_CurHashValue;
1070 
1071  /*
1072  * hj_CurTuple is the address of the tuple last returned from the current
1073  * bucket, or NULL if it's time to start scanning a new bucket.
1074  *
1075  * If the tuple hashed to a skew bucket then scan the skew bucket
1076  * otherwise scan the standard hashtable bucket.
1077  */
1078  if (hashTuple != NULL)
1079  hashTuple = hashTuple->next;
1080  else if (hjstate->hj_CurSkewBucketNo != INVALID_SKEW_BUCKET_NO)
1081  hashTuple = hashtable->skewBucket[hjstate->hj_CurSkewBucketNo]->tuples;
1082  else
1083  hashTuple = hashtable->buckets[hjstate->hj_CurBucketNo];
1084 
1085  while (hashTuple != NULL)
1086  {
1087  if (hashTuple->hashvalue == hashvalue)
1088  {
1089  TupleTableSlot *inntuple;
1090 
1091  /* insert hashtable's tuple into exec slot so ExecQual sees it */
1092  inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
1093  hjstate->hj_HashTupleSlot,
1094  false); /* do not pfree */
1095  econtext->ecxt_innertuple = inntuple;
1096 
1097  /* reset temp memory each time to avoid leaks from qual expr */
1098  ResetExprContext(econtext);
1099 
1100  if (ExecQual(hjclauses, econtext, false))
1101  {
1102  hjstate->hj_CurTuple = hashTuple;
1103  return true;
1104  }
1105  }
1106 
1107  hashTuple = hashTuple->next;
1108  }
1109 
1110  /*
1111  * no match
1112  */
1113  return false;
1114 }
#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:137
uint32 hj_CurHashValue
Definition: execnodes.h:1783
int hj_CurSkewBucketNo
Definition: execnodes.h:1785
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1786
HashJoinTuple tuples
Definition: hashjoin.h:97
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:129
bool ExecQual(List *qual, ExprContext *econtext, bool resultForNull)
Definition: execQual.c:5055
unsigned int uint32
Definition: c.h:265
int hj_CurBucketNo
Definition: execnodes.h:1784
HashSkewBucket ** skewBucket
Definition: hashjoin.h:143
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
#define NULL
Definition: c.h:226
struct HashJoinTupleData * next
Definition: hashjoin.h:66
List * hashclauses
Definition: execnodes.h:1778
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1788
HashJoinTable hj_HashTable
Definition: execnodes.h:1782
Definition: pg_list.h:45
uint32 hashvalue
Definition: hashjoin.h:67
#define ResetExprContext(econtext)
Definition: executor.h:332
bool ExecScanHashTableForUnmatched ( HashJoinState hjstate,
ExprContext econtext 
)

Definition at line 1145 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().

1146 {
1147  HashJoinTable hashtable = hjstate->hj_HashTable;
1148  HashJoinTuple hashTuple = hjstate->hj_CurTuple;
1149 
1150  for (;;)
1151  {
1152  /*
1153  * hj_CurTuple is the address of the tuple last returned from the
1154  * current bucket, or NULL if it's time to start scanning a new
1155  * bucket.
1156  */
1157  if (hashTuple != NULL)
1158  hashTuple = hashTuple->next;
1159  else if (hjstate->hj_CurBucketNo < hashtable->nbuckets)
1160  {
1161  hashTuple = hashtable->buckets[hjstate->hj_CurBucketNo];
1162  hjstate->hj_CurBucketNo++;
1163  }
1164  else if (hjstate->hj_CurSkewBucketNo < hashtable->nSkewBuckets)
1165  {
1166  int j = hashtable->skewBucketNums[hjstate->hj_CurSkewBucketNo];
1167 
1168  hashTuple = hashtable->skewBucket[j]->tuples;
1169  hjstate->hj_CurSkewBucketNo++;
1170  }
1171  else
1172  break; /* finished all buckets */
1173 
1174  while (hashTuple != NULL)
1175  {
1176  if (!HeapTupleHeaderHasMatch(HJTUPLE_MINTUPLE(hashTuple)))
1177  {
1178  TupleTableSlot *inntuple;
1179 
1180  /* insert hashtable's tuple into exec slot */
1181  inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
1182  hjstate->hj_HashTupleSlot,
1183  false); /* do not pfree */
1184  econtext->ecxt_innertuple = inntuple;
1185 
1186  /*
1187  * Reset temp memory each time; although this function doesn't
1188  * do any qual eval, the caller will, so let's keep it
1189  * parallel to ExecScanHashBucket.
1190  */
1191  ResetExprContext(econtext);
1192 
1193  hjstate->hj_CurTuple = hashTuple;
1194  return true;
1195  }
1196 
1197  hashTuple = hashTuple->next;
1198  }
1199  }
1200 
1201  /*
1202  * no more unmatched tuples
1203  */
1204  return false;
1205 }
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:384
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:137
int * skewBucketNums
Definition: hashjoin.h:146
int hj_CurSkewBucketNo
Definition: execnodes.h:1785
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1786
HashJoinTuple tuples
Definition: hashjoin.h:97
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:129
int hj_CurBucketNo
Definition: execnodes.h:1784
HashSkewBucket ** skewBucket
Definition: hashjoin.h:143
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
#define HeapTupleHeaderHasMatch(tup)
Definition: htup_details.h:517
#define NULL
Definition: c.h:226
struct HashJoinTupleData * next
Definition: hashjoin.h:66
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1788
HashJoinTable hj_HashTable
Definition: execnodes.h:1782
#define ResetExprContext(econtext)
Definition: executor.h:332
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:157
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:380
Instrumentation * instrument
Definition: execnodes.h:1053
ExprContext * ps_ExprContext
Definition: execnodes.h:1078
HashJoinTable hashtable
Definition: execnodes.h:2022
int ExecHashGetSkewBucket(HashJoinTable hashtable, uint32 hashvalue)
Definition: nodeHash.c:1448
void ExecHashTableInsert(HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue)
Definition: nodeHash.c:833
static void ExecHashIncreaseNumBuckets(HashJoinTable hashtable)
Definition: nodeHash.c:762
void InstrStartNode(Instrumentation *instr)
Definition: instrument.c:63
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1772
#define outerPlanState(node)
Definition: execnodes.h:1090
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:129
List * hashkeys
Definition: execnodes.h:2023
#define TupIsNull(slot)
Definition: tuptable.h:138
unsigned int uint32
Definition: c.h:265
PlanState ps
Definition: execnodes.h:2021
double totalTuples
Definition: hashjoin.h:156
#define NULL
Definition: c.h:226
static void ExecHashSkewTableInsert(HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue, int bucketNumber)
Definition: nodeHash.c:1494
bool ExecHashGetHashValue(HashJoinTable hashtable, ExprContext *econtext, List *hashkeys, bool outer_tuple, bool keep_nulls, uint32 *hashvalue)
Definition: nodeHash.c:927
Definition: pg_list.h:45