LCOV - code coverage report
Current view: top level - src/backend/executor - nodeHash.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 1033 1083 95.4 %
Date: 2020-06-05 20:07:15 Functions: 51 52 98.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * nodeHash.c
       4             :  *    Routines to hash relations for hashjoin
       5             :  *
       6             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/executor/nodeHash.c
      12             :  *
      13             :  * See note on parallelism in nodeHashjoin.c.
      14             :  *
      15             :  *-------------------------------------------------------------------------
      16             :  */
      17             : /*
      18             :  * INTERFACE ROUTINES
      19             :  *      MultiExecHash   - generate an in-memory hash table of the relation
      20             :  *      ExecInitHash    - initialize node and subnodes
      21             :  *      ExecEndHash     - shutdown node and subnodes
      22             :  */
      23             : 
      24             : #include "postgres.h"
      25             : 
      26             : #include <math.h>
      27             : #include <limits.h>
      28             : 
      29             : #include "access/htup_details.h"
      30             : #include "access/parallel.h"
      31             : #include "catalog/pg_statistic.h"
      32             : #include "commands/tablespace.h"
      33             : #include "executor/execdebug.h"
      34             : #include "executor/hashjoin.h"
      35             : #include "executor/nodeHash.h"
      36             : #include "executor/nodeHashjoin.h"
      37             : #include "miscadmin.h"
      38             : #include "pgstat.h"
      39             : #include "port/atomics.h"
      40             : #include "port/pg_bitutils.h"
      41             : #include "utils/dynahash.h"
      42             : #include "utils/lsyscache.h"
      43             : #include "utils/memutils.h"
      44             : #include "utils/syscache.h"
      45             : 
      46             : static void ExecHashIncreaseNumBatches(HashJoinTable hashtable);
      47             : static void ExecHashIncreaseNumBuckets(HashJoinTable hashtable);
      48             : static void ExecParallelHashIncreaseNumBatches(HashJoinTable hashtable);
      49             : static void ExecParallelHashIncreaseNumBuckets(HashJoinTable hashtable);
      50             : static void ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node,
      51             :                                   int mcvsToUse);
      52             : static void ExecHashSkewTableInsert(HashJoinTable hashtable,
      53             :                                     TupleTableSlot *slot,
      54             :                                     uint32 hashvalue,
      55             :                                     int bucketNumber);
      56             : static void ExecHashRemoveNextSkewBucket(HashJoinTable hashtable);
      57             : 
      58             : static void *dense_alloc(HashJoinTable hashtable, Size size);
      59             : static HashJoinTuple ExecParallelHashTupleAlloc(HashJoinTable hashtable,
      60             :                                                 size_t size,
      61             :                                                 dsa_pointer *shared);
      62             : static void MultiExecPrivateHash(HashState *node);
      63             : static void MultiExecParallelHash(HashState *node);
      64             : static inline HashJoinTuple ExecParallelHashFirstTuple(HashJoinTable table,
      65             :                                                        int bucketno);
      66             : static inline HashJoinTuple ExecParallelHashNextTuple(HashJoinTable table,
      67             :                                                       HashJoinTuple tuple);
      68             : static inline void ExecParallelHashPushTuple(dsa_pointer_atomic *head,
      69             :                                              HashJoinTuple tuple,
      70             :                                              dsa_pointer tuple_shared);
      71             : static void ExecParallelHashJoinSetUpBatches(HashJoinTable hashtable, int nbatch);
      72             : static void ExecParallelHashEnsureBatchAccessors(HashJoinTable hashtable);
      73             : static void ExecParallelHashRepartitionFirst(HashJoinTable hashtable);
      74             : static void ExecParallelHashRepartitionRest(HashJoinTable hashtable);
      75             : static HashMemoryChunk ExecParallelHashPopChunkQueue(HashJoinTable table,
      76             :                                                      dsa_pointer *shared);
      77             : static bool ExecParallelHashTuplePrealloc(HashJoinTable hashtable,
      78             :                                           int batchno,
      79             :                                           size_t size);
      80             : static void ExecParallelHashMergeCounters(HashJoinTable hashtable);
      81             : static void ExecParallelHashCloseBatchAccessors(HashJoinTable hashtable);
      82             : 
      83             : 
      84             : /* ----------------------------------------------------------------
      85             :  *      ExecHash
      86             :  *
      87             :  *      stub for pro forma compliance
      88             :  * ----------------------------------------------------------------
      89             :  */
      90             : static TupleTableSlot *
      91           0 : ExecHash(PlanState *pstate)
      92             : {
      93           0 :     elog(ERROR, "Hash node does not support ExecProcNode call convention");
      94             :     return NULL;
      95             : }
      96             : 
      97             : /* ----------------------------------------------------------------
      98             :  *      MultiExecHash
      99             :  *
     100             :  *      build hash table for hashjoin, doing partitioning if more
     101             :  *      than one batch is required.
     102             :  * ----------------------------------------------------------------
     103             :  */
     104             : Node *
     105      266956 : MultiExecHash(HashState *node)
     106             : {
     107             :     /* must provide our own instrumentation support */
     108      266956 :     if (node->ps.instrument)
     109         218 :         InstrStartNode(node->ps.instrument);
     110             : 
     111      266956 :     if (node->parallel_state != NULL)
     112         224 :         MultiExecParallelHash(node);
     113             :     else
     114      266732 :         MultiExecPrivateHash(node);
     115             : 
     116             :     /* must provide our own instrumentation support */
     117      266956 :     if (node->ps.instrument)
     118         218 :         InstrStopNode(node->ps.instrument, node->hashtable->partialTuples);
     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      266956 :     return NULL;
     128             : }
     129             : 
     130             : /* ----------------------------------------------------------------
     131             :  *      MultiExecPrivateHash
     132             :  *
     133             :  *      parallel-oblivious version, building a backend-private
     134             :  *      hash table and (if necessary) batch files.
     135             :  * ----------------------------------------------------------------
     136             :  */
     137             : static void
     138      266732 : MultiExecPrivateHash(HashState *node)
     139             : {
     140             :     PlanState  *outerNode;
     141             :     List       *hashkeys;
     142             :     HashJoinTable hashtable;
     143             :     TupleTableSlot *slot;
     144             :     ExprContext *econtext;
     145             :     uint32      hashvalue;
     146             : 
     147             :     /*
     148             :      * get state info from node
     149             :      */
     150      266732 :     outerNode = outerPlanState(node);
     151      266732 :     hashtable = node->hashtable;
     152             : 
     153             :     /*
     154             :      * set expression context
     155             :      */
     156      266732 :     hashkeys = node->hashkeys;
     157      266732 :     econtext = node->ps.ps_ExprContext;
     158             : 
     159             :     /*
     160             :      * Get all tuples from the node below the Hash node and insert into the
     161             :      * hash table (or temp files).
     162             :      */
     163             :     for (;;)
     164             :     {
     165     7488944 :         slot = ExecProcNode(outerNode);
     166     7488944 :         if (TupIsNull(slot))
     167             :             break;
     168             :         /* We have to compute the hash value */
     169     7222212 :         econtext->ecxt_outertuple = slot;
     170     7222212 :         if (ExecHashGetHashValue(hashtable, econtext, hashkeys,
     171     7222212 :                                  false, hashtable->keepNulls,
     172             :                                  &hashvalue))
     173             :         {
     174             :             int         bucketNumber;
     175             : 
     176     7222204 :             bucketNumber = ExecHashGetSkewBucket(hashtable, hashvalue);
     177     7222204 :             if (bucketNumber != INVALID_SKEW_BUCKET_NO)
     178             :             {
     179             :                 /* It's a skew tuple, so put it into that hash table */
     180         172 :                 ExecHashSkewTableInsert(hashtable, slot, hashvalue,
     181             :                                         bucketNumber);
     182         172 :                 hashtable->skewTuples += 1;
     183             :             }
     184             :             else
     185             :             {
     186             :                 /* Not subject to skew optimization, so insert normally */
     187     7222032 :                 ExecHashTableInsert(hashtable, slot, hashvalue);
     188             :             }
     189     7222204 :             hashtable->totalTuples += 1;
     190             :         }
     191             :     }
     192             : 
     193             :     /* resize the hash table if needed (NTUP_PER_BUCKET exceeded) */
     194      266732 :     if (hashtable->nbuckets != hashtable->nbuckets_optimal)
     195         410 :         ExecHashIncreaseNumBuckets(hashtable);
     196             : 
     197             :     /* Account for the buckets in spaceUsed (reported in EXPLAIN ANALYZE) */
     198      266732 :     hashtable->spaceUsed += hashtable->nbuckets * sizeof(HashJoinTuple);
     199      266732 :     if (hashtable->spaceUsed > hashtable->spacePeak)
     200      266708 :         hashtable->spacePeak = hashtable->spaceUsed;
     201             : 
     202      266732 :     hashtable->partialTuples = hashtable->totalTuples;
     203      266732 : }
     204             : 
     205             : /* ----------------------------------------------------------------
     206             :  *      MultiExecParallelHash
     207             :  *
     208             :  *      parallel-aware version, building a shared hash table and
     209             :  *      (if necessary) batch files using the combined effort of
     210             :  *      a set of co-operating backends.
     211             :  * ----------------------------------------------------------------
     212             :  */
     213             : static void
     214         224 : MultiExecParallelHash(HashState *node)
     215             : {
     216             :     ParallelHashJoinState *pstate;
     217             :     PlanState  *outerNode;
     218             :     List       *hashkeys;
     219             :     HashJoinTable hashtable;
     220             :     TupleTableSlot *slot;
     221             :     ExprContext *econtext;
     222             :     uint32      hashvalue;
     223             :     Barrier    *build_barrier;
     224             :     int         i;
     225             : 
     226             :     /*
     227             :      * get state info from node
     228             :      */
     229         224 :     outerNode = outerPlanState(node);
     230         224 :     hashtable = node->hashtable;
     231             : 
     232             :     /*
     233             :      * set expression context
     234             :      */
     235         224 :     hashkeys = node->hashkeys;
     236         224 :     econtext = node->ps.ps_ExprContext;
     237             : 
     238             :     /*
     239             :      * Synchronize the parallel hash table build.  At this stage we know that
     240             :      * the shared hash table has been or is being set up by
     241             :      * ExecHashTableCreate(), but we don't know if our peers have returned
     242             :      * from there or are here in MultiExecParallelHash(), and if so how far
     243             :      * through they are.  To find out, we check the build_barrier phase then
     244             :      * and jump to the right step in the build algorithm.
     245             :      */
     246         224 :     pstate = hashtable->parallel_state;
     247         224 :     build_barrier = &pstate->build_barrier;
     248             :     Assert(BarrierPhase(build_barrier) >= PHJ_BUILD_ALLOCATING);
     249         224 :     switch (BarrierPhase(build_barrier))
     250             :     {
     251          96 :         case PHJ_BUILD_ALLOCATING:
     252             : 
     253             :             /*
     254             :              * Either I just allocated the initial hash table in
     255             :              * ExecHashTableCreate(), or someone else is doing that.  Either
     256             :              * way, wait for everyone to arrive here so we can proceed.
     257             :              */
     258          96 :             BarrierArriveAndWait(build_barrier, WAIT_EVENT_HASH_BUILD_ALLOCATE);
     259             :             /* Fall through. */
     260             : 
     261         150 :         case PHJ_BUILD_HASHING_INNER:
     262             : 
     263             :             /*
     264             :              * It's time to begin hashing, or if we just arrived here then
     265             :              * hashing is already underway, so join in that effort.  While
     266             :              * hashing we have to be prepared to help increase the number of
     267             :              * batches or buckets at any time, and if we arrived here when
     268             :              * that was already underway we'll have to help complete that work
     269             :              * immediately so that it's safe to access batches and buckets
     270             :              * below.
     271             :              */
     272         150 :             if (PHJ_GROW_BATCHES_PHASE(BarrierAttach(&pstate->grow_batches_barrier)) !=
     273             :                 PHJ_GROW_BATCHES_ELECTING)
     274           0 :                 ExecParallelHashIncreaseNumBatches(hashtable);
     275         150 :             if (PHJ_GROW_BUCKETS_PHASE(BarrierAttach(&pstate->grow_buckets_barrier)) !=
     276             :                 PHJ_GROW_BUCKETS_ELECTING)
     277           0 :                 ExecParallelHashIncreaseNumBuckets(hashtable);
     278         150 :             ExecParallelHashEnsureBatchAccessors(hashtable);
     279         150 :             ExecParallelHashTableSetCurrentBatch(hashtable, 0);
     280             :             for (;;)
     281             :             {
     282     1200202 :                 slot = ExecProcNode(outerNode);
     283     1200202 :                 if (TupIsNull(slot))
     284             :                     break;
     285     1200052 :                 econtext->ecxt_outertuple = slot;
     286     1200052 :                 if (ExecHashGetHashValue(hashtable, econtext, hashkeys,
     287     1200052 :                                          false, hashtable->keepNulls,
     288             :                                          &hashvalue))
     289     1200052 :                     ExecParallelHashTableInsert(hashtable, slot, hashvalue);
     290     1200052 :                 hashtable->partialTuples++;
     291             :             }
     292             : 
     293             :             /*
     294             :              * Make sure that any tuples we wrote to disk are visible to
     295             :              * others before anyone tries to load them.
     296             :              */
     297         850 :             for (i = 0; i < hashtable->nbatch; ++i)
     298         700 :                 sts_end_write(hashtable->batches[i].inner_tuples);
     299             : 
     300             :             /*
     301             :              * Update shared counters.  We need an accurate total tuple count
     302             :              * to control the empty table optimization.
     303             :              */
     304         150 :             ExecParallelHashMergeCounters(hashtable);
     305             : 
     306         150 :             BarrierDetach(&pstate->grow_buckets_barrier);
     307         150 :             BarrierDetach(&pstate->grow_batches_barrier);
     308             : 
     309             :             /*
     310             :              * Wait for everyone to finish building and flushing files and
     311             :              * counters.
     312             :              */
     313         150 :             if (BarrierArriveAndWait(build_barrier,
     314             :                                      WAIT_EVENT_HASH_BUILD_HASH_INNER))
     315             :             {
     316             :                 /*
     317             :                  * Elect one backend to disable any further growth.  Batches
     318             :                  * are now fixed.  While building them we made sure they'd fit
     319             :                  * in our memory budget when we load them back in later (or we
     320             :                  * tried to do that and gave up because we detected extreme
     321             :                  * skew).
     322             :                  */
     323          96 :                 pstate->growth = PHJ_GROWTH_DISABLED;
     324             :             }
     325             :     }
     326             : 
     327             :     /*
     328             :      * We're not yet attached to a batch.  We all agree on the dimensions and
     329             :      * number of inner tuples (for the empty table optimization).
     330             :      */
     331         224 :     hashtable->curbatch = -1;
     332         224 :     hashtable->nbuckets = pstate->nbuckets;
     333         224 :     hashtable->log2_nbuckets = my_log2(hashtable->nbuckets);
     334         224 :     hashtable->totalTuples = pstate->total_tuples;
     335         224 :     ExecParallelHashEnsureBatchAccessors(hashtable);
     336             : 
     337             :     /*
     338             :      * The next synchronization point is in ExecHashJoin's HJ_BUILD_HASHTABLE
     339             :      * case, which will bring the build phase to PHJ_BUILD_DONE (if it isn't
     340             :      * there already).
     341             :      */
     342             :     Assert(BarrierPhase(build_barrier) == PHJ_BUILD_HASHING_OUTER ||
     343             :            BarrierPhase(build_barrier) == PHJ_BUILD_DONE);
     344         224 : }
     345             : 
     346             : /* ----------------------------------------------------------------
     347             :  *      ExecInitHash
     348             :  *
     349             :  *      Init routine for Hash node
     350             :  * ----------------------------------------------------------------
     351             :  */
     352             : HashState *
     353       29510 : ExecInitHash(Hash *node, EState *estate, int eflags)
     354             : {
     355             :     HashState  *hashstate;
     356             : 
     357             :     /* check for unsupported flags */
     358             :     Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
     359             : 
     360             :     /*
     361             :      * create state structure
     362             :      */
     363       29510 :     hashstate = makeNode(HashState);
     364       29510 :     hashstate->ps.plan = (Plan *) node;
     365       29510 :     hashstate->ps.state = estate;
     366       29510 :     hashstate->ps.ExecProcNode = ExecHash;
     367       29510 :     hashstate->hashtable = NULL;
     368       29510 :     hashstate->hashkeys = NIL;   /* will be set by parent HashJoin */
     369             : 
     370             :     /*
     371             :      * Miscellaneous initialization
     372             :      *
     373             :      * create expression context for node
     374             :      */
     375       29510 :     ExecAssignExprContext(estate, &hashstate->ps);
     376             : 
     377             :     /*
     378             :      * initialize child nodes
     379             :      */
     380       29510 :     outerPlanState(hashstate) = ExecInitNode(outerPlan(node), estate, eflags);
     381             : 
     382             :     /*
     383             :      * initialize our result slot and type. No need to build projection
     384             :      * because this node doesn't do projections.
     385             :      */
     386       29510 :     ExecInitResultTupleSlotTL(&hashstate->ps, &TTSOpsMinimalTuple);
     387       29510 :     hashstate->ps.ps_ProjInfo = NULL;
     388             : 
     389             :     /*
     390             :      * initialize child expressions
     391             :      */
     392             :     Assert(node->plan.qual == NIL);
     393       29510 :     hashstate->hashkeys =
     394       29510 :         ExecInitExprList(node->hashkeys, (PlanState *) hashstate);
     395             : 
     396       29510 :     return hashstate;
     397             : }
     398             : 
     399             : /* ---------------------------------------------------------------
     400             :  *      ExecEndHash
     401             :  *
     402             :  *      clean up routine for Hash node
     403             :  * ----------------------------------------------------------------
     404             :  */
     405             : void
     406       29474 : ExecEndHash(HashState *node)
     407             : {
     408             :     PlanState  *outerPlan;
     409             : 
     410             :     /*
     411             :      * free exprcontext
     412             :      */
     413       29474 :     ExecFreeExprContext(&node->ps);
     414             : 
     415             :     /*
     416             :      * shut down the subplan
     417             :      */
     418       29474 :     outerPlan = outerPlanState(node);
     419       29474 :     ExecEndNode(outerPlan);
     420       29474 : }
     421             : 
     422             : 
     423             : /* ----------------------------------------------------------------
     424             :  *      ExecHashTableCreate
     425             :  *
     426             :  *      create an empty hashtable data structure for hashjoin.
     427             :  * ----------------------------------------------------------------
     428             :  */
     429             : HashJoinTable
     430      266956 : ExecHashTableCreate(HashState *state, List *hashOperators, List *hashCollations, bool keepNulls)
     431             : {
     432             :     Hash       *node;
     433             :     HashJoinTable hashtable;
     434             :     Plan       *outerNode;
     435             :     size_t      space_allowed;
     436             :     int         nbuckets;
     437             :     int         nbatch;
     438             :     double      rows;
     439             :     int         num_skew_mcvs;
     440             :     int         log2_nbuckets;
     441             :     int         nkeys;
     442             :     int         i;
     443             :     ListCell   *ho;
     444             :     ListCell   *hc;
     445             :     MemoryContext oldcxt;
     446             : 
     447             :     /*
     448             :      * Get information about the size of the relation to be hashed (it's the
     449             :      * "outer" subtree of this node, but the inner relation of the hashjoin).
     450             :      * Compute the appropriate size of the hash table.
     451             :      */
     452      266956 :     node = (Hash *) state->ps.plan;
     453      266956 :     outerNode = outerPlan(node);
     454             : 
     455             :     /*
     456             :      * If this is shared hash table with a partial plan, then we can't use
     457             :      * outerNode->plan_rows to estimate its size.  We need an estimate of the
     458             :      * total number of rows across all copies of the partial plan.
     459             :      */
     460      266956 :     rows = node->plan.parallel_aware ? node->rows_total : outerNode->plan_rows;
     461             : 
     462      266732 :     ExecChooseHashTableSize(rows, outerNode->plan_width,
     463      266956 :                             OidIsValid(node->skewTable),
     464      266956 :                             state->parallel_state != NULL,
     465      266956 :                             state->parallel_state != NULL ?
     466         224 :                             state->parallel_state->nparticipants - 1 : 0,
     467             :                             &space_allowed,
     468             :                             &nbuckets, &nbatch, &num_skew_mcvs);
     469             : 
     470             :     /* nbuckets must be a power of 2 */
     471      266956 :     log2_nbuckets = my_log2(nbuckets);
     472             :     Assert(nbuckets == (1 << log2_nbuckets));
     473             : 
     474             :     /*
     475             :      * Initialize the hash table control block.
     476             :      *
     477             :      * The hashtable control block is just palloc'd from the executor's
     478             :      * per-query memory context.  Everything else should be kept inside the
     479             :      * subsidiary hashCxt or batchCxt.
     480             :      */
     481      266956 :     hashtable = (HashJoinTable) palloc(sizeof(HashJoinTableData));
     482      266956 :     hashtable->nbuckets = nbuckets;
     483      266956 :     hashtable->nbuckets_original = nbuckets;
     484      266956 :     hashtable->nbuckets_optimal = nbuckets;
     485      266956 :     hashtable->log2_nbuckets = log2_nbuckets;
     486      266956 :     hashtable->log2_nbuckets_optimal = log2_nbuckets;
     487      266956 :     hashtable->buckets.unshared = NULL;
     488      266956 :     hashtable->keepNulls = keepNulls;
     489      266956 :     hashtable->skewEnabled = false;
     490      266956 :     hashtable->skewBucket = NULL;
     491      266956 :     hashtable->skewBucketLen = 0;
     492      266956 :     hashtable->nSkewBuckets = 0;
     493      266956 :     hashtable->skewBucketNums = NULL;
     494      266956 :     hashtable->nbatch = nbatch;
     495      266956 :     hashtable->curbatch = 0;
     496      266956 :     hashtable->nbatch_original = nbatch;
     497      266956 :     hashtable->nbatch_outstart = nbatch;
     498      266956 :     hashtable->growEnabled = true;
     499      266956 :     hashtable->totalTuples = 0;
     500      266956 :     hashtable->partialTuples = 0;
     501      266956 :     hashtable->skewTuples = 0;
     502      266956 :     hashtable->innerBatchFile = NULL;
     503      266956 :     hashtable->outerBatchFile = NULL;
     504      266956 :     hashtable->spaceUsed = 0;
     505      266956 :     hashtable->spacePeak = 0;
     506      266956 :     hashtable->spaceAllowed = space_allowed;
     507      266956 :     hashtable->spaceUsedSkew = 0;
     508      266956 :     hashtable->spaceAllowedSkew =
     509      266956 :         hashtable->spaceAllowed * SKEW_WORK_MEM_PERCENT / 100;
     510      266956 :     hashtable->chunks = NULL;
     511      266956 :     hashtable->current_chunk = NULL;
     512      266956 :     hashtable->parallel_state = state->parallel_state;
     513      266956 :     hashtable->area = state->ps.state->es_query_dsa;
     514      266956 :     hashtable->batches = NULL;
     515             : 
     516             : #ifdef HJDEBUG
     517             :     printf("Hashjoin %p: initial nbatch = %d, nbuckets = %d\n",
     518             :            hashtable, nbatch, nbuckets);
     519             : #endif
     520             : 
     521             :     /*
     522             :      * Create temporary memory contexts in which to keep the hashtable working
     523             :      * storage.  See notes in executor/hashjoin.h.
     524             :      */
     525      266956 :     hashtable->hashCxt = AllocSetContextCreate(CurrentMemoryContext,
     526             :                                                "HashTableContext",
     527             :                                                ALLOCSET_DEFAULT_SIZES);
     528             : 
     529      266956 :     hashtable->batchCxt = AllocSetContextCreate(hashtable->hashCxt,
     530             :                                                 "HashBatchContext",
     531             :                                                 ALLOCSET_DEFAULT_SIZES);
     532             : 
     533             :     /* Allocate data that will live for the life of the hashjoin */
     534             : 
     535      266956 :     oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
     536             : 
     537             :     /*
     538             :      * Get info about the hash functions to be used for each hash key. Also
     539             :      * remember whether the join operators are strict.
     540             :      */
     541      266956 :     nkeys = list_length(hashOperators);
     542      266956 :     hashtable->outer_hashfunctions =
     543      266956 :         (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
     544      266956 :     hashtable->inner_hashfunctions =
     545      266956 :         (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
     546      266956 :     hashtable->hashStrict = (bool *) palloc(nkeys * sizeof(bool));
     547      266956 :     hashtable->collations = (Oid *) palloc(nkeys * sizeof(Oid));
     548      266956 :     i = 0;
     549      535412 :     forboth(ho, hashOperators, hc, hashCollations)
     550             :     {
     551      268456 :         Oid         hashop = lfirst_oid(ho);
     552             :         Oid         left_hashfn;
     553             :         Oid         right_hashfn;
     554             : 
     555      268456 :         if (!get_op_hash_functions(hashop, &left_hashfn, &right_hashfn))
     556           0 :             elog(ERROR, "could not find hash function for hash operator %u",
     557             :                  hashop);
     558      268456 :         fmgr_info(left_hashfn, &hashtable->outer_hashfunctions[i]);
     559      268456 :         fmgr_info(right_hashfn, &hashtable->inner_hashfunctions[i]);
     560      268456 :         hashtable->hashStrict[i] = op_strict(hashop);
     561      268456 :         hashtable->collations[i] = lfirst_oid(hc);
     562      268456 :         i++;
     563             :     }
     564             : 
     565      266956 :     if (nbatch > 1 && hashtable->parallel_state == NULL)
     566             :     {
     567             :         /*
     568             :          * allocate and initialize the file arrays in hashCxt (not needed for
     569             :          * parallel case which uses shared tuplestores instead of raw files)
     570             :          */
     571          78 :         hashtable->innerBatchFile = (BufFile **)
     572          78 :             palloc0(nbatch * sizeof(BufFile *));
     573          78 :         hashtable->outerBatchFile = (BufFile **)
     574          78 :             palloc0(nbatch * sizeof(BufFile *));
     575             :         /* The files will not be opened until needed... */
     576             :         /* ... but make sure we have temp tablespaces established for them */
     577          78 :         PrepareTempTablespaces();
     578             :     }
     579             : 
     580      266956 :     MemoryContextSwitchTo(oldcxt);
     581             : 
     582      266956 :     if (hashtable->parallel_state)
     583             :     {
     584         224 :         ParallelHashJoinState *pstate = hashtable->parallel_state;
     585             :         Barrier    *build_barrier;
     586             : 
     587             :         /*
     588             :          * Attach to the build barrier.  The corresponding detach operation is
     589             :          * in ExecHashTableDetach.  Note that we won't attach to the
     590             :          * batch_barrier for batch 0 yet.  We'll attach later and start it out
     591             :          * in PHJ_BATCH_PROBING phase, because batch 0 is allocated up front
     592             :          * and then loaded while hashing (the standard hybrid hash join
     593             :          * algorithm), and we'll coordinate that using build_barrier.
     594             :          */
     595         224 :         build_barrier = &pstate->build_barrier;
     596         224 :         BarrierAttach(build_barrier);
     597             : 
     598             :         /*
     599             :          * So far we have no idea whether there are any other participants,
     600             :          * and if so, what phase they are working on.  The only thing we care
     601             :          * about at this point is whether someone has already created the
     602             :          * SharedHashJoinBatch objects and the hash table for batch 0.  One
     603             :          * backend will be elected to do that now if necessary.
     604             :          */
     605         320 :         if (BarrierPhase(build_barrier) == PHJ_BUILD_ELECTING &&
     606          96 :             BarrierArriveAndWait(build_barrier, WAIT_EVENT_HASH_BUILD_ELECT))
     607             :         {
     608          96 :             pstate->nbatch = nbatch;
     609          96 :             pstate->space_allowed = space_allowed;
     610          96 :             pstate->growth = PHJ_GROWTH_OK;
     611             : 
     612             :             /* Set up the shared state for coordinating batches. */
     613          96 :             ExecParallelHashJoinSetUpBatches(hashtable, nbatch);
     614             : 
     615             :             /*
     616             :              * Allocate batch 0's hash table up front so we can load it
     617             :              * directly while hashing.
     618             :              */
     619          96 :             pstate->nbuckets = nbuckets;
     620          96 :             ExecParallelHashTableAlloc(hashtable, 0);
     621             :         }
     622             : 
     623             :         /*
     624             :          * The next Parallel Hash synchronization point is in
     625             :          * MultiExecParallelHash(), which will progress it all the way to
     626             :          * PHJ_BUILD_DONE.  The caller must not return control from this
     627             :          * executor node between now and then.
     628             :          */
     629             :     }
     630             :     else
     631             :     {
     632             :         /*
     633             :          * Prepare context for the first-scan space allocations; allocate the
     634             :          * hashbucket array therein, and set each bucket "empty".
     635             :          */
     636      266732 :         MemoryContextSwitchTo(hashtable->batchCxt);
     637             : 
     638      266732 :         hashtable->buckets.unshared = (HashJoinTuple *)
     639      266732 :             palloc0(nbuckets * sizeof(HashJoinTuple));
     640             : 
     641             :         /*
     642             :          * Set up for skew optimization, if possible and there's a need for
     643             :          * more than one batch.  (In a one-batch join, there's no point in
     644             :          * it.)
     645             :          */
     646      266732 :         if (nbatch > 1)
     647          78 :             ExecHashBuildSkewHash(hashtable, node, num_skew_mcvs);
     648             : 
     649      266732 :         MemoryContextSwitchTo(oldcxt);
     650             :     }
     651             : 
     652      266956 :     return hashtable;
     653             : }
     654             : 
     655             : 
     656             : /*
     657             :  * Compute appropriate size for hashtable given the estimated size of the
     658             :  * relation to be hashed (number of rows and average row width).
     659             :  *
     660             :  * This is exported so that the planner's costsize.c can use it.
     661             :  */
     662             : 
     663             : /* Target bucket loading (tuples per bucket) */
     664             : #define NTUP_PER_BUCKET         1
     665             : 
     666             : void
     667      556870 : ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
     668             :                         bool try_combined_work_mem,
     669             :                         int parallel_workers,
     670             :                         size_t *space_allowed,
     671             :                         int *numbuckets,
     672             :                         int *numbatches,
     673             :                         int *num_skew_mcvs)
     674             : {
     675             :     int         tupsize;
     676             :     double      inner_rel_bytes;
     677             :     long        bucket_bytes;
     678             :     long        hash_table_bytes;
     679             :     long        skew_table_bytes;
     680             :     long        max_pointers;
     681             :     long        mppow2;
     682      556870 :     int         nbatch = 1;
     683             :     int         nbuckets;
     684             :     double      dbuckets;
     685             : 
     686             :     /* Force a plausible relation size if no info */
     687      556870 :     if (ntuples <= 0.0)
     688          40 :         ntuples = 1000.0;
     689             : 
     690             :     /*
     691             :      * Estimate tupsize based on footprint of tuple in hashtable... note this
     692             :      * does not allow for any palloc overhead.  The manipulations of spaceUsed
     693             :      * don't count palloc overhead either.
     694             :      */
     695      556870 :     tupsize = HJTUPLE_OVERHEAD +
     696      556870 :         MAXALIGN(SizeofMinimalTupleHeader) +
     697      556870 :         MAXALIGN(tupwidth);
     698      556870 :     inner_rel_bytes = ntuples * tupsize;
     699             : 
     700             :     /*
     701             :      * Target in-memory hashtable size is work_mem kilobytes.
     702             :      */
     703      556870 :     hash_table_bytes = work_mem * 1024L;
     704             : 
     705             :     /*
     706             :      * Parallel Hash tries to use the combined work_mem of all workers to
     707             :      * avoid the need to batch.  If that won't work, it falls back to work_mem
     708             :      * per worker and tries to process batches in parallel.
     709             :      */
     710      556870 :     if (try_combined_work_mem)
     711        5052 :         hash_table_bytes += hash_table_bytes * parallel_workers;
     712             : 
     713      556870 :     *space_allowed = hash_table_bytes;
     714             : 
     715             :     /*
     716             :      * If skew optimization is possible, estimate the number of skew buckets
     717             :      * that will fit in the memory allowed, and decrement the assumed space
     718             :      * available for the main hash table accordingly.
     719             :      *
     720             :      * We make the optimistic assumption that each skew bucket will contain
     721             :      * one inner-relation tuple.  If that turns out to be low, we will recover
     722             :      * at runtime by reducing the number of skew buckets.
     723             :      *
     724             :      * hashtable->skewBucket will have up to 8 times as many HashSkewBucket
     725             :      * pointers as the number of MCVs we allow, since ExecHashBuildSkewHash
     726             :      * will round up to the next power of 2 and then multiply by 4 to reduce
     727             :      * collisions.
     728             :      */
     729      556870 :     if (useskew)
     730             :     {
     731      303418 :         skew_table_bytes = hash_table_bytes * SKEW_WORK_MEM_PERCENT / 100;
     732             : 
     733             :         /*----------
     734             :          * Divisor is:
     735             :          * size of a hash tuple +
     736             :          * worst-case size of skewBucket[] per MCV +
     737             :          * size of skewBucketNums[] entry +
     738             :          * size of skew bucket struct itself
     739             :          *----------
     740             :          */
     741      606836 :         *num_skew_mcvs = skew_table_bytes / (tupsize +
     742             :                                              (8 * sizeof(HashSkewBucket *)) +
     743      303418 :                                              sizeof(int) +
     744             :                                              SKEW_BUCKET_OVERHEAD);
     745      303418 :         if (*num_skew_mcvs > 0)
     746      303418 :             hash_table_bytes -= skew_table_bytes;
     747             :     }
     748             :     else
     749      253452 :         *num_skew_mcvs = 0;
     750             : 
     751             :     /*
     752             :      * Set nbuckets to achieve an average bucket load of NTUP_PER_BUCKET when
     753             :      * memory is filled, assuming a single batch; but limit the value so that
     754             :      * the pointer arrays we'll try to allocate do not exceed work_mem nor
     755             :      * MaxAllocSize.
     756             :      *
     757             :      * Note that both nbuckets and nbatch must be powers of 2 to make
     758             :      * ExecHashGetBucketAndBatch fast.
     759             :      */
     760      556870 :     max_pointers = *space_allowed / sizeof(HashJoinTuple);
     761      556870 :     max_pointers = Min(max_pointers, MaxAllocSize / sizeof(HashJoinTuple));
     762             :     /* If max_pointers isn't a power of 2, must round it down to one */
     763      556870 :     mppow2 = 1L << my_log2(max_pointers);
     764      556870 :     if (max_pointers != mppow2)
     765        1988 :         max_pointers = mppow2 / 2;
     766             : 
     767             :     /* Also ensure we avoid integer overflow in nbatch and nbuckets */
     768             :     /* (this step is redundant given the current value of MaxAllocSize) */
     769      556870 :     max_pointers = Min(max_pointers, INT_MAX / 2);
     770             : 
     771      556870 :     dbuckets = ceil(ntuples / NTUP_PER_BUCKET);
     772      556870 :     dbuckets = Min(dbuckets, max_pointers);
     773      556870 :     nbuckets = (int) dbuckets;
     774             :     /* don't let nbuckets be really small, though ... */
     775      556870 :     nbuckets = Max(nbuckets, 1024);
     776             :     /* ... and force it to be a power of 2. */
     777      556870 :     nbuckets = 1 << my_log2(nbuckets);
     778             : 
     779             :     /*
     780             :      * If there's not enough space to store the projected number of tuples and
     781             :      * the required bucket headers, we will need multiple batches.
     782             :      */
     783      556870 :     bucket_bytes = sizeof(HashJoinTuple) * nbuckets;
     784      556870 :     if (inner_rel_bytes + bucket_bytes > hash_table_bytes)
     785             :     {
     786             :         /* We'll need multiple batches */
     787             :         long        lbuckets;
     788             :         double      dbatch;
     789             :         int         minbatch;
     790             :         long        bucket_size;
     791             : 
     792             :         /*
     793             :          * If Parallel Hash with combined work_mem would still need multiple
     794             :          * batches, we'll have to fall back to regular work_mem budget.
     795             :          */
     796        2406 :         if (try_combined_work_mem)
     797             :         {
     798         144 :             ExecChooseHashTableSize(ntuples, tupwidth, useskew,
     799             :                                     false, parallel_workers,
     800             :                                     space_allowed,
     801             :                                     numbuckets,
     802             :                                     numbatches,
     803             :                                     num_skew_mcvs);
     804         144 :             return;
     805             :         }
     806             : 
     807             :         /*
     808             :          * Estimate the number of buckets we'll want to have when work_mem is
     809             :          * entirely full.  Each bucket will contain a bucket pointer plus
     810             :          * NTUP_PER_BUCKET tuples, whose projected size already includes
     811             :          * overhead for the hash code, pointer to the next tuple, etc.
     812             :          */
     813        2262 :         bucket_size = (tupsize * NTUP_PER_BUCKET + sizeof(HashJoinTuple));
     814        2262 :         lbuckets = 1L << my_log2(hash_table_bytes / bucket_size);
     815        2262 :         lbuckets = Min(lbuckets, max_pointers);
     816        2262 :         nbuckets = (int) lbuckets;
     817        2262 :         nbuckets = 1 << my_log2(nbuckets);
     818        2262 :         bucket_bytes = nbuckets * sizeof(HashJoinTuple);
     819             : 
     820             :         /*
     821             :          * Buckets are simple pointers to hashjoin tuples, while tupsize
     822             :          * includes the pointer, hash code, and MinimalTupleData.  So buckets
     823             :          * should never really exceed 25% of work_mem (even for
     824             :          * NTUP_PER_BUCKET=1); except maybe for work_mem values that are not
     825             :          * 2^N bytes, where we might get more because of doubling. So let's
     826             :          * look for 50% here.
     827             :          */
     828             :         Assert(bucket_bytes <= hash_table_bytes / 2);
     829             : 
     830             :         /* Calculate required number of batches. */
     831        2262 :         dbatch = ceil(inner_rel_bytes / (hash_table_bytes - bucket_bytes));
     832        2262 :         dbatch = Min(dbatch, max_pointers);
     833        2262 :         minbatch = (int) dbatch;
     834        2262 :         nbatch = pg_nextpower2_32(Max(2, minbatch));
     835             :     }
     836             : 
     837             :     Assert(nbuckets > 0);
     838             :     Assert(nbatch > 0);
     839             : 
     840      556726 :     *numbuckets = nbuckets;
     841      556726 :     *numbatches = nbatch;
     842             : }
     843             : 
     844             : 
     845             : /* ----------------------------------------------------------------
     846             :  *      ExecHashTableDestroy
     847             :  *
     848             :  *      destroy a hash table
     849             :  * ----------------------------------------------------------------
     850             :  */
     851             : void
     852      266924 : ExecHashTableDestroy(HashJoinTable hashtable)
     853             : {
     854             :     int         i;
     855             : 
     856             :     /*
     857             :      * Make sure all the temp files are closed.  We skip batch 0, since it
     858             :      * can't have any temp files (and the arrays might not even exist if
     859             :      * nbatch is only 1).  Parallel hash joins don't use these files.
     860             :      */
     861      266924 :     if (hashtable->innerBatchFile != NULL)
     862             :     {
     863        1072 :         for (i = 1; i < hashtable->nbatch; i++)
     864             :         {
     865         946 :             if (hashtable->innerBatchFile[i])
     866           0 :                 BufFileClose(hashtable->innerBatchFile[i]);
     867         946 :             if (hashtable->outerBatchFile[i])
     868           0 :                 BufFileClose(hashtable->outerBatchFile[i]);
     869             :         }
     870             :     }
     871             : 
     872             :     /* Release working memory (batchCxt is a child, so it goes away too) */
     873      266924 :     MemoryContextDelete(hashtable->hashCxt);
     874             : 
     875             :     /* And drop the control block */
     876      266924 :     pfree(hashtable);
     877      266924 : }
     878             : 
     879             : /*
     880             :  * ExecHashIncreaseNumBatches
     881             :  *      increase the original number of batches in order to reduce
     882             :  *      current memory consumption
     883             :  */
     884             : static void
     885      414580 : ExecHashIncreaseNumBatches(HashJoinTable hashtable)
     886             : {
     887      414580 :     int         oldnbatch = hashtable->nbatch;
     888      414580 :     int         curbatch = hashtable->curbatch;
     889             :     int         nbatch;
     890             :     MemoryContext oldcxt;
     891             :     long        ninmemory;
     892             :     long        nfreed;
     893             :     HashMemoryChunk oldchunks;
     894             : 
     895             :     /* do nothing if we've decided to shut off growth */
     896      414580 :     if (!hashtable->growEnabled)
     897      414480 :         return;
     898             : 
     899             :     /* safety check to avoid overflow */
     900         100 :     if (oldnbatch > Min(INT_MAX / 2, MaxAllocSize / (sizeof(void *) * 2)))
     901           0 :         return;
     902             : 
     903         100 :     nbatch = oldnbatch * 2;
     904             :     Assert(nbatch > 1);
     905             : 
     906             : #ifdef HJDEBUG
     907             :     printf("Hashjoin %p: increasing nbatch to %d because space = %zu\n",
     908             :            hashtable, nbatch, hashtable->spaceUsed);
     909             : #endif
     910             : 
     911         100 :     oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
     912             : 
     913         100 :     if (hashtable->innerBatchFile == NULL)
     914             :     {
     915             :         /* we had no file arrays before */
     916          48 :         hashtable->innerBatchFile = (BufFile **)
     917          48 :             palloc0(nbatch * sizeof(BufFile *));
     918          48 :         hashtable->outerBatchFile = (BufFile **)
     919          48 :             palloc0(nbatch * sizeof(BufFile *));
     920             :         /* time to establish the temp tablespaces, too */
     921          48 :         PrepareTempTablespaces();
     922             :     }
     923             :     else
     924             :     {
     925             :         /* enlarge arrays and zero out added entries */
     926          52 :         hashtable->innerBatchFile = (BufFile **)
     927          52 :             repalloc(hashtable->innerBatchFile, nbatch * sizeof(BufFile *));
     928          52 :         hashtable->outerBatchFile = (BufFile **)
     929          52 :             repalloc(hashtable->outerBatchFile, nbatch * sizeof(BufFile *));
     930         212 :         MemSet(hashtable->innerBatchFile + oldnbatch, 0,
     931             :                (nbatch - oldnbatch) * sizeof(BufFile *));
     932         212 :         MemSet(hashtable->outerBatchFile + oldnbatch, 0,
     933             :                (nbatch - oldnbatch) * sizeof(BufFile *));
     934             :     }
     935             : 
     936         100 :     MemoryContextSwitchTo(oldcxt);
     937             : 
     938         100 :     hashtable->nbatch = nbatch;
     939             : 
     940             :     /*
     941             :      * Scan through the existing hash table entries and dump out any that are
     942             :      * no longer of the current batch.
     943             :      */
     944         100 :     ninmemory = nfreed = 0;
     945             : 
     946             :     /* If know we need to resize nbuckets, we can do it while rebatching. */
     947         100 :     if (hashtable->nbuckets_optimal != hashtable->nbuckets)
     948             :     {
     949             :         /* we never decrease the number of buckets */
     950             :         Assert(hashtable->nbuckets_optimal > hashtable->nbuckets);
     951             : 
     952          48 :         hashtable->nbuckets = hashtable->nbuckets_optimal;
     953          48 :         hashtable->log2_nbuckets = hashtable->log2_nbuckets_optimal;
     954             : 
     955          48 :         hashtable->buckets.unshared =
     956          48 :             repalloc(hashtable->buckets.unshared,
     957          48 :                      sizeof(HashJoinTuple) * hashtable->nbuckets);
     958             :     }
     959             : 
     960             :     /*
     961             :      * We will scan through the chunks directly, so that we can reset the
     962             :      * buckets now and not have to keep track which tuples in the buckets have
     963             :      * already been processed. We will free the old chunks as we go.
     964             :      */
     965         100 :     memset(hashtable->buckets.unshared, 0,
     966         100 :            sizeof(HashJoinTuple) * hashtable->nbuckets);
     967         100 :     oldchunks = hashtable->chunks;
     968         100 :     hashtable->chunks = NULL;
     969             : 
     970             :     /* so, let's scan through the old chunks, and all tuples in each chunk */
     971         492 :     while (oldchunks != NULL)
     972             :     {
     973         392 :         HashMemoryChunk nextchunk = oldchunks->next.unshared;
     974             : 
     975             :         /* position within the buffer (up to oldchunks->used) */
     976         392 :         size_t      idx = 0;
     977             : 
     978             :         /* process all tuples stored in this chunk (and then free it) */
     979      267896 :         while (idx < oldchunks->used)
     980             :         {
     981      267504 :             HashJoinTuple hashTuple = (HashJoinTuple) (HASH_CHUNK_DATA(oldchunks) + idx);
     982      267504 :             MinimalTuple tuple = HJTUPLE_MINTUPLE(hashTuple);
     983      267504 :             int         hashTupleSize = (HJTUPLE_OVERHEAD + tuple->t_len);
     984             :             int         bucketno;
     985             :             int         batchno;
     986             : 
     987      267504 :             ninmemory++;
     988      267504 :             ExecHashGetBucketAndBatch(hashtable, hashTuple->hashvalue,
     989             :                                       &bucketno, &batchno);
     990             : 
     991      267504 :             if (batchno == curbatch)
     992             :             {
     993             :                 /* keep tuple in memory - copy it into the new chunk */
     994             :                 HashJoinTuple copyTuple;
     995             : 
     996      100096 :                 copyTuple = (HashJoinTuple) dense_alloc(hashtable, hashTupleSize);
     997      100096 :                 memcpy(copyTuple, hashTuple, hashTupleSize);
     998             : 
     999             :                 /* and add it back to the appropriate bucket */
    1000      100096 :                 copyTuple->next.unshared = hashtable->buckets.unshared[bucketno];
    1001      100096 :                 hashtable->buckets.unshared[bucketno] = copyTuple;
    1002             :             }
    1003             :             else
    1004             :             {
    1005             :                 /* dump it out */
    1006             :                 Assert(batchno > curbatch);
    1007      167408 :                 ExecHashJoinSaveTuple(HJTUPLE_MINTUPLE(hashTuple),
    1008             :                                       hashTuple->hashvalue,
    1009      167408 :                                       &hashtable->innerBatchFile[batchno]);
    1010             : 
    1011      167408 :                 hashtable->spaceUsed -= hashTupleSize;
    1012      167408 :                 nfreed++;
    1013             :             }
    1014             : 
    1015             :             /* next tuple in this chunk */
    1016      267504 :             idx += MAXALIGN(hashTupleSize);
    1017             : 
    1018             :             /* allow this loop to be cancellable */
    1019      267504 :             CHECK_FOR_INTERRUPTS();
    1020             :         }
    1021             : 
    1022             :         /* we're done with this chunk - free it and proceed to the next one */
    1023         392 :         pfree(oldchunks);
    1024         392 :         oldchunks = nextchunk;
    1025             :     }
    1026             : 
    1027             : #ifdef HJDEBUG
    1028             :     printf("Hashjoin %p: freed %ld of %ld tuples, space now %zu\n",
    1029             :            hashtable, nfreed, ninmemory, hashtable->spaceUsed);
    1030             : #endif
    1031             : 
    1032             :     /*
    1033             :      * If we dumped out either all or none of the tuples in the table, disable
    1034             :      * further expansion of nbatch.  This situation implies that we have
    1035             :      * enough tuples of identical hashvalues to overflow spaceAllowed.
    1036             :      * Increasing nbatch will not fix it since there's no way to subdivide the
    1037             :      * group any more finely. We have to just gut it out and hope the server
    1038             :      * has enough RAM.
    1039             :      */
    1040         100 :     if (nfreed == 0 || nfreed == ninmemory)
    1041             :     {
    1042          24 :         hashtable->growEnabled = false;
    1043             : #ifdef HJDEBUG
    1044             :         printf("Hashjoin %p: disabling further increase of nbatch\n",
    1045             :                hashtable);
    1046             : #endif
    1047             :     }
    1048             : }
    1049             : 
    1050             : /*
    1051             :  * ExecParallelHashIncreaseNumBatches
    1052             :  *      Every participant attached to grow_batches_barrier must run this
    1053             :  *      function when it observes growth == PHJ_GROWTH_NEED_MORE_BATCHES.
    1054             :  */
    1055             : static void
    1056          34 : ExecParallelHashIncreaseNumBatches(HashJoinTable hashtable)
    1057             : {
    1058          34 :     ParallelHashJoinState *pstate = hashtable->parallel_state;
    1059             :     int         i;
    1060             : 
    1061             :     Assert(BarrierPhase(&pstate->build_barrier) == PHJ_BUILD_HASHING_INNER);
    1062             : 
    1063             :     /*
    1064             :      * It's unlikely, but we need to be prepared for new participants to show
    1065             :      * up while we're in the middle of this operation so we need to switch on
    1066             :      * barrier phase here.
    1067             :      */
    1068          34 :     switch (PHJ_GROW_BATCHES_PHASE(BarrierPhase(&pstate->grow_batches_barrier)))
    1069             :     {
    1070          34 :         case PHJ_GROW_BATCHES_ELECTING:
    1071             : 
    1072             :             /*
    1073             :              * Elect one participant to prepare to grow the number of batches.
    1074             :              * This involves reallocating or resetting the buckets of batch 0
    1075             :              * in preparation for all participants to begin repartitioning the
    1076             :              * tuples.
    1077             :              */
    1078          34 :             if (BarrierArriveAndWait(&pstate->grow_batches_barrier,
    1079             :                                      WAIT_EVENT_HASH_GROW_BATCHES_ELECT))
    1080             :             {
    1081             :                 dsa_pointer_atomic *buckets;
    1082             :                 ParallelHashJoinBatch *old_batch0;
    1083             :                 int         new_nbatch;
    1084             :                 int         i;
    1085             : 
    1086             :                 /* Move the old batch out of the way. */
    1087          34 :                 old_batch0 = hashtable->batches[0].shared;
    1088          34 :                 pstate->old_batches = pstate->batches;
    1089          34 :                 pstate->old_nbatch = hashtable->nbatch;
    1090          34 :                 pstate->batches = InvalidDsaPointer;
    1091             : 
    1092             :                 /* Free this backend's old accessors. */
    1093          34 :                 ExecParallelHashCloseBatchAccessors(hashtable);
    1094             : 
    1095             :                 /* Figure out how many batches to use. */
    1096          34 :                 if (hashtable->nbatch == 1)
    1097             :                 {
    1098             :                     /*
    1099             :                      * We are going from single-batch to multi-batch.  We need
    1100             :                      * to switch from one large combined memory budget to the
    1101             :                      * regular work_mem budget.
    1102             :                      */
    1103          24 :                     pstate->space_allowed = work_mem * 1024L;
    1104             : 
    1105             :                     /*
    1106             :                      * The combined work_mem of all participants wasn't
    1107             :                      * enough. Therefore one batch per participant would be
    1108             :                      * approximately equivalent and would probably also be
    1109             :                      * insufficient.  So try two batches per participant,
    1110             :                      * rounded up to a power of two.
    1111             :                      */
    1112          24 :                     new_nbatch = 1 << my_log2(pstate->nparticipants * 2);
    1113             :                 }
    1114             :                 else
    1115             :                 {
    1116             :                     /*
    1117             :                      * We were already multi-batched.  Try doubling the number
    1118             :                      * of batches.
    1119             :                      */
    1120          10 :                     new_nbatch = hashtable->nbatch * 2;
    1121             :                 }
    1122             : 
    1123             :                 /* Allocate new larger generation of batches. */
    1124             :                 Assert(hashtable->nbatch == pstate->nbatch);
    1125          34 :                 ExecParallelHashJoinSetUpBatches(hashtable, new_nbatch);
    1126             :                 Assert(hashtable->nbatch == pstate->nbatch);
    1127             : 
    1128             :                 /* Replace or recycle batch 0's bucket array. */
    1129          34 :                 if (pstate->old_nbatch == 1)
    1130             :                 {
    1131             :                     double      dtuples;
    1132             :                     double      dbuckets;
    1133             :                     int         new_nbuckets;
    1134             : 
    1135             :                     /*
    1136             :                      * We probably also need a smaller bucket array.  How many
    1137             :                      * tuples do we expect per batch, assuming we have only
    1138             :                      * half of them so far?  Normally we don't need to change
    1139             :                      * the bucket array's size, because the size of each batch
    1140             :                      * stays the same as we add more batches, but in this
    1141             :                      * special case we move from a large batch to many smaller
    1142             :                      * batches and it would be wasteful to keep the large
    1143             :                      * array.
    1144             :                      */
    1145          24 :                     dtuples = (old_batch0->ntuples * 2.0) / new_nbatch;
    1146          24 :                     dbuckets = ceil(dtuples / NTUP_PER_BUCKET);
    1147          24 :                     dbuckets = Min(dbuckets,
    1148             :                                    MaxAllocSize / sizeof(dsa_pointer_atomic));
    1149          24 :                     new_nbuckets = (int) dbuckets;
    1150          24 :                     new_nbuckets = Max(new_nbuckets, 1024);
    1151          24 :                     new_nbuckets = 1 << my_log2(new_nbuckets);
    1152          24 :                     dsa_free(hashtable->area, old_batch0->buckets);
    1153          48 :                     hashtable->batches[0].shared->buckets =
    1154          24 :                         dsa_allocate(hashtable->area,
    1155             :                                      sizeof(dsa_pointer_atomic) * new_nbuckets);
    1156             :                     buckets = (dsa_pointer_atomic *)
    1157          24 :                         dsa_get_address(hashtable->area,
    1158          24 :                                         hashtable->batches[0].shared->buckets);
    1159       73752 :                     for (i = 0; i < new_nbuckets; ++i)
    1160       73728 :                         dsa_pointer_atomic_init(&buckets[i], InvalidDsaPointer);
    1161          24 :                     pstate->nbuckets = new_nbuckets;
    1162             :                 }
    1163             :                 else
    1164             :                 {
    1165             :                     /* Recycle the existing bucket array. */
    1166          10 :                     hashtable->batches[0].shared->buckets = old_batch0->buckets;
    1167             :                     buckets = (dsa_pointer_atomic *)
    1168          10 :                         dsa_get_address(hashtable->area, old_batch0->buckets);
    1169       36874 :                     for (i = 0; i < hashtable->nbuckets; ++i)
    1170       36864 :                         dsa_pointer_atomic_write(&buckets[i], InvalidDsaPointer);
    1171             :                 }
    1172             : 
    1173             :                 /* Move all chunks to the work queue for parallel processing. */
    1174          34 :                 pstate->chunk_work_queue = old_batch0->chunks;
    1175             : 
    1176             :                 /* Disable further growth temporarily while we're growing. */
    1177          34 :                 pstate->growth = PHJ_GROWTH_DISABLED;
    1178             :             }
    1179             :             else
    1180             :             {
    1181             :                 /* All other participants just flush their tuples to disk. */
    1182           0 :                 ExecParallelHashCloseBatchAccessors(hashtable);
    1183             :             }
    1184             :             /* Fall through. */
    1185             : 
    1186             :         case PHJ_GROW_BATCHES_ALLOCATING:
    1187             :             /* Wait for the above to be finished. */
    1188          34 :             BarrierArriveAndWait(&pstate->grow_batches_barrier,
    1189             :                                  WAIT_EVENT_HASH_GROW_BATCHES_ALLOCATE);
    1190             :             /* Fall through. */
    1191             : 
    1192          34 :         case PHJ_GROW_BATCHES_REPARTITIONING:
    1193             :             /* Make sure that we have the current dimensions and buckets. */
    1194          34 :             ExecParallelHashEnsureBatchAccessors(hashtable);
    1195          34 :             ExecParallelHashTableSetCurrentBatch(hashtable, 0);
    1196             :             /* Then partition, flush counters. */
    1197          34 :             ExecParallelHashRepartitionFirst(hashtable);
    1198          34 :             ExecParallelHashRepartitionRest(hashtable);
    1199          34 :             ExecParallelHashMergeCounters(hashtable);
    1200             :             /* Wait for the above to be finished. */
    1201          34 :             BarrierArriveAndWait(&pstate->grow_batches_barrier,
    1202             :                                  WAIT_EVENT_HASH_GROW_BATCHES_REPARTITION);
    1203             :             /* Fall through. */
    1204             : 
    1205          34 :         case PHJ_GROW_BATCHES_DECIDING:
    1206             : 
    1207             :             /*
    1208             :              * Elect one participant to clean up and decide whether further
    1209             :              * repartitioning is needed, or should be disabled because it's
    1210             :              * not helping.
    1211             :              */
    1212          34 :             if (BarrierArriveAndWait(&pstate->grow_batches_barrier,
    1213             :                                      WAIT_EVENT_HASH_GROW_BATCHES_DECIDE))
    1214             :             {
    1215          34 :                 bool        space_exhausted = false;
    1216          34 :                 bool        extreme_skew_detected = false;
    1217             : 
    1218             :                 /* Make sure that we have the current dimensions and buckets. */
    1219          34 :                 ExecParallelHashEnsureBatchAccessors(hashtable);
    1220          34 :                 ExecParallelHashTableSetCurrentBatch(hashtable, 0);
    1221             : 
    1222             :                 /* Are any of the new generation of batches exhausted? */
    1223         258 :                 for (i = 0; i < hashtable->nbatch; ++i)
    1224             :                 {
    1225         224 :                     ParallelHashJoinBatch *batch = hashtable->batches[i].shared;
    1226             : 
    1227         224 :                     if (batch->space_exhausted ||
    1228         224 :                         batch->estimated_size > pstate->space_allowed)
    1229             :                     {
    1230             :                         int         parent;
    1231             : 
    1232          16 :                         space_exhausted = true;
    1233             : 
    1234             :                         /*
    1235             :                          * Did this batch receive ALL of the tuples from its
    1236             :                          * parent batch?  That would indicate that further
    1237             :                          * repartitioning isn't going to help (the hash values
    1238             :                          * are probably all the same).
    1239             :                          */
    1240          16 :                         parent = i % pstate->old_nbatch;
    1241          16 :                         if (batch->ntuples == hashtable->batches[parent].shared->old_ntuples)
    1242          16 :                             extreme_skew_detected = true;
    1243             :                     }
    1244             :                 }
    1245             : 
    1246             :                 /* Don't keep growing if it's not helping or we'd overflow. */
    1247          34 :                 if (extreme_skew_detected || hashtable->nbatch >= INT_MAX / 2)
    1248          16 :                     pstate->growth = PHJ_GROWTH_DISABLED;
    1249          18 :                 else if (space_exhausted)
    1250           0 :                     pstate->growth = PHJ_GROWTH_NEED_MORE_BATCHES;
    1251             :                 else
    1252          18 :                     pstate->growth = PHJ_GROWTH_OK;
    1253             : 
    1254             :                 /* Free the old batches in shared memory. */
    1255          34 :                 dsa_free(hashtable->area, pstate->old_batches);
    1256          34 :                 pstate->old_batches = InvalidDsaPointer;
    1257             :             }
    1258             :             /* Fall through. */
    1259             : 
    1260             :         case PHJ_GROW_BATCHES_FINISHING:
    1261             :             /* Wait for the above to complete. */
    1262          34 :             BarrierArriveAndWait(&pstate->grow_batches_barrier,
    1263             :                                  WAIT_EVENT_HASH_GROW_BATCHES_FINISH);
    1264             :     }
    1265          34 : }
    1266             : 
    1267             : /*
    1268             :  * Repartition the tuples currently loaded into memory for inner batch 0
    1269             :  * because the number of batches has been increased.  Some tuples are retained
    1270             :  * in memory and some are written out to a later batch.
    1271             :  */
    1272             : static void
    1273          34 : ExecParallelHashRepartitionFirst(HashJoinTable hashtable)
    1274             : {
    1275             :     dsa_pointer chunk_shared;
    1276             :     HashMemoryChunk chunk;
    1277             : 
    1278             :     Assert(hashtable->nbatch == hashtable->parallel_state->nbatch);
    1279             : 
    1280         230 :     while ((chunk = ExecParallelHashPopChunkQueue(hashtable, &chunk_shared)))
    1281             :     {
    1282         196 :         size_t      idx = 0;
    1283             : 
    1284             :         /* Repartition all tuples in this chunk. */
    1285      149604 :         while (idx < chunk->used)
    1286             :         {
    1287      149408 :             HashJoinTuple hashTuple = (HashJoinTuple) (HASH_CHUNK_DATA(chunk) + idx);
    1288      149408 :             MinimalTuple tuple = HJTUPLE_MINTUPLE(hashTuple);
    1289             :             HashJoinTuple copyTuple;
    1290             :             dsa_pointer shared;
    1291             :             int         bucketno;
    1292             :             int         batchno;
    1293             : 
    1294      149408 :             ExecHashGetBucketAndBatch(hashtable, hashTuple->hashvalue,
    1295             :                                       &bucketno, &batchno);
    1296             : 
    1297             :             Assert(batchno < hashtable->nbatch);
    1298      149408 :             if (batchno == 0)
    1299             :             {
    1300             :                 /* It still belongs in batch 0.  Copy to a new chunk. */
    1301             :                 copyTuple =
    1302       34954 :                     ExecParallelHashTupleAlloc(hashtable,
    1303       34954 :                                                HJTUPLE_OVERHEAD + tuple->t_len,
    1304             :                                                &shared);
    1305       34954 :                 copyTuple->hashvalue = hashTuple->hashvalue;
    1306       34954 :                 memcpy(HJTUPLE_MINTUPLE(copyTuple), tuple, tuple->t_len);
    1307       34954 :                 ExecParallelHashPushTuple(&hashtable->buckets.shared[bucketno],
    1308             :                                           copyTuple, shared);
    1309             :             }
    1310             :             else
    1311             :             {
    1312      114454 :                 size_t      tuple_size =
    1313      114454 :                 MAXALIGN(HJTUPLE_OVERHEAD + tuple->t_len);
    1314             : 
    1315             :                 /* It belongs in a later batch. */
    1316      114454 :                 hashtable->batches[batchno].estimated_size += tuple_size;
    1317      114454 :                 sts_puttuple(hashtable->batches[batchno].inner_tuples,
    1318      114454 :                              &hashTuple->hashvalue, tuple);
    1319             :             }
    1320             : 
    1321             :             /* Count this tuple. */
    1322      149408 :             ++hashtable->batches[0].old_ntuples;
    1323      149408 :             ++hashtable->batches[batchno].ntuples;
    1324             : 
    1325      149408 :             idx += MAXALIGN(HJTUPLE_OVERHEAD +
    1326             :                             HJTUPLE_MINTUPLE(hashTuple)->t_len);
    1327             :         }
    1328             : 
    1329             :         /* Free this chunk. */
    1330         196 :         dsa_free(hashtable->area, chunk_shared);
    1331             : 
    1332         196 :         CHECK_FOR_INTERRUPTS();
    1333             :     }
    1334          34 : }
    1335             : 
    1336             : /*
    1337             :  * Help repartition inner batches 1..n.
    1338             :  */
    1339             : static void
    1340          34 : ExecParallelHashRepartitionRest(HashJoinTable hashtable)
    1341             : {
    1342          34 :     ParallelHashJoinState *pstate = hashtable->parallel_state;
    1343          34 :     int         old_nbatch = pstate->old_nbatch;
    1344             :     SharedTuplestoreAccessor **old_inner_tuples;
    1345             :     ParallelHashJoinBatch *old_batches;
    1346             :     int         i;
    1347             : 
    1348             :     /* Get our hands on the previous generation of batches. */
    1349             :     old_batches = (ParallelHashJoinBatch *)
    1350          34 :         dsa_get_address(hashtable->area, pstate->old_batches);
    1351          34 :     old_inner_tuples = palloc0(sizeof(SharedTuplestoreAccessor *) * old_nbatch);
    1352          72 :     for (i = 1; i < old_nbatch; ++i)
    1353             :     {
    1354          38 :         ParallelHashJoinBatch *shared =
    1355          38 :         NthParallelHashJoinBatch(old_batches, i);
    1356             : 
    1357          38 :         old_inner_tuples[i] = sts_attach(ParallelHashJoinBatchInner(shared),
    1358             :                                          ParallelWorkerNumber + 1,
    1359             :                                          &pstate->fileset);
    1360             :     }
    1361             : 
    1362             :     /* Join in the effort to repartition them. */
    1363          72 :     for (i = 1; i < old_nbatch; ++i)
    1364             :     {
    1365             :         MinimalTuple tuple;
    1366             :         uint32      hashvalue;
    1367             : 
    1368             :         /* Scan one partition from the previous generation. */
    1369          38 :         sts_begin_parallel_scan(old_inner_tuples[i]);
    1370      125148 :         while ((tuple = sts_parallel_scan_next(old_inner_tuples[i], &hashvalue)))
    1371             :         {
    1372      125110 :             size_t      tuple_size = MAXALIGN(HJTUPLE_OVERHEAD + tuple->t_len);
    1373             :             int         bucketno;
    1374             :             int         batchno;
    1375             : 
    1376             :             /* Decide which partition it goes to in the new generation. */
    1377      125110 :             ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno,
    1378             :                                       &batchno);
    1379             : 
    1380      125110 :             hashtable->batches[batchno].estimated_size += tuple_size;
    1381      125110 :             ++hashtable->batches[batchno].ntuples;
    1382      125110 :             ++hashtable->batches[i].old_ntuples;
    1383             : 
    1384             :             /* Store the tuple its new batch. */
    1385      125110 :             sts_puttuple(hashtable->batches[batchno].inner_tuples,
    1386             :                          &hashvalue, tuple);
    1387             : 
    1388      125110 :             CHECK_FOR_INTERRUPTS();
    1389             :         }
    1390          38 :         sts_end_parallel_scan(old_inner_tuples[i]);
    1391             :     }
    1392             : 
    1393          34 :     pfree(old_inner_tuples);
    1394          34 : }
    1395             : 
    1396             : /*
    1397             :  * Transfer the backend-local per-batch counters to the shared totals.
    1398             :  */
    1399             : static void
    1400         184 : ExecParallelHashMergeCounters(HashJoinTable hashtable)
    1401             : {
    1402         184 :     ParallelHashJoinState *pstate = hashtable->parallel_state;
    1403             :     int         i;
    1404             : 
    1405         184 :     LWLockAcquire(&pstate->lock, LW_EXCLUSIVE);
    1406         184 :     pstate->total_tuples = 0;
    1407        1108 :     for (i = 0; i < hashtable->nbatch; ++i)
    1408             :     {
    1409         924 :         ParallelHashJoinBatchAccessor *batch = &hashtable->batches[i];
    1410             : 
    1411         924 :         batch->shared->size += batch->size;
    1412         924 :         batch->shared->estimated_size += batch->estimated_size;
    1413         924 :         batch->shared->ntuples += batch->ntuples;
    1414         924 :         batch->shared->old_ntuples += batch->old_ntuples;
    1415         924 :         batch->size = 0;
    1416         924 :         batch->estimated_size = 0;
    1417         924 :         batch->ntuples = 0;
    1418         924 :         batch->old_ntuples = 0;
    1419         924 :         pstate->total_tuples += batch->shared->ntuples;
    1420             :     }
    1421         184 :     LWLockRelease(&pstate->lock);
    1422         184 : }
    1423             : 
    1424             : /*
    1425             :  * ExecHashIncreaseNumBuckets
    1426             :  *      increase the original number of buckets in order to reduce
    1427             :  *      number of tuples per bucket
    1428             :  */
    1429             : static void
    1430         410 : ExecHashIncreaseNumBuckets(HashJoinTable hashtable)
    1431             : {
    1432             :     HashMemoryChunk chunk;
    1433             : 
    1434             :     /* do nothing if not an increase (it's called increase for a reason) */
    1435         410 :     if (hashtable->nbuckets >= hashtable->nbuckets_optimal)
    1436           0 :         return;
    1437             : 
    1438             : #ifdef HJDEBUG
    1439             :     printf("Hashjoin %p: increasing nbuckets %d => %d\n",
    1440             :            hashtable, hashtable->nbuckets, hashtable->nbuckets_optimal);
    1441             : #endif
    1442             : 
    1443         410 :     hashtable->nbuckets = hashtable->nbuckets_optimal;
    1444         410 :     hashtable->log2_nbuckets = hashtable->log2_nbuckets_optimal;
    1445             : 
    1446             :     Assert(hashtable->nbuckets > 1);
    1447             :     Assert(hashtable->nbuckets <= (INT_MAX / 2));
    1448             :     Assert(hashtable->nbuckets == (1 << hashtable->log2_nbuckets));
    1449             : 
    1450             :     /*
    1451             :      * Just reallocate the proper number of buckets - we don't need to walk
    1452             :      * through them - we can walk the dense-allocated chunks (just like in
    1453             :      * ExecHashIncreaseNumBatches, but without all the copying into new
    1454             :      * chunks)
    1455             :      */
    1456         410 :     hashtable->buckets.unshared =
    1457         410 :         (HashJoinTuple *) repalloc(hashtable->buckets.unshared,
    1458         410 :                                    hashtable->nbuckets * sizeof(HashJoinTuple));
    1459             : 
    1460         410 :     memset(hashtable->buckets.unshared, 0,
    1461         410 :            hashtable->nbuckets * sizeof(HashJoinTuple));
    1462             : 
    1463             :     /* scan through all tuples in all chunks to rebuild the hash table */
    1464        2156 :     for (chunk = hashtable->chunks; chunk != NULL; chunk = chunk->next.unshared)
    1465             :     {
    1466             :         /* process all tuples stored in this chunk */
    1467        1746 :         size_t      idx = 0;
    1468             : 
    1469     1330228 :         while (idx < chunk->used)
    1470             :         {
    1471     1328482 :             HashJoinTuple hashTuple = (HashJoinTuple) (HASH_CHUNK_DATA(chunk) + idx);
    1472             :             int         bucketno;
    1473             :             int         batchno;
    1474             : 
    1475     1328482 :             ExecHashGetBucketAndBatch(hashtable, hashTuple->hashvalue,
    1476             :                                       &bucketno, &batchno);
    1477             : 
    1478             :             /* add the tuple to the proper bucket */
    1479     1328482 :             hashTuple->next.unshared = hashtable->buckets.unshared[bucketno];
    1480     1328482 :             hashtable->buckets.unshared[bucketno] = hashTuple;
    1481             : 
    1482             :             /* advance index past the tuple */
    1483     1328482 :             idx += MAXALIGN(HJTUPLE_OVERHEAD +
    1484             :                             HJTUPLE_MINTUPLE(hashTuple)->t_len);
    1485             :         }
    1486             : 
    1487             :         /* allow this loop to be cancellable */
    1488        1746 :         CHECK_FOR_INTERRUPTS();
    1489             :     }
    1490             : }
    1491             : 
    1492             : static void
    1493          96 : ExecParallelHashIncreaseNumBuckets(HashJoinTable hashtable)
    1494             : {
    1495          96 :     ParallelHashJoinState *pstate = hashtable->parallel_state;
    1496             :     int         i;
    1497             :     HashMemoryChunk chunk;
    1498             :     dsa_pointer chunk_s;
    1499             : 
    1500             :     Assert(BarrierPhase(&pstate->build_barrier) == PHJ_BUILD_HASHING_INNER);
    1501             : 
    1502             :     /*
    1503             :      * It's unlikely, but we need to be prepared for new participants to show
    1504             :      * up while we're in the middle of this operation so we need to switch on
    1505             :      * barrier phase here.
    1506             :      */
    1507          96 :     switch (PHJ_GROW_BUCKETS_PHASE(BarrierPhase(&pstate->grow_buckets_barrier)))
    1508             :     {
    1509          96 :         case PHJ_GROW_BUCKETS_ELECTING:
    1510             :             /* Elect one participant to prepare to increase nbuckets. */
    1511          96 :             if (BarrierArriveAndWait(&pstate->grow_buckets_barrier,
    1512             :                                      WAIT_EVENT_HASH_GROW_BUCKETS_ELECT))
    1513             :             {
    1514             :                 size_t      size;
    1515             :                 dsa_pointer_atomic *buckets;
    1516             : 
    1517             :                 /* Double the size of the bucket array. */
    1518          72 :                 pstate->nbuckets *= 2;
    1519          72 :                 size = pstate->nbuckets * sizeof(dsa_pointer_atomic);
    1520          72 :                 hashtable->batches[0].shared->size += size / 2;
    1521          72 :                 dsa_free(hashtable->area, hashtable->batches[0].shared->buckets);
    1522         144 :                 hashtable->batches[0].shared->buckets =
    1523          72 :                     dsa_allocate(hashtable->area, size);
    1524             :                 buckets = (dsa_pointer_atomic *)
    1525          72 :                     dsa_get_address(hashtable->area,
    1526          72 :                                     hashtable->batches[0].shared->buckets);
    1527      622664 :                 for (i = 0; i < pstate->nbuckets; ++i)
    1528      622592 :                     dsa_pointer_atomic_init(&buckets[i], InvalidDsaPointer);
    1529             : 
    1530             :                 /* Put the chunk list onto the work queue. */
    1531          72 :                 pstate->chunk_work_queue = hashtable->batches[0].shared->chunks;
    1532             : 
    1533             :                 /* Clear the flag. */
    1534          72 :                 pstate->growth = PHJ_GROWTH_OK;
    1535             :             }
    1536             :             /* Fall through. */
    1537             : 
    1538             :         case PHJ_GROW_BUCKETS_ALLOCATING:
    1539             :             /* Wait for the above to complete. */
    1540          96 :             BarrierArriveAndWait(&pstate->grow_buckets_barrier,
    1541             :                                  WAIT_EVENT_HASH_GROW_BUCKETS_ALLOCATE);
    1542             :             /* Fall through. */
    1543             : 
    1544          96 :         case PHJ_GROW_BUCKETS_REINSERTING:
    1545             :             /* Reinsert all tuples into the hash table. */
    1546          96 :             ExecParallelHashEnsureBatchAccessors(hashtable);
    1547          96 :             ExecParallelHashTableSetCurrentBatch(hashtable, 0);
    1548         538 :             while ((chunk = ExecParallelHashPopChunkQueue(hashtable, &chunk_s)))
    1549         442 :             {
    1550         442 :                 size_t      idx = 0;
    1551             : 
    1552      361998 :                 while (idx < chunk->used)
    1553             :                 {
    1554      361556 :                     HashJoinTuple hashTuple = (HashJoinTuple) (HASH_CHUNK_DATA(chunk) + idx);
    1555      361556 :                     dsa_pointer shared = chunk_s + HASH_CHUNK_HEADER_SIZE + idx;
    1556             :                     int         bucketno;
    1557             :                     int         batchno;
    1558             : 
    1559      361556 :                     ExecHashGetBucketAndBatch(hashtable, hashTuple->hashvalue,
    1560             :                                               &bucketno, &batchno);
    1561             :                     Assert(batchno == 0);
    1562             : 
    1563             :                     /* add the tuple to the proper bucket */
    1564      361556 :                     ExecParallelHashPushTuple(&hashtable->buckets.shared[bucketno],
    1565             :                                               hashTuple, shared);
    1566             : 
    1567             :                     /* advance index past the tuple */
    1568      361556 :                     idx += MAXALIGN(HJTUPLE_OVERHEAD +
    1569             :                                     HJTUPLE_MINTUPLE(hashTuple)->t_len);
    1570             :                 }
    1571             : 
    1572             :                 /* allow this loop to be cancellable */
    1573         442 :                 CHECK_FOR_INTERRUPTS();
    1574             :             }
    1575          96 :             BarrierArriveAndWait(&pstate->grow_buckets_barrier,
    1576             :                                  WAIT_EVENT_HASH_GROW_BUCKETS_REINSERT);
    1577             :     }
    1578          96 : }
    1579             : 
    1580             : /*
    1581             :  * ExecHashTableInsert
    1582             :  *      insert a tuple into the hash table depending on the hash value
    1583             :  *      it may just go to a temp file for later batches
    1584             :  *
    1585             :  * Note: the passed TupleTableSlot may contain a regular, minimal, or virtual
    1586             :  * tuple; the minimal case in particular is certain to happen while reloading
    1587             :  * tuples from batch files.  We could save some cycles in the regular-tuple
    1588             :  * case by not forcing the slot contents into minimal form; not clear if it's
    1589             :  * worth the messiness required.
    1590             :  */
    1591             : void
    1592     9190532 : ExecHashTableInsert(HashJoinTable hashtable,
    1593             :                     TupleTableSlot *slot,
    1594             :                     uint32 hashvalue)
    1595             : {
    1596             :     bool        shouldFree;
    1597     9190532 :     MinimalTuple tuple = ExecFetchSlotMinimalTuple(slot, &shouldFree);
    1598             :     int         bucketno;
    1599             :     int         batchno;
    1600             : 
    1601     9190532 :     ExecHashGetBucketAndBatch(hashtable, hashvalue,
    1602             :                               &bucketno, &batchno);
    1603             : 
    1604             :     /*
    1605             :      * decide whether to put the tuple in the hash table or a temp file
    1606             :      */
    1607     9190532 :     if (batchno == hashtable->curbatch)
    1608             :     {
    1609             :         /*
    1610             :          * put the tuple in hash table
    1611             :          */
    1612             :         HashJoinTuple hashTuple;
    1613             :         int         hashTupleSize;
    1614     7389524 :         double      ntuples = (hashtable->totalTuples - hashtable->skewTuples);
    1615             : 
    1616             :         /* Create the HashJoinTuple */
    1617     7389524 :         hashTupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
    1618     7389524 :         hashTuple = (HashJoinTuple) dense_alloc(hashtable, hashTupleSize);
    1619             : 
    1620     7389524 :         hashTuple->hashvalue = hashvalue;
    1621     7389524 :         memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
    1622             : 
    1623             :         /*
    1624             :          * We always reset the tuple-matched flag on insertion.  This is okay
    1625             :          * even when reloading a tuple from a batch file, since the tuple
    1626             :          * could not possibly have been matched to an outer tuple before it
    1627             :          * went into the batch file.
    1628             :          */
    1629     7389524 :         HeapTupleHeaderClearMatch(HJTUPLE_MINTUPLE(hashTuple));
    1630             : 
    1631             :         /* Push it onto the front of the bucket's list */
    1632     7389524 :         hashTuple->next.unshared = hashtable->buckets.unshared[bucketno];
    1633     7389524 :         hashtable->buckets.unshared[bucketno] = hashTuple;
    1634             : 
    1635             :         /*
    1636             :          * Increase the (optimal) number of buckets if we just exceeded the
    1637             :          * NTUP_PER_BUCKET threshold, but only when there's still a single
    1638             :          * batch.
    1639             :          */
    1640     7389524 :         if (hashtable->nbatch == 1 &&
    1641     5353292 :             ntuples > (hashtable->nbuckets_optimal * NTUP_PER_BUCKET))
    1642             :         {
    1643             :             /* Guard against integer overflow and alloc size overflow */
    1644         864 :             if (hashtable->nbuckets_optimal <= INT_MAX / 2 &&
    1645         864 :                 hashtable->nbuckets_optimal * 2 <= MaxAllocSize / sizeof(HashJoinTuple))
    1646             :             {
    1647         864 :                 hashtable->nbuckets_optimal *= 2;
    1648         864 :                 hashtable->log2_nbuckets_optimal += 1;
    1649             :             }
    1650             :         }
    1651             : 
    1652             :         /* Account for space used, and back off if we've used too much */
    1653     7389524 :         hashtable->spaceUsed += hashTupleSize;
    1654     7389524 :         if (hashtable->spaceUsed > hashtable->spacePeak)
    1655     5862860 :             hashtable->spacePeak = hashtable->spaceUsed;
    1656    22168572 :         if (hashtable->spaceUsed +
    1657     7389524 :             hashtable->nbuckets_optimal * sizeof(HashJoinTuple)
    1658     7389524 :             > hashtable->spaceAllowed)
    1659      414580 :             ExecHashIncreaseNumBatches(hashtable);
    1660             :     }
    1661             :     else
    1662             :     {
    1663             :         /*
    1664             :          * put the tuple into a temp file for later batches
    1665             :          */
    1666             :         Assert(batchno > hashtable->curbatch);
    1667     1801008 :         ExecHashJoinSaveTuple(tuple,
    1668             :                               hashvalue,
    1669     1801008 :                               &hashtable->innerBatchFile[batchno]);
    1670             :     }
    1671             : 
    1672     9190532 :     if (shouldFree)
    1673     6754860 :         heap_free_minimal_tuple(tuple);
    1674     9190532 : }
    1675             : 
    1676             : /*
    1677             :  * ExecParallelHashTableInsert
    1678             :  *      insert a tuple into a shared hash table or shared batch tuplestore
    1679             :  */
    1680             : void
    1681     1200052 : ExecParallelHashTableInsert(HashJoinTable hashtable,
    1682             :                             TupleTableSlot *slot,
    1683             :                             uint32 hashvalue)
    1684             : {
    1685             :     bool        shouldFree;
    1686     1200052 :     MinimalTuple tuple = ExecFetchSlotMinimalTuple(slot, &shouldFree);
    1687             :     dsa_pointer shared;
    1688             :     int         bucketno;
    1689             :     int         batchno;
    1690             : 
    1691     1200288 : retry:
    1692     1200288 :     ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno, &batchno);
    1693             : 
    1694     1200288 :     if (batchno == 0)
    1695             :     {
    1696             :         HashJoinTuple hashTuple;
    1697             : 
    1698             :         /* Try to load it into memory. */
    1699             :         Assert(BarrierPhase(&hashtable->parallel_state->build_barrier) ==
    1700             :                PHJ_BUILD_HASHING_INNER);
    1701      661604 :         hashTuple = ExecParallelHashTupleAlloc(hashtable,
    1702      661604 :                                                HJTUPLE_OVERHEAD + tuple->t_len,
    1703             :                                                &shared);
    1704      661604 :         if (hashTuple == NULL)
    1705         216 :             goto retry;
    1706             : 
    1707             :         /* Store the hash value in the HashJoinTuple header. */
    1708      661388 :         hashTuple->hashvalue = hashvalue;
    1709      661388 :         memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
    1710             : 
    1711             :         /* Push it onto the front of the bucket's list */
    1712      661388 :         ExecParallelHashPushTuple(&hashtable->buckets.shared[bucketno],
    1713             :                                   hashTuple, shared);
    1714             :     }
    1715             :     else
    1716             :     {
    1717      538684 :         size_t      tuple_size = MAXALIGN(HJTUPLE_OVERHEAD + tuple->t_len);
    1718             : 
    1719             :         Assert(batchno > 0);
    1720             : 
    1721             :         /* Try to preallocate space in the batch if necessary. */
    1722      538684 :         if (hashtable->batches[batchno].preallocated < tuple_size)
    1723             :         {
    1724        1046 :             if (!ExecParallelHashTuplePrealloc(hashtable, batchno, tuple_size))
    1725          20 :                 goto retry;
    1726             :         }
    1727             : 
    1728             :         Assert(hashtable->batches[batchno].preallocated >= tuple_size);
    1729      538664 :         hashtable->batches[batchno].preallocated -= tuple_size;
    1730      538664 :         sts_puttuple(hashtable->batches[batchno].inner_tuples, &hashvalue,
    1731             :                      tuple);
    1732             :     }
    1733     1200052 :     ++hashtable->batches[batchno].ntuples;
    1734             : 
    1735     1200052 :     if (shouldFree)
    1736     1200052 :         heap_free_minimal_tuple(tuple);
    1737     1200052 : }
    1738             : 
    1739             : /*
    1740             :  * Insert a tuple into the current hash table.  Unlike
    1741             :  * ExecParallelHashTableInsert, this version is not prepared to send the tuple
    1742             :  * to other batches or to run out of memory, and should only be called with
    1743             :  * tuples that belong in the current batch once growth has been disabled.
    1744             :  */
    1745             : void
    1746      653118 : ExecParallelHashTableInsertCurrentBatch(HashJoinTable hashtable,
    1747             :                                         TupleTableSlot *slot,
    1748             :                                         uint32 hashvalue)
    1749             : {
    1750             :     bool        shouldFree;
    1751      653118 :     MinimalTuple tuple = ExecFetchSlotMinimalTuple(slot, &shouldFree);
    1752             :     HashJoinTuple hashTuple;
    1753             :     dsa_pointer shared;
    1754             :     int         batchno;
    1755             :     int         bucketno;
    1756             : 
    1757      653118 :     ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno, &batchno);
    1758             :     Assert(batchno == hashtable->curbatch);
    1759      653118 :     hashTuple = ExecParallelHashTupleAlloc(hashtable,
    1760      653118 :                                            HJTUPLE_OVERHEAD + tuple->t_len,
    1761             :                                            &shared);
    1762      653118 :     hashTuple->hashvalue = hashvalue;
    1763      653118 :     memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
    1764      653118 :     HeapTupleHeaderClearMatch(HJTUPLE_MINTUPLE(hashTuple));
    1765      653118 :     ExecParallelHashPushTuple(&hashtable->buckets.shared[bucketno],
    1766             :                               hashTuple, shared);
    1767             : 
    1768      653118 :     if (shouldFree)
    1769           0 :         heap_free_minimal_tuple(tuple);
    1770      653118 : }
    1771             : 
    1772             : /*
    1773             :  * ExecHashGetHashValue
    1774             :  *      Compute the hash value for a tuple
    1775             :  *
    1776             :  * The tuple to be tested must be in econtext->ecxt_outertuple (thus Vars in
    1777             :  * the hashkeys expressions need to have OUTER_VAR as varno). If outer_tuple
    1778             :  * is false (meaning it's the HashJoin's inner node, Hash), econtext,
    1779             :  * hashkeys, and slot need to be from Hash, with hashkeys/slot referencing and
    1780             :  * being suitable for tuples from the node below the Hash. Conversely, if
    1781             :  * outer_tuple is true, econtext is from HashJoin, and hashkeys/slot need to
    1782             :  * be appropriate for tuples from HashJoin's outer node.
    1783             :  *
    1784             :  * A true result means the tuple's hash value has been successfully computed
    1785             :  * and stored at *hashvalue.  A false result means the tuple cannot match
    1786             :  * because it contains a null attribute, and hence it should be discarded
    1787             :  * immediately.  (If keep_nulls is true then false is never returned.)
    1788             :  */
    1789             : bool
    1790    17350090 : ExecHashGetHashValue(HashJoinTable hashtable,
    1791             :                      ExprContext *econtext,
    1792             :                      List *hashkeys,
    1793             :                      bool outer_tuple,
    1794             :                      bool keep_nulls,
    1795             :                      uint32 *hashvalue)
    1796             : {
    1797    17350090 :     uint32      hashkey = 0;
    1798             :     FmgrInfo   *hashfunctions;
    1799             :     ListCell   *hk;
    1800    17350090 :     int         i = 0;
    1801             :     MemoryContext oldContext;
    1802             : 
    1803             :     /*
    1804             :      * We reset the eval context each time to reclaim any memory leaked in the
    1805             :      * hashkey expressions.
    1806             :      */
    1807    17350090 :     ResetExprContext(econtext);
    1808             : 
    1809    17350090 :     oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
    1810             : 
    1811    17350090 :     if (outer_tuple)
    1812     8927826 :         hashfunctions = hashtable->outer_hashfunctions;
    1813             :     else
    1814     8422264 :         hashfunctions = hashtable->inner_hashfunctions;
    1815             : 
    1816    35778228 :     foreach(hk, hashkeys)
    1817             :     {
    1818    18428626 :         ExprState  *keyexpr = (ExprState *) lfirst(hk);
    1819             :         Datum       keyval;
    1820             :         bool        isNull;
    1821             : 
    1822             :         /* rotate hashkey left 1 bit at each step */
    1823    18428626 :         hashkey = (hashkey << 1) | ((hashkey & 0x80000000) ? 1 : 0);
    1824             : 
    1825             :         /*
    1826             :          * Get the join attribute value of the tuple
    1827             :          */
    1828    18428626 :         keyval = ExecEvalExpr(keyexpr, econtext, &isNull);
    1829             : 
    1830             :         /*
    1831             :          * If the attribute is NULL, and the join operator is strict, then
    1832             :          * this tuple cannot pass the join qual so we can reject it
    1833             :          * immediately (unless we're scanning the outside of an outer join, in
    1834             :          * which case we must not reject it).  Otherwise we act like the
    1835             :          * hashcode of NULL is zero (this will support operators that act like
    1836             :          * IS NOT DISTINCT, though not any more-random behavior).  We treat
    1837             :          * the hash support function as strict even if the operator is not.
    1838             :          *
    1839             :          * Note: currently, all hashjoinable operators must be strict since
    1840             :          * the hash index AM assumes that.  However, it takes so little extra
    1841             :          * code here to allow non-strict that we may as well do it.
    1842             :          */
    1843    18428626 :         if (isNull)
    1844             :         {
    1845         662 :             if (hashtable->hashStrict[i] && !keep_nulls)
    1846             :             {
    1847         488 :                 MemoryContextSwitchTo(oldContext);
    1848         488 :                 return false;   /* cannot match */
    1849             :             }
    1850             :             /* else, leave hashkey unmodified, equivalent to hashcode 0 */
    1851             :         }
    1852             :         else
    1853             :         {
    1854             :             /* Compute the hash function */
    1855             :             uint32      hkey;
    1856             : 
    1857    18427964 :             hkey = DatumGetUInt32(FunctionCall1Coll(&hashfunctions[i], hashtable->collations[i], keyval));
    1858    18427964 :             hashkey ^= hkey;
    1859             :         }
    1860             : 
    1861    18428138 :         i++;
    1862             :     }
    1863             : 
    1864    17349602 :     MemoryContextSwitchTo(oldContext);
    1865             : 
    1866    17349602 :     *hashvalue = hashkey;
    1867    17349602 :     return true;
    1868             : }
    1869             : 
    1870             : /*
    1871             :  * ExecHashGetBucketAndBatch
    1872             :  *      Determine the bucket number and batch number for a hash value
    1873             :  *
    1874             :  * Note: on-the-fly increases of nbatch must not change the bucket number
    1875             :  * for a given hash code (since we don't move tuples to different hash
    1876             :  * chains), and must only cause the batch number to remain the same or
    1877             :  * increase.  Our algorithm is
    1878             :  *      bucketno = hashvalue MOD nbuckets
    1879             :  *      batchno = ROR(hashvalue, log2_nbuckets) MOD nbatch
    1880             :  * where nbuckets and nbatch are both expected to be powers of 2, so we can
    1881             :  * do the computations by shifting and masking.  (This assumes that all hash
    1882             :  * functions are good about randomizing all their output bits, else we are
    1883             :  * likely to have very skewed bucket or batch occupancy.)
    1884             :  *
    1885             :  * nbuckets and log2_nbuckets may change while nbatch == 1 because of dynamic
    1886             :  * bucket count growth.  Once we start batching, the value is fixed and does
    1887             :  * not change over the course of the join (making it possible to compute batch
    1888             :  * number the way we do here).
    1889             :  *
    1890             :  * nbatch is always a power of 2; we increase it only by doubling it.  This
    1891             :  * effectively adds one more bit to the top of the batchno.  In very large
    1892             :  * joins, we might run out of bits to add, so we do this by rotating the hash
    1893             :  * value.  This causes batchno to steal bits from bucketno when the number of
    1894             :  * virtual buckets exceeds 2^32.  It's better to have longer bucket chains
    1895             :  * than to lose the ability to divide batches.
    1896             :  */
    1897             : void
    1898    23909520 : ExecHashGetBucketAndBatch(HashJoinTable hashtable,
    1899             :                           uint32 hashvalue,
    1900             :                           int *bucketno,
    1901             :                           int *batchno)
    1902             : {
    1903    23909520 :     uint32      nbuckets = (uint32) hashtable->nbuckets;
    1904    23909520 :     uint32      nbatch = (uint32) hashtable->nbatch;
    1905             : 
    1906    23909520 :     if (nbatch > 1)
    1907             :     {
    1908     9307352 :         *bucketno = hashvalue & (nbuckets - 1);
    1909     9307352 :         *batchno = pg_rotate_right32(hashvalue,
    1910     9307352 :                                      hashtable->log2_nbuckets) & (nbatch - 1);
    1911             :     }
    1912             :     else
    1913             :     {
    1914    14602168 :         *bucketno = hashvalue & (nbuckets - 1);
    1915    14602168 :         *batchno = 0;
    1916             :     }
    1917    23909520 : }
    1918             : 
    1919             : /*
    1920             :  * ExecScanHashBucket
    1921             :  *      scan a hash bucket for matches to the current outer tuple
    1922             :  *
    1923             :  * The current outer tuple must be stored in econtext->ecxt_outertuple.
    1924             :  *
    1925             :  * On success, the inner tuple is stored into hjstate->hj_CurTuple and
    1926             :  * econtext->ecxt_innertuple, using hjstate->hj_HashTupleSlot as the slot
    1927             :  * for the latter.
    1928             :  */
    1929             : bool
    1930    10416552 : ExecScanHashBucket(HashJoinState *hjstate,
    1931             :                    ExprContext *econtext)
    1932             : {
    1933    10416552 :     ExprState  *hjclauses = hjstate->hashclauses;
    1934    10416552 :     HashJoinTable hashtable = hjstate->hj_HashTable;
    1935    10416552 :     HashJoinTuple hashTuple = hjstate->hj_CurTuple;
    1936    10416552 :     uint32      hashvalue = hjstate->hj_CurHashValue;
    1937             : 
    1938             :     /*
    1939             :      * hj_CurTuple is the address of the tuple last returned from the current
    1940             :      * bucket, or NULL if it's time to start scanning a new bucket.
    1941             :      *
    1942             :      * If the tuple hashed to a skew bucket then scan the skew bucket
    1943             :      * otherwise scan the standard hashtable bucket.
    1944             :      */
    1945    10416552 :     if (hashTuple != NULL)
    1946     2689222 :         hashTuple = hashTuple->next.unshared;
    1947     7727330 :     else if (hjstate->hj_CurSkewBucketNo != INVALID_SKEW_BUCKET_NO)
    1948         800 :         hashTuple = hashtable->skewBucket[hjstate->hj_CurSkewBucketNo]->tuples;
    1949             :     else
    1950     7726530 :         hashTuple = hashtable->buckets.unshared[hjstate->hj_CurBucketNo];
    1951             : 
    1952    12862446 :     while (hashTuple != NULL)
    1953             :     {
    1954     7009894 :         if (hashTuple->hashvalue == hashvalue)
    1955             :         {
    1956             :             TupleTableSlot *inntuple;
    1957             : 
    1958             :             /* insert hashtable's tuple into exec slot so ExecQual sees it */
    1959     4564004 :             inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
    1960             :                                              hjstate->hj_HashTupleSlot,
    1961             :                                              false);    /* do not pfree */
    1962     4564004 :             econtext->ecxt_innertuple = inntuple;
    1963             : 
    1964     4564004 :             if (ExecQualAndReset(hjclauses, econtext))
    1965             :             {
    1966     4564000 :                 hjstate->hj_CurTuple = hashTuple;
    1967     4564000 :                 return true;
    1968             :             }
    1969             :         }
    1970             : 
    1971     2445894 :         hashTuple = hashTuple->next.unshared;
    1972             :     }
    1973             : 
    1974             :     /*
    1975             :      * no match
    1976             :      */
    1977     5852552 :     return false;
    1978             : }
    1979             : 
    1980             : /*
    1981             :  * ExecParallelScanHashBucket
    1982             :  *      scan a hash bucket for matches to the current outer tuple
    1983             :  *
    1984             :  * The current outer tuple must be stored in econtext->ecxt_outertuple.
    1985             :  *
    1986             :  * On success, the inner tuple is stored into hjstate->hj_CurTuple and
    1987             :  * econtext->ecxt_innertuple, using hjstate->hj_HashTupleSlot as the slot
    1988             :  * for the latter.
    1989             :  */
    1990             : bool
    1991     2400032 : ExecParallelScanHashBucket(HashJoinState *hjstate,
    1992             :                            ExprContext *econtext)
    1993             : {
    1994     2400032 :     ExprState  *hjclauses = hjstate->hashclauses;
    1995     2400032 :     HashJoinTable hashtable = hjstate->hj_HashTable;
    1996     2400032 :     HashJoinTuple hashTuple = hjstate->hj_CurTuple;
    1997     2400032 :     uint32      hashvalue = hjstate->hj_CurHashValue;
    1998             : 
    1999             :     /*
    2000             :      * hj_CurTuple is the address of the tuple last returned from the current
    2001             :      * bucket, or NULL if it's time to start scanning a new bucket.
    2002             :      */
    2003     2400032 :     if (hashTuple != NULL)
    2004     1200016 :         hashTuple = ExecParallelHashNextTuple(hashtable, hashTuple);
    2005             :     else
    2006     1200016 :         hashTuple = ExecParallelHashFirstTuple(hashtable,
    2007             :                                                hjstate->hj_CurBucketNo);
    2008             : 
    2009     3186808 :     while (hashTuple != NULL)
    2010             :     {
    2011     1986792 :         if (hashTuple->hashvalue == hashvalue)
    2012             :         {
    2013             :             TupleTableSlot *inntuple;
    2014             : 
    2015             :             /* insert hashtable's tuple into exec slot so ExecQual sees it */
    2016     1200016 :             inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
    2017             :                                              hjstate->hj_HashTupleSlot,
    2018             :                                              false);    /* do not pfree */
    2019     1200016 :             econtext->ecxt_innertuple = inntuple;
    2020             : 
    2021     1200016 :             if (ExecQualAndReset(hjclauses, econtext))
    2022             :             {
    2023     1200016 :                 hjstate->hj_CurTuple = hashTuple;
    2024     1200016 :                 return true;
    2025             :             }
    2026             :         }
    2027             : 
    2028      786776 :         hashTuple = ExecParallelHashNextTuple(hashtable, hashTuple);
    2029             :     }
    2030             : 
    2031             :     /*
    2032             :      * no match
    2033             :      */
    2034     1200016 :     return false;
    2035             : }
    2036             : 
    2037             : /*
    2038             :  * ExecPrepHashTableForUnmatched
    2039             :  *      set up for a series of ExecScanHashTableForUnmatched calls
    2040             :  */
    2041             : void
    2042        4890 : ExecPrepHashTableForUnmatched(HashJoinState *hjstate)
    2043             : {
    2044             :     /*----------
    2045             :      * During this scan we use the HashJoinState fields as follows:
    2046             :      *
    2047             :      * hj_CurBucketNo: next regular bucket to scan
    2048             :      * hj_CurSkewBucketNo: next skew bucket (an index into skewBucketNums)
    2049             :      * hj_CurTuple: last tuple returned, or NULL to start next bucket
    2050             :      *----------
    2051             :      */
    2052        4890 :     hjstate->hj_CurBucketNo = 0;
    2053        4890 :     hjstate->hj_CurSkewBucketNo = 0;
    2054        4890 :     hjstate->hj_CurTuple = NULL;
    2055        4890 : }
    2056             : 
    2057             : /*
    2058             :  * ExecScanHashTableForUnmatched
    2059             :  *      scan the hash table for unmatched inner tuples
    2060             :  *
    2061             :  * On success, the inner tuple is stored into hjstate->hj_CurTuple and
    2062             :  * econtext->ecxt_innertuple, using hjstate->hj_HashTupleSlot as the slot
    2063             :  * for the latter.
    2064             :  */
    2065             : bool
    2066      210966 : ExecScanHashTableForUnmatched(HashJoinState *hjstate, ExprContext *econtext)
    2067             : {
    2068      210966 :     HashJoinTable hashtable = hjstate->hj_HashTable;
    2069      210966 :     HashJoinTuple hashTuple = hjstate->hj_CurTuple;
    2070             : 
    2071             :     for (;;)
    2072             :     {
    2073             :         /*
    2074             :          * hj_CurTuple is the address of the tuple last returned from the
    2075             :          * current bucket, or NULL if it's time to start scanning a new
    2076             :          * bucket.
    2077             :          */
    2078     5836822 :         if (hashTuple != NULL)
    2079      206076 :             hashTuple = hashTuple->next.unshared;
    2080     5630746 :         else if (hjstate->hj_CurBucketNo < hashtable->nbuckets)
    2081             :         {
    2082     5625860 :             hashTuple = hashtable->buckets.unshared[hjstate->hj_CurBucketNo];
    2083     5625860 :             hjstate->hj_CurBucketNo++;
    2084             :         }
    2085        4886 :         else if (hjstate->hj_CurSkewBucketNo < hashtable->nSkewBuckets)
    2086             :         {
    2087           0 :             int         j = hashtable->skewBucketNums[hjstate->hj_CurSkewBucketNo];
    2088             : 
    2089           0 :             hashTuple = hashtable->skewBucket[j]->tuples;
    2090           0 :             hjstate->hj_CurSkewBucketNo++;
    2091             :         }
    2092             :         else
    2093        4886 :             break;              /* finished all buckets */
    2094             : 
    2095     6062774 :         while (hashTuple != NULL)
    2096             :         {
    2097      436918 :             if (!HeapTupleHeaderHasMatch(HJTUPLE_MINTUPLE(hashTuple)))
    2098             :             {
    2099             :                 TupleTableSlot *inntuple;
    2100             : 
    2101             :                 /* insert hashtable's tuple into exec slot */
    2102      206080 :                 inntuple = ExecStoreMinimalTuple(HJTUPLE_MINTUPLE(hashTuple),
    2103             :                                                  hjstate->hj_HashTupleSlot,
    2104             :                                                  false);    /* do not pfree */
    2105      206080 :                 econtext->ecxt_innertuple = inntuple;
    2106             : 
    2107             :                 /*
    2108             :                  * Reset temp memory each time; although this function doesn't
    2109             :                  * do any qual eval, the caller will, so let's keep it
    2110             :                  * parallel to ExecScanHashBucket.
    2111             :                  */
    2112      206080 :                 ResetExprContext(econtext);
    2113             : 
    2114      206080 :                 hjstate->hj_CurTuple = hashTuple;
    2115      206080 :                 return true;
    2116             :             }
    2117             : 
    2118      230838 :             hashTuple = hashTuple->next.unshared;
    2119             :         }
    2120             : 
    2121             :         /* allow this loop to be cancellable */
    2122     5625856 :         CHECK_FOR_INTERRUPTS();
    2123             :     }
    2124             : 
    2125             :     /*
    2126             :      * no more unmatched tuples
    2127             :      */
    2128        4886 :     return false;
    2129             : }
    2130             : 
    2131             : /*
    2132             :  * ExecHashTableReset
    2133             :  *
    2134             :  *      reset hash table header for new batch
    2135             :  */
    2136             : void
    2137         946 : ExecHashTableReset(HashJoinTable hashtable)
    2138             : {
    2139             :     MemoryContext oldcxt;
    2140         946 :     int         nbuckets = hashtable->nbuckets;
    2141             : 
    2142             :     /*
    2143             :      * Release all the hash buckets and tuples acquired in the prior pass, and
    2144             :      * reinitialize the context for a new pass.
    2145             :      */
    2146         946 :     MemoryContextReset(hashtable->batchCxt);
    2147         946 :     oldcxt = MemoryContextSwitchTo(hashtable->batchCxt);
    2148             : 
    2149             :     /* Reallocate and reinitialize the hash bucket headers. */
    2150         946 :     hashtable->buckets.unshared = (HashJoinTuple *)
    2151         946 :         palloc0(nbuckets * sizeof(HashJoinTuple));
    2152             : 
    2153         946 :     hashtable->spaceUsed = 0;
    2154             : 
    2155         946 :     MemoryContextSwitchTo(oldcxt);
    2156             : 
    2157             :     /* Forget the chunks (the memory was freed by the context reset above). */
    2158         946 :     hashtable->chunks = NULL;
    2159         946 : }
    2160             : 
    2161             : /*
    2162             :  * ExecHashTableResetMatchFlags
    2163             :  *      Clear all the HeapTupleHeaderHasMatch flags in the table
    2164             :  */
    2165             : void
    2166          20 : ExecHashTableResetMatchFlags(HashJoinTable hashtable)
    2167             : {
    2168             :     HashJoinTuple tuple;
    2169             :     int         i;
    2170             : 
    2171             :     /* Reset all flags in the main table ... */
    2172       20500 :     for (i = 0; i < hashtable->nbuckets; i++)
    2173             :     {
    2174       20568 :         for (tuple = hashtable->buckets.unshared[i]; tuple != NULL;
    2175          88 :              tuple = tuple->next.unshared)
    2176          88 :             HeapTupleHeaderClearMatch(HJTUPLE_MINTUPLE(tuple));
    2177             :     }
    2178             : 
    2179             :     /* ... and the same for the skew buckets, if any */
    2180          20 :     for (i = 0; i < hashtable->nSkewBuckets; i++)
    2181             :     {
    2182           0 :         int         j = hashtable->skewBucketNums[i];
    2183           0 :         HashSkewBucket *skewBucket = hashtable->skewBucket[j];
    2184             : 
    2185           0 :         for (tuple = skewBucket->tuples; tuple != NULL; tuple = tuple->next.unshared)
    2186           0 :             HeapTupleHeaderClearMatch(HJTUPLE_MINTUPLE(tuple));
    2187             :     }
    2188          20 : }
    2189             : 
    2190             : 
    2191             : void
    2192      252190 : ExecReScanHash(HashState *node)
    2193             : {
    2194             :     /*
    2195             :      * if chgParam of subnode is not null then plan will be re-scanned by
    2196             :      * first ExecProcNode.
    2197             :      */
    2198      252190 :     if (node->ps.lefttree->chgParam == NULL)
    2199           0 :         ExecReScan(node->ps.lefttree);
    2200      252190 : }
    2201             : 
    2202             : 
    2203             : /*
    2204             :  * ExecHashBuildSkewHash
    2205             :  *
    2206             :  *      Set up for skew optimization if we can identify the most common values
    2207             :  *      (MCVs) of the outer relation's join key.  We make a skew hash bucket
    2208             :  *      for the hash value of each MCV, up to the number of slots allowed
    2209             :  *      based on available memory.
    2210             :  */
    2211             : static void
    2212          78 : ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
    2213             : {
    2214             :     HeapTupleData *statsTuple;
    2215             :     AttStatsSlot sslot;
    2216             : 
    2217             :     /* Do nothing if planner didn't identify the outer relation's join key */
    2218          78 :     if (!OidIsValid(node->skewTable))
    2219          48 :         return;
    2220             :     /* Also, do nothing if we don't have room for at least one skew bucket */
    2221          78 :     if (mcvsToUse <= 0)
    2222           0 :         return;
    2223             : 
    2224             :     /*
    2225             :      * Try to find the MCV statistics for the outer relation's join key.
    2226             :      */
    2227          78 :     statsTuple = SearchSysCache3(STATRELATTINH,
    2228          78 :                                  ObjectIdGetDatum(node->skewTable),
    2229          78 :                                  Int16GetDatum(node->skewColumn),
    2230          78 :                                  BoolGetDatum(node->skewInherit));
    2231          78 :     if (!HeapTupleIsValid(statsTuple))
    2232          48 :         return;
    2233             : 
    2234          30 :     if (get_attstatsslot(&sslot, statsTuple,
    2235             :                          STATISTIC_KIND_MCV, InvalidOid,
    2236             :                          ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
    2237             :     {
    2238             :         double      frac;
    2239             :         int         nbuckets;
    2240             :         FmgrInfo   *hashfunctions;
    2241             :         int         i;
    2242             : 
    2243           4 :         if (mcvsToUse > sslot.nvalues)
    2244           0 :             mcvsToUse = sslot.nvalues;
    2245             : 
    2246             :         /*
    2247             :          * Calculate the expected fraction of outer relation that will
    2248             :          * participate in the skew optimization.  If this isn't at least
    2249             :          * SKEW_MIN_OUTER_FRACTION, don't use skew optimization.
    2250             :          */
    2251           4 :         frac = 0;
    2252          44 :         for (i = 0; i < mcvsToUse; i++)
    2253          40 :             frac += sslot.numbers[i];
    2254           4 :         if (frac < SKEW_MIN_OUTER_FRACTION)
    2255             :         {
    2256           0 :             free_attstatsslot(&sslot);
    2257           0 :             ReleaseSysCache(statsTuple);
    2258           0 :             return;
    2259             :         }
    2260             : 
    2261             :         /*
    2262             :          * Okay, set up the skew hashtable.
    2263             :          *
    2264             :          * skewBucket[] is an open addressing hashtable with a power of 2 size
    2265             :          * that is greater than the number of MCV values.  (This ensures there
    2266             :          * will be at least one null entry, so searches will always
    2267             :          * terminate.)
    2268             :          *
    2269             :          * Note: this code could fail if mcvsToUse exceeds INT_MAX/8 or
    2270             :          * MaxAllocSize/sizeof(void *)/8, but that is not currently possible
    2271             :          * since we limit pg_statistic entries to much less than that.
    2272             :          */
    2273           4 :         nbuckets = pg_nextpower2_32(mcvsToUse + 1);
    2274             :         /* use two more bits just to help avoid collisions */
    2275           4 :         nbuckets <<= 2;
    2276             : 
    2277           4 :         hashtable->skewEnabled = true;
    2278           4 :         hashtable->skewBucketLen = nbuckets;
    2279             : 
    2280             :         /*
    2281             :          * We allocate the bucket memory in the hashtable's batch context. It
    2282             :          * is only needed during the first batch, and this ensures it will be
    2283             :          * automatically removed once the first batch is done.
    2284             :          */
    2285           4 :         hashtable->skewBucket = (HashSkewBucket **)
    2286           4 :             MemoryContextAllocZero(hashtable->batchCxt,
    2287             :                                    nbuckets * sizeof(HashSkewBucket *));
    2288           4 :         hashtable->skewBucketNums = (int *)
    2289           4 :             MemoryContextAllocZero(hashtable->batchCxt,
    2290             :                                    mcvsToUse * sizeof(int));
    2291             : 
    2292           8 :         hashtable->spaceUsed += nbuckets * sizeof(HashSkewBucket *)
    2293           4 :             + mcvsToUse * sizeof(int);
    2294           8 :         hashtable->spaceUsedSkew += nbuckets * sizeof(HashSkewBucket *)
    2295           4 :             + mcvsToUse * sizeof(int);
    2296           4 :         if (hashtable->spaceUsed > hashtable->spacePeak)
    2297           4 :             hashtable->spacePeak = hashtable->spaceUsed;
    2298             : 
    2299             :         /*
    2300             :          * Create a skew bucket for each MCV hash value.
    2301             :          *
    2302             :          * Note: it is very important that we create the buckets in order of
    2303             :          * decreasing MCV frequency.  If we have to remove some buckets, they
    2304             :          * must be removed in reverse order of creation (see notes in
    2305             :          * ExecHashRemoveNextSkewBucket) and we want the least common MCVs to
    2306             :          * be removed first.
    2307             :          */
    2308           4 :         hashfunctions = hashtable->outer_hashfunctions;
    2309             : 
    2310          44 :         for (i = 0; i < mcvsToUse; i++)
    2311             :         {
    2312             :             uint32      hashvalue;
    2313             :             int         bucket;
    2314             : 
    2315          40 :             hashvalue = DatumGetUInt32(FunctionCall1Coll(&hashfunctions[0],
    2316             :                                                          hashtable->collations[0],
    2317             :                                                          sslot.values[i]));
    2318             : 
    2319             :             /*
    2320             :              * While we have not hit a hole in the hashtable and have not hit
    2321             :              * the desired bucket, we have collided with some previous hash
    2322             :              * value, so try the next bucket location.  NB: this code must
    2323             :              * match ExecHashGetSkewBucket.
    2324             :              */
    2325          40 :             bucket = hashvalue & (nbuckets - 1);
    2326          40 :             while (hashtable->skewBucket[bucket] != NULL &&
    2327           0 :                    hashtable->skewBucket[bucket]->hashvalue != hashvalue)
    2328           0 :                 bucket = (bucket + 1) & (nbuckets - 1);
    2329             : 
    2330             :             /*
    2331             :              * If we found an existing bucket with the same hashvalue, leave
    2332             :              * it alone.  It's okay for two MCVs to share a hashvalue.
    2333             :              */
    2334          40 :             if (hashtable->skewBucket[bucket] != NULL)
    2335           0 :                 continue;
    2336             : 
    2337             :             /* Okay, create a new skew bucket for this hashvalue. */
    2338          80 :             hashtable->skewBucket[bucket] = (HashSkewBucket *)
    2339          40 :                 MemoryContextAlloc(hashtable->batchCxt,
    2340             :                                    sizeof(HashSkewBucket));
    2341          40 :             hashtable->skewBucket[bucket]->hashvalue = hashvalue;
    2342          40 :             hashtable->skewBucket[bucket]->tuples = NULL;
    2343          40 :             hashtable->skewBucketNums[hashtable->nSkewBuckets] = bucket;
    2344          40 :             hashtable->nSkewBuckets++;
    2345          40 :             hashtable->spaceUsed += SKEW_BUCKET_OVERHEAD;
    2346          40 :             hashtable->spaceUsedSkew += SKEW_BUCKET_OVERHEAD;
    2347          40 :             if (hashtable->spaceUsed > hashtable->spacePeak)
    2348          40 :                 hashtable->spacePeak = hashtable->spaceUsed;
    2349             :         }
    2350             : 
    2351           4 :         free_attstatsslot(&sslot);
    2352             :     }
    2353             : 
    2354          30 :     ReleaseSysCache(statsTuple);
    2355             : }
    2356             : 
    2357             : /*
    2358             :  * ExecHashGetSkewBucket
    2359             :  *
    2360             :  *      Returns the index of the skew bucket for this hashvalue,
    2361             :  *      or INVALID_SKEW_BUCKET_NO if the hashvalue is not
    2362             :  *      associated with any active skew bucket.
    2363             :  */
    2364             : int
    2365    17135678 : ExecHashGetSkewBucket(HashJoinTable hashtable, uint32 hashvalue)
    2366             : {
    2367             :     int         bucket;
    2368             : 
    2369             :     /*
    2370             :      * Always return INVALID_SKEW_BUCKET_NO if not doing skew optimization (in
    2371             :      * particular, this happens after the initial batch is done).
    2372             :      */
    2373    17135678 :     if (!hashtable->skewEnabled)
    2374    17055678 :         return INVALID_SKEW_BUCKET_NO;
    2375             : 
    2376             :     /*
    2377             :      * Since skewBucketLen is a power of 2, we can do a modulo by ANDing.
    2378             :      */
    2379       80000 :     bucket = hashvalue & (hashtable->skewBucketLen - 1);
    2380             : 
    2381             :     /*
    2382             :      * While we have not hit a hole in the hashtable and have not hit the
    2383             :      * desired bucket, we have collided with some other hash value, so try the
    2384             :      * next bucket location.
    2385             :      */
    2386       84484 :     while (hashtable->skewBucket[bucket] != NULL &&
    2387        5456 :            hashtable->skewBucket[bucket]->hashvalue != hashvalue)
    2388        4484 :         bucket = (bucket + 1) & (hashtable->skewBucketLen - 1);
    2389             : 
    2390             :     /*
    2391             :      * Found the desired bucket?
    2392             :      */
    2393       80000 :     if (hashtable->skewBucket[bucket] != NULL)
    2394         972 :         return bucket;
    2395             : 
    2396             :     /*
    2397             :      * There must not be any hashtable entry for this hash value.
    2398             :      */
    2399       79028 :     return INVALID_SKEW_BUCKET_NO;
    2400             : }
    2401             : 
    2402             : /*
    2403             :  * ExecHashSkewTableInsert
    2404             :  *
    2405             :  *      Insert a tuple into the skew hashtable.
    2406             :  *
    2407             :  * This should generally match up with the current-batch case in
    2408             :  * ExecHashTableInsert.
    2409             :  */
    2410             : static void
    2411         172 : ExecHashSkewTableInsert(HashJoinTable hashtable,
    2412             :                         TupleTableSlot *slot,
    2413             :                         uint32 hashvalue,
    2414             :                         int bucketNumber)
    2415             : {
    2416             :     bool        shouldFree;
    2417         172 :     MinimalTuple tuple = ExecFetchSlotMinimalTuple(slot, &shouldFree);
    2418             :     HashJoinTuple hashTuple;
    2419             :     int         hashTupleSize;
    2420             : 
    2421             :     /* Create the HashJoinTuple */
    2422         172 :     hashTupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
    2423         172 :     hashTuple = (HashJoinTuple) MemoryContextAlloc(hashtable->batchCxt,
    2424             :                                                    hashTupleSize);
    2425         172 :     hashTuple->hashvalue = hashvalue;
    2426         172 :     memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
    2427         172 :     HeapTupleHeaderClearMatch(HJTUPLE_MINTUPLE(hashTuple));
    2428             : 
    2429             :     /* Push it onto the front of the skew bucket's list */
    2430         172 :     hashTuple->next.unshared = hashtable->skewBucket[bucketNumber]->tuples;
    2431         172 :     hashtable->skewBucket[bucketNumber]->tuples = hashTuple;
    2432             :     Assert(hashTuple != hashTuple->next.unshared);
    2433             : 
    2434             :     /* Account for space used, and back off if we've used too much */
    2435         172 :     hashtable->spaceUsed += hashTupleSize;
    2436         172 :     hashtable->spaceUsedSkew += hashTupleSize;
    2437         172 :     if (hashtable->spaceUsed > hashtable->spacePeak)
    2438         128 :         hashtable->spacePeak = hashtable->spaceUsed;
    2439         204 :     while (hashtable->spaceUsedSkew > hashtable->spaceAllowedSkew)
    2440          32 :         ExecHashRemoveNextSkewBucket(hashtable);
    2441             : 
    2442             :     /* Check we are not over the total spaceAllowed, either */
    2443         172 :     if (hashtable->spaceUsed > hashtable->spaceAllowed)
    2444           0 :         ExecHashIncreaseNumBatches(hashtable);
    2445             : 
    2446         172 :     if (shouldFree)
    2447         172 :         heap_free_minimal_tuple(tuple);
    2448         172 : }
    2449             : 
    2450             : /*
    2451             :  *      ExecHashRemoveNextSkewBucket
    2452             :  *
    2453             :  *      Remove the least valuable skew bucket by pushing its tuples into
    2454             :  *      the main hash table.
    2455             :  */
    2456             : static void
    2457          32 : ExecHashRemoveNextSkewBucket(HashJoinTable hashtable)
    2458             : {
    2459             :     int         bucketToRemove;
    2460             :     HashSkewBucket *bucket;
    2461             :     uint32      hashvalue;
    2462             :     int         bucketno;
    2463             :     int         batchno;
    2464             :     HashJoinTuple hashTuple;
    2465             : 
    2466             :     /* Locate the bucket to remove */
    2467          32 :     bucketToRemove = hashtable->skewBucketNums[hashtable->nSkewBuckets - 1];
    2468          32 :     bucket = hashtable->skewBucket[bucketToRemove];
    2469             : 
    2470             :     /*
    2471             :      * Calculate which bucket and batch the tuples belong to in the main
    2472             :      * hashtable.  They all have the same hash value, so it's the same for all
    2473             :      * of them.  Also note that it's not possible for nbatch to increase while
    2474             :      * we are processing the tuples.
    2475             :      */
    2476          32 :     hashvalue = bucket->hashvalue;
    2477          32 :     ExecHashGetBucketAndBatch(hashtable, hashvalue, &bucketno, &batchno);
    2478             : 
    2479             :     /* Process all tuples in the bucket */
    2480          32 :     hashTuple = bucket->tuples;
    2481         124 :     while (hashTuple != NULL)
    2482             :     {
    2483          92 :         HashJoinTuple nextHashTuple = hashTuple->next.unshared;
    2484             :         MinimalTuple tuple;
    2485             :         Size        tupleSize;
    2486             : 
    2487             :         /*
    2488             :          * This code must agree with ExecHashTableInsert.  We do not use
    2489             :          * ExecHashTableInsert directly as ExecHashTableInsert expects a
    2490             :          * TupleTableSlot while we already have HashJoinTuples.
    2491             :          */
    2492          92 :         tuple = HJTUPLE_MINTUPLE(hashTuple);
    2493          92 :         tupleSize = HJTUPLE_OVERHEAD + tuple->t_len;
    2494             : 
    2495             :         /* Decide whether to put the tuple in the hash table or a temp file */
    2496          92 :         if (batchno == hashtable->curbatch)
    2497             :         {
    2498             :             /* Move the tuple to the main hash table */
    2499             :             HashJoinTuple copyTuple;
    2500             : 
    2501             :             /*
    2502             :              * We must copy the tuple into the dense storage, else it will not
    2503             :              * be found by, eg, ExecHashIncreaseNumBatches.
    2504             :              */
    2505           8 :             copyTuple = (HashJoinTuple) dense_alloc(hashtable, tupleSize);
    2506           8 :             memcpy(copyTuple, hashTuple, tupleSize);
    2507           8 :             pfree(hashTuple);
    2508             : 
    2509           8 :             copyTuple->next.unshared = hashtable->buckets.unshared[bucketno];
    2510           8 :             hashtable->buckets.unshared[bucketno] = copyTuple;
    2511             : 
    2512             :             /* We have reduced skew space, but overall space doesn't change */
    2513           8 :             hashtable->spaceUsedSkew -= tupleSize;
    2514             :         }
    2515             :         else
    2516             :         {
    2517             :             /* Put the tuple into a temp file for later batches */
    2518             :             Assert(batchno > hashtable->curbatch);
    2519          84 :             ExecHashJoinSaveTuple(tuple, hashvalue,
    2520          84 :                                   &hashtable->innerBatchFile[batchno]);
    2521          84 :             pfree(hashTuple);
    2522          84 :             hashtable->spaceUsed -= tupleSize;
    2523          84 :             hashtable->spaceUsedSkew -= tupleSize;
    2524             :         }
    2525             : 
    2526          92 :         hashTuple = nextHashTuple;
    2527             : 
    2528             :         /* allow this loop to be cancellable */
    2529          92 :         CHECK_FOR_INTERRUPTS();
    2530             :     }
    2531             : 
    2532             :     /*
    2533             :      * Free the bucket struct itself and reset the hashtable entry to NULL.
    2534             :      *
    2535             :      * NOTE: this is not nearly as simple as it looks on the surface, because
    2536             :      * of the possibility of collisions in the hashtable.  Suppose that hash
    2537             :      * values A and B collide at a particular hashtable entry, and that A was
    2538             :      * entered first so B gets shifted to a different table entry.  If we were
    2539             :      * to remove A first then ExecHashGetSkewBucket would mistakenly start
    2540             :      * reporting that B is not in the hashtable, because it would hit the NULL
    2541             :      * before finding B.  However, we always remove entries in the reverse
    2542             :      * order of creation, so this failure cannot happen.
    2543             :      */
    2544          32 :     hashtable->skewBucket[bucketToRemove] = NULL;
    2545          32 :     hashtable->nSkewBuckets--;
    2546          32 :     pfree(bucket);
    2547          32 :     hashtable->spaceUsed -= SKEW_BUCKET_OVERHEAD;
    2548          32 :     hashtable->spaceUsedSkew -= SKEW_BUCKET_OVERHEAD;
    2549             : 
    2550             :     /*
    2551             :      * If we have removed all skew buckets then give up on skew optimization.
    2552             :      * Release the arrays since they aren't useful any more.
    2553             :      */
    2554          32 :     if (hashtable->nSkewBuckets == 0)
    2555             :     {
    2556           0 :         hashtable->skewEnabled = false;
    2557           0 :         pfree(hashtable->skewBucket);
    2558           0 :         pfree(hashtable->skewBucketNums);
    2559           0 :         hashtable->skewBucket = NULL;
    2560           0 :         hashtable->skewBucketNums = NULL;
    2561           0 :         hashtable->spaceUsed -= hashtable->spaceUsedSkew;
    2562           0 :         hashtable->spaceUsedSkew = 0;
    2563             :     }
    2564          32 : }
    2565             : 
    2566             : /*
    2567             :  * Reserve space in the DSM segment for instrumentation data.
    2568             :  */
    2569             : void
    2570         112 : ExecHashEstimate(HashState *node, ParallelContext *pcxt)
    2571             : {
    2572             :     size_t      size;
    2573             : 
    2574             :     /* don't need this if not instrumenting or no workers */
    2575         112 :     if (!node->ps.instrument || pcxt->nworkers == 0)
    2576          56 :         return;
    2577             : 
    2578          56 :     size = mul_size(pcxt->nworkers, sizeof(HashInstrumentation));
    2579          56 :     size = add_size(size, offsetof(SharedHashInfo, hinstrument));
    2580          56 :     shm_toc_estimate_chunk(&pcxt->estimator, size);
    2581          56 :     shm_toc_estimate_keys(&pcxt->estimator, 1);
    2582             : }
    2583             : 
    2584             : /*
    2585             :  * Set up a space in the DSM for all workers to record instrumentation data
    2586             :  * about their hash table.
    2587             :  */
    2588             : void
    2589         112 : ExecHashInitializeDSM(HashState *node, ParallelContext *pcxt)
    2590             : {
    2591             :     size_t      size;
    2592             : 
    2593             :     /* don't need this if not instrumenting or no workers */
    2594         112 :     if (!node->ps.instrument || pcxt->nworkers == 0)
    2595          56 :         return;
    2596             : 
    2597          56 :     size = offsetof(SharedHashInfo, hinstrument) +
    2598          56 :         pcxt->nworkers * sizeof(HashInstrumentation);
    2599          56 :     node->shared_info = (SharedHashInfo *) shm_toc_allocate(pcxt->toc, size);
    2600             : 
    2601             :     /* Each per-worker area must start out as zeroes. */
    2602          56 :     memset(node->shared_info, 0, size);
    2603             : 
    2604          56 :     node->shared_info->num_workers = pcxt->nworkers;
    2605          56 :     shm_toc_insert(pcxt->toc, node->ps.plan->plan_node_id,
    2606          56 :                    node->shared_info);
    2607             : }
    2608             : 
    2609             : /*
    2610             :  * Locate the DSM space for hash table instrumentation data that we'll write
    2611             :  * to at shutdown time.
    2612             :  */
    2613             : void
    2614         340 : ExecHashInitializeWorker(HashState *node, ParallelWorkerContext *pwcxt)
    2615             : {
    2616             :     SharedHashInfo *shared_info;
    2617             : 
    2618             :     /* don't need this if not instrumenting */
    2619         340 :     if (!node->ps.instrument)
    2620         172 :         return;
    2621             : 
    2622             :     /*
    2623             :      * Find our entry in the shared area, and set up a pointer to it so that
    2624             :      * we'll accumulate stats there when shutting down or rebuilding the hash
    2625             :      * table.
    2626             :      */
    2627             :     shared_info = (SharedHashInfo *)
    2628         168 :         shm_toc_lookup(pwcxt->toc, node->ps.plan->plan_node_id, false);
    2629         168 :     node->hinstrument = &shared_info->hinstrument[ParallelWorkerNumber];
    2630             : }
    2631             : 
    2632             : /*
    2633             :  * Collect EXPLAIN stats if needed, saving them into DSM memory if
    2634             :  * ExecHashInitializeWorker was called, or local storage if not.  In the
    2635             :  * parallel case, this must be done in ExecShutdownHash() rather than
    2636             :  * ExecEndHash() because the latter runs after we've detached from the DSM
    2637             :  * segment.
    2638             :  */
    2639             : void
    2640       27554 : ExecShutdownHash(HashState *node)
    2641             : {
    2642             :     /* Allocate save space if EXPLAIN'ing and we didn't do so already */
    2643       27554 :     if (node->ps.instrument && !node->hinstrument)
    2644          72 :         node->hinstrument = (HashInstrumentation *)
    2645          72 :             palloc0(sizeof(HashInstrumentation));
    2646             :     /* Now accumulate data for the current (final) hash table */
    2647       27554 :     if (node->hinstrument && node->hashtable)
    2648         218 :         ExecHashAccumInstrumentation(node->hinstrument, node->hashtable);
    2649       27554 : }
    2650             : 
    2651             : /*
    2652             :  * Retrieve instrumentation data from workers before the DSM segment is
    2653             :  * detached, so that EXPLAIN can access it.
    2654             :  */
    2655             : void
    2656          56 : ExecHashRetrieveInstrumentation(HashState *node)
    2657             : {
    2658          56 :     SharedHashInfo *shared_info = node->shared_info;
    2659             :     size_t      size;
    2660             : 
    2661          56 :     if (shared_info == NULL)
    2662           0 :         return;
    2663             : 
    2664             :     /* Replace node->shared_info with a copy in backend-local memory. */
    2665          56 :     size = offsetof(SharedHashInfo, hinstrument) +
    2666          56 :         shared_info->num_workers * sizeof(HashInstrumentation);
    2667          56 :     node->shared_info = palloc(size);
    2668          56 :     memcpy(node->shared_info, shared_info, size);
    2669             : }
    2670             : 
    2671             : /*
    2672             :  * Accumulate instrumentation data from 'hashtable' into an
    2673             :  * initially-zeroed HashInstrumentation struct.
    2674             :  *
    2675             :  * This is used to merge information across successive hash table instances
    2676             :  * within a single plan node.  We take the maximum values of each interesting
    2677             :  * number.  The largest nbuckets and largest nbatch values might have occurred
    2678             :  * in different instances, so there's some risk of confusion from reporting
    2679             :  * unrelated numbers; but there's a bigger risk of misdiagnosing a performance
    2680             :  * issue if we don't report the largest values.  Similarly, we want to report
    2681             :  * the largest spacePeak regardless of whether it happened in the same
    2682             :  * instance as the largest nbuckets or nbatch.  All the instances should have
    2683             :  * the same nbuckets_original and nbatch_original; but there's little value
    2684             :  * in depending on that here, so handle them the same way.
    2685             :  */
    2686             : void
    2687         218 : ExecHashAccumInstrumentation(HashInstrumentation *instrument,
    2688             :                              HashJoinTable hashtable)
    2689             : {
    2690         218 :     instrument->nbuckets = Max(instrument->nbuckets,
    2691             :                                hashtable->nbuckets);
    2692         218 :     instrument->nbuckets_original = Max(instrument->nbuckets_original,
    2693             :                                         hashtable->nbuckets_original);
    2694         218 :     instrument->nbatch = Max(instrument->nbatch,
    2695             :                              hashtable->nbatch);
    2696         218 :     instrument->nbatch_original = Max(instrument->nbatch_original,
    2697             :                                       hashtable->nbatch_original);
    2698         218 :     instrument->space_peak = Max(instrument->space_peak,
    2699             :                                  hashtable->spacePeak);
    2700         218 : }
    2701             : 
    2702             : /*
    2703             :  * Allocate 'size' bytes from the currently active HashMemoryChunk
    2704             :  */
    2705             : static void *
    2706     7489628 : dense_alloc(HashJoinTable hashtable, Size size)
    2707             : {
    2708             :     HashMemoryChunk newChunk;
    2709             :     char       *ptr;
    2710             : 
    2711             :     /* just in case the size is not already aligned properly */
    2712     7489628 :     size = MAXALIGN(size);
    2713             : 
    2714             :     /*
    2715             :      * If tuple size is larger than threshold, allocate a separate chunk.
    2716             :      */
    2717     7489628 :     if (size > HASH_CHUNK_THRESHOLD)
    2718             :     {
    2719             :         /* allocate new chunk and put it at the beginning of the list */
    2720           0 :         newChunk = (HashMemoryChunk) MemoryContextAlloc(hashtable->batchCxt,
    2721             :                                                         HASH_CHUNK_HEADER_SIZE + size);
    2722           0 :         newChunk->maxlen = size;
    2723           0 :         newChunk->used = size;
    2724           0 :         newChunk->ntuples = 1;
    2725             : 
    2726             :         /*
    2727             :          * Add this chunk to the list after the first existing chunk, so that
    2728             :          * we don't lose the remaining space in the "current" chunk.
    2729             :          */
    2730           0 :         if (hashtable->chunks != NULL)
    2731             :         {
    2732           0 :             newChunk->next = hashtable->chunks->next;
    2733           0 :             hashtable->chunks->next.unshared = newChunk;
    2734             :         }
    2735             :         else
    2736             :         {
    2737           0 :             newChunk->next.unshared = hashtable->chunks;
    2738           0 :             hashtable->chunks = newChunk;
    2739             :         }
    2740             : 
    2741           0 :         return HASH_CHUNK_DATA(newChunk);
    2742             :     }
    2743             : 
    2744             :     /*
    2745             :      * See if we have enough space for it in the current chunk (if any). If
    2746             :      * not, allocate a fresh chunk.
    2747             :      */
    2748     7489628 :     if ((hashtable->chunks == NULL) ||
    2749     7227692 :         (hashtable->chunks->maxlen - hashtable->chunks->used) < size)
    2750             :     {
    2751             :         /* allocate new chunk and put it at the beginning of the list */
    2752      270482 :         newChunk = (HashMemoryChunk) MemoryContextAlloc(hashtable->batchCxt,
    2753             :                                                         HASH_CHUNK_HEADER_SIZE + HASH_CHUNK_SIZE);
    2754             : 
    2755      270482 :         newChunk->maxlen = HASH_CHUNK_SIZE;
    2756      270482 :         newChunk->used = size;
    2757      270482 :         newChunk->ntuples = 1;
    2758             : 
    2759      270482 :         newChunk->next.unshared = hashtable->chunks;
    2760      270482 :         hashtable->chunks = newChunk;
    2761             : 
    2762      270482 :         return HASH_CHUNK_DATA(newChunk);
    2763             :     }
    2764             : 
    2765             :     /* There is enough space in the current chunk, let's add the tuple */
    2766     7219146 :     ptr = HASH_CHUNK_DATA(hashtable->chunks) + hashtable->chunks->used;
    2767     7219146 :     hashtable->chunks->used += size;
    2768     7219146 :     hashtable->chunks->ntuples += 1;
    2769             : 
    2770             :     /* return pointer to the start of the tuple memory */
    2771     7219146 :     return ptr;
    2772             : }
    2773             : 
    2774             : /*
    2775             :  * Allocate space for a tuple in shared dense storage.  This is equivalent to
    2776             :  * dense_alloc but for Parallel Hash using shared memory.
    2777             :  *
    2778             :  * While loading a tuple into shared memory, we might run out of memory and
    2779             :  * decide to repartition, or determine that the load factor is too high and
    2780             :  * decide to expand the bucket array, or discover that another participant has
    2781             :  * commanded us to help do that.  Return NULL if number of buckets or batches
    2782             :  * has changed, indicating that the caller must retry (considering the
    2783             :  * possibility that the tuple no longer belongs in the same batch).
    2784             :  */
    2785             : static HashJoinTuple
    2786     1349676 : ExecParallelHashTupleAlloc(HashJoinTable hashtable, size_t size,
    2787             :                            dsa_pointer *shared)
    2788             : {
    2789     1349676 :     ParallelHashJoinState *pstate = hashtable->parallel_state;
    2790             :     dsa_pointer chunk_shared;
    2791             :     HashMemoryChunk chunk;
    2792             :     Size        chunk_size;
    2793             :     HashJoinTuple result;
    2794     1349676 :     int         curbatch = hashtable->curbatch;
    2795             : 
    2796     1349676 :     size = MAXALIGN(size);
    2797             : 
    2798             :     /*
    2799             :      * Fast path: if there is enough space in this backend's current chunk,
    2800             :      * then we can allocate without any locking.
    2801             :      */
    2802     1349676 :     chunk = hashtable->current_chunk;
    2803     1349676 :     if (chunk != NULL &&
    2804     1349052 :         size <= HASH_CHUNK_THRESHOLD &&
    2805     1349052 :         chunk->maxlen - chunk->used >= size)
    2806             :     {
    2807             : 
    2808     1347508 :         chunk_shared = hashtable->current_chunk_shared;
    2809             :         Assert(chunk == dsa_get_address(hashtable->area, chunk_shared));
    2810     1347508 :         *shared = chunk_shared + HASH_CHUNK_HEADER_SIZE + chunk->used;
    2811     1347508 :         result = (HashJoinTuple) (HASH_CHUNK_DATA(chunk) + chunk->used);
    2812     1347508 :         chunk->used += size;
    2813             : 
    2814             :         Assert(chunk->used <= chunk->maxlen);
    2815             :         Assert(result == dsa_get_address(hashtable->area, *shared));
    2816             : 
    2817     1347508 :         return result;
    2818             :     }
    2819             : 
    2820             :     /* Slow path: try to allocate a new chunk. */
    2821        2168 :     LWLockAcquire(&pstate->lock, LW_EXCLUSIVE);
    2822             : 
    2823             :     /*
    2824             :      * Check if we need to help increase the number of buckets or batches.
    2825             :      */
    2826        2168 :     if (pstate->growth == PHJ_GROWTH_NEED_MORE_BATCHES ||
    2827        2144 :         pstate->growth == PHJ_GROWTH_NEED_MORE_BUCKETS)
    2828             :     {
    2829         120 :         ParallelHashGrowth growth = pstate->growth;
    2830             : 
    2831         120 :         hashtable->current_chunk = NULL;
    2832         120 :         LWLockRelease(&pstate->lock);
    2833             : 
    2834             :         /* Another participant has commanded us to help grow. */
    2835         120 :         if (growth == PHJ_GROWTH_NEED_MORE_BATCHES)
    2836          24 :             ExecParallelHashIncreaseNumBatches(hashtable);
    2837          96 :         else if (growth == PHJ_GROWTH_NEED_MORE_BUCKETS)
    2838          96 :             ExecParallelHashIncreaseNumBuckets(hashtable);
    2839             : 
    2840             :         /* The caller must retry. */
    2841         120 :         return NULL;
    2842             :     }
    2843             : 
    2844             :     /* Oversized tuples get their own chunk. */
    2845        2048 :     if (size > HASH_CHUNK_THRESHOLD)
    2846          32 :         chunk_size = size + HASH_CHUNK_HEADER_SIZE;
    2847             :     else
    2848        2016 :         chunk_size = HASH_CHUNK_SIZE;
    2849             : 
    2850             :     /* Check if it's time to grow batches or buckets. */
    2851        2048 :     if (pstate->growth != PHJ_GROWTH_DISABLED)
    2852             :     {
    2853             :         Assert(curbatch == 0);
    2854             :         Assert(BarrierPhase(&pstate->build_barrier) == PHJ_BUILD_HASHING_INNER);
    2855             : 
    2856             :         /*
    2857             :          * Check if our space limit would be exceeded.  To avoid choking on
    2858             :          * very large tuples or very low work_mem setting, we'll always allow
    2859             :          * each backend to allocate at least one chunk.
    2860             :          */
    2861         994 :         if (hashtable->batches[0].at_least_one_chunk &&
    2862         754 :             hashtable->batches[0].shared->size +
    2863         754 :             chunk_size > pstate->space_allowed)
    2864             :         {
    2865          24 :             pstate->growth = PHJ_GROWTH_NEED_MORE_BATCHES;
    2866          24 :             hashtable->batches[0].shared->space_exhausted = true;
    2867          24 :             LWLockRelease(&pstate->lock);
    2868             : 
    2869          24 :             return NULL;
    2870             :         }
    2871             : 
    2872             :         /* Check if our load factor limit would be exceeded. */
    2873         970 :         if (hashtable->nbatch == 1)
    2874             :         {
    2875         834 :             hashtable->batches[0].shared->ntuples += hashtable->batches[0].ntuples;
    2876         834 :             hashtable->batches[0].ntuples = 0;
    2877             :             /* Guard against integer overflow and alloc size overflow */
    2878        1668 :             if (hashtable->batches[0].shared->ntuples + 1 >
    2879         834 :                 hashtable->nbuckets * NTUP_PER_BUCKET &&
    2880          72 :                 hashtable->nbuckets < (INT_MAX / 2) &&
    2881          72 :                 hashtable->nbuckets * 2 <=
    2882             :                 MaxAllocSize / sizeof(dsa_pointer_atomic))
    2883             :             {
    2884          72 :                 pstate->growth = PHJ_GROWTH_NEED_MORE_BUCKETS;
    2885          72 :                 LWLockRelease(&pstate->lock);
    2886             : 
    2887          72 :                 return NULL;
    2888             :             }
    2889             :         }
    2890             :     }
    2891             : 
    2892             :     /* We are cleared to allocate a new chunk. */
    2893        1952 :     chunk_shared = dsa_allocate(hashtable->area, chunk_size);
    2894        1952 :     hashtable->batches[curbatch].shared->size += chunk_size;
    2895        1952 :     hashtable->batches[curbatch].at_least_one_chunk = true;
    2896             : 
    2897             :     /* Set up the chunk. */
    2898        1952 :     chunk = (HashMemoryChunk) dsa_get_address(hashtable->area, chunk_shared);
    2899        1952 :     *shared = chunk_shared + HASH_CHUNK_HEADER_SIZE;
    2900        1952 :     chunk->maxlen = chunk_size - HASH_CHUNK_HEADER_SIZE;
    2901        1952 :     chunk->used = size;
    2902             : 
    2903             :     /*
    2904             :      * Push it onto the list of chunks, so that it can be found if we need to
    2905             :      * increase the number of buckets or batches (batch 0 only) and later for
    2906             :      * freeing the memory (all batches).
    2907             :      */
    2908        1952 :     chunk->next.shared = hashtable->batches[curbatch].shared->chunks;
    2909        1952 :     hashtable->batches[curbatch].shared->chunks = chunk_shared;
    2910             : 
    2911        1952 :     if (size <= HASH_CHUNK_THRESHOLD)
    2912             :     {
    2913             :         /*
    2914             :          * Make this the current chunk so that we can use the fast path to
    2915             :          * fill the rest of it up in future calls.
    2916             :          */
    2917        1928 :         hashtable->current_chunk = chunk;
    2918        1928 :         hashtable->current_chunk_shared = chunk_shared;
    2919             :     }
    2920        1952 :     LWLockRelease(&pstate->lock);
    2921             : 
    2922             :     Assert(HASH_CHUNK_DATA(chunk) == dsa_get_address(hashtable->area, *shared));
    2923        1952 :     result = (HashJoinTuple) HASH_CHUNK_DATA(chunk);
    2924             : 
    2925        1952 :     return result;
    2926             : }
    2927             : 
    2928             : /*
    2929             :  * One backend needs to set up the shared batch state including tuplestores.
    2930             :  * Other backends will ensure they have correctly configured accessors by
    2931             :  * called ExecParallelHashEnsureBatchAccessors().
    2932             :  */
    2933             : static void
    2934         130 : ExecParallelHashJoinSetUpBatches(HashJoinTable hashtable, int nbatch)
    2935             : {
    2936         130 :     ParallelHashJoinState *pstate = hashtable->parallel_state;
    2937             :     ParallelHashJoinBatch *batches;
    2938             :     MemoryContext oldcxt;
    2939             :     int         i;
    2940             : 
    2941             :     Assert(hashtable->batches == NULL);
    2942             : 
    2943             :     /* Allocate space. */
    2944         130 :     pstate->batches =
    2945         130 :         dsa_allocate0(hashtable->area,
    2946             :                       EstimateParallelHashJoinBatch(hashtable) * nbatch);
    2947         130 :     pstate->nbatch = nbatch;
    2948         130 :     batches = dsa_get_address(hashtable->area, pstate->batches);
    2949             : 
    2950             :     /* Use hash join memory context. */
    2951         130 :     oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
    2952             : 
    2953             :     /* Allocate this backend's accessor array. */
    2954         130 :     hashtable->nbatch = nbatch;
    2955         130 :     hashtable->batches = (ParallelHashJoinBatchAccessor *)
    2956         130 :         palloc0(sizeof(ParallelHashJoinBatchAccessor) * hashtable->nbatch);
    2957             : 
    2958             :     /* Set up the shared state, tuplestores and backend-local accessors. */
    2959         674 :     for (i = 0; i < hashtable->nbatch; ++i)
    2960             :     {
    2961         544 :         ParallelHashJoinBatchAccessor *accessor = &hashtable->batches[i];
    2962         544 :         ParallelHashJoinBatch *shared = NthParallelHashJoinBatch(batches, i);
    2963             :         char        name[MAXPGPATH];
    2964             : 
    2965             :         /*
    2966             :          * All members of shared were zero-initialized.  We just need to set
    2967             :          * up the Barrier.
    2968             :          */
    2969         544 :         BarrierInit(&shared->batch_barrier, 0);
    2970         544 :         if (i == 0)
    2971             :         {
    2972             :             /* Batch 0 doesn't need to be loaded. */
    2973         130 :             BarrierAttach(&shared->batch_barrier);
    2974         520 :             while (BarrierPhase(&shared->batch_barrier) < PHJ_BATCH_PROBING)
    2975         390 :                 BarrierArriveAndWait(&shared->batch_barrier, 0);
    2976         130 :             BarrierDetach(&shared->batch_barrier);
    2977             :         }
    2978             : 
    2979             :         /* Initialize accessor state.  All members were zero-initialized. */
    2980         544 :         accessor->shared = shared;
    2981             : 
    2982             :         /* Initialize the shared tuplestores. */
    2983         544 :         snprintf(name, sizeof(name), "i%dof%d", i, hashtable->nbatch);
    2984         544 :         accessor->inner_tuples =
    2985         544 :             sts_initialize(ParallelHashJoinBatchInner(shared),
    2986             :                            pstate->nparticipants,
    2987             :                            ParallelWorkerNumber + 1,
    2988             :                            sizeof(uint32),
    2989             :                            SHARED_TUPLESTORE_SINGLE_PASS,
    2990             :                            &pstate->fileset,
    2991             :                            name);
    2992         544 :         snprintf(name, sizeof(name), "o%dof%d", i, hashtable->nbatch);
    2993         544 :         accessor->outer_tuples =
    2994         544 :             sts_initialize(ParallelHashJoinBatchOuter(shared,
    2995             :                                                       pstate->nparticipants),
    2996             :                            pstate->nparticipants,
    2997             :                            ParallelWorkerNumber + 1,
    2998             :                            sizeof(uint32),
    2999             :                            SHARED_TUPLESTORE_SINGLE_PASS,
    3000             :                            &pstate->fileset,
    3001             :                            name);
    3002             :     }
    3003             : 
    3004         130 :     MemoryContextSwitchTo(oldcxt);
    3005         130 : }
    3006             : 
    3007             : /*
    3008             :  * Free the current set of ParallelHashJoinBatchAccessor objects.
    3009             :  */
    3010             : static void
    3011          36 : ExecParallelHashCloseBatchAccessors(HashJoinTable hashtable)
    3012             : {
    3013             :     int         i;
    3014             : 
    3015         124 :     for (i = 0; i < hashtable->nbatch; ++i)
    3016             :     {
    3017             :         /* Make sure no files are left open. */
    3018          88 :         sts_end_write(hashtable->batches[i].inner_tuples);
    3019          88 :         sts_end_write(hashtable->batches[i].outer_tuples);
    3020          88 :         sts_end_parallel_scan(hashtable->batches[i].inner_tuples);
    3021          88 :         sts_end_parallel_scan(hashtable->batches[i].outer_tuples);
    3022             :     }
    3023          36 :     pfree(hashtable->batches);
    3024          36 :     hashtable->batches = NULL;
    3025          36 : }
    3026             : 
    3027             : /*
    3028             :  * Make sure this backend has up-to-date accessors for the current set of
    3029             :  * batches.
    3030             :  */
    3031             : static void
    3032         538 : ExecParallelHashEnsureBatchAccessors(HashJoinTable hashtable)
    3033             : {
    3034         538 :     ParallelHashJoinState *pstate = hashtable->parallel_state;
    3035             :     ParallelHashJoinBatch *batches;
    3036             :     MemoryContext oldcxt;
    3037             :     int         i;
    3038             : 
    3039         538 :     if (hashtable->batches != NULL)
    3040             :     {
    3041         410 :         if (hashtable->nbatch == pstate->nbatch)
    3042         408 :             return;
    3043           2 :         ExecParallelHashCloseBatchAccessors(hashtable);
    3044             :     }
    3045             : 
    3046             :     /*
    3047             :      * It's possible for a backend to start up very late so that the whole
    3048             :      * join is finished and the shm state for tracking batches has already
    3049             :      * been freed by ExecHashTableDetach().  In that case we'll just leave
    3050             :      * hashtable->batches as NULL so that ExecParallelHashJoinNewBatch() gives
    3051             :      * up early.
    3052             :      */
    3053         130 :     if (!DsaPointerIsValid(pstate->batches))
    3054           0 :         return;
    3055             : 
    3056             :     /* Use hash join memory context. */
    3057         130 :     oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
    3058             : 
    3059             :     /* Allocate this backend's accessor array. */
    3060         130 :     hashtable->nbatch = pstate->nbatch;
    3061         130 :     hashtable->batches = (ParallelHashJoinBatchAccessor *)
    3062         130 :         palloc0(sizeof(ParallelHashJoinBatchAccessor) * hashtable->nbatch);
    3063             : 
    3064             :     /* Find the base of the pseudo-array of ParallelHashJoinBatch objects. */
    3065             :     batches = (ParallelHashJoinBatch *)
    3066         130 :         dsa_get_address(hashtable->area, pstate->batches);
    3067             : 
    3068             :     /* Set up the accessor array and attach to the tuplestores. */
    3069         762 :     for (i = 0; i < hashtable->nbatch; ++i)
    3070             :     {
    3071         632 :         ParallelHashJoinBatchAccessor *accessor = &hashtable->batches[i];
    3072         632 :         ParallelHashJoinBatch *shared = NthParallelHashJoinBatch(batches, i);
    3073             : 
    3074         632 :         accessor->shared = shared;
    3075         632 :         accessor->preallocated = 0;
    3076         632 :         accessor->done = false;
    3077         632 :         accessor->inner_tuples =
    3078         632 :             sts_attach(ParallelHashJoinBatchInner(shared),
    3079             :                        ParallelWorkerNumber + 1,
    3080             :                        &pstate->fileset);
    3081         632 :         accessor->outer_tuples =
    3082         632 :             sts_attach(ParallelHashJoinBatchOuter(shared,
    3083             :                                                   pstate->nparticipants),
    3084             :                        ParallelWorkerNumber + 1,
    3085             :                        &pstate->fileset);
    3086             :     }
    3087             : 
    3088         130 :     MemoryContextSwitchTo(oldcxt);
    3089             : }
    3090             : 
    3091             : /*
    3092             :  * Allocate an empty shared memory hash table for a given batch.
    3093             :  */
    3094             : void
    3095         472 : ExecParallelHashTableAlloc(HashJoinTable hashtable, int batchno)
    3096             : {
    3097         472 :     ParallelHashJoinBatch *batch = hashtable->batches[batchno].shared;
    3098             :     dsa_pointer_atomic *buckets;
    3099         472 :     int         nbuckets = hashtable->parallel_state->nbuckets;
    3100             :     int         i;
    3101             : 
    3102         472 :     batch->buckets =
    3103         472 :         dsa_allocate(hashtable->area, sizeof(dsa_pointer_atomic) * nbuckets);
    3104             :     buckets = (dsa_pointer_atomic *)
    3105         472 :         dsa_get_address(hashtable->area, batch->buckets);
    3106     1700312 :     for (i = 0; i < nbuckets; ++i)
    3107     1699840 :         dsa_pointer_atomic_init(&buckets[i], InvalidDsaPointer);
    3108         472 : }
    3109             : 
    3110             : /*
    3111             :  * If we are currently attached to a shared hash join batch, detach.  If we
    3112             :  * are last to detach, clean up.
    3113             :  */
    3114             : void
    3115       18028 : ExecHashTableDetachBatch(HashJoinTable hashtable)
    3116             : {
    3117       18028 :     if (hashtable->parallel_state != NULL &&
    3118         776 :         hashtable->curbatch >= 0)
    3119             :     {
    3120         552 :         int         curbatch = hashtable->curbatch;
    3121         552 :         ParallelHashJoinBatch *batch = hashtable->batches[curbatch].shared;
    3122             : 
    3123             :         /* Make sure any temporary files are closed. */
    3124         552 :         sts_end_parallel_scan(hashtable->batches[curbatch].inner_tuples);
    3125         552 :         sts_end_parallel_scan(hashtable->batches[curbatch].outer_tuples);
    3126             : 
    3127             :         /* Detach from the batch we were last working on. */
    3128         552 :         if (BarrierArriveAndDetach(&batch->batch_barrier))
    3129             :         {
    3130             :             /*
    3131             :              * Technically we shouldn't access the barrier because we're no
    3132             :              * longer attached, but since there is no way it's moving after
    3133             :              * this point it seems safe to make the following assertion.
    3134             :              */
    3135             :             Assert(BarrierPhase(&batch->batch_barrier) == PHJ_BATCH_DONE);
    3136             : 
    3137             :             /* Free shared chunks and buckets. */
    3138        2228 :             while (DsaPointerIsValid(batch->chunks))
    3139             :             {
    3140             :                 HashMemoryChunk chunk =
    3141        1756 :                 dsa_get_address(hashtable->area, batch->chunks);
    3142        1756 :                 dsa_pointer next = chunk->next.shared;
    3143             : 
    3144        1756 :                 dsa_free(hashtable->area, batch->chunks);
    3145        1756 :                 batch->chunks = next;
    3146             :             }
    3147         472 :             if (DsaPointerIsValid(batch->buckets))
    3148             :             {
    3149         472 :                 dsa_free(hashtable->area, batch->buckets);
    3150         472 :                 batch->buckets = InvalidDsaPointer;
    3151             :             }
    3152             :         }
    3153             : 
    3154             :         /*
    3155             :          * Track the largest batch we've been attached to.  Though each
    3156             :          * backend might see a different subset of batches, explain.c will
    3157             :          * scan the results from all backends to find the largest value.
    3158             :          */
    3159         552 :         hashtable->spacePeak =
    3160         552 :             Max(hashtable->spacePeak,
    3161             :                 batch->size + sizeof(dsa_pointer_atomic) * hashtable->nbuckets);
    3162             : 
    3163             :         /* Remember that we are not attached to a batch. */
    3164         552 :         hashtable->curbatch = -1;
    3165             :     }
    3166       18028 : }
    3167             : 
    3168             : /*
    3169             :  * Detach from all shared resources.  If we are last to detach, clean up.
    3170             :  */
    3171             : void
    3172       17476 : ExecHashTableDetach(HashJoinTable hashtable)
    3173             : {
    3174       17476 :     if (hashtable->parallel_state)
    3175             :     {
    3176         224 :         ParallelHashJoinState *pstate = hashtable->parallel_state;
    3177             :         int         i;
    3178             : 
    3179             :         /* Make sure any temporary files are closed. */
    3180         224 :         if (hashtable->batches)
    3181             :         {
    3182        1312 :             for (i = 0; i < hashtable->nbatch; ++i)
    3183             :             {
    3184        1088 :                 sts_end_write(hashtable->batches[i].inner_tuples);
    3185        1088 :                 sts_end_write(hashtable->batches[i].outer_tuples);
    3186        1088 :                 sts_end_parallel_scan(hashtable->batches[i].inner_tuples);
    3187        1088 :                 sts_end_parallel_scan(hashtable->batches[i].outer_tuples);
    3188             :             }
    3189             :         }
    3190             : 
    3191             :         /* If we're last to detach, clean up shared memory. */
    3192         224 :         if (BarrierDetach(&pstate->build_barrier))
    3193             :         {
    3194          96 :             if (DsaPointerIsValid(pstate->batches))
    3195             :             {
    3196          96 :                 dsa_free(hashtable->area, pstate->batches);
    3197          96 :                 pstate->batches = InvalidDsaPointer;
    3198             :             }
    3199             :         }
    3200             : 
    3201         224 :         hashtable->parallel_state = NULL;
    3202             :     }
    3203       17476 : }
    3204             : 
    3205             : /*
    3206             :  * Get the first tuple in a given bucket identified by number.
    3207             :  */
    3208             : static inline HashJoinTuple
    3209     1200016 : ExecParallelHashFirstTuple(HashJoinTable hashtable, int bucketno)
    3210             : {
    3211             :     HashJoinTuple tuple;
    3212             :     dsa_pointer p;
    3213             : 
    3214             :     Assert(hashtable->parallel_state);
    3215     1200016 :     p = dsa_pointer_atomic_read(&hashtable->buckets.shared[bucketno]);
    3216     1200016 :     tuple = (HashJoinTuple) dsa_get_address(hashtable->area, p);
    3217             : 
    3218     1200016 :     return tuple;
    3219             : }
    3220             : 
    3221             : /*
    3222             :  * Get the next tuple in the same bucket as 'tuple'.
    3223             :  */
    3224             : static inline HashJoinTuple
    3225     1986792 : ExecParallelHashNextTuple(HashJoinTable hashtable, HashJoinTuple tuple)
    3226             : {
    3227             :     HashJoinTuple next;
    3228             : 
    3229             :     Assert(hashtable->parallel_state);
    3230     1986792 :     next = (HashJoinTuple) dsa_get_address(hashtable->area, tuple->next.shared);
    3231             : 
    3232     1986792 :     return next;
    3233             : }
    3234             : 
    3235             : /*
    3236             :  * Insert a tuple at the front of a chain of tuples in DSA memory atomically.
    3237             :  */
    3238             : static inline void
    3239     1715670 : ExecParallelHashPushTuple(dsa_pointer_atomic *head,
    3240             :                           HashJoinTuple tuple,
    3241             :                           dsa_pointer tuple_shared)
    3242             : {
    3243             :     for (;;)
    3244             :     {
    3245     1715670 :         tuple->next.shared = dsa_pointer_atomic_read(head);
    3246     1715670 :         if (dsa_pointer_atomic_compare_exchange(head,
    3247     1715670 :                                                 &tuple->next.shared,
    3248             :                                                 tuple_shared))
    3249     1711016 :             break;
    3250             :     }
    3251     1711016 : }
    3252             : 
    3253             : /*
    3254             :  * Prepare to work on a given batch.
    3255             :  */
    3256             : void
    3257        1252 : ExecParallelHashTableSetCurrentBatch(HashJoinTable hashtable, int batchno)
    3258             : {
    3259             :     Assert(hashtable->batches[batchno].shared->buckets != InvalidDsaPointer);
    3260             : 
    3261        1252 :     hashtable->curbatch = batchno;
    3262        1252 :     hashtable->buckets.shared = (dsa_pointer_atomic *)
    3263        1252 :         dsa_get_address(hashtable->area,
    3264        1252 :                         hashtable->batches[batchno].shared->buckets);
    3265        1252 :     hashtable->nbuckets = hashtable->parallel_state->nbuckets;
    3266        1252 :     hashtable->log2_nbuckets = my_log2(hashtable->nbuckets);
    3267        1252 :     hashtable->current_chunk = NULL;
    3268        1252 :     hashtable->current_chunk_shared = InvalidDsaPointer;
    3269        1252 :     hashtable->batches[batchno].at_least_one_chunk = false;
    3270        1252 : }
    3271             : 
    3272             : /*
    3273             :  * Take the next available chunk from the queue of chunks being worked on in
    3274             :  * parallel.  Return NULL if there are none left.  Otherwise return a pointer
    3275             :  * to the chunk, and set *shared to the DSA pointer to the chunk.
    3276             :  */
    3277             : static HashMemoryChunk
    3278         768 : ExecParallelHashPopChunkQueue(HashJoinTable hashtable, dsa_pointer *shared)
    3279             : {
    3280         768 :     ParallelHashJoinState *pstate = hashtable->parallel_state;
    3281             :     HashMemoryChunk chunk;
    3282             : 
    3283         768 :     LWLockAcquire(&pstate->lock, LW_EXCLUSIVE);
    3284         768 :     if (DsaPointerIsValid(pstate->chunk_work_queue))
    3285             :     {
    3286         638 :         *shared = pstate->chunk_work_queue;
    3287             :         chunk = (HashMemoryChunk)
    3288         638 :             dsa_get_address(hashtable->area, *shared);
    3289         638 :         pstate->chunk_work_queue = chunk->next.shared;
    3290             :     }
    3291             :     else
    3292         130 :         chunk = NULL;
    3293         768 :     LWLockRelease(&pstate->lock);
    3294             : 
    3295         768 :     return chunk;
    3296             : }
    3297             : 
    3298             : /*
    3299             :  * Increase the space preallocated in this backend for a given inner batch by
    3300             :  * at least a given amount.  This allows us to track whether a given batch
    3301             :  * would fit in memory when loaded back in.  Also increase the number of
    3302             :  * batches or buckets if required.
    3303             :  *
    3304             :  * This maintains a running estimation of how much space will be taken when we
    3305             :  * load the batch back into memory by simulating the way chunks will be handed
    3306             :  * out to workers.  It's not perfectly accurate because the tuples will be
    3307             :  * packed into memory chunks differently by ExecParallelHashTupleAlloc(), but
    3308             :  * it should be pretty close.  It tends to overestimate by a fraction of a
    3309             :  * chunk per worker since all workers gang up to preallocate during hashing,
    3310             :  * but workers tend to reload batches alone if there are enough to go around,
    3311             :  * leaving fewer partially filled chunks.  This effect is bounded by
    3312             :  * nparticipants.
    3313             :  *
    3314             :  * Return false if the number of batches or buckets has changed, and the
    3315             :  * caller should reconsider which batch a given tuple now belongs in and call
    3316             :  * again.
    3317             :  */
    3318             : static bool
    3319        1046 : ExecParallelHashTuplePrealloc(HashJoinTable hashtable, int batchno, size_t size)
    3320             : {
    3321        1046 :     ParallelHashJoinState *pstate = hashtable->parallel_state;
    3322        1046 :     ParallelHashJoinBatchAccessor *batch = &hashtable->batches[batchno];
    3323        1046 :     size_t      want = Max(size, HASH_CHUNK_SIZE - HASH_CHUNK_HEADER_SIZE);
    3324             : 
    3325             :     Assert(batchno > 0);
    3326             :     Assert(batchno < hashtable->nbatch);
    3327             :     Assert(size == MAXALIGN(size));
    3328             : 
    3329        1046 :     LWLockAcquire(&pstate->lock, LW_EXCLUSIVE);
    3330             : 
    3331             :     /* Has another participant commanded us to help grow? */
    3332        1046 :     if (pstate->growth == PHJ_GROWTH_NEED_MORE_BATCHES ||
    3333        1036 :         pstate->growth == PHJ_GROWTH_NEED_MORE_BUCKETS)
    3334             :     {
    3335          10 :         ParallelHashGrowth growth = pstate->growth;
    3336             : 
    3337          10 :         LWLockRelease(&pstate->lock);
    3338          10 :         if (growth == PHJ_GROWTH_NEED_MORE_BATCHES)
    3339          10 :             ExecParallelHashIncreaseNumBatches(hashtable);
    3340           0 :         else if (growth == PHJ_GROWTH_NEED_MORE_BUCKETS)
    3341           0 :             ExecParallelHashIncreaseNumBuckets(hashtable);
    3342             : 
    3343          10 :         return false;
    3344             :     }
    3345             : 
    3346        1036 :     if (pstate->growth != PHJ_GROWTH_DISABLED &&
    3347         882 :         batch->at_least_one_chunk &&
    3348         380 :         (batch->shared->estimated_size + want + HASH_CHUNK_HEADER_SIZE
    3349         380 :          > pstate->space_allowed))
    3350             :     {
    3351             :         /*
    3352             :          * We have determined that this batch would exceed the space budget if
    3353             :          * loaded into memory.  Command all participants to help repartition.
    3354             :          */
    3355          10 :         batch->shared->space_exhausted = true;
    3356          10 :         pstate->growth = PHJ_GROWTH_NEED_MORE_BATCHES;
    3357          10 :         LWLockRelease(&pstate->lock);
    3358             : 
    3359          10 :         return false;
    3360             :     }
    3361             : 
    3362        1026 :     batch->at_least_one_chunk = true;
    3363        1026 :     batch->shared->estimated_size += want + HASH_CHUNK_HEADER_SIZE;
    3364        1026 :     batch->preallocated = want;
    3365        1026 :     LWLockRelease(&pstate->lock);
    3366             : 
    3367        1026 :     return true;
    3368             : }

Generated by: LCOV version 1.13