PostgreSQL Source Code  git master
nodeHash.c File Reference
#include "postgres.h"
#include <math.h>
#include <limits.h>
#include "access/htup_details.h"
#include "access/parallel.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 "pgstat.h"
#include "port/atomics.h"
#include "port/pg_bitutils.h"
#include "utils/dynahash.h"
#include "utils/lsyscache.h"
#include "utils/memutils.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 ExecParallelHashIncreaseNumBatches (HashJoinTable hashtable)
 
static void ExecParallelHashIncreaseNumBuckets (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 HashJoinTuple ExecParallelHashTupleAlloc (HashJoinTable hashtable, size_t size, dsa_pointer *shared)
 
static void MultiExecPrivateHash (HashState *node)
 
static void MultiExecParallelHash (HashState *node)
 
static HashJoinTuple ExecParallelHashFirstTuple (HashJoinTable table, int bucketno)
 
static HashJoinTuple ExecParallelHashNextTuple (HashJoinTable table, HashJoinTuple tuple)
 
static void ExecParallelHashPushTuple (dsa_pointer_atomic *head, HashJoinTuple tuple, dsa_pointer tuple_shared)
 
static void ExecParallelHashJoinSetUpBatches (HashJoinTable hashtable, int nbatch)
 
static void ExecParallelHashEnsureBatchAccessors (HashJoinTable hashtable)
 
static void ExecParallelHashRepartitionFirst (HashJoinTable hashtable)
 
static void ExecParallelHashRepartitionRest (HashJoinTable hashtable)
 
static HashMemoryChunk ExecParallelHashPopChunkQueue (HashJoinTable table, dsa_pointer *shared)
 
static bool ExecParallelHashTuplePrealloc (HashJoinTable hashtable, int batchno, size_t size)
 
static void ExecParallelHashMergeCounters (HashJoinTable hashtable)
 
static void ExecParallelHashCloseBatchAccessors (HashJoinTable hashtable)
 
static TupleTableSlotExecHash (PlanState *pstate)
 
NodeMultiExecHash (HashState *node)
 
HashStateExecInitHash (Hash *node, EState *estate, int eflags)
 
void ExecEndHash (HashState *node)
 
HashJoinTable ExecHashTableCreate (HashState *state, List *hashOperators, List *hashCollations, bool keepNulls)
 
void ExecChooseHashTableSize (double ntuples, int tupwidth, bool useskew, bool try_combined_work_mem, int parallel_workers, size_t *space_allowed, int *numbuckets, int *numbatches, int *num_skew_mcvs)
 
void ExecHashTableDestroy (HashJoinTable hashtable)
 
void ExecHashTableInsert (HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue)
 
void ExecParallelHashTableInsert (HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue)
 
void ExecParallelHashTableInsertCurrentBatch (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)
 
bool ExecParallelScanHashBucket (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 ExecHashInitializeWorker (HashState *node, ParallelWorkerContext *pwcxt)
 
void ExecShutdownHash (HashState *node)
 
void ExecHashRetrieveInstrumentation (HashState *node)
 
void ExecHashAccumInstrumentation (HashInstrumentation *instrument, HashJoinTable hashtable)
 
void ExecParallelHashTableAlloc (HashJoinTable hashtable, int batchno)
 
void ExecHashTableDetachBatch (HashJoinTable hashtable)
 
void ExecHashTableDetach (HashJoinTable hashtable)
 
void ExecParallelHashTableSetCurrentBatch (HashJoinTable hashtable, int batchno)
 

Macro Definition Documentation

◆ NTUP_PER_BUCKET

#define NTUP_PER_BUCKET   1

Function Documentation

◆ dense_alloc()

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

Definition at line 2706 of file nodeHash.c.

References HashJoinTableData::batchCxt, HashJoinTableData::chunks, HASH_CHUNK_DATA, HASH_CHUNK_HEADER_SIZE, HASH_CHUNK_SIZE, HASH_CHUNK_THRESHOLD, MAXALIGN, HashMemoryChunkData::maxlen, MemoryContextAlloc(), HashMemoryChunkData::next, HashMemoryChunkData::ntuples, HashMemoryChunkData::unshared, and HashMemoryChunkData::used.

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

2707 {
2708  HashMemoryChunk newChunk;
2709  char *ptr;
2710 
2711  /* just in case the size is not already aligned properly */
2712  size = MAXALIGN(size);
2713 
2714  /*
2715  * If tuple size is larger than threshold, allocate a separate chunk.
2716  */
2717  if (size > HASH_CHUNK_THRESHOLD)
2718  {
2719  /* allocate new chunk and put it at the beginning of the list */
2720  newChunk = (HashMemoryChunk) MemoryContextAlloc(hashtable->batchCxt,
2721  HASH_CHUNK_HEADER_SIZE + size);
2722  newChunk->maxlen = size;
2723  newChunk->used = size;
2724  newChunk->ntuples = 1;
2725 
2726  /*
2727  * Add this chunk to the list after the first existing chunk, so that
2728  * we don't lose the remaining space in the "current" chunk.
2729  */
2730  if (hashtable->chunks != NULL)
2731  {
2732  newChunk->next = hashtable->chunks->next;
2733  hashtable->chunks->next.unshared = newChunk;
2734  }
2735  else
2736  {
2737  newChunk->next.unshared = hashtable->chunks;
2738  hashtable->chunks = newChunk;
2739  }
2740 
2741  return HASH_CHUNK_DATA(newChunk);
2742  }
2743 
2744  /*
2745  * See if we have enough space for it in the current chunk (if any). If
2746  * not, allocate a fresh chunk.
2747  */
2748  if ((hashtable->chunks == NULL) ||
2749  (hashtable->chunks->maxlen - hashtable->chunks->used) < size)
2750  {
2751  /* allocate new chunk and put it at the beginning of the list */
2752  newChunk = (HashMemoryChunk) MemoryContextAlloc(hashtable->batchCxt,
2754 
2755  newChunk->maxlen = HASH_CHUNK_SIZE;
2756  newChunk->used = size;
2757  newChunk->ntuples = 1;
2758 
2759  newChunk->next.unshared = hashtable->chunks;
2760  hashtable->chunks = newChunk;
2761 
2762  return HASH_CHUNK_DATA(newChunk);
2763  }
2764 
2765  /* There is enough space in the current chunk, let's add the tuple */
2766  ptr = HASH_CHUNK_DATA(hashtable->chunks) + hashtable->chunks->used;
2767  hashtable->chunks->used += size;
2768  hashtable->chunks->ntuples += 1;
2769 
2770  /* return pointer to the start of the tuple memory */
2771  return ptr;
2772 }
#define HASH_CHUNK_SIZE
Definition: hashjoin.h:139
#define HASH_CHUNK_THRESHOLD
Definition: hashjoin.h:143
struct HashMemoryChunkData * unshared
Definition: hashjoin.h:126
MemoryContext batchCxt
Definition: hashjoin.h:349
struct HashMemoryChunkData * HashMemoryChunk
Definition: hashjoin.h:137
#define HASH_CHUNK_HEADER_SIZE
Definition: hashjoin.h:140
#define MAXALIGN(LEN)
Definition: c.h:691
HashMemoryChunk chunks
Definition: hashjoin.h:352
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
union HashMemoryChunkData::@94 next
#define HASH_CHUNK_DATA(hc)
Definition: hashjoin.h:141

◆ ExecChooseHashTableSize()

void ExecChooseHashTableSize ( double  ntuples,
int  tupwidth,
bool  useskew,
bool  try_combined_work_mem,
int  parallel_workers,
size_t *  space_allowed,
int *  numbuckets,
int *  numbatches,
int *  num_skew_mcvs 
)

Definition at line 667 of file nodeHash.c.

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

Referenced by ExecChooseHashTableSize(), ExecHashTableCreate(), and initial_cost_hashjoin().

674 {
675  int tupsize;
676  double inner_rel_bytes;
677  long bucket_bytes;
678  long hash_table_bytes;
679  long skew_table_bytes;
680  long max_pointers;
681  long mppow2;
682  int nbatch = 1;
683  int nbuckets;
684  double dbuckets;
685 
686  /* Force a plausible relation size if no info */
687  if (ntuples <= 0.0)
688  ntuples = 1000.0;
689 
690  /*
691  * Estimate tupsize based on footprint of tuple in hashtable... note this
692  * does not allow for any palloc overhead. The manipulations of spaceUsed
693  * don't count palloc overhead either.
694  */
695  tupsize = HJTUPLE_OVERHEAD +
697  MAXALIGN(tupwidth);
698  inner_rel_bytes = ntuples * tupsize;
699 
700  /*
701  * Target in-memory hashtable size is work_mem kilobytes.
702  */
703  hash_table_bytes = work_mem * 1024L;
704 
705  /*
706  * Parallel Hash tries to use the combined work_mem of all workers to
707  * avoid the need to batch. If that won't work, it falls back to work_mem
708  * per worker and tries to process batches in parallel.
709  */
710  if (try_combined_work_mem)
711  hash_table_bytes += hash_table_bytes * parallel_workers;
712 
713  *space_allowed = hash_table_bytes;
714 
715  /*
716  * If skew optimization is possible, estimate the number of skew buckets
717  * that will fit in the memory allowed, and decrement the assumed space
718  * available for the main hash table accordingly.
719  *
720  * We make the optimistic assumption that each skew bucket will contain
721  * one inner-relation tuple. If that turns out to be low, we will recover
722  * at runtime by reducing the number of skew buckets.
723  *
724  * hashtable->skewBucket will have up to 8 times as many HashSkewBucket
725  * pointers as the number of MCVs we allow, since ExecHashBuildSkewHash
726  * will round up to the next power of 2 and then multiply by 4 to reduce
727  * collisions.
728  */
729  if (useskew)
730  {
731  skew_table_bytes = hash_table_bytes * SKEW_WORK_MEM_PERCENT / 100;
732 
733  /*----------
734  * Divisor is:
735  * size of a hash tuple +
736  * worst-case size of skewBucket[] per MCV +
737  * size of skewBucketNums[] entry +
738  * size of skew bucket struct itself
739  *----------
740  */
741  *num_skew_mcvs = skew_table_bytes / (tupsize +
742  (8 * sizeof(HashSkewBucket *)) +
743  sizeof(int) +
745  if (*num_skew_mcvs > 0)
746  hash_table_bytes -= skew_table_bytes;
747  }
748  else
749  *num_skew_mcvs = 0;
750 
751  /*
752  * Set nbuckets to achieve an average bucket load of NTUP_PER_BUCKET when
753  * memory is filled, assuming a single batch; but limit the value so that
754  * the pointer arrays we'll try to allocate do not exceed work_mem nor
755  * MaxAllocSize.
756  *
757  * Note that both nbuckets and nbatch must be powers of 2 to make
758  * ExecHashGetBucketAndBatch fast.
759  */
760  max_pointers = *space_allowed / sizeof(HashJoinTuple);
761  max_pointers = Min(max_pointers, MaxAllocSize / sizeof(HashJoinTuple));
762  /* If max_pointers isn't a power of 2, must round it down to one */
763  mppow2 = 1L << my_log2(max_pointers);
764  if (max_pointers != mppow2)
765  max_pointers = mppow2 / 2;
766 
767  /* Also ensure we avoid integer overflow in nbatch and nbuckets */
768  /* (this step is redundant given the current value of MaxAllocSize) */
769  max_pointers = Min(max_pointers, INT_MAX / 2);
770 
771  dbuckets = ceil(ntuples / NTUP_PER_BUCKET);
772  dbuckets = Min(dbuckets, max_pointers);
773  nbuckets = (int) dbuckets;
774  /* don't let nbuckets be really small, though ... */
775  nbuckets = Max(nbuckets, 1024);
776  /* ... and force it to be a power of 2. */
777  nbuckets = 1 << my_log2(nbuckets);
778 
779  /*
780  * If there's not enough space to store the projected number of tuples and
781  * the required bucket headers, we will need multiple batches.
782  */
783  bucket_bytes = sizeof(HashJoinTuple) * nbuckets;
784  if (inner_rel_bytes + bucket_bytes > hash_table_bytes)
785  {
786  /* We'll need multiple batches */
787  long lbuckets;
788  double dbatch;
789  int minbatch;
790  long bucket_size;
791 
792  /*
793  * If Parallel Hash with combined work_mem would still need multiple
794  * batches, we'll have to fall back to regular work_mem budget.
795  */
796  if (try_combined_work_mem)
797  {
798  ExecChooseHashTableSize(ntuples, tupwidth, useskew,
799  false, parallel_workers,
800  space_allowed,
801  numbuckets,
802  numbatches,
803  num_skew_mcvs);
804  return;
805  }
806 
807  /*
808  * Estimate the number of buckets we'll want to have when work_mem is
809  * entirely full. Each bucket will contain a bucket pointer plus
810  * NTUP_PER_BUCKET tuples, whose projected size already includes
811  * overhead for the hash code, pointer to the next tuple, etc.
812  */
813  bucket_size = (tupsize * NTUP_PER_BUCKET + sizeof(HashJoinTuple));
814  lbuckets = 1L << my_log2(hash_table_bytes / bucket_size);
815  lbuckets = Min(lbuckets, max_pointers);
816  nbuckets = (int) lbuckets;
817  nbuckets = 1 << my_log2(nbuckets);
818  bucket_bytes = nbuckets * sizeof(HashJoinTuple);
819 
820  /*
821  * Buckets are simple pointers to hashjoin tuples, while tupsize
822  * includes the pointer, hash code, and MinimalTupleData. So buckets
823  * should never really exceed 25% of work_mem (even for
824  * NTUP_PER_BUCKET=1); except maybe for work_mem values that are not
825  * 2^N bytes, where we might get more because of doubling. So let's
826  * look for 50% here.
827  */
828  Assert(bucket_bytes <= hash_table_bytes / 2);
829 
830  /* Calculate required number of batches. */
831  dbatch = ceil(inner_rel_bytes / (hash_table_bytes - bucket_bytes));
832  dbatch = Min(dbatch, max_pointers);
833  minbatch = (int) dbatch;
834  nbatch = pg_nextpower2_32(Max(2, minbatch));
835  }
836 
837  Assert(nbuckets > 0);
838  Assert(nbatch > 0);
839 
840  *numbuckets = nbuckets;
841  *numbatches = nbatch;
842 }
#define SKEW_BUCKET_OVERHEAD
Definition: hashjoin.h:108
#define Min(x, y)
Definition: c.h:920
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1937
static uint32 pg_nextpower2_32(uint32 num)
Definition: pg_bitutils.h:146
int my_log2(long num)
Definition: dynahash.c:1720
#define MaxAllocSize
Definition: memutils.h:40
#define SizeofMinimalTupleHeader
Definition: htup_details.h:649
#define NTUP_PER_BUCKET
Definition: nodeHash.c:664
int work_mem
Definition: globals.c:121
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:79
#define Max(x, y)
Definition: c.h:914
#define Assert(condition)
Definition: c.h:738
#define MAXALIGN(LEN)
Definition: c.h:691
#define SKEW_WORK_MEM_PERCENT
Definition: hashjoin.h:110
void ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew, bool try_combined_work_mem, int parallel_workers, size_t *space_allowed, int *numbuckets, int *numbatches, int *num_skew_mcvs)
Definition: nodeHash.c:667

◆ ExecEndHash()

void ExecEndHash ( HashState node)

Definition at line 406 of file nodeHash.c.

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

Referenced by ExecEndNode().

407 {
409 
410  /*
411  * free exprcontext
412  */
413  ExecFreeExprContext(&node->ps);
414 
415  /*
416  * shut down the subplan
417  */
418  outerPlan = outerPlanState(node);
419  ExecEndNode(outerPlan);
420 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:543
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:655
#define outerPlanState(node)
Definition: execnodes.h:1039
PlanState ps
Definition: execnodes.h:2380
#define outerPlan(node)
Definition: plannodes.h:172

◆ ExecHash()

static TupleTableSlot* ExecHash ( PlanState pstate)
static

Definition at line 91 of file nodeHash.c.

References elog, and ERROR.

Referenced by ExecInitHash().

92 {
93  elog(ERROR, "Hash node does not support ExecProcNode call convention");
94  return NULL;
95 }
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:214

◆ ExecHashAccumInstrumentation()

void ExecHashAccumInstrumentation ( HashInstrumentation instrument,
HashJoinTable  hashtable 
)

Definition at line 2687 of file nodeHash.c.

References Max, HashJoinTableData::nbatch, HashInstrumentation::nbatch, HashJoinTableData::nbatch_original, HashInstrumentation::nbatch_original, HashJoinTableData::nbuckets, HashInstrumentation::nbuckets, HashJoinTableData::nbuckets_original, HashInstrumentation::nbuckets_original, HashInstrumentation::space_peak, and HashJoinTableData::spacePeak.

Referenced by ExecReScanHashJoin(), and ExecShutdownHash().

2689 {
2690  instrument->nbuckets = Max(instrument->nbuckets,
2691  hashtable->nbuckets);
2692  instrument->nbuckets_original = Max(instrument->nbuckets_original,
2693  hashtable->nbuckets_original);
2694  instrument->nbatch = Max(instrument->nbatch,
2695  hashtable->nbatch);
2696  instrument->nbatch_original = Max(instrument->nbatch_original,
2697  hashtable->nbatch_original);
2698  instrument->space_peak = Max(instrument->space_peak,
2699  hashtable->spacePeak);
2700 }
#define Max(x, y)
Definition: c.h:914

◆ ExecHashBuildSkewHash()

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

Definition at line 2212 of file nodeHash.c.

References ATTSTATSSLOT_NUMBERS, ATTSTATSSLOT_VALUES, HashJoinTableData::batchCxt, BoolGetDatum, HashJoinTableData::collations, DatumGetUInt32, free_attstatsslot(), FunctionCall1Coll(), get_attstatsslot(), HashSkewBucket::hashvalue, HeapTupleIsValid, i, Int16GetDatum, InvalidOid, MemoryContextAlloc(), MemoryContextAllocZero(), HashJoinTableData::nSkewBuckets, AttStatsSlot::numbers, AttStatsSlot::nvalues, ObjectIdGetDatum, OidIsValid, HashJoinTableData::outer_hashfunctions, pg_nextpower2_32(), 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, STATRELATTINH, HashSkewBucket::tuples, and AttStatsSlot::values.

Referenced by ExecHashTableCreate().

2213 {
2214  HeapTupleData *statsTuple;
2215  AttStatsSlot sslot;
2216 
2217  /* Do nothing if planner didn't identify the outer relation's join key */
2218  if (!OidIsValid(node->skewTable))
2219  return;
2220  /* Also, do nothing if we don't have room for at least one skew bucket */
2221  if (mcvsToUse <= 0)
2222  return;
2223 
2224  /*
2225  * Try to find the MCV statistics for the outer relation's join key.
2226  */
2227  statsTuple = SearchSysCache3(STATRELATTINH,
2228  ObjectIdGetDatum(node->skewTable),
2229  Int16GetDatum(node->skewColumn),
2230  BoolGetDatum(node->skewInherit));
2231  if (!HeapTupleIsValid(statsTuple))
2232  return;
2233 
2234  if (get_attstatsslot(&sslot, statsTuple,
2235  STATISTIC_KIND_MCV, InvalidOid,
2237  {
2238  double frac;
2239  int nbuckets;
2240  FmgrInfo *hashfunctions;
2241  int i;
2242 
2243  if (mcvsToUse > sslot.nvalues)
2244  mcvsToUse = sslot.nvalues;
2245 
2246  /*
2247  * Calculate the expected fraction of outer relation that will
2248  * participate in the skew optimization. If this isn't at least
2249  * SKEW_MIN_OUTER_FRACTION, don't use skew optimization.
2250  */
2251  frac = 0;
2252  for (i = 0; i < mcvsToUse; i++)
2253  frac += sslot.numbers[i];
2254  if (frac < SKEW_MIN_OUTER_FRACTION)
2255  {
2256  free_attstatsslot(&sslot);
2257  ReleaseSysCache(statsTuple);
2258  return;
2259  }
2260 
2261  /*
2262  * Okay, set up the skew hashtable.
2263  *
2264  * skewBucket[] is an open addressing hashtable with a power of 2 size
2265  * that is greater than the number of MCV values. (This ensures there
2266  * will be at least one null entry, so searches will always
2267  * terminate.)
2268  *
2269  * Note: this code could fail if mcvsToUse exceeds INT_MAX/8 or
2270  * MaxAllocSize/sizeof(void *)/8, but that is not currently possible
2271  * since we limit pg_statistic entries to much less than that.
2272  */
2273  nbuckets = pg_nextpower2_32(mcvsToUse + 1);
2274  /* use two more bits just to help avoid collisions */
2275  nbuckets <<= 2;
2276 
2277  hashtable->skewEnabled = true;
2278  hashtable->skewBucketLen = nbuckets;
2279 
2280  /*
2281  * We allocate the bucket memory in the hashtable's batch context. It
2282  * is only needed during the first batch, and this ensures it will be
2283  * automatically removed once the first batch is done.
2284  */
2285  hashtable->skewBucket = (HashSkewBucket **)
2286  MemoryContextAllocZero(hashtable->batchCxt,
2287  nbuckets * sizeof(HashSkewBucket *));
2288  hashtable->skewBucketNums = (int *)
2289  MemoryContextAllocZero(hashtable->batchCxt,
2290  mcvsToUse * sizeof(int));
2291 
2292  hashtable->spaceUsed += nbuckets * sizeof(HashSkewBucket *)
2293  + mcvsToUse * sizeof(int);
2294  hashtable->spaceUsedSkew += nbuckets * sizeof(HashSkewBucket *)
2295  + mcvsToUse * sizeof(int);
2296  if (hashtable->spaceUsed > hashtable->spacePeak)
2297  hashtable->spacePeak = hashtable->spaceUsed;
2298 
2299  /*
2300  * Create a skew bucket for each MCV hash value.
2301  *
2302  * Note: it is very important that we create the buckets in order of
2303  * decreasing MCV frequency. If we have to remove some buckets, they
2304  * must be removed in reverse order of creation (see notes in
2305  * ExecHashRemoveNextSkewBucket) and we want the least common MCVs to
2306  * be removed first.
2307  */
2308  hashfunctions = hashtable->outer_hashfunctions;
2309 
2310  for (i = 0; i < mcvsToUse; i++)
2311  {
2312  uint32 hashvalue;
2313  int bucket;
2314 
2315  hashvalue = DatumGetUInt32(FunctionCall1Coll(&hashfunctions[0],
2316  hashtable->collations[0],
2317  sslot.values[i]));
2318 
2319  /*
2320  * While we have not hit a hole in the hashtable and have not hit
2321  * the desired bucket, we have collided with some previous hash
2322  * value, so try the next bucket location. NB: this code must
2323  * match ExecHashGetSkewBucket.
2324  */
2325  bucket = hashvalue & (nbuckets - 1);
2326  while (hashtable->skewBucket[bucket] != NULL &&
2327  hashtable->skewBucket[bucket]->hashvalue != hashvalue)
2328  bucket = (bucket + 1) & (nbuckets - 1);
2329 
2330  /*
2331  * If we found an existing bucket with the same hashvalue, leave
2332  * it alone. It's okay for two MCVs to share a hashvalue.
2333  */
2334  if (hashtable->skewBucket[bucket] != NULL)
2335  continue;
2336 
2337  /* Okay, create a new skew bucket for this hashvalue. */
2338  hashtable->skewBucket[bucket] = (HashSkewBucket *)
2339  MemoryContextAlloc(hashtable->batchCxt,
2340  sizeof(HashSkewBucket));
2341  hashtable->skewBucket[bucket]->hashvalue = hashvalue;
2342  hashtable->skewBucket[bucket]->tuples = NULL;
2343  hashtable->skewBucketNums[hashtable->nSkewBuckets] = bucket;
2344  hashtable->nSkewBuckets++;
2345  hashtable->spaceUsed += SKEW_BUCKET_OVERHEAD;
2346  hashtable->spaceUsedSkew += SKEW_BUCKET_OVERHEAD;
2347  if (hashtable->spaceUsed > hashtable->spacePeak)
2348  hashtable->spacePeak = hashtable->spaceUsed;
2349  }
2350 
2351  free_attstatsslot(&sslot);
2352  }
2353 
2354  ReleaseSysCache(statsTuple);
2355 }
Oid skewTable
Definition: plannodes.h:931
#define DatumGetUInt32(X)
Definition: postgres.h:486
Definition: fmgr.h:56
#define SKEW_BUCKET_OVERHEAD
Definition: hashjoin.h:108
bool skewInherit
Definition: plannodes.h:933
#define SKEW_MIN_OUTER_FRACTION
Definition: hashjoin.h:111
#define ATTSTATSSLOT_VALUES
Definition: lsyscache.h:39
#define Int16GetDatum(X)
Definition: postgres.h:451
#define OidIsValid(objectId)
Definition: c.h:644
int * skewBucketNums
Definition: hashjoin.h:308
#define ATTSTATSSLOT_NUMBERS
Definition: lsyscache.h:40
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
HeapTuple SearchSysCache3(int cacheId, Datum key1, Datum key2, Datum key3)
Definition: syscache.c:1138
float4 * numbers
Definition: lsyscache.h:53
AttrNumber skewColumn
Definition: plannodes.h:932
HashJoinTuple tuples
Definition: hashjoin.h:105
static uint32 pg_nextpower2_32(uint32 num)
Definition: pg_bitutils.h:146
unsigned int uint32
Definition: c.h:367
MemoryContext batchCxt
Definition: hashjoin.h:349
FmgrInfo * outer_hashfunctions
Definition: hashjoin.h:337
HashSkewBucket ** skewBucket
Definition: hashjoin.h:305
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1132
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:839
#define BoolGetDatum(X)
Definition: postgres.h:402
#define InvalidOid
Definition: postgres_ext.h:36
uint32 hashvalue
Definition: hashjoin.h:104
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:3003
Datum * values
Definition: lsyscache.h:50
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
int i
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3133

◆ ExecHashEstimate()

void ExecHashEstimate ( HashState node,
ParallelContext pcxt 
)

Definition at line 2570 of file nodeHash.c.

References add_size(), ParallelContext::estimator, PlanState::instrument, mul_size(), ParallelContext::nworkers, offsetof, HashState::ps, shm_toc_estimate_chunk, and shm_toc_estimate_keys.

Referenced by ExecParallelEstimate().

2571 {
2572  size_t size;
2573 
2574  /* don't need this if not instrumenting or no workers */
2575  if (!node->ps.instrument || pcxt->nworkers == 0)
2576  return;
2577 
2578  size = mul_size(pcxt->nworkers, sizeof(HashInstrumentation));
2579  size = add_size(size, offsetof(SharedHashInfo, hinstrument));
2580  shm_toc_estimate_chunk(&pcxt->estimator, size);
2581  shm_toc_estimate_keys(&pcxt->estimator, 1);
2582 }
Instrumentation * instrument
Definition: execnodes.h:955
shm_toc_estimator estimator
Definition: parallel.h:42
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
PlanState ps
Definition: execnodes.h:2380
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define offsetof(type, field)
Definition: c.h:661

◆ ExecHashGetBucketAndBatch()

void ExecHashGetBucketAndBatch ( HashJoinTable  hashtable,
uint32  hashvalue,
int *  bucketno,
int *  batchno 
)

Definition at line 1898 of file nodeHash.c.

References HashJoinTableData::log2_nbuckets, HashJoinTableData::nbatch, HashJoinTableData::nbuckets, and pg_rotate_right32().

Referenced by ExecHashIncreaseNumBatches(), ExecHashIncreaseNumBuckets(), ExecHashJoinImpl(), ExecHashRemoveNextSkewBucket(), ExecHashTableInsert(), ExecParallelHashIncreaseNumBuckets(), ExecParallelHashJoinPartitionOuter(), ExecParallelHashRepartitionFirst(), ExecParallelHashRepartitionRest(), ExecParallelHashTableInsert(), and ExecParallelHashTableInsertCurrentBatch().

1902 {
1903  uint32 nbuckets = (uint32) hashtable->nbuckets;
1904  uint32 nbatch = (uint32) hashtable->nbatch;
1905 
1906  if (nbatch > 1)
1907  {
1908  *bucketno = hashvalue & (nbuckets - 1);
1909  *batchno = pg_rotate_right32(hashvalue,
1910  hashtable->log2_nbuckets) & (nbatch - 1);
1911  }
1912  else
1913  {
1914  *bucketno = hashvalue & (nbuckets - 1);
1915  *batchno = 0;
1916  }
1917 }
static uint32 pg_rotate_right32(uint32 word, int n)
Definition: pg_bitutils.h:221
unsigned int uint32
Definition: c.h:367

◆ ExecHashGetHashValue()

bool ExecHashGetHashValue ( HashJoinTable  hashtable,
ExprContext econtext,
List hashkeys,
bool  outer_tuple,
bool  keep_nulls,
uint32 hashvalue 
)

Definition at line 1790 of file nodeHash.c.

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

Referenced by ExecHashJoinOuterGetTuple(), ExecParallelHashJoinOuterGetTuple(), ExecParallelHashJoinPartitionOuter(), MultiExecParallelHash(), and MultiExecPrivateHash().

1796 {
1797  uint32 hashkey = 0;
1798  FmgrInfo *hashfunctions;
1799  ListCell *hk;
1800  int i = 0;
1801  MemoryContext oldContext;
1802 
1803  /*
1804  * We reset the eval context each time to reclaim any memory leaked in the
1805  * hashkey expressions.
1806  */
1807  ResetExprContext(econtext);
1808 
1809  oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
1810 
1811  if (outer_tuple)
1812  hashfunctions = hashtable->outer_hashfunctions;
1813  else
1814  hashfunctions = hashtable->inner_hashfunctions;
1815 
1816  foreach(hk, hashkeys)
1817  {
1818  ExprState *keyexpr = (ExprState *) lfirst(hk);
1819  Datum keyval;
1820  bool isNull;
1821 
1822  /* rotate hashkey left 1 bit at each step */
1823  hashkey = (hashkey << 1) | ((hashkey & 0x80000000) ? 1 : 0);
1824 
1825  /*
1826  * Get the join attribute value of the tuple
1827  */
1828  keyval = ExecEvalExpr(keyexpr, econtext, &isNull);
1829 
1830  /*
1831  * If the attribute is NULL, and the join operator is strict, then
1832  * this tuple cannot pass the join qual so we can reject it
1833  * immediately (unless we're scanning the outside of an outer join, in
1834  * which case we must not reject it). Otherwise we act like the
1835  * hashcode of NULL is zero (this will support operators that act like
1836  * IS NOT DISTINCT, though not any more-random behavior). We treat
1837  * the hash support function as strict even if the operator is not.
1838  *
1839  * Note: currently, all hashjoinable operators must be strict since
1840  * the hash index AM assumes that. However, it takes so little extra
1841  * code here to allow non-strict that we may as well do it.
1842  */
1843  if (isNull)
1844  {
1845  if (hashtable->hashStrict[i] && !keep_nulls)
1846  {
1847  MemoryContextSwitchTo(oldContext);
1848  return false; /* cannot match */
1849  }
1850  /* else, leave hashkey unmodified, equivalent to hashcode 0 */
1851  }
1852  else
1853  {
1854  /* Compute the hash function */
1855  uint32 hkey;
1856 
1857  hkey = DatumGetUInt32(FunctionCall1Coll(&hashfunctions[i], hashtable->collations[i], keyval));
1858  hashkey ^= hkey;
1859  }
1860 
1861  i++;
1862  }
1863 
1864  MemoryContextSwitchTo(oldContext);
1865 
1866  *hashvalue = hashkey;
1867  return true;
1868 }
#define DatumGetUInt32(X)
Definition: postgres.h:486
Definition: fmgr.h:56
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:234
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
FmgrInfo * inner_hashfunctions
Definition: hashjoin.h:338
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:290
unsigned int uint32
Definition: c.h:367
FmgrInfo * outer_hashfunctions
Definition: hashjoin.h:337
uintptr_t Datum
Definition: postgres.h:367
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1132
#define lfirst(lc)
Definition: pg_list.h:190
int i
bool * hashStrict
Definition: hashjoin.h:339
#define ResetExprContext(econtext)
Definition: executor.h:501

◆ ExecHashGetSkewBucket()

int ExecHashGetSkewBucket ( HashJoinTable  hashtable,
uint32  hashvalue 
)

Definition at line 2365 of file nodeHash.c.

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

Referenced by ExecHashJoinImpl(), and MultiExecPrivateHash().

2366 {
2367  int bucket;
2368 
2369  /*
2370  * Always return INVALID_SKEW_BUCKET_NO if not doing skew optimization (in
2371  * particular, this happens after the initial batch is done).
2372  */
2373  if (!hashtable->skewEnabled)
2374  return INVALID_SKEW_BUCKET_NO;
2375 
2376  /*
2377  * Since skewBucketLen is a power of 2, we can do a modulo by ANDing.
2378  */
2379  bucket = hashvalue & (hashtable->skewBucketLen - 1);
2380 
2381  /*
2382  * While we have not hit a hole in the hashtable and have not hit the
2383  * desired bucket, we have collided with some other hash value, so try the
2384  * next bucket location.
2385  */
2386  while (hashtable->skewBucket[bucket] != NULL &&
2387  hashtable->skewBucket[bucket]->hashvalue != hashvalue)
2388  bucket = (bucket + 1) & (hashtable->skewBucketLen - 1);
2389 
2390  /*
2391  * Found the desired bucket?
2392  */
2393  if (hashtable->skewBucket[bucket] != NULL)
2394  return bucket;
2395 
2396  /*
2397  * There must not be any hashtable entry for this hash value.
2398  */
2399  return INVALID_SKEW_BUCKET_NO;
2400 }
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:109
HashSkewBucket ** skewBucket
Definition: hashjoin.h:305
uint32 hashvalue
Definition: hashjoin.h:104

◆ ExecHashIncreaseNumBatches()

static void ExecHashIncreaseNumBatches ( HashJoinTable  hashtable)
static

Definition at line 885 of file nodeHash.c.

References Assert, HashJoinTableData::buckets, CHECK_FOR_INTERRUPTS, HashJoinTableData::chunks, HashJoinTableData::curbatch, dense_alloc(), ExecHashGetBucketAndBatch(), ExecHashJoinSaveTuple(), HashJoinTableData::growEnabled, HASH_CHUNK_DATA, 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(), printf, repalloc(), HashJoinTableData::spaceUsed, MinimalTupleData::t_len, HashJoinTupleData::unshared, HashMemoryChunkData::unshared, and HashJoinTableData::unshared.

Referenced by ExecHashSkewTableInsert(), and ExecHashTableInsert().

886 {
887  int oldnbatch = hashtable->nbatch;
888  int curbatch = hashtable->curbatch;
889  int nbatch;
890  MemoryContext oldcxt;
891  long ninmemory;
892  long nfreed;
893  HashMemoryChunk oldchunks;
894 
895  /* do nothing if we've decided to shut off growth */
896  if (!hashtable->growEnabled)
897  return;
898 
899  /* safety check to avoid overflow */
900  if (oldnbatch > Min(INT_MAX / 2, MaxAllocSize / (sizeof(void *) * 2)))
901  return;
902 
903  nbatch = oldnbatch * 2;
904  Assert(nbatch > 1);
905 
906 #ifdef HJDEBUG
907  printf("Hashjoin %p: increasing nbatch to %d because space = %zu\n",
908  hashtable, nbatch, hashtable->spaceUsed);
909 #endif
910 
911  oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
912 
913  if (hashtable->innerBatchFile == NULL)
914  {
915  /* we had no file arrays before */
916  hashtable->innerBatchFile = (BufFile **)
917  palloc0(nbatch * sizeof(BufFile *));
918  hashtable->outerBatchFile = (BufFile **)
919  palloc0(nbatch * sizeof(BufFile *));
920  /* time to establish the temp tablespaces, too */
922  }
923  else
924  {
925  /* enlarge arrays and zero out added entries */
926  hashtable->innerBatchFile = (BufFile **)
927  repalloc(hashtable->innerBatchFile, nbatch * sizeof(BufFile *));
928  hashtable->outerBatchFile = (BufFile **)
929  repalloc(hashtable->outerBatchFile, nbatch * sizeof(BufFile *));
930  MemSet(hashtable->innerBatchFile + oldnbatch, 0,
931  (nbatch - oldnbatch) * sizeof(BufFile *));
932  MemSet(hashtable->outerBatchFile + oldnbatch, 0,
933  (nbatch - oldnbatch) * sizeof(BufFile *));
934  }
935 
936  MemoryContextSwitchTo(oldcxt);
937 
938  hashtable->nbatch = nbatch;
939 
940  /*
941  * Scan through the existing hash table entries and dump out any that are
942  * no longer of the current batch.
943  */
944  ninmemory = nfreed = 0;
945 
946  /* If know we need to resize nbuckets, we can do it while rebatching. */
947  if (hashtable->nbuckets_optimal != hashtable->nbuckets)
948  {
949  /* we never decrease the number of buckets */
950  Assert(hashtable->nbuckets_optimal > hashtable->nbuckets);
951 
952  hashtable->nbuckets = hashtable->nbuckets_optimal;
953  hashtable->log2_nbuckets = hashtable->log2_nbuckets_optimal;
954 
955  hashtable->buckets.unshared =
956  repalloc(hashtable->buckets.unshared,
957  sizeof(HashJoinTuple) * hashtable->nbuckets);
958  }
959 
960  /*
961  * We will scan through the chunks directly, so that we can reset the
962  * buckets now and not have to keep track which tuples in the buckets have
963  * already been processed. We will free the old chunks as we go.
964  */
965  memset(hashtable->buckets.unshared, 0,
966  sizeof(HashJoinTuple) * hashtable->nbuckets);
967  oldchunks = hashtable->chunks;
968  hashtable->chunks = NULL;
969 
970  /* so, let's scan through the old chunks, and all tuples in each chunk */
971  while (oldchunks != NULL)
972  {
973  HashMemoryChunk nextchunk = oldchunks->next.unshared;
974 
975  /* position within the buffer (up to oldchunks->used) */
976  size_t idx = 0;
977 
978  /* process all tuples stored in this chunk (and then free it) */
979  while (idx < oldchunks->used)
980  {
981  HashJoinTuple hashTuple = (HashJoinTuple) (HASH_CHUNK_DATA(oldchunks) + idx);
982  MinimalTuple tuple = HJTUPLE_MINTUPLE(hashTuple);
983  int hashTupleSize = (HJTUPLE_OVERHEAD + tuple->t_len);
984  int bucketno;
985  int batchno;
986 
987  ninmemory++;
988  ExecHashGetBucketAndBatch(hashtable, hashTuple->hashvalue,
989  &bucketno, &batchno);
990 
991  if (batchno == curbatch)
992  {
993  /* keep tuple in memory - copy it into the new chunk */
994  HashJoinTuple copyTuple;
995 
996  copyTuple = (HashJoinTuple) dense_alloc(hashtable, hashTupleSize);
997  memcpy(copyTuple, hashTuple, hashTupleSize);
998 
999  /* and add it back to the appropriate bucket */
1000  copyTuple->next.unshared = hashtable->buckets.unshared[bucketno];
1001  hashtable->buckets.unshared[bucketno] = copyTuple;
1002  }
1003  else
1004  {
1005  /* dump it out */
1006  Assert(batchno > curbatch);
1008  hashTuple->hashvalue,
1009  &hashtable->innerBatchFile[batchno]);
1010 
1011  hashtable->spaceUsed -= hashTupleSize;
1012  nfreed++;
1013  }
1014 
1015  /* next tuple in this chunk */
1016  idx += MAXALIGN(hashTupleSize);
1017 
1018  /* allow this loop to be cancellable */
1020  }
1021 
1022  /* we're done with this chunk - free it and proceed to the next one */
1023  pfree(oldchunks);
1024  oldchunks = nextchunk;
1025  }
1026 
1027 #ifdef HJDEBUG
1028  printf("Hashjoin %p: freed %ld of %ld tuples, space now %zu\n",
1029  hashtable, nfreed, ninmemory, hashtable->spaceUsed);
1030 #endif
1031 
1032  /*
1033  * If we dumped out either all or none of the tuples in the table, disable
1034  * further expansion of nbatch. This situation implies that we have
1035  * enough tuples of identical hashvalues to overflow spaceAllowed.
1036  * Increasing nbatch will not fix it since there's no way to subdivide the
1037  * group any more finely. We have to just gut it out and hope the server
1038  * has enough RAM.
1039  */
1040  if (nfreed == 0 || nfreed == ninmemory)
1041  {
1042  hashtable->growEnabled = false;
1043 #ifdef HJDEBUG
1044  printf("Hashjoin %p: disabling further increase of nbatch\n",
1045  hashtable);
1046 #endif
1047  }
1048 }
int log2_nbuckets_optimal
Definition: hashjoin.h:291
#define Min(x, y)
Definition: c.h:920
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define MemSet(start, val, len)
Definition: c.h:971
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
#define printf(...)
Definition: port.h:199
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1898
void pfree(void *pointer)
Definition: mcxt.c:1056
struct HashJoinTupleData * unshared
Definition: hashjoin.h:72
void PrepareTempTablespaces(void)
Definition: tablespace.c:1323
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1937
BufFile ** outerBatchFile
Definition: hashjoin.h:330
struct HashMemoryChunkData * unshared
Definition: hashjoin.h:126
union HashJoinTupleData::@93 next
#define MaxAllocSize
Definition: memutils.h:40
void * palloc0(Size size)
Definition: mcxt.c:980
static void * dense_alloc(HashJoinTable hashtable, Size size)
Definition: nodeHash.c:2706
union HashJoinTableData::@95 buckets
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:79
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
#define Assert(condition)
Definition: c.h:738
BufFile ** innerBatchFile
Definition: hashjoin.h:329
#define MAXALIGN(LEN)
Definition: c.h:691
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
HashMemoryChunk chunks
Definition: hashjoin.h:352
struct HashJoinTupleData ** unshared
Definition: hashjoin.h:297
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
MemoryContext hashCxt
Definition: hashjoin.h:348
union HashMemoryChunkData::@94 next
#define HASH_CHUNK_DATA(hc)
Definition: hashjoin.h:141
void ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr)
uint32 hashvalue
Definition: hashjoin.h:75

◆ ExecHashIncreaseNumBuckets()

static void ExecHashIncreaseNumBuckets ( HashJoinTable  hashtable)
static

Definition at line 1430 of file nodeHash.c.

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

Referenced by MultiExecPrivateHash().

1431 {
1432  HashMemoryChunk chunk;
1433 
1434  /* do nothing if not an increase (it's called increase for a reason) */
1435  if (hashtable->nbuckets >= hashtable->nbuckets_optimal)
1436  return;
1437 
1438 #ifdef HJDEBUG
1439  printf("Hashjoin %p: increasing nbuckets %d => %d\n",
1440  hashtable, hashtable->nbuckets, hashtable->nbuckets_optimal);
1441 #endif
1442 
1443  hashtable->nbuckets = hashtable->nbuckets_optimal;
1444  hashtable->log2_nbuckets = hashtable->log2_nbuckets_optimal;
1445 
1446  Assert(hashtable->nbuckets > 1);
1447  Assert(hashtable->nbuckets <= (INT_MAX / 2));
1448  Assert(hashtable->nbuckets == (1 << hashtable->log2_nbuckets));
1449 
1450  /*
1451  * Just reallocate the proper number of buckets - we don't need to walk
1452  * through them - we can walk the dense-allocated chunks (just like in
1453  * ExecHashIncreaseNumBatches, but without all the copying into new
1454  * chunks)
1455  */
1456  hashtable->buckets.unshared =
1457  (HashJoinTuple *) repalloc(hashtable->buckets.unshared,
1458  hashtable->nbuckets * sizeof(HashJoinTuple));
1459 
1460  memset(hashtable->buckets.unshared, 0,
1461  hashtable->nbuckets * sizeof(HashJoinTuple));
1462 
1463  /* scan through all tuples in all chunks to rebuild the hash table */
1464  for (chunk = hashtable->chunks; chunk != NULL; chunk = chunk->next.unshared)
1465  {
1466  /* process all tuples stored in this chunk */
1467  size_t idx = 0;
1468 
1469  while (idx < chunk->used)
1470  {
1471  HashJoinTuple hashTuple = (HashJoinTuple) (HASH_CHUNK_DATA(chunk) + idx);
1472  int bucketno;
1473  int batchno;
1474 
1475  ExecHashGetBucketAndBatch(hashtable, hashTuple->hashvalue,
1476  &bucketno, &batchno);
1477 
1478  /* add the tuple to the proper bucket */
1479  hashTuple->next.unshared = hashtable->buckets.unshared[bucketno];
1480  hashtable->buckets.unshared[bucketno] = hashTuple;
1481 
1482  /* advance index past the tuple */
1483  idx += MAXALIGN(HJTUPLE_OVERHEAD +
1484  HJTUPLE_MINTUPLE(hashTuple)->t_len);
1485  }
1486 
1487  /* allow this loop to be cancellable */
1489  }
1490 }
int log2_nbuckets_optimal
Definition: hashjoin.h:291
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
#define printf(...)
Definition: port.h:199
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1898
struct HashJoinTupleData * unshared
Definition: hashjoin.h:72
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1937
struct HashMemoryChunkData * unshared
Definition: hashjoin.h:126
union HashJoinTupleData::@93 next
union HashJoinTableData::@95 buckets
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:79
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
#define Assert(condition)
Definition: c.h:738
#define MAXALIGN(LEN)
Definition: c.h:691
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
HashMemoryChunk chunks
Definition: hashjoin.h:352
struct HashJoinTupleData ** unshared
Definition: hashjoin.h:297
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
union HashMemoryChunkData::@94 next
#define HASH_CHUNK_DATA(hc)
Definition: hashjoin.h:141
uint32 hashvalue
Definition: hashjoin.h:75

◆ ExecHashInitializeDSM()

void ExecHashInitializeDSM ( HashState node,
ParallelContext pcxt 
)

Definition at line 2589 of file nodeHash.c.

References PlanState::instrument, 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().

2590 {
2591  size_t size;
2592 
2593  /* don't need this if not instrumenting or no workers */
2594  if (!node->ps.instrument || pcxt->nworkers == 0)
2595  return;
2596 
2597  size = offsetof(SharedHashInfo, hinstrument) +
2598  pcxt->nworkers * sizeof(HashInstrumentation);
2599  node->shared_info = (SharedHashInfo *) shm_toc_allocate(pcxt->toc, size);
2600 
2601  /* Each per-worker area must start out as zeroes. */
2602  memset(node->shared_info, 0, size);
2603 
2604  node->shared_info->num_workers = pcxt->nworkers;
2605  shm_toc_insert(pcxt->toc, node->ps.plan->plan_node_id,
2606  node->shared_info);
2607 }
Instrumentation * instrument
Definition: execnodes.h:955
struct HashInstrumentation HashInstrumentation
int plan_node_id
Definition: plannodes.h:141
SharedHashInfo * shared_info
Definition: execnodes.h:2390
PlanState ps
Definition: execnodes.h:2380
Plan * plan
Definition: execnodes.h:945
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:661
shm_toc * toc
Definition: parallel.h:45

◆ ExecHashInitializeWorker()

void ExecHashInitializeWorker ( HashState node,
ParallelWorkerContext pwcxt 
)

Definition at line 2614 of file nodeHash.c.

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

Referenced by ExecParallelInitializeWorker().

2615 {
2616  SharedHashInfo *shared_info;
2617 
2618  /* don't need this if not instrumenting */
2619  if (!node->ps.instrument)
2620  return;
2621 
2622  /*
2623  * Find our entry in the shared area, and set up a pointer to it so that
2624  * we'll accumulate stats there when shutting down or rebuilding the hash
2625  * table.
2626  */
2627  shared_info = (SharedHashInfo *)
2628  shm_toc_lookup(pwcxt->toc, node->ps.plan->plan_node_id, false);
2629  node->hinstrument = &shared_info->hinstrument[ParallelWorkerNumber];
2630 }
Instrumentation * instrument
Definition: execnodes.h:955
int plan_node_id
Definition: plannodes.h:141
int ParallelWorkerNumber
Definition: parallel.c:112
PlanState ps
Definition: execnodes.h:2380
HashInstrumentation * hinstrument
Definition: execnodes.h:2397
HashInstrumentation hinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:2371
Plan * plan
Definition: execnodes.h:945
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

◆ ExecHashRemoveNextSkewBucket()

static void ExecHashRemoveNextSkewBucket ( HashJoinTable  hashtable)
static

Definition at line 2457 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, HashSkewBucket::tuples, HashJoinTupleData::unshared, and HashJoinTableData::unshared.

Referenced by ExecHashSkewTableInsert().

2458 {
2459  int bucketToRemove;
2460  HashSkewBucket *bucket;
2461  uint32 hashvalue;
2462  int bucketno;
2463  int batchno;
2464  HashJoinTuple hashTuple;
2465 
2466  /* Locate the bucket to remove */
2467  bucketToRemove = hashtable->skewBucketNums[hashtable->nSkewBuckets - 1];
2468  bucket = hashtable->skewBucket[bucketToRemove];
2469 
2470  /*
2471  * Calculate which bucket and batch the tuples belong to in the main
2472  * hashtable. They all have the same hash value, so it's the same for all
2473  * of them. Also note that it's not possible for nbatch to increase while
2474  * we are processing the tuples.
2475  */
2476  hashvalue = bucket->hashvalue;
2477  ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno, &batchno);
2478 
2479  /* Process all tuples in the bucket */
2480  hashTuple = bucket->tuples;
2481  while (hashTuple != NULL)
2482  {
2483  HashJoinTuple nextHashTuple = hashTuple->next.unshared;
2484  MinimalTuple tuple;
2485  Size tupleSize;
2486 
2487  /*
2488  * This code must agree with ExecHashTableInsert. We do not use
2489  * ExecHashTableInsert directly as ExecHashTableInsert expects a
2490  * TupleTableSlot while we already have HashJoinTuples.
2491  */
2492  tuple = HJTUPLE_MINTUPLE(hashTuple);
2493  tupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
2494 
2495  /* Decide whether to put the tuple in the hash table or a temp file */
2496  if (batchno == hashtable->curbatch)
2497  {
2498  /* Move the tuple to the main hash table */
2499  HashJoinTuple copyTuple;
2500 
2501  /*
2502  * We must copy the tuple into the dense storage, else it will not
2503  * be found by, eg, ExecHashIncreaseNumBatches.
2504  */
2505  copyTuple = (HashJoinTuple) dense_alloc(hashtable, tupleSize);
2506  memcpy(copyTuple, hashTuple, tupleSize);
2507  pfree(hashTuple);
2508 
2509  copyTuple->next.unshared = hashtable->buckets.unshared[bucketno];
2510  hashtable->buckets.unshared[bucketno] = copyTuple;
2511 
2512  /* We have reduced skew space, but overall space doesn't change */
2513  hashtable->spaceUsedSkew -= tupleSize;
2514  }
2515  else
2516  {
2517  /* Put the tuple into a temp file for later batches */
2518  Assert(batchno > hashtable->curbatch);
2519  ExecHashJoinSaveTuple(tuple, hashvalue,
2520  &hashtable->innerBatchFile[batchno]);
2521  pfree(hashTuple);
2522  hashtable->spaceUsed -= tupleSize;
2523  hashtable->spaceUsedSkew -= tupleSize;
2524  }
2525 
2526  hashTuple = nextHashTuple;
2527 
2528  /* allow this loop to be cancellable */
2530  }
2531 
2532  /*
2533  * Free the bucket struct itself and reset the hashtable entry to NULL.
2534  *
2535  * NOTE: this is not nearly as simple as it looks on the surface, because
2536  * of the possibility of collisions in the hashtable. Suppose that hash
2537  * values A and B collide at a particular hashtable entry, and that A was
2538  * entered first so B gets shifted to a different table entry. If we were
2539  * to remove A first then ExecHashGetSkewBucket would mistakenly start
2540  * reporting that B is not in the hashtable, because it would hit the NULL
2541  * before finding B. However, we always remove entries in the reverse
2542  * order of creation, so this failure cannot happen.
2543  */
2544  hashtable->skewBucket[bucketToRemove] = NULL;
2545  hashtable->nSkewBuckets--;
2546  pfree(bucket);
2547  hashtable->spaceUsed -= SKEW_BUCKET_OVERHEAD;
2548  hashtable->spaceUsedSkew -= SKEW_BUCKET_OVERHEAD;
2549 
2550  /*
2551  * If we have removed all skew buckets then give up on skew optimization.
2552  * Release the arrays since they aren't useful any more.
2553  */
2554  if (hashtable->nSkewBuckets == 0)
2555  {
2556  hashtable->skewEnabled = false;
2557  pfree(hashtable->skewBucket);
2558  pfree(hashtable->skewBucketNums);
2559  hashtable->skewBucket = NULL;
2560  hashtable->skewBucketNums = NULL;
2561  hashtable->spaceUsed -= hashtable->spaceUsedSkew;
2562  hashtable->spaceUsedSkew = 0;
2563  }
2564 }
#define SKEW_BUCKET_OVERHEAD
Definition: hashjoin.h:108
int * skewBucketNums
Definition: hashjoin.h:308
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1898
void pfree(void *pointer)
Definition: mcxt.c:1056
struct HashJoinTupleData * unshared
Definition: hashjoin.h:72
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1937
HashJoinTuple tuples
Definition: hashjoin.h:105
unsigned int uint32
Definition: c.h:367
union HashJoinTupleData::@93 next
HashSkewBucket ** skewBucket
Definition: hashjoin.h:305
static void * dense_alloc(HashJoinTable hashtable, Size size)
Definition: nodeHash.c:2706
union HashJoinTableData::@95 buckets
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:79
uint32 hashvalue
Definition: hashjoin.h:104
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
#define Assert(condition)
Definition: c.h:738
size_t Size
Definition: c.h:466
BufFile ** innerBatchFile
Definition: hashjoin.h:329
struct HashJoinTupleData ** unshared
Definition: hashjoin.h:297
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
void ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr)

◆ ExecHashRetrieveInstrumentation()

void ExecHashRetrieveInstrumentation ( HashState node)

Definition at line 2656 of file nodeHash.c.

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

Referenced by ExecParallelRetrieveInstrumentation().

2657 {
2658  SharedHashInfo *shared_info = node->shared_info;
2659  size_t size;
2660 
2661  if (shared_info == NULL)
2662  return;
2663 
2664  /* Replace node->shared_info with a copy in backend-local memory. */
2665  size = offsetof(SharedHashInfo, hinstrument) +
2666  shared_info->num_workers * sizeof(HashInstrumentation);
2667  node->shared_info = palloc(size);
2668  memcpy(node->shared_info, shared_info, size);
2669 }
struct HashInstrumentation HashInstrumentation
SharedHashInfo * shared_info
Definition: execnodes.h:2390
void * palloc(Size size)
Definition: mcxt.c:949
#define offsetof(type, field)
Definition: c.h:661

◆ ExecHashSkewTableInsert()

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

Definition at line 2411 of file nodeHash.c.

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

Referenced by MultiExecPrivateHash().

2415 {
2416  bool shouldFree;
2417  MinimalTuple tuple = ExecFetchSlotMinimalTuple(slot, &shouldFree);
2418  HashJoinTuple hashTuple;
2419  int hashTupleSize;
2420 
2421  /* Create the HashJoinTuple */
2422  hashTupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
2423  hashTuple = (HashJoinTuple) MemoryContextAlloc(hashtable->batchCxt,
2424  hashTupleSize);
2425  hashTuple->hashvalue = hashvalue;
2426  memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
2428 
2429  /* Push it onto the front of the skew bucket's list */
2430  hashTuple->next.unshared = hashtable->skewBucket[bucketNumber]->tuples;
2431  hashtable->skewBucket[bucketNumber]->tuples = hashTuple;
2432  Assert(hashTuple != hashTuple->next.unshared);
2433 
2434  /* Account for space used, and back off if we've used too much */
2435  hashtable->spaceUsed += hashTupleSize;
2436  hashtable->spaceUsedSkew += hashTupleSize;
2437  if (hashtable->spaceUsed > hashtable->spacePeak)
2438  hashtable->spacePeak = hashtable->spaceUsed;
2439  while (hashtable->spaceUsedSkew > hashtable->spaceAllowedSkew)
2440  ExecHashRemoveNextSkewBucket(hashtable);
2441 
2442  /* Check we are not over the total spaceAllowed, either */
2443  if (hashtable->spaceUsed > hashtable->spaceAllowed)
2444  ExecHashIncreaseNumBatches(hashtable);
2445 
2446  if (shouldFree)
2447  heap_free_minimal_tuple(tuple);
2448 }
static void ExecHashRemoveNextSkewBucket(HashJoinTable hashtable)
Definition: nodeHash.c:2457
MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot, bool *shouldFree)
Definition: execTuples.c:1662
static void ExecHashIncreaseNumBatches(HashJoinTable hashtable)
Definition: nodeHash.c:885
struct HashJoinTupleData * unshared
Definition: hashjoin.h:72
void heap_free_minimal_tuple(MinimalTuple mtup)
Definition: heaptuple.c:1427
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1937
Size spaceAllowedSkew
Definition: hashjoin.h:346
HashJoinTuple tuples
Definition: hashjoin.h:105
union HashJoinTupleData::@93 next
MemoryContext batchCxt
Definition: hashjoin.h:349
HashSkewBucket ** skewBucket
Definition: hashjoin.h:305
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:79
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
#define Assert(condition)
Definition: c.h:738
#define HeapTupleHeaderClearMatch(tup)
Definition: htup_details.h:526
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
uint32 hashvalue
Definition: hashjoin.h:75

◆ ExecHashTableCreate()

HashJoinTable ExecHashTableCreate ( HashState state,
List hashOperators,
List hashCollations,
bool  keepNulls 
)

Definition at line 430 of file nodeHash.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, HashJoinTableData::area, Assert, BarrierArriveAndWait(), BarrierAttach(), BarrierPhase(), HashJoinTableData::batchCxt, HashJoinTableData::batches, HashJoinTableData::buckets, ParallelHashJoinState::build_barrier, HashJoinTableData::chunks, HashJoinTableData::collations, HashJoinTableData::curbatch, HashJoinTableData::current_chunk, CurrentMemoryContext, elog, ERROR, EState::es_query_dsa, ExecChooseHashTableSize(), ExecHashBuildSkewHash(), ExecParallelHashJoinSetUpBatches(), ExecParallelHashTableAlloc(), fmgr_info(), forboth, get_op_hash_functions(), HashJoinTableData::growEnabled, ParallelHashJoinState::growth, 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(), ParallelHashJoinState::nbatch, HashJoinTableData::nbatch, HashJoinTableData::nbatch_original, HashJoinTableData::nbatch_outstart, ParallelHashJoinState::nbuckets, HashJoinTableData::nbuckets, HashJoinTableData::nbuckets_optimal, HashJoinTableData::nbuckets_original, ParallelHashJoinState::nparticipants, HashJoinTableData::nSkewBuckets, OidIsValid, op_strict(), HashJoinTableData::outer_hashfunctions, HashJoinTableData::outerBatchFile, outerPlan, palloc(), palloc0(), Plan::parallel_aware, HashJoinTableData::parallel_state, HashState::parallel_state, HashJoinTableData::partialTuples, PHJ_BUILD_ELECTING, PHJ_GROWTH_OK, Hash::plan, PlanState::plan, Plan::plan_rows, Plan::plan_width, PrepareTempTablespaces(), printf, HashState::ps, Hash::rows_total, SKEW_WORK_MEM_PERCENT, HashJoinTableData::skewBucket, HashJoinTableData::skewBucketLen, HashJoinTableData::skewBucketNums, HashJoinTableData::skewEnabled, Hash::skewTable, HashJoinTableData::skewTuples, ParallelHashJoinState::space_allowed, HashJoinTableData::spaceAllowed, HashJoinTableData::spaceAllowedSkew, HashJoinTableData::spacePeak, HashJoinTableData::spaceUsed, HashJoinTableData::spaceUsedSkew, PlanState::state, HashJoinTableData::totalTuples, HashJoinTableData::unshared, and WAIT_EVENT_HASH_BUILD_ELECT.

Referenced by ExecHashJoinImpl().

431 {
432  Hash *node;
433  HashJoinTable hashtable;
434  Plan *outerNode;
435  size_t space_allowed;
436  int nbuckets;
437  int nbatch;
438  double rows;
439  int num_skew_mcvs;
440  int log2_nbuckets;
441  int nkeys;
442  int i;
443  ListCell *ho;
444  ListCell *hc;
445  MemoryContext oldcxt;
446 
447  /*
448  * Get information about the size of the relation to be hashed (it's the
449  * "outer" subtree of this node, but the inner relation of the hashjoin).
450  * Compute the appropriate size of the hash table.
451  */
452  node = (Hash *) state->ps.plan;
453  outerNode = outerPlan(node);
454 
455  /*
456  * If this is shared hash table with a partial plan, then we can't use
457  * outerNode->plan_rows to estimate its size. We need an estimate of the
458  * total number of rows across all copies of the partial plan.
459  */
460  rows = node->plan.parallel_aware ? node->rows_total : outerNode->plan_rows;
461 
462  ExecChooseHashTableSize(rows, outerNode->plan_width,
463  OidIsValid(node->skewTable),
464  state->parallel_state != NULL,
465  state->parallel_state != NULL ?
466  state->parallel_state->nparticipants - 1 : 0,
467  &space_allowed,
468  &nbuckets, &nbatch, &num_skew_mcvs);
469 
470  /* nbuckets must be a power of 2 */
471  log2_nbuckets = my_log2(nbuckets);
472  Assert(nbuckets == (1 << log2_nbuckets));
473 
474  /*
475  * Initialize the hash table control block.
476  *
477  * The hashtable control block is just palloc'd from the executor's
478  * per-query memory context. Everything else should be kept inside the
479  * subsidiary hashCxt or batchCxt.
480  */
481  hashtable = (HashJoinTable) palloc(sizeof(HashJoinTableData));
482  hashtable->nbuckets = nbuckets;
483  hashtable->nbuckets_original = nbuckets;
484  hashtable->nbuckets_optimal = nbuckets;
485  hashtable->log2_nbuckets = log2_nbuckets;
486  hashtable->log2_nbuckets_optimal = log2_nbuckets;
487  hashtable->buckets.unshared = NULL;
488  hashtable->keepNulls = keepNulls;
489  hashtable->skewEnabled = false;
490  hashtable->skewBucket = NULL;
491  hashtable->skewBucketLen = 0;
492  hashtable->nSkewBuckets = 0;
493  hashtable->skewBucketNums = NULL;
494  hashtable->nbatch = nbatch;
495  hashtable->curbatch = 0;
496  hashtable->nbatch_original = nbatch;
497  hashtable->nbatch_outstart = nbatch;
498  hashtable->growEnabled = true;
499  hashtable->totalTuples = 0;
500  hashtable->partialTuples = 0;
501  hashtable->skewTuples = 0;
502  hashtable->innerBatchFile = NULL;
503  hashtable->outerBatchFile = NULL;
504  hashtable->spaceUsed = 0;
505  hashtable->spacePeak = 0;
506  hashtable->spaceAllowed = space_allowed;
507  hashtable->spaceUsedSkew = 0;
508  hashtable->spaceAllowedSkew =
509  hashtable->spaceAllowed * SKEW_WORK_MEM_PERCENT / 100;
510  hashtable->chunks = NULL;
511  hashtable->current_chunk = NULL;
512  hashtable->parallel_state = state->parallel_state;
513  hashtable->area = state->ps.state->es_query_dsa;
514  hashtable->batches = NULL;
515 
516 #ifdef HJDEBUG
517  printf("Hashjoin %p: initial nbatch = %d, nbuckets = %d\n",
518  hashtable, nbatch, nbuckets);
519 #endif
520 
521  /*
522  * Create temporary memory contexts in which to keep the hashtable working
523  * storage. See notes in executor/hashjoin.h.
524  */
526  "HashTableContext",
528 
529  hashtable->batchCxt = AllocSetContextCreate(hashtable->hashCxt,
530  "HashBatchContext",
532 
533  /* Allocate data that will live for the life of the hashjoin */
534 
535  oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
536 
537  /*
538  * Get info about the hash functions to be used for each hash key. Also
539  * remember whether the join operators are strict.
540  */
541  nkeys = list_length(hashOperators);
542  hashtable->outer_hashfunctions =
543  (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
544  hashtable->inner_hashfunctions =
545  (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
546  hashtable->hashStrict = (bool *) palloc(nkeys * sizeof(bool));
547  hashtable->collations = (Oid *) palloc(nkeys * sizeof(Oid));
548  i = 0;
549  forboth(ho, hashOperators, hc, hashCollations)
550  {
551  Oid hashop = lfirst_oid(ho);
552  Oid left_hashfn;
553  Oid right_hashfn;
554 
555  if (!get_op_hash_functions(hashop, &left_hashfn, &right_hashfn))
556  elog(ERROR, "could not find hash function for hash operator %u",
557  hashop);
558  fmgr_info(left_hashfn, &hashtable->outer_hashfunctions[i]);
559  fmgr_info(right_hashfn, &hashtable->inner_hashfunctions[i]);
560  hashtable->hashStrict[i] = op_strict(hashop);
561  hashtable->collations[i] = lfirst_oid(hc);
562  i++;
563  }
564 
565  if (nbatch > 1 && hashtable->parallel_state == NULL)
566  {
567  /*
568  * allocate and initialize the file arrays in hashCxt (not needed for
569  * parallel case which uses shared tuplestores instead of raw files)
570  */
571  hashtable->innerBatchFile = (BufFile **)
572  palloc0(nbatch * sizeof(BufFile *));
573  hashtable->outerBatchFile = (BufFile **)
574  palloc0(nbatch * sizeof(BufFile *));
575  /* The files will not be opened until needed... */
576  /* ... but make sure we have temp tablespaces established for them */
578  }
579 
580  MemoryContextSwitchTo(oldcxt);
581 
582  if (hashtable->parallel_state)
583  {
584  ParallelHashJoinState *pstate = hashtable->parallel_state;
585  Barrier *build_barrier;
586 
587  /*
588  * Attach to the build barrier. The corresponding detach operation is
589  * in ExecHashTableDetach. Note that we won't attach to the
590  * batch_barrier for batch 0 yet. We'll attach later and start it out
591  * in PHJ_BATCH_PROBING phase, because batch 0 is allocated up front
592  * and then loaded while hashing (the standard hybrid hash join
593  * algorithm), and we'll coordinate that using build_barrier.
594  */
595  build_barrier = &pstate->build_barrier;
596  BarrierAttach(build_barrier);
597 
598  /*
599  * So far we have no idea whether there are any other participants,
600  * and if so, what phase they are working on. The only thing we care
601  * about at this point is whether someone has already created the
602  * SharedHashJoinBatch objects and the hash table for batch 0. One
603  * backend will be elected to do that now if necessary.
604  */
605  if (BarrierPhase(build_barrier) == PHJ_BUILD_ELECTING &&
607  {
608  pstate->nbatch = nbatch;
609  pstate->space_allowed = space_allowed;
610  pstate->growth = PHJ_GROWTH_OK;
611 
612  /* Set up the shared state for coordinating batches. */
613  ExecParallelHashJoinSetUpBatches(hashtable, nbatch);
614 
615  /*
616  * Allocate batch 0's hash table up front so we can load it
617  * directly while hashing.
618  */
619  pstate->nbuckets = nbuckets;
620  ExecParallelHashTableAlloc(hashtable, 0);
621  }
622 
623  /*
624  * The next Parallel Hash synchronization point is in
625  * MultiExecParallelHash(), which will progress it all the way to
626  * PHJ_BUILD_DONE. The caller must not return control from this
627  * executor node between now and then.
628  */
629  }
630  else
631  {
632  /*
633  * Prepare context for the first-scan space allocations; allocate the
634  * hashbucket array therein, and set each bucket "empty".
635  */
636  MemoryContextSwitchTo(hashtable->batchCxt);
637 
638  hashtable->buckets.unshared = (HashJoinTuple *)
639  palloc0(nbuckets * sizeof(HashJoinTuple));
640 
641  /*
642  * Set up for skew optimization, if possible and there's a need for
643  * more than one batch. (In a one-batch join, there's no point in
644  * it.)
645  */
646  if (nbatch > 1)
647  ExecHashBuildSkewHash(hashtable, node, num_skew_mcvs);
648 
649  MemoryContextSwitchTo(oldcxt);
650  }
651 
652  return hashtable;
653 }
int log2_nbuckets_optimal
Definition: hashjoin.h:291
double rows_total
Definition: plannodes.h:935
Oid skewTable
Definition: plannodes.h:931
struct ParallelHashJoinState * parallel_state
Definition: execnodes.h:2400
double skewTuples
Definition: hashjoin.h:320
Definition: fmgr.h:56
struct dsa_area * es_query_dsa
Definition: execnodes.h:589
double plan_rows
Definition: plannodes.h:129
bool op_strict(Oid opno)
Definition: lsyscache.c:1340
#define AllocSetContextCreate
Definition: memutils.h:170
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:508
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define printf(...)
Definition: port.h:199
FmgrInfo * inner_hashfunctions
Definition: hashjoin.h:338
void ExecParallelHashTableAlloc(HashJoinTable hashtable, int batchno)
Definition: nodeHash.c:3095
EState * state
Definition: execnodes.h:947
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:644
static void ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
Definition: nodeHash.c:2212
double partialTuples
Definition: hashjoin.h:319
dsa_area * area
Definition: hashjoin.h:356
int * skewBucketNums
Definition: hashjoin.h:308
#define ERROR
Definition: elog.h:43
void PrepareTempTablespaces(void)
Definition: tablespace.c:1323
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
BufFile ** outerBatchFile
Definition: hashjoin.h:330
Size spaceAllowedSkew
Definition: hashjoin.h:346
bool parallel_aware
Definition: plannodes.h:135
PlanState ps
Definition: execnodes.h:2380
#define PHJ_BUILD_ELECTING
Definition: hashjoin.h:257
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
MemoryContext batchCxt
Definition: hashjoin.h:349
struct HashJoinTableData * HashJoinTable
Definition: execnodes.h:1938
int my_log2(long num)
Definition: dynahash.c:1720
#define outerPlan(node)
Definition: plannodes.h:172
FmgrInfo * outer_hashfunctions
Definition: hashjoin.h:337
HashSkewBucket ** skewBucket
Definition: hashjoin.h:305
int BarrierAttach(Barrier *barrier)
Definition: barrier.c:214
void * palloc0(Size size)
Definition: mcxt.c:980
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
union HashJoinTableData::@95 buckets
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
Plan * plan
Definition: execnodes.h:945
double totalTuples
Definition: hashjoin.h:318
int plan_width
Definition: plannodes.h:130
#define Assert(condition)
Definition: c.h:738
ParallelHashGrowth growth
Definition: hashjoin.h:241
BufFile ** innerBatchFile
Definition: hashjoin.h:329
static int list_length(const List *l)
Definition: pg_list.h:169
int BarrierPhase(Barrier *barrier)
Definition: barrier.c:243
bool BarrierArriveAndWait(Barrier *barrier, uint32 wait_event_info)
Definition: barrier.c:125
HashMemoryChunk chunks
Definition: hashjoin.h:352
Plan plan
Definition: plannodes.h:924
void * palloc(Size size)
Definition: mcxt.c:949
static void ExecParallelHashJoinSetUpBatches(HashJoinTable hashtable, int nbatch)
Definition: nodeHash.c:2934
struct HashJoinTupleData ** unshared
Definition: hashjoin.h:297
HashMemoryChunk current_chunk
Definition: hashjoin.h:355
#define elog(elevel,...)
Definition: elog.h:214
int i
bool * hashStrict
Definition: hashjoin.h:339
MemoryContext hashCxt
Definition: hashjoin.h:348
#define SKEW_WORK_MEM_PERCENT
Definition: hashjoin.h:110
void ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew, bool try_combined_work_mem, int parallel_workers, size_t *space_allowed, int *numbuckets, int *numbatches, int *num_skew_mcvs)
Definition: nodeHash.c:667
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ ExecHashTableDestroy()

void ExecHashTableDestroy ( HashJoinTable  hashtable)

Definition at line 852 of file nodeHash.c.

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

Referenced by ExecEndHashJoin(), and ExecReScanHashJoin().

853 {
854  int i;
855 
856  /*
857  * Make sure all the temp files are closed. We skip batch 0, since it
858  * can't have any temp files (and the arrays might not even exist if
859  * nbatch is only 1). Parallel hash joins don't use these files.
860  */
861  if (hashtable->innerBatchFile != NULL)
862  {
863  for (i = 1; i < hashtable->nbatch; i++)
864  {
865  if (hashtable->innerBatchFile[i])
866  BufFileClose(hashtable->innerBatchFile[i]);
867  if (hashtable->outerBatchFile[i])
868  BufFileClose(hashtable->outerBatchFile[i]);
869  }
870  }
871 
872  /* Release working memory (batchCxt is a child, so it goes away too) */
873  MemoryContextDelete(hashtable->hashCxt);
874 
875  /* And drop the control block */
876  pfree(hashtable);
877 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
void BufFileClose(BufFile *file)
Definition: buffile.c:391
void pfree(void *pointer)
Definition: mcxt.c:1056
BufFile ** outerBatchFile
Definition: hashjoin.h:330
BufFile ** innerBatchFile
Definition: hashjoin.h:329
int i
MemoryContext hashCxt
Definition: hashjoin.h:348

◆ ExecHashTableDetach()

void ExecHashTableDetach ( HashJoinTable  hashtable)

Definition at line 3172 of file nodeHash.c.

References HashJoinTableData::area, BarrierDetach(), ParallelHashJoinState::batches, HashJoinTableData::batches, ParallelHashJoinState::build_barrier, dsa_free(), DsaPointerIsValid, i, ParallelHashJoinBatchAccessor::inner_tuples, InvalidDsaPointer, HashJoinTableData::nbatch, ParallelHashJoinBatchAccessor::outer_tuples, HashJoinTableData::parallel_state, sts_end_parallel_scan(), and sts_end_write().

Referenced by ExecHashJoinReInitializeDSM(), and ExecShutdownHashJoin().

3173 {
3174  if (hashtable->parallel_state)
3175  {
3176  ParallelHashJoinState *pstate = hashtable->parallel_state;
3177  int i;
3178 
3179  /* Make sure any temporary files are closed. */
3180  if (hashtable->batches)
3181  {
3182  for (i = 0; i < hashtable->nbatch; ++i)
3183  {
3184  sts_end_write(hashtable->batches[i].inner_tuples);
3185  sts_end_write(hashtable->batches[i].outer_tuples);
3188  }
3189  }
3190 
3191  /* If we're last to detach, clean up shared memory. */
3192  if (BarrierDetach(&pstate->build_barrier))
3193  {
3194  if (DsaPointerIsValid(pstate->batches))
3195  {
3196  dsa_free(hashtable->area, pstate->batches);
3197  pstate->batches = InvalidDsaPointer;
3198  }
3199  }
3200 
3201  hashtable->parallel_state = NULL;
3202  }
3203 }
SharedTuplestoreAccessor * outer_tuples
Definition: hashjoin.h:209
#define InvalidDsaPointer
Definition: dsa.h:78
SharedTuplestoreAccessor * inner_tuples
Definition: hashjoin.h:208
dsa_area * area
Definition: hashjoin.h:356
dsa_pointer batches
Definition: hashjoin.h:236
void sts_end_parallel_scan(SharedTuplestoreAccessor *accessor)
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
bool BarrierDetach(Barrier *barrier)
Definition: barrier.c:234
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
int i
void sts_end_write(SharedTuplestoreAccessor *accessor)

◆ ExecHashTableDetachBatch()

void ExecHashTableDetachBatch ( HashJoinTable  hashtable)

Definition at line 3115 of file nodeHash.c.

References HashJoinTableData::area, Assert, BarrierArriveAndDetach(), BarrierPhase(), ParallelHashJoinBatch::batch_barrier, HashJoinTableData::batches, ParallelHashJoinBatch::buckets, ParallelHashJoinBatch::chunks, HashJoinTableData::curbatch, dsa_free(), dsa_get_address(), DsaPointerIsValid, ParallelHashJoinBatchAccessor::inner_tuples, InvalidDsaPointer, Max, HashJoinTableData::nbuckets, HashMemoryChunkData::next, next, ParallelHashJoinBatchAccessor::outer_tuples, HashJoinTableData::parallel_state, PHJ_BATCH_DONE, HashMemoryChunkData::shared, ParallelHashJoinBatchAccessor::shared, ParallelHashJoinBatch::size, HashJoinTableData::spacePeak, and sts_end_parallel_scan().

Referenced by ExecHashJoinReInitializeDSM(), ExecParallelHashJoinNewBatch(), and ExecShutdownHashJoin().

3116 {
3117  if (hashtable->parallel_state != NULL &&
3118  hashtable->curbatch >= 0)
3119  {
3120  int curbatch = hashtable->curbatch;
3121  ParallelHashJoinBatch *batch = hashtable->batches[curbatch].shared;
3122 
3123  /* Make sure any temporary files are closed. */
3124  sts_end_parallel_scan(hashtable->batches[curbatch].inner_tuples);
3125  sts_end_parallel_scan(hashtable->batches[curbatch].outer_tuples);
3126 
3127  /* Detach from the batch we were last working on. */
3129  {
3130  /*
3131  * Technically we shouldn't access the barrier because we're no
3132  * longer attached, but since there is no way it's moving after
3133  * this point it seems safe to make the following assertion.
3134  */
3136 
3137  /* Free shared chunks and buckets. */
3138  while (DsaPointerIsValid(batch->chunks))
3139  {
3140  HashMemoryChunk chunk =
3141  dsa_get_address(hashtable->area, batch->chunks);
3142  dsa_pointer next = chunk->next.shared;
3143 
3144  dsa_free(hashtable->area, batch->chunks);
3145  batch->chunks = next;
3146  }
3147  if (DsaPointerIsValid(batch->buckets))
3148  {
3149  dsa_free(hashtable->area, batch->buckets);
3150  batch->buckets = InvalidDsaPointer;
3151  }
3152  }
3153 
3154  /*
3155  * Track the largest batch we've been attached to. Though each
3156  * backend might see a different subset of batches, explain.c will
3157  * scan the results from all backends to find the largest value.
3158  */
3159  hashtable->spacePeak =
3160  Max(hashtable->spacePeak,
3161  batch->size + sizeof(dsa_pointer_atomic) * hashtable->nbuckets);
3162 
3163  /* Remember that we are not attached to a batch. */
3164  hashtable->curbatch = -1;
3165  }
3166 }
SharedTuplestoreAccessor * outer_tuples
Definition: hashjoin.h:209
#define PHJ_BATCH_DONE
Definition: hashjoin.h:268
static int32 next
Definition: blutils.c:218
#define InvalidDsaPointer
Definition: dsa.h:78
dsa_pointer chunks
Definition: hashjoin.h:156
uint64 dsa_pointer
Definition: dsa.h:62
SharedTuplestoreAccessor * inner_tuples
Definition: hashjoin.h:208
dsa_area * area
Definition: hashjoin.h:356
dsa_pointer shared
Definition: hashjoin.h:127
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
void sts_end_parallel_scan(SharedTuplestoreAccessor *accessor)
bool BarrierArriveAndDetach(Barrier *barrier)
Definition: barrier.c:203
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
#define Max(x, y)
Definition: c.h:914
#define Assert(condition)
Definition: c.h:738
int BarrierPhase(Barrier *barrier)
Definition: barrier.c:243
ParallelHashJoinBatch * shared
Definition: hashjoin.h:197
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
union HashMemoryChunkData::@94 next
dsa_pointer buckets
Definition: hashjoin.h:153

◆ ExecHashTableInsert()

void ExecHashTableInsert ( HashJoinTable  hashtable,
TupleTableSlot slot,
uint32  hashvalue 
)

Definition at line 1592 of file nodeHash.c.

References Assert, HashJoinTableData::buckets, HashJoinTableData::curbatch, dense_alloc(), ExecFetchSlotMinimalTuple(), ExecHashGetBucketAndBatch(), ExecHashIncreaseNumBatches(), ExecHashJoinSaveTuple(), HashJoinTupleData::hashvalue, heap_free_minimal_tuple(), 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, HashJoinTableData::totalTuples, HashJoinTupleData::unshared, and HashJoinTableData::unshared.

Referenced by ExecHashJoinNewBatch(), and MultiExecPrivateHash().

1595 {
1596  bool shouldFree;
1597  MinimalTuple tuple = ExecFetchSlotMinimalTuple(slot, &shouldFree);
1598  int bucketno;
1599  int batchno;
1600 
1601  ExecHashGetBucketAndBatch(hashtable, hashvalue,
1602  &bucketno, &batchno);
1603 
1604  /*
1605  * decide whether to put the tuple in the hash table or a temp file
1606  */
1607  if (batchno == hashtable->curbatch)
1608  {
1609  /*
1610  * put the tuple in hash table
1611  */
1612  HashJoinTuple hashTuple;
1613  int hashTupleSize;
1614  double ntuples = (hashtable->totalTuples - hashtable->skewTuples);
1615 
1616  /* Create the HashJoinTuple */
1617  hashTupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
1618  hashTuple = (HashJoinTuple) dense_alloc(hashtable, hashTupleSize);
1619 
1620  hashTuple->hashvalue = hashvalue;
1621  memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
1622 
1623  /*
1624  * We always reset the tuple-matched flag on insertion. This is okay
1625  * even when reloading a tuple from a batch file, since the tuple
1626  * could not possibly have been matched to an outer tuple before it
1627  * went into the batch file.
1628  */
1630 
1631  /* Push it onto the front of the bucket's list */
1632  hashTuple->next.unshared = hashtable->buckets.unshared[bucketno];
1633  hashtable->buckets.unshared[bucketno] = hashTuple;
1634 
1635  /*
1636  * Increase the (optimal) number of buckets if we just exceeded the
1637  * NTUP_PER_BUCKET threshold, but only when there's still a single
1638  * batch.
1639  */
1640  if (hashtable->nbatch == 1 &&
1641  ntuples > (hashtable->nbuckets_optimal * NTUP_PER_BUCKET))
1642  {
1643  /* Guard against integer overflow and alloc size overflow */
1644  if (hashtable->nbuckets_optimal <= INT_MAX / 2 &&
1645  hashtable->nbuckets_optimal * 2 <= MaxAllocSize / sizeof(HashJoinTuple))
1646  {
1647  hashtable->nbuckets_optimal *= 2;
1648  hashtable->log2_nbuckets_optimal += 1;
1649  }
1650  }
1651 
1652  /* Account for space used, and back off if we've used too much */
1653  hashtable->spaceUsed += hashTupleSize;
1654  if (hashtable->spaceUsed > hashtable->spacePeak)
1655  hashtable->spacePeak = hashtable->spaceUsed;
1656  if (hashtable->spaceUsed +
1657  hashtable->nbuckets_optimal * sizeof(HashJoinTuple)
1658  > hashtable->spaceAllowed)
1659  ExecHashIncreaseNumBatches(hashtable);
1660  }
1661  else
1662  {
1663  /*
1664  * put the tuple into a temp file for later batches
1665  */
1666  Assert(batchno > hashtable->curbatch);
1667  ExecHashJoinSaveTuple(tuple,
1668  hashvalue,
1669  &hashtable->innerBatchFile[batchno]);
1670  }
1671 
1672  if (shouldFree)
1673  heap_free_minimal_tuple(tuple);
1674 }
int log2_nbuckets_optimal
Definition: hashjoin.h:291
double skewTuples
Definition: hashjoin.h:320
MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot, bool *shouldFree)
Definition: execTuples.c:1662
static void ExecHashIncreaseNumBatches(HashJoinTable hashtable)
Definition: nodeHash.c:885
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1898
struct HashJoinTupleData * unshared
Definition: hashjoin.h:72
void heap_free_minimal_tuple(MinimalTuple mtup)
Definition: heaptuple.c:1427
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1937
union HashJoinTupleData::@93 next
#define MaxAllocSize
Definition: memutils.h:40
static void * dense_alloc(HashJoinTable hashtable, Size size)
Definition: nodeHash.c:2706
#define NTUP_PER_BUCKET
Definition: nodeHash.c:664
union HashJoinTableData::@95 buckets
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:79
double totalTuples
Definition: hashjoin.h:318
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
#define Assert(condition)
Definition: c.h:738
BufFile ** innerBatchFile
Definition: hashjoin.h:329
#define HeapTupleHeaderClearMatch(tup)
Definition: htup_details.h:526
struct HashJoinTupleData ** unshared
Definition: hashjoin.h:297
void ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr)
uint32 hashvalue
Definition: hashjoin.h:75

◆ ExecHashTableReset()

void ExecHashTableReset ( HashJoinTable  hashtable)

Definition at line 2137 of file nodeHash.c.

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

Referenced by ExecHashJoinNewBatch().

2138 {
2139  MemoryContext oldcxt;
2140  int nbuckets = hashtable->nbuckets;
2141 
2142  /*
2143  * Release all the hash buckets and tuples acquired in the prior pass, and
2144  * reinitialize the context for a new pass.
2145  */
2146  MemoryContextReset(hashtable->batchCxt);
2147  oldcxt = MemoryContextSwitchTo(hashtable->batchCxt);
2148 
2149  /* Reallocate and reinitialize the hash bucket headers. */
2150  hashtable->buckets.unshared = (HashJoinTuple *)
2151  palloc0(nbuckets * sizeof(HashJoinTuple));
2152 
2153  hashtable->spaceUsed = 0;
2154 
2155  MemoryContextSwitchTo(oldcxt);
2156 
2157  /* Forget the chunks (the memory was freed by the context reset above). */
2158  hashtable->chunks = NULL;
2159 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
MemoryContext batchCxt
Definition: hashjoin.h:349
void * palloc0(Size size)
Definition: mcxt.c:980
union HashJoinTableData::@95 buckets
HashMemoryChunk chunks
Definition: hashjoin.h:352
struct HashJoinTupleData ** unshared
Definition: hashjoin.h:297

◆ ExecHashTableResetMatchFlags()

void ExecHashTableResetMatchFlags ( HashJoinTable  hashtable)

Definition at line 2166 of file nodeHash.c.

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

Referenced by ExecReScanHashJoin().

2167 {
2168  HashJoinTuple tuple;
2169  int i;
2170 
2171  /* Reset all flags in the main table ... */
2172  for (i = 0; i < hashtable->nbuckets; i++)
2173  {
2174  for (tuple = hashtable->buckets.unshared[i]; tuple != NULL;
2175  tuple = tuple->next.unshared)
2177  }
2178 
2179  /* ... and the same for the skew buckets, if any */
2180  for (i = 0; i < hashtable->nSkewBuckets; i++)
2181  {
2182  int j = hashtable->skewBucketNums[i];
2183  HashSkewBucket *skewBucket = hashtable->skewBucket[j];
2184 
2185  for (tuple = skewBucket->tuples; tuple != NULL; tuple = tuple->next.unshared)
2187  }
2188 }
int * skewBucketNums
Definition: hashjoin.h:308
struct HashJoinTupleData * unshared
Definition: hashjoin.h:72
HashJoinTuple tuples
Definition: hashjoin.h:105
union HashJoinTupleData::@93 next
HashSkewBucket ** skewBucket
Definition: hashjoin.h:305
union HashJoinTableData::@95 buckets
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
#define HeapTupleHeaderClearMatch(tup)
Definition: htup_details.h:526
struct HashJoinTupleData ** unshared
Definition: hashjoin.h:297
int i

◆ ExecInitHash()

HashState* ExecInitHash ( Hash node,
EState estate,
int  eflags 
)

Definition at line 353 of file nodeHash.c.

References Assert, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecHash(), ExecInitExprList(), ExecInitNode(), ExecInitResultTupleSlotTL(), PlanState::ExecProcNode, Hash::hashkeys, HashState::hashkeys, HashState::hashtable, makeNode, NIL, outerPlan, outerPlanState, Hash::plan, PlanState::plan, HashState::ps, PlanState::ps_ProjInfo, Plan::qual, PlanState::state, and TTSOpsMinimalTuple.

Referenced by ExecInitNode().

354 {
355  HashState *hashstate;
356 
357  /* check for unsupported flags */
358  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
359 
360  /*
361  * create state structure
362  */
363  hashstate = makeNode(HashState);
364  hashstate->ps.plan = (Plan *) node;
365  hashstate->ps.state = estate;
366  hashstate->ps.ExecProcNode = ExecHash;
367  hashstate->hashtable = NULL;
368  hashstate->hashkeys = NIL; /* will be set by parent HashJoin */
369 
370  /*
371  * Miscellaneous initialization
372  *
373  * create expression context for node
374  */
375  ExecAssignExprContext(estate, &hashstate->ps);
376 
377  /*
378  * initialize child nodes
379  */
380  outerPlanState(hashstate) = ExecInitNode(outerPlan(node), estate, eflags);
381 
382  /*
383  * initialize our result slot and type. No need to build projection
384  * because this node doesn't do projections.
385  */
387  hashstate->ps.ps_ProjInfo = NULL;
388 
389  /*
390  * initialize child expressions
391  */
392  Assert(node->plan.qual == NIL);
393  hashstate->hashkeys =
394  ExecInitExprList(node->hashkeys, (PlanState *) hashstate);
395 
396  return hashstate;
397 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:143
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:985
HashJoinTable hashtable
Definition: execnodes.h:2381
EState * state
Definition: execnodes.h:947
#define EXEC_FLAG_BACKWARD
Definition: executor.h:58
#define outerPlanState(node)
Definition: execnodes.h:1039
List * ExecInitExprList(List *nodes, PlanState *parent)
Definition: execExpr.c:318
List * hashkeys
Definition: execnodes.h:2382
PlanState ps
Definition: execnodes.h:2380
#define outerPlan(node)
Definition: plannodes.h:172
static TupleTableSlot * ExecHash(PlanState *pstate)
Definition: nodeHash.c:91
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:951
List * hashkeys
Definition: plannodes.h:930
Plan * plan
Definition: execnodes.h:945
#define makeNode(_type_)
Definition: nodes.h:577
#define Assert(condition)
Definition: c.h:738
#define EXEC_FLAG_MARK
Definition: executor.h:59
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:485
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1769
Plan plan
Definition: plannodes.h:924
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85

◆ ExecParallelHashCloseBatchAccessors()

static void ExecParallelHashCloseBatchAccessors ( HashJoinTable  hashtable)
static

Definition at line 3011 of file nodeHash.c.

References HashJoinTableData::batches, i, ParallelHashJoinBatchAccessor::inner_tuples, HashJoinTableData::nbatch, ParallelHashJoinBatchAccessor::outer_tuples, pfree(), sts_end_parallel_scan(), and sts_end_write().

Referenced by ExecParallelHashEnsureBatchAccessors(), and ExecParallelHashIncreaseNumBatches().

3012 {
3013  int i;
3014 
3015  for (i = 0; i < hashtable->nbatch; ++i)
3016  {
3017  /* Make sure no files are left open. */
3018  sts_end_write(hashtable->batches[i].inner_tuples);
3019  sts_end_write(hashtable->batches[i].outer_tuples);
3022  }
3023  pfree(hashtable->batches);
3024  hashtable->batches = NULL;
3025 }
SharedTuplestoreAccessor * outer_tuples
Definition: hashjoin.h:209
SharedTuplestoreAccessor * inner_tuples
Definition: hashjoin.h:208
void pfree(void *pointer)
Definition: mcxt.c:1056
void sts_end_parallel_scan(SharedTuplestoreAccessor *accessor)
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
int i
void sts_end_write(SharedTuplestoreAccessor *accessor)

◆ ExecParallelHashEnsureBatchAccessors()

static void ExecParallelHashEnsureBatchAccessors ( HashJoinTable  hashtable)
static

Definition at line 3032 of file nodeHash.c.

References HashJoinTableData::area, ParallelHashJoinState::batches, HashJoinTableData::batches, ParallelHashJoinBatchAccessor::done, dsa_get_address(), DsaPointerIsValid, ExecParallelHashCloseBatchAccessors(), ParallelHashJoinState::fileset, HashJoinTableData::hashCxt, i, ParallelHashJoinBatchAccessor::inner_tuples, MemoryContextSwitchTo(), ParallelHashJoinState::nbatch, HashJoinTableData::nbatch, ParallelHashJoinState::nparticipants, NthParallelHashJoinBatch, ParallelHashJoinBatchAccessor::outer_tuples, palloc0(), HashJoinTableData::parallel_state, ParallelHashJoinBatchInner, ParallelHashJoinBatchOuter, ParallelWorkerNumber, ParallelHashJoinBatchAccessor::preallocated, ParallelHashJoinBatchAccessor::shared, and sts_attach().

Referenced by ExecParallelHashIncreaseNumBatches(), ExecParallelHashIncreaseNumBuckets(), and MultiExecParallelHash().

3033 {
3034  ParallelHashJoinState *pstate = hashtable->parallel_state;
3035  ParallelHashJoinBatch *batches;
3036  MemoryContext oldcxt;
3037  int i;
3038 
3039  if (hashtable->batches != NULL)
3040  {
3041  if (hashtable->nbatch == pstate->nbatch)
3042  return;
3044  }
3045 
3046  /*
3047  * It's possible for a backend to start up very late so that the whole
3048  * join is finished and the shm state for tracking batches has already
3049  * been freed by ExecHashTableDetach(). In that case we'll just leave
3050  * hashtable->batches as NULL so that ExecParallelHashJoinNewBatch() gives
3051  * up early.
3052  */
3053  if (!DsaPointerIsValid(pstate->batches))
3054  return;
3055 
3056  /* Use hash join memory context. */
3057  oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
3058 
3059  /* Allocate this backend's accessor array. */
3060  hashtable->nbatch = pstate->nbatch;
3061  hashtable->batches = (ParallelHashJoinBatchAccessor *)
3062  palloc0(sizeof(ParallelHashJoinBatchAccessor) * hashtable->nbatch);
3063 
3064  /* Find the base of the pseudo-array of ParallelHashJoinBatch objects. */
3065  batches = (ParallelHashJoinBatch *)
3066  dsa_get_address(hashtable->area, pstate->batches);
3067 
3068  /* Set up the accessor array and attach to the tuplestores. */
3069  for (i = 0; i < hashtable->nbatch; ++i)
3070  {
3071  ParallelHashJoinBatchAccessor *accessor = &hashtable->batches[i];
3072  ParallelHashJoinBatch *shared = NthParallelHashJoinBatch(batches, i);
3073 
3074  accessor->shared = shared;
3075  accessor->preallocated = 0;
3076  accessor->done = false;
3077  accessor->inner_tuples =
3080  &pstate->fileset);
3081  accessor->outer_tuples =
3083  pstate->nparticipants),
3085  &pstate->fileset);
3086  }
3087 
3088  MemoryContextSwitchTo(oldcxt);
3089 }
SharedTuplestoreAccessor * outer_tuples
Definition: hashjoin.h:209
static void ExecParallelHashCloseBatchAccessors(HashJoinTable hashtable)
Definition: nodeHash.c:3011
#define ParallelHashJoinBatchOuter(batch, nparticipants)
Definition: hashjoin.h:175
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SharedFileSet fileset
Definition: hashjoin.h:253
SharedTuplestoreAccessor * inner_tuples
Definition: hashjoin.h:208
dsa_area * area
Definition: hashjoin.h:356
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
dsa_pointer batches
Definition: hashjoin.h:236
#define ParallelHashJoinBatchInner(batch)
Definition: hashjoin.h:170
SharedTuplestoreAccessor * sts_attach(SharedTuplestore *sts, int my_participant_number, SharedFileSet *fileset)
int ParallelWorkerNumber
Definition: parallel.c:112
void * palloc0(Size size)
Definition: mcxt.c:980
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
#define NthParallelHashJoinBatch(base, n)
Definition: hashjoin.h:186
ParallelHashJoinBatch * shared
Definition: hashjoin.h:197
#define DsaPointerIsValid(x)
Definition: dsa.h:81
int i
MemoryContext hashCxt
Definition: hashjoin.h:348

◆ ExecParallelHashFirstTuple()

static HashJoinTuple ExecParallelHashFirstTuple ( HashJoinTable  table,
int  bucketno 
)
inlinestatic

Definition at line 3209 of file nodeHash.c.

References HashJoinTableData::area, Assert, HashJoinTableData::buckets, dsa_get_address(), dsa_pointer_atomic_read, HashJoinTableData::parallel_state, and HashJoinTableData::shared.

Referenced by ExecParallelScanHashBucket().

3210 {
3211  HashJoinTuple tuple;
3212  dsa_pointer p;
3213 
3214  Assert(hashtable->parallel_state);
3215  p = dsa_pointer_atomic_read(&hashtable->buckets.shared[bucketno]);
3216  tuple = (HashJoinTuple) dsa_get_address(hashtable->area, p);
3217 
3218  return tuple;
3219 }
uint64 dsa_pointer
Definition: dsa.h:62
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1937
#define Assert(condition)
Definition: c.h:738
#define dsa_pointer_atomic_read
Definition: dsa.h:65

◆ ExecParallelHashIncreaseNumBatches()

static void ExecParallelHashIncreaseNumBatches ( HashJoinTable  hashtable)
static

Definition at line 1056 of file nodeHash.c.

References HashJoinTableData::area, Assert, BarrierArriveAndWait(), BarrierPhase(), ParallelHashJoinState::batches, HashJoinTableData::batches, ParallelHashJoinBatch::buckets, ParallelHashJoinState::build_barrier, ParallelHashJoinState::chunk_work_queue, ParallelHashJoinBatch::chunks, dsa_allocate, dsa_free(), dsa_get_address(), dsa_pointer_atomic_init, dsa_pointer_atomic_write, ParallelHashJoinBatch::estimated_size, ExecParallelHashCloseBatchAccessors(), ExecParallelHashEnsureBatchAccessors(), ExecParallelHashJoinSetUpBatches(), ExecParallelHashMergeCounters(), ExecParallelHashRepartitionFirst(), ExecParallelHashRepartitionRest(), ExecParallelHashTableSetCurrentBatch(), ParallelHashJoinState::grow_batches_barrier, ParallelHashJoinState::growth, i, InvalidDsaPointer, Max, MaxAllocSize, Min, my_log2(), ParallelHashJoinState::nbatch, HashJoinTableData::nbatch, ParallelHashJoinState::nbuckets, HashJoinTableData::nbuckets, ParallelHashJoinState::nparticipants, NTUP_PER_BUCKET, ParallelHashJoinBatch::ntuples, ParallelHashJoinState::old_batches, ParallelHashJoinState::old_nbatch, ParallelHashJoinBatch::old_ntuples, HashJoinTableData::parallel_state, PHJ_BUILD_HASHING_INNER, PHJ_GROW_BATCHES_ALLOCATING, PHJ_GROW_BATCHES_DECIDING, PHJ_GROW_BATCHES_ELECTING, PHJ_GROW_BATCHES_FINISHING, PHJ_GROW_BATCHES_PHASE, PHJ_GROW_BATCHES_REPARTITIONING, PHJ_GROWTH_DISABLED, PHJ_GROWTH_NEED_MORE_BATCHES, PHJ_GROWTH_OK, ParallelHashJoinBatchAccessor::shared, ParallelHashJoinState::space_allowed, ParallelHashJoinBatch::space_exhausted, WAIT_EVENT_HASH_GROW_BATCHES_ALLOCATE, WAIT_EVENT_HASH_GROW_BATCHES_DECIDE, WAIT_EVENT_HASH_GROW_BATCHES_ELECT, WAIT_EVENT_HASH_GROW_BATCHES_FINISH, WAIT_EVENT_HASH_GROW_BATCHES_REPARTITION, and work_mem.

Referenced by ExecParallelHashTupleAlloc(), ExecParallelHashTuplePrealloc(), and MultiExecParallelHash().

1057 {
1058  ParallelHashJoinState *pstate = hashtable->parallel_state;
1059  int i;
1060 
1062 
1063  /*
1064  * It's unlikely, but we need to be prepared for new participants to show
1065  * up while we're in the middle of this operation so we need to switch on
1066  * barrier phase here.
1067  */
1069  {
1071 
1072  /*
1073  * Elect one participant to prepare to grow the number of batches.
1074  * This involves reallocating or resetting the buckets of batch 0
1075  * in preparation for all participants to begin repartitioning the
1076  * tuples.
1077  */
1080  {
1081  dsa_pointer_atomic *buckets;
1082  ParallelHashJoinBatch *old_batch0;
1083  int new_nbatch;
1084  int i;
1085 
1086  /* Move the old batch out of the way. */
1087  old_batch0 = hashtable->batches[0].shared;
1088  pstate->old_batches = pstate->batches;
1089  pstate->old_nbatch = hashtable->nbatch;
1090  pstate->batches = InvalidDsaPointer;
1091 
1092  /* Free this backend's old accessors. */
1094 
1095  /* Figure out how many batches to use. */
1096  if (hashtable->nbatch == 1)
1097  {
1098  /*
1099  * We are going from single-batch to multi-batch. We need
1100  * to switch from one large combined memory budget to the
1101  * regular work_mem budget.
1102  */
1103  pstate->space_allowed = work_mem * 1024L;
1104 
1105  /*
1106  * The combined work_mem of all participants wasn't
1107  * enough. Therefore one batch per participant would be
1108  * approximately equivalent and would probably also be
1109  * insufficient. So try two batches per participant,
1110  * rounded up to a power of two.
1111  */
1112  new_nbatch = 1 << my_log2(pstate->nparticipants * 2);
1113  }
1114  else
1115  {
1116  /*
1117  * We were already multi-batched. Try doubling the number
1118  * of batches.
1119  */
1120  new_nbatch = hashtable->nbatch * 2;
1121  }
1122 
1123  /* Allocate new larger generation of batches. */
1124  Assert(hashtable->nbatch == pstate->nbatch);
1125  ExecParallelHashJoinSetUpBatches(hashtable, new_nbatch);
1126  Assert(hashtable->nbatch == pstate->nbatch);
1127 
1128  /* Replace or recycle batch 0's bucket array. */
1129  if (pstate->old_nbatch == 1)
1130  {
1131  double dtuples;
1132  double dbuckets;
1133  int new_nbuckets;
1134 
1135  /*
1136  * We probably also need a smaller bucket array. How many
1137  * tuples do we expect per batch, assuming we have only
1138  * half of them so far? Normally we don't need to change
1139  * the bucket array's size, because the size of each batch
1140  * stays the same as we add more batches, but in this
1141  * special case we move from a large batch to many smaller
1142  * batches and it would be wasteful to keep the large
1143  * array.
1144  */
1145  dtuples = (old_batch0->ntuples * 2.0) / new_nbatch;
1146  dbuckets = ceil(dtuples / NTUP_PER_BUCKET);
1147  dbuckets = Min(dbuckets,
1148  MaxAllocSize / sizeof(dsa_pointer_atomic));
1149  new_nbuckets = (int) dbuckets;
1150  new_nbuckets = Max(new_nbuckets, 1024);
1151  new_nbuckets = 1 << my_log2(new_nbuckets);
1152  dsa_free(hashtable->area, old_batch0->buckets);
1153  hashtable->batches[0].shared->buckets =
1154  dsa_allocate(hashtable->area,
1155  sizeof(dsa_pointer_atomic) * new_nbuckets);
1156  buckets = (dsa_pointer_atomic *)
1157  dsa_get_address(hashtable->area,
1158  hashtable->batches[0].shared->buckets);
1159  for (i = 0; i < new_nbuckets; ++i)
1161  pstate->nbuckets = new_nbuckets;
1162  }
1163  else
1164  {
1165  /* Recycle the existing bucket array. */
1166  hashtable->batches[0].shared->buckets = old_batch0->buckets;
1167  buckets = (dsa_pointer_atomic *)
1168  dsa_get_address(hashtable->area, old_batch0->buckets);
1169  for (i = 0; i < hashtable->nbuckets; ++i)
1171  }
1172 
1173  /* Move all chunks to the work queue for parallel processing. */
1174  pstate->chunk_work_queue = old_batch0->chunks;
1175 
1176  /* Disable further growth temporarily while we're growing. */
1177  pstate->growth = PHJ_GROWTH_DISABLED;
1178  }
1179  else
1180  {
1181  /* All other participants just flush their tuples to disk. */
1183  }
1184  /* Fall through. */
1185 
1187  /* Wait for the above to be finished. */
1190  /* Fall through. */
1191 
1193  /* Make sure that we have the current dimensions and buckets. */
1196  /* Then partition, flush counters. */
1199  ExecParallelHashMergeCounters(hashtable);
1200  /* Wait for the above to be finished. */
1203  /* Fall through. */
1204 
1206 
1207  /*
1208  * Elect one participant to clean up and decide whether further
1209  * repartitioning is needed, or should be disabled because it's
1210  * not helping.
1211  */
1214  {
1215  bool space_exhausted = false;
1216  bool extreme_skew_detected = false;
1217 
1218  /* Make sure that we have the current dimensions and buckets. */
1221 
1222  /* Are any of the new generation of batches exhausted? */
1223  for (i = 0; i < hashtable->nbatch; ++i)
1224  {
1225  ParallelHashJoinBatch *batch = hashtable->batches[i].shared;
1226 
1227  if (batch->space_exhausted ||
1228  batch->estimated_size > pstate->space_allowed)
1229  {
1230  int parent;
1231 
1232  space_exhausted = true;
1233 
1234  /*
1235  * Did this batch receive ALL of the tuples from its
1236  * parent batch? That would indicate that further
1237  * repartitioning isn't going to help (the hash values
1238  * are probably all the same).
1239  */
1240  parent = i % pstate->old_nbatch;
1241  if (batch->ntuples == hashtable->batches[parent].shared->old_ntuples)
1242  extreme_skew_detected = true;
1243  }
1244  }
1245 
1246  /* Don't keep growing if it's not helping or we'd overflow. */
1247  if (extreme_skew_detected || hashtable->nbatch >= INT_MAX / 2)
1248  pstate->growth = PHJ_GROWTH_DISABLED;
1249  else if (space_exhausted)
1251  else
1252  pstate->growth = PHJ_GROWTH_OK;
1253 
1254  /* Free the old batches in shared memory. */
1255  dsa_free(hashtable->area, pstate->old_batches);
1256  pstate->old_batches = InvalidDsaPointer;
1257  }
1258  /* Fall through. */
1259 
1261  /* Wait for the above to complete. */
1264  }
1265 }
static void ExecParallelHashRepartitionRest(HashJoinTable hashtable)
Definition: nodeHash.c:1340
static void ExecParallelHashCloseBatchAccessors(HashJoinTable hashtable)
Definition: nodeHash.c:3011
dsa_pointer chunk_work_queue
Definition: hashjoin.h:242
#define PHJ_GROW_BATCHES_DECIDING
Definition: hashjoin.h:274
#define InvalidDsaPointer
Definition: dsa.h:78
void ExecParallelHashTableSetCurrentBatch(HashJoinTable hashtable, int batchno)
Definition: nodeHash.c:3257
dsa_pointer chunks
Definition: hashjoin.h:156
#define Min(x, y)
Definition: c.h:920
static void ExecParallelHashMergeCounters(HashJoinTable hashtable)
Definition: nodeHash.c:1400
static void ExecParallelHashEnsureBatchAccessors(HashJoinTable hashtable)
Definition: nodeHash.c:3032
static void ExecParallelHashRepartitionFirst(HashJoinTable hashtable)
Definition: nodeHash.c:1273
dsa_area * area
Definition: hashjoin.h:356
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
dsa_pointer batches
Definition: hashjoin.h:236
#define PHJ_GROW_BATCHES_PHASE(n)
Definition: hashjoin.h:276
int my_log2(long num)
Definition: dynahash.c:1720
#define PHJ_GROW_BATCHES_ALLOCATING
Definition: hashjoin.h:272
#define MaxAllocSize
Definition: memutils.h:40
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
#define NTUP_PER_BUCKET
Definition: nodeHash.c:664
#define PHJ_GROW_BATCHES_REPARTITIONING
Definition: hashjoin.h:273
int work_mem
Definition: globals.c:121
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
#define Max(x, y)
Definition: c.h:914
#define dsa_pointer_atomic_write
Definition: dsa.h:66
#define Assert(condition)
Definition: c.h:738
ParallelHashGrowth growth
Definition: hashjoin.h:241
dsa_pointer old_batches
Definition: hashjoin.h:237
int BarrierPhase(Barrier *barrier)
Definition: barrier.c:243
ParallelHashJoinBatch * shared
Definition: hashjoin.h:197
bool BarrierArriveAndWait(Barrier *barrier, uint32 wait_event_info)
Definition: barrier.c:125
#define dsa_pointer_atomic_init
Definition: dsa.h:64
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
#define PHJ_BUILD_HASHING_INNER
Definition: hashjoin.h:259
static void ExecParallelHashJoinSetUpBatches(HashJoinTable hashtable, int nbatch)
Definition: nodeHash.c:2934
int i
#define PHJ_GROW_BATCHES_FINISHING
Definition: hashjoin.h:275
Barrier grow_batches_barrier
Definition: hashjoin.h:249
#define PHJ_GROW_BATCHES_ELECTING
Definition: hashjoin.h:271
#define dsa_allocate(area, size)
Definition: dsa.h:84
dsa_pointer buckets
Definition: hashjoin.h:153

◆ ExecParallelHashIncreaseNumBuckets()

static void ExecParallelHashIncreaseNumBuckets ( HashJoinTable  hashtable)
static

Definition at line 1493 of file nodeHash.c.

References HashJoinTableData::area, Assert, BarrierArriveAndWait(), BarrierPhase(), HashJoinTableData::batches, ParallelHashJoinBatch::buckets, HashJoinTableData::buckets, ParallelHashJoinState::build_barrier, CHECK_FOR_INTERRUPTS, ParallelHashJoinState::chunk_work_queue, ParallelHashJoinBatch::chunks, dsa_allocate, dsa_free(), dsa_get_address(), dsa_pointer_atomic_init, ExecHashGetBucketAndBatch(), ExecParallelHashEnsureBatchAccessors(), ExecParallelHashPopChunkQueue(), ExecParallelHashPushTuple(), ExecParallelHashTableSetCurrentBatch(), ParallelHashJoinState::grow_buckets_barrier, ParallelHashJoinState::growth, HASH_CHUNK_DATA, HASH_CHUNK_HEADER_SIZE, HashJoinTupleData::hashvalue, HJTUPLE_MINTUPLE, HJTUPLE_OVERHEAD, i, idx(), InvalidDsaPointer, MAXALIGN, ParallelHashJoinState::nbuckets, HashJoinTableData::parallel_state, PHJ_BUILD_HASHING_INNER, PHJ_GROW_BUCKETS_ALLOCATING, PHJ_GROW_BUCKETS_ELECTING, PHJ_GROW_BUCKETS_PHASE, PHJ_GROW_BUCKETS_REINSERTING, PHJ_GROWTH_OK, ParallelHashJoinBatchAccessor::shared, HashJoinTableData::shared, ParallelHashJoinBatch::size, WAIT_EVENT_HASH_GROW_BUCKETS_ALLOCATE, WAIT_EVENT_HASH_GROW_BUCKETS_ELECT, and WAIT_EVENT_HASH_GROW_BUCKETS_REINSERT.

Referenced by ExecParallelHashTupleAlloc(), ExecParallelHashTuplePrealloc(), and MultiExecParallelHash().

1494 {
1495  ParallelHashJoinState *pstate = hashtable->parallel_state;
1496  int i;
1497  HashMemoryChunk chunk;
1498  dsa_pointer chunk_s;
1499 
1501 
1502  /*
1503  * It's unlikely, but we need to be prepared for new participants to show
1504  * up while we're in the middle of this operation so we need to switch on
1505  * barrier phase here.
1506  */
1508  {
1510  /* Elect one participant to prepare to increase nbuckets. */
1513  {
1514  size_t size;
1515  dsa_pointer_atomic *buckets;
1516 
1517  /* Double the size of the bucket array. */
1518  pstate->nbuckets *= 2;
1519  size = pstate->nbuckets * sizeof(dsa_pointer_atomic);
1520  hashtable->batches[0].shared->size += size / 2;
1521  dsa_free(hashtable->area, hashtable->batches[0].shared->buckets);
1522  hashtable->batches[0].shared->buckets =
1523  dsa_allocate(hashtable->area, size);
1524  buckets = (dsa_pointer_atomic *)
1525  dsa_get_address(hashtable->area,
1526  hashtable->batches[0].shared->buckets);
1527  for (i = 0; i < pstate->nbuckets; ++i)
1529 
1530  /* Put the chunk list onto the work queue. */
1531  pstate->chunk_work_queue = hashtable->batches[0].shared->chunks;
1532 
1533  /* Clear the flag. */
1534  pstate->growth = PHJ_GROWTH_OK;
1535  }
1536  /* Fall through. */
1537 
1539  /* Wait for the above to complete. */
1542  /* Fall through. */
1543 
1545  /* Reinsert all tuples into the hash table. */
1548  while ((chunk = ExecParallelHashPopChunkQueue(hashtable, &chunk_s)))
1549  {
1550  size_t idx = 0;
1551 
1552  while (idx < chunk->used)
1553  {
1554  HashJoinTuple hashTuple = (HashJoinTuple) (HASH_CHUNK_DATA(chunk) + idx);
1555  dsa_pointer shared = chunk_s + HASH_CHUNK_HEADER_SIZE + idx;
1556  int bucketno;
1557  int batchno;
1558 
1559  ExecHashGetBucketAndBatch(hashtable, hashTuple->hashvalue,
1560  &bucketno, &batchno);
1561  Assert(batchno == 0);
1562 
1563  /* add the tuple to the proper bucket */
1564  ExecParallelHashPushTuple(&hashtable->buckets.shared[bucketno],
1565  hashTuple, shared);
1566 
1567  /* advance index past the tuple */
1568  idx += MAXALIGN(HJTUPLE_OVERHEAD +
1569  HJTUPLE_MINTUPLE(hashTuple)->t_len);
1570  }
1571 
1572  /* allow this loop to be cancellable */
1574  }
1577  }
1578 }
dsa_pointer_atomic * shared
Definition: hashjoin.h:299
dsa_pointer chunk_work_queue
Definition: hashjoin.h:242
#define InvalidDsaPointer
Definition: dsa.h:78
void ExecParallelHashTableSetCurrentBatch(HashJoinTable hashtable, int batchno)
Definition: nodeHash.c:3257
dsa_pointer chunks
Definition: hashjoin.h:156
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
static void ExecParallelHashEnsureBatchAccessors(HashJoinTable hashtable)
Definition: nodeHash.c:3032
uint64 dsa_pointer
Definition: dsa.h:62
#define PHJ_GROW_BUCKETS_REINSERTING
Definition: hashjoin.h:281
dsa_area * area
Definition: hashjoin.h:356
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1898
#define PHJ_GROW_BUCKETS_ALLOCATING
Definition: hashjoin.h:280
Barrier grow_buckets_barrier
Definition: hashjoin.h:250
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1937
static HashMemoryChunk ExecParallelHashPopChunkQueue(HashJoinTable table, dsa_pointer *shared)
Definition: nodeHash.c:3278
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
union HashJoinTableData::@95 buckets
#define PHJ_GROW_BUCKETS_ELECTING
Definition: hashjoin.h:279
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:79
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
#define HASH_CHUNK_HEADER_SIZE
Definition: hashjoin.h:140
#define Assert(condition)
Definition: c.h:738
ParallelHashGrowth growth
Definition: hashjoin.h:241
#define PHJ_GROW_BUCKETS_PHASE(n)
Definition: hashjoin.h:282
pg_atomic_uint64 dsa_pointer_atomic
Definition: dsa.h:63
int BarrierPhase(Barrier *barrier)
Definition: barrier.c:243
#define MAXALIGN(LEN)
Definition: c.h:691
ParallelHashJoinBatch * shared
Definition: hashjoin.h:197
bool BarrierArriveAndWait(Barrier *barrier, uint32 wait_event_info)
Definition: barrier.c:125
#define dsa_pointer_atomic_init
Definition: dsa.h:64
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
static void ExecParallelHashPushTuple(dsa_pointer_atomic *head, HashJoinTuple tuple, dsa_pointer tuple_shared)
Definition: nodeHash.c:3239
#define PHJ_BUILD_HASHING_INNER
Definition: hashjoin.h:259
int i
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define HASH_CHUNK_DATA(hc)
Definition: hashjoin.h:141
uint32 hashvalue
Definition: hashjoin.h:75
#define dsa_allocate(area, size)
Definition: dsa.h:84
dsa_pointer buckets
Definition: hashjoin.h:153

◆ ExecParallelHashJoinSetUpBatches()

static void ExecParallelHashJoinSetUpBatches ( HashJoinTable  hashtable,
int  nbatch 
)
static

Definition at line 2934 of file nodeHash.c.

References HashJoinTableData::area, Assert, BarrierArriveAndWait(), BarrierAttach(), BarrierDetach(), BarrierInit(), BarrierPhase(), ParallelHashJoinBatch::batch_barrier, ParallelHashJoinState::batches, HashJoinTableData::batches, dsa_allocate0, dsa_get_address(), EstimateParallelHashJoinBatch, ParallelHashJoinState::fileset, HashJoinTableData::hashCxt, i, ParallelHashJoinBatchAccessor::inner_tuples, MAXPGPATH, MemoryContextSwitchTo(), name, ParallelHashJoinState::nbatch, HashJoinTableData::nbatch, ParallelHashJoinState::nparticipants, NthParallelHashJoinBatch, ParallelHashJoinBatchAccessor::outer_tuples, palloc0(), HashJoinTableData::parallel_state, ParallelHashJoinBatchInner, ParallelHashJoinBatchOuter, ParallelWorkerNumber, PHJ_BATCH_PROBING, ParallelHashJoinBatchAccessor::shared, SHARED_TUPLESTORE_SINGLE_PASS, snprintf, and sts_initialize().

Referenced by ExecHashTableCreate(), and ExecParallelHashIncreaseNumBatches().

2935 {
2936  ParallelHashJoinState *pstate = hashtable->parallel_state;
2937  ParallelHashJoinBatch *batches;
2938  MemoryContext oldcxt;
2939  int i;
2940 
2941  Assert(hashtable->batches == NULL);
2942 
2943  /* Allocate space. */
2944  pstate->batches =
2945  dsa_allocate0(hashtable->area,
2946  EstimateParallelHashJoinBatch(hashtable) * nbatch);
2947  pstate->nbatch = nbatch;
2948  batches = dsa_get_address(hashtable->area, pstate->batches);
2949 
2950  /* Use hash join memory context. */
2951  oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
2952 
2953  /* Allocate this backend's accessor array. */
2954  hashtable->nbatch = nbatch;
2955  hashtable->batches = (ParallelHashJoinBatchAccessor *)
2956  palloc0(sizeof(ParallelHashJoinBatchAccessor) * hashtable->nbatch);
2957 
2958  /* Set up the shared state, tuplestores and backend-local accessors. */
2959  for (i = 0; i < hashtable->nbatch; ++i)
2960  {
2961  ParallelHashJoinBatchAccessor *accessor = &hashtable->batches[i];
2962  ParallelHashJoinBatch *shared = NthParallelHashJoinBatch(batches, i);
2963  char name[MAXPGPATH];
2964 
2965  /*
2966  * All members of shared were zero-initialized. We just need to set
2967  * up the Barrier.
2968  */
2969  BarrierInit(&shared->batch_barrier, 0);
2970  if (i == 0)
2971  {
2972  /* Batch 0 doesn't need to be loaded. */
2973  BarrierAttach(&shared->batch_barrier);
2974  while (BarrierPhase(&shared->batch_barrier) < PHJ_BATCH_PROBING)
2975  BarrierArriveAndWait(&shared->batch_barrier, 0);
2976  BarrierDetach(&shared->batch_barrier);
2977  }
2978 
2979  /* Initialize accessor state. All members were zero-initialized. */
2980  accessor->shared = shared;
2981 
2982  /* Initialize the shared tuplestores. */
2983  snprintf(name, sizeof(name), "i%dof%d", i, hashtable->nbatch);
2984  accessor->inner_tuples =
2986  pstate->nparticipants,
2988  sizeof(uint32),
2990  &pstate->fileset,
2991  name);
2992  snprintf(name, sizeof(name), "o%dof%d", i, hashtable->nbatch);
2993  accessor->outer_tuples =
2995  pstate->nparticipants),
2996  pstate->nparticipants,
2998  sizeof(uint32),
3000  &pstate->fileset,
3001  name);
3002  }
3003 
3004  MemoryContextSwitchTo(oldcxt);
3005 }
SharedTuplestoreAccessor * outer_tuples
Definition: hashjoin.h:209
SharedTuplestoreAccessor * sts_initialize(SharedTuplestore *sts, int participants, int my_participant_number, size_t meta_data_size, int flags, SharedFileSet *fileset, const char *name)
void BarrierInit(Barrier *barrier, int participants)
Definition: barrier.c:100
#define ParallelHashJoinBatchOuter(batch, nparticipants)
Definition: hashjoin.h:175
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SharedFileSet fileset
Definition: hashjoin.h:253
SharedTuplestoreAccessor * inner_tuples
Definition: hashjoin.h:208
dsa_area * area
Definition: hashjoin.h:356
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
dsa_pointer batches
Definition: hashjoin.h:236
#define ParallelHashJoinBatchInner(batch)
Definition: hashjoin.h:170
#define MAXPGPATH
#define dsa_allocate0(area, size)
Definition: dsa.h:88
int ParallelWorkerNumber
Definition: parallel.c:112
unsigned int uint32
Definition: c.h:367
#define SHARED_TUPLESTORE_SINGLE_PASS
int BarrierAttach(Barrier *barrier)
Definition: barrier.c:214
void * palloc0(Size size)
Definition: mcxt.c:980
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
#define PHJ_BATCH_PROBING
Definition: hashjoin.h:267
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
#define Assert(condition)
Definition: c.h:738
#define NthParallelHashJoinBatch(base, n)
Definition: hashjoin.h:186
bool BarrierDetach(Barrier *barrier)
Definition: barrier.c:234
int BarrierPhase(Barrier *barrier)
Definition: barrier.c:243
const char * name
Definition: encode.c:555
ParallelHashJoinBatch * shared
Definition: hashjoin.h:197
bool BarrierArriveAndWait(Barrier *barrier, uint32 wait_event_info)
Definition: barrier.c:125
#define EstimateParallelHashJoinBatch(hashtable)
Definition: hashjoin.h:181
int i
MemoryContext hashCxt
Definition: hashjoin.h:348
#define snprintf
Definition: port.h:193

