PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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)
 
TuplestorestateExecHashBuildNullTupleStore (HashJoinTable hashtable)
 
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 
)
extern

Definition at line 683 of file nodeHash.c.

690{
691 int tupsize;
692 double inner_rel_bytes;
693 size_t hash_table_bytes;
694 size_t bucket_bytes;
695 size_t max_pointers;
696 int nbatch = 1;
697 int nbuckets;
698 double dbuckets;
699
700 /* Force a plausible relation size if no info */
701 if (ntuples <= 0.0)
702 ntuples = 1000.0;
703
704 /*
705 * Estimate tupsize based on footprint of tuple in hashtable... note this
706 * does not allow for any palloc overhead. The manipulations of spaceUsed
707 * don't count palloc overhead either.
708 */
712 inner_rel_bytes = ntuples * tupsize;
713
714 /*
715 * Compute in-memory hashtable size limit from GUCs.
716 */
718
719 /*
720 * Parallel Hash tries to use the combined hash_mem of all workers to
721 * avoid the need to batch. If that won't work, it falls back to hash_mem
722 * per worker and tries to process batches in parallel.
723 */
725 {
726 /* Careful, this could overflow size_t */
727 double newlimit;
728
729 newlimit = (double) hash_table_bytes * (double) (parallel_workers + 1);
730 newlimit = Min(newlimit, (double) SIZE_MAX);
732 }
733
734 *space_allowed = hash_table_bytes;
735
736 /*
737 * If skew optimization is possible, estimate the number of skew buckets
738 * that will fit in the memory allowed, and decrement the assumed space
739 * available for the main hash table accordingly.
740 *
741 * We make the optimistic assumption that each skew bucket will contain
742 * one inner-relation tuple. If that turns out to be low, we will recover
743 * at runtime by reducing the number of skew buckets.
744 *
745 * hashtable->skewBucket will have up to 8 times as many HashSkewBucket
746 * pointers as the number of MCVs we allow, since ExecHashBuildSkewHash
747 * will round up to the next power of 2 and then multiply by 4 to reduce
748 * collisions.
749 */
750 if (useskew)
751 {
752 size_t bytes_per_mcv;
753 size_t skew_mcvs;
754
755 /*----------
756 * Compute number of MCVs we could hold in hash_table_bytes
757 *
758 * Divisor is:
759 * size of a hash tuple +
760 * worst-case size of skewBucket[] per MCV +
761 * size of skewBucketNums[] entry +
762 * size of skew bucket struct itself
763 *----------
764 */
766 (8 * sizeof(HashSkewBucket *)) +
767 sizeof(int) +
770
771 /*
772 * Now scale by SKEW_HASH_MEM_PERCENT (we do it in this order so as
773 * not to worry about size_t overflow in the multiplication)
774 */
776
777 /* Now clamp to integer range */
779
781
782 /* Reduce hash_table_bytes by the amount needed for the skew table */
783 if (skew_mcvs > 0)
785 }
786 else
787 *num_skew_mcvs = 0;
788
789 /*
790 * Set nbuckets to achieve an average bucket load of NTUP_PER_BUCKET when
791 * memory is filled, assuming a single batch; but limit the value so that
792 * the pointer arrays we'll try to allocate do not exceed hash_table_bytes
793 * nor MaxAllocSize.
794 *
795 * Note that both nbuckets and nbatch must be powers of 2 to make
796 * ExecHashGetBucketAndBatch fast.
797 */
800 /* If max_pointers isn't a power of 2, must round it down to one */
802
803 /* Also ensure we avoid integer overflow in nbatch and nbuckets */
804 /* (this step is redundant given the current value of MaxAllocSize) */
806
807 dbuckets = ceil(ntuples / NTUP_PER_BUCKET);
809 nbuckets = (int) dbuckets;
810 /* don't let nbuckets be really small, though ... */
811 nbuckets = Max(nbuckets, 1024);
812 /* ... and force it to be a power of 2. */
813 nbuckets = pg_nextpower2_32(nbuckets);
814
815 /*
816 * If there's not enough space to store the projected number of tuples and
817 * the required bucket headers, we will need multiple batches.
818 */
819 bucket_bytes = sizeof(HashJoinTuple) * nbuckets;
821 {
822 /* We'll need multiple batches */
823 size_t sbuckets;
824 double dbatch;
825 int minbatch;
826 size_t bucket_size;
827
828 /*
829 * If Parallel Hash with combined hash_mem would still need multiple
830 * batches, we'll have to fall back to regular hash_mem budget.
831 */
833 {
835 false, parallel_workers,
836 space_allowed,
837 numbuckets,
838 numbatches,
840 return;
841 }
842
843 /*
844 * Estimate the number of buckets we'll want to have when hash_mem is
845 * entirely full. Each bucket will contain a bucket pointer plus
846 * NTUP_PER_BUCKET tuples, whose projected size already includes
847 * overhead for the hash code, pointer to the next tuple, etc.
848 */
851 sbuckets = 1; /* avoid pg_nextpower2_size_t(0) */
852 else
855 nbuckets = (int) sbuckets;
856 nbuckets = pg_nextpower2_32(nbuckets);
857 bucket_bytes = nbuckets * sizeof(HashJoinTuple);
858
859 /*
860 * Buckets are simple pointers to hashjoin tuples, while tupsize
861 * includes the pointer, hash code, and MinimalTupleData. So buckets
862 * should never really exceed 25% of hash_mem (even for
863 * NTUP_PER_BUCKET=1); except maybe for hash_mem values that are not
864 * 2^N bytes, where we might get more because of doubling. So let's
865 * look for 50% here.
866 */
868
869 /* Calculate required number of batches. */
872 minbatch = (int) dbatch;
873 nbatch = pg_nextpower2_32(Max(2, minbatch));
874 }
875
876 /*
877 * Optimize the total amount of memory consumed by the hash node.
878 *
879 * The nbatch calculation above focuses on the in-memory hash table,
880 * assuming no per-batch overhead. But each batch may have two files, each
881 * with a BLCKSZ buffer. For large nbatch values these buffers may use
882 * significantly more memory than the hash table.
883 *
884 * The total memory usage may be expressed by this formula:
885 *
886 * (inner_rel_bytes / nbatch) + (2 * nbatch * BLCKSZ)
887 *
888 * where (inner_rel_bytes / nbatch) is the size of the in-memory hash
889 * table and (2 * nbatch * BLCKSZ) is the amount of memory used by file
890 * buffers.
891 *
892 * The nbatch calculation however ignores the second part. And for very
893 * large inner_rel_bytes, there may be no nbatch that keeps total memory
894 * usage under the budget (work_mem * hash_mem_multiplier). To deal with
895 * that, we will adjust nbatch to minimize total memory consumption across
896 * both the hashtable and file buffers.
897 *
898 * As we increase the size of the hashtable, the number of batches
899 * decreases, and the total memory usage follows a U-shaped curve. We find
900 * the minimum nbatch by "walking back" -- checking if halving nbatch
901 * would lower the total memory usage. We stop when it no longer helps.
902 *
903 * We only reduce the number of batches. Adding batches reduces memory
904 * usage only when most of the memory is used by the hash table, with
905 * total memory usage within the limit or not far from it. We don't want
906 * to start batching when not needed, even if that would reduce memory
907 * usage.
908 *
909 * While growing the hashtable, we also adjust the number of buckets to
910 * maintain a load factor of NTUP_PER_BUCKET while squeezing tuples back
911 * from batches into the hashtable.
912 *
913 * Note that we can only change nbuckets during initial hashtable sizing.
914 * Once we start building the hash, nbuckets is fixed (we may still grow
915 * the hash table).
916 *
917 * We double several parameters (space_allowed, nbuckets, num_skew_mcvs),
918 * which introduces a risk of overflow. We avoid this by exiting the loop.
919 * We could do something smarter (e.g. capping nbuckets and continue), but
920 * the complexity is not worth it. Such cases are extremely rare, and this
921 * is a best-effort attempt to reduce memory usage.
922 */
923 while (nbatch > 1)
924 {
925 /* Check that buckets won't overflow MaxAllocSize */
926 if (nbuckets > (MaxAllocSize / sizeof(HashJoinTuple) / 2))
927 break;
928
929 /* num_skew_mcvs should be less than nbuckets */
930 Assert((*num_skew_mcvs) < (INT_MAX / 2));
931
932 /*
933 * Check that space_allowed won't overflow SIZE_MAX.
934 *
935 * We don't use hash_table_bytes here, because it does not include the
936 * skew buckets. And we want to limit the overall memory limit.
937 */
938 if ((*space_allowed) > (SIZE_MAX / 2))
939 break;
940
941 /*
942 * Will halving the number of batches and doubling the size of the
943 * hashtable reduce overall memory usage?
944 *
945 * This is the same as (S = space_allowed):
946 *
947 * (S + 2 * nbatch * BLCKSZ) < (S * 2 + nbatch * BLCKSZ)
948 *
949 * but avoiding intermediate overflow.
950 */
951 if (nbatch < (*space_allowed) / BLCKSZ)
952 break;
953
954 /*
955 * MaxAllocSize is sufficiently small that we are not worried about
956 * overflowing nbuckets.
957 */
958 nbuckets *= 2;
959
960 *num_skew_mcvs = (*num_skew_mcvs) * 2;
961 *space_allowed = (*space_allowed) * 2;
962
963 nbatch /= 2;
964 }
965
966 Assert(nbuckets > 0);
967 Assert(nbatch > 0);
968
969 *numbuckets = nbuckets;
970 *numbatches = nbatch;
971}
#define Min(x, y)
Definition c.h:1093
#define MAXALIGN(LEN)
Definition c.h:898
#define Max(x, y)
Definition c.h:1087
#define Assert(condition)
Definition c.h:945
struct HashJoinTupleData * HashJoinTuple
Definition execnodes.h:2215
#define MaxAllocSize
Definition fe_memutils.h:22
#define HJTUPLE_OVERHEAD
Definition hashjoin.h:101
#define SKEW_BUCKET_OVERHEAD
Definition hashjoin.h:130
#define SKEW_HASH_MEM_PERCENT
Definition hashjoin.h:132
#define SizeofMinimalTupleHeader
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:683
#define NTUP_PER_BUCKET
Definition nodeHash.c:680
size_t get_hash_memory_limit(void)
Definition nodeHash.c:3680
static uint32 pg_nextpower2_32(uint32 num)
#define pg_nextpower2_size_t
#define pg_prevpower2_size_t
static int fb(int x)

