PostgreSQL Source Code  git master
nodeHash.c File Reference
#include "postgres.h"
#include <math.h>
#include <limits.h>
#include "access/htup_details.h"
#include "catalog/pg_statistic.h"
#include "commands/tablespace.h"
#include "executor/execdebug.h"
#include "executor/hashjoin.h"
#include "executor/nodeHash.h"
#include "executor/nodeHashjoin.h"
#include "miscadmin.h"
#include "utils/dynahash.h"
#include "utils/memutils.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Include dependency graph for nodeHash.c:

Go to the source code of this file.

Macros

#define NTUP_PER_BUCKET   1
 

Functions

static void ExecHashIncreaseNumBatches (HashJoinTable hashtable)
 
static void ExecHashIncreaseNumBuckets (HashJoinTable hashtable)
 
static void ExecHashBuildSkewHash (HashJoinTable hashtable, Hash *node, int mcvsToUse)
 
static void ExecHashSkewTableInsert (HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue, int bucketNumber)
 
static void ExecHashRemoveNextSkewBucket (HashJoinTable hashtable)
 
static void * dense_alloc (HashJoinTable hashtable, Size size)
 
static TupleTableSlotExecHash (PlanState *pstate)
 
NodeMultiExecHash (HashState *node)
 
HashStateExecInitHash (Hash *node, EState *estate, int eflags)
 
void ExecEndHash (HashState *node)
 
HashJoinTable ExecHashTableCreate (Hash *node, List *hashOperators, bool keepNulls)
 
void ExecChooseHashTableSize (double ntuples, int tupwidth, bool useskew, int *numbuckets, int *numbatches, int *num_skew_mcvs)
 
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 ExecReScanHash (HashState *node)
 
int ExecHashGetSkewBucket (HashJoinTable hashtable, uint32 hashvalue)
 
void ExecHashEstimate (HashState *node, ParallelContext *pcxt)
 
void ExecHashInitializeDSM (HashState *node, ParallelContext *pcxt)
 
void ExecHashReInitializeDSM (HashState *node, ParallelContext *pcxt)
 
void ExecHashInitializeWorker (HashState *node, ParallelWorkerContext *pwcxt)
 
void ExecShutdownHash (HashState *node)
 
void ExecHashRetrieveInstrumentation (HashState *node)
 
void ExecHashGetInstrumentation (HashInstrumentation *instrument, HashJoinTable hashtable)
 

Macro Definition Documentation

◆ NTUP_PER_BUCKET

#define NTUP_PER_BUCKET   1

Definition at line 398 of file nodeHash.c.

Referenced by ExecChooseHashTableSize(), and ExecHashTableInsert().

Function Documentation

◆ dense_alloc()

static void * dense_alloc ( HashJoinTable  hashtable,
Size  size 
)
static

Definition at line 1748 of file nodeHash.c.

References HashJoinTableData::batchCxt, HashJoinTableData::chunks, HashMemoryChunkData::data, HASH_CHUNK_SIZE, HASH_CHUNK_THRESHOLD, MAXALIGN, HashMemoryChunkData::maxlen, MemoryContextAlloc(), HashMemoryChunkData::next, HashMemoryChunkData::ntuples, offsetof, and HashMemoryChunkData::used.

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