◆ ExecParallelHashMergeCounters()

static void ExecParallelHashMergeCounters ( HashJoinTable  hashtable)
static

Definition at line 1400 of file nodeHash.c.

References HashJoinTableData::batches, ParallelHashJoinBatch::estimated_size, ParallelHashJoinBatchAccessor::estimated_size, i, ParallelHashJoinState::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), HashJoinTableData::nbatch, ParallelHashJoinBatch::ntuples, ParallelHashJoinBatchAccessor::ntuples, ParallelHashJoinBatch::old_ntuples, ParallelHashJoinBatchAccessor::old_ntuples, HashJoinTableData::parallel_state, ParallelHashJoinBatchAccessor::shared, ParallelHashJoinBatch::size, ParallelHashJoinBatchAccessor::size, and ParallelHashJoinState::total_tuples.

Referenced by ExecParallelHashIncreaseNumBatches(), and MultiExecParallelHash().

1401 {
1402  ParallelHashJoinState *pstate = hashtable->parallel_state;
1403  int i;
1404 
1405  LWLockAcquire(&pstate->lock, LW_EXCLUSIVE);
1406  pstate->total_tuples = 0;
1407  for (i = 0; i < hashtable->nbatch; ++i)
1408  {
1409  ParallelHashJoinBatchAccessor *batch = &hashtable->batches[i];
1410 
1411  batch->shared->size += batch->size;
1412  batch->shared->estimated_size += batch->estimated_size;
1413  batch->shared->ntuples += batch->ntuples;
1414  batch->shared->old_ntuples += batch->old_ntuples;
1415  batch->size = 0;
1416  batch->estimated_size = 0;
1417  batch->ntuples = 0;
1418  batch->old_ntuples = 0;
1419  pstate->total_tuples += batch->shared->ntuples;
1420  }
1421  LWLockRelease(&pstate->lock);
1422 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
ParallelHashJoinBatch * shared
Definition: hashjoin.h:197
int i

◆ ExecParallelHashNextTuple()

static HashJoinTuple ExecParallelHashNextTuple ( HashJoinTable  table,
HashJoinTuple  tuple 
)
inlinestatic

Definition at line 3225 of file nodeHash.c.

References HashJoinTableData::area, Assert, dsa_get_address(), HashJoinTupleData::next, next, HashJoinTableData::parallel_state, and HashJoinTupleData::shared.

Referenced by ExecParallelScanHashBucket().

3226 {
3228 
3229  Assert(hashtable->parallel_state);
3230  next = (HashJoinTuple) dsa_get_address(hashtable->area, tuple->next.shared);
3231 
3232  return next;
3233 }
static int32 next
Definition: blutils.c:218
dsa_pointer shared
Definition: hashjoin.h:73
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1937
union HashJoinTupleData::@93 next
#define Assert(condition)
Definition: c.h:738

◆ ExecParallelHashPopChunkQueue()

static HashMemoryChunk ExecParallelHashPopChunkQueue ( HashJoinTable  table,
dsa_pointer shared 
)
static

Definition at line 3278 of file nodeHash.c.

References HashJoinTableData::area, ParallelHashJoinState::chunk_work_queue, dsa_get_address(), DsaPointerIsValid, ParallelHashJoinState::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), HashMemoryChunkData::next, HashJoinTableData::parallel_state, and HashMemoryChunkData::shared.

