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