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

Go to the source code of this file.

Functions

HashStateExecInitHash (Hash *node, EState *estate, int eflags)
 
NodeMultiExecHash (HashState *node)
 
void ExecEndHash (HashState *node)
 
void ExecReScanHash (HashState *node)
 
HashJoinTable ExecHashTableCreate (HashState *state)
 
void ExecParallelHashTableAlloc (HashJoinTable hashtable, int batchno)
 
void ExecHashTableDestroy (HashJoinTable hashtable)
 
void ExecHashTableDetach (HashJoinTable hashtable)
 
void ExecHashTableDetachBatch (HashJoinTable hashtable)
 
void ExecParallelHashTableSetCurrentBatch (HashJoinTable hashtable, int batchno)
 
void ExecHashTableInsert (HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue)
 
void ExecParallelHashTableInsert (HashJoinTable hashtable, TupleTableSlot *slot, uint32 hashvalue)
 
void ExecParallelHashTableInsertCurrentBatch (HashJoinTable hashtable, TupleTableSlot *slot, 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 ExecParallelPrepHashTableForUnmatched (HashJoinState *hjstate)
 
bool ExecScanHashTableForUnmatched (HashJoinState *hjstate, ExprContext *econtext)
 
bool ExecParallelScanHashTableForUnmatched (HashJoinState *hjstate, ExprContext *econtext)
 
void ExecHashTableReset (HashJoinTable hashtable)
 
void ExecHashTableResetMatchFlags (HashJoinTable hashtable)
 
void ExecChooseHashTableSize (double ntuples, int tupwidth, bool useskew, bool try_combined_hash_mem, int parallel_workers, size_t *space_allowed, int *numbuckets, int *numbatches, int *num_skew_mcvs)
 
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 ExecHashRetrieveInstrumentation (HashState *node)
 
void ExecShutdownHash (HashState *node)
 
void ExecHashAccumInstrumentation (HashInstrumentation *instrument, HashJoinTable hashtable)
 

Function Documentation

◆ ExecChooseHashTableSize()

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

Definition at line 658 of file nodeHash.c.

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

References Assert, ExecChooseHashTableSize(), get_hash_memory_limit(), HJTUPLE_OVERHEAD, Max, MAXALIGN, MaxAllocSize, Min, NTUP_PER_BUCKET, pg_nextpower2_32(), pg_nextpower2_size_t, pg_prevpower2_size_t, SizeofMinimalTupleHeader, SKEW_BUCKET_OVERHEAD, and SKEW_HASH_MEM_PERCENT.

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

◆ ExecEndHash()

void ExecEndHash ( HashState node)

Definition at line 427 of file nodeHash.c.

428{
430
431 /*
432 * shut down the subplan
433 */
436}
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:562
#define outerPlanState(node)
Definition: execnodes.h:1221
#define outerPlan(node)
Definition: plannodes.h:183

References ExecEndNode(), outerPlan, and outerPlanState.

Referenced by ExecEndNode().

◆ ExecHashAccumInstrumentation()

void ExecHashAccumInstrumentation ( HashInstrumentation instrument,
HashJoinTable  hashtable 
)

Definition at line 2742 of file nodeHash.c.

2744{
2745 instrument->nbuckets = Max(instrument->nbuckets,
2746 hashtable->nbuckets);
2747 instrument->nbuckets_original = Max(instrument->nbuckets_original,
2748 hashtable->nbuckets_original);
2749 instrument->nbatch = Max(instrument->nbatch,
2750 hashtable->nbatch);
2751 instrument->nbatch_original = Max(instrument->nbatch_original,
2752 hashtable->nbatch_original);
2753 instrument->space_peak = Max(instrument->space_peak,
2754 hashtable->spacePeak);
2755}

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

◆ ExecHashEstimate()

void ExecHashEstimate ( HashState node,
ParallelContext pcxt 
)

Definition at line 2626 of file nodeHash.c.

2627{
2628 size_t size;
2629
2630 /* don't need this if not instrumenting or no workers */
2631 if (!node->ps.instrument || pcxt->nworkers == 0)
2632 return;
2633
2634 size = mul_size(pcxt->nworkers, sizeof(HashInstrumentation));
2635 size = add_size(size, offsetof(SharedHashInfo, hinstrument));
2638}
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
Size add_size(Size s1, Size s2)
Definition: shmem.c:488
Size mul_size(Size s1, Size s2)
Definition: shmem.c:505
static pg_noinline void Size size
Definition: slab.c:607
PlanState ps
Definition: execnodes.h:2776
shm_toc_estimator estimator
Definition: parallel.h:41
Instrumentation * instrument
Definition: execnodes.h:1135

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

Referenced by ExecParallelEstimate().

◆ ExecHashGetBucketAndBatch()

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

Definition at line 1825 of file nodeHash.c.

1829{
1830 uint32 nbuckets = (uint32) hashtable->nbuckets;
1831 uint32 nbatch = (uint32) hashtable->nbatch;
1832
1833 if (nbatch > 1)
1834 {
1835 *bucketno = hashvalue & (nbuckets - 1);
1836 *batchno = pg_rotate_right32(hashvalue,
1837 hashtable->log2_nbuckets) & (nbatch - 1);
1838 }
1839 else
1840 {
1841 *bucketno = hashvalue & (nbuckets - 1);
1842 *batchno = 0;
1843 }
1844}
uint32_t uint32
Definition: c.h:485
static uint32 pg_rotate_right32(uint32 word, int n)
Definition: pg_bitutils.h:398

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

◆ ExecHashGetSkewBucket()

int ExecHashGetSkewBucket ( HashJoinTable  hashtable,
uint32  hashvalue 
)

Definition at line 2420 of file nodeHash.c.

2421{
2422 int bucket;
2423
2424 /*
2425 * Always return INVALID_SKEW_BUCKET_NO if not doing skew optimization (in
2426 * particular, this happens after the initial batch is done).
2427 */
2428 if (!hashtable->skewEnabled)
2430
2431 /*
2432 * Since skewBucketLen is a power of 2, we can do a modulo by ANDing.
2433 */
2434 bucket = hashvalue & (hashtable->skewBucketLen - 1);
2435
2436 /*
2437 * While we have not hit a hole in the hashtable and have not hit the
2438 * desired bucket, we have collided with some other hash value, so try the
2439 * next bucket location.
2440 */
2441 while (hashtable->skewBucket[bucket] != NULL &&
2442 hashtable->skewBucket[bucket]->hashvalue != hashvalue)
2443 bucket = (bucket + 1) & (hashtable->skewBucketLen - 1);
2444
2445 /*
2446 * Found the desired bucket?
2447 */
2448 if (hashtable->skewBucket[bucket] != NULL)
2449 return bucket;
2450
2451 /*
2452 * There must not be any hashtable entry for this hash value.
2453 */
2455}
#define INVALID_SKEW_BUCKET_NO
Definition: hashjoin.h:120
HashSkewBucket ** skewBucket
Definition: hashjoin.h:317
uint32 hashvalue
Definition: hashjoin.h:115

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

Referenced by ExecHashJoinImpl(), and MultiExecPrivateHash().

◆ ExecHashInitializeDSM()

void ExecHashInitializeDSM ( HashState node,
ParallelContext pcxt 
)

Definition at line 2645 of file nodeHash.c.

2646{
2647 size_t size;
2648
2649 /* don't need this if not instrumenting or no workers */
2650 if (!node->ps.instrument || pcxt->nworkers == 0)
2651 return;
2652
2653 size = offsetof(SharedHashInfo, hinstrument) +
2654 pcxt->nworkers * sizeof(HashInstrumentation);
2656
2657 /* Each per-worker area must start out as zeroes. */
2658 memset(node->shared_info, 0, size);
2659
2660 node->shared_info->num_workers = pcxt->nworkers;
2661 shm_toc_insert(pcxt->toc, node->ps.plan->plan_node_id,
2662 node->shared_info);
2663}
struct HashInstrumentation HashInstrumentation
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
SharedHashInfo * shared_info
Definition: execnodes.h:2789
shm_toc * toc
Definition: parallel.h:44
Plan * plan
Definition: execnodes.h:1125
int plan_node_id
Definition: plannodes.h:152

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

Referenced by ExecParallelInitializeDSM().

◆ ExecHashInitializeWorker()

void ExecHashInitializeWorker ( HashState node,
ParallelWorkerContext pwcxt 
)

Definition at line 2670 of file nodeHash.c.

2671{
2672 SharedHashInfo *shared_info;
2673
2674 /* don't need this if not instrumenting */
2675 if (!node->ps.instrument)
2676 return;
2677
2678 /*
2679 * Find our entry in the shared area, and set up a pointer to it so that
2680 * we'll accumulate stats there when shutting down or rebuilding the hash
2681 * table.
2682 */
2683 shared_info = (SharedHashInfo *)
2684 shm_toc_lookup(pwcxt->toc, node->ps.plan->plan_node_id, false);
2685 node->hinstrument = &shared_info->hinstrument[ParallelWorkerNumber];
2686}
int ParallelWorkerNumber
Definition: parallel.c:114
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
HashInstrumentation * hinstrument
Definition: execnodes.h:2796
HashInstrumentation hinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:2767

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

◆ ExecHashRetrieveInstrumentation()

void ExecHashRetrieveInstrumentation ( HashState node)

Definition at line 2711 of file nodeHash.c.

2712{
2713 SharedHashInfo *shared_info = node->shared_info;
2714 size_t size;
2715
2716 if (shared_info == NULL)
2717 return;
2718
2719 /* Replace node->shared_info with a copy in backend-local memory. */
2720 size = offsetof(SharedHashInfo, hinstrument) +
2721 shared_info->num_workers * sizeof(HashInstrumentation);
2722 node->shared_info = palloc(size);
2723 memcpy(node->shared_info, shared_info, size);
2724}
void * palloc(Size size)
Definition: mcxt.c:1317

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

Referenced by ExecParallelRetrieveInstrumentation().

◆ ExecHashTableCreate()

HashJoinTable ExecHashTableCreate ( HashState state)

Definition at line 446 of file nodeHash.c.

447{
448 Hash *node;
449 HashJoinTable hashtable;
450 Plan *outerNode;
451 size_t space_allowed;
452 int nbuckets;
453 int nbatch;
454 double rows;
455 int num_skew_mcvs;
456 int log2_nbuckets;
457 MemoryContext oldcxt;
458
459 /*
460 * Get information about the size of the relation to be hashed (it's the
461 * "outer" subtree of this node, but the inner relation of the hashjoin).
462 * Compute the appropriate size of the hash table.
463 */
464 node = (Hash *) state->ps.plan;
465 outerNode = outerPlan(node);
466
467 /*
468 * If this is shared hash table with a partial plan, then we can't use
469 * outerNode->plan_rows to estimate its size. We need an estimate of the
470 * total number of rows across all copies of the partial plan.
471 */
472 rows = node->plan.parallel_aware ? node->rows_total : outerNode->plan_rows;
473
474 ExecChooseHashTableSize(rows, outerNode->plan_width,
475 OidIsValid(node->skewTable),
476 state->parallel_state != NULL,
477 state->parallel_state != NULL ?
478 state->parallel_state->nparticipants - 1 : 0,
479 &space_allowed,
480 &nbuckets, &nbatch, &num_skew_mcvs);
481
482 /* nbuckets must be a power of 2 */
483 log2_nbuckets = my_log2(nbuckets);
484 Assert(nbuckets == (1 << log2_nbuckets));
485
486 /*
487 * Initialize the hash table control block.
488 *
489 * The hashtable control block is just palloc'd from the executor's
490 * per-query memory context. Everything else should be kept inside the
491 * subsidiary hashCxt, batchCxt or spillCxt.
492 */
493 hashtable = palloc_object(HashJoinTableData);
494 hashtable->nbuckets = nbuckets;
495 hashtable->nbuckets_original = nbuckets;
496 hashtable->nbuckets_optimal = nbuckets;
497 hashtable->log2_nbuckets = log2_nbuckets;
498 hashtable->log2_nbuckets_optimal = log2_nbuckets;
499 hashtable->buckets.unshared = NULL;
500 hashtable->skewEnabled = false;
501 hashtable->skewBucket = NULL;
502 hashtable->skewBucketLen = 0;
503 hashtable->nSkewBuckets = 0;
504 hashtable->skewBucketNums = NULL;
505 hashtable->nbatch = nbatch;
506 hashtable->curbatch = 0;
507 hashtable->nbatch_original = nbatch;
508 hashtable->nbatch_outstart = nbatch;
509 hashtable->growEnabled = true;
510 hashtable->totalTuples = 0;
511 hashtable->partialTuples = 0;
512 hashtable->skewTuples = 0;
513 hashtable->innerBatchFile = NULL;
514 hashtable->outerBatchFile = NULL;
515 hashtable->spaceUsed = 0;
516 hashtable->spacePeak = 0;
517 hashtable->spaceAllowed = space_allowed;
518 hashtable->spaceUsedSkew = 0;
519 hashtable->spaceAllowedSkew =
520 hashtable->spaceAllowed * SKEW_HASH_MEM_PERCENT / 100;
521 hashtable->chunks = NULL;
522 hashtable->current_chunk = NULL;
523 hashtable->parallel_state = state->parallel_state;
524 hashtable->area = state->ps.state->es_query_dsa;
525 hashtable->batches = NULL;
526
527#ifdef HJDEBUG
528 printf("Hashjoin %p: initial nbatch = %d, nbuckets = %d\n",
529 hashtable, nbatch, nbuckets);
530#endif
531
532 /*
533 * Create temporary memory contexts in which to keep the hashtable working
534 * storage. See notes in executor/hashjoin.h.
535 */
537 "HashTableContext",
539
540 hashtable->batchCxt = AllocSetContextCreate(hashtable->hashCxt,
541 "HashBatchContext",
543
544 hashtable->spillCxt = AllocSetContextCreate(hashtable->hashCxt,
545 "HashSpillContext",
547
548 /* Allocate data that will live for the life of the hashjoin */
549
550 oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
551
552 if (nbatch > 1 && hashtable->parallel_state == NULL)
553 {
554 MemoryContext oldctx;
555
556 /*
557 * allocate and initialize the file arrays in hashCxt (not needed for
558 * parallel case which uses shared tuplestores instead of raw files)
559 */
560 oldctx = MemoryContextSwitchTo(hashtable->spillCxt);
561
562 hashtable->innerBatchFile = palloc0_array(BufFile *, nbatch);
563 hashtable->outerBatchFile = palloc0_array(BufFile *, nbatch);
564
565 MemoryContextSwitchTo(oldctx);
566
567 /* The files will not be opened until needed... */
568 /* ... but make sure we have temp tablespaces established for them */
570 }
571
572 MemoryContextSwitchTo(oldcxt);
573
574 if (hashtable->parallel_state)
575 {
576 ParallelHashJoinState *pstate = hashtable->parallel_state;
577 Barrier *build_barrier;
578
579 /*
580 * Attach to the build barrier. The corresponding detach operation is
581 * in ExecHashTableDetach. Note that we won't attach to the
582 * batch_barrier for batch 0 yet. We'll attach later and start it out
583 * in PHJ_BATCH_PROBE phase, because batch 0 is allocated up front and
584 * then loaded while hashing (the standard hybrid hash join
585 * algorithm), and we'll coordinate that using build_barrier.
586 */
587 build_barrier = &pstate->build_barrier;
588 BarrierAttach(build_barrier);
589
590 /*
591 * So far we have no idea whether there are any other participants,
592 * and if so, what phase they are working on. The only thing we care
593 * about at this point is whether someone has already created the
594 * SharedHashJoinBatch objects and the hash table for batch 0. One
595 * backend will be elected to do that now if necessary.
596 */
597 if (BarrierPhase(build_barrier) == PHJ_BUILD_ELECT &&
598 BarrierArriveAndWait(build_barrier, WAIT_EVENT_HASH_BUILD_ELECT))
599 {
600 pstate->nbatch = nbatch;
601 pstate->space_allowed = space_allowed;
602 pstate->growth = PHJ_GROWTH_OK;
603
604 /* Set up the shared state for coordinating batches. */
605 ExecParallelHashJoinSetUpBatches(hashtable, nbatch);
606
607 /*
608 * Allocate batch 0's hash table up front so we can load it
609 * directly while hashing.
610 */
611 pstate->nbuckets = nbuckets;
612 ExecParallelHashTableAlloc(hashtable, 0);
613 }
614
615 /*
616 * The next Parallel Hash synchronization point is in
617 * MultiExecParallelHash(), which will progress it all the way to
618 * PHJ_BUILD_RUN. The caller must not return control from this
619 * executor node between now and then.
620 */
621 }
622 else
623 {
624 /*
625 * Prepare context for the first-scan space allocations; allocate the
626 * hashbucket array therein, and set each bucket "empty".
627 */
629
630 hashtable->buckets.unshared = palloc0_array(HashJoinTuple, nbuckets);
631
632 /*
633 * Set up for skew optimization, if possible and there's a need for
634 * more than one batch. (In a one-batch join, there's no point in
635 * it.)
636 */
637 if (nbatch > 1)
638 ExecHashBuildSkewHash(state, hashtable, node, num_skew_mcvs);
639
640 MemoryContextSwitchTo(oldcxt);
641 }
642
643 return hashtable;
644}
void PrepareTempTablespaces(void)
Definition: tablespace.c:1331
int BarrierAttach(Barrier *barrier)
Definition: barrier.c:236
int BarrierPhase(Barrier *barrier)
Definition: barrier.c:265
bool BarrierArriveAndWait(Barrier *barrier, uint32 wait_event_info)
Definition: barrier.c:125
#define OidIsValid(objectId)
Definition: c.h:729
int my_log2(long num)
Definition: dynahash.c:1794
#define palloc_object(type)
Definition: fe_memutils.h:74
#define palloc0_array(type, count)
Definition: fe_memutils.h:77
@ PHJ_GROWTH_OK
Definition: hashjoin.h:233
#define PHJ_BUILD_ELECT
Definition: hashjoin.h:269
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
static void ExecHashBuildSkewHash(HashState *hashstate, HashJoinTable hashtable, Hash *node, int mcvsToUse)
Definition: nodeHash.c:2268
static void ExecParallelHashJoinSetUpBatches(HashJoinTable hashtable, int nbatch)
Definition: nodeHash.c:2989
void ExecParallelHashTableAlloc(HashJoinTable hashtable, int batchno)
Definition: nodeHash.c:3154
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define printf(...)
Definition: port.h:244
struct HashJoinTupleData ** unshared
Definition: hashjoin.h:311
HashMemoryChunk chunks
Definition: hashjoin.h:355
ParallelHashJoinBatchAccessor * batches
Definition: hashjoin.h:361
MemoryContext hashCxt
Definition: hashjoin.h:350
double totalTuples
Definition: hashjoin.h:330
union HashJoinTableData::@107 buckets
double partialTuples
Definition: hashjoin.h:331
ParallelHashJoinState * parallel_state
Definition: hashjoin.h:360
MemoryContext spillCxt
Definition: hashjoin.h:352
HashMemoryChunk current_chunk
Definition: hashjoin.h:358
Size spaceAllowedSkew
Definition: hashjoin.h:348
int * skewBucketNums
Definition: hashjoin.h:320
BufFile ** innerBatchFile
Definition: hashjoin.h:341
int log2_nbuckets_optimal
Definition: hashjoin.h:305
dsa_area * area
Definition: hashjoin.h:359
BufFile ** outerBatchFile
Definition: hashjoin.h:342
MemoryContext batchCxt
Definition: hashjoin.h:351
double skewTuples
Definition: hashjoin.h:332
Oid skewTable
Definition: plannodes.h:1207
Cardinality rows_total
Definition: plannodes.h:1211
Plan plan
Definition: plannodes.h:1200
ParallelHashGrowth growth
Definition: hashjoin.h:253
bool parallel_aware
Definition: plannodes.h:141
int plan_width
Definition: plannodes.h:136
Cardinality plan_rows
Definition: plannodes.h:135
Definition: regguts.h:323

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, HashJoinTableData::area, Assert, BarrierArriveAndWait(), BarrierAttach(), BarrierPhase(), HashJoinTableData::batchCxt, HashJoinTableData::batches, HashJoinTableData::buckets, ParallelHashJoinState::build_barrier, HashJoinTableData::chunks, HashJoinTableData::curbatch, HashJoinTableData::current_chunk, CurrentMemoryContext, ExecChooseHashTableSize(), ExecHashBuildSkewHash(), ExecParallelHashJoinSetUpBatches(), ExecParallelHashTableAlloc(), HashJoinTableData::growEnabled, ParallelHashJoinState::growth, HashJoinTableData::hashCxt, HashJoinTableData::innerBatchFile, 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, HashJoinTableData::nSkewBuckets, OidIsValid, HashJoinTableData::outerBatchFile, outerPlan, palloc0_array, palloc_object, Plan::parallel_aware, HashJoinTableData::parallel_state, HashJoinTableData::partialTuples, PHJ_BUILD_ELECT, PHJ_GROWTH_OK, Hash::plan, Plan::plan_rows, Plan::plan_width, PrepareTempTablespaces(), printf, Hash::rows_total, SKEW_HASH_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, HashJoinTableData::spillCxt, HashJoinTableData::totalTuples, and HashJoinTableData::unshared.

Referenced by ExecHashJoinImpl().

◆ ExecHashTableDestroy()

void ExecHashTableDestroy ( HashJoinTable  hashtable)

Definition at line 866 of file nodeHash.c.

867{
868 int i;
869
870 /*
871 * Make sure all the temp files are closed. We skip batch 0, since it
872 * can't have any temp files (and the arrays might not even exist if
873 * nbatch is only 1). Parallel hash joins don't use these files.
874 */
875 if (hashtable->innerBatchFile != NULL)
876 {
877 for (i = 1; i < hashtable->nbatch; i++)
878 {
879 if (hashtable->innerBatchFile[i])
880 BufFileClose(hashtable->innerBatchFile[i]);
881 if (hashtable->outerBatchFile[i])
882 BufFileClose(hashtable->outerBatchFile[i]);
883 }
884 }
885
886 /* Release working memory (batchCxt is a child, so it goes away too) */
887 MemoryContextDelete(hashtable->hashCxt);
888
889 /* And drop the control block */
890 pfree(hashtable);
891}
void BufFileClose(BufFile *file)
Definition: buffile.c:412
int i
Definition: isn.c:72
void pfree(void *pointer)
Definition: mcxt.c:1521
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454

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

Referenced by ExecEndHashJoin(), and ExecReScanHashJoin().

◆ ExecHashTableDetach()

void ExecHashTableDetach ( HashJoinTable  hashtable)

Definition at line 3266 of file nodeHash.c.

3267{
3268 ParallelHashJoinState *pstate = hashtable->parallel_state;
3269
3270 /*
3271 * If we're involved in a parallel query, we must either have gotten all
3272 * the way to PHJ_BUILD_RUN, or joined too late and be in PHJ_BUILD_FREE.
3273 */
3274 Assert(!pstate ||
3276
3277 if (pstate && BarrierPhase(&pstate->build_barrier) == PHJ_BUILD_RUN)
3278 {
3279 int i;
3280
3281 /* Make sure any temporary files are closed. */
3282 if (hashtable->batches)
3283 {
3284 for (i = 0; i < hashtable->nbatch; ++i)
3285 {
3286 sts_end_write(hashtable->batches[i].inner_tuples);
3287 sts_end_write(hashtable->batches[i].outer_tuples);
3290 }
3291 }
3292
3293 /* If we're last to detach, clean up shared memory. */
3295 {
3296 /*
3297 * Late joining processes will see this state and give up
3298 * immediately.
3299 */
3301
3302 if (DsaPointerIsValid(pstate->batches))
3303 {
3304 dsa_free(hashtable->area, pstate->batches);
3305 pstate->batches = InvalidDsaPointer;
3306 }
3307 }
3308 }
3309 hashtable->parallel_state = NULL;
3310}
bool BarrierArriveAndDetach(Barrier *barrier)
Definition: barrier.c:203
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:826
#define InvalidDsaPointer
Definition: dsa.h:78
#define DsaPointerIsValid(x)
Definition: dsa.h:106
#define PHJ_BUILD_FREE
Definition: hashjoin.h:274
#define PHJ_BUILD_RUN
Definition: hashjoin.h:273
void sts_end_write(SharedTuplestoreAccessor *accessor)
void sts_end_parallel_scan(SharedTuplestoreAccessor *accessor)
SharedTuplestoreAccessor * outer_tuples
Definition: hashjoin.h:221
SharedTuplestoreAccessor * inner_tuples
Definition: hashjoin.h:220
dsa_pointer batches
Definition: hashjoin.h:248

References HashJoinTableData::area, Assert, BarrierArriveAndDetach(), BarrierPhase(), ParallelHashJoinState::batches, HashJoinTableData::batches, ParallelHashJoinState::build_barrier, dsa_free(), DsaPointerIsValid, i, ParallelHashJoinBatchAccessor::inner_tuples, InvalidDsaPointer, HashJoinTableData::nbatch, ParallelHashJoinBatchAccessor::outer_tuples, HashJoinTableData::parallel_state, PHJ_BUILD_FREE, PHJ_BUILD_RUN, sts_end_parallel_scan(), and sts_end_write().

Referenced by ExecHashJoinReInitializeDSM(), and ExecShutdownHashJoin().

◆ ExecHashTableDetachBatch()

void ExecHashTableDetachBatch ( HashJoinTable  hashtable)

Definition at line 3174 of file nodeHash.c.

3175{
3176 if (hashtable->parallel_state != NULL &&
3177 hashtable->curbatch >= 0)
3178 {
3179 int curbatch = hashtable->curbatch;
3180 ParallelHashJoinBatch *batch = hashtable->batches[curbatch].shared;
3181 bool attached = true;
3182
3183 /* Make sure any temporary files are closed. */
3184 sts_end_parallel_scan(hashtable->batches[curbatch].inner_tuples);
3185 sts_end_parallel_scan(hashtable->batches[curbatch].outer_tuples);
3186
3187 /* After attaching we always get at least to PHJ_BATCH_PROBE. */
3190
3191 /*
3192 * If we're abandoning the PHJ_BATCH_PROBE phase early without having
3193 * reached the end of it, it means the plan doesn't want any more
3194 * tuples, and it is happy to abandon any tuples buffered in this
3195 * process's subplans. For correctness, we can't allow any process to
3196 * execute the PHJ_BATCH_SCAN phase, because we will never have the
3197 * complete set of match bits. Therefore we skip emitting unmatched
3198 * tuples in all backends (if this is a full/right join), as if those
3199 * tuples were all due to be emitted by this process and it has
3200 * abandoned them too.
3201 */
3202 if (BarrierPhase(&batch->batch_barrier) == PHJ_BATCH_PROBE &&
3203 !hashtable->batches[curbatch].outer_eof)
3204 {
3205 /*
3206 * This flag may be written to by multiple backends during
3207 * PHJ_BATCH_PROBE phase, but will only be read in PHJ_BATCH_SCAN
3208 * phase so requires no extra locking.
3209 */
3210 batch->skip_unmatched = true;
3211 }
3212
3213 /*
3214 * Even if we aren't doing a full/right outer join, we'll step through
3215 * the PHJ_BATCH_SCAN phase just to maintain the invariant that
3216 * freeing happens in PHJ_BATCH_FREE, but that'll be wait-free.
3217 */
3220 if (attached && BarrierArriveAndDetach(&batch->batch_barrier))
3221 {
3222 /*
3223 * We are not longer attached to the batch barrier, but we're the
3224 * process that was chosen to free resources and it's safe to
3225 * assert the current phase. The ParallelHashJoinBatch can't go
3226 * away underneath us while we are attached to the build barrier,
3227 * making this access safe.
3228 */
3230
3231 /* Free shared chunks and buckets. */
3232 while (DsaPointerIsValid(batch->chunks))
3233 {
3235 dsa_get_address(hashtable->area, batch->chunks);
3236 dsa_pointer next = chunk->next.shared;
3237
3238 dsa_free(hashtable->area, batch->chunks);
3239 batch->chunks = next;
3240 }
3241 if (DsaPointerIsValid(batch->buckets))
3242 {
3243 dsa_free(hashtable->area, batch->buckets);
3244 batch->buckets = InvalidDsaPointer;
3245 }
3246 }
3247
3248 /*
3249 * Track the largest batch we've been attached to. Though each
3250 * backend might see a different subset of batches, explain.c will
3251 * scan the results from all backends to find the largest value.
3252 */
3253 hashtable->spacePeak =
3254 Max(hashtable->spacePeak,
3255 batch->size + sizeof(dsa_pointer_atomic) * hashtable->nbuckets);
3256
3257 /* Remember that we are not attached to a batch. */
3258 hashtable->curbatch = -1;
3259 }
3260}
bool BarrierArriveAndDetachExceptLast(Barrier *barrier)
Definition: barrier.c:213
static int32 next
Definition: blutils.c:219
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:942
uint64 dsa_pointer
Definition: dsa.h:62
uint64 chunk
#define PHJ_BATCH_SCAN
Definition: hashjoin.h:281
#define PHJ_BATCH_PROBE
Definition: hashjoin.h:280
#define PHJ_BATCH_FREE
Definition: hashjoin.h:282
ParallelHashJoinBatch * shared
Definition: hashjoin.h:209
dsa_pointer chunks
Definition: hashjoin.h:167
dsa_pointer buckets
Definition: hashjoin.h:164

References HashJoinTableData::area, Assert, BarrierArriveAndDetach(), BarrierArriveAndDetachExceptLast(), BarrierPhase(), ParallelHashJoinBatch::batch_barrier, HashJoinTableData::batches, ParallelHashJoinBatch::buckets, chunk, ParallelHashJoinBatch::chunks, HashJoinTableData::curbatch, dsa_free(), dsa_get_address(), DsaPointerIsValid, ParallelHashJoinBatchAccessor::inner_tuples, InvalidDsaPointer, Max, HashJoinTableData::nbuckets, next, ParallelHashJoinBatchAccessor::outer_eof, ParallelHashJoinBatchAccessor::outer_tuples, HashJoinTableData::parallel_state, PHJ_BATCH_FREE, PHJ_BATCH_PROBE, PHJ_BATCH_SCAN, ParallelHashJoinBatchAccessor::shared, ParallelHashJoinBatch::size, ParallelHashJoinBatch::skip_unmatched, HashJoinTableData::spacePeak, and sts_end_parallel_scan().

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

◆ ExecHashTableInsert()

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

Definition at line 1614 of file nodeHash.c.

1617{
1618 bool shouldFree;
1619 MinimalTuple tuple = ExecFetchSlotMinimalTuple(slot, &shouldFree);
1620 int bucketno;
1621 int batchno;
1622
1623 ExecHashGetBucketAndBatch(hashtable, hashvalue,
1624 &bucketno, &batchno);
1625
1626 /*
1627 * decide whether to put the tuple in the hash table or a temp file
1628 */
1629 if (batchno == hashtable->curbatch)
1630 {
1631 /*
1632 * put the tuple in hash table
1633 */
1634 HashJoinTuple hashTuple;
1635 int hashTupleSize;
1636 double ntuples = (hashtable->totalTuples - hashtable->skewTuples);
1637
1638 /* Create the HashJoinTuple */
1639 hashTupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
1640 hashTuple = (HashJoinTuple) dense_alloc(hashtable, hashTupleSize);
1641
1642 hashTuple->hashvalue = hashvalue;
1643 memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
1644
1645 /*
1646 * We always reset the tuple-matched flag on insertion. This is okay
1647 * even when reloading a tuple from a batch file, since the tuple
1648 * could not possibly have been matched to an outer tuple before it
1649 * went into the batch file.
1650 */
1652
1653 /* Push it onto the front of the bucket's list */
1654 hashTuple->next.unshared = hashtable->buckets.unshared[bucketno];
1655 hashtable->buckets.unshared[bucketno] = hashTuple;
1656
1657 /*
1658 * Increase the (optimal) number of buckets if we just exceeded the
1659 * NTUP_PER_BUCKET threshold, but only when there's still a single
1660 * batch.
1661 */
1662 if (hashtable->nbatch == 1 &&
1663 ntuples > (hashtable->nbuckets_optimal * NTUP_PER_BUCKET))
1664 {
1665 /* Guard against integer overflow and alloc size overflow */
1666 if (hashtable->nbuckets_optimal <= INT_MAX / 2 &&
1667 hashtable->nbuckets_optimal * 2 <= MaxAllocSize / sizeof(HashJoinTuple))
1668 {
1669 hashtable->nbuckets_optimal *= 2;
1670 hashtable->log2_nbuckets_optimal += 1;
1671 }
1672 }
1673
1674 /* Account for space used, and back off if we've used too much */
1675 hashtable->spaceUsed += hashTupleSize;
1676 if (hashtable->spaceUsed > hashtable->spacePeak)
1677 hashtable->spacePeak = hashtable->spaceUsed;
1678 if (hashtable->spaceUsed +
1679 hashtable->nbuckets_optimal * sizeof(HashJoinTuple)
1680 > hashtable->spaceAllowed)
1681 ExecHashIncreaseNumBatches(hashtable);
1682 }
1683 else
1684 {
1685 /*
1686 * put the tuple into a temp file for later batches
1687 */
1688 Assert(batchno > hashtable->curbatch);
1690 hashvalue,
1691 &hashtable->innerBatchFile[batchno],
1692 hashtable);
1693 }
1694
1695 if (shouldFree)
1697}
MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot, bool *shouldFree)
Definition: execTuples.c:1879
#define HJTUPLE_MINTUPLE(hjtup)
Definition: hashjoin.h:91
void heap_free_minimal_tuple(MinimalTuple mtup)
Definition: heaptuple.c:1524
#define HeapTupleHeaderClearMatch(tup)
Definition: htup_details.h:524
static void * dense_alloc(HashJoinTable hashtable, Size size)
Definition: nodeHash.c:2761
static void ExecHashIncreaseNumBatches(HashJoinTable hashtable)
Definition: nodeHash.c:899
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition: nodeHash.c:1825
void ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr, HashJoinTable hashtable)
union HashJoinTupleData::@105 next
uint32 hashvalue
Definition: hashjoin.h:86
struct HashJoinTupleData * unshared
Definition: hashjoin.h:83

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

◆ ExecHashTableReset()

void ExecHashTableReset ( HashJoinTable  hashtable)

Definition at line 2192 of file nodeHash.c.

2193{
2194 MemoryContext oldcxt;
2195 int nbuckets = hashtable->nbuckets;
2196
2197 /*
2198 * Release all the hash buckets and tuples acquired in the prior pass, and
2199 * reinitialize the context for a new pass.
2200 */
2201 MemoryContextReset(hashtable->batchCxt);
2202 oldcxt = MemoryContextSwitchTo(hashtable->batchCxt);
2203
2204 /* Reallocate and reinitialize the hash bucket headers. */
2205 hashtable->buckets.unshared = palloc0_array(HashJoinTuple, nbuckets);
2206
2207 hashtable->spaceUsed = 0;
2208
2209 MemoryContextSwitchTo(oldcxt);
2210
2211 /* Forget the chunks (the memory was freed by the context reset above). */
2212 hashtable->chunks = NULL;
2213}
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:383

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

Referenced by ExecHashJoinNewBatch().

◆ ExecHashTableResetMatchFlags()

void ExecHashTableResetMatchFlags ( HashJoinTable  hashtable)

Definition at line 2220 of file nodeHash.c.

2221{
2222 HashJoinTuple tuple;
2223 int i;
2224
2225 /* Reset all flags in the main table ... */
2226 for (i = 0; i < hashtable->nbuckets; i++)
2227 {
2228 for (tuple = hashtable->buckets.unshared[i]; tuple != NULL;
2229 tuple = tuple->next.unshared)
2231 }
2232
2233 /* ... and the same for the skew buckets, if any */
2234 for (i = 0; i < hashtable->nSkewBuckets; i++)
2235 {
2236 int j = hashtable->skewBucketNums[i];
2237 HashSkewBucket *skewBucket = hashtable->skewBucket[j];
2238
2239 for (tuple = skewBucket->tuples; tuple != NULL; tuple = tuple->next.unshared)
2241 }
2242}
int j
Definition: isn.c:73
HashJoinTuple tuples
Definition: hashjoin.h:116

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

Referenced by ExecReScanHashJoin().

◆ ExecInitHash()

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

Definition at line 370 of file nodeHash.c.

371{
372 HashState *hashstate;
373
374 /* check for unsupported flags */
376
377 /*
378 * create state structure
379 */
380 hashstate = makeNode(HashState);
381 hashstate->ps.plan = (Plan *) node;
382 hashstate->ps.state = estate;
383 hashstate->ps.ExecProcNode = ExecHash;
384 /* delay building hashtable until ExecHashTableCreate() in executor run */
385 hashstate->hashtable = NULL;
386
387 /*
388 * Miscellaneous initialization
389 *
390 * create expression context for node
391 */
392 ExecAssignExprContext(estate, &hashstate->ps);
393
394 /*
395 * initialize child nodes
396 */
397 outerPlanState(hashstate) = ExecInitNode(outerPlan(node), estate, eflags);
398
399 /*
400 * initialize our result slot and type. No need to build projection
401 * because this node doesn't do projections.
402 */
404 hashstate->ps.ps_ProjInfo = NULL;
405
406 Assert(node->plan.qual == NIL);
407
408 /*
409 * Delay initialization of hash_expr until ExecInitHashJoin(). We cannot
410 * build the ExprState here as we don't yet know the join type we're going
411 * to be hashing values for and we need to know that before calling
412 * ExecBuildHash32Expr as the keep_nulls parameter depends on the join
413 * type.
414 */
415 hashstate->hash_expr = NULL;
416
417 return hashstate;
418}
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1986
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:485
#define EXEC_FLAG_BACKWARD
Definition: executor.h:68
#define EXEC_FLAG_MARK
Definition: executor.h:69
static TupleTableSlot * ExecHash(PlanState *pstate)
Definition: nodeHash.c:91
#define makeNode(_type_)
Definition: nodes.h:155
#define NIL
Definition: pg_list.h:68
HashJoinTable hashtable
Definition: execnodes.h:2777
ExprState * hash_expr
Definition: execnodes.h:2778
EState * state
Definition: execnodes.h:1127
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1165
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1131
List * qual
Definition: plannodes.h:154

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

Referenced by ExecInitNode().

◆ ExecParallelHashTableAlloc()

void ExecParallelHashTableAlloc ( HashJoinTable  hashtable,
int  batchno 
)

Definition at line 3154 of file nodeHash.c.

3155{
3156 ParallelHashJoinBatch *batch = hashtable->batches[batchno].shared;
3157 dsa_pointer_atomic *buckets;
3158 int nbuckets = hashtable->parallel_state->nbuckets;
3159 int i;
3160
3161 batch->buckets =
3162 dsa_allocate(hashtable->area, sizeof(dsa_pointer_atomic) * nbuckets);
3163 buckets = (dsa_pointer_atomic *)
3164 dsa_get_address(hashtable->area, batch->buckets);
3165 for (i = 0; i < nbuckets; ++i)
3167}
#define dsa_pointer_atomic_init
Definition: dsa.h:64
#define dsa_allocate(area, size)
Definition: dsa.h:109

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

◆ ExecParallelHashTableInsert()

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

Definition at line 1704 of file nodeHash.c.

1707{
1708 bool shouldFree;
1709 MinimalTuple tuple = ExecFetchSlotMinimalTuple(slot, &shouldFree);
1710 dsa_pointer shared;
1711 int bucketno;
1712 int batchno;
1713
1714retry:
1715 ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno, &batchno);
1716
1717 if (batchno == 0)
1718 {
1719 HashJoinTuple hashTuple;
1720
1721 /* Try to load it into memory. */
1724 hashTuple = ExecParallelHashTupleAlloc(hashtable,
1725 HJTUPLE_OVERHEAD + tuple->t_len,
1726 &shared);
1727 if (hashTuple == NULL)
1728 goto retry;
1729
1730 /* Store the hash value in the HashJoinTuple header. */
1731 hashTuple->hashvalue = hashvalue;
1732 memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
1734
1735 /* Push it onto the front of the bucket's list */
1736 ExecParallelHashPushTuple(&hashtable->buckets.shared[bucketno],
1737 hashTuple, shared);
1738 }
1739 else
1740 {
1741 size_t tuple_size = MAXALIGN(HJTUPLE_OVERHEAD + tuple->t_len);
1742
1743 Assert(batchno > 0);
1744
1745 /* Try to preallocate space in the batch if necessary. */
1746 if (hashtable->batches[batchno].preallocated < tuple_size)
1747 {
1748 if (!ExecParallelHashTuplePrealloc(hashtable, batchno, tuple_size))
1749 goto retry;
1750 }
1751
1752 Assert(hashtable->batches[batchno].preallocated >= tuple_size);
1753 hashtable->batches[batchno].preallocated -= tuple_size;
1754 sts_puttuple(hashtable->batches[batchno].inner_tuples, &hashvalue,
1755 tuple);
1756 }
1757 ++hashtable->batches[batchno].ntuples;
1758
1759 if (shouldFree)
1761}
#define PHJ_BUILD_HASH_INNER
Definition: hashjoin.h:271
static bool ExecParallelHashTuplePrealloc(HashJoinTable hashtable, int batchno, size_t size)
Definition: nodeHash.c:3426
static HashJoinTuple ExecParallelHashTupleAlloc(HashJoinTable hashtable, size_t size, dsa_pointer *shared)
Definition: nodeHash.c:2841
static void ExecParallelHashPushTuple(dsa_pointer_atomic *head, HashJoinTuple tuple, dsa_pointer tuple_shared)
Definition: nodeHash.c:3346
void sts_puttuple(SharedTuplestoreAccessor *accessor, void *meta_data, MinimalTuple tuple)
dsa_pointer_atomic * shared
Definition: hashjoin.h:313

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