1749 {
1750  HashMemoryChunk newChunk;
1751  char *ptr;
1752 
1753  /* just in case the size is not already aligned properly */
1754  size = MAXALIGN(size);
1755 
1756  /*
1757  * If tuple size is larger than of 1/4 of chunk size, allocate a separate
1758  * chunk.
1759  */
1760  if (size > HASH_CHUNK_THRESHOLD)
1761  {
1762  /* allocate new chunk and put it at the beginning of the list */
1763  newChunk = (HashMemoryChunk) MemoryContextAlloc(hashtable->batchCxt,
1764  offsetof(HashMemoryChunkData, data) + size);
1765  newChunk->maxlen = size;
1766  newChunk->used = 0;
1767  newChunk->ntuples = 0;
1768 
1769  /*
1770  * Add this chunk to the list after the first existing chunk, so that
1771  * we don't lose the remaining space in the "current" chunk.
1772  */
1773  if (hashtable->chunks != NULL)
1774  {
1775  newChunk->next = hashtable->chunks->next;
1776  hashtable->chunks->next = newChunk;
1777  }
1778  else
1779  {
1780  newChunk->next = hashtable->chunks;
1781  hashtable->chunks = newChunk;
1782  }
1783 
1784  newChunk->used += size;
1785  newChunk->ntuples += 1;
1786 
1787  return newChunk->data;
1788  }
1789 
1790  /*
1791  * See if we have enough space for it in the current chunk (if any). If
1792  * not, allocate a fresh chunk.
1793  */
1794  if ((hashtable->chunks == NULL) ||
1795  (hashtable->chunks->maxlen - hashtable->chunks->used) < size)
1796  {
1797  /* allocate new chunk and put it at the beginning of the list */
1798  newChunk = (HashMemoryChunk) MemoryContextAlloc(hashtable->batchCxt,
1800 
1801  newChunk->maxlen = HASH_CHUNK_SIZE;
1802  newChunk->used = size;
1803  newChunk->ntuples = 1;
1804 
1805  newChunk->next = hashtable->chunks;
1806  hashtable->chunks = newChunk;
1807 
1808  return newChunk->data;
1809  }
1810 
1811  /* There is enough space in the current chunk, let's add the tuple */
1812  ptr = hashtable->chunks->data + hashtable->chunks->used;
1813  hashtable->chunks->used += size;
1814  hashtable->chunks->ntuples += 1;
1815 
1816  /* return pointer to the start of the tuple memory */
1817  return ptr;
1818 }
#define HASH_CHUNK_SIZE
Definition: hashjoin.h:123
#define HASH_CHUNK_THRESHOLD
Definition: hashjoin.h:124
struct HashMemoryChunkData * next
Definition: hashjoin.h:115
MemoryContext batchCxt
Definition: hashjoin.h:184
struct HashMemoryChunkData * HashMemoryChunk
Definition: hashjoin.h:121
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: hashjoin.h:118
#define MAXALIGN(LEN)
Definition: c.h:633
HashMemoryChunk chunks
Definition: hashjoin.h:187
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:693
#define offsetof(type, field)
Definition: c.h:603

◆ ExecChooseHashTableSize()

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

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

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

◆ ExecEndHash()

void ExecEndHash ( HashState node)

Definition at line 219 of file nodeHash.c.

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

Referenced by ExecEndNode().

220 {
222 
223  /*
224  * free exprcontext
225  */
226  ExecFreeExprContext(&node->ps);
227 
228  /*
229  * shut down the subplan
230  */
231  outerPlan = outerPlanState(node);
232  ExecEndNode(outerPlan);
233 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:539
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:603
#define outerPlanState(node)
Definition: execnodes.h:896
PlanState ps
Definition: execnodes.h:2022
#define outerPlan(node)
Definition: plannodes.h:174

◆ ExecHash()

static TupleTableSlot* ExecHash ( PlanState pstate)
static

Definition at line 60 of file nodeHash.c.

References elog, and ERROR.

Referenced by ExecInitHash().

61 {
62  elog(ERROR, "Hash node does not support ExecProcNode call convention");
63  return NULL;
64 }
#define ERROR
Definition: elog.h:43
#define elog
Definition: elog.h:219

◆ ExecHashBuildSkewHash()

static void ExecHashBuildSkewHash ( HashJoinTable  hashtable,
Hash node,
int  mcvsToUse 
)
static

Definition at line 1290 of file nodeHash.c.

References ATTSTATSSLOT_NUMBERS, ATTSTATSSLOT_VALUES, HashJoinTableData::batchCxt, BoolGetDatum, DatumGetUInt32, free_attstatsslot(), FunctionCall1, get_attstatsslot(), HashSkewBucket::hashvalue, HeapTupleIsValid, i, Int16GetDatum, InvalidOid, MemoryContextAlloc(), MemoryContextAllocZero(), HashJoinTableData::nSkewBuckets, AttStatsSlot::numbers, AttStatsSlot::nvalues, ObjectIdGetDatum, OidIsValid, HashJoinTableData::outer_hashfunctions, ReleaseSysCache(), SearchSysCache3(), SKEW_BUCKET_OVERHEAD, SKEW_MIN_OUTER_FRACTION, HashJoinTableData::skewBucket, HashJoinTableData::skewBucketLen, HashJoinTableData::skewBucketNums, Hash::skewColumn, HashJoinTableData::skewEnabled, Hash::skewInherit, Hash::skewTable, HashJoinTableData::spacePeak, HashJoinTableData::spaceUsed, HashJoinTableData::spaceUsedSkew, STATISTIC_KIND_MCV, STATRELATTINH, HashSkewBucket::tuples, and AttStatsSlot::values.

Referenced by ExecHashTableCreate().

1291 {
1292  HeapTupleData *statsTuple;
1293  AttStatsSlot sslot;
1294 
1295  /* Do nothing if planner didn't identify the outer relation's join key */
1296  if (!OidIsValid(node->skewTable))
1297  return;
1298  /* Also, do nothing if we don't have room for at least one skew bucket */
1299  if (mcvsToUse <= 0)
1300  return;
1301 
1302  /*
1303  * Try to find the MCV statistics for the outer relation's join key.
1304  */
1305  statsTuple = SearchSysCache3(STATRELATTINH,
1306  ObjectIdGetDatum(node->skewTable),
1307  Int16GetDatum(node->skewColumn),
1308  BoolGetDatum(node->skewInherit));
1309  if (!HeapTupleIsValid(statsTuple))
1310  return;
1311 
1312  if (get_attstatsslot(&sslot, statsTuple,
1315  {
1316  double frac;
1317  int nbuckets;
1318  FmgrInfo *hashfunctions;
1319  int i;
1320 
1321  if (mcvsToUse > sslot.nvalues)
1322  mcvsToUse = sslot.nvalues;
1323 
1324  /*
1325  * Calculate the expected fraction of outer relation that will
1326  * participate in the skew optimization. If this isn't at least
1327  * SKEW_MIN_OUTER_FRACTION, don't use skew optimization.
1328  */
1329  frac = 0;
1330  for (i = 0; i < mcvsToUse; i++)
1331  frac += sslot.numbers[i];
1332  if (frac < SKEW_MIN_OUTER_FRACTION)
1333  {
1334  free_attstatsslot(&sslot);
1335  ReleaseSysCache(statsTuple);
1336  return;
1337  }
1338 
1339  /*
1340  * Okay, set up the skew hashtable.
1341  *
1342  * skewBucket[] is an open addressing hashtable with a power of 2 size
1343  * that is greater than the number of MCV values. (This ensures there
1344  * will be at least one null entry, so searches will always
1345  * terminate.)
1346  *
1347  * Note: this code could fail if mcvsToUse exceeds INT_MAX/8 or
1348  * MaxAllocSize/sizeof(void *)/8, but that is not currently possible
1349  * since we limit pg_statistic entries to much less than that.
1350  */
1351  nbuckets = 2;
1352  while (nbuckets <= mcvsToUse)
1353  nbuckets <<= 1;
1354  /* use two more bits just to help avoid collisions */
1355  nbuckets <<= 2;
1356 
1357  hashtable->skewEnabled = true;
1358  hashtable->skewBucketLen = nbuckets;
1359 
1360  /*
1361  * We allocate the bucket memory in the hashtable's batch context. It
1362  * is only needed during the first batch, and this ensures it will be
1363  * automatically removed once the first batch is done.
1364  */
1365  hashtable->skewBucket = (HashSkewBucket **)
1366  MemoryContextAllocZero(hashtable->batchCxt,
1367  nbuckets * sizeof(HashSkewBucket *));
1368  hashtable->skewBucketNums = (int *)
1369  MemoryContextAllocZero(hashtable->batchCxt,
1370  mcvsToUse * sizeof(int));
1371 
1372  hashtable->spaceUsed += nbuckets * sizeof(HashSkewBucket *)
1373  + mcvsToUse * sizeof(int);
1374  hashtable->spaceUsedSkew += nbuckets * sizeof(HashSkewBucket *)
1375  + mcvsToUse * sizeof(int);
1376  if (hashtable->spaceUsed > hashtable->spacePeak)
1377  hashtable->spacePeak = hashtable->spaceUsed;
1378 
1379  /*
1380  * Create a skew bucket for each MCV hash value.
1381  *
1382  * Note: it is very important that we create the buckets in order of
1383  * decreasing MCV frequency. If we have to remove some buckets, they
1384  * must be removed in reverse order of creation (see notes in
1385  * ExecHashRemoveNextSkewBucket) and we want the least common MCVs to
1386  * be removed first.
1387  */
1388  hashfunctions = hashtable->outer_hashfunctions;
1389 
1390  for (i = 0; i < mcvsToUse; i++)
1391  {
1392  uint32 hashvalue;
1393  int bucket;
1394 
1395  hashvalue = DatumGetUInt32(FunctionCall1(&hashfunctions[0],
1396  sslot.values[i]));
1397 
1398  /*
1399  * While we have not hit a hole in the hashtable and have not hit
1400  * the desired bucket, we have collided with some previous hash
1401  * value, so try the next bucket location. NB: this code must
1402  * match ExecHashGetSkewBucket.
1403  */
1404  bucket = hashvalue & (nbuckets - 1);
1405  while (hashtable->skewBucket[bucket] != NULL &&
1406  hashtable->skewBucket[bucket]->hashvalue != hashvalue)
1407  bucket = (bucket + 1) & (nbuckets - 1);
1408 
1409  /*
1410  * If we found an existing bucket with the same hashvalue, leave
1411  * it alone. It's okay for two MCVs to share a hashvalue.
1412  */
1413  if (hashtable->skewBucket[bucket] != NULL)
1414  continue;
1415 
1416  /* Okay, create a new skew bucket for this hashvalue. */
1417  hashtable->skewBucket[bucket] = (HashSkewBucket *)
1418  MemoryContextAlloc(hashtable->batchCxt,
1419  sizeof(HashSkewBucket));
1420  hashtable->skewBucket[bucket]->hashvalue = hashvalue;
1421  hashtable->skewBucket[bucket]->tuples = NULL;
1422  hashtable->skewBucketNums[hashtable->nSkewBuckets] = bucket;
1423  hashtable->nSkewBuckets++;
1424  hashtable->spaceUsed += SKEW_BUCKET_OVERHEAD;
1425  hashtable->spaceUsedSkew += SKEW_BUCKET_OVERHEAD;
1426  if (hashtable->spaceUsed > hashtable->spacePeak)
1427  hashtable->spacePeak = hashtable->spaceUsed;
1428  }
1429 
1430  free_attstatsslot(&sslot);
1431  }
1432 
1433  ReleaseSysCache(statsTuple);
1434 }
Oid skewTable
Definition: plannodes.h:879
#define DatumGetUInt32(X)
Definition: postgres.h:492
Definition: fmgr.h:56
#define SKEW_BUCKET_OVERHEAD
Definition: hashjoin.h:100
bool skewInherit
Definition: plannodes.h:881
#define SKEW_MIN_OUTER_FRACTION
Definition: hashjoin.h:103
#define ATTSTATSSLOT_VALUES
Definition: lsyscache.h:39
#define Int16GetDatum(X)
Definition: postgres.h:457
#define OidIsValid(objectId)
Definition: c.h:586
int * skewBucketNums
Definition: hashjoin.h:145
#define ATTSTATSSLOT_NUMBERS
Definition: lsyscache.h:40
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
HeapTuple SearchSysCache3(int cacheId, Datum key1, Datum key2, Datum key3)
Definition: syscache.c:1134
float4 * numbers
Definition: lsyscache.h:52
AttrNumber skewColumn
Definition: plannodes.h:880
HashJoinTuple tuples
Definition: hashjoin.h:97
unsigned int uint32
Definition: c.h:306
#define STATISTIC_KIND_MCV
Definition: pg_statistic.h:204
MemoryContext batchCxt
Definition: hashjoin.h:184
FmgrInfo * outer_hashfunctions
Definition: hashjoin.h:173
HashSkewBucket ** skewBucket
Definition: hashjoin.h:142
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:728
#define BoolGetDatum(X)
Definition: postgres.h:408
#define InvalidOid
Definition: postgres_ext.h:36
uint32 hashvalue
Definition: hashjoin.h:96
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:2928
Datum * values
Definition: lsyscache.h:49
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:693
int i
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:603
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3044

◆ ExecHashEstimate()

void ExecHashEstimate ( HashState node,
ParallelContext pcxt 
)

Definition at line 1644 of file nodeHash.c.

References add_size(), ParallelContext::estimator, mul_size(), ParallelContext::nworkers, offsetof, shm_toc_estimate_chunk, and shm_toc_estimate_keys.

Referenced by ExecParallelEstimate().

1645 {
1646  size_t size;
1647 
1648  size = mul_size(pcxt->nworkers, sizeof(HashInstrumentation));
1649  size = add_size(size, offsetof(SharedHashInfo, hinstrument));
1650  shm_toc_estimate_chunk(&pcxt->estimator, size);
1651  shm_toc_estimate_keys(&pcxt->estimator, 1);
1652 }
shm_toc_estimator estimator
Definition: parallel.h:41
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define offsetof(type, field)
Definition: c.h:603

◆ ExecHashGetBucketAndBatch()

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:306

◆ ExecHashGetHashValue()

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:492
Definition: fmgr.h:56
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:204
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:277
unsigned int uint32
Definition: c.h:306
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:603
bool * hashStrict
Definition: hashjoin.h:175
#define ResetExprContext(econtext)
Definition: executor.h:462

◆ ExecHashGetInstrumentation()

void ExecHashGetInstrumentation ( HashInstrumentation instrument,
HashJoinTable  hashtable 
)

◆ ExecHashGetSkewBucket()

int ExecHashGetSkewBucket ( HashJoinTable  hashtable,
uint32  hashvalue 
)

Definition at line 1444 of file nodeHash.c.

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

Referenced by ExecHashJoin(), and MultiExecHash().

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

◆ ExecHashIncreaseNumBatches()

static void ExecHashIncreaseNumBatches ( HashJoinTable  hashtable)
static

Definition at line 590 of file nodeHash.c.

References Assert, HashJoinTableData::buckets, CHECK_FOR_INTERRUPTS, HashJoinTableData::chunks, HashJoinTableData::curbatch, HashMemoryChunkData::data, dense_alloc(), ExecHashGetBucketAndBatch(), ExecHashJoinSaveTuple(), HashJoinTableData::growEnabled, HashJoinTableData::hashCxt, HashJoinTupleData::hashvalue, HJTUPLE_MINTUPLE, HJTUPLE_OVERHEAD, idx(), HashJoinTableData::innerBatchFile, HashJoinTableData::log2_nbuckets, HashJoinTableData::log2_nbuckets_optimal, MAXALIGN, MaxAllocSize, MemoryContextSwitchTo(), MemSet, Min, HashJoinTableData::nbatch, HashJoinTableData::nbuckets, HashJoinTableData::nbuckets_optimal, HashJoinTupleData::next, HashMemoryChunkData::next, HashJoinTableData::outerBatchFile, palloc0(), pfree(), PrepareTempTablespaces(), repalloc(), HashJoinTableData::spaceUsed, and MinimalTupleData::t_len.

Referenced by ExecHashSkewTableInsert(), and ExecHashTableInsert().

591 {
592  int oldnbatch = hashtable->nbatch;
593  int curbatch = hashtable->curbatch;
594  int nbatch;
595  MemoryContext oldcxt;
596  long ninmemory;
597  long nfreed;
598  HashMemoryChunk oldchunks;
599 
600  /* do nothing if we've decided to shut off growth */
601  if (!hashtable->growEnabled)
602  return;
603 
604  /* safety check to avoid overflow */
605  if (oldnbatch > Min(INT_MAX / 2, MaxAllocSize / (sizeof(void *) * 2)))
606  return;
607 
608  nbatch = oldnbatch * 2;
609  Assert(nbatch > 1);
610 
611 #ifdef HJDEBUG
612  printf("Hashjoin %p: increasing nbatch to %d because space = %zu\n",
613  hashtable, nbatch, hashtable->spaceUsed);
614 #endif
615 
616  oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
617 
618  if (hashtable->innerBatchFile == NULL)
619  {
620  /* we had no file arrays before */
621  hashtable->innerBatchFile = (BufFile **)
622  palloc0(nbatch * sizeof(BufFile *));
623  hashtable->outerBatchFile = (BufFile **)
624  palloc0(nbatch * sizeof(BufFile *));
625  /* time to establish the temp tablespaces, too */
627  }
628  else
629  {
630  /* enlarge arrays and zero out added entries */
631  hashtable->innerBatchFile = (BufFile **)
632  repalloc(hashtable->innerBatchFile, nbatch * sizeof(BufFile *));
633  hashtable->outerBatchFile = (BufFile **)
634  repalloc(hashtable->outerBatchFile, nbatch * sizeof(BufFile *));
635  MemSet(hashtable->innerBatchFile + oldnbatch, 0,
636  (nbatch - oldnbatch) * sizeof(BufFile *));
637  MemSet(hashtable->outerBatchFile + oldnbatch, 0,
638  (nbatch - oldnbatch) * sizeof(BufFile *));
639  }
640 
641  MemoryContextSwitchTo(oldcxt);
642 
643  hashtable->nbatch = nbatch;
644 
645  /*
646  * Scan through the existing hash table entries and dump out any that are
647  * no longer of the current batch.
648  */
649  ninmemory = nfreed = 0;
650 
651  /* If know we need to resize nbuckets, we can do it while rebatching. */
652  if (hashtable->nbuckets_optimal != hashtable->nbuckets)
653  {
654  /* we never decrease the number of buckets */
655  Assert(hashtable->nbuckets_optimal > hashtable->nbuckets);
656 
657  hashtable->nbuckets = hashtable->nbuckets_optimal;
658  hashtable->log2_nbuckets = hashtable->log2_nbuckets_optimal;
659 
660  hashtable->buckets = repalloc(hashtable->buckets,
661  sizeof(HashJoinTuple) * hashtable->nbuckets);
662  }
663 
664  /*
665  * We will scan through the chunks directly, so that we can reset the
666  * buckets now and not have to keep track which tuples in the buckets have
667  * already been processed. We will free the old chunks as we go.
668  */
669  memset(hashtable->buckets, 0, sizeof(HashJoinTuple) * hashtable->nbuckets);
670  oldchunks = hashtable->chunks;
671  hashtable->chunks = NULL;
672 
673  /* so, let's scan through the old chunks, and all tuples in each chunk */
674  while (oldchunks != NULL)
675  {
676  HashMemoryChunk nextchunk = oldchunks->next;
677 
678  /* position within the buffer (up to oldchunks->used) */
679  size_t idx = 0;
680 
681  /* process all tuples stored in this chunk (and then free it) */
682  while (idx < oldchunks->used)
683  {
684  HashJoinTuple hashTuple = (HashJoinTuple) (oldchunks->data + idx);
685  MinimalTuple tuple = HJTUPLE_MINTUPLE(hashTuple);
686  int hashTupleSize = (HJTUPLE_OVERHEAD + tuple->t_len);
687  int bucketno;
688  int batchno;
689 
690  ninmemory++;
691  ExecHashGetBucketAndBatch(hashtable, hashTuple->hashvalue,
692  &bucketno, &batchno);
693 
694  if (batchno == curbatch)
695  {
696  /* keep tuple in memory - copy it into the new chunk */
697  HashJoinTuple copyTuple;
698 
699  copyTuple = (HashJoinTuple) dense_alloc(hashtable, hashTupleSize);
700  memcpy(copyTuple, hashTuple, hashTupleSize);
701 
702  /* and add it back to the appropriate bucket */
703  copyTuple->next = hashtable->buckets[bucketno];
704  hashtable->buckets[bucketno] = copyTuple;
705  }
706  else
707  {
708  /* dump it out */
709  Assert(batchno > curbatch);
711  hashTuple->hashvalue,
712  &hashtable->innerBatchFile[batchno]);
713 
714  hashtable->spaceUsed -= hashTupleSize;
715  nfreed++;
716  }
717 
718  /* next tuple in this chunk */
719  idx += MAXALIGN(hashTupleSize);
720 
721  /* allow this loop to be cancellable */
723  }
724 
725  /* we're done with this chunk - free it and proceed to the next one */
726  pfree(oldchunks);
727  oldchunks = nextchunk;
728  }
729 
730 #ifdef HJDEBUG
731  printf("Hashjoin %p: freed %ld of %ld tuples, space now %zu\n",
732  hashtable, nfreed, ninmemory, hashtable->spaceUsed);
733 #endif
734 
735  /*
736  * If we dumped out either all or none of the tuples in the table, disable
737  * further expansion of nbatch. This situation implies that we have
738  * enough tuples of identical hashvalues to overflow spaceAllowed.
739  * Increasing nbatch will not fix it since there's no way to subdivide the
740  * group any more finely. We have to just gut it out and hope the server
741  * has enough RAM.
742  */
743  if (nfreed == 0 || nfreed == ninmemory)
744  {
745  hashtable->growEnabled = false;
746 #ifdef HJDEBUG
747  printf("Hashjoin %p: disabling further increase of nbatch\n",
748  hashtable);
749 #endif
750  }
751 }
int log2_nbuckets_optimal
Definition: hashjoin.h:133
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:136
#define Min(x, y)
Definition: c.h:812
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define MemSet(start, val, len)
Definition: c.h:863
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1031
void pfree(void *pointer)
Definition: mcxt.c:936
void PrepareTempTablespaces(void)
Definition: tablespace.c:1287
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1708
BufFile ** outerBatchFile
Definition: hashjoin.h:166
struct HashMemoryChunkData * next
Definition: hashjoin.h:115
#define MaxAllocSize
Definition: memutils.h:40
void * palloc0(Size size)
Definition: mcxt.c:864
static void * dense_alloc(HashJoinTable hashtable, Size size)
Definition: nodeHash.c:1748
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:71
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
#define Assert(condition)
Definition: c.h:680
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: hashjoin.h:118
BufFile ** innerBatchFile
Definition: hashjoin.h:165
struct HashJoinTupleData * next
Definition: hashjoin.h:66
#define MAXALIGN(LEN)
Definition: c.h:633
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:949
HashMemoryChunk chunks
Definition: hashjoin.h:187
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
MemoryContext hashCxt
Definition: hashjoin.h:183
void ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr)
Definition: nodeHashjoin.c:818
uint32 hashvalue
Definition: hashjoin.h:67

◆ ExecHashIncreaseNumBuckets()

static void ExecHashIncreaseNumBuckets ( HashJoinTable  hashtable)
static

Definition at line 759 of file nodeHash.c.

References Assert, HashJoinTableData::buckets, CHECK_FOR_INTERRUPTS, HashJoinTableData::chunks, HashMemoryChunkData::data, ExecHashGetBucketAndBatch(), HashJoinTupleData::hashvalue, HJTUPLE_MINTUPLE, HJTUPLE_OVERHEAD, idx(), HashJoinTableData::log2_nbuckets, HashJoinTableData::log2_nbuckets_optimal, MAXALIGN, HashJoinTableData::nbuckets, HashJoinTableData::nbuckets_optimal, HashJoinTupleData::next, HashMemoryChunkData::next, and repalloc().

Referenced by MultiExecHash().

760 {
761  HashMemoryChunk chunk;
762 
763  /* do nothing if not an increase (it's called increase for a reason) */
764  if (hashtable->nbuckets >= hashtable->nbuckets_optimal)
765  return;
766 
767 #ifdef HJDEBUG
768  printf("Hashjoin %p: increasing nbuckets %d => %d\n",
769  hashtable, hashtable->nbuckets, hashtable->nbuckets_optimal);
770 #endif
771 
772  hashtable->nbuckets = hashtable->nbuckets_optimal;
773  hashtable->log2_nbuckets = hashtable->log2_nbuckets_optimal;
774 
775  Assert(hashtable->nbuckets > 1);
776  Assert(hashtable->nbuckets <= (INT_MAX / 2));
777  Assert(hashtable->nbuckets == (1 << hashtable->log2_nbuckets));
778 
779  /*
780  * Just reallocate the proper number of buckets - we don't need to walk
781  * through them - we can walk the dense-allocated chunks (just like in
782  * ExecHashIncreaseNumBatches, but without all the copying into new
783  * chunks)
784  */
785  hashtable->buckets =
786  (HashJoinTuple *) repalloc(hashtable->buckets,
787  hashtable->nbuckets * sizeof(HashJoinTuple));
788 
789  memset(hashtable->buckets, 0, hashtable->nbuckets * sizeof(HashJoinTuple));
790 
791  /* scan through all tuples in all chunks to rebuild the hash table */
792  for (chunk = hashtable->chunks; chunk != NULL; chunk = chunk->next)
793  {
794  /* process all tuples stored in this chunk */
795  size_t idx = 0;
796 
797  while (idx < chunk->used)
798  {
799  HashJoinTuple hashTuple = (HashJoinTuple) (chunk->data + idx);
800  int bucketno;
801  int batchno;
802 
803  ExecHashGetBucketAndBatch(hashtable, hashTuple->hashvalue,
804  &bucketno, &batchno);
805 
806  /* add the tuple to the proper bucket */
807  hashTuple->next = hashtable->buckets[bucketno];
808  hashtable->buckets[bucketno] = hashTuple;
809 
810  /* advance index past the tuple */
811  idx += MAXALIGN(HJTUPLE_OVERHEAD +
812  HJTUPLE_MINTUPLE(hashTuple)->t_len);
813  }
814 
815  /* allow this loop to be cancellable */
817  }
818 }
int log2_nbuckets_optimal
Definition: hashjoin.h:133
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:136
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1031
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1708
struct HashMemoryChunkData * next
Definition: hashjoin.h:115
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:71
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
#define Assert(condition)
Definition: c.h:680
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: hashjoin.h:118
struct HashJoinTupleData * next
Definition: hashjoin.h:66
#define MAXALIGN(LEN)
Definition: c.h:633
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:949
HashMemoryChunk chunks
Definition: hashjoin.h:187
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
uint32 hashvalue
Definition: hashjoin.h:67

◆ ExecHashInitializeDSM()

void ExecHashInitializeDSM ( HashState node,
ParallelContext pcxt 
)

Definition at line 1659 of file nodeHash.c.

References SharedHashInfo::num_workers, ParallelContext::nworkers, offsetof, PlanState::plan, Plan::plan_node_id, HashState::ps, HashState::shared_info, shm_toc_allocate(), shm_toc_insert(), and ParallelContext::toc.

Referenced by ExecParallelInitializeDSM().

1660 {
1661  size_t size;
1662 
1663  size = offsetof(SharedHashInfo, hinstrument) +
1664  pcxt->nworkers * sizeof(HashInstrumentation);
1665  node->shared_info = (SharedHashInfo *) shm_toc_allocate(pcxt->toc, size);
1666  memset(node->shared_info, 0, size);
1667  node->shared_info->num_workers = pcxt->nworkers;
1668  shm_toc_insert(pcxt->toc, node->ps.plan->plan_node_id,
1669  node->shared_info);
1670 }
struct HashInstrumentation HashInstrumentation
int plan_node_id
Definition: plannodes.h:143
SharedHashInfo * shared_info
Definition: execnodes.h:2027
PlanState ps
Definition: execnodes.h:2022
Plan * plan
Definition: execnodes.h:850
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
#define offsetof(type, field)
Definition: c.h:603
shm_toc * toc
Definition: parallel.h:44

◆ ExecHashInitializeWorker()

void ExecHashInitializeWorker ( HashState node,
ParallelWorkerContext pwcxt 
)

Definition at line 1690 of file nodeHash.c.

References SharedHashInfo::hinstrument, HashState::hinstrument, ParallelWorkerNumber, PlanState::plan, Plan::plan_node_id, HashState::ps, shm_toc_lookup(), and ParallelWorkerContext::toc.

Referenced by ExecParallelInitializeWorker().

1691 {
1692  SharedHashInfo *shared_info;
1693 
1694  shared_info = (SharedHashInfo *)
1695  shm_toc_lookup(pwcxt->toc, node->ps.plan->plan_node_id, true);
1696  node->hinstrument = &shared_info->hinstrument[ParallelWorkerNumber];
1697 }
int plan_node_id
Definition: plannodes.h:143
int ParallelWorkerNumber
Definition: parallel.c:100
PlanState ps
Definition: execnodes.h:2022
HashInstrumentation * hinstrument
Definition: execnodes.h:2028
HashInstrumentation hinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:2013
Plan * plan
Definition: execnodes.h:850
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

◆ ExecHashReInitializeDSM()

void ExecHashReInitializeDSM ( HashState node,
ParallelContext pcxt 
)

Definition at line 1676 of file nodeHash.c.

References SharedHashInfo::hinstrument, SharedHashInfo::num_workers, and HashState::shared_info.

Referenced by ExecParallelReInitializeDSM().

1677 {
1678  if (node->shared_info != NULL)
1679  {
1680  memset(node->shared_info->hinstrument, 0,
1681  node->shared_info->num_workers * sizeof(HashInstrumentation));
1682  }
1683 }
SharedHashInfo * shared_info
Definition: execnodes.h:2027
HashInstrumentation hinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:2013

◆ ExecHashRemoveNextSkewBucket()

static void ExecHashRemoveNextSkewBucket ( HashJoinTable  hashtable)
static

Definition at line 1531 of file nodeHash.c.

References Assert, HashJoinTableData::buckets, CHECK_FOR_INTERRUPTS, HashJoinTableData::curbatch, dense_alloc(), ExecHashGetBucketAndBatch(), ExecHashJoinSaveTuple(), HashSkewBucket::hashvalue, HJTUPLE_MINTUPLE, HJTUPLE_OVERHEAD, HashJoinTableData::innerBatchFile, HashJoinTupleData::next, HashJoinTableData::nSkewBuckets, pfree(), SKEW_BUCKET_OVERHEAD, HashJoinTableData::skewBucket, HashJoinTableData::skewBucketNums, HashJoinTableData::skewEnabled, HashJoinTableData::spaceUsed, HashJoinTableData::spaceUsedSkew, MinimalTupleData::t_len, and HashSkewBucket::tuples.

Referenced by ExecHashSkewTableInsert().

1532 {
1533  int bucketToRemove;
1534  HashSkewBucket *bucket;
1535  uint32 hashvalue;
1536  int bucketno;
1537  int batchno;
1538  HashJoinTuple hashTuple;
1539 
1540  /* Locate the bucket to remove */
1541  bucketToRemove = hashtable->skewBucketNums[hashtable->nSkewBuckets - 1];
1542  bucket = hashtable->skewBucket[bucketToRemove];
1543 
1544  /*
1545  * Calculate which bucket and batch the tuples belong to in the main
1546  * hashtable. They all have the same hash value, so it's the same for all
1547  * of them. Also note that it's not possible for nbatch to increase while
1548  * we are processing the tuples.
1549  */
1550  hashvalue = bucket->hashvalue;
1551  ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno, &batchno);
1552 
1553  /* Process all tuples in the bucket */
1554  hashTuple = bucket->tuples;
1555  while (hashTuple != NULL)
1556  {
1557  HashJoinTuple nextHashTuple = hashTuple->next;
1558  MinimalTuple tuple;
1559  Size tupleSize;
1560 
1561  /*
1562  * This code must agree with ExecHashTableInsert. We do not use
1563  * ExecHashTableInsert directly as ExecHashTableInsert expects a
1564  * TupleTableSlot while we already have HashJoinTuples.
1565  */
1566  tuple = HJTUPLE_MINTUPLE(hashTuple);
1567  tupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
1568 
1569  /* Decide whether to put the tuple in the hash table or a temp file */
1570  if (batchno == hashtable->curbatch)
1571  {
1572  /* Move the tuple to the main hash table */
1573  HashJoinTuple copyTuple;
1574 
1575  /*
1576  * We must copy the tuple into the dense storage, else it will not
1577  * be found by, eg, ExecHashIncreaseNumBatches.
1578  */
1579  copyTuple = (HashJoinTuple) dense_alloc(hashtable, tupleSize);
1580  memcpy(copyTuple, hashTuple, tupleSize);
1581  pfree(hashTuple);
1582 
1583  copyTuple->next = hashtable->buckets[bucketno];
1584  hashtable->buckets[bucketno] = copyTuple;
1585 
1586  /* We have reduced skew space, but overall space doesn't change */
1587  hashtable->spaceUsedSkew -= tupleSize;
1588  }
1589  else
1590  {
1591  /* Put the tuple into a temp file for later batches */
1592  Assert(batchno > hashtable->curbatch);
1593  ExecHashJoinSaveTuple(tuple, hashvalue,
1594  &hashtable->innerBatchFile[batchno]);
1595  pfree(hashTuple);
1596  hashtable->spaceUsed -= tupleSize;
1597  hashtable->spaceUsedSkew -= tupleSize;
1598  }
1599 
1600  hashTuple = nextHashTuple;
1601 
1602  /* allow this loop to be cancellable */
1604  }
1605 
1606  /*
1607  * Free the bucket struct itself and reset the hashtable entry to NULL.
1608  *
1609  * NOTE: this is not nearly as simple as it looks on the surface, because
1610  * of the possibility of collisions in the hashtable. Suppose that hash
1611  * values A and B collide at a particular hashtable entry, and that A was
1612  * entered first so B gets shifted to a different table entry. If we were
1613  * to remove A first then ExecHashGetSkewBucket would mistakenly start
1614  * reporting that B is not in the hashtable, because it would hit the NULL
1615  * before finding B. However, we always remove entries in the reverse
1616  * order of creation, so this failure cannot happen.
1617  */
1618  hashtable->skewBucket[bucketToRemove] = NULL;
1619  hashtable->nSkewBuckets--;
1620  pfree(bucket);
1621  hashtable->spaceUsed -= SKEW_BUCKET_OVERHEAD;
1622  hashtable->spaceUsedSkew -= SKEW_BUCKET_OVERHEAD;
1623 
1624  /*
1625  * If we have removed all skew buckets then give up on skew optimization.
1626  * Release the arrays since they aren't useful any more.
1627  */
1628  if (hashtable->nSkewBuckets == 0)
1629  {
1630  hashtable->skewEnabled = false;
1631  pfree(hashtable->skewBucket);
1632  pfree(hashtable->skewBucketNums);
1633  hashtable->skewBucket = NULL;
1634  hashtable->skewBucketNums = NULL;
1635  hashtable->spaceUsed -= hashtable->spaceUsedSkew;
1636  hashtable->spaceUsedSkew = 0;
1637  }
1638 }
#define SKEW_BUCKET_OVERHEAD
Definition: hashjoin.h:100
struct HashJoinTupleData ** buckets
Definition: hashjoin.h:136
int * skewBucketNums
Definition: hashjoin.h:145
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1031
void pfree(void *pointer)
Definition: mcxt.c:936
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1708
HashJoinTuple tuples
Definition: hashjoin.h:97
unsigned int uint32
Definition: c.h:306
HashSkewBucket ** skewBucket
Definition: hashjoin.h:142
static void * dense_alloc(HashJoinTable hashtable, Size size)
Definition: nodeHash.c:1748
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:71
uint32 hashvalue
Definition: hashjoin.h:96
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
#define Assert(condition)
Definition: c.h:680
size_t Size
Definition: c.h:414
BufFile ** innerBatchFile
Definition: hashjoin.h:165
struct HashJoinTupleData * next
Definition: hashjoin.h:66
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
void ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr)
Definition: nodeHashjoin.c:818