References Assert, ExecChooseHashTableSize(), fb(), 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)
extern

Definition at line 452 of file nodeHash.c.

453{
455
456 /*
457 * shut down the subplan
458 */
461}
void ExecEndNode(PlanState *node)
#define outerPlanState(node)
Definition execnodes.h:1273
#define outerPlan(node)
Definition plannodes.h:265

References ExecEndNode(), outerPlan, and outerPlanState.

Referenced by ExecEndNode().

◆ ExecHashAccumInstrumentation()

void ExecHashAccumInstrumentation ( HashInstrumentation instrument,
HashJoinTable  hashtable 
)
extern

Definition at line 2935 of file nodeHash.c.

2937{
2938 instrument->nbuckets = Max(instrument->nbuckets,
2939 hashtable->nbuckets);
2940 instrument->nbuckets_original = Max(instrument->nbuckets_original,
2941 hashtable->nbuckets_original);
2942 instrument->nbatch = Max(instrument->nbatch,
2943 hashtable->nbatch);
2944 instrument->nbatch_original = Max(instrument->nbatch_original,
2945 hashtable->nbatch_original);
2946 instrument->space_peak = Max(instrument->space_peak,
2947 hashtable->spacePeak);
2948}

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

◆ ExecHashBuildNullTupleStore()

Tuplestorestate * ExecHashBuildNullTupleStore ( HashJoinTable  hashtable)
extern

Definition at line 2799 of file nodeHash.c.

2800{
2801 Tuplestorestate *tstore;
2803
2804 /*
2805 * We keep the tuplestore in the hashCxt to ensure it won't go away too
2806 * soon. Size it at work_mem/16 so that it doesn't bloat the node's space
2807 * consumption too much.
2808 */
2809 oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
2810 tstore = tuplestore_begin_heap(false, false, work_mem / 16);
2812 return tstore;
2813}
int work_mem
Definition globals.c:131
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
MemoryContext hashCxt
Definition hashjoin.h:368
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition tuplestore.c:331

References fb(), HashJoinTableData::hashCxt, MemoryContextSwitchTo(), tuplestore_begin_heap(), and work_mem.

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

◆ ExecHashEstimate()

void ExecHashEstimate ( HashState node,
ParallelContext pcxt 
)
extern

Definition at line 2819 of file nodeHash.c.

2820{
2821 size_t size;
2822
2823 /* don't need this if not instrumenting or no workers */
2824 if (!node->ps.instrument || pcxt->nworkers == 0)
2825 return;
2826
2827 size = mul_size(pcxt->nworkers, sizeof(HashInstrumentation));
2828 size = add_size(size, offsetof(SharedHashInfo, hinstrument));
2829 shm_toc_estimate_chunk(&pcxt->estimator, size);
2831}
#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:485
Size mul_size(Size s1, Size s2)
Definition shmem.c:500
PlanState ps
Definition execnodes.h:2665
shm_toc_estimator estimator
Definition parallel.h:43
Instrumentation * instrument
Definition execnodes.h:1187

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

Referenced by ExecParallelEstimate().

◆ ExecHashGetBucketAndBatch()

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

Definition at line 1986 of file nodeHash.c.

1990{
1991 uint32 nbuckets = (uint32) hashtable->nbuckets;
1992 uint32 nbatch = (uint32) hashtable->nbatch;
1993
1994 if (nbatch > 1)
1995 {
1996 *bucketno = hashvalue & (nbuckets - 1);
1997 *batchno = pg_rotate_right32(hashvalue,
1998 hashtable->log2_nbuckets) & (nbatch - 1);
1999 }
2000 else
2001 {
2002 *bucketno = hashvalue & (nbuckets - 1);
2003 *batchno = 0;
2004 }
2005}
uint32_t uint32
Definition c.h:618
static uint32 pg_rotate_right32(uint32 word, int n)

References fb(), 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 
)
extern

Definition at line 2581 of file nodeHash.c.

2582{
2583 int bucket;
2584
2585 /*
2586 * Always return INVALID_SKEW_BUCKET_NO if not doing skew optimization (in
2587 * particular, this happens after the initial batch is done).
2588 */
2589 if (!hashtable->skewEnabled)
2591
2592 /*
2593 * Since skewBucketLen is a power of 2, we can do a modulo by ANDing.
2594 */
2595 bucket = hashvalue & (hashtable->skewBucketLen - 1);
2596
2597 /*
2598 * While we have not hit a hole in the hashtable and have not hit the
2599 * desired bucket, we have collided with some other hash value, so try the
2600 * next bucket location.
2601 */
2602 while (hashtable->skewBucket[bucket] != NULL &&
2603 hashtable->skewBucket[bucket]->hashvalue != hashvalue)
2604 bucket = (bucket + 1) & (hashtable->skewBucketLen - 1);
2605
2606 /*
2607 * Found the desired bucket?
2608 */
2609 if (hashtable->skewBucket[bucket] != NULL)
2610 return bucket;
2611
2612 /*
2613 * There must not be any hashtable entry for this hash value.
2614 */
2616}
#define INVALID_SKEW_BUCKET_NO
Definition hashjoin.h:131
HashSkewBucket ** skewBucket
Definition hashjoin.h:328
uint32 hashvalue
Definition hashjoin.h:126

References fb(), 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 
)
extern

Definition at line 2838 of file nodeHash.c.

