/* *Pending-tuplearrayforeachworker.Thisholdsadditionaltuplesthat *wewereabletofetchfromtheworker,butcan'tprocessyet.Inaddition, *thisstructholdsthe"done"flagindicatingtheworkerisknowntohave *nomoretuples.(Wedonotusethisstructfortheleader;wedon'tkeep *anypendingtuplesfortheleader,andtheneed_to_scan_locallyflagserves *asits"done"indicator.)
*/ typedefstruct GMReaderTupleBuffer
{
MinimalTuple *tuple; /* array of length MAX_TUPLE_STORE */ int nTuples; /* number of tuples currently stored */ int readCounter; /* index of next tuple to extract */ bool done; /* true if reader is known exhausted */
} GMReaderTupleBuffer;
static TupleTableSlot *ExecGatherMerge(PlanState *pstate); static int32 heap_compare_slots(Datum a, Datum b, void *arg); static TupleTableSlot *gather_merge_getnext(GatherMergeState *gm_state); static MinimalTuple gm_readnext_tuple(GatherMergeState *gm_state, int nreader, bool nowait, bool *done); staticvoid ExecShutdownGatherMergeWorkers(GatherMergeState *node); staticvoid gather_merge_setup(GatherMergeState *gm_state); staticvoid gather_merge_init(GatherMergeState *gm_state); staticvoid gather_merge_clear_tuples(GatherMergeState *gm_state); staticbool gather_merge_readnext(GatherMergeState *gm_state, int reader, bool nowait); staticvoid load_tuple_array(GatherMergeState *gm_state, int reader);
/* ---------------------------------------------------------------- *ExecInitGather *----------------------------------------------------------------
*/
GatherMergeState *
ExecInitGatherMerge(GatherMerge *node, EState *estate, int eflags)
{
GatherMergeState *gm_state;
Plan *outerNode;
TupleDesc tupDesc;
/* Initialize, or re-initialize, shared state needed by workers. */ if (!node->pei)
node->pei = ExecInitParallelPlan(outerPlanState(node),
estate,
gm->initParam,
gm->num_workers,
node->tuples_needed); else
ExecParallelReinitialize(outerPlanState(node),
node->pei,
gm->initParam);
/* Try to launch workers. */
pcxt = node->pei->pcxt;
LaunchParallelWorkers(pcxt); /* We save # workers launched for the benefit of EXPLAIN */
node->nworkers_launched = pcxt->nworkers_launched;
/* Set up tuple queue readers to read the results. */ if (pcxt->nworkers_launched > 0)
{
ExecParallelCreateReaders(node->pei); /* Make a working array showing the active readers */
node->nreaders = pcxt->nworkers_launched;
node->reader = (TupleQueueReader **)
palloc(node->nreaders * sizeof(TupleQueueReader *));
memcpy(node->reader, node->pei->reader,
node->nreaders * sizeof(TupleQueueReader *));
} else
{ /* No workers? Then never mind. */
node->nreaders = 0;
node->reader = NULL;
}
}
/* allow leader to participate if enabled or no choice */ if (parallel_leader_participation || node->nreaders == 0)
node->need_to_scan_locally = true;
node->initialized = true;
}
/* Allocate the tuple slot and tuple array for each worker */
gm_state->gm_tuple_buffers = (GMReaderTupleBuffer *)
palloc0(nreaders * sizeof(GMReaderTupleBuffer));
for (i = 0; i < nreaders; i++)
{ /* Allocate the tuple array with length MAX_TUPLE_STORE */
gm_state->gm_tuple_buffers[i].tuple =
(MinimalTuple *) palloc0(sizeof(MinimalTuple) * MAX_TUPLE_STORE);
/* Reset the tuple slot and tuple array for each worker */ for (i = 0; i < nreaders; i++)
{ /* Reset tuple array to empty */
gm_state->gm_tuple_buffers[i].nTuples = 0;
gm_state->gm_tuple_buffers[i].readCounter = 0; /* Reset done flag to not-done */
gm_state->gm_tuple_buffers[i].done = false; /* Ensure output slot is empty */
ExecClearTuple(gm_state->gm_slots[i + 1]);
}
/* Reset binary heap to empty */
binaryheap_reset(gm_state->gm_heap);
/* *First,trytoreadatuplefromeachworker(includingleader)in *nowaitmode.Afterthis,ifnotallworkerswereabletoproducea *tuple(ora"done"indication),thenre-readfromremainingworkers, *thistimeusingwaitmode.Addalllivereaders(thoseproducingat *leastonetuple)totheheap.
*/
reread: for (i = 0; i <= nreaders; i++)
{
CHECK_FOR_INTERRUPTS();
/* skip this source if already known done */ if ((i == 0) ? gm_state->need_to_scan_locally :
!gm_state->gm_tuple_buffers[i - 1].done)
{ if (TupIsNull(gm_state->gm_slots[i]))
{ /* Don't have a tuple yet, try to get one */ if (gather_merge_readnext(gm_state, i, nowait))
binaryheap_add_unordered(gm_state->gm_heap,
Int32GetDatum(i));
} else
{ /* *Wealreadygotatleastonetuplefromthisworker,but *mightaswellseeifithasanymorereadybynow.
*/
load_tuple_array(gm_state, i);
}
}
}
/* need not recheck leader, since nowait doesn't matter for it */ for (i = 1; i <= nreaders; i++)
{ if (!gm_state->gm_tuple_buffers[i - 1].done &&
TupIsNull(gm_state->gm_slots[i]))
{
nowait = false; goto reread;
}
}
/* Now heapify the heap. */
binaryheap_build(gm_state->gm_heap);
gm_state->gm_initialized = true;
}
/* *Clearoutthetupletableslot,andanyunusedpendingtuples, *foreachgathermergeinput.
*/ staticvoid
gather_merge_clear_tuples(GatherMergeState *gm_state)
{ int i;
for (i = 0; i < gm_state->nreaders; i++)
{
GMReaderTupleBuffer *tuple_buffer = &gm_state->gm_tuple_buffers[i];
while (tuple_buffer->readCounter < tuple_buffer->nTuples)
pfree(tuple_buffer->tuple[tuple_buffer->readCounter++]);
if (!gm_state->gm_initialized)
{ /* *Firsttimethrough:pullthefirsttuplefromeachparticipant,and *setuptheheap.
*/
gather_merge_init(gm_state);
} else
{ /* *Otherwise,pullthenexttuplefromwhicheverparticipantwe *returnedfromlasttime,andreinsertthatparticipant'sindexinto *theheap,becauseitmightnowcomparedifferentlyagainstthe *otherelementsoftheheap.
*/
i = DatumGetInt32(binaryheap_first(gm_state->gm_heap));
if (gather_merge_readnext(gm_state, i, false))
binaryheap_replace_first(gm_state->gm_heap, Int32GetDatum(i)); else
{ /* reader exhausted, remove it from heap */
(void) binaryheap_remove_first(gm_state->gm_heap);
}
}
if (binaryheap_empty(gm_state->gm_heap))
{ /* All the queues are exhausted, and so is the heap */
gather_merge_clear_tuples(gm_state); return NULL;
} else
{ /* Return next tuple from whichever participant has the leading one */
i = DatumGetInt32(binaryheap_first(gm_state->gm_heap)); return gm_state->gm_slots[i];
}
}
/* *Readtuple(s)forgivenreaderinnowaitmode,andloadintoitstuple *array,untilwehaveMAX_TUPLE_STOREofthemorwouldhavetoblock.
*/ staticvoid
load_tuple_array(GatherMergeState *gm_state, int reader)
{
GMReaderTupleBuffer *tuple_buffer; int i;
/* Don't do anything if this is the leader. */ if (reader == 0) return;
/* If there's nothing in the array, reset the counters to zero. */ if (tuple_buffer->nTuples == tuple_buffer->readCounter)
tuple_buffer->nTuples = tuple_buffer->readCounter = 0;
/* Try to fill additional slots in the array. */ for (i = tuple_buffer->nTuples; i < MAX_TUPLE_STORE; i++)
{
MinimalTuple tuple;
/* Install our DSA area while executing the plan. */
estate->es_query_dsa = gm_state->pei ? gm_state->pei->area : NULL;
outerTupleSlot = ExecProcNode(outerPlan);
estate->es_query_dsa = NULL;
if (!TupIsNull(outerTupleSlot))
{
gm_state->gm_slots[0] = outerTupleSlot; returntrue;
} /* need_to_scan_locally serves as "done" flag for leader */
gm_state->need_to_scan_locally = false;
} returnfalse;
}
/* Otherwise, check the state of the relevant tuple buffer. */
tuple_buffer = &gm_state->gm_tuple_buffers[reader - 1];
if (tuple_buffer->nTuples > tuple_buffer->readCounter)
{ /* Return any tuple previously read that is still buffered. */
tup = tuple_buffer->tuple[tuple_buffer->readCounter++];
} elseif (tuple_buffer->done)
{ /* Reader is known to be exhausted. */ returnfalse;
} else
{ /* Read and buffer next tuple. */
tup = gm_readnext_tuple(gm_state,
reader,
nowait,
&tuple_buffer->done); if (!tup) returnfalse;
/* Build the TupleTableSlot for the given tuple */
ExecStoreMinimalTuple(tup, /* tuple to store */
gm_state->gm_slots[reader], /* slot in which to
* store the tuple */ true); /* pfree tuple when done with it */
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.27Angebot
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.