1 /*-------------------------------------------------------------------------
4 * Scan a plan in multiple workers, and do order-preserving merge.
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/backend/executor/nodeGatherMerge.c
12 *-------------------------------------------------------------------------
17 #include "access/relscan.h"
18 #include "access/xact.h"
19 #include "executor/execdebug.h"
20 #include "executor/execParallel.h"
21 #include "executor/nodeGatherMerge.h"
22 #include "executor/nodeSubplan.h"
23 #include "executor/tqueue.h"
24 #include "lib/binaryheap.h"
25 #include "miscadmin.h"
26 #include "optimizer/planmain.h"
27 #include "utils/memutils.h"
28 #include "utils/rel.h"
31 * When we read tuples from workers, it's a good idea to read several at once
32 * for efficiency when possible: this minimizes context-switching overhead.
33 * But reading too many at a time wastes memory without improving performance.
34 * We'll read up to MAX_TUPLE_STORE tuples (in addition to the first one).
36 #define MAX_TUPLE_STORE 10
39 * Pending-tuple array for each worker. This holds additional tuples that
40 * we were able to fetch from the worker, but can't process yet. In addition,
41 * this struct holds the "done" flag indicating the worker is known to have
42 * no more tuples. (We do not use this struct for the leader; we don't keep
43 * any pending tuples for the leader, and the need_to_scan_locally flag serves
44 * as its "done" indicator.)
46 typedef struct GMReaderTupleBuffer
48 HeapTuple *tuple; /* array of length MAX_TUPLE_STORE */
49 int nTuples; /* number of tuples currently stored */
50 int readCounter; /* index of next tuple to extract */
51 bool done; /* true if reader is known exhausted */
52 } GMReaderTupleBuffer;
54 static TupleTableSlot *ExecGatherMerge(PlanState *pstate);
55 static int32 heap_compare_slots(Datum a, Datum b, void *arg);
56 static TupleTableSlot *gather_merge_getnext(GatherMergeState *gm_state);
57 static HeapTuple gm_readnext_tuple(GatherMergeState *gm_state, int nreader,
58 bool nowait, bool *done);
59 static void ExecShutdownGatherMergeWorkers(GatherMergeState *node);
60 static void gather_merge_setup(GatherMergeState *gm_state);
61 static void gather_merge_init(GatherMergeState *gm_state);
62 static void gather_merge_clear_tuples(GatherMergeState *gm_state);
63 static bool gather_merge_readnext(GatherMergeState *gm_state, int reader,
65 static void load_tuple_array(GatherMergeState *gm_state, int reader);
67 /* ----------------------------------------------------------------
69 * ----------------------------------------------------------------
72 ExecInitGatherMerge(GatherMerge *node, EState *estate, int eflags)
74 GatherMergeState *gm_state;
78 /* Gather merge node doesn't have innerPlan node. */
79 Assert(innerPlan(node) == NULL);
82 * create state structure
84 gm_state = makeNode(GatherMergeState);
85 gm_state->ps.plan = (Plan *) node;
86 gm_state->ps.state = estate;
87 gm_state->ps.ExecProcNode = ExecGatherMerge;
89 gm_state->initialized = false;
90 gm_state->gm_initialized = false;
91 gm_state->tuples_needed = -1;
94 * Miscellaneous initialization
96 * create expression context for node
98 ExecAssignExprContext(estate, &gm_state->ps);
101 * GatherMerge doesn't support checking a qual (it's always more efficient
102 * to do it in the child node).
104 Assert(!node->plan.qual);
107 * now initialize outer plan
109 outerNode = outerPlan(node);
110 outerPlanState(gm_state) = ExecInitNode(outerNode, estate, eflags);
113 * Leader may access ExecProcNode result directly (if
114 * need_to_scan_locally), or from workers via tuple queue. So we can't
115 * trivially rely on the slot type being fixed for expressions evaluated
118 gm_state->ps.outeropsset = true;
119 gm_state->ps.outeropsfixed = false;
122 * Store the tuple descriptor into gather merge state, so we can use it
123 * while initializing the gather merge slots.
125 tupDesc = ExecGetResultType(outerPlanState(gm_state));
126 gm_state->tupDesc = tupDesc;
129 * Initialize result type and projection.
131 ExecInitResultTypeTL(&gm_state->ps);
132 ExecConditionalAssignProjectionInfo(&gm_state->ps, tupDesc, OUTER_VAR);
135 * Without projections result slot type is not trivially known, see
138 if (gm_state->ps.ps_ProjInfo == NULL)
140 gm_state->ps.resultopsset = true;
141 gm_state->ps.resultopsfixed = false;
145 * initialize sort-key information
151 gm_state->gm_nkeys = node->numCols;
152 gm_state->gm_sortkeys =
153 palloc0(sizeof(SortSupportData) * node->numCols);
155 for (i = 0; i < node->numCols; i++)
157 SortSupport sortKey = gm_state->gm_sortkeys + i;
159 sortKey->ssup_cxt = CurrentMemoryContext;
160 sortKey->ssup_collation = node->collations[i];
161 sortKey->ssup_nulls_first = node->nullsFirst[i];
162 sortKey->ssup_attno = node->sortColIdx[i];
165 * We don't perform abbreviated key conversion here, for the same
166 * reasons that it isn't used in MergeAppend
168 sortKey->abbreviate = false;
170 PrepareSortSupportFromOrderingOp(node->sortOperators[i], sortKey);
174 /* Now allocate the workspace for gather merge */
175 gather_merge_setup(gm_state);
180 /* ----------------------------------------------------------------
181 * ExecGatherMerge(node)
183 * Scans the relation via multiple workers and returns
184 * the next qualifying tuple.
185 * ----------------------------------------------------------------
187 static TupleTableSlot *
188 ExecGatherMerge(PlanState *pstate)
190 GatherMergeState *node = castNode(GatherMergeState, pstate);
191 TupleTableSlot *slot;
192 ExprContext *econtext;
194 CHECK_FOR_INTERRUPTS();
197 * As with Gather, we don't launch workers until this node is actually
200 if (!node->initialized)
202 EState *estate = node->ps.state;
203 GatherMerge *gm = castNode(GatherMerge, node->ps.plan);
206 * Sometimes we might have to run without parallelism; but if parallel
207 * mode is active then we can try to fire up some workers.
209 if (gm->num_workers > 0 && estate->es_use_parallel_mode)
211 ParallelContext *pcxt;
213 /* Initialize, or re-initialize, shared state needed by workers. */
215 node->pei = ExecInitParallelPlan(node->ps.lefttree,
219 node->tuples_needed);
221 ExecParallelReinitialize(node->ps.lefttree,
225 /* Try to launch workers. */
226 pcxt = node->pei->pcxt;
227 LaunchParallelWorkers(pcxt);
228 /* We save # workers launched for the benefit of EXPLAIN */
229 node->nworkers_launched = pcxt->nworkers_launched;
231 /* Set up tuple queue readers to read the results. */
232 if (pcxt->nworkers_launched > 0)
234 ExecParallelCreateReaders(node->pei);
235 /* Make a working array showing the active readers */
236 node->nreaders = pcxt->nworkers_launched;
237 node->reader = (TupleQueueReader **)
238 palloc(node->nreaders * sizeof(TupleQueueReader *));
239 memcpy(node->reader, node->pei->reader,
240 node->nreaders * sizeof(TupleQueueReader *));
244 /* No workers? Then never mind. */
250 /* allow leader to participate if enabled or no choice */
251 if (parallel_leader_participation || node->nreaders == 0)
252 node->need_to_scan_locally = true;
253 node->initialized = true;
257 * Reset per-tuple memory context to free any expression evaluation
258 * storage allocated in the previous tuple cycle.
260 econtext = node->ps.ps_ExprContext;
261 ResetExprContext(econtext);
264 * Get next tuple, either from one of our workers, or by running the plan
267 slot = gather_merge_getnext(node);
271 /* If no projection is required, we're done. */
272 if (node->ps.ps_ProjInfo == NULL)
276 * Form the result tuple using ExecProject(), and return it.
278 econtext->ecxt_outertuple = slot;
279 return ExecProject(node->ps.ps_ProjInfo);
282 /* ----------------------------------------------------------------
285 * frees any storage allocated through C routines.
286 * ----------------------------------------------------------------
289 ExecEndGatherMerge(GatherMergeState *node)
291 ExecEndNode(outerPlanState(node)); /* let children clean up first */
292 ExecShutdownGatherMerge(node);
293 ExecFreeExprContext(&node->ps);
294 if (node->ps.ps_ResultTupleSlot)
295 ExecClearTuple(node->ps.ps_ResultTupleSlot);
298 /* ----------------------------------------------------------------
299 * ExecShutdownGatherMerge
301 * Destroy the setup for parallel workers including parallel context.
302 * ----------------------------------------------------------------
305 ExecShutdownGatherMerge(GatherMergeState *node)
307 ExecShutdownGatherMergeWorkers(node);
309 /* Now destroy the parallel context. */
310 if (node->pei != NULL)
312 ExecParallelCleanup(node->pei);
317 /* ----------------------------------------------------------------
318 * ExecShutdownGatherMergeWorkers
320 * Stop all the parallel workers.
321 * ----------------------------------------------------------------
324 ExecShutdownGatherMergeWorkers(GatherMergeState *node)
326 if (node->pei != NULL)
327 ExecParallelFinish(node->pei);
329 /* Flush local copy of reader array */
335 /* ----------------------------------------------------------------
336 * ExecReScanGatherMerge
338 * Prepare to re-scan the result of a GatherMerge.
339 * ----------------------------------------------------------------
342 ExecReScanGatherMerge(GatherMergeState *node)
344 GatherMerge *gm = (GatherMerge *) node->ps.plan;
345 PlanState *outerPlan = outerPlanState(node);
347 /* Make sure any existing workers are gracefully shut down */
348 ExecShutdownGatherMergeWorkers(node);
350 /* Free any unused tuples, so we don't leak memory across rescans */
351 gather_merge_clear_tuples(node);
353 /* Mark node so that shared state will be rebuilt at next call */
354 node->initialized = false;
355 node->gm_initialized = false;
358 * Set child node's chgParam to tell it that the next scan might deliver a
359 * different set of rows within the leader process. (The overall rowset
360 * shouldn't change, but the leader process's subset might; hence nodes
361 * between here and the parallel table scan node mustn't optimize on the
362 * assumption of an unchanging rowset.)
364 if (gm->rescan_param >= 0)
365 outerPlan->chgParam = bms_add_member(outerPlan->chgParam,
369 * If chgParam of subnode is not null then plan will be re-scanned by
370 * first ExecProcNode. Note: because this does nothing if we have a
371 * rescan_param, it's currently guaranteed that parallel-aware child nodes
372 * will not see a ReScan call until after they get a ReInitializeDSM call.
373 * That ordering might not be something to rely on, though. A good rule
374 * of thumb is that ReInitializeDSM should reset only shared state, ReScan
375 * should reset only local state, and anything that depends on both of
376 * those steps being finished must wait until the first ExecProcNode call.
378 if (outerPlan->chgParam == NULL)
379 ExecReScan(outerPlan);
383 * Set up the data structures that we'll need for Gather Merge.
385 * We allocate these once on the basis of gm->num_workers, which is an
386 * upper bound for the number of workers we'll actually have. During
387 * a rescan, we reset the structures to empty. This approach simplifies
388 * not leaking memory across rescans.
390 * In the gm_slots[] array, index 0 is for the leader, and indexes 1 to n
391 * are for workers. The values placed into gm_heap correspond to indexes
392 * in gm_slots[]. The gm_tuple_buffers[] array, however, is indexed from
393 * 0 to n-1; it has no entry for the leader.
396 gather_merge_setup(GatherMergeState *gm_state)
398 GatherMerge *gm = castNode(GatherMerge, gm_state->ps.plan);
399 int nreaders = gm->num_workers;
403 * Allocate gm_slots for the number of workers + one more slot for leader.
404 * Slot 0 is always for the leader. Leader always calls ExecProcNode() to
405 * read the tuple, and then stores it directly into its gm_slots entry.
406 * For other slots, code below will call ExecInitExtraTupleSlot() to
407 * create a slot for the worker's results. Note that during any single
408 * scan, we might have fewer than num_workers available workers, in which
409 * case the extra array entries go unused.
411 gm_state->gm_slots = (TupleTableSlot **)
412 palloc0((nreaders + 1) * sizeof(TupleTableSlot *));
414 /* Allocate the tuple slot and tuple array for each worker */
415 gm_state->gm_tuple_buffers = (GMReaderTupleBuffer *)
416 palloc0(nreaders * sizeof(GMReaderTupleBuffer));
418 for (i = 0; i < nreaders; i++)
420 /* Allocate the tuple array with length MAX_TUPLE_STORE */
421 gm_state->gm_tuple_buffers[i].tuple =
422 (HeapTuple *) palloc0(sizeof(HeapTuple) * MAX_TUPLE_STORE);
424 /* Initialize tuple slot for worker */
425 gm_state->gm_slots[i + 1] =
426 ExecInitExtraTupleSlot(gm_state->ps.state, gm_state->tupDesc,
430 /* Allocate the resources for the merge */
431 gm_state->gm_heap = binaryheap_allocate(nreaders + 1,
437 * Initialize the Gather Merge.
439 * Reset data structures to ensure they're empty. Then pull at least one
440 * tuple from leader + each worker (or set its "done" indicator), and set up
444 gather_merge_init(GatherMergeState *gm_state)
446 int nreaders = gm_state->nreaders;
450 /* Assert that gather_merge_setup made enough space */
451 Assert(nreaders <= castNode(GatherMerge, gm_state->ps.plan)->num_workers);
453 /* Reset leader's tuple slot to empty */
454 gm_state->gm_slots[0] = NULL;
456 /* Reset the tuple slot and tuple array for each worker */
457 for (i = 0; i < nreaders; i++)
459 /* Reset tuple array to empty */
460 gm_state->gm_tuple_buffers[i].nTuples = 0;
461 gm_state->gm_tuple_buffers[i].readCounter = 0;
462 /* Reset done flag to not-done */
463 gm_state->gm_tuple_buffers[i].done = false;
464 /* Ensure output slot is empty */
465 ExecClearTuple(gm_state->gm_slots[i + 1]);
468 /* Reset binary heap to empty */
469 binaryheap_reset(gm_state->gm_heap);
472 * First, try to read a tuple from each worker (including leader) in
473 * nowait mode. After this, if not all workers were able to produce a
474 * tuple (or a "done" indication), then re-read from remaining workers,
475 * this time using wait mode. Add all live readers (those producing at
476 * least one tuple) to the heap.
479 for (i = 0; i <= nreaders; i++)
481 CHECK_FOR_INTERRUPTS();
483 /* skip this source if already known done */
484 if ((i == 0) ? gm_state->need_to_scan_locally :
485 !gm_state->gm_tuple_buffers[i - 1].done)
487 if (TupIsNull(gm_state->gm_slots[i]))
489 /* Don't have a tuple yet, try to get one */
490 if (gather_merge_readnext(gm_state, i, nowait))
491 binaryheap_add_unordered(gm_state->gm_heap,
497 * We already got at least one tuple from this worker, but
498 * might as well see if it has any more ready by now.
500 load_tuple_array(gm_state, i);
505 /* need not recheck leader, since nowait doesn't matter for it */
506 for (i = 1; i <= nreaders; i++)
508 if (!gm_state->gm_tuple_buffers[i - 1].done &&
509 TupIsNull(gm_state->gm_slots[i]))
516 /* Now heapify the heap. */
517 binaryheap_build(gm_state->gm_heap);
519 gm_state->gm_initialized = true;
523 * Clear out the tuple table slot, and any unused pending tuples,
524 * for each gather merge input.
527 gather_merge_clear_tuples(GatherMergeState *gm_state)
531 for (i = 0; i < gm_state->nreaders; i++)
533 GMReaderTupleBuffer *tuple_buffer = &gm_state->gm_tuple_buffers[i];
535 while (tuple_buffer->readCounter < tuple_buffer->nTuples)
536 heap_freetuple(tuple_buffer->tuple[tuple_buffer->readCounter++]);
538 ExecClearTuple(gm_state->gm_slots[i + 1]);
543 * Read the next tuple for gather merge.
545 * Fetch the sorted tuple out of the heap.
547 static TupleTableSlot *
548 gather_merge_getnext(GatherMergeState *gm_state)
552 if (!gm_state->gm_initialized)
555 * First time through: pull the first tuple from each participant, and
558 gather_merge_init(gm_state);
563 * Otherwise, pull the next tuple from whichever participant we
564 * returned from last time, and reinsert that participant's index into
565 * the heap, because it might now compare differently against the
566 * other elements of the heap.
568 i = DatumGetInt32(binaryheap_first(gm_state->gm_heap));
570 if (gather_merge_readnext(gm_state, i, false))
571 binaryheap_replace_first(gm_state->gm_heap, Int32GetDatum(i));
574 /* reader exhausted, remove it from heap */
575 (void) binaryheap_remove_first(gm_state->gm_heap);
579 if (binaryheap_empty(gm_state->gm_heap))
581 /* All the queues are exhausted, and so is the heap */
582 gather_merge_clear_tuples(gm_state);
587 /* Return next tuple from whichever participant has the leading one */
588 i = DatumGetInt32(binaryheap_first(gm_state->gm_heap));
589 return gm_state->gm_slots[i];
594 * Read tuple(s) for given reader in nowait mode, and load into its tuple
595 * array, until we have MAX_TUPLE_STORE of them or would have to block.
598 load_tuple_array(GatherMergeState *gm_state, int reader)
600 GMReaderTupleBuffer *tuple_buffer;
603 /* Don't do anything if this is the leader. */
607 tuple_buffer = &gm_state->gm_tuple_buffers[reader - 1];
609 /* If there's nothing in the array, reset the counters to zero. */
610 if (tuple_buffer->nTuples == tuple_buffer->readCounter)
611 tuple_buffer->nTuples = tuple_buffer->readCounter = 0;
613 /* Try to fill additional slots in the array. */
614 for (i = tuple_buffer->nTuples; i < MAX_TUPLE_STORE; i++)
618 tuple = gm_readnext_tuple(gm_state,
621 &tuple_buffer->done);
622 if (!HeapTupleIsValid(tuple))
624 tuple_buffer->tuple[i] = tuple;
625 tuple_buffer->nTuples++;
630 * Store the next tuple for a given reader into the appropriate slot.
632 * Returns true if successful, false if not (either reader is exhausted,
633 * or we didn't want to wait for a tuple). Sets done flag if reader
634 * is found to be exhausted.
637 gather_merge_readnext(GatherMergeState *gm_state, int reader, bool nowait)
639 GMReaderTupleBuffer *tuple_buffer;
643 * If we're being asked to generate a tuple from the leader, then we just
644 * call ExecProcNode as normal to produce one.
648 if (gm_state->need_to_scan_locally)
650 PlanState *outerPlan = outerPlanState(gm_state);
651 TupleTableSlot *outerTupleSlot;
652 EState *estate = gm_state->ps.state;
654 /* Install our DSA area while executing the plan. */
655 estate->es_query_dsa = gm_state->pei ? gm_state->pei->area : NULL;
656 outerTupleSlot = ExecProcNode(outerPlan);
657 estate->es_query_dsa = NULL;
659 if (!TupIsNull(outerTupleSlot))
661 gm_state->gm_slots[0] = outerTupleSlot;
664 /* need_to_scan_locally serves as "done" flag for leader */
665 gm_state->need_to_scan_locally = false;
670 /* Otherwise, check the state of the relevant tuple buffer. */
671 tuple_buffer = &gm_state->gm_tuple_buffers[reader - 1];
673 if (tuple_buffer->nTuples > tuple_buffer->readCounter)
675 /* Return any tuple previously read that is still buffered. */
676 tup = tuple_buffer->tuple[tuple_buffer->readCounter++];
678 else if (tuple_buffer->done)
680 /* Reader is known to be exhausted. */
685 /* Read and buffer next tuple. */
686 tup = gm_readnext_tuple(gm_state,
689 &tuple_buffer->done);
690 if (!HeapTupleIsValid(tup))
694 * Attempt to read more tuples in nowait mode and store them in the
695 * pending-tuple array for the reader.
697 load_tuple_array(gm_state, reader);
700 Assert(HeapTupleIsValid(tup));
702 /* Build the TupleTableSlot for the given tuple */
703 ExecStoreHeapTuple(tup, /* tuple to store */
704 gm_state->gm_slots[reader], /* slot in which to store
706 true); /* pfree tuple when done with it */
712 * Attempt to read a tuple from given worker.
715 gm_readnext_tuple(GatherMergeState *gm_state, int nreader, bool nowait,
718 TupleQueueReader *reader;
721 /* Check for async events, particularly messages from workers. */
722 CHECK_FOR_INTERRUPTS();
725 * Attempt to read a tuple.
727 * Note that TupleQueueReaderNext will just return NULL for a worker which
728 * fails to initialize. We'll treat that worker as having produced no
729 * tuples; WaitForParallelWorkersToFinish will error out when we get
732 reader = gm_state->reader[nreader - 1];
733 tup = TupleQueueReaderNext(reader, nowait, done);
739 * We have one slot for each item in the heap array. We use SlotNumber
740 * to store slot indexes. This doesn't actually provide any formal
741 * type-safety, but it makes the code more self-documenting.
743 typedef int32 SlotNumber;
746 * Compare the tuples in the two given slots.
749 heap_compare_slots(Datum a, Datum b, void *arg)
751 GatherMergeState *node = (GatherMergeState *) arg;
752 SlotNumber slot1 = DatumGetInt32(a);
753 SlotNumber slot2 = DatumGetInt32(b);
755 TupleTableSlot *s1 = node->gm_slots[slot1];
756 TupleTableSlot *s2 = node->gm_slots[slot2];
759 Assert(!TupIsNull(s1));
760 Assert(!TupIsNull(s2));
762 for (nkey = 0; nkey < node->gm_nkeys; nkey++)
764 SortSupport sortKey = node->gm_sortkeys + nkey;
765 AttrNumber attno = sortKey->ssup_attno;
772 datum1 = slot_getattr(s1, attno, &isNull1);
773 datum2 = slot_getattr(s2, attno, &isNull2);
775 compare = ApplySortComparator(datum1, isNull1,
780 INVERT_COMPARE_RESULT(compare);