2839{
2840 size_t size;
2841
2842 /* don't need this if not instrumenting or no workers */
2843 if (!node->ps.instrument || pcxt->nworkers == 0)
2844 return;
2845
2846 size = offsetof(SharedHashInfo, hinstrument) +
2847 pcxt->nworkers * sizeof(HashInstrumentation);
2848 node->shared_info = (SharedHashInfo *) shm_toc_allocate(pcxt->toc, size);
2849
2850 /* Each per-worker area must start out as zeroes. */
2851 memset(node->shared_info, 0, size);
2852
2853 node->shared_info->num_workers = pcxt->nworkers;
2854 shm_toc_insert(pcxt->toc, node->ps.plan->plan_node_id,
2855 node->shared_info);
2856}
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:2681
shm_toc * toc
Definition parallel.h:46
Plan * plan
Definition execnodes.h:1177
int plan_node_id
Definition plannodes.h:231

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

Referenced by ExecParallelInitializeDSM().

◆ ExecHashInitializeWorker()

void ExecHashInitializeWorker ( HashState node,
ParallelWorkerContext pwcxt 
)
extern

Definition at line 2863 of file nodeHash.c.

2864{
2865 SharedHashInfo *shared_info;
2866
2867 /* don't need this if not instrumenting */
2868 if (!node->ps.instrument)
2869 return;
2870
2871 /*
2872 * Find our entry in the shared area, and set up a pointer to it so that
2873 * we'll accumulate stats there when shutting down or rebuilding the hash
2874 * table.
2875 */
2876 shared_info = (SharedHashInfo *)
2877 shm_toc_lookup(pwcxt->toc, node->ps.plan->plan_node_id, false);
2878 node->hinstrument = &shared_info->hinstrument[ParallelWorkerNumber];
2879}
int ParallelWorkerNumber
Definition parallel.c:117
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition shm_toc.c:232
HashInstrumentation * hinstrument
Definition execnodes.h:2688
HashInstrumentation hinstrument[FLEXIBLE_ARRAY_MEMBER]

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

Referenced by ExecParallelInitializeWorker().

◆ ExecHashRetrieveInstrumentation()

void ExecHashRetrieveInstrumentation ( HashState node)
extern

Definition at line 2904 of file nodeHash.c.

2905{
2906 SharedHashInfo *shared_info = node->shared_info;
2907 size_t size;
2908
2909 if (shared_info == NULL)
2910 return;
2911
2912 /* Replace node->shared_info with a copy in backend-local memory. */
2913 size = offsetof(SharedHashInfo, hinstrument) +
2914 shared_info->num_workers * sizeof(HashInstrumentation);
2915 node->shared_info = palloc(size);
2916 memcpy(node->shared_info, shared_info, size);
2917}
void * palloc(Size size)
Definition mcxt.c:1387

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

Referenced by ExecParallelRetrieveInstrumentation().

◆ ExecHashTableCreate()

HashJoinTable ExecHashTableCreate ( HashState state)
extern

Definition at line 471 of file nodeHash.c.