Referenced by MultiExecParallelHash().

◆ ExecParallelHashTableInsertCurrentBatch()

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

Definition at line 1770 of file nodeHash.c.

1773{
1774 bool shouldFree;
1775 MinimalTuple tuple = ExecFetchSlotMinimalTuple(slot, &shouldFree);
1776 HashJoinTuple hashTuple;
1777 dsa_pointer shared;
1778 int batchno;
1779 int bucketno;
1780
1781 ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno, &batchno);
1782 Assert(batchno == hashtable->curbatch);
1783 hashTuple = ExecParallelHashTupleAlloc(hashtable,
1784 HJTUPLE_OVERHEAD + tuple->t_len,
1785 &shared);
1786 hashTuple->hashvalue = hashvalue;
1787 memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
1789 ExecParallelHashPushTuple(&hashtable->buckets.shared[bucketno],
1790 hashTuple, shared);
1791
1792 if (shouldFree)
1794}

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

◆ ExecParallelHashTableSetCurrentBatch()

void ExecParallelHashTableSetCurrentBatch ( HashJoinTable  hashtable,
int  batchno 
)

◆ ExecParallelPrepHashTableForUnmatched()

bool ExecParallelPrepHashTableForUnmatched ( HashJoinState hjstate)

Definition at line 1990 of file nodeHash.c.

