Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * nodeMergeAppend.c
4 : : * routines to handle MergeAppend nodes.
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/executor/nodeMergeAppend.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : /*
16 : : * INTERFACE ROUTINES
17 : : * ExecInitMergeAppend - initialize the MergeAppend node
18 : : * ExecMergeAppend - retrieve the next tuple from the node
19 : : * ExecEndMergeAppend - shut down the MergeAppend node
20 : : * ExecReScanMergeAppend - rescan the MergeAppend node
21 : : *
22 : : * NOTES
23 : : * A MergeAppend node contains a list of one or more subplans.
24 : : * These are each expected to deliver tuples that are sorted according
25 : : * to a common sort key. The MergeAppend node merges these streams
26 : : * to produce output sorted the same way.
27 : : *
28 : : * MergeAppend nodes don't make use of their left and right
29 : : * subtrees, rather they maintain a list of subplans so
30 : : * a typical MergeAppend node looks like this in the plan tree:
31 : : *
32 : : * ...
33 : : * /
34 : : * MergeAppend---+------+------+--- nil
35 : : * / \ | | |
36 : : * nil nil ... ... ...
37 : : * subplans
38 : : */
39 : :
40 : : #include "postgres.h"
41 : :
42 : : #include "executor/execAsync.h"
43 : : #include "executor/executor.h"
44 : : #include "executor/execPartition.h"
45 : : #include "executor/nodeMergeAppend.h"
46 : : #include "lib/binaryheap.h"
47 : : #include "miscadmin.h"
48 : : #include "utils/sortsupport.h"
49 : : #include "utils/wait_event.h"
50 : :
51 : : /*
52 : : * We have one slot for each item in the heap array. We use SlotNumber
53 : : * to store slot indexes. This doesn't actually provide any formal
54 : : * type-safety, but it makes the code more self-documenting.
55 : : */
56 : : typedef int32 SlotNumber;
57 : :
58 : : static TupleTableSlot *ExecMergeAppend(PlanState *pstate);
59 : : static int heap_compare_slots(Datum a, Datum b, void *arg);
60 : : static void ExecMergeAppendAsyncBegin(MergeAppendState *node,
61 : : Bitmapset *valid_asyncplans);
62 : : static void ExecMergeAppendAsyncRequest(MergeAppendState *node, SlotNumber i);
63 : : static void ExecMergeAppendAsyncDrain(MergeAppendState *node);
64 : : static void ExecMergeAppendAsyncEventWait(MergeAppendState *node);
65 : : static void ExecMergeAppendAsyncReset(MergeAppendState *node);
66 : :
67 : :
68 : : /* ----------------------------------------------------------------
69 : : * ExecInitMergeAppend
70 : : *
71 : : * Begin all of the subscans of the MergeAppend node.
72 : : * ----------------------------------------------------------------
73 : : */
74 : : MergeAppendState *
75 : 439 : ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
76 : : {
77 : 439 : MergeAppendState *mergestate = makeNode(MergeAppendState);
78 : : PlanState **mergeplanstates;
79 : : const TupleTableSlotOps *mergeops;
80 : : Bitmapset *validsubplans;
81 : : int nplans;
82 : : int i,
83 : : j;
84 : :
85 : : /* check for unsupported flags */
86 : : Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
87 : :
88 : : /*
89 : : * create new MergeAppendState for our node
90 : : */
91 : 439 : mergestate->ps.plan = (Plan *) node;
92 : 439 : mergestate->ps.state = estate;
93 : 439 : mergestate->ps.ExecProcNode = ExecMergeAppend;
94 : :
95 : : /* If run-time partition pruning is enabled, then set that up now */
96 [ + + ]: 439 : if (node->part_prune_index >= 0)
97 : : {
98 : : PartitionPruneState *prunestate;
99 : :
100 : : /*
101 : : * Set up pruning data structure. This also initializes the set of
102 : : * subplans to initialize (validsubplans) by taking into account the
103 : : * result of performing initial pruning if any.
104 : : */
105 : 52 : prunestate = ExecInitPartitionExecPruning(&mergestate->ps,
106 : 52 : list_length(node->mergeplans),
107 : : node->part_prune_index,
108 : : node->apprelids,
109 : : &validsubplans);
110 : 52 : mergestate->ms_prune_state = prunestate;
111 : 52 : nplans = bms_num_members(validsubplans);
112 : :
113 : : /*
114 : : * When no run-time pruning is required and there's at least one
115 : : * subplan, we can fill ms_valid_subplans immediately, preventing
116 : : * later calls to ExecFindMatchingSubPlans.
117 : : */
118 [ + + + + ]: 52 : if (!prunestate->do_exec_prune && nplans > 0)
119 : 28 : mergestate->ms_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
120 : : }
121 : : else
122 : : {
123 : 387 : nplans = list_length(node->mergeplans);
124 : :
125 : : /*
126 : : * When run-time partition pruning is not enabled we can just mark all
127 : : * subplans as valid; they must also all be initialized.
128 : : */
129 : : Assert(nplans > 0);
130 : 387 : mergestate->ms_valid_subplans = validsubplans =
131 : 387 : bms_add_range(NULL, 0, nplans - 1);
132 : 387 : mergestate->ms_prune_state = NULL;
133 : : }
134 : :
135 : 439 : mergeplanstates = palloc_array(PlanState *, nplans);
136 : 439 : mergestate->mergeplans = mergeplanstates;
137 : 439 : mergestate->ms_nplans = nplans;
138 : :
139 : 439 : mergestate->ms_slots = palloc0_array(TupleTableSlot *, nplans);
140 : 439 : mergestate->ms_heap = binaryheap_allocate(nplans, heap_compare_slots,
141 : : mergestate);
142 : :
143 : : /*
144 : : * call ExecInitNode on each of the valid plans to be executed and save
145 : : * the results into the mergeplanstates array.
146 : : *
147 : : * While at it, find out which subplans can be executed asynchronously.
148 : : */
149 : 439 : j = 0;
150 : 439 : i = -1;
151 : 439 : mergestate->ms_asyncplans = NULL;
152 : 439 : mergestate->ms_nasyncplans = 0;
153 [ + + ]: 1646 : while ((i = bms_next_member(validsubplans, i)) >= 0)
154 : : {
155 : 1207 : Plan *initNode = (Plan *) list_nth(node->mergeplans, i);
156 : :
157 : : /*
158 : : * Record async subplans. When executing EvalPlanQual, we treat them
159 : : * as sync ones; don't do this when initializing an EvalPlanQual plan
160 : : * tree.
161 : : */
162 [ + + + - ]: 1207 : if (initNode->async_capable && estate->es_epq_active == NULL)
163 : : {
164 : 54 : mergestate->ms_asyncplans =
165 : 54 : bms_add_member(mergestate->ms_asyncplans, j);
166 : 54 : mergestate->ms_nasyncplans++;
167 : : }
168 : :
169 : 1207 : mergeplanstates[j++] = ExecInitNode(initNode, estate, eflags);
170 : : }
171 : :
172 : : /*
173 : : * Initialize MergeAppend's result tuple type and slot. If the child
174 : : * plans all produce the same fixed slot type, we can use that slot type;
175 : : * otherwise make a virtual slot. (Note that the result slot itself is
176 : : * used only to return a null tuple at end of execution; real tuples are
177 : : * returned to the caller in the children's own result slots. What we are
178 : : * doing here is allowing the parent plan node to optimize if the
179 : : * MergeAppend will return only one kind of slot.)
180 : : */
181 : 439 : mergeops = ExecGetCommonSlotOps(mergeplanstates, j);
182 [ + + ]: 439 : if (mergeops != NULL)
183 : : {
184 : 317 : ExecInitResultTupleSlotTL(&mergestate->ps, mergeops);
185 : : }
186 : : else
187 : : {
188 : 122 : ExecInitResultTupleSlotTL(&mergestate->ps, &TTSOpsVirtual);
189 : : /* show that the output slot type is not fixed */
190 : 122 : mergestate->ps.resultopsset = true;
191 : 122 : mergestate->ps.resultopsfixed = false;
192 : : }
193 : :
194 : : /* Initialize async state */
195 : 439 : mergestate->ms_asyncrequests = NULL;
196 : 439 : mergestate->ms_asyncpending = NULL;
197 : 439 : mergestate->ms_eventset = NULL;
198 : :
199 [ + + ]: 439 : if (mergestate->ms_nasyncplans > 0)
200 : : {
201 : 25 : mergestate->ms_asyncrequests = palloc0_array(AsyncRequest *, nplans);
202 : :
203 : 25 : i = -1;
204 [ + + ]: 79 : while ((i = bms_next_member(mergestate->ms_asyncplans, i)) >= 0)
205 : : {
206 : : AsyncRequest *areq;
207 : :
208 : 54 : areq = palloc_object(AsyncRequest);
209 : 54 : areq->requestor = (PlanState *) mergestate;
210 : 54 : areq->requestee = mergeplanstates[i];
211 : 54 : areq->request_index = i;
212 : 54 : areq->callback_pending = false;
213 : 54 : areq->request_complete = false;
214 : 54 : areq->result = NULL;
215 : :
216 : 54 : mergestate->ms_asyncrequests[i] = areq;
217 : : }
218 : : }
219 : :
220 : : /*
221 : : * Miscellaneous initialization
222 : : */
223 : 439 : mergestate->ps.ps_ProjInfo = NULL;
224 : :
225 : : /*
226 : : * initialize sort-key information
227 : : */
228 : 439 : mergestate->ms_nkeys = node->numCols;
229 : 439 : mergestate->ms_sortkeys = palloc0_array(SortSupportData, node->numCols);
230 : :
231 [ + + ]: 962 : for (i = 0; i < node->numCols; i++)
232 : : {
233 : 523 : SortSupport sortKey = mergestate->ms_sortkeys + i;
234 : :
235 : 523 : sortKey->ssup_cxt = CurrentMemoryContext;
236 : 523 : sortKey->ssup_collation = node->collations[i];
237 : 523 : sortKey->ssup_nulls_first = node->nullsFirst[i];
238 : 523 : sortKey->ssup_attno = node->sortColIdx[i];
239 : :
240 : : /*
241 : : * It isn't feasible to perform abbreviated key conversion, since
242 : : * tuples are pulled into mergestate's binary heap as needed. It
243 : : * would likely be counter-productive to convert tuples into an
244 : : * abbreviated representation as they're pulled up, so opt out of that
245 : : * additional optimization entirely.
246 : : */
247 : 523 : sortKey->abbreviate = false;
248 : :
249 : 523 : PrepareSortSupportFromOrderingOp(node->sortOperators[i], sortKey);
250 : : }
251 : :
252 : : /*
253 : : * initialize to show we have not run the subplans yet
254 : : */
255 : 439 : mergestate->ms_initialized = false;
256 : :
257 : 439 : return mergestate;
258 : : }
259 : :
260 : : /* ----------------------------------------------------------------
261 : : * ExecMergeAppend
262 : : *
263 : : * Handles iteration over multiple subplans.
264 : : * ----------------------------------------------------------------
265 : : */
266 : : static TupleTableSlot *
267 : 110370 : ExecMergeAppend(PlanState *pstate)
268 : : {
269 : 110370 : MergeAppendState *node = castNode(MergeAppendState, pstate);
270 : : TupleTableSlot *result;
271 : : SlotNumber i;
272 : :
273 [ - + ]: 110370 : CHECK_FOR_INTERRUPTS();
274 : :
275 [ + + ]: 110370 : if (!node->ms_initialized)
276 : : {
277 : : /* Nothing to do if all subplans were pruned */
278 [ + + ]: 211 : if (node->ms_nplans == 0)
279 : 12 : return ExecClearTuple(node->ps.ps_ResultTupleSlot);
280 : :
281 : : /*
282 : : * If we've yet to determine the valid subplans then do so now. If
283 : : * run-time pruning is disabled then the valid subplans will always be
284 : : * set to all subplans.
285 : : */
286 [ + + ]: 199 : if (node->ms_valid_subplans == NULL)
287 : 13 : node->ms_valid_subplans =
288 : 13 : ExecFindMatchingSubPlans(node->ms_prune_state, false, NULL);
289 : :
290 : : /*
291 : : * First time through: pull the first tuple from each valid subplan,
292 : : * and set up the heap.
293 : : *
294 : : * When there are async-capable subplans, fire off all their requests
295 : : * up front so that they can run concurrently (for example, on
296 : : * different foreign servers). We then pull the first tuple from each
297 : : * synchronous subplan while the asynchronous ones are still in
298 : : * flight, and finally collect the asynchronous results.
299 : : */
300 [ + + ]: 199 : if (node->ms_nasyncplans > 0)
301 : : {
302 : : Bitmapset *valid_asyncplans;
303 : :
304 : : /* Determine the valid async subplans and begin executing them. */
305 : 15 : valid_asyncplans = bms_intersect(node->ms_valid_subplans,
306 : 15 : node->ms_asyncplans);
307 : 15 : ExecMergeAppendAsyncBegin(node, valid_asyncplans);
308 : :
309 : : /* Pull the first tuple from each valid synchronous subplan. */
310 : 15 : i = -1;
311 [ + + ]: 51 : while ((i = bms_next_member(node->ms_valid_subplans, i)) >= 0)
312 : : {
313 [ + + ]: 36 : if (bms_is_member(i, node->ms_asyncplans))
314 : 31 : continue;
315 : :
316 : 5 : node->ms_slots[i] = ExecProcNode(node->mergeplans[i]);
317 [ + - + - ]: 5 : if (!TupIsNull(node->ms_slots[i]))
318 : 5 : binaryheap_add_unordered(node->ms_heap, Int32GetDatum(i));
319 : : }
320 : :
321 : : /* Now wait for the asynchronous subplans to deliver their tuples. */
322 : 15 : ExecMergeAppendAsyncDrain(node);
323 : 14 : i = -1;
324 [ + + ]: 42 : while ((i = bms_next_member(valid_asyncplans, i)) >= 0)
325 : : {
326 [ + - + - ]: 28 : if (!TupIsNull(node->ms_slots[i]))
327 : 28 : binaryheap_add_unordered(node->ms_heap, Int32GetDatum(i));
328 : : }
329 : :
330 : 14 : bms_free(valid_asyncplans);
331 : : }
332 : : else
333 : : {
334 : 184 : i = -1;
335 [ + + ]: 709 : while ((i = bms_next_member(node->ms_valid_subplans, i)) >= 0)
336 : : {
337 : 525 : node->ms_slots[i] = ExecProcNode(node->mergeplans[i]);
338 [ + + + + ]: 525 : if (!TupIsNull(node->ms_slots[i]))
339 : 413 : binaryheap_add_unordered(node->ms_heap, Int32GetDatum(i));
340 : : }
341 : : }
342 : :
343 : 198 : binaryheap_build(node->ms_heap);
344 : 198 : node->ms_initialized = true;
345 : : }
346 : : else
347 : : {
348 : : /*
349 : : * Otherwise, pull the next tuple from whichever subplan we returned
350 : : * from last time, and reinsert the subplan index into the heap,
351 : : * because it might now compare differently against the existing
352 : : * elements of the heap. (We could perhaps simplify the logic a bit
353 : : * by doing this before returning from the prior call, but it's better
354 : : * to not pull tuples until necessary.)
355 : : *
356 : : * If the subplan is async-capable, we fire a fresh request for it and
357 : : * wait for the result. Unlike a plain Append, the merge cannot make
358 : : * progress until this particular subplan's next tuple is known (it
359 : : * might be the new heap minimum), so there is at most one outstanding
360 : : * async request during this phase.
361 : : */
362 : 110159 : i = DatumGetInt32(binaryheap_first(node->ms_heap));
363 : :
364 [ + + ]: 110159 : if (bms_is_member(i, node->ms_asyncplans))
365 : : {
366 : 1662 : ExecMergeAppendAsyncRequest(node, i);
367 : 1662 : ExecMergeAppendAsyncDrain(node);
368 : : }
369 : : else
370 : 108497 : node->ms_slots[i] = ExecProcNode(node->mergeplans[i]);
371 : :
372 [ + + + + ]: 110159 : if (!TupIsNull(node->ms_slots[i]))
373 : 109860 : binaryheap_replace_first(node->ms_heap, Int32GetDatum(i));
374 : : else
375 : 299 : (void) binaryheap_remove_first(node->ms_heap);
376 : : }
377 : :
378 [ + + ]: 110357 : if (binaryheap_empty(node->ms_heap))
379 : : {
380 : : /* All the subplans are exhausted, and so is the heap */
381 : 145 : result = ExecClearTuple(node->ps.ps_ResultTupleSlot);
382 : : }
383 : : else
384 : : {
385 : 110212 : i = DatumGetInt32(binaryheap_first(node->ms_heap));
386 : 110212 : result = node->ms_slots[i];
387 : : }
388 : :
389 : 110357 : return result;
390 : : }
391 : :
392 : : /*
393 : : * Compare the tuples in the two given slots.
394 : : */
395 : : static int32
396 : 102436 : heap_compare_slots(Datum a, Datum b, void *arg)
397 : : {
398 : 102436 : MergeAppendState *node = (MergeAppendState *) arg;
399 : 102436 : SlotNumber slot1 = DatumGetInt32(a);
400 : 102436 : SlotNumber slot2 = DatumGetInt32(b);
401 : :
402 : 102436 : TupleTableSlot *s1 = node->ms_slots[slot1];
403 : 102436 : TupleTableSlot *s2 = node->ms_slots[slot2];
404 : : int nkey;
405 : :
406 : : Assert(!TupIsNull(s1));
407 : : Assert(!TupIsNull(s2));
408 : :
409 [ + + ]: 137002 : for (nkey = 0; nkey < node->ms_nkeys; nkey++)
410 : : {
411 : 102653 : SortSupport sortKey = node->ms_sortkeys + nkey;
412 : 102653 : AttrNumber attno = sortKey->ssup_attno;
413 : : Datum datum1,
414 : : datum2;
415 : : bool isNull1,
416 : : isNull2;
417 : : int compare;
418 : :
419 : 102653 : datum1 = slot_getattr(s1, attno, &isNull1);
420 : 102653 : datum2 = slot_getattr(s2, attno, &isNull2);
421 : :
422 : 102653 : compare = ApplySortComparator(datum1, isNull1,
423 : : datum2, isNull2,
424 : : sortKey);
425 [ + + ]: 102653 : if (compare != 0)
426 : : {
427 [ + + ]: 68087 : INVERT_COMPARE_RESULT(compare);
428 : 68087 : return compare;
429 : : }
430 : : }
431 : 34349 : return 0;
432 : : }
433 : :
434 : : /* ----------------------------------------------------------------
435 : : * ExecEndMergeAppend
436 : : *
437 : : * Shuts down the subscans of the MergeAppend node.
438 : : *
439 : : * Returns nothing of interest.
440 : : * ----------------------------------------------------------------
441 : : */
442 : : void
443 : 438 : ExecEndMergeAppend(MergeAppendState *node)
444 : : {
445 : : PlanState **mergeplans;
446 : : int nplans;
447 : : int i;
448 : :
449 : : /*
450 : : * get information from the node
451 : : */
452 : 438 : mergeplans = node->mergeplans;
453 : 438 : nplans = node->ms_nplans;
454 : :
455 : : /*
456 : : * shut down each of the subscans
457 : : */
458 [ + + ]: 1642 : for (i = 0; i < nplans; i++)
459 : 1204 : ExecEndNode(mergeplans[i]);
460 : 438 : }
461 : :
462 : : void
463 : 17 : ExecReScanMergeAppend(MergeAppendState *node)
464 : : {
465 : : int i;
466 : :
467 : : /* If there are any async subplans, reset async requests made for them. */
468 [ + + ]: 17 : if (node->ms_nasyncplans > 0)
469 : 5 : ExecMergeAppendAsyncReset(node);
470 : :
471 : : /*
472 : : * If any PARAM_EXEC Params used in pruning expressions have changed, then
473 : : * we'd better unset the valid subplans so that they are reselected for
474 : : * the new parameter values.
475 : : */
476 [ + + + - ]: 22 : if (node->ms_prune_state &&
477 : 5 : bms_overlap(node->ps.chgParam,
478 : 5 : node->ms_prune_state->execparamids))
479 : : {
480 : 5 : bms_free(node->ms_valid_subplans);
481 : 5 : node->ms_valid_subplans = NULL;
482 : : }
483 : :
484 [ + + ]: 56 : for (i = 0; i < node->ms_nplans; i++)
485 : : {
486 : 39 : PlanState *subnode = node->mergeplans[i];
487 : :
488 : : /*
489 : : * ExecReScan doesn't know about my subplans, so I have to do
490 : : * changed-parameter signaling myself.
491 : : */
492 [ + - ]: 39 : if (node->ps.chgParam != NULL)
493 : 39 : UpdateChangedParamSet(subnode, node->ps.chgParam);
494 : :
495 : : /*
496 : : * If chgParam of subnode is not null then plan will be re-scanned by
497 : : * first ExecProcNode or by first ExecAsyncRequest.
498 : : */
499 [ - + ]: 39 : if (subnode->chgParam == NULL)
500 : 0 : ExecReScan(subnode);
501 : : }
502 : :
503 : 17 : binaryheap_reset(node->ms_heap);
504 : 17 : node->ms_initialized = false;
505 : 17 : }
506 : :
507 : : /* ----------------------------------------------------------------
508 : : * Asynchronous MergeAppend Support
509 : : * ----------------------------------------------------------------
510 : : */
511 : :
512 : : /* ----------------------------------------------------------------
513 : : * ExecMergeAppendAsyncBegin
514 : : *
515 : : * Begin executing the designated valid async-capable subplans.
516 : : * ----------------------------------------------------------------
517 : : */
518 : : static void
519 : 15 : ExecMergeAppendAsyncBegin(MergeAppendState *node, Bitmapset *valid_asyncplans)
520 : : {
521 : : int i;
522 : :
523 : : /* Backward scan is not supported by async-aware MergeAppends. */
524 : : Assert(ScanDirectionIsForward(node->ps.state->es_direction));
525 : :
526 : : /* We should never be called when there are no async subplans. */
527 : : Assert(node->ms_nasyncplans > 0);
528 : :
529 : : /* Make a request for each of the valid async subplans. */
530 : 15 : i = -1;
531 [ + + ]: 46 : while ((i = bms_next_member(valid_asyncplans, i)) >= 0)
532 : 31 : ExecMergeAppendAsyncRequest(node, i);
533 : 15 : }
534 : :
535 : : /* ----------------------------------------------------------------
536 : : * ExecMergeAppendAsyncRequest
537 : : *
538 : : * Request the next tuple from a single async-capable subplan.
539 : : *
540 : : * The subplan is marked as having an outstanding request. If
541 : : * the request happens to complete synchronously,
542 : : * ExecAsyncMergeAppendResponse clears that marker (and stores
543 : : * the result slot) before this returns.
544 : : * ----------------------------------------------------------------
545 : : */
546 : : static void
547 : 1693 : ExecMergeAppendAsyncRequest(MergeAppendState *node, SlotNumber i)
548 : : {
549 : 1693 : AsyncRequest *areq = node->ms_asyncrequests[i];
550 : :
551 : : Assert(areq != NULL);
552 : : Assert(areq->request_index == i);
553 : : Assert(!areq->callback_pending);
554 : : Assert(!bms_is_member(i, node->ms_asyncpending));
555 : :
556 : 1693 : node->ms_asyncpending = bms_add_member(node->ms_asyncpending, i);
557 : :
558 : : /* Do the actual work. */
559 : 1693 : ExecAsyncRequest(areq);
560 : 1693 : }
561 : :
562 : : /* ----------------------------------------------------------------
563 : : * ExecMergeAppendAsyncDrain
564 : : *
565 : : * Wait until every outstanding async request has delivered its
566 : : * tuple (or reached end-of-scan) into the corresponding
567 : : * ms_slots entry.
568 : : * ----------------------------------------------------------------
569 : : */
570 : : static void
571 : 1677 : ExecMergeAppendAsyncDrain(MergeAppendState *node)
572 : : {
573 [ + + ]: 1736 : while (!bms_is_empty(node->ms_asyncpending))
574 : : {
575 [ - + ]: 60 : CHECK_FOR_INTERRUPTS();
576 : :
577 : 60 : ExecMergeAppendAsyncEventWait(node);
578 : : }
579 : 1676 : }
580 : :
581 : : /* ----------------------------------------------------------------
582 : : * ExecMergeAppendAsyncEventWait
583 : : *
584 : : * Wait for file descriptor events and fire callbacks.
585 : : *
586 : : * Unlike an async Append, a MergeAppend cannot make progress
587 : : * until the outstanding tuples arrive, so we always wait
588 : : * (indefinitely) for at least one event.
589 : : * ----------------------------------------------------------------
590 : : */
591 : : static void
592 : 60 : ExecMergeAppendAsyncEventWait(MergeAppendState *node)
593 : : {
594 : 60 : int nevents = node->ms_nasyncplans + 2;
595 : :
596 : : /* We should never be called when there are no outstanding requests. */
597 : : Assert(!bms_is_empty(node->ms_asyncpending));
598 : :
599 : : Assert(node->ms_eventset == NULL);
600 : 60 : node->ms_eventset = CreateWaitEventSet(CurrentResourceOwner, nevents);
601 : 60 : ExecAsyncEventWait(node->ms_asyncplans,
602 : : node->ms_asyncrequests,
603 : 60 : node->ms_eventset,
604 : : nevents,
605 : : -1,
606 : : WAIT_EVENT_MERGE_APPEND_READY);
607 : 59 : FreeWaitEventSet(node->ms_eventset);
608 : 59 : node->ms_eventset = NULL;
609 : 59 : }
610 : :
611 : : /* ----------------------------------------------------------------
612 : : * ExecMergeAppendAsyncReset
613 : : *
614 : : * Reset asynchronous requests made for async-capable subplans.
615 : : * ----------------------------------------------------------------
616 : : */
617 : : static void
618 : 5 : ExecMergeAppendAsyncReset(MergeAppendState *node)
619 : : {
620 : : int i;
621 : :
622 : : /* We should never be called when there are no async subplans. */
623 : : Assert(node->ms_nasyncplans > 0);
624 : :
625 : : /* Reset async requests. */
626 : 5 : i = -1;
627 [ + + ]: 17 : while ((i = bms_next_member(node->ms_asyncplans, i)) >= 0)
628 : : {
629 : 12 : AsyncRequest *areq = node->ms_asyncrequests[i];
630 : :
631 : : /*
632 : : * Unlike in ExecAppendAsyncReset, the request wouldn't have been
633 : : * pending anymore at ReScan.
634 : : */
635 : : Assert(!areq->callback_pending);
636 : 12 : areq->request_complete = false;
637 : 12 : areq->result = NULL;
638 : : }
639 : :
640 : : /* Reset state variables. */
641 : 5 : bms_free(node->ms_asyncpending);
642 : 5 : node->ms_asyncpending = NULL;
643 : 5 : }
644 : :
645 : : /* ----------------------------------------------------------------
646 : : * ExecAsyncMergeAppendResponse
647 : : *
648 : : * Receive a response from an asynchronous request we made.
649 : : * ----------------------------------------------------------------
650 : : */
651 : : void
652 : 1755 : ExecAsyncMergeAppendResponse(AsyncRequest *areq)
653 : : {
654 : 1755 : MergeAppendState *node = (MergeAppendState *) areq->requestor;
655 : 1755 : TupleTableSlot *slot = areq->result;
656 : 1755 : SlotNumber i = areq->request_index;
657 : :
658 : : /* The result should be a TupleTableSlot or NULL. */
659 : : Assert(slot == NULL || IsA(slot, TupleTableSlot));
660 : :
661 : : /* Nothing to do if the request is still pending. */
662 [ + + ]: 1755 : if (!areq->request_complete)
663 : : {
664 : : /* The request would have been pending for a callback. */
665 : : Assert(areq->callback_pending);
666 : 63 : return;
667 : : }
668 : :
669 : : /*
670 : : * Store the delivered result (a tuple, or an empty/NULL slot at
671 : : * end-of-scan) as this subplan's current slot, and mark the request as no
672 : : * longer outstanding.
673 : : *
674 : : * Note that the binary heap is manipulated by ExecMergeAppend, not here,
675 : : * because the required operation differs between the initial heap build
676 : : * and later single-subplan refills.
677 : : */
678 : : Assert(bms_is_member(i, node->ms_asyncpending));
679 : 1692 : node->ms_slots[i] = slot;
680 : 1692 : node->ms_asyncpending = bms_del_member(node->ms_asyncpending, i);
681 : : }
|