◆ ExecHashRetrieveInstrumentation()

void ExecHashRetrieveInstrumentation ( HashState node)

Definition at line 1717 of file nodeHash.c.

References SharedHashInfo::num_workers, offsetof, palloc(), and HashState::shared_info.

Referenced by ExecParallelRetrieveInstrumentation().

1718 {
1719  SharedHashInfo *shared_info = node->shared_info;
1720  size_t size;
1721 
1722  /* Replace node->shared_info with a copy in backend-local memory. */
1723  size = offsetof(SharedHashInfo, hinstrument) +
1724  shared_info->num_workers * sizeof(HashInstrumentation);
1725  node->shared_info = palloc(size);
1726  memcpy(node->shared_info, shared_info, size);
1727 }
struct HashInstrumentation HashInstrumentation
SharedHashInfo * shared_info
Definition: execnodes.h:2027
void * palloc(Size size)
Definition: mcxt.c:835
#define offsetof(type, field)
Definition: c.h:603

◆ ExecHashSkewTableInsert()

static void ExecHashSkewTableInsert ( HashJoinTable  hashtable,
TupleTableSlot slot,
uint32  hashvalue,
int  bucketNumber 
)
static

Definition at line 1490 of file nodeHash.c.

References HashJoinTableData::batchCxt, ExecFetchSlotMinimalTuple(), ExecHashIncreaseNumBatches(), ExecHashRemoveNextSkewBucket(), HashJoinTupleData::hashvalue, HeapTupleHeaderClearMatch, HJTUPLE_MINTUPLE, HJTUPLE_OVERHEAD, MemoryContextAlloc(), HashJoinTupleData::next, HashJoinTableData::skewBucket, HashJoinTableData::spaceAllowed, HashJoinTableData::spaceAllowedSkew, HashJoinTableData::spacePeak, HashJoinTableData::spaceUsed, HashJoinTableData::spaceUsedSkew, MinimalTupleData::t_len, and HashSkewBucket::tuples.