472{
473 Hash *node;
474 HashJoinTable hashtable;
476 size_t space_allowed;
477 int nbuckets;
478 int nbatch;
479 double rows;
480 int num_skew_mcvs;
481 int log2_nbuckets;
483
484 /*
485 * Get information about the size of the relation to be hashed (it's the
486 * "outer" subtree of this node, but the inner relation of the hashjoin).
487 * Compute the appropriate size of the hash table.
488 */
489 node = (Hash *) state->ps.plan;
490 outerNode = outerPlan(node);
491
492 /*
493 * If this is shared hash table with a partial plan, then we can't use
494 * outerNode->plan_rows to estimate its size. We need an estimate of the
495 * total number of rows across all copies of the partial plan.
496 */
497 rows = node->plan.parallel_aware ? node->rows_total : outerNode->plan_rows;
498
499 ExecChooseHashTableSize(rows, outerNode->plan_width,
500 OidIsValid(node->skewTable),
501 state->parallel_state != NULL,
502 state->parallel_state != NULL ?
503 state->parallel_state->nparticipants - 1 : 0,
504 &space_allowed,
505 &nbuckets, &nbatch, &num_skew_mcvs);
506
507 /* nbuckets must be a power of 2 */
508 log2_nbuckets = pg_ceil_log2_32(nbuckets);
509 Assert(nbuckets == (1 << log2_nbuckets));
510
511 /*
512 * Initialize the hash table control block.
513 *
514 * The hashtable control block is just palloc'd from the executor's
515 * per-query memory context. Everything else should be kept inside the
516 * subsidiary hashCxt, batchCxt or spillCxt.
517 */
518 hashtable = palloc_object(HashJoinTableData);
519 hashtable->nbuckets = nbuckets;
520 hashtable->nbuckets_original = nbuckets;
521 hashtable->nbuckets_optimal = nbuckets;
522 hashtable->log2_nbuckets = log2_nbuckets;
523 hashtable->log2_nbuckets_optimal = log2_nbuckets;
524 hashtable->buckets.unshared = NULL;
525 hashtable->skewEnabled = false;
526 hashtable->skewBucket = NULL;
527 hashtable->skewBucketLen = 0;
528 hashtable->nSkewBuckets = 0;
529 hashtable->skewBucketNums = NULL;
530 hashtable->nbatch = nbatch;
531 hashtable->curbatch = 0;
532 hashtable->nbatch_original = nbatch;
533 hashtable->nbatch_outstart = nbatch;
534 hashtable->growEnabled = true;
535 hashtable->totalTuples = 0;
536 hashtable->reportTuples = 0;
537 hashtable->skewTuples = 0;
538 hashtable->innerBatchFile = NULL;
539 hashtable->outerBatchFile = NULL;
540 hashtable->spaceUsed = 0;
541 hashtable->spacePeak = 0;
542 hashtable->spaceAllowed = space_allowed;
543 hashtable->spaceUsedSkew = 0;
544 hashtable->spaceAllowedSkew =
545 hashtable->spaceAllowed * SKEW_HASH_MEM_PERCENT / 100;
546 hashtable->chunks = NULL;
547 hashtable->current_chunk = NULL;
548 hashtable->parallel_state = state->parallel_state;
549 hashtable->area = state->ps.state->es_query_dsa;
550 hashtable->batches = NULL;
551
552#ifdef HJDEBUG
553 printf("Hashjoin %p: initial nbatch = %d, nbuckets = %d\n",
554 hashtable, nbatch, nbuckets);
555#endif
556
557 /*
558 * Create temporary memory contexts in which to keep the hashtable working
559 * storage. See notes in executor/hashjoin.h.
560 */
561 hashtable->hashCxt = AllocSetContextCreate(CurrentMemoryContext,
562 "HashTableContext",
564
565 hashtable->batchCxt = AllocSetContextCreate(hashtable->hashCxt,
566 "HashBatchContext",
568
569 hashtable->spillCxt = AllocSetContextCreate(hashtable->hashCxt,
570 "HashSpillContext",
572
573 /* Allocate data that will live for the life of the hashjoin */
574
575 oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
576
577 if (nbatch > 1 && hashtable->parallel_state == NULL)
578 {
580
581 /*
582 * allocate and initialize the file arrays in hashCxt (not needed for
583 * parallel case which uses shared tuplestores instead of raw files)
584 */
585 oldctx = MemoryContextSwitchTo(hashtable->spillCxt);
586
587 hashtable->innerBatchFile = palloc0_array(BufFile *, nbatch);
588 hashtable->outerBatchFile = palloc0_array(BufFile *, nbatch);
589
591
592 /* The files will not be opened until needed... */
593 /* ... but make sure we have temp tablespaces established for them */
595 }
596
598
599 if (hashtable->parallel_state)
600 {
601 ParallelHashJoinState *pstate = hashtable->parallel_state;
602 Barrier *build_barrier;
603
604 /*
605 * Attach to the build barrier. The corresponding detach operation is
606 * in ExecHashTableDetach. Note that we won't attach to the
607 * batch_barrier for batch 0 yet. We'll attach later and start it out
608 * in PHJ_BATCH_PROBE phase, because batch 0 is allocated up front and
609 * then loaded while hashing (the standard hybrid hash join
610 * algorithm), and we'll coordinate that using build_barrier.
611 */
612 build_barrier = &pstate->build_barrier;
613 BarrierAttach(build_barrier);
614
615 /*
616 * So far we have no idea whether there are any other participants,
617 * and if so, what phase they are working on. The only thing we care
618 * about at this point is whether someone has already created the
619 * SharedHashJoinBatch objects and the hash table for batch 0. One
620 * backend will be elected to do that now if necessary.
621 */
622 if (BarrierPhase(build_barrier) == PHJ_BUILD_ELECT &&
624 {
625 pstate->nbatch = nbatch;
626 pstate->space_allowed = space_allowed;
627 pstate->growth = PHJ_GROWTH_OK;
628
629 /* Set up the shared state for coordinating batches. */
630 ExecParallelHashJoinSetUpBatches(hashtable, nbatch);
631
632 /*
633 * Allocate batch 0's hash table up front so we can load it
634 * directly while hashing.
635 */
636 pstate->nbuckets = nbuckets;
637 ExecParallelHashTableAlloc(hashtable, 0);
638 }
639
640 /*
641 * The next Parallel Hash synchronization point is in
642 * MultiExecParallelHash(), which will progress it all the way to
643 * PHJ_BUILD_RUN. The caller must not return control from this
644 * executor node between now and then.
645 */
646 }
647 else
648 {
649 /*
650 * Prepare context for the first-scan space allocations; allocate the
651 * hashbucket array therein, and set each bucket "empty".
652 */
653 MemoryContextSwitchTo(hashtable->batchCxt);
654
655 hashtable->buckets.unshared = palloc0_array(HashJoinTuple, nbuckets);
656
657 /*
658 * Set up for skew optimization, if possible and there's a need for
659 * more than one batch. (In a one-batch join, there's no point in
660 * it.)
661 */
662 if (nbatch > 1)
663 ExecHashBuildSkewHash(state, hashtable, node, num_skew_mcvs);
664
666 }
667
668 return hashtable;
669}
void PrepareTempTablespaces(void)
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:860
#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:244
#define PHJ_BUILD_ELECT
Definition hashjoin.h:280
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#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:2429
static void ExecParallelHashJoinSetUpBatches(HashJoinTable hashtable, int nbatch)
Definition nodeHash.c:3182
void ExecParallelHashTableAlloc(HashJoinTable hashtable, int batchno)
Definition nodeHash.c:3347
static uint32 pg_ceil_log2_32(uint32 num)
#define printf(...)
Definition port.h:266
ParallelHashGrowth growth
Definition hashjoin.h:264

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(), fb(), HashJoinTableData::growEnabled, ParallelHashJoinState::growth, HashJoinTableData::hashCxt, HashJoinTableData::innerBatchFile, HashJoinTableData::log2_nbuckets, HashJoinTableData::log2_nbuckets_optimal, MemoryContextSwitchTo(), 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, pg_ceil_log2_32(), PHJ_BUILD_ELECT, PHJ_GROWTH_OK, Hash::plan, PrepareTempTablespaces(), printf, HashJoinTableData::reportTuples, 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)
extern

Definition at line 981 of file nodeHash.c.

982{
983 int i;
984
985 /*
986 * Make sure all the temp files are closed. We skip batch 0, since it
987 * can't have any temp files (and the arrays might not even exist if
988 * nbatch is only 1). Parallel hash joins don't use these files.
989 */
990 if (hashtable->innerBatchFile != NULL)
991 {
992 for (i = 1; i < hashtable->nbatch; i++)
993 {
994 if (hashtable->innerBatchFile[i])
995 BufFileClose(hashtable->innerBatchFile[i]);
996 if (hashtable->outerBatchFile[i])
997 BufFileClose(hashtable->outerBatchFile[i]);
998 }
999 }
1000
1001 /* Release working memory (batchCxt is a child, so it goes away too) */
1002 MemoryContextDelete(hashtable->hashCxt);
1003
1004 /* And drop the control block */
1005 pfree(hashtable);
1006}
void BufFileClose(BufFile *file)
Definition buffile.c:413
int i
Definition isn.c:77
void pfree(void *pointer)
Definition mcxt.c:1616
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472
BufFile ** innerBatchFile
Definition hashjoin.h:359
BufFile ** outerBatchFile
Definition hashjoin.h:360

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

Referenced by ExecEndHashJoin(), and ExecReScanHashJoin().

◆ ExecHashTableDetach()

void ExecHashTableDetach ( HashJoinTable  hashtable)
extern

Definition at line 3459 of file nodeHash.c.

3460{
3461 ParallelHashJoinState *pstate = hashtable->parallel_state;
3462
3463 /*
3464 * If we're involved in a parallel query, we must either have gotten all
3465 * the way to PHJ_BUILD_RUN, or joined too late and be in PHJ_BUILD_FREE.
3466 */
3467 Assert(!pstate ||
3469
3470 if (pstate && BarrierPhase(&pstate->build_barrier) == PHJ_BUILD_RUN)
3471 {
3472 int i;
3473
3474 /* Make sure any temporary files are closed. */
3475 if (hashtable->batches)
3476 {
3477 for (i = 0; i < hashtable->nbatch; ++i)
3478 {
3479 sts_end_write(hashtable->batches[i].inner_tuples);
3480 sts_end_write(hashtable->batches[i].outer_tuples);
3483 }
3484 }
3485
3486 /* If we're last to detach, clean up shared memory. */
3488 {
3489 /*
3490 * Late joining processes will see this state and give up
3491 * immediately.
3492 */
3494
3495 if (DsaPointerIsValid(pstate->batches))
3496 {
3497 dsa_free(hashtable->area, pstate->batches);
3498 pstate->batches = InvalidDsaPointer;
3499 }
3500 }
3501 }
3502 hashtable->parallel_state = NULL;
3503}
bool BarrierArriveAndDetach(Barrier *barrier)
Definition barrier.c:203
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition dsa.c:841
#define InvalidDsaPointer
Definition dsa.h:78
#define DsaPointerIsValid(x)
Definition dsa.h:106
#define PHJ_BUILD_FREE
Definition hashjoin.h:285
#define PHJ_BUILD_RUN
Definition hashjoin.h:284
void sts_end_write(SharedTuplestoreAccessor *accessor)
void sts_end_parallel_scan(SharedTuplestoreAccessor *accessor)
ParallelHashJoinBatchAccessor * batches
Definition hashjoin.h:379
ParallelHashJoinState * parallel_state
Definition hashjoin.h:378
dsa_area * area
Definition hashjoin.h:377
SharedTuplestoreAccessor * outer_tuples
Definition hashjoin.h:232
SharedTuplestoreAccessor * inner_tuples
Definition hashjoin.h:231
dsa_pointer batches
Definition hashjoin.h:259

References HashJoinTableData::area, Assert, BarrierArriveAndDetach(), BarrierPhase(), ParallelHashJoinState::batches, HashJoinTableData::batches, ParallelHashJoinState::build_barrier, dsa_free(), DsaPointerIsValid, fb(), 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)
extern

Definition at line 3367 of file nodeHash.c.

3368{
3369 if (hashtable->parallel_state != NULL &&
3370 hashtable->curbatch >= 0)
3371 {
3372 int curbatch = hashtable->curbatch;
3373 ParallelHashJoinBatch *batch = hashtable->batches[curbatch].shared;
3374 bool attached = true;
3375
3376 /* Make sure any temporary files are closed. */
3377 sts_end_parallel_scan(hashtable->batches[curbatch].inner_tuples);
3378 sts_end_parallel_scan(hashtable->batches[curbatch].outer_tuples);
3379
3380 /* After attaching we always get at least to PHJ_BATCH_PROBE. */
3381 Assert(BarrierPhase(&batch->batch_barrier) == PHJ_BATCH_PROBE ||
3382 BarrierPhase(&batch->batch_barrier) == PHJ_BATCH_SCAN);
3383
3384 /*
3385 * If we're abandoning the PHJ_BATCH_PROBE phase early without having
3386 * reached the end of it, it means the plan doesn't want any more
3387 * tuples, and it is happy to abandon any tuples buffered in this
3388 * process's subplans. For correctness, we can't allow any process to
3389 * execute the PHJ_BATCH_SCAN phase, because we will never have the
3390 * complete set of match bits. Therefore we skip emitting unmatched
3391 * tuples in all backends (if this is a full/right join), as if those
3392 * tuples were all due to be emitted by this process and it has
3393 * abandoned them too.
3394 */
3395 if (BarrierPhase(&batch->batch_barrier) == PHJ_BATCH_PROBE &&
3396 !hashtable->batches[curbatch].outer_eof)
3397 {
3398 /*
3399 * This flag may be written to by multiple backends during
3400 * PHJ_BATCH_PROBE phase, but will only be read in PHJ_BATCH_SCAN
3401 * phase so requires no extra locking.
3402 */
3403 batch->skip_unmatched = true;
3404 }
3405
3406 /*
3407 * Even if we aren't doing a full/right outer join, we'll step through
3408 * the PHJ_BATCH_SCAN phase just to maintain the invariant that
3409 * freeing happens in PHJ_BATCH_FREE, but that'll be wait-free.
3410 */
3411 if (BarrierPhase(&batch->batch_barrier) == PHJ_BATCH_PROBE)
3413 if (attached && BarrierArriveAndDetach(&batch->batch_barrier))
3414 {
3415 /*
3416 * We are not longer attached to the batch barrier, but we're the
3417 * process that was chosen to free resources and it's safe to
3418 * assert the current phase. The ParallelHashJoinBatch can't go
3419 * away underneath us while we are attached to the build barrier,
3420 * making this access safe.
3421 */
3422 Assert(BarrierPhase(&batch->batch_barrier) == PHJ_BATCH_FREE);
3423
3424 /* Free shared chunks and buckets. */
3425 while (DsaPointerIsValid(batch->chunks))
3426 {
3427 HashMemoryChunk chunk =
3428 dsa_get_address(hashtable->area, batch->chunks);
3429 dsa_pointer next = chunk->next.shared;
3430
3431 dsa_free(hashtable->area, batch->chunks);
3432 batch->chunks = next;
3433 }
3434 if (DsaPointerIsValid(batch->buckets))
3435 {
3436 dsa_free(hashtable->area, batch->buckets);
3437 batch->buckets = InvalidDsaPointer;
3438 }
3439 }
3440
3441 /*
3442 * Track the largest batch we've been attached to. Though each
3443 * backend might see a different subset of batches, explain.c will
3444 * scan the results from all backends to find the largest value.
3445 */
3446 hashtable->spacePeak =
3447 Max(hashtable->spacePeak,
3448 batch->size + sizeof(dsa_pointer_atomic) * hashtable->nbuckets);
3449
3450 /* Remember that we are not attached to a batch. */
3451 hashtable->curbatch = -1;
3452 }
3453}
bool BarrierArriveAndDetachExceptLast(Barrier *barrier)
Definition barrier.c:213
static int32 next
Definition blutils.c:225
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition dsa.c:957
uint64 dsa_pointer
Definition dsa.h:62
#define PHJ_BATCH_SCAN
Definition hashjoin.h:292
#define PHJ_BATCH_PROBE
Definition hashjoin.h:291
#define PHJ_BATCH_FREE
Definition hashjoin.h:293
dsa_pointer shared
Definition hashjoin.h:149
union HashMemoryChunkData::@112 next
ParallelHashJoinBatch * shared
Definition hashjoin.h:220

References HashJoinTableData::area, Assert, BarrierArriveAndDetach(), BarrierArriveAndDetachExceptLast(), BarrierPhase(), HashJoinTableData::batches, HashJoinTableData::curbatch, dsa_free(), dsa_get_address(), DsaPointerIsValid, fb(), ParallelHashJoinBatchAccessor::inner_tuples, InvalidDsaPointer, Max, HashJoinTableData::nbuckets, next, HashMemoryChunkData::next, ParallelHashJoinBatchAccessor::outer_eof, ParallelHashJoinBatchAccessor::outer_tuples, HashJoinTableData::parallel_state, PHJ_BATCH_FREE, PHJ_BATCH_PROBE, PHJ_BATCH_SCAN, HashMemoryChunkData::shared, ParallelHashJoinBatchAccessor::shared, HashJoinTableData::spacePeak, and sts_end_parallel_scan().

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

◆ ExecHashTableInsert()

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

Definition at line 1774 of file nodeHash.c.

1777{
1778 bool shouldFree;
1780 int bucketno;
1781 int batchno;
1782
1783 ExecHashGetBucketAndBatch(hashtable, hashvalue,
1784 &bucketno, &batchno);
1785
1786 /*
1787 * decide whether to put the tuple in the hash table or a temp file
1788 */
1789 if (batchno == hashtable->curbatch)
1790 {
1791 /*
1792 * put the tuple in hash table
1793 */
1795 int hashTupleSize;
1796
1797 /* Create the HashJoinTuple */
1800
1801 hashTuple->hashvalue = hashvalue;
1802 memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
1803
1804 /*
1805 * We always reset the tuple-matched flag on insertion. This is okay
1806 * even when reloading a tuple from a batch file, since the tuple
1807 * could not possibly have been matched to an outer tuple before it
1808 * went into the batch file.
1809 */
1811
1812 /* Push it onto the front of the bucket's list */
1813 hashTuple->next.unshared = hashtable->buckets.unshared[bucketno];
1814 hashtable->buckets.unshared[bucketno] = hashTuple;
1815
1816 /*
1817 * Increase the (optimal) number of buckets if we just exceeded the
1818 * NTUP_PER_BUCKET threshold, but only when there's still a single
1819 * batch. Note that totalTuples - skewTuples is a reliable indicator
1820 * of the hash table's size only as long as there's just one batch.
1821 */
1822 if (hashtable->nbatch == 1 &&
1823 (hashtable->totalTuples - hashtable->skewTuples) >
1824 (hashtable->nbuckets_optimal * NTUP_PER_BUCKET))
1825 {
1826 /* Guard against integer overflow and alloc size overflow */
1827 if (hashtable->nbuckets_optimal <= INT_MAX / 2 &&
1828 hashtable->nbuckets_optimal * 2 <= MaxAllocSize / sizeof(HashJoinTuple))
1829 {
1830 hashtable->nbuckets_optimal *= 2;
1831 hashtable->log2_nbuckets_optimal += 1;
1832 }
1833 }
1834
1835 /* Account for space used, and back off if we've used too much */
1836 hashtable->spaceUsed += hashTupleSize;
1837 if (hashtable->spaceUsed > hashtable->spacePeak)
1838 hashtable->spacePeak = hashtable->spaceUsed;
1839 if (hashtable->spaceUsed +
1840 hashtable->nbuckets_optimal * sizeof(HashJoinTuple)
1841 > hashtable->spaceAllowed)
1842 ExecHashIncreaseNumBatches(hashtable);
1843 }
1844 else
1845 {
1846 /*
1847 * put the tuple into a temp file for later batches
1848 */
1849 Assert(batchno > hashtable->curbatch);
1851 hashvalue,
1852 &hashtable->innerBatchFile[batchno],
1853 hashtable);
1854 }
1855
1856 if (shouldFree)
1858}
MinimalTuple ExecFetchSlotMinimalTuple(TupleTableSlot *slot, bool *shouldFree)
#define HJTUPLE_MINTUPLE(hjtup)
Definition hashjoin.h:102
void heap_free_minimal_tuple(MinimalTuple mtup)
Definition heaptuple.c:1478
static void HeapTupleHeaderClearMatch(MinimalTupleData *tup)
static void * dense_alloc(HashJoinTable hashtable, Size size)
Definition nodeHash.c:2954
static void ExecHashIncreaseNumBatches(HashJoinTable hashtable)
Definition nodeHash.c:1055
void ExecHashGetBucketAndBatch(HashJoinTable hashtable, uint32 hashvalue, int *bucketno, int *batchno)
Definition nodeHash.c:1986
void ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 hashvalue, BufFile **fileptr, HashJoinTable hashtable)
struct HashJoinTupleData ** unshared
Definition hashjoin.h:322
union HashJoinTableData::@113 buckets
int log2_nbuckets_optimal
Definition hashjoin.h:316

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

Referenced by ExecHashJoinNewBatch(), and MultiExecPrivateHash().

◆ ExecHashTableReset()

void ExecHashTableReset ( HashJoinTable  hashtable)
extern

Definition at line 2353 of file nodeHash.c.

2354{
2356 int nbuckets = hashtable->nbuckets;
2357
2358 /*
2359 * Release all the hash buckets and tuples acquired in the prior pass, and
2360 * reinitialize the context for a new pass.
2361 */
2362 MemoryContextReset(hashtable->batchCxt);
2364
2365 /* Reallocate and reinitialize the hash bucket headers. */
2366 hashtable->buckets.unshared = palloc0_array(HashJoinTuple, nbuckets);
2367
2368 hashtable->spaceUsed = 0;
2369
2371
2372 /* Forget the chunks (the memory was freed by the context reset above). */
2373 hashtable->chunks = NULL;
2374}
void MemoryContextReset(MemoryContext context)
Definition mcxt.c:403
HashMemoryChunk chunks
Definition hashjoin.h:373
MemoryContext batchCxt
Definition hashjoin.h:369

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

Referenced by ExecHashJoinNewBatch().

◆ ExecHashTableResetMatchFlags()

void ExecHashTableResetMatchFlags ( HashJoinTable  hashtable)
extern

Definition at line 2381 of file nodeHash.c.

2382{
2383 HashJoinTuple tuple;
2384 int i;
2385
2386 /* Reset all flags in the main table ... */
2387 for (i = 0; i < hashtable->nbuckets; i++)
2388 {
2389 for (tuple = hashtable->buckets.unshared[i]; tuple != NULL;
2390 tuple = tuple->next.unshared)
2392 }
2393
2394 /* ... and the same for the skew buckets, if any */
2395 for (i = 0; i < hashtable->nSkewBuckets; i++)
2396 {
2397 int j = hashtable->skewBucketNums[i];
2398 HashSkewBucket *skewBucket = hashtable->skewBucket[j];
2399
2400 for (tuple = skewBucket->tuples; tuple != NULL; tuple = tuple->next.unshared)
2402 }
2403}
int j
Definition isn.c:78
union HashJoinTupleData::@111 next
struct HashJoinTupleData * unshared
Definition hashjoin.h:94
HashJoinTuple tuples
Definition hashjoin.h:127

References HashJoinTableData::buckets, fb(), 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 
)
extern