1991{
1992 HashJoinTable hashtable = hjstate->hj_HashTable;
1993 int curbatch = hashtable->curbatch;
1994 ParallelHashJoinBatch *batch = hashtable->batches[curbatch].shared;
1995
1997
1998 /*
1999 * It would not be deadlock-free to wait on the batch barrier, because it
2000 * is in PHJ_BATCH_PROBE phase, and thus processes attached to it have
2001 * already emitted tuples. Therefore, we'll hold a wait-free election:
2002 * only one process can continue to the next phase, and all others detach
2003 * from this batch. They can still go any work on other batches, if there
2004 * are any.
2005 */
2007 {
2008 /* This process considers the batch to be done. */
2009 hashtable->batches[hashtable->curbatch].done = true;
2010
2011 /* Make sure any temporary files are closed. */
2012 sts_end_parallel_scan(hashtable->batches[curbatch].inner_tuples);
2013 sts_end_parallel_scan(hashtable->batches[curbatch].outer_tuples);
2014
2015 /*
2016 * Track largest batch we've seen, which would normally happen in
2017 * ExecHashTableDetachBatch().
2018 */
2019 hashtable->spacePeak =
2020 Max(hashtable->spacePeak,
2021 batch->size + sizeof(dsa_pointer_atomic) * hashtable->nbuckets);
2022 hashtable->curbatch = -1;
2023 return false;
2024 }
2025
2026 /* Now we are alone with this batch. */
2028
2029 /*
2030 * Has another process decided to give up early and command all processes
2031 * to skip the unmatched scan?
2032 */
2033 if (batch->skip_unmatched)
2034 {
2035 hashtable->batches[hashtable->curbatch].done = true;
2036 ExecHashTableDetachBatch(hashtable);
2037 return false;
2038 }
2039
2040 /* Now prepare the process local state, just as for non-parallel join. */
2042
2043 return true;
2044}
void ExecHashTableDetachBatch(HashJoinTable hashtable)
Definition: nodeHash.c:3174
void ExecPrepHashTableForUnmatched(HashJoinState *hjstate)
Definition: nodeHash.c:1969
HashJoinTable hj_HashTable
Definition: execnodes.h:2224