Referenced by ExecParallelHashIncreaseNumBuckets(), and ExecParallelHashRepartitionFirst().

3279 {
3280  ParallelHashJoinState *pstate = hashtable->parallel_state;
3281  HashMemoryChunk chunk;
3282 
3283  LWLockAcquire(&pstate->lock, LW_EXCLUSIVE);
3284  if (DsaPointerIsValid(pstate->chunk_work_queue))
3285  {
3286  *shared = pstate->chunk_work_queue;
3287  chunk = (HashMemoryChunk)
3288  dsa_get_address(hashtable->area, *shared);
3289  pstate->chunk_work_queue = chunk->next.shared;
3290  }
3291  else
3292  chunk = NULL;
3293  LWLockRelease(&pstate->lock);
3294 
3295  return chunk;
3296 }
dsa_pointer chunk_work_queue
Definition: hashjoin.h:242
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
dsa_pointer shared
Definition: hashjoin.h:127
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
struct HashMemoryChunkData * HashMemoryChunk
Definition: hashjoin.h:137
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
#define DsaPointerIsValid(x)
Definition: dsa.h:81
union HashMemoryChunkData::@94 next

◆ ExecParallelHashPushTuple()

static void ExecParallelHashPushTuple ( dsa_pointer_atomic head,
HashJoinTuple  tuple,
dsa_pointer  tuple_shared 
)
inlinestatic