Definition at line 399 of file nodeHash.c.

400{
402
403 /* check for unsupported flags */
405
406 /*
407 * create state structure
408 */
410 hashstate->ps.plan = (Plan *) node;
411 hashstate->ps.state = estate;
412 hashstate->ps.ExecProcNode = ExecHash;
413 /* delay building hashtable until ExecHashTableCreate() in executor run */
414 hashstate->hashtable = NULL;
415
416 /*
417 * Miscellaneous initialization
418 *
419 * create expression context for node
420 */
421 ExecAssignExprContext(estate, &hashstate->ps);
422
423 /*
424 * initialize child nodes
425 */
426 outerPlanState(hashstate) = ExecInitNode(outerPlan(node), estate, eflags);
427
428 /*
429 * initialize our result slot and type. No need to build projection
430 * because this node doesn't do projections.
431 */
433 hashstate->ps.ps_ProjInfo = NULL;
434
435 Assert(node->plan.qual == NIL);
436
437 /* these fields will be filled by ExecInitHashJoin() */
438 hashstate->hash_expr = NULL;
439 hashstate->null_tuple_store = NULL;
440 hashstate->keep_null_tuples = false;
441
442 return hashstate;
443}
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
const TupleTableSlotOps TTSOpsMinimalTuple
Definition execTuples.c:86
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition execUtils.c:490
#define EXEC_FLAG_BACKWARD
Definition executor.h:70
#define EXEC_FLAG_MARK
Definition executor.h:71
static TupleTableSlot * ExecHash(PlanState *pstate)
Definition nodeHash.c:92
#define makeNode(_type_)
Definition nodes.h:161
#define NIL
Definition pg_list.h:68
Plan plan
Definition plannodes.h:1419
List * qual
Definition plannodes.h:235