References Assert, BarrierArriveAndDetachExceptLast(), BarrierPhase(), ParallelHashJoinBatch::batch_barrier, HashJoinTableData::batches, HashJoinTableData::curbatch, ParallelHashJoinBatchAccessor::done, ExecHashTableDetachBatch(), ExecPrepHashTableForUnmatched(), HashJoinState::hj_HashTable, ParallelHashJoinBatchAccessor::inner_tuples, Max, HashJoinTableData::nbuckets, ParallelHashJoinBatchAccessor::outer_tuples, PHJ_BATCH_PROBE, PHJ_BATCH_SCAN, ParallelHashJoinBatchAccessor::shared, ParallelHashJoinBatch::size, ParallelHashJoinBatch::skip_unmatched, HashJoinTableData::spacePeak, and sts_end_parallel_scan().

Referenced by ExecHashJoinImpl().

◆ ExecParallelScanHashBucket()

bool ExecParallelScanHashBucket ( HashJoinState hjstate,
ExprContext econtext 
)

Definition at line 1918 of file nodeHash.c.

1920{
1921 ExprState *hjclauses = hjstate->hashclauses;
1922 HashJoinTable hashtable = hjstate->hj_HashTable;
1923 HashJoinTuple hashTuple = hjstate->hj_CurTuple;
1924 uint32 hashvalue = hjstate->hj_CurHashValue;
1925
1926 /*
1927 * hj_CurTuple is the address of the tuple last returned from the current
1928 * bucket, or NULL if it's time to start scanning a new bucket.
1929 */
1930 if (hashTuple != NULL)
1931 hashTuple = ExecParallelHashNextTuple(hashtable, hashTuple);
1932 else
1933 hashTuple = ExecParallelHashFirstTuple(hashtable,
1934 hjstate->hj_CurBucketNo);
1935
1936 while (hashTuple != NULL)
1937 {
1938 if (hashTuple->hashvalue == hashvalue)
1939 {
1940 TupleTableSlot *inntuple;
1941
1942 /* insert hashtable's tuple into exec slot so ExecQual sees it */
1943 inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
1944 hjstate->hj_HashTupleSlot,
1945 false); /* do not pfree */
1946 econtext->ecxt_innertuple = inntuple;
1947
1948 if (ExecQualAndReset(hjclauses, econtext))
1949 {
1950 hjstate->hj_CurTuple = hashTuple;
1951 return true;
1952 }
1953 }
1954
1955 hashTuple = ExecParallelHashNextTuple(hashtable, hashTuple);
1956 }
1957
1958 /*
1959 * no match
1960 */
1961 return false;
1962}
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1633
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
Definition: executor.h:453
static HashJoinTuple ExecParallelHashFirstTuple(HashJoinTable hashtable, int bucketno)
Definition: nodeHash.c:3316
static HashJoinTuple ExecParallelHashNextTuple(HashJoinTable hashtable, HashJoinTuple tuple)
Definition: nodeHash.c:3332
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:260
HashJoinTuple hj_CurTuple
Definition: execnodes.h:2228
ExprState * hashclauses
Definition: execnodes.h:2222
uint32 hj_CurHashValue
Definition: execnodes.h:2225
int hj_CurBucketNo
Definition: execnodes.h:2226
TupleTableSlot * hj_HashTupleSlot
Definition: execnodes.h:2230

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