Definition at line 3239 of file nodeHash.c.

References dsa_pointer_atomic_compare_exchange, dsa_pointer_atomic_read, HashJoinTupleData::next, and HashJoinTupleData::shared.

Referenced by ExecParallelHashIncreaseNumBuckets(), ExecParallelHashRepartitionFirst(), ExecParallelHashTableInsert(), and ExecParallelHashTableInsertCurrentBatch().

3242 {
3243  for (;;)
3244  {
3245  tuple->next.shared = dsa_pointer_atomic_read(head);
3247  &tuple->next.shared,
3248  tuple_shared))
3249  break;
3250  }
3251 }
dsa_pointer shared
Definition: hashjoin.h:73
#define dsa_pointer_atomic_compare_exchange
Definition: dsa.h:68
union HashJoinTupleData::@93 next
#define dsa_pointer_atomic_read
Definition: dsa.h:65

◆ ExecParallelHashRepartitionFirst()

static void ExecParallelHashRepartitionFirst ( HashJoinTable  hashtable)
static

Definition at line 1273 of file nodeHash.c.

References HashJoinTableData::area, Assert, HashJoinTableData::batches, HashJoinTableData::buckets, CHECK_FOR_INTERRUPTS, dsa_free(), ParallelHashJoinBatchAccessor::estimated_size, ExecHashGetBucketAndBatch(), ExecParallelHashPopChunkQueue(), ExecParallelHashPushTuple(), ExecParallelHashTupleAlloc(), HASH_CHUNK_DATA, HashJoinTupleData::hashvalue, HJTUPLE_MINTUPLE, HJTUPLE_OVERHEAD, idx(), ParallelHashJoinBatchAccessor::inner_tuples, MAXALIGN, ParallelHashJoinState::nbatch, HashJoinTableData::nbatch, ParallelHashJoinBatchAccessor::ntuples, ParallelHashJoinBatchAccessor::old_ntuples, HashJoinTableData::parallel_state, HashJoinTableData::shared, sts_puttuple(), and MinimalTupleData::t_len.