Referenced by MultiExecHash().

1494 {
1496  HashJoinTuple hashTuple;
1497  int hashTupleSize;
1498 
1499  /* Create the HashJoinTuple */
1500  hashTupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
1501  hashTuple = (HashJoinTuple) MemoryContextAlloc(hashtable->batchCxt,
1502  hashTupleSize);
1503  hashTuple->hashvalue = hashvalue;
1504  memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
1506 
1507  /* Push it onto the front of the skew bucket's list */
1508  hashTuple->next = hashtable->skewBucket[bucketNumber]->tuples;
1509  hashtable->skewBucket[bucketNumber]->tuples = hashTuple;
1510 
1511  /* Account for space used, and back off if we've used too much */
1512  hashtable->spaceUsed += hashTupleSize;
1513  hashtable->spaceUsedSkew += hashTupleSize;
1514  if (hashtable->spaceUsed > hashtable->spacePeak)
1515  hashtable->spacePeak = hashtable->spaceUsed;
1516  while (hashtable->spaceUsedSkew > hashtable->spaceAllowedSkew)
1517  ExecHashRemoveNextSkewBucket(hashtable);
1518 
1519  /* Check we are not over the total spaceAllowed, either */
1520  if (hashtable->spaceUsed > hashtable->spaceAllowed)
1521  ExecHashIncreaseNumBatches(hashtable);
1522 }
static void ExecHashRemoveNextSkewBucket(HashJoinTable hashtable)
Definition: nodeHash.c:1531
MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot)
Definition: execTuples.c:652
static void ExecHashIncreaseNumBatches(HashJoinTable hashtable)
Definition: nodeHash.c:590
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1708
Size spaceAllowedSkew
Definition: hashjoin.h:181
HashJoinTuple tuples
Definition: hashjoin.h:97
MemoryContext batchCxt
Definition: hashjoin.h:184
HashSkewBucket ** skewBucket
Definition: hashjoin.h:142
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:71
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
struct HashJoinTupleData * next
Definition: hashjoin.h:66
#define HeapTupleHeaderClearMatch(tup)
Definition: htup_details.h:532
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:693
uint32 hashvalue
Definition: hashjoin.h:67