◆ ExecParallelScanHashTableForUnmatched()

bool ExecParallelScanHashTableForUnmatched ( HashJoinState hjstate,
ExprContext econtext 
)

Definition at line 2129 of file nodeHash.c.

2131{
2132 HashJoinTable hashtable = hjstate->hj_HashTable;
2133 HashJoinTuple hashTuple = hjstate->hj_CurTuple;
2134
2135 for (;;)
2136 {
2137 /*
2138 * hj_CurTuple is the address of the tuple last returned from the
2139 * current bucket, or NULL if it's time to start scanning a new
2140 * bucket.
2141 */
2142 if (hashTuple != NULL)
2143 hashTuple = ExecParallelHashNextTuple(hashtable, hashTuple);
2144 else if (hjstate->hj_CurBucketNo < hashtable->nbuckets)
2145 hashTuple = ExecParallelHashFirstTuple(hashtable,
2146 hjstate->hj_CurBucketNo++);
2147 else
2148 break; /* finished all buckets */
2149
2150 while (hashTuple != NULL)
2151 {
2153 {
2154 TupleTableSlot *inntuple;
2155
2156 /* insert hashtable's tuple into exec slot */
2157 inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
2158 hjstate->hj_HashTupleSlot,
2159 false); /* do not pfree */
2160 econtext->ecxt_innertuple = inntuple;
2161
2162 /*
2163 * Reset temp memory each time; although this function doesn't
2164 * do any qual eval, the caller will, so let's keep it
2165 * parallel to ExecScanHashBucket.
2166 */
2167 ResetExprContext(econtext);
2168
2169 hjstate->hj_CurTuple = hashTuple;
2170 return true;
2171 }
2172
2173 hashTuple = ExecParallelHashNextTuple(hashtable, hashTuple);
2174 }
2175
2176 /* allow this loop to be cancellable */
2178 }
2179
2180 /*
2181 * no more unmatched tuples
2182 */
2183 return false;
2184}
#define ResetExprContext(econtext)
Definition: executor.h:557
#define HeapTupleHeaderHasMatch(tup)
Definition: htup_details.h:514
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122