References Assert, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecHash(), ExecInitNode(), ExecInitResultTupleSlotTL(), fb(), makeNode, NIL, outerPlan, outerPlanState, Hash::plan, Plan::qual, and TTSOpsMinimalTuple.

Referenced by ExecInitNode().

◆ ExecParallelHashTableAlloc()

void ExecParallelHashTableAlloc ( HashJoinTable  hashtable,
int  batchno 
)
extern

Definition at line 3347 of file nodeHash.c.

3348{
3350 dsa_pointer_atomic *buckets;
3351 int nbuckets = hashtable->parallel_state->nbuckets;
3352 int i;
3353
3354 batch->buckets =
3355 dsa_allocate(hashtable->area, sizeof(dsa_pointer_atomic) * nbuckets);
3356 buckets = (dsa_pointer_atomic *)
3357 dsa_get_address(hashtable->area, batch->buckets);
3358 for (i = 0; i < nbuckets; ++i)
3360}
#define dsa_pointer_atomic_init
Definition dsa.h:64
#define dsa_allocate(area, size)
Definition dsa.h:109

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

Referenced by ExecHashTableCreate(), and ExecParallelHashJoinNewBatch().

◆ ExecParallelHashTableInsert()

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

Definition at line 1865 of file nodeHash.c.

1868{
1869 bool shouldFree;
1871 dsa_pointer shared;
1872 int bucketno;
1873 int batchno;
1874
1875retry:
1876 ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno, &batchno);
1877
1878 if (batchno == 0)
1879 {
1881
1882 /* Try to load it into memory. */
1886 HJTUPLE_OVERHEAD + tuple->t_len,
1887 &shared);
1888 if (hashTuple == NULL)
1889 goto retry;
1890
1891 /* Store the hash value in the HashJoinTuple header. */
1892 hashTuple->hashvalue = hashvalue;
1893 memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
1895
1896 /* Push it onto the front of the bucket's list */
1898 hashTuple, shared);
1899 }
1900 else
1901 {
1902 size_t tuple_size = MAXALIGN(HJTUPLE_OVERHEAD + tuple->t_len);
1903
1904 Assert(batchno > 0);
1905
1906 /* Try to preallocate space in the batch if necessary. */
1907 if (hashtable->batches[batchno].preallocated < tuple_size)
1908 {
1910 goto retry;
1911 }
1912
1914 hashtable->batches[batchno].preallocated -= tuple_size;
1915 sts_puttuple(hashtable->batches[batchno].inner_tuples, &hashvalue,
1916 tuple);
1917 }
1918 ++hashtable->batches[batchno].ntuples;
1919
1920 if (shouldFree)
1922}
#define PHJ_BUILD_HASH_INNER
Definition hashjoin.h:282
static bool ExecParallelHashTuplePrealloc(HashJoinTable hashtable, int batchno, size_t size)
Definition nodeHash.c:3619
static HashJoinTuple ExecParallelHashTupleAlloc(HashJoinTable hashtable, size_t size, dsa_pointer *shared)
Definition nodeHash.c:3034
static void ExecParallelHashPushTuple(dsa_pointer_atomic *head, HashJoinTuple tuple, dsa_pointer tuple_shared)
Definition nodeHash.c:3539
void sts_puttuple(SharedTuplestoreAccessor *accessor, void *meta_data, MinimalTuple tuple)
dsa_pointer_atomic * shared
Definition hashjoin.h:324