◆ ExecHashTableCreate()

HashJoinTable ExecHashTableCreate ( Hash node,
List hashOperators,
bool  keepNulls 
)

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

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

◆ ExecHashTableDestroy()

void ExecHashTableDestroy ( HashJoinTable  hashtable)

Definition at line 560 of file nodeHash.c.

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

Referenced by ExecEndHashJoin(), and ExecReScanHashJoin().

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

◆ ExecHashTableInsert()

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: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:590
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1031
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1708
#define MaxAllocSize
Definition: memutils.h:40
static void * dense_alloc(HashJoinTable hashtable, Size size)
Definition: nodeHash.c:1748
#define NTUP_PER_BUCKET
Definition: nodeHash.c:398
#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:680
BufFile ** innerBatchFile
Definition: hashjoin.h:165
struct HashJoinTupleData * next
Definition: hashjoin.h:66
#define HeapTupleHeaderClearMatch(tup)
Definition: htup_details.h:532
void ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr)
Definition: nodeHashjoin.c:818
uint32 hashvalue
Definition: hashjoin.h:67

◆ ExecHashTableReset()

void ExecHashTableReset ( HashJoinTable  hashtable)

Definition at line 1216 of file nodeHash.c.

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

Referenced by ExecHashJoinNewBatch().

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