References CHECK_FOR_INTERRUPTS, ExprContext::ecxt_innertuple, ExecParallelHashFirstTuple(), ExecParallelHashNextTuple(), ExecStoreMinimalTuple(), HeapTupleHeaderHasMatch, HashJoinState::hj_CurBucketNo, HashJoinState::hj_CurTuple, HashJoinState::hj_HashTable, HashJoinState::hj_HashTupleSlot, HJTUPLE_MINTUPLE, HashJoinTableData::nbuckets, and ResetExprContext.

Referenced by ExecHashJoinImpl().

◆ ExecPrepHashTableForUnmatched()

void ExecPrepHashTableForUnmatched ( HashJoinState hjstate)

Definition at line 1969 of file nodeHash.c.

1970{
1971 /*----------
1972 * During this scan we use the HashJoinState fields as follows:
1973 *
1974 * hj_CurBucketNo: next regular bucket to scan
1975 * hj_CurSkewBucketNo: next skew bucket (an index into skewBucketNums)
1976 * hj_CurTuple: last tuple returned, or NULL to start next bucket
1977 *----------
1978 */
1979 hjstate->hj_CurBucketNo = 0;
1980 hjstate->hj_CurSkewBucketNo = 0;
1981 hjstate->hj_CurTuple = NULL;
1982}
int hj_CurSkewBucketNo
Definition: execnodes.h:2227

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