Referenced by ExecParallelHashIncreaseNumBatches().

1274 {
1275  dsa_pointer chunk_shared;
1276  HashMemoryChunk chunk;
1277 
1278  Assert(hashtable->nbatch == hashtable->parallel_state->nbatch);
1279 
1280  while ((chunk = ExecParallelHashPopChunkQueue(hashtable, &chunk_shared)))
1281  {
1282  size_t idx = 0;
1283 
1284  /* Repartition all tuples in this chunk. */
1285  while (idx < chunk->used)
1286  {
1287  HashJoinTuple hashTuple = (HashJoinTuple) (HASH_CHUNK_DATA(chunk) + idx);
1288  MinimalTuple tuple = HJTUPLE_MINTUPLE(hashTuple);
1289  HashJoinTuple copyTuple;
1290  dsa_pointer shared;
1291  int bucketno;
1292  int batchno;
1293 
1294  ExecHashGetBucketAndBatch(hashtable, hashTuple->hashvalue,
1295  &bucketno, &batchno);
1296 
1297  Assert(batchno < hashtable->nbatch);
1298  if (batchno == 0)
1299  {
1300  /* It still belongs in batch 0. Copy to a new chunk. */
1301  copyTuple =
1302  ExecParallelHashTupleAlloc(hashtable,
1303  HJTUPLE_OVERHEAD + tuple->t_len,
1304  &shared);
1305  copyTuple->hashvalue = hashTuple->hashvalue;
1306  memcpy(HJTUPLE_MINTUPLE(copyTuple), tuple, tuple->t_len);
1307  ExecParallelHashPushTuple(&hashtable->buckets.shared[bucketno],
1308  copyTuple, shared);
1309  }
1310  else
1311  {
1312  size_t tuple_size =
1313  MAXALIGN(HJTUPLE_OVERHEAD + tuple->t_len);
1314 
1315  /* It belongs in a later batch. */
1316  hashtable->batches[batchno].estimated_size += tuple_size;
1317  sts_puttuple(hashtable->batches[batchno].inner_tuples,
1318  &hashTuple->hashvalue, tuple);
1319  }
1320 
1321  /* Count this tuple. */
1322  ++hashtable->batches[0].old_ntuples;
1323  ++hashtable->batches[batchno].ntuples;
1324 
1325  idx += MAXALIGN(HJTUPLE_OVERHEAD +
1326  HJTUPLE_MINTUPLE(hashTuple)->t_len);
1327  }
1328 
1329  /* Free this chunk. */
1330  dsa_free(hashtable->area, chunk_shared);
1331 
1333  }
1334 }
dsa_pointer_atomic * shared
Definition: hashjoin.h:299
void sts_puttuple(SharedTuplestoreAccessor *accessor, void *meta_data, MinimalTuple tuple)
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
uint64 dsa_pointer
Definition: dsa.h:62
SharedTuplestoreAccessor * inner_tuples
Definition: hashjoin.h:208
dsa_area * area
Definition: hashjoin.h:356
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1898
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1937
static HashMemoryChunk ExecParallelHashPopChunkQueue(HashJoinTable table, dsa_pointer *shared)
Definition: nodeHash.c:3278
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
union HashJoinTableData::@95 buckets
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:79
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
#define Assert(condition)
Definition: c.h:738
#define MAXALIGN(LEN)
Definition: c.h:691
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
static void ExecParallelHashPushTuple(dsa_pointer_atomic *head, HashJoinTuple tuple, dsa_pointer tuple_shared)
Definition: nodeHash.c:3239
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define HASH_CHUNK_DATA(hc)
Definition: hashjoin.h:141
static HashJoinTuple ExecParallelHashTupleAlloc(HashJoinTable hashtable, size_t size, dsa_pointer *shared)
Definition: nodeHash.c:2786
uint32 hashvalue
Definition: hashjoin.h:75