◆ ExecHashTableResetMatchFlags()

void ExecHashTableResetMatchFlags ( HashJoinTable  hashtable)

Definition at line 1245 of file nodeHash.c.

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

Referenced by ExecReScanHashJoin().

1246 {
1247  HashJoinTuple tuple;
1248  int i;
1249 
1250  /* Reset all flags in the main table ... */
1251  for (i = 0; i < hashtable->nbuckets; i++)
1252  {
1253  for (tuple = hashtable->buckets[i]; tuple != NULL; tuple = tuple->next)
1255  }
1256 
1257  /* ... and the same for the skew buckets, if any */
1258  for (i = 0; i < hashtable->nSkewBuckets; i++)
1259  {
1260  int j = hashtable->skewBucketNums[i];
1261  HashSkewBucket *skewBucket = hashtable->skewBucket[j];
1262 
1263  for (tuple = skewBucket->tuples; tuple != NULL; tuple = tuple->next)
1265  }
1266 }
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
struct HashJoinTupleData * next
Definition: hashjoin.h:66
#define HeapTupleHeaderClearMatch(tup)
Definition: htup_details.h:532
int i

◆ ExecInitHash()

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(), ExecHash(), ExecInitNode(), ExecInitQual(), ExecInitResultTupleSlot(), PlanState::ExecProcNode, HashState::hashkeys, HashState::hashtable, makeNode, NIL, 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->ps.ExecProcNode = ExecHash;
176  hashstate->hashtable = NULL;
177  hashstate->hashkeys = NIL; /* will be set by parent HashJoin */
178 
179  /*
180  * Miscellaneous initialization
181  *
182  * create expression context for node
183  */
184  ExecAssignExprContext(estate, &hashstate->ps);
185 
186  /*
187  * initialize our result slot
188  */
189  ExecInitResultTupleSlot(estate, &hashstate->ps);
190 
191  /*
192  * initialize child expressions
193  */
194  hashstate->ps.qual =
195  ExecInitQual(node->plan.qual, (PlanState *) hashstate);
196 
197  /*
198  * initialize child nodes
199  */
200  outerPlanState(hashstate) = ExecInitNode(outerPlan(node), estate, eflags);
201 
202  /*
203  * initialize tuple type. no need to initialize projection info because
204  * this node doesn't do projections
205  */
206  ExecAssignResultTypeFromTL(&hashstate->ps);
207  hashstate->ps.ps_ProjInfo = NULL;
208 
209  return hashstate;
210 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:885
HashJoinTable hashtable
Definition: execnodes.h:2023
EState * state
Definition: execnodes.h:852
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:448
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:896
List * hashkeys
Definition: execnodes.h:2024
PlanState ps
Definition: execnodes.h:2022
#define outerPlan(node)
Definition: plannodes.h:174
static TupleTableSlot * ExecHash(PlanState *pstate)
Definition: nodeHash.c:60
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:856
Plan * plan
Definition: execnodes.h:850
#define makeNode(_type_)
Definition: nodes.h:560
#define Assert(condition)
Definition: c.h:680
#define EXEC_FLAG_MARK
Definition: executor.h:61
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:426
ExprState * qual
Definition: execnodes.h:868
Plan plan
Definition: plannodes.h:878
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139