Referenced by ExecHashJoinImpl(), and ExecParallelPrepHashTableForUnmatched().

◆ ExecReScanHash()

void ExecReScanHash ( HashState node)

Definition at line 2246 of file nodeHash.c.

2247{
2249
2250 /*
2251 * if chgParam of subnode is not null then plan will be re-scanned by
2252 * first ExecProcNode.
2253 */
2254 if (outerPlan->chgParam == NULL)
2256}
void ExecReScan(PlanState *node)
Definition: execAmi.c:76

References ExecReScan(), outerPlan, and outerPlanState.

Referenced by ExecReScan().

◆ ExecScanHashBucket()

bool ExecScanHashBucket ( HashJoinState hjstate,
ExprContext econtext 
)

Definition at line 1857 of file nodeHash.c.

1859{
1860 ExprState *hjclauses = hjstate->hashclauses;
1861 HashJoinTable hashtable = hjstate->hj_HashTable;
1862 HashJoinTuple hashTuple = hjstate->hj_CurTuple;
1863 uint32 hashvalue = hjstate->hj_CurHashValue;
1864
1865 /*
1866 * hj_CurTuple is the address of the tuple last returned from the current
1867 * bucket, or NULL if it's time to start scanning a new bucket.
1868 *
1869 * If the tuple hashed to a skew bucket then scan the skew bucket
1870 * otherwise scan the standard hashtable bucket.
1871 */
1872 if (hashTuple != NULL)
1873 hashTuple = hashTuple->next.unshared;
1874 else if (hjstate->hj_CurSkewBucketNo != INVALID_SKEW_BUCKET_NO)
1875 hashTuple = hashtable->skewBucket[hjstate->hj_CurSkewBucketNo]->tuples;
1876 else
1877 hashTuple = hashtable->buckets.unshared[hjstate->hj_CurBucketNo];
1878
1879 while (hashTuple != NULL)
1880 {
1881 if (hashTuple->hashvalue == hashvalue)
1882 {
1883 TupleTableSlot *inntuple;
1884
1885 /* insert hashtable's tuple into exec slot so ExecQual sees it */
1886 inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
1887 hjstate->hj_HashTupleSlot,
1888 false); /* do not pfree */
1889 econtext->ecxt_innertuple = inntuple;
1890
1891 if (ExecQualAndReset(hjclauses, econtext))
1892 {
1893 hjstate->hj_CurTuple = hashTuple;
1894 return true;
1895 }
1896 }
1897
1898 hashTuple = hashTuple->next.unshared;
1899 }
1900
1901 /*
1902 * no match
1903 */
1904 return false;
1905}

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

