Debian Codegraph @

src/backend/executor/nodeAppend.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * nodeAppend.c
4 * routines to handle append nodes.
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/executor/nodeAppend.c
12 *
13 *-------------------------------------------------------------------------
14 */
15 /* INTERFACE ROUTINES
16 * ExecInitAppend - initialize the append node
17 * ExecAppend - retrieve the next tuple from the node
18 * ExecEndAppend - shut down the append node
19 * ExecReScanAppend - rescan the append node
20 *
21 * NOTES
22 * Each append node contains a list of one or more subplans which
23 * must be iteratively processed (forwards or backwards).
24 * Tuples are retrieved by executing the 'whichplan'th subplan
25 * until the subplan stops returning tuples, at which point that
26 * plan is shut down and the next started up.
27 *
28 * Append nodes don't make use of their left and right
29 * subtrees, rather they maintain a list of subplans so
30 * a typical append node looks like this in the plan tree:
31 *
32 * ...
33 * /
34 * Append -------+------+------+--- nil
35 * / \ | | |
36 * nil nil ... ... ...
37 * subplans
38 *
39 * Append nodes are currently used for unions, and to support
40 * inheritance queries, where several relations need to be scanned.
41 * For example, in our standard person/student/employee/student-emp
42 * example, where student and employee inherit from person
43 * and student-emp inherits from student and employee, the
44 * query:
45 *
46 * select name from person
47 *
48 * generates the plan:
49 *
50 * |
51 * Append -------+-------+--------+--------+
52 * / \ | | | |
53 * nil nil Scan Scan Scan Scan
54 * | | | |
55 * person employee student student-emp
56 */
57
58 #include "postgres.h"
59
60 #include "executor/execAsync.h"
61 #include "executor/execPartition.h"
62 #include "executor/executor.h"
63 #include "executor/nodeAppend.h"
64 #include "miscadmin.h"
65 #include "pgstat.h"
66 #include "storage/latch.h"
67
68 /* Shared state for parallel-aware Append. */
69 struct ParallelAppendState
70 {
71 LWLock pa_lock; /* mutual exclusion to choose next subplan */
72 int pa_next_plan; /* next plan to choose by any worker */
73
74 /*
75 * pa_finished[i] should be true if no more workers should select subplan
76 * i. for a non-partial plan, this should be set to true as soon as a
77 * worker selects the plan; for a partial plan, it remains false until
78 * some worker executes the plan to completion.
79 */
80 bool pa_finished[FLEXIBLE_ARRAY_MEMBER];
81 };
82
83 #define INVALID_SUBPLAN_INDEX -1
84 #define EVENT_BUFFER_SIZE 16
85
86 static TupleTableSlot *ExecAppend(PlanState *pstate);
87 static bool choose_next_subplan_locally(AppendState *node);
88 static bool choose_next_subplan_for_leader(AppendState *node);
89 static bool choose_next_subplan_for_worker(AppendState *node);
90 static void mark_invalid_subplans_as_finished(AppendState *node);
91 static void ExecAppendAsyncBegin(AppendState *node);
92 static bool ExecAppendAsyncGetNext(AppendState *node, TupleTableSlot **result);
93 static bool ExecAppendAsyncRequest(AppendState *node, TupleTableSlot **result);
94 static void ExecAppendAsyncEventWait(AppendState *node);
95 static void ExecAppendAsyncReset(AppendState *node);
96 static void classify_matching_subplans(AppendState *node);
97
98 /* ----------------------------------------------------------------
99 * ExecInitAppend
100 *
101 * Begin all of the subscans of the append node.
102 *
103 * (This is potentially wasteful, since the entire result of the
104 * append node may not be scanned, but this way all of the
105 * structures get allocated in the executor's top level memory
106 * block instead of that of the call to ExecAppend.)
107 * ----------------------------------------------------------------
108 */
109 AppendState *
110 ExecInitAppend(Append *node, EState *estate, int eflags)
111 {
112 AppendState *appendstate = makeNode(AppendState);
113 PlanState **appendplanstates;
114 const TupleTableSlotOps *appendops;
115 Bitmapset *validsubplans;
116 Bitmapset *asyncplans;
117 int nplans;
118 int nasyncplans;
119 int firstvalid;
120 int i,
121 j;
122
123 /* check for unsupported flags */
124 Assert(!(eflags & EXEC_FLAG_MARK));
125
126 /*
127 * create new AppendState for our append node
128 */
129 appendstate->ps.plan = (Plan *) node;
130 appendstate->ps.state = estate;
131 appendstate->ps.ExecProcNode = ExecAppend;
132
133 /* Let choose_next_subplan_* function handle setting the first subplan */
134 appendstate->as_whichplan = INVALID_SUBPLAN_INDEX;
135 appendstate->as_syncdone = false;
136 appendstate->as_begun = false;
137
138 /* If run-time partition pruning is enabled, then set that up now */
139 if (node->part_prune_index >= 0)
140 {
141 PartitionPruneState *prunestate;
142
143 /*
144 * Set up pruning data structure. This also initializes the set of
145 * subplans to initialize (validsubplans) by taking into account the
146 * result of performing initial pruning if any.
147 */
148 prunestate = ExecInitPartitionExecPruning(&appendstate->ps,
149 list_length(node->appendplans),
150 node->part_prune_index,
151 node->apprelids,
152 &validsubplans);
153 appendstate->as_prune_state = prunestate;
154 nplans = bms_num_members(validsubplans);
155
156 /*
157 * When no run-time pruning is required and there's at least one
158 * subplan, we can fill as_valid_subplans immediately, preventing
159 * later calls to ExecFindMatchingSubPlans.
160 */
161 if (!prunestate->do_exec_prune && nplans > 0)
162 {
163 appendstate->as_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
164 appendstate->as_valid_subplans_identified = true;
165 }
166 }
167 else
168 {
169 nplans = list_length(node->appendplans);
170
171 /*
172 * When run-time partition pruning is not enabled we can just mark all
173 * subplans as valid; they must also all be initialized.
174 */
175 Assert(nplans > 0);
176 appendstate->as_valid_subplans = validsubplans =
177 bms_add_range(NULL, 0, nplans - 1);
178 appendstate->as_valid_subplans_identified = true;
179 appendstate->as_prune_state = NULL;
180 }
181
182 appendplanstates = (PlanState **) palloc(nplans *
183 sizeof(PlanState *));
184
185 /*
186 * call ExecInitNode on each of the valid plans to be executed and save
187 * the results into the appendplanstates array.
188 *
189 * While at it, find out the first valid partial plan.
190 */
191 j = 0;
192 asyncplans = NULL;
193 nasyncplans = 0;
194 firstvalid = nplans;
195 i = -1;
196 while ((i = bms_next_member(validsubplans, i)) >= 0)
197 {
198 Plan *initNode = (Plan *) list_nth(node->appendplans, i);
199
200 /*
201 * Record async subplans. When executing EvalPlanQual, we treat them
202 * as sync ones; don't do this when initializing an EvalPlanQual plan
203 * tree.
204 */
205 if (initNode->async_capable && estate->es_epq_active == NULL)
206 {
207 asyncplans = bms_add_member(asyncplans, j);
208 nasyncplans++;
209 }
210
211 /*
212 * Record the lowest appendplans index which is a valid partial plan.
213 */
214 if (i >= node->first_partial_plan && j < firstvalid)
215 firstvalid = j;
216
217 appendplanstates[j++] = ExecInitNode(initNode, estate, eflags);
218 }
219
220 appendstate->as_first_partial_plan = firstvalid;
221 appendstate->appendplans = appendplanstates;
222 appendstate->as_nplans = nplans;
223
224 /*
225 * Initialize Append's result tuple type and slot. If the child plans all
226 * produce the same fixed slot type, we can use that slot type; otherwise
227 * make a virtual slot. (Note that the result slot itself is used only to
228 * return a null tuple at end of execution; real tuples are returned to
229 * the caller in the children's own result slots. What we are doing here
230 * is allowing the parent plan node to optimize if the Append will return
231 * only one kind of slot.)
232 */
233 appendops = ExecGetCommonSlotOps(appendplanstates, j);
234 if (appendops != NULL)
235 {
236 ExecInitResultTupleSlotTL(&appendstate->ps, appendops);
237 }
238 else
239 {
240 ExecInitResultTupleSlotTL(&appendstate->ps, &TTSOpsVirtual);
241 /* show that the output slot type is not fixed */
242 appendstate->ps.resultopsset = true;
243 appendstate->ps.resultopsfixed = false;
244 }
245
246 /* Initialize async state */
247 appendstate->as_asyncplans = asyncplans;
248 appendstate->as_nasyncplans = nasyncplans;
249 appendstate->as_asyncrequests = NULL;
250 appendstate->as_asyncresults = NULL;
251 appendstate->as_nasyncresults = 0;
252 appendstate->as_nasyncremain = 0;
253 appendstate->as_needrequest = NULL;
254 appendstate->as_eventset = NULL;
255 appendstate->as_valid_asyncplans = NULL;
256
257 if (nasyncplans > 0)
258 {
259 appendstate->as_asyncrequests = (AsyncRequest **)
260 palloc0(nplans * sizeof(AsyncRequest *));
261
262 i = -1;
263 while ((i = bms_next_member(asyncplans, i)) >= 0)
264 {
265 AsyncRequest *areq;
266
267 areq = palloc(sizeof(AsyncRequest));
268 areq->requestor = (PlanState *) appendstate;
269 areq->requestee = appendplanstates[i];
270 areq->request_index = i;
271 areq->callback_pending = false;
272 areq->request_complete = false;
273 areq->result = NULL;
274
275 appendstate->as_asyncrequests[i] = areq;
276 }
277
278 appendstate->as_asyncresults = (TupleTableSlot **)
279 palloc0(nasyncplans * sizeof(TupleTableSlot *));
280
281 if (appendstate->as_valid_subplans_identified)
282 classify_matching_subplans(appendstate);
283 }
284
285 /*
286 * Miscellaneous initialization
287 */
288
289 appendstate->ps.ps_ProjInfo = NULL;
290
291 /* For parallel query, this will be overridden later. */
292 appendstate->choose_next_subplan = choose_next_subplan_locally;
293
294 return appendstate;
295 }
296
297 /* ----------------------------------------------------------------
298 * ExecAppend
299 *
300 * Handles iteration over multiple subplans.
301 * ----------------------------------------------------------------
302 */
303 static TupleTableSlot *
304 ExecAppend(PlanState *pstate)
305 {
306 AppendState *node = castNode(AppendState, pstate);
307 TupleTableSlot *result;
308
309 /*
310 * If this is the first call after Init or ReScan, we need to do the
311 * initialization work.
312 */
313 if (!node->as_begun)
314 {
315 Assert(node->as_whichplan == INVALID_SUBPLAN_INDEX);
316 Assert(!node->as_syncdone);
317
318 /* Nothing to do if there are no subplans */
319 if (node->as_nplans == 0)
320 return ExecClearTuple(node->ps.ps_ResultTupleSlot);
321
322 /* If there are any async subplans, begin executing them. */
323 if (node->as_nasyncplans > 0)
324 ExecAppendAsyncBegin(node);
325
326 /*
327 * If no sync subplan has been chosen, we must choose one before
328 * proceeding.
329 */
330 if (!node->choose_next_subplan(node) && node->as_nasyncremain == 0)
331 return ExecClearTuple(node->ps.ps_ResultTupleSlot);
332
333 Assert(node->as_syncdone ||
334 (node->as_whichplan >= 0 &&
335 node->as_whichplan < node->as_nplans));
336
337 /* And we're initialized. */
338 node->as_begun = true;
339 }
340
341 for (;;)
342 {
343 PlanState *subnode;
344
345 CHECK_FOR_INTERRUPTS();
346
347 /*
348 * try to get a tuple from an async subplan if any
349 */
350 if (node->as_syncdone || !bms_is_empty(node->as_needrequest))
351 {
352 if (ExecAppendAsyncGetNext(node, &result))
353 return result;
354 Assert(!node->as_syncdone);
355 Assert(bms_is_empty(node->as_needrequest));
356 }
357
358 /*
359 * figure out which sync subplan we are currently processing
360 */
361 Assert(node->as_whichplan >= 0 && node->as_whichplan < node->as_nplans);
362 subnode = node->appendplans[node->as_whichplan];
363
364 /*
365 * get a tuple from the subplan
366 */
367 result = ExecProcNode(subnode);
368
369 if (!TupIsNull(result))
370 {
371 /*
372 * If the subplan gave us something then return it as-is. We do
373 * NOT make use of the result slot that was set up in
374 * ExecInitAppend; there's no need for it.
375 */
376 return result;
377 }
378
379 /*
380 * wait or poll for async events if any. We do this before checking
381 * for the end of iteration, because it might drain the remaining
382 * async subplans.
383 */
384 if (node->as_nasyncremain > 0)
385 ExecAppendAsyncEventWait(node);
386
387 /* choose new sync subplan; if no sync/async subplans, we're done */
388 if (!node->choose_next_subplan(node) && node->as_nasyncremain == 0)
389 return ExecClearTuple(node->ps.ps_ResultTupleSlot);
390 }
391 }
392
393 /* ----------------------------------------------------------------
394 * ExecEndAppend
395 *
396 * Shuts down the subscans of the append node.
397 *
398 * Returns nothing of interest.
399 * ----------------------------------------------------------------
400 */
401 void
402 ExecEndAppend(AppendState *node)
403 {
404 PlanState **appendplans;
405 int nplans;
406 int i;
407
408 /*
409 * get information from the node
410 */
411 appendplans = node->appendplans;
412 nplans = node->as_nplans;
413
414 /*
415 * shut down each of the subscans
416 */
417 for (i = 0; i < nplans; i++)
418 ExecEndNode(appendplans[i]);
419 }
420
421 void
422 ExecReScanAppend(AppendState *node)
423 {
424 int nasyncplans = node->as_nasyncplans;
425 int i;
426
427 /* If there are any async subplans, reset async requests made for them. */
428 if (nasyncplans > 0)
429 ExecAppendAsyncReset(node);
430
431 /*
432 * If any PARAM_EXEC Params used in pruning expressions have changed, then
433 * we'd better unset the valid subplans so that they are reselected for
434 * the new parameter values.
435 */
436 if (node->as_prune_state &&
437 bms_overlap(node->ps.chgParam,
438 node->as_prune_state->execparamids))
439 {
440 node->as_valid_subplans_identified = false;
441 bms_free(node->as_valid_subplans);
442 node->as_valid_subplans = NULL;
443 bms_free(node->as_valid_asyncplans);
444 node->as_valid_asyncplans = NULL;
445 }
446
447 for (i = 0; i < node->as_nplans; i++)
448 {
449 PlanState *subnode = node->appendplans[i];
450
451 /*
452 * ExecReScan doesn't know about my subplans, so I have to do
453 * changed-parameter signaling myself.
454 */
455 if (node->ps.chgParam != NULL)
456 UpdateChangedParamSet(subnode, node->ps.chgParam);
457
458 /*
459 * If chgParam of subnode is not null then plan will be re-scanned by
460 * first ExecProcNode or by first ExecAsyncRequest.
461 */
462 if (subnode->chgParam == NULL)
463 ExecReScan(subnode);
464 }
465
466 /* Let choose_next_subplan_* function handle setting the first subplan */
467 node->as_whichplan = INVALID_SUBPLAN_INDEX;
468 node->as_syncdone = false;
469 node->as_begun = false;
470 }
471
472 /* ----------------------------------------------------------------
473 * Parallel Append Support
474 * ----------------------------------------------------------------
475 */
476
477 /* ----------------------------------------------------------------
478 * ExecAppendEstimate
479 *
480 * Compute the amount of space we'll need in the parallel
481 * query DSM, and inform pcxt->estimator about our needs.
482 * ----------------------------------------------------------------
483 */
484 void
485 ExecAppendEstimate(AppendState *node,
486 ParallelContext *pcxt)
487 {
488 node->pstate_len =
489 add_size(offsetof(ParallelAppendState, pa_finished),
490 sizeof(bool) * node->as_nplans);
491
492 shm_toc_estimate_chunk(&pcxt->estimator, node->pstate_len);
493 shm_toc_estimate_keys(&pcxt->estimator, 1);
494 }
495
496
497 /* ----------------------------------------------------------------
498 * ExecAppendInitializeDSM
499 *
500 * Set up shared state for Parallel Append.
501 * ----------------------------------------------------------------
502 */
503 void
504 ExecAppendInitializeDSM(AppendState *node,
505 ParallelContext *pcxt)
506 {
507 ParallelAppendState *pstate;
508
509 pstate = shm_toc_allocate(pcxt->toc, node->pstate_len);
510 memset(pstate, 0, node->pstate_len);
511 LWLockInitialize(&pstate->pa_lock, LWTRANCHE_PARALLEL_APPEND);
512 shm_toc_insert(pcxt->toc, node->ps.plan->plan_node_id, pstate);
513
514 node->as_pstate = pstate;
515 node->choose_next_subplan = choose_next_subplan_for_leader;
516 }
517
518 /* ----------------------------------------------------------------
519 * ExecAppendReInitializeDSM
520 *
521 * Reset shared state before beginning a fresh scan.
522 * ----------------------------------------------------------------
523 */
524 void
525 ExecAppendReInitializeDSM(AppendState *node, ParallelContext *pcxt)
526 {
527 ParallelAppendState *pstate = node->as_pstate;
528
529 pstate->pa_next_plan = 0;
530 memset(pstate->pa_finished, 0, sizeof(bool) * node->as_nplans);
531 }
532
533 /* ----------------------------------------------------------------
534 * ExecAppendInitializeWorker
535 *
536 * Copy relevant information from TOC into planstate, and initialize
537 * whatever is required to choose and execute the optimal subplan.
538 * ----------------------------------------------------------------
539 */
540 void
541 ExecAppendInitializeWorker(AppendState *node, ParallelWorkerContext *pwcxt)
542 {
543 node->as_pstate = shm_toc_lookup(pwcxt->toc, node->ps.plan->plan_node_id, false);
544 node->choose_next_subplan = choose_next_subplan_for_worker;
545 }
546
547 /* ----------------------------------------------------------------
548 * choose_next_subplan_locally
549 *
550 * Choose next sync subplan for a non-parallel-aware Append,
551 * returning false if there are no more.
552 * ----------------------------------------------------------------
553 */
554 static bool
555 choose_next_subplan_locally(AppendState *node)
556 {
557 int whichplan = node->as_whichplan;
558 int nextplan;
559
560 /* We should never be called when there are no subplans */
561 Assert(node->as_nplans > 0);
562
563 /* Nothing to do if syncdone */
564 if (node->as_syncdone)
565 return false;
566
567 /*
568 * If first call then have the bms member function choose the first valid
569 * sync subplan by initializing whichplan to -1. If there happen to be no
570 * valid sync subplans then the bms member function will handle that by
571 * returning a negative number which will allow us to exit returning a
572 * false value.
573 */
574 if (whichplan == INVALID_SUBPLAN_INDEX)
575 {
576 if (node->as_nasyncplans > 0)
577 {
578 /* We'd have filled as_valid_subplans already */
579 Assert(node->as_valid_subplans_identified);
580 }
581 else if (!node->as_valid_subplans_identified)
582 {
583 node->as_valid_subplans =
584 ExecFindMatchingSubPlans(node->as_prune_state, false, NULL);
585 node->as_valid_subplans_identified = true;
586 }
587
588 whichplan = -1;
589 }
590
591 /* Ensure whichplan is within the expected range */
592 Assert(whichplan >= -1 && whichplan <= node->as_nplans);
593
594 if (ScanDirectionIsForward(node->ps.state->es_direction))
595 nextplan = bms_next_member(node->as_valid_subplans, whichplan);
596 else
597 nextplan = bms_prev_member(node->as_valid_subplans, whichplan);
598
599 if (nextplan < 0)
600 {
601 /* Set as_syncdone if in async mode */
602 if (node->as_nasyncplans > 0)
603 node->as_syncdone = true;
604 return false;
605 }
606
607 node->as_whichplan = nextplan;
608
609 return true;
610 }
611
612 /* ----------------------------------------------------------------
613 * choose_next_subplan_for_leader
614 *
615 * Try to pick a plan which doesn't commit us to doing much
616 * work locally, so that as much work as possible is done in
617 * the workers. Cheapest subplans are at the end.
618 * ----------------------------------------------------------------
619 */
620 static bool
621 choose_next_subplan_for_leader(AppendState *node)
622 {
623 ParallelAppendState *pstate = node->as_pstate;
624
625 /* Backward scan is not supported by parallel-aware plans */
626 Assert(ScanDirectionIsForward(node->ps.state->es_direction));
627
628 /* We should never be called when there are no subplans */
629 Assert(node->as_nplans > 0);
630
631 LWLockAcquire(&pstate->pa_lock, LW_EXCLUSIVE);
632
633 if (node->as_whichplan != INVALID_SUBPLAN_INDEX)
634 {
635 /* Mark just-completed subplan as finished. */
636 node->as_pstate->pa_finished[node->as_whichplan] = true;
637 }
638 else
639 {
640 /* Start with last subplan. */
641 node->as_whichplan = node->as_nplans - 1;
642
643 /*
644 * If we've yet to determine the valid subplans then do so now. If
645 * run-time pruning is disabled then the valid subplans will always be
646 * set to all subplans.
647 */
648 if (!node->as_valid_subplans_identified)
649 {
650 node->as_valid_subplans =
651 ExecFindMatchingSubPlans(node->as_prune_state, false, NULL);
652 node->as_valid_subplans_identified = true;
653
654 /*
655 * Mark each invalid plan as finished to allow the loop below to
656 * select the first valid subplan.
657 */
658 mark_invalid_subplans_as_finished(node);
659 }
660 }
661
662 /* Loop until we find a subplan to execute. */
663 while (pstate->pa_finished[node->as_whichplan])
664 {
665 if (node->as_whichplan == 0)
666 {
667 pstate->pa_next_plan = INVALID_SUBPLAN_INDEX;
668 node->as_whichplan = INVALID_SUBPLAN_INDEX;
669 LWLockRelease(&pstate->pa_lock);
670 return false;
671 }
672
673 /*
674 * We needn't pay attention to as_valid_subplans here as all invalid
675 * plans have been marked as finished.
676 */
677 node->as_whichplan--;
678 }
679
680 /* If non-partial, immediately mark as finished. */
681 if (node->as_whichplan < node->as_first_partial_plan)
682 node->as_pstate->pa_finished[node->as_whichplan] = true;
683
684 LWLockRelease(&pstate->pa_lock);
685
686 return true;
687 }
688
689 /* ----------------------------------------------------------------
690 * choose_next_subplan_for_worker
691 *
692 * Choose next subplan for a parallel-aware Append, returning
693 * false if there are no more.
694 *
695 * We start from the first plan and advance through the list;
696 * when we get back to the end, we loop back to the first
697 * partial plan. This assigns the non-partial plans first in
698 * order of descending cost and then spreads out the workers
699 * as evenly as possible across the remaining partial plans.
700 * ----------------------------------------------------------------
701 */
702 static bool
703 choose_next_subplan_for_worker(AppendState *node)
704 {
705 ParallelAppendState *pstate = node->as_pstate;
706
707 /* Backward scan is not supported by parallel-aware plans */
708 Assert(ScanDirectionIsForward(node->ps.state->es_direction));
709
710 /* We should never be called when there are no subplans */
711 Assert(node->as_nplans > 0);
712
713 LWLockAcquire(&pstate->pa_lock, LW_EXCLUSIVE);
714
715 /* Mark just-completed subplan as finished. */
716 if (node->as_whichplan != INVALID_SUBPLAN_INDEX)
717 node->as_pstate->pa_finished[node->as_whichplan] = true;
718
719 /*
720 * If we've yet to determine the valid subplans then do so now. If
721 * run-time pruning is disabled then the valid subplans will always be set
722 * to all subplans.
723 */
724 else if (!node->as_valid_subplans_identified)
725 {
726 node->as_valid_subplans =
727 ExecFindMatchingSubPlans(node->as_prune_state, false, NULL);
728 node->as_valid_subplans_identified = true;
729
730 mark_invalid_subplans_as_finished(node);
731 }
732
733 /* If all the plans are already done, we have nothing to do */
734 if (pstate->pa_next_plan == INVALID_SUBPLAN_INDEX)
735 {
736 LWLockRelease(&pstate->pa_lock);
737 return false;
738 }
739
740 /* Save the plan from which we are starting the search. */
741 node->as_whichplan = pstate->pa_next_plan;
742
743 /* Loop until we find a valid subplan to execute. */
744 while (pstate->pa_finished[pstate->pa_next_plan])
745 {
746 int nextplan;
747
748 nextplan = bms_next_member(node->as_valid_subplans,
749 pstate->pa_next_plan);
750 if (nextplan >= 0)
751 {
752 /* Advance to the next valid plan. */
753 pstate->pa_next_plan = nextplan;
754 }
755 else if (node->as_whichplan > node->as_first_partial_plan)
756 {
757 /*
758 * Try looping back to the first valid partial plan, if there is
759 * one. If there isn't, arrange to bail out below.
760 */
761 nextplan = bms_next_member(node->as_valid_subplans,
762 node->as_first_partial_plan - 1);
763 pstate->pa_next_plan =
764 nextplan < 0 ? node->as_whichplan : nextplan;
765 }
766 else
767 {
768 /*
769 * At last plan, and either there are no partial plans or we've
770 * tried them all. Arrange to bail out.
771 */
772 pstate->pa_next_plan = node->as_whichplan;
773 }
774
775 if (pstate->pa_next_plan == node->as_whichplan)
776 {
777 /* We've tried everything! */
778 pstate->pa_next_plan = INVALID_SUBPLAN_INDEX;
779 LWLockRelease(&pstate->pa_lock);
780 return false;
781 }
782 }
783
784 /* Pick the plan we found, and advance pa_next_plan one more time. */
785 node->as_whichplan = pstate->pa_next_plan;
786 pstate->pa_next_plan = bms_next_member(node->as_valid_subplans,
787 pstate->pa_next_plan);
788
789 /*
790 * If there are no more valid plans then try setting the next plan to the
791 * first valid partial plan.
792 */
793 if (pstate->pa_next_plan < 0)
794 {
795 int nextplan = bms_next_member(node->as_valid_subplans,
796 node->as_first_partial_plan - 1);
797
798 if (nextplan >= 0)
799 pstate->pa_next_plan = nextplan;
800 else
801 {
802 /*
803 * There are no valid partial plans, and we already chose the last
804 * non-partial plan; so flag that there's nothing more for our
805 * fellow workers to do.
806 */
807 pstate->pa_next_plan = INVALID_SUBPLAN_INDEX;
808 }
809 }
810
811 /* If non-partial, immediately mark as finished. */
812 if (node->as_whichplan < node->as_first_partial_plan)
813 node->as_pstate->pa_finished[node->as_whichplan] = true;
814
815 LWLockRelease(&pstate->pa_lock);
816
817 return true;
818 }
819
820 /*
821 * mark_invalid_subplans_as_finished
822 * Marks the ParallelAppendState's pa_finished as true for each invalid
823 * subplan.
824 *
825 * This function should only be called for parallel Append with run-time
826 * pruning enabled.
827 */
828 static void
829 mark_invalid_subplans_as_finished(AppendState *node)
830 {
831 int i;
832
833 /* Only valid to call this while in parallel Append mode */
834 Assert(node->as_pstate);
835
836 /* Shouldn't have been called when run-time pruning is not enabled */
837 Assert(node->as_prune_state);
838
839 /* Nothing to do if all plans are valid */
840 if (bms_num_members(node->as_valid_subplans) == node->as_nplans)
841 return;
842
843 /* Mark all non-valid plans as finished */
844 for (i = 0; i < node->as_nplans; i++)
845 {
846 if (!bms_is_member(i, node->as_valid_subplans))
847 node->as_pstate->pa_finished[i] = true;
848 }
849 }
850
851 /* ----------------------------------------------------------------
852 * Asynchronous Append Support
853 * ----------------------------------------------------------------
854 */
855
856 /* ----------------------------------------------------------------
857 * ExecAppendAsyncBegin
858 *
859 * Begin executing designed async-capable subplans.
860 * ----------------------------------------------------------------
861 */
862 static void
863 ExecAppendAsyncBegin(AppendState *node)
864 {
865 int i;
866
867 /* Backward scan is not supported by async-aware Appends. */
868 Assert(ScanDirectionIsForward(node->ps.state->es_direction));
869
870 /* We should never be called when there are no subplans */
871 Assert(node->as_nplans > 0);
872
873 /* We should never be called when there are no async subplans. */
874 Assert(node->as_nasyncplans > 0);
875
876 /* If we've yet to determine the valid subplans then do so now. */
877 if (!node->as_valid_subplans_identified)
878 {
879 node->as_valid_subplans =
880 ExecFindMatchingSubPlans(node->as_prune_state, false, NULL);
881 node->as_valid_subplans_identified = true;
882
883 classify_matching_subplans(node);
884 }
885
886 /* Initialize state variables. */
887 node->as_syncdone = bms_is_empty(node->as_valid_subplans);
888 node->as_nasyncremain = bms_num_members(node->as_valid_asyncplans);
889
890 /* Nothing to do if there are no valid async subplans. */
891 if (node->as_nasyncremain == 0)
892 return;
893
894 /* Make a request for each of the valid async subplans. */
895 i = -1;
896 while ((i = bms_next_member(node->as_valid_asyncplans, i)) >= 0)
897 {
898 AsyncRequest *areq = node->as_asyncrequests[i];
899
900 Assert(areq->request_index == i);
901 Assert(!areq->callback_pending);
902
903 /* Do the actual work. */
904 ExecAsyncRequest(areq);
905 }
906 }
907
908 /* ----------------------------------------------------------------
909 * ExecAppendAsyncGetNext
910 *
911 * Get the next tuple from any of the asynchronous subplans.
912 * ----------------------------------------------------------------
913 */
914 static bool
915 ExecAppendAsyncGetNext(AppendState *node, TupleTableSlot **result)
916 {
917 *result = NULL;
918
919 /* We should never be called when there are no valid async subplans. */
920 Assert(node->as_nasyncremain > 0);
921
922 /* Request a tuple asynchronously. */
923 if (ExecAppendAsyncRequest(node, result))
924 return true;
925
926 while (node->as_nasyncremain > 0)
927 {
928 CHECK_FOR_INTERRUPTS();
929
930 /* Wait or poll for async events. */
931 ExecAppendAsyncEventWait(node);
932
933 /* Request a tuple asynchronously. */
934 if (ExecAppendAsyncRequest(node, result))
935 return true;
936
937 /* Break from loop if there's any sync subplan that isn't complete. */
938 if (!node->as_syncdone)
939 break;
940 }
941
942 /*
943 * If all sync subplans are complete, we're totally done scanning the
944 * given node. Otherwise, we're done with the asynchronous stuff but must
945 * continue scanning the sync subplans.
946 */
947 if (node->as_syncdone)
948 {
949 Assert(node->as_nasyncremain == 0);
950 *result = ExecClearTuple(node->ps.ps_ResultTupleSlot);
951 return true;
952 }
953
954 return false;
955 }
956
957 /* ----------------------------------------------------------------
958 * ExecAppendAsyncRequest
959 *
960 * Request a tuple asynchronously.
961 * ----------------------------------------------------------------
962 */
963 static bool
964 ExecAppendAsyncRequest(AppendState *node, TupleTableSlot **result)
965 {
966 Bitmapset *needrequest;
967 int i;
968
969 /* Nothing to do if there are no async subplans needing a new request. */
970 if (bms_is_empty(node->as_needrequest))
971 {
972 Assert(node->as_nasyncresults == 0);
973 return false;
974 }
975
976 /*
977 * If there are any asynchronously-generated results that have not yet
978 * been returned, we have nothing to do; just return one of them.
979 */
980 if (node->as_nasyncresults > 0)
981 {
982 --node->as_nasyncresults;
983 *result = node->as_asyncresults[node->as_nasyncresults];
984 return true;
985 }
986
987 /* Make a new request for each of the async subplans that need it. */
988 needrequest = node->as_needrequest;
989 node->as_needrequest = NULL;
990 i = -1;
991 while ((i = bms_next_member(needrequest, i)) >= 0)
992 {
993 AsyncRequest *areq = node->as_asyncrequests[i];
994
995 /* Do the actual work. */
996 ExecAsyncRequest(areq);
997 }
998 bms_free(needrequest);
999
1000 /* Return one of the asynchronously-generated results if any. */
1001 if (node->as_nasyncresults > 0)
1002 {
1003 --node->as_nasyncresults;
1004 *result = node->as_asyncresults[node->as_nasyncresults];
1005 return true;
1006 }
1007
1008 return false;
1009 }
1010
1011 /* ----------------------------------------------------------------
1012 * ExecAppendAsyncEventWait
1013 *
1014 * Wait or poll for file descriptor events and fire callbacks.
1015 * ----------------------------------------------------------------
1016 */
1017 static void
1018 ExecAppendAsyncEventWait(AppendState *node)
1019 {
1020 int nevents = node->as_nasyncplans + 2;
1021 long timeout = node->as_syncdone ? -1 : 0;
1022 WaitEvent occurred_event[EVENT_BUFFER_SIZE];
1023 int noccurred;
1024 int i;
1025
1026 /* We should never be called when there are no valid async subplans. */
1027 Assert(node->as_nasyncremain > 0);
1028
1029 Assert(node->as_eventset == NULL);
1030 node->as_eventset = CreateWaitEventSet(CurrentResourceOwner, nevents);
1031 AddWaitEventToSet(node->as_eventset, WL_EXIT_ON_PM_DEATH, PGINVALID_SOCKET,
1032 NULL, NULL);
1033
1034 /* Give each waiting subplan a chance to add an event. */
1035 i = -1;
1036 while ((i = bms_next_member(node->as_asyncplans, i)) >= 0)
1037 {
1038 AsyncRequest *areq = node->as_asyncrequests[i];
1039
1040 if (areq->callback_pending)
1041 ExecAsyncConfigureWait(areq);
1042 }
1043
1044 /*
1045 * No need for further processing if none of the subplans configured any
1046 * events.
1047 */
1048 if (GetNumRegisteredWaitEvents(node->as_eventset) == 1)
1049 {
1050 FreeWaitEventSet(node->as_eventset);
1051 node->as_eventset = NULL;
1052 return;
1053 }
1054
1055 /*
1056 * Add the process latch to the set, so that we wake up to process the
1057 * standard interrupts with CHECK_FOR_INTERRUPTS().
1058 *
1059 * NOTE: For historical reasons, it's important that this is added to the
1060 * WaitEventSet after the ExecAsyncConfigureWait() calls. Namely,
1061 * postgres_fdw calls "GetNumRegisteredWaitEvents(set) == 1" to check if
1062 * any other events are in the set. That's a poor design, it's
1063 * questionable for postgres_fdw to be doing that in the first place, but
1064 * we cannot change it now. The pattern has possibly been copied to other
1065 * extensions too.
1066 */
1067 AddWaitEventToSet(node->as_eventset, WL_LATCH_SET, PGINVALID_SOCKET,
1068 MyLatch, NULL);
1069
1070 /* Return at most EVENT_BUFFER_SIZE events in one call. */
1071 if (nevents > EVENT_BUFFER_SIZE)
1072 nevents = EVENT_BUFFER_SIZE;
1073
1074 /*
1075 * If the timeout is -1, wait until at least one event occurs. If the
1076 * timeout is 0, poll for events, but do not wait at all.
1077 */
1078 noccurred = WaitEventSetWait(node->as_eventset, timeout, occurred_event,
1079 nevents, WAIT_EVENT_APPEND_READY);
1080 FreeWaitEventSet(node->as_eventset);
1081 node->as_eventset = NULL;
1082 if (noccurred == 0)
1083 return;
1084
1085 /* Deliver notifications. */
1086 for (i = 0; i < noccurred; i++)
1087 {
1088 WaitEvent *w = &occurred_event[i];
1089
1090 /*
1091 * Each waiting subplan should have registered its wait event with
1092 * user_data pointing back to its AsyncRequest.
1093 */
1094 if ((w->events & WL_SOCKET_READABLE) != 0)
1095 {
1096 AsyncRequest *areq = (AsyncRequest *) w->user_data;
1097
1098 if (areq->callback_pending)
1099 {
1100 /*
1101 * Mark it as no longer needing a callback. We must do this
1102 * before dispatching the callback in case the callback resets
1103 * the flag.
1104 */
1105 areq->callback_pending = false;
1106
1107 /* Do the actual work. */
1108 ExecAsyncNotify(areq);
1109 }
1110 }
1111
1112 /* Handle standard interrupts */
1113 if ((w->events & WL_LATCH_SET) != 0)
1114 {
1115 ResetLatch(MyLatch);
1116 CHECK_FOR_INTERRUPTS();
1117 }
1118 }
1119 }
1120
1121 /* ----------------------------------------------------------------
1122 * ExecAppendAsyncReset
1123 *
1124 * Reset asynchronous requests made for async-capable subplans.
1125 * ----------------------------------------------------------------
1126 */
1127 static void
1128 ExecAppendAsyncReset(AppendState *node)
1129 {
1130 int i;
1131
1132 /* We should never be called when there are no async subplans. */
1133 Assert(node->as_nasyncplans > 0);
1134
1135 /*
1136 * Drain pending async requests if any. We force the as_syncdone flag to
1137 * be true so that ExecAppendAsyncEventWait() waits until at least one
1138 * event occurs.
1139 */
1140 node->as_syncdone = true;
1141 for (;;)
1142 {
1143 bool found = false;
1144
1145 /*
1146 * When called from ExecAppendAsyncEventWait(), postgres_fdw (and
1147 * possibly other FDWs) will skip configuration of events for pending
1148 * requests in some cases if as_needrequest isn't empty. To avoid
1149 * that, discard results we already have. Note that we need to do
1150 * this on every iteration, as the call to that function may produce
1151 * new results.
1152 */
1153 node->as_nasyncresults = 0;
1154 bms_free(node->as_needrequest);
1155 node->as_needrequest = NULL;
1156
1157 i = -1;
1158 while ((i = bms_next_member(node->as_asyncplans, i)) >= 0)
1159 {
1160 AsyncRequest *areq = node->as_asyncrequests[i];
1161
1162 if (areq->callback_pending)
1163 {
1164 found = true;
1165 break;
1166 }
1167 }
1168 if (!found)
1169 break;
1170
1171 CHECK_FOR_INTERRUPTS();
1172
1173 /* Wait or poll for async events. */
1174 ExecAppendAsyncEventWait(node);
1175 }
1176
1177 /* Reset async requests. */
1178 i = -1;
1179 while ((i = bms_next_member(node->as_asyncplans, i)) >= 0)
1180 {
1181 AsyncRequest *areq = node->as_asyncrequests[i];
1182
1183 Assert(!areq->callback_pending);
1184 areq->request_complete = false;
1185 areq->result = NULL;
1186 }
1187
1188 /* Reset state variables. */
1189 Assert(node->as_nasyncresults == 0);
1190 Assert(node->as_needrequest == NULL);
1191 node->as_nasyncremain = 0;
1192 }
1193
1194 /* ----------------------------------------------------------------
1195 * ExecAsyncAppendResponse
1196 *
1197 * Receive a response from an asynchronous request we made.
1198 * ----------------------------------------------------------------
1199 */
1200 void
1201 ExecAsyncAppendResponse(AsyncRequest *areq)
1202 {
1203 AppendState *node = (AppendState *) areq->requestor;
1204 TupleTableSlot *slot = areq->result;
1205
1206 /* The result should be a TupleTableSlot or NULL. */
1207 Assert(slot == NULL || IsA(slot, TupleTableSlot));
1208
1209 /* Nothing to do if the request is pending. */
1210 if (!areq->request_complete)
1211 {
1212 /* The request would have been pending for a callback. */
1213 Assert(areq->callback_pending);
1214 return;
1215 }
1216
1217 /* If the result is NULL or an empty slot, there's nothing more to do. */
1218 if (TupIsNull(slot))
1219 {
1220 /* The ending subplan wouldn't have been pending for a callback. */
1221 Assert(!areq->callback_pending);
1222 --node->as_nasyncremain;
1223 return;
1224 }
1225
1226 /* Save result so we can return it. */
1227 Assert(node->as_nasyncresults < node->as_nasyncplans);
1228 node->as_asyncresults[node->as_nasyncresults++] = slot;
1229
1230 /*
1231 * Mark the subplan that returned a result as ready for a new request. We
1232 * don't launch another one here immediately because it might complete.
1233 */
1234 node->as_needrequest = bms_add_member(node->as_needrequest,
1235 areq->request_index);
1236 }
1237
1238 /* ----------------------------------------------------------------
1239 * classify_matching_subplans
1240 *
1241 * Classify the node's as_valid_subplans into sync ones and
1242 * async ones, adjust it to contain sync ones only, and save
1243 * async ones in the node's as_valid_asyncplans.
1244 * ----------------------------------------------------------------
1245 */
1246 static void
1247 classify_matching_subplans(AppendState *node)
1248 {
1249 Bitmapset *valid_asyncplans;
1250
1251 Assert(node->as_valid_subplans_identified);
1252 Assert(node->as_valid_asyncplans == NULL);
1253
1254 /* Nothing to do if there are no valid subplans. */
1255 if (bms_is_empty(node->as_valid_subplans))
1256 {
1257 node->as_syncdone = true;
1258 node->as_nasyncremain = 0;
1259 return;
1260 }
1261
1262 /* Nothing to do if there are no valid async subplans. */
1263 if (!bms_overlap(node->as_valid_subplans, node->as_asyncplans))
1264 {
1265 node->as_nasyncremain = 0;
1266 return;
1267 }
1268
1269 /* Get valid async subplans. */
1270 valid_asyncplans = bms_intersect(node->as_asyncplans,
1271 node->as_valid_subplans);
1272
1273 /* Adjust the valid subplans to contain sync subplans only. */
1274 node->as_valid_subplans = bms_del_members(node->as_valid_subplans,
1275 valid_asyncplans);
1276
1277 /* Save valid async subplans. */
1278 node->as_valid_asyncplans = valid_asyncplans;
1279 }