◆ ExecPrepHashTableForUnmatched()

void ExecPrepHashTableForUnmatched ( HashJoinState hjstate)

Definition at line 1121 of file nodeHash.c.

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

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:1721
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1722
int hj_CurBucketNo
Definition: execnodes.h:1720

◆ ExecReScanHash()

void ExecReScanHash ( HashState node)

Definition at line 1270 of file nodeHash.c.

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

Referenced by ExecReScan().

1271 {
1272  /*
1273  * if chgParam of subnode is not null then plan will be re-scanned by
1274  * first ExecProcNode.
1275  */
1276  if (node->ps.lefttree->chgParam == NULL)
1277  ExecReScan(node->ps.lefttree);
1278 }
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
struct PlanState * lefttree
Definition: execnodes.h:869
PlanState ps
Definition: execnodes.h:2022
Bitmapset * chgParam
Definition: execnodes.h:878

◆ ExecScanHashBucket()

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, ResetExprContext, HashJoinTableData::skewBucket, and HashSkewBucket::tuples.

Referenced by ExecHashJoin().

1065 {
1066  ExprState *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))
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:136
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:357
uint32 hj_CurHashValue
Definition: execnodes.h:1719
int hj_CurSkewBucketNo
Definition: execnodes.h:1721
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1722
HashJoinTuple tuples
Definition: hashjoin.h:97
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:199
unsigned int uint32
Definition: c.h:306
int hj_CurBucketNo
Definition: execnodes.h:1720
HashSkewBucket ** skewBucket
Definition: hashjoin.h:142
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
struct HashJoinTupleData * next
Definition: hashjoin.h:66
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1724
HashJoinTable hj_HashTable
Definition: execnodes.h:1718
uint32 hashvalue
Definition: hashjoin.h:67
ExprState * hashclauses
Definition: execnodes.h:1714
#define ResetExprContext(econtext)
Definition: executor.h:462