◆ ExecScanHashTableForUnmatched()

bool ExecScanHashTableForUnmatched ( HashJoinState hjstate,
ExprContext econtext 
)

Definition at line 2055 of file nodeHash.c.

2056{
2057 HashJoinTable hashtable = hjstate->hj_HashTable;
2058 HashJoinTuple hashTuple = hjstate->hj_CurTuple;
2059
2060 for (;;)
2061 {
2062 /*
2063 * hj_CurTuple is the address of the tuple last returned from the
2064 * current bucket, or NULL if it's time to start scanning a new
2065 * bucket.
2066 */
2067 if (hashTuple != NULL)
2068 hashTuple = hashTuple->next.unshared;
2069 else if (hjstate->hj_CurBucketNo < hashtable->nbuckets)
2070 {
2071 hashTuple = hashtable->buckets.unshared[hjstate->hj_CurBucketNo];
2072 hjstate->hj_CurBucketNo++;
2073 }
2074 else if (hjstate->hj_CurSkewBucketNo < hashtable->nSkewBuckets)
2075 {
2076 int j = hashtable->skewBucketNums[hjstate->hj_CurSkewBucketNo];
2077
2078 hashTuple = hashtable->skewBucket[j]->tuples;
2079 hjstate->hj_CurSkewBucketNo++;
2080 }
2081 else
2082 break; /* finished all buckets */
2083
2084 while (hashTuple != NULL)
2085 {
2087 {
2088 TupleTableSlot *inntuple;
2089
2090 /* insert hashtable's tuple into exec slot */
2091 inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
2092 hjstate->hj_HashTupleSlot,
2093 false); /* do not pfree */
2094 econtext->ecxt_innertuple = inntuple;
2095
2096 /*
2097 * Reset temp memory each time; although this function doesn't
2098 * do any qual eval, the caller will, so let's keep it
2099 * parallel to ExecScanHashBucket.
2100 */
2101 ResetExprContext(econtext);
2102
2103 hjstate->hj_CurTuple = hashTuple;
2104 return true;
2105 }
2106
2107 hashTuple = hashTuple->next.unshared;
2108 }
2109
2110 /* allow this loop to be cancellable */
2112 }
2113
2114 /*
2115 * no more unmatched tuples
2116 */
2117 return false;
2118}

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, j, HashJoinTableData::nbuckets, HashJoinTupleData::next, HashJoinTableData::nSkewBuckets, ResetExprContext, HashJoinTableData::skewBucket, HashJoinTableData::skewBucketNums, HashSkewBucket::tuples, HashJoinTupleData::unshared, and HashJoinTableData::unshared.

Referenced by ExecHashJoinImpl().

◆ ExecShutdownHash()

void ExecShutdownHash ( HashState node)

Definition at line 2696 of file nodeHash.c.

2697{
2698 /* Allocate save space if EXPLAIN'ing and we didn't do so already */
2699 if (node->ps.instrument && !node->hinstrument)
2701 /* Now accumulate data for the current (final) hash table */
2702 if (node->hinstrument && node->hashtable)
2704}
#define palloc0_object(type)
Definition: fe_memutils.h:75
void ExecHashAccumInstrumentation(HashInstrumentation *instrument, HashJoinTable hashtable)
Definition: nodeHash.c:2742

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

Referenced by ExecShutdownNode_walker().

◆ MultiExecHash()

Node * MultiExecHash ( HashState node)

Definition at line 105 of file nodeHash.c.

106{
107 /* must provide our own instrumentation support */
108 if (node->ps.instrument)
110
111 if (node->parallel_state != NULL)
113 else
115
116 /* must provide our own instrumentation support */
117 if (node->ps.instrument)
119
120 /*
121 * We do not return the hash table directly because it's not a subtype of
122 * Node, and so would violate the MultiExecProcNode API. Instead, our
123 * parent Hashjoin node is expected to know how to fish it out of our node
124 * state. Ugly but not really worth cleaning up, since Hashjoin knows
125 * quite a bit more about Hash besides that.
126 */
127 return NULL;
128}
void InstrStartNode(Instrumentation *instr)
Definition: instrument.c:68
void InstrStopNode(Instrumentation *instr, double nTuples)
Definition: instrument.c:84
static void MultiExecParallelHash(HashState *node)
Definition: nodeHash.c:219
static void MultiExecPrivateHash(HashState *node)
Definition: nodeHash.c:138
struct ParallelHashJoinState * parallel_state
Definition: execnodes.h:2799

References HashState::hashtable, InstrStartNode(), InstrStopNode(), PlanState::instrument, MultiExecParallelHash(), MultiExecPrivateHash(), HashState::parallel_state, HashJoinTableData::partialTuples, and HashState::ps.

Referenced by MultiExecProcNode().