References Assert, BarrierPhase(), HashJoinTableData::batches, HashJoinTableData::buckets, ParallelHashJoinState::build_barrier, ExecFetchSlotMinimalTuple(), ExecHashGetBucketAndBatch(), ExecParallelHashPushTuple(), ExecParallelHashTupleAlloc(), ExecParallelHashTuplePrealloc(), fb(), 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 
)
extern

◆ ExecParallelHashTableSetCurrentBatch()

void ExecParallelHashTableSetCurrentBatch ( HashJoinTable  hashtable,
int  batchno 
)
extern

Definition at line 3557 of file nodeHash.c.

3558{
3560
3561 hashtable->curbatch = batchno;
3562 hashtable->buckets.shared = (dsa_pointer_atomic *)
3563 dsa_get_address(hashtable->area,
3564 hashtable->batches[batchno].shared->buckets);
3565 hashtable->nbuckets = hashtable->parallel_state->nbuckets;
3566 hashtable->log2_nbuckets = pg_ceil_log2_32(hashtable->nbuckets);
3567 hashtable->current_chunk = NULL;
3569 hashtable->batches[batchno].at_least_one_chunk = false;
3570}
HashMemoryChunk current_chunk
Definition hashjoin.h:376
dsa_pointer current_chunk_shared
Definition hashjoin.h:380
dsa_pointer buckets
Definition hashjoin.h:175

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(), fb(), InvalidDsaPointer, HashJoinTableData::log2_nbuckets, ParallelHashJoinState::nbuckets, HashJoinTableData::nbuckets, HashJoinTableData::parallel_state, pg_ceil_log2_32(), ParallelHashJoinBatchAccessor::shared, and HashJoinTableData::shared.

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

◆ ExecParallelPrepHashTableForUnmatched()

bool ExecParallelPrepHashTableForUnmatched ( HashJoinState hjstate)
extern

Definition at line 2151 of file nodeHash.c.

2152{
2153 HashJoinTable hashtable = hjstate->hj_HashTable;
2154 int curbatch = hashtable->curbatch;
2155 ParallelHashJoinBatch *batch = hashtable->batches[curbatch].shared;
2156
2157 Assert(BarrierPhase(&batch->batch_barrier) == PHJ_BATCH_PROBE);
2158
2159 /*
2160 * It would not be deadlock-free to wait on the batch barrier, because it
2161 * is in PHJ_BATCH_PROBE phase, and thus processes attached to it have
2162 * already emitted tuples. Therefore, we'll hold a wait-free election:
2163 * only one process can continue to the next phase, and all others detach
2164 * from this batch. They can still go any work on other batches, if there
2165 * are any.
2166 */
2167 if (!BarrierArriveAndDetachExceptLast(&batch->batch_barrier))
2168 {
2169 /* This process considers the batch to be done. */
2170 hashtable->batches[hashtable->curbatch].done = true;
2171
2172 /* Make sure any temporary files are closed. */
2173 sts_end_parallel_scan(hashtable->batches[curbatch].inner_tuples);
2174 sts_end_parallel_scan(hashtable->batches[curbatch].outer_tuples);
2175
2176 /*
2177 * Track largest batch we've seen, which would normally happen in
2178 * ExecHashTableDetachBatch().
2179 */
2180 hashtable->spacePeak =
2181 Max(hashtable->spacePeak,
2182 batch->size + sizeof(dsa_pointer_atomic) * hashtable->nbuckets);
2183 hashtable->curbatch = -1;
2184 return false;
2185 }
2186
2187 /* Now we are alone with this batch. */
2188 Assert(BarrierPhase(&batch->batch_barrier) == PHJ_BATCH_SCAN);
2189
2190 /*
2191 * Has another process decided to give up early and command all processes
2192 * to skip the unmatched scan?
2193 */
2194 if (batch->skip_unmatched)
2195 {
2196 hashtable->batches[hashtable->curbatch].done = true;
2197 ExecHashTableDetachBatch(hashtable);
2198 return false;
2199 }
2200
2201 /* Now prepare the process local state, just as for non-parallel join. */
2203
2204 return true;
2205}
void ExecHashTableDetachBatch(HashJoinTable hashtable)
Definition nodeHash.c:3367
void ExecPrepHashTableForUnmatched(HashJoinState *hjstate)
Definition nodeHash.c:2130

References Assert, BarrierArriveAndDetachExceptLast(), BarrierPhase(), HashJoinTableData::batches, HashJoinTableData::curbatch, ParallelHashJoinBatchAccessor::done, ExecHashTableDetachBatch(), ExecPrepHashTableForUnmatched(), fb(), ParallelHashJoinBatchAccessor::inner_tuples, Max, HashJoinTableData::nbuckets, ParallelHashJoinBatchAccessor::outer_tuples, PHJ_BATCH_PROBE, PHJ_BATCH_SCAN, ParallelHashJoinBatchAccessor::shared, HashJoinTableData::spacePeak, and sts_end_parallel_scan().

Referenced by ExecHashJoinImpl().

◆ ExecParallelScanHashBucket()

bool ExecParallelScanHashBucket ( HashJoinState hjstate,
ExprContext econtext 
)
extern

Definition at line 2079 of file nodeHash.c.

2081{
2082 ExprState *hjclauses = hjstate->hashclauses;
2083 HashJoinTable hashtable = hjstate->hj_HashTable;
2084 HashJoinTuple hashTuple = hjstate->hj_CurTuple;
2085 uint32 hashvalue = hjstate->hj_CurHashValue;
2086
2087 /*
2088 * hj_CurTuple is the address of the tuple last returned from the current
2089 * bucket, or NULL if it's time to start scanning a new bucket.
2090 */
2091 if (hashTuple != NULL)
2093 else
2095 hjstate->hj_CurBucketNo);
2096
2097 while (hashTuple != NULL)
2098 {
2099 if (hashTuple->hashvalue == hashvalue)
2100 {
2102
2103 /* insert hashtable's tuple into exec slot so ExecQual sees it */
2105 hjstate->hj_HashTupleSlot,
2106 false); /* do not pfree */
2107 econtext->ecxt_innertuple = inntuple;
2108
2109 if (ExecQualAndReset(hjclauses, econtext))
2110 {
2111 hjstate->hj_CurTuple = hashTuple;
2112 return true;
2113 }
2114 }
2115
2117 }
2118
2119 /*
2120 * no match
2121 */
2122 return false;
2123}
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
Definition executor.h:549
static HashJoinTuple ExecParallelHashFirstTuple(HashJoinTable hashtable, int bucketno)
Definition nodeHash.c:3509
static HashJoinTuple ExecParallelHashNextTuple(HashJoinTable hashtable, HashJoinTuple tuple)
Definition nodeHash.c:3525
TupleTableSlot * ecxt_innertuple
Definition execnodes.h:286

References ExprContext::ecxt_innertuple, ExecParallelHashFirstTuple(), ExecParallelHashNextTuple(), ExecQualAndReset(), ExecStoreMinimalTuple(), fb(), and HJTUPLE_MINTUPLE.

Referenced by ExecHashJoinImpl().

◆ ExecParallelScanHashTableForUnmatched()

bool ExecParallelScanHashTableForUnmatched ( HashJoinState hjstate,
ExprContext econtext 
)
extern

Definition at line 2290 of file nodeHash.c.