◆ ExecParallelHashRepartitionRest()

static void ExecParallelHashRepartitionRest ( HashJoinTable  hashtable)
static

Definition at line 1340 of file nodeHash.c.

References HashJoinTableData::area, HashJoinTableData::batches, CHECK_FOR_INTERRUPTS, dsa_get_address(), ParallelHashJoinBatchAccessor::estimated_size, ExecHashGetBucketAndBatch(), ParallelHashJoinState::fileset, HJTUPLE_OVERHEAD, i, ParallelHashJoinBatchAccessor::inner_tuples, MAXALIGN, NthParallelHashJoinBatch, ParallelHashJoinBatchAccessor::ntuples, ParallelHashJoinState::old_batches, ParallelHashJoinState::old_nbatch, ParallelHashJoinBatchAccessor::old_ntuples, palloc0(), HashJoinTableData::parallel_state, ParallelHashJoinBatchInner, ParallelWorkerNumber, pfree(), sts_attach(), sts_begin_parallel_scan(), sts_end_parallel_scan(), sts_parallel_scan_next(), sts_puttuple(), and MinimalTupleData::t_len.

Referenced by ExecParallelHashIncreaseNumBatches().

1341 {
1342  ParallelHashJoinState *pstate = hashtable->parallel_state;
1343  int old_nbatch = pstate->old_nbatch;
1344  SharedTuplestoreAccessor **old_inner_tuples;
1345  ParallelHashJoinBatch *old_batches;
1346  int i;
1347 
1348  /* Get our hands on the previous generation of batches. */
1349  old_batches = (ParallelHashJoinBatch *)
1350  dsa_get_address(hashtable->area, pstate->old_batches);
1351  old_inner_tuples = palloc0(sizeof(SharedTuplestoreAccessor *) * old_nbatch);
1352  for (i = 1; i < old_nbatch; ++i)
1353  {
1354  ParallelHashJoinBatch *shared =
1355  NthParallelHashJoinBatch(old_batches, i);
1356 
1357  old_inner_tuples[i] = sts_attach(ParallelHashJoinBatchInner(shared),
1359  &pstate->fileset);
1360  }
1361 
1362  /* Join in the effort to repartition them. */
1363  for (i = 1; i < old_nbatch; ++i)
1364  {
1365  MinimalTuple tuple;
1366  uint32 hashvalue;
1367 
1368  /* Scan one partition from the previous generation. */
1369  sts_begin_parallel_scan(old_inner_tuples[i]);
1370  while ((tuple = sts_parallel_scan_next(old_inner_tuples[i], &hashvalue)))
1371  {
1372  size_t tuple_size = MAXALIGN(HJTUPLE_OVERHEAD + tuple->t_len);
1373  int bucketno;
1374  int batchno;
1375 
1376  /* Decide which partition it goes to in the new generation. */
1377  ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno,
1378  &batchno);
1379 
1380  hashtable->batches[batchno].estimated_size += tuple_size;
1381  ++hashtable->batches[batchno].ntuples;
1382  ++hashtable->batches[i].old_ntuples;
1383 
1384  /* Store the tuple its new batch. */
1385  sts_puttuple(hashtable->batches[batchno].inner_tuples,
1386  &hashvalue, tuple);
1387 
1389  }
1390  sts_end_parallel_scan(old_inner_tuples[i]);
1391  }
1392 
1393  pfree(old_inner_tuples);
1394 }
void sts_puttuple(SharedTuplestoreAccessor *accessor, void *meta_data, MinimalTuple tuple)
SharedFileSet fileset
Definition: hashjoin.h:253
SharedTuplestoreAccessor * inner_tuples
Definition: hashjoin.h:208
dsa_area * area
Definition: hashjoin.h:356
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1898
void pfree(void *pointer)
Definition: mcxt.c:1056
void sts_end_parallel_scan(SharedTuplestoreAccessor *accessor)
#define ParallelHashJoinBatchInner(batch)
Definition: hashjoin.h:170
SharedTuplestoreAccessor * sts_attach(SharedTuplestore *sts, int my_participant_number, SharedFileSet *fileset)
int ParallelWorkerNumber
Definition: parallel.c:112
unsigned int uint32
Definition: c.h:367
void sts_begin_parallel_scan(SharedTuplestoreAccessor *accessor)
void * palloc0(Size size)
Definition: mcxt.c:980
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:79
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
dsa_pointer old_batches
Definition: hashjoin.h:237
#define NthParallelHashJoinBatch(base, n)
Definition: hashjoin.h:186
#define MAXALIGN(LEN)
Definition: c.h:691
int i
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
MinimalTuple sts_parallel_scan_next(SharedTuplestoreAccessor *accessor, void *meta_data)

