* Choose a batch to work on, and attach to it. Returns true if successful, * false if there are no more batches. */
| 1420 | * false if there are no more batches. |
| 1421 | */ |
| 1422 | static bool |
| 1423 | ExecParallelHashJoinNewBatch(HashJoinState *hjstate) |
| 1424 | { |
| 1425 | HashJoinTable hashtable = hjstate->hj_HashTable; |
| 1426 | int start_batchno; |
| 1427 | int batchno; |
| 1428 | Barrier *batch0_barrier = NULL; |
| 1429 | ParallelHashJoinState *pstate = hashtable->parallel_state; |
| 1430 | |
| 1431 | /* |
| 1432 | * If we started up so late that the batch tracking array has been freed |
| 1433 | * already by ExecHashTableDetach(), then we are finished. See also |
| 1434 | * ExecParallelHashEnsureBatchAccessors(). |
| 1435 | */ |
| 1436 | if (hashtable->batches == NULL) |
| 1437 | return false; |
| 1438 | |
| 1439 | /* |
| 1440 | * If we were already attached to a batch, remember not to bother checking |
| 1441 | * it again, and detach from it (possibly freeing the hash table if we are |
| 1442 | * last to detach). |
| 1443 | */ |
| 1444 | if (hashtable->curbatch >= 0) |
| 1445 | { |
| 1446 | hashtable->batches[hashtable->curbatch].done = true; |
| 1447 | ExecHashTableDetachBatch(hashtable); |
| 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * Search for a batch that isn't done. We use an atomic counter to start |
| 1452 | * our search at a different batch in every participant when there are |
| 1453 | * more batches than participants. |
| 1454 | */ |
| 1455 | batchno = start_batchno = |
| 1456 | pg_atomic_fetch_add_u32(&hashtable->parallel_state->distributor, 1) % |
| 1457 | hashtable->nbatch; |
| 1458 | do |
| 1459 | { |
| 1460 | uint32 hashvalue; |
| 1461 | MinimalTuple tuple; |
| 1462 | TupleTableSlot *slot; |
| 1463 | |
| 1464 | if (!hashtable->batches[batchno].done) |
| 1465 | { |
| 1466 | SharedTuplestoreAccessor *inner_tuples; |
| 1467 | Barrier *batch_barrier = |
| 1468 | &hashtable->batches[batchno].shared->batch_barrier; |
| 1469 | int phase = BarrierAttach(batch_barrier); |
| 1470 | |
| 1471 | if (hashtable->nbatch == 1 && batchno == 0 && ((HashJoin *)hjstate->js.ps.plan)->batch0_barrier) |
| 1472 | { |
| 1473 | Assert(phase == PHJ_BATCH_PROBING); |
| 1474 | |
| 1475 | batch0_barrier = &pstate->batch0_barrier; |
| 1476 | BarrierArriveAndWait(batch0_barrier, WAIT_EVENT_PARALLEL_FINISH); |
| 1477 | } |
| 1478 | |
| 1479 | switch (phase) |
no test coverage detected