2292{
2293 HashJoinTable hashtable = hjstate->hj_HashTable;
2294 HashJoinTuple hashTuple = hjstate->hj_CurTuple;
2295
2296 for (;;)
2297 {
2298 /*
2299 * hj_CurTuple is the address of the tuple last returned from the
2300 * current bucket, or NULL if it's time to start scanning a new
2301 * bucket.
2302 */
2303 if (hashTuple != NULL)
2305 else if (hjstate->hj_CurBucketNo < hashtable->nbuckets)
2307 hjstate->hj_CurBucketNo++);
2308 else
2309 break; /* finished all buckets */
2310
2311 while (hashTuple != NULL)
2312 {
2314 {
2316
2317 /* insert hashtable's tuple into exec slot */
2319 hjstate->hj_HashTupleSlot,
2320 false); /* do not pfree */
2321 econtext->ecxt_innertuple = inntuple;
2322
2323 /*
2324 * Reset temp memory each time; although this function doesn't
2325 * do any qual eval, the caller will, so let's keep it
2326 * parallel to ExecScanHashBucket.
2327 */
2328 ResetExprContext(econtext);
2329
2330 hjstate->hj_CurTuple = hashTuple;
2331 return true;
2332 }
2333
2335 }
2336
2337 /* allow this loop to be cancellable */
2339 }
2340
2341 /*
2342 * no more unmatched tuples
2343 */
2344 return false;
2345}
#define ResetExprContext(econtext)
Definition executor.h:654
static bool HeapTupleHeaderHasMatch(const MinimalTupleData *tup)
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123

References CHECK_FOR_INTERRUPTS, ExprContext::ecxt_innertuple, ExecParallelHashFirstTuple(), ExecParallelHashNextTuple(), ExecStoreMinimalTuple(), fb(), HeapTupleHeaderHasMatch(), HJTUPLE_MINTUPLE, HashJoinTableData::nbuckets, and ResetExprContext.

Referenced by ExecHashJoinImpl().

◆ ExecPrepHashTableForUnmatched()

void ExecPrepHashTableForUnmatched ( HashJoinState hjstate)
extern

Definition at line 2130 of file nodeHash.c.

2131{
2132 /*----------
2133 * During this scan we use the HashJoinState fields as follows:
2134 *
2135 * hj_CurBucketNo: next regular bucket to scan
2136 * hj_CurSkewBucketNo: next skew bucket (an index into skewBucketNums)
2137 * hj_CurTuple: last tuple returned, or NULL to start next bucket
2138 *----------
2139 */
2140 hjstate->hj_CurBucketNo = 0;
2141 hjstate->hj_CurSkewBucketNo = 0;
2142 hjstate->hj_CurTuple = NULL;
2143}

References fb().

Referenced by ExecHashJoinImpl(), and ExecParallelPrepHashTableForUnmatched().

◆ ExecReScanHash()

void ExecReScanHash ( HashState node)
extern

Definition at line 2407 of file nodeHash.c.

2408{
2410
2411 /*
2412 * if chgParam of subnode is not null then plan will be re-scanned by
2413 * first ExecProcNode.
2414 */
2415 if (outerPlan->chgParam == NULL)
2417}
void ExecReScan(PlanState *node)
Definition execAmi.c:78

References ExecReScan(), fb(), outerPlan, and outerPlanState.

Referenced by ExecReScan().

◆ ExecScanHashBucket()

bool ExecScanHashBucket ( HashJoinState hjstate,
ExprContext econtext 
)
extern

Definition at line 2018 of file nodeHash.c.

2020{
2021 ExprState *hjclauses = hjstate->hashclauses;
2022 HashJoinTable hashtable = hjstate->hj_HashTable;
2023 HashJoinTuple hashTuple = hjstate->hj_CurTuple;
2024 uint32 hashvalue = hjstate->hj_CurHashValue;
2025
2026 /*
2027 * hj_CurTuple is the address of the tuple last returned from the current
2028 * bucket, or NULL if it's time to start scanning a new bucket.
2029 *
2030 * If the tuple hashed to a skew bucket then scan the skew bucket
2031 * otherwise scan the standard hashtable bucket.
2032 */
2033 if (hashTuple != NULL)
2035 else if (hjstate->hj_CurSkewBucketNo != INVALID_SKEW_BUCKET_NO)
2036 hashTuple = hashtable->skewBucket[hjstate->hj_CurSkewBucketNo]->tuples;
2037 else
2038 hashTuple = hashtable->buckets.unshared[hjstate->hj_CurBucketNo];
2039
2040 while (hashTuple != NULL)
2041 {
2042 if (hashTuple->hashvalue == hashvalue)
2043 {
2045
2046 /* insert hashtable's tuple into exec slot so ExecQual sees it */
2048 hjstate->hj_HashTupleSlot,
2049 false); /* do not pfree */
2050 econtext->ecxt_innertuple = inntuple;
2051
2052 if (ExecQualAndReset(hjclauses, econtext))
2053 {
2054 hjstate->hj_CurTuple = hashTuple;
2055 return true;
2056 }
2057 }
2058
2059 hashTuple = hashTuple->next.unshared;
2060 }
2061
2062 /*
2063 * no match
2064 */
2065 return false;
2066}

References HashJoinTableData::buckets, ExprContext::ecxt_innertuple, ExecQualAndReset(), ExecStoreMinimalTuple(), fb(), 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 
)
extern

Definition at line 2216 of file nodeHash.c.

2217{
2218 HashJoinTable hashtable = hjstate->hj_HashTable;
2219 HashJoinTuple hashTuple = hjstate->hj_CurTuple;
2220
2221 for (;;)
2222 {
2223 /*
2224 * hj_CurTuple is the address of the tuple last returned from the
2225 * current bucket, or NULL if it's time to start scanning a new
2226 * bucket.
2227 */
2228 if (hashTuple != NULL)
2230 else if (hjstate->hj_CurBucketNo < hashtable->nbuckets)
2231 {
2232 hashTuple = hashtable->buckets.unshared[hjstate->hj_CurBucketNo];
2233 hjstate->hj_CurBucketNo++;
2234 }
2235 else if (hjstate->hj_CurSkewBucketNo < hashtable->nSkewBuckets)
2236 {
2237 int j = hashtable->skewBucketNums[hjstate->hj_CurSkewBucketNo];
2238
2239 hashTuple = hashtable->skewBucket[j]->tuples;
2240 hjstate->hj_CurSkewBucketNo++;
2241 }
2242 else
2243 break; /* finished all buckets */
2244
2245 while (hashTuple != NULL)
2246 {
2248 {
2250
2251 /* insert hashtable's tuple into exec slot */
2253 hjstate->hj_HashTupleSlot,
2254 false); /* do not pfree */
2255 econtext->ecxt_innertuple = inntuple;
2256
2257 /*
2258 * Reset temp memory each time; although this function doesn't
2259 * do any qual eval, the caller will, so let's keep it
2260 * parallel to ExecScanHashBucket.
2261 */
2262 ResetExprContext(econtext);
2263
2264 hjstate->hj_CurTuple = hashTuple;
2265 return true;
2266 }
2267
2268 hashTuple = hashTuple->next.unshared;
2269 }
2270
2271 /* allow this loop to be cancellable */
2273 }
2274
2275 /*
2276 * no more unmatched tuples
2277 */
2278 return false;
2279}

References HashJoinTableData::buckets, CHECK_FOR_INTERRUPTS, ExprContext::ecxt_innertuple, ExecStoreMinimalTuple(), fb(), HeapTupleHeaderHasMatch(), 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)
extern

Definition at line 2889 of file nodeHash.c.

2890{
2891 /* Allocate save space if EXPLAIN'ing and we didn't do so already */
2892 if (node->ps.instrument && !node->hinstrument)
2894 /* Now accumulate data for the current (final) hash table */
2895 if (node->hinstrument && node->hashtable)
2897}
#define palloc0_object(type)
Definition fe_memutils.h:75
void ExecHashAccumInstrumentation(HashInstrumentation *instrument, HashJoinTable hashtable)
Definition nodeHash.c:2935
HashJoinTable hashtable
Definition execnodes.h:2666

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

Referenced by ExecShutdownNode_walker().

◆ MultiExecHash()

Node * MultiExecHash ( HashState node)
extern

Definition at line 106 of file nodeHash.c.

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

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

Referenced by MultiExecProcNode().