◆ ExecParallelHashTableAlloc()

void ExecParallelHashTableAlloc ( HashJoinTable  hashtable,
int  batchno 
)

Definition at line 3095 of file nodeHash.c.

References HashJoinTableData::area, HashJoinTableData::batches, ParallelHashJoinBatch::buckets, dsa_allocate, dsa_get_address(), dsa_pointer_atomic_init, i, InvalidDsaPointer, ParallelHashJoinState::nbuckets, HashJoinTableData::parallel_state, and ParallelHashJoinBatchAccessor::shared.

Referenced by ExecHashTableCreate(), and ExecParallelHashJoinNewBatch().

3096 {
3097  ParallelHashJoinBatch *batch = hashtable->batches[batchno].shared;
3098  dsa_pointer_atomic *buckets;
3099  int nbuckets = hashtable->parallel_state->nbuckets;
3100  int i;
3101 
3102  batch->buckets =
3103  dsa_allocate(hashtable->area, sizeof(dsa_pointer_atomic) * nbuckets);
3104  buckets = (dsa_pointer_atomic *)
3105  dsa_get_address(hashtable->area, batch->buckets);
3106  for (i = 0; i < nbuckets; ++i)
3108 }
#define InvalidDsaPointer
Definition: dsa.h:78
dsa_area * area
Definition: hashjoin.h:356
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
ParallelHashJoinBatch * shared
Definition: hashjoin.h:197
#define dsa_pointer_atomic_init
Definition: dsa.h:64
int i
#define dsa_allocate(area, size)
Definition: dsa.h:84
dsa_pointer buckets
Definition: hashjoin.h:153

◆ ExecParallelHashTableInsert()

void ExecParallelHashTableInsert ( HashJoinTable  hashtable,
TupleTableSlot slot,
uint32  hashvalue 
)

Definition at line 1681 of file nodeHash.c.

References Assert, BarrierPhase(), HashJoinTableData::batches, HashJoinTableData::buckets, ParallelHashJoinState::build_barrier, ExecFetchSlotMinimalTuple(), ExecHashGetBucketAndBatch(), ExecParallelHashPushTuple(), ExecParallelHashTupleAlloc(), ExecParallelHashTuplePrealloc(), HashJoinTupleData::hashvalue, heap_free_minimal_tuple(), HJTUPLE_MINTUPLE, HJTUPLE_OVERHEAD, ParallelHashJoinBatchAccessor::inner_tuples, MAXALIGN, ParallelHashJoinBatchAccessor::ntuples, HashJoinTableData::parallel_state, PHJ_BUILD_HASHING_INNER, ParallelHashJoinBatchAccessor::preallocated, HashJoinTableData::shared, sts_puttuple(), and MinimalTupleData::t_len.

Referenced by MultiExecParallelHash().

1684 {
1685  bool shouldFree;
1686  MinimalTuple tuple = ExecFetchSlotMinimalTuple(slot, &shouldFree);
1687  dsa_pointer shared;
1688  int bucketno;
1689  int batchno;
1690 
1691 retry:
1692  ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno, &batchno);
1693 
1694  if (batchno == 0)
1695  {
1696  HashJoinTuple hashTuple;
1697 
1698  /* Try to load it into memory. */
1701  hashTuple = ExecParallelHashTupleAlloc(hashtable,
1702  HJTUPLE_OVERHEAD + tuple->t_len,
1703  &shared);
1704  if (hashTuple == NULL)
1705  goto retry;
1706 
1707  /* Store the hash value in the HashJoinTuple header. */
1708  hashTuple->hashvalue = hashvalue;
1709  memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
1710 
1711  /* Push it onto the front of the bucket's list */
1712  ExecParallelHashPushTuple(&hashtable->buckets.shared[bucketno],
1713  hashTuple, shared);
1714  }
1715  else
1716  {
1717  size_t tuple_size = MAXALIGN(HJTUPLE_OVERHEAD + tuple->t_len);
1718 
1719  Assert(batchno > 0);
1720 
1721  /* Try to preallocate space in the batch if necessary. */
1722  if (hashtable->batches[batchno].preallocated < tuple_size)
1723  {
1724  if (!ExecParallelHashTuplePrealloc(hashtable, batchno, tuple_size))
1725  goto retry;
1726  }
1727 
1728  Assert(hashtable->batches[batchno].preallocated >= tuple_size);
1729  hashtable->batches[batchno].preallocated -= tuple_size;
1730  sts_puttuple(hashtable->batches[batchno].inner_tuples, &hashvalue,
1731  tuple);
1732  }
1733  ++hashtable->batches[batchno].ntuples;
1734 
1735  if (shouldFree)
1736  heap_free_minimal_tuple(tuple);
1737 }
dsa_pointer_atomic * shared
Definition: hashjoin.h:299
void sts_puttuple(SharedTuplestoreAccessor *accessor, void *meta_data, MinimalTuple tuple)
MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot, bool *shouldFree)
Definition: execTuples.c:1662
uint64 dsa_pointer
Definition: dsa.h:62
SharedTuplestoreAccessor * inner_tuples
Definition: hashjoin.h:208
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1898
void heap_free_minimal_tuple(MinimalTuple mtup)
Definition: heaptuple.c:1427
static bool ExecParallelHashTuplePrealloc(HashJoinTable hashtable, int batchno, size_t size)
Definition: nodeHash.c:3319
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
union HashJoinTableData::@95 buckets
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:79
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
#define Assert(condition)
Definition: c.h:738
int BarrierPhase(Barrier *barrier)
Definition: barrier.c:243
#define MAXALIGN(LEN)
Definition: c.h:691
static void ExecParallelHashPushTuple(dsa_pointer_atomic *head, HashJoinTuple tuple, dsa_pointer tuple_shared)
Definition: nodeHash.c:3239
#define PHJ_BUILD_HASHING_INNER
Definition: hashjoin.h:259
static HashJoinTuple ExecParallelHashTupleAlloc(HashJoinTable hashtable, size_t size, dsa_pointer *shared)
Definition: nodeHash.c:2786
uint32 hashvalue
Definition: hashjoin.h:75

◆ ExecParallelHashTableInsertCurrentBatch()

void ExecParallelHashTableInsertCurrentBatch ( HashJoinTable  hashtable,
TupleTableSlot slot,
uint32  hashvalue 
)

Definition at line 1746 of file nodeHash.c.

References Assert, HashJoinTableData::buckets, HashJoinTableData::curbatch, ExecFetchSlotMinimalTuple(), ExecHashGetBucketAndBatch(), ExecParallelHashPushTuple(), ExecParallelHashTupleAlloc(), HashJoinTupleData::hashvalue, heap_free_minimal_tuple(), HeapTupleHeaderClearMatch, HJTUPLE_MINTUPLE, HJTUPLE_OVERHEAD, HashJoinTableData::shared, and MinimalTupleData::t_len.

Referenced by ExecParallelHashJoinNewBatch().

1749 {
1750  bool shouldFree;
1751  MinimalTuple tuple = ExecFetchSlotMinimalTuple(slot, &shouldFree);
1752  HashJoinTuple hashTuple;
1753  dsa_pointer shared;
1754  int batchno;
1755  int bucketno;
1756 
1757  ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno, &batchno);
1758  Assert(batchno == hashtable->curbatch);
1759  hashTuple = ExecParallelHashTupleAlloc(hashtable,
1760  HJTUPLE_OVERHEAD + tuple->t_len,
1761  &shared);
1762  hashTuple->hashvalue = hashvalue;
1763  memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
1765  ExecParallelHashPushTuple(&hashtable->buckets.shared[bucketno],
1766  hashTuple, shared);
1767 
1768  if (shouldFree)
1769  heap_free_minimal_tuple(tuple);
1770 }
dsa_pointer_atomic * shared
Definition: hashjoin.h:299
MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot, bool *shouldFree)
Definition: execTuples.c:1662
uint64 dsa_pointer
Definition: dsa.h:62
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1898
void heap_free_minimal_tuple(MinimalTuple mtup)
Definition: heaptuple.c:1427
union HashJoinTableData::@95 buckets
#define HJTUPLE_OVERHEAD
Definition: hashjoin.h:79
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
#define Assert(condition)
Definition: c.h:738
#define HeapTupleHeaderClearMatch(tup)
Definition: htup_details.h:526
static void ExecParallelHashPushTuple(dsa_pointer_atomic *head, HashJoinTuple tuple, dsa_pointer tuple_shared)
Definition: nodeHash.c:3239
static HashJoinTuple ExecParallelHashTupleAlloc(HashJoinTable hashtable, size_t size, dsa_pointer *shared)
Definition: nodeHash.c:2786
uint32 hashvalue
Definition: hashjoin.h:75

◆ ExecParallelHashTableSetCurrentBatch()

void ExecParallelHashTableSetCurrentBatch ( HashJoinTable  hashtable,
int  batchno 
)

Definition at line 3257 of file nodeHash.c.

References HashJoinTableData::area, Assert, ParallelHashJoinBatchAccessor::at_least_one_chunk, HashJoinTableData::batches, ParallelHashJoinBatch::buckets, HashJoinTableData::buckets, HashJoinTableData::curbatch, HashJoinTableData::current_chunk, HashJoinTableData::current_chunk_shared, dsa_get_address(), InvalidDsaPointer, HashJoinTableData::log2_nbuckets, my_log2(), ParallelHashJoinState::nbuckets, HashJoinTableData::nbuckets, HashJoinTableData::parallel_state, ParallelHashJoinBatchAccessor::shared, and HashJoinTableData::shared.

Referenced by ExecParallelHashIncreaseNumBatches(), ExecParallelHashIncreaseNumBuckets(), ExecParallelHashJoinNewBatch(), and MultiExecParallelHash().

3258 {
3259  Assert(hashtable->batches[batchno].shared->buckets != InvalidDsaPointer);
3260 
3261  hashtable->curbatch = batchno;
3262  hashtable->buckets.shared = (dsa_pointer_atomic *)
3263  dsa_get_address(hashtable->area,
3264  hashtable->batches[batchno].shared->buckets);
3265  hashtable->nbuckets = hashtable->parallel_state->nbuckets;
3266  hashtable->log2_nbuckets = my_log2(hashtable->nbuckets);
3267  hashtable->current_chunk = NULL;
3269  hashtable->batches[batchno].at_least_one_chunk = false;
3270 }
dsa_pointer current_chunk_shared
Definition: hashjoin.h:359
dsa_pointer_atomic * shared
Definition: hashjoin.h:299
#define InvalidDsaPointer
Definition: dsa.h:78
dsa_area * area
Definition: hashjoin.h:356
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
int my_log2(long num)
Definition: dynahash.c:1720
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
union HashJoinTableData::@95 buckets
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
#define Assert(condition)
Definition: c.h:738
ParallelHashJoinBatch * shared
Definition: hashjoin.h:197
HashMemoryChunk current_chunk
Definition: hashjoin.h:355
dsa_pointer buckets
Definition: hashjoin.h:153

◆ ExecParallelHashTupleAlloc()

static HashJoinTuple ExecParallelHashTupleAlloc ( HashJoinTable  hashtable,
size_t  size,
dsa_pointer shared 
)
static

Definition at line 2786 of file nodeHash.c.

References HashJoinTableData::area, Assert, ParallelHashJoinBatchAccessor::at_least_one_chunk, BarrierPhase(), HashJoinTableData::batches, ParallelHashJoinState::build_barrier, ParallelHashJoinBatch::chunks, HashJoinTableData::curbatch, HashJoinTableData::current_chunk, HashJoinTableData::current_chunk_shared, dsa_allocate, dsa_get_address(), ExecParallelHashIncreaseNumBatches(), ExecParallelHashIncreaseNumBuckets(), ParallelHashJoinState::growth, HASH_CHUNK_DATA, HASH_CHUNK_HEADER_SIZE, HASH_CHUNK_SIZE, HASH_CHUNK_THRESHOLD, ParallelHashJoinState::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAXALIGN, MaxAllocSize, HashMemoryChunkData::maxlen, HashJoinTableData::nbatch, HashJoinTableData::nbuckets, HashMemoryChunkData::next, NTUP_PER_BUCKET, ParallelHashJoinBatch::ntuples, ParallelHashJoinBatchAccessor::ntuples, HashJoinTableData::parallel_state, PHJ_BUILD_HASHING_INNER, PHJ_GROWTH_DISABLED, PHJ_GROWTH_NEED_MORE_BATCHES, PHJ_GROWTH_NEED_MORE_BUCKETS, HashMemoryChunkData::shared, ParallelHashJoinBatchAccessor::shared, ParallelHashJoinBatch::size, ParallelHashJoinState::space_allowed, ParallelHashJoinBatch::space_exhausted, and HashMemoryChunkData::used.

Referenced by ExecParallelHashRepartitionFirst(), ExecParallelHashTableInsert(), and ExecParallelHashTableInsertCurrentBatch().