◆ ExecScanHashTableForUnmatched()

bool ExecScanHashTableForUnmatched ( HashJoinState hjstate,
ExprContext econtext 
)

Definition at line 1145 of file nodeHash.c.

References HashJoinTableData::buckets, CHECK_FOR_INTERRUPTS, 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, 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  /* allow this loop to be cancellable */
1202  }
1203 
1204  /*
1205  * no more unmatched tuples
1206  */
1207  return false;
1208 }
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:1721
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1722
HashJoinTuple tuples
Definition: hashjoin.h:97
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:199
int hj_CurBucketNo
Definition: execnodes.h:1720
HashSkewBucket ** skewBucket
Definition: hashjoin.h:142
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:72
#define HeapTupleHeaderHasMatch(tup)
Definition: htup_details.h:522
struct HashJoinTupleData * next
Definition: hashjoin.h:66
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1724
HashJoinTable hj_HashTable
Definition: execnodes.h:1718
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define ResetExprContext(econtext)
Definition: executor.h:462

◆ ExecShutdownHash()

void ExecShutdownHash ( HashState node)

Definition at line 1706 of file nodeHash.c.

References ExecHashGetInstrumentation(), HashState::hashtable, and HashState::hinstrument.

Referenced by ExecShutdownNode().

1707 {
1708  if (node->hinstrument && node->hashtable)
1710 }
void ExecHashGetInstrumentation(HashInstrumentation *instrument, HashJoinTable hashtable)
Definition: nodeHash.c:1734
HashJoinTable hashtable
Definition: execnodes.h:2023
HashInstrumentation * hinstrument
Definition: execnodes.h:2028

◆ MultiExecHash()

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, 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
Instrumentation * instrument
Definition: execnodes.h:860
ExprContext * ps_ExprContext
Definition: execnodes.h:884
HashJoinTable hashtable
Definition: execnodes.h:2023
int ExecHashGetSkewBucket(HashJoinTable hashtable, uint32 hashvalue)
Definition: nodeHash.c:1444
void ExecHashTableInsert(HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue)
Definition: nodeHash.c:833
static void ExecHashIncreaseNumBuckets(HashJoinTable hashtable)
Definition: nodeHash.c:759
void InstrStartNode(Instrumentation *instr)
Definition: instrument.c:63
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1708
#define outerPlanState(node)
Definition: execnodes.h:896
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:199
List * hashkeys
Definition: execnodes.h:2024
#define TupIsNull(slot)
Definition: tuptable.h:138
unsigned int uint32
Definition: c.h:306
PlanState ps
Definition: execnodes.h:2022
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:237
double totalTuples
Definition: hashjoin.h:155
static void ExecHashSkewTableInsert(HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue, int bucketNumber)
Definition: nodeHash.c:1490
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