2788 {
2789  ParallelHashJoinState *pstate = hashtable->parallel_state;
2790  dsa_pointer chunk_shared;
2791  HashMemoryChunk chunk;
2792  Size chunk_size;
2793  HashJoinTuple result;
2794  int curbatch = hashtable->curbatch;
2795 
2796  size = MAXALIGN(size);
2797 
2798  /*
2799  * Fast path: if there is enough space in this backend's current chunk,
2800  * then we can allocate without any locking.
2801  */
2802  chunk = hashtable->current_chunk;
2803  if (chunk != NULL &&
2804  size <= HASH_CHUNK_THRESHOLD &&
2805  chunk->maxlen - chunk->used >= size)
2806  {
2807 
2808  chunk_shared = hashtable->current_chunk_shared;
2809  Assert(chunk == dsa_get_address(hashtable->area, chunk_shared));
2810  *shared = chunk_shared + HASH_CHUNK_HEADER_SIZE + chunk->used;
2811  result = (HashJoinTuple) (HASH_CHUNK_DATA(chunk) + chunk->used);
2812  chunk->used += size;
2813 
2814  Assert(chunk->used <= chunk->maxlen);
2815  Assert(result == dsa_get_address(hashtable->area, *shared));
2816 
2817  return result;
2818  }
2819 
2820  /* Slow path: try to allocate a new chunk. */
2821  LWLockAcquire(&pstate->lock, LW_EXCLUSIVE);
2822 
2823  /*
2824  * Check if we need to help increase the number of buckets or batches.
2825  */
2826  if (pstate->growth == PHJ_GROWTH_NEED_MORE_BATCHES ||
2828  {
2829  ParallelHashGrowth growth = pstate->growth;
2830 
2831  hashtable->current_chunk = NULL;
2832  LWLockRelease(&pstate->lock);
2833 
2834  /* Another participant has commanded us to help grow. */
2835  if (growth == PHJ_GROWTH_NEED_MORE_BATCHES)
2837  else if (growth == PHJ_GROWTH_NEED_MORE_BUCKETS)
2839 
2840  /* The caller must retry. */
2841  return NULL;
2842  }
2843 
2844  /* Oversized tuples get their own chunk. */
2845  if (size > HASH_CHUNK_THRESHOLD)
2846  chunk_size = size + HASH_CHUNK_HEADER_SIZE;
2847  else
2848  chunk_size = HASH_CHUNK_SIZE;
2849 
2850  /* Check if it's time to grow batches or buckets. */
2851  if (pstate->growth != PHJ_GROWTH_DISABLED)
2852  {
2853  Assert(curbatch == 0);
2855 
2856  /*
2857  * Check if our space limit would be exceeded. To avoid choking on
2858  * very large tuples or very low work_mem setting, we'll always allow
2859  * each backend to allocate at least one chunk.
2860  */
2861  if (hashtable->batches[0].at_least_one_chunk &&
2862  hashtable->batches[0].shared->size +
2863  chunk_size > pstate->space_allowed)
2864  {
2866  hashtable->batches[0].shared->space_exhausted = true;
2867  LWLockRelease(&pstate->lock);
2868 
2869  return NULL;
2870  }
2871 
2872  /* Check if our load factor limit would be exceeded. */
2873  if (hashtable->nbatch == 1)
2874  {
2875  hashtable->batches[0].shared->ntuples += hashtable->batches[0].ntuples;
2876  hashtable->batches[0].ntuples = 0;
2877  /* Guard against integer overflow and alloc size overflow */
2878  if (hashtable->batches[0].shared->ntuples + 1 >
2879  hashtable->nbuckets * NTUP_PER_BUCKET &&
2880  hashtable->nbuckets < (INT_MAX / 2) &&
2881  hashtable->nbuckets * 2 <=
2882  MaxAllocSize / sizeof(dsa_pointer_atomic))
2883  {
2885  LWLockRelease(&pstate->lock);
2886 
2887  return NULL;
2888  }
2889  }
2890  }
2891 
2892  /* We are cleared to allocate a new chunk. */
2893  chunk_shared = dsa_allocate(hashtable->area, chunk_size);
2894  hashtable->batches[curbatch].shared->size += chunk_size;
2895  hashtable->batches[curbatch].at_least_one_chunk = true;
2896 
2897  /* Set up the chunk. */
2898  chunk = (HashMemoryChunk) dsa_get_address(hashtable->area, chunk_shared);
2899  *shared = chunk_shared + HASH_CHUNK_HEADER_SIZE;
2900  chunk->maxlen = chunk_size - HASH_CHUNK_HEADER_SIZE;
2901  chunk->used = size;
2902 
2903  /*
2904  * Push it onto the list of chunks, so that it can be found if we need to
2905  * increase the number of buckets or batches (batch 0 only) and later for
2906  * freeing the memory (all batches).
2907  */
2908  chunk->next.shared = hashtable->batches[curbatch].shared->chunks;
2909  hashtable->batches[curbatch].shared->chunks = chunk_shared;
2910 
2911  if (size <= HASH_CHUNK_THRESHOLD)
2912  {
2913  /*
2914  * Make this the current chunk so that we can use the fast path to
2915  * fill the rest of it up in future calls.
2916  */
2917  hashtable->current_chunk = chunk;
2918  hashtable->current_chunk_shared = chunk_shared;
2919  }
2920  LWLockRelease(&pstate->lock);
2921 
2922  Assert(HASH_CHUNK_DATA(chunk) == dsa_get_address(hashtable->area, *shared));
2923  result = (HashJoinTuple) HASH_CHUNK_DATA(chunk);
2924 
2925  return result;
2926 }
dsa_pointer current_chunk_shared
Definition: hashjoin.h:359
static void ExecParallelHashIncreaseNumBatches(HashJoinTable hashtable)
Definition: nodeHash.c:1056
#define HASH_CHUNK_SIZE
Definition: hashjoin.h:139
dsa_pointer chunks
Definition: hashjoin.h:156
uint64 dsa_pointer
Definition: dsa.h:62
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
dsa_area * area
Definition: hashjoin.h:356
dsa_pointer shared
Definition: hashjoin.h:127
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
#define HASH_CHUNK_THRESHOLD
Definition: hashjoin.h:143
struct HashJoinTupleData * HashJoinTuple
Definition: execnodes.h:1937
#define MaxAllocSize
Definition: memutils.h:40
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
struct HashMemoryChunkData * HashMemoryChunk
Definition: hashjoin.h:137
#define NTUP_PER_BUCKET
Definition: nodeHash.c:664
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
#define HASH_CHUNK_HEADER_SIZE
Definition: hashjoin.h:140
#define Assert(condition)
Definition: c.h:738
ParallelHashGrowth growth
Definition: hashjoin.h:241
size_t Size
Definition: c.h:466
int BarrierPhase(Barrier *barrier)
Definition: barrier.c:243
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
#define MAXALIGN(LEN)
Definition: c.h:691
ParallelHashJoinBatch * shared
Definition: hashjoin.h:197
#define PHJ_BUILD_HASHING_INNER
Definition: hashjoin.h:259
HashMemoryChunk current_chunk
Definition: hashjoin.h:355
union HashMemoryChunkData::@94 next
#define HASH_CHUNK_DATA(hc)
Definition: hashjoin.h:141
ParallelHashGrowth
Definition: hashjoin.h:218
static void ExecParallelHashIncreaseNumBuckets(HashJoinTable hashtable)
Definition: nodeHash.c:1493
#define dsa_allocate(area, size)
Definition: dsa.h:84

◆ ExecParallelHashTuplePrealloc()

static bool ExecParallelHashTuplePrealloc ( HashJoinTable  hashtable,
int  batchno,
size_t  size 
)
static

Definition at line 3319 of file nodeHash.c.

References Assert, ParallelHashJoinBatchAccessor::at_least_one_chunk, HashJoinTableData::batches, ParallelHashJoinBatch::estimated_size, ExecParallelHashIncreaseNumBatches(), ExecParallelHashIncreaseNumBuckets(), ParallelHashJoinState::growth, HASH_CHUNK_HEADER_SIZE, HASH_CHUNK_SIZE, ParallelHashJoinState::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), Max, MAXALIGN, HashJoinTableData::parallel_state, PHJ_GROWTH_DISABLED, PHJ_GROWTH_NEED_MORE_BATCHES, PHJ_GROWTH_NEED_MORE_BUCKETS, ParallelHashJoinBatchAccessor::preallocated, ParallelHashJoinBatchAccessor::shared, ParallelHashJoinState::space_allowed, and ParallelHashJoinBatch::space_exhausted.

Referenced by ExecParallelHashTableInsert().

3320 {
3321  ParallelHashJoinState *pstate = hashtable->parallel_state;
3322  ParallelHashJoinBatchAccessor *batch = &hashtable->batches[batchno];
3323  size_t want = Max(size, HASH_CHUNK_SIZE - HASH_CHUNK_HEADER_SIZE);
3324 
3325  Assert(batchno > 0);
3326  Assert(batchno < hashtable->nbatch);
3327  Assert(size == MAXALIGN(size));
3328 
3329  LWLockAcquire(&pstate->lock, LW_EXCLUSIVE);
3330 
3331  /* Has another participant commanded us to help grow? */
3332  if (pstate->growth == PHJ_GROWTH_NEED_MORE_BATCHES ||
3334  {
3335  ParallelHashGrowth growth = pstate->growth;
3336 
3337  LWLockRelease(&pstate->lock);
3338  if (growth == PHJ_GROWTH_NEED_MORE_BATCHES)
3340  else if (growth == PHJ_GROWTH_NEED_MORE_BUCKETS)
3342 
3343  return false;
3344  }
3345 
3346  if (pstate->growth != PHJ_GROWTH_DISABLED &&
3347  batch->at_least_one_chunk &&
3348  (batch->shared->estimated_size + want + HASH_CHUNK_HEADER_SIZE
3349  > pstate->space_allowed))
3350  {
3351  /*
3352  * We have determined that this batch would exceed the space budget if
3353  * loaded into memory. Command all participants to help repartition.
3354  */
3355  batch->shared->space_exhausted = true;
3357  LWLockRelease(&pstate->lock);
3358 
3359  return false;
3360  }
3361 
3362  batch->at_least_one_chunk = true;
3363  batch->shared->estimated_size += want + HASH_CHUNK_HEADER_SIZE;
3364  batch->preallocated = want;
3365  LWLockRelease(&pstate->lock);
3366 
3367  return true;
3368 }
static void ExecParallelHashIncreaseNumBatches(HashJoinTable hashtable)
Definition: nodeHash.c:1056
#define HASH_CHUNK_SIZE
Definition: hashjoin.h:139
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:357
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:358
#define HASH_CHUNK_HEADER_SIZE
Definition: hashjoin.h:140
#define Max(x, y)
Definition: c.h:914
#define Assert(condition)
Definition: c.h:738
ParallelHashGrowth growth
Definition: hashjoin.h:241
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
#define MAXALIGN(LEN)
Definition: c.h:691
ParallelHashJoinBatch * shared
Definition: hashjoin.h:197
ParallelHashGrowth
Definition: hashjoin.h:218
static void ExecParallelHashIncreaseNumBuckets(HashJoinTable hashtable)
Definition: nodeHash.c:1493

◆ ExecParallelScanHashBucket()

bool ExecParallelScanHashBucket ( HashJoinState hjstate,
ExprContext econtext 
)

Definition at line 1991 of file nodeHash.c.

References ExprContext::ecxt_innertuple, ExecParallelHashFirstTuple(), ExecParallelHashNextTuple(), ExecQualAndReset(), ExecStoreMinimalTuple(), HashJoinState::hashclauses, HashJoinTupleData::hashvalue, HashJoinState::hj_CurBucketNo, HashJoinState::hj_CurHashValue, HashJoinState::hj_CurTuple, HashJoinState::hj_HashTable, HashJoinState::hj_HashTupleSlot, and HJTUPLE_MINTUPLE.

Referenced by ExecHashJoinImpl().

1993 {
1994  ExprState *hjclauses = hjstate->hashclauses;
1995  HashJoinTable hashtable = hjstate->hj_HashTable;
1996  HashJoinTuple hashTuple = hjstate->hj_CurTuple;
1997  uint32 hashvalue = hjstate->hj_CurHashValue;
1998 
1999  /*
2000  * hj_CurTuple is the address of the tuple last returned from the current
2001  * bucket, or NULL if it's time to start scanning a new bucket.
2002  */
2003  if (hashTuple != NULL)
2004  hashTuple = ExecParallelHashNextTuple(hashtable, hashTuple);
2005  else
2006  hashTuple = ExecParallelHashFirstTuple(hashtable,
2007  hjstate->hj_CurBucketNo);
2008 
2009  while (hashTuple != NULL)
2010  {
2011  if (hashTuple->hashvalue == hashvalue)
2012  {
2013  TupleTableSlot *inntuple;
2014 
2015  /* insert hashtable's tuple into exec slot so ExecQual sees it */
2016  inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
2017  hjstate->hj_HashTupleSlot,
2018  false); /* do not pfree */
2019  econtext->ecxt_innertuple = inntuple;
2020 
2021  if (ExecQualAndReset(hjclauses, econtext))
2022  {
2023  hjstate->hj_CurTuple = hashTuple;
2024  return true;
2025  }
2026  }
2027 
2028  hashTuple = ExecParallelHashNextTuple(hashtable, hashTuple);
2029  }
2030 
2031  /*
2032  * no match
2033  */
2034  return false;
2035 }
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1416
uint32 hj_CurHashValue
Definition: execnodes.h:1948
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1951
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:228
unsigned int uint32
Definition: c.h:367
int hj_CurBucketNo
Definition: execnodes.h:1949
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
Definition: executor.h:397
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
static HashJoinTuple ExecParallelHashFirstTuple(HashJoinTable table, int bucketno)
Definition: nodeHash.c:3209
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1953
static HashJoinTuple ExecParallelHashNextTuple(HashJoinTable table, HashJoinTuple tuple)
Definition: nodeHash.c:3225
HashJoinTable hj_HashTable
Definition: execnodes.h:1947
uint32 hashvalue
Definition: hashjoin.h:75
ExprState * hashclauses
Definition: execnodes.h:1943

◆ ExecPrepHashTableForUnmatched()

void ExecPrepHashTableForUnmatched ( HashJoinState hjstate)

Definition at line 2042 of file nodeHash.c.

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

Referenced by ExecHashJoinImpl().

2043 {
2044  /*----------
2045  * During this scan we use the HashJoinState fields as follows:
2046  *
2047  * hj_CurBucketNo: next regular bucket to scan
2048  * hj_CurSkewBucketNo: next skew bucket (an index into skewBucketNums)
2049  * hj_CurTuple: last tuple returned, or NULL to start next bucket
2050  *----------
2051  */
2052  hjstate->hj_CurBucketNo = 0;
2053  hjstate->hj_CurSkewBucketNo = 0;
2054  hjstate->hj_CurTuple = NULL;
2055 }
int hj_CurSkewBucketNo
Definition: execnodes.h:1950
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1951
int hj_CurBucketNo
Definition: execnodes.h:1949

◆ ExecReScanHash()

void ExecReScanHash ( HashState node)

Definition at line 2192 of file nodeHash.c.

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

Referenced by ExecReScan().

2193 {
2194  /*
2195  * if chgParam of subnode is not null then plan will be re-scanned by
2196  * first ExecProcNode.
2197  */
2198  if (node->ps.lefttree->chgParam == NULL)
2199  ExecReScan(node->ps.lefttree);
2200 }
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
struct PlanState * lefttree
Definition: execnodes.h:967
PlanState ps
Definition: execnodes.h:2380
Bitmapset * chgParam
Definition: execnodes.h:977

◆ ExecScanHashBucket()

bool ExecScanHashBucket ( HashJoinState hjstate,
ExprContext econtext 
)

Definition at line 1930 of file nodeHash.c.

References HashJoinTableData::buckets, ExprContext::ecxt_innertuple, ExecQualAndReset(), 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, HashJoinTableData::skewBucket, HashSkewBucket::tuples, HashJoinTupleData::unshared, and HashJoinTableData::unshared.

Referenced by ExecHashJoinImpl().

1932 {
1933  ExprState *hjclauses = hjstate->hashclauses;
1934  HashJoinTable hashtable = hjstate->hj_HashTable;
1935  HashJoinTuple hashTuple = hjstate->hj_CurTuple;
1936  uint32 hashvalue = hjstate->hj_CurHashValue;
1937 
1938  /*
1939  * hj_CurTuple is the address of the tuple last returned from the current
1940  * bucket, or NULL if it's time to start scanning a new bucket.
1941  *
1942  * If the tuple hashed to a skew bucket then scan the skew bucket
1943  * otherwise scan the standard hashtable bucket.
1944  */
1945  if (hashTuple != NULL)
1946  hashTuple = hashTuple->next.unshared;
1947  else if (hjstate->hj_CurSkewBucketNo != INVALID_SKEW_BUCKET_NO)
1948  hashTuple = hashtable->skewBucket[hjstate->hj_CurSkewBucketNo]->tuples;
1949  else
1950  hashTuple = hashtable->buckets.unshared[hjstate->hj_CurBucketNo];
1951 
1952  while (hashTuple != NULL)
1953  {
1954  if (hashTuple->hashvalue == hashvalue)
1955  {
1956  TupleTableSlot *inntuple;
1957 
1958  /* insert hashtable's tuple into exec slot so ExecQual sees it */
1959  inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
1960  hjstate->hj_HashTupleSlot,
1961  false); /* do not pfree */
1962  econtext->ecxt_innertuple = inntuple;
1963 
1964  if (ExecQualAndReset(hjclauses, econtext))
1965  {
1966  hjstate->hj_CurTuple = hashTuple;
1967  return true;
1968  }
1969  }
1970 
1971  hashTuple = hashTuple->next.unshared;
1972  }
1973 
1974  /*
1975  * no match
1976  */
1977  return false;
1978 }
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:109
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1416
uint32 hj_CurHashValue
Definition: execnodes.h:1948
int hj_CurSkewBucketNo
Definition: execnodes.h:1950
struct HashJoinTupleData * unshared
Definition: hashjoin.h:72
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1951
HashJoinTuple tuples
Definition: hashjoin.h:105
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:228
unsigned int uint32
Definition: c.h:367
union HashJoinTupleData::@93 next
int hj_CurBucketNo
Definition: execnodes.h:1949
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
Definition: executor.h:397
HashSkewBucket ** skewBucket
Definition: hashjoin.h:305
union HashJoinTableData::@95 buckets
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1953
HashJoinTable hj_HashTable
Definition: execnodes.h:1947
struct HashJoinTupleData ** unshared
Definition: hashjoin.h:297
uint32 hashvalue
Definition: hashjoin.h:75
ExprState * hashclauses
Definition: execnodes.h:1943

◆ ExecScanHashTableForUnmatched()

bool ExecScanHashTableForUnmatched ( HashJoinState hjstate,
ExprContext econtext 
)

Definition at line 2066 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, HashSkewBucket::tuples, HashJoinTupleData::unshared, and HashJoinTableData::unshared.

Referenced by ExecHashJoinImpl().

2067 {
2068  HashJoinTable hashtable = hjstate->hj_HashTable;
2069  HashJoinTuple hashTuple = hjstate->hj_CurTuple;
2070 
2071  for (;;)
2072  {
2073  /*
2074  * hj_CurTuple is the address of the tuple last returned from the
2075  * current bucket, or NULL if it's time to start scanning a new
2076  * bucket.
2077  */
2078  if (hashTuple != NULL)
2079  hashTuple = hashTuple->next.unshared;
2080  else if (hjstate->hj_CurBucketNo < hashtable->nbuckets)
2081  {
2082  hashTuple = hashtable->buckets.unshared[hjstate->hj_CurBucketNo];
2083  hjstate->hj_CurBucketNo++;
2084  }
2085  else if (hjstate->hj_CurSkewBucketNo < hashtable->nSkewBuckets)
2086  {
2087  int j = hashtable->skewBucketNums[hjstate->hj_CurSkewBucketNo];
2088 
2089  hashTuple = hashtable->skewBucket[j]->tuples;
2090  hjstate->hj_CurSkewBucketNo++;
2091  }
2092  else
2093  break; /* finished all buckets */
2094 
2095  while (hashTuple != NULL)
2096  {
2097  if (!HeapTupleHeaderHasMatch(HJTUPLE_MINTUPLE(hashTuple)))
2098  {
2099  TupleTableSlot *inntuple;
2100 
2101  /* insert hashtable's tuple into exec slot */
2102  inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
2103  hjstate->hj_HashTupleSlot,
2104  false); /* do not pfree */
2105  econtext->ecxt_innertuple = inntuple;
2106 
2107  /*
2108  * Reset temp memory each time; although this function doesn't
2109  * do any qual eval, the caller will, so let's keep it
2110  * parallel to ExecScanHashBucket.
2111  */
2112  ResetExprContext(econtext);
2113 
2114  hjstate->hj_CurTuple = hashTuple;
2115  return true;
2116  }
2117 
2118  hashTuple = hashTuple->next.unshared;
2119  }
2120 
2121  /* allow this loop to be cancellable */
2123  }
2124 
2125  /*
2126  * no more unmatched tuples
2127  */
2128  return false;
2129 }
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1416
int * skewBucketNums
Definition: hashjoin.h:308
int hj_CurSkewBucketNo
Definition: execnodes.h:1950
struct HashJoinTupleData * unshared
Definition: hashjoin.h:72
HashJoinTuple hj_CurTuple
Definition: execnodes.h:1951
HashJoinTuple tuples
Definition: hashjoin.h:105
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:228
union HashJoinTupleData::@93 next
int hj_CurBucketNo
Definition: execnodes.h:1949
HashSkewBucket ** skewBucket
Definition: hashjoin.h:305
union HashJoinTableData::@95 buckets
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:80
#define HeapTupleHeaderHasMatch(tup)
Definition: htup_details.h:516
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:1953
HashJoinTable hj_HashTable
Definition: execnodes.h:1947
struct HashJoinTupleData ** unshared
Definition: hashjoin.h:297
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define ResetExprContext(econtext)
Definition: executor.h:501

◆ ExecShutdownHash()

void ExecShutdownHash ( HashState node)

Definition at line 2640 of file nodeHash.c.

References ExecHashAccumInstrumentation(), HashState::hashtable, HashState::hinstrument, PlanState::instrument, palloc0(), and HashState::ps.

Referenced by ExecShutdownNode().

2641 {
2642  /* Allocate save space if EXPLAIN'ing and we didn't do so already */
2643  if (node->ps.instrument && !node->hinstrument)
2644  node->hinstrument = (HashInstrumentation *)
2645  palloc0(sizeof(HashInstrumentation));
2646  /* Now accumulate data for the current (final) hash table */
2647  if (node->hinstrument && node->hashtable)
2649 }