Age Owner Branch data TLA 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 *
7578 tgl@sss.pgh.pa.us 111 :CBC 12483 : ExecInitAppend(Append *node, EState *estate, int eflags)
112 : : {
8759 113 : 12483 : 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 */
7578 125 [ - + ]: 12483 : Assert(!(eflags & EXEC_FLAG_MARK));
126 : :
127 : : /*
128 : : * create new AppendState for our append node
129 : : */
8759 130 : 12483 : appendstate->ps.plan = (Plan *) node;
131 : 12483 : appendstate->ps.state = estate;
3421 andres@anarazel.de 132 : 12483 : appendstate->ps.ExecProcNode = ExecAppend;
133 : :
134 : : /* Let choose_next_subplan_* function handle setting the first subplan */
3157 alvherre@alvh.no-ip. 135 : 12483 : appendstate->as_whichplan = INVALID_SUBPLAN_INDEX;
2068 efujita@postgresql.o 136 : 12483 : appendstate->as_syncdone = false;
137 : 12483 : appendstate->as_begun = false;
138 : :
139 : : /* If run-time partition pruning is enabled, then set that up now */
667 amitlan@postgresql.o 140 [ + + ]: 12483 : 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 : : */
666 149 : 526 : prunestate = ExecInitPartitionExecPruning(&appendstate->ps,
150 : 526 : list_length(node->appendplans),
151 : : node->part_prune_index,
152 : : node->apprelids,
153 : : &validsubplans);
3090 tgl@sss.pgh.pa.us 154 : 526 : appendstate->as_prune_state = prunestate;
1698 alvherre@alvh.no-ip. 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 : : */
2544 tgl@sss.pgh.pa.us 162 [ + + + + ]: 526 : if (!prunestate->do_exec_prune && nplans > 0)
163 : : {
3157 alvherre@alvh.no-ip. 164 : 198 : appendstate->as_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
1367 tgl@sss.pgh.pa.us 165 : 198 : appendstate->as_valid_subplans_identified = true;
166 : : }
167 : : }
168 : : else
169 : : {
3157 alvherre@alvh.no-ip. 170 : 11957 : 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 : : */
3043 176 [ - + ]: 11957 : Assert(nplans > 0);
3157 177 : 11957 : appendstate->as_valid_subplans = validsubplans =
178 : 11957 : bms_add_range(NULL, 0, nplans - 1);
1367 tgl@sss.pgh.pa.us 179 : 11957 : appendstate->as_valid_subplans_identified = true;
3157 alvherre@alvh.no-ip. 180 : 11957 : appendstate->as_prune_state = NULL;
181 : : }
182 : :
103 michael@paquier.xyz 183 :GNC 12483 : 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 : : */
2686 drowley@postgresql.o 191 :CBC 12483 : j = 0;
2068 efujita@postgresql.o 192 : 12483 : asyncplans = NULL;
193 : 12483 : nasyncplans = 0;
3147 alvherre@alvh.no-ip. 194 : 12483 : firstvalid = nplans;
2686 drowley@postgresql.o 195 : 12483 : i = -1;
196 [ + + ]: 50762 : while ((i = bms_next_member(validsubplans, i)) >= 0)
197 : : {
198 : 38279 : 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 : : */
2068 efujita@postgresql.o 205 [ + + + - ]: 38279 : 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 : : */
2686 drowley@postgresql.o 214 [ + + + + ]: 38279 : if (i >= node->first_partial_plan && j < firstvalid)
215 : 316 : firstvalid = j;
216 : :
217 : 38279 : appendplanstates[j++] = ExecInitNode(initNode, estate, eflags);
218 : : }
219 : :
3147 alvherre@alvh.no-ip. 220 : 12483 : appendstate->as_first_partial_plan = firstvalid;
3157 221 : 12483 : appendstate->appendplans = appendplanstates;
222 : 12483 : 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 : : */
709 tgl@sss.pgh.pa.us 233 : 12483 : appendops = ExecGetCommonSlotOps(appendplanstates, j);
234 [ + + ]: 12483 : if (appendops != NULL)
235 : : {
236 : 11805 : ExecInitResultTupleSlotTL(&appendstate->ps, appendops);
237 : : }
238 : : else
239 : : {
240 : 678 : ExecInitResultTupleSlotTL(&appendstate->ps, &TTSOpsVirtual);
241 : : /* show that the output slot type is not fixed */
242 : 678 : appendstate->ps.resultopsset = true;
243 : 678 : appendstate->ps.resultopsfixed = false;
244 : : }
245 : :
246 : : /* Initialize async state */
2068 efujita@postgresql.o 247 : 12483 : appendstate->as_asyncplans = asyncplans;
248 : 12483 : appendstate->as_nasyncplans = nasyncplans;
249 : 12483 : appendstate->as_asyncrequests = NULL;
2000 250 : 12483 : appendstate->as_asyncresults = NULL;
251 : 12483 : appendstate->as_nasyncresults = 0;
252 : 12483 : appendstate->as_nasyncremain = 0;
2068 253 : 12483 : appendstate->as_needrequest = NULL;
254 : 12483 : appendstate->as_eventset = NULL;
2000 255 : 12483 : appendstate->as_valid_asyncplans = NULL;
256 : :
2068 257 [ + + ]: 12483 : if (nasyncplans > 0)
258 : : {
103 michael@paquier.xyz 259 :GNC 51 : appendstate->as_asyncrequests = palloc0_array(AsyncRequest *, nplans);
260 : :
2068 efujita@postgresql.o 261 :CBC 51 : i = -1;
262 [ + + ]: 156 : while ((i = bms_next_member(asyncplans, i)) >= 0)
263 : : {
264 : : AsyncRequest *areq;
265 : :
353 michael@paquier.xyz 266 : 105 : areq = palloc_object(AsyncRequest);
2068 efujita@postgresql.o 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 : :
103 michael@paquier.xyz 277 :GNC 51 : appendstate->as_asyncresults = palloc0_array(TupleTableSlot *, nasyncplans);
278 : :
1367 tgl@sss.pgh.pa.us 279 [ + + ]:CBC 51 : if (appendstate->as_valid_subplans_identified)
2000 efujita@postgresql.o 280 : 44 : classify_matching_subplans(appendstate);
281 : : }
282 : :
283 : : /*
284 : : * Miscellaneous initialization
285 : : */
286 : :
3157 alvherre@alvh.no-ip. 287 : 12483 : appendstate->ps.ps_ProjInfo = NULL;
288 : :
289 : : /* For parallel query, this will be overridden later. */
3280 rhaas@postgresql.org 290 : 12483 : appendstate->choose_next_subplan = choose_next_subplan_locally;
291 : :
8759 tgl@sss.pgh.pa.us 292 : 12483 : return appendstate;
293 : : }
294 : :
295 : : /* ----------------------------------------------------------------
296 : : * ExecAppend
297 : : *
298 : : * Handles iteration over multiple subplans.
299 : : * ----------------------------------------------------------------
300 : : */
301 : : static TupleTableSlot *
3421 andres@anarazel.de 302 : 1699757 : ExecAppend(PlanState *pstate)
303 : : {
304 : 1699757 : 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 : : */
2068 efujita@postgresql.o 311 [ + + ]: 1699757 : if (!node->as_begun)
312 : : {
313 [ - + ]: 21075 : Assert(node->as_whichplan == INVALID_SUBPLAN_INDEX);
314 [ - + ]: 21075 : Assert(!node->as_syncdone);
315 : :
316 : : /* Nothing to do if there are no subplans */
2544 tgl@sss.pgh.pa.us 317 [ + + ]: 21075 : if (node->as_nplans == 0)
318 : 40 : return ExecClearTuple(node->ps.ps_ResultTupleSlot);
319 : :
320 : : /* If there are any async subplans, begin executing them. */
2068 efujita@postgresql.o 321 [ + + ]: 21035 : 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 [ + + + + ]: 21035 : if (!node->choose_next_subplan(node) && node->as_nasyncremain == 0)
3157 alvherre@alvh.no-ip. 329 : 2109 : return ExecClearTuple(node->ps.ps_ResultTupleSlot);
330 : :
2068 efujita@postgresql.o 331 [ + + + - : 18926 : Assert(node->as_syncdone ||
- + ]
332 : : (node->as_whichplan >= 0 &&
333 : : node->as_whichplan < node->as_nplans));
334 : :
335 : : /* And we're initialized. */
336 : 18926 : node->as_begun = true;
337 : : }
338 : :
339 : : for (;;)
10674 bruce@momjian.us 340 : 28196 : {
341 : : PlanState *subnode;
342 : :
3413 andres@anarazel.de 343 [ + + ]: 1725804 : CHECK_FOR_INTERRUPTS();
344 : :
345 : : /*
346 : : * try to get a tuple from an async subplan if any
347 : : */
2068 efujita@postgresql.o 348 [ + + - + ]: 1725804 : if (node->as_syncdone || !bms_is_empty(node->as_needrequest))
349 : : {
350 [ + - ]: 6141 : if (ExecAppendAsyncGetNext(node, &result))
351 : 6140 : return result;
2068 efujita@postgresql.o 352 [ # # ]:UBC 0 : Assert(!node->as_syncdone);
353 [ # # ]: 0 : Assert(bms_is_empty(node->as_needrequest));
354 : : }
355 : :
356 : : /*
357 : : * figure out which sync subplan we are currently processing
358 : : */
3280 rhaas@postgresql.org 359 [ + - - + ]:CBC 1719663 : Assert(node->as_whichplan >= 0 && node->as_whichplan < node->as_nplans);
7860 tgl@sss.pgh.pa.us 360 : 1719663 : subnode = node->appendplans[node->as_whichplan];
361 : :
362 : : /*
363 : : * get a tuple from the subplan
364 : : */
365 : 1719663 : result = ExecProcNode(subnode);
366 : :
367 [ + + + + ]: 1719617 : 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 : 1672945 : 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 : : */
2068 efujita@postgresql.o 382 [ + + ]: 46672 : if (node->as_nasyncremain > 0)
383 : 17 : ExecAppendAsyncEventWait(node);
384 : :
385 : : /* choose new sync subplan; if no sync/async subplans, we're done */
386 [ + + + + ]: 46672 : if (!node->choose_next_subplan(node) && node->as_nasyncremain == 0)
7860 tgl@sss.pgh.pa.us 387 : 18476 : 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
8759 400 : 12262 : ExecEndAppend(AppendState *node)
401 : : {
402 : : PlanState **appendplans;
403 : : int nplans;
404 : : int i;
405 : :
406 : : /*
407 : : * get information from the node
408 : : */
10363 bruce@momjian.us 409 : 12262 : appendplans = node->appendplans;
8759 tgl@sss.pgh.pa.us 410 : 12262 : nplans = node->as_nplans;
411 : :
412 : : /*
413 : : * shut down each of the subscans
414 : : */
10674 bruce@momjian.us 415 [ + + ]: 50025 : for (i = 0; i < nplans; i++)
6258 tgl@sss.pgh.pa.us 416 : 37763 : ExecEndNode(appendplans[i]);
10674 bruce@momjian.us 417 : 12262 : }
418 : :
419 : : void
5983 tgl@sss.pgh.pa.us 420 : 12685 : ExecReScanAppend(AppendState *node)
421 : : {
2068 efujita@postgresql.o 422 : 12685 : int nasyncplans = node->as_nasyncplans;
423 : : int i;
424 : :
425 : : /* If there are any async subplans, reset async requests made for them. */
113 426 [ + + ]: 12685 : 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 : : */
3157 alvherre@alvh.no-ip. 434 [ + + + + ]: 14893 : if (node->as_prune_state &&
435 : 2208 : bms_overlap(node->ps.chgParam,
3093 tgl@sss.pgh.pa.us 436 : 2208 : node->as_prune_state->execparamids))
437 : : {
1367 438 : 2206 : node->as_valid_subplans_identified = false;
3157 alvherre@alvh.no-ip. 439 : 2206 : bms_free(node->as_valid_subplans);
440 : 2206 : node->as_valid_subplans = NULL;
1367 tgl@sss.pgh.pa.us 441 : 2206 : bms_free(node->as_valid_asyncplans);
442 : 2206 : node->as_valid_asyncplans = NULL;
443 : : }
444 : :
6258 445 [ + + ]: 53816 : for (i = 0; i < node->as_nplans; i++)
446 : : {
8517 bruce@momjian.us 447 : 41131 : 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 : : */
8693 tgl@sss.pgh.pa.us 453 [ + + ]: 41131 : if (node->ps.chgParam != NULL)
454 : 38891 : 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 : : */
5983 460 [ + + ]: 41131 : if (subnode->chgParam == NULL)
461 : 10110 : ExecReScan(subnode);
462 : : }
463 : :
464 : : /* Let choose_next_subplan_* function handle setting the first subplan */
3157 alvherre@alvh.no-ip. 465 : 12685 : node->as_whichplan = INVALID_SUBPLAN_INDEX;
2068 efujita@postgresql.o 466 : 12685 : node->as_syncdone = false;
467 : 12685 : node->as_begun = false;
3280 rhaas@postgresql.org 468 : 12685 : }
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
3280 rhaas@postgresql.org 523 :UBC 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
3280 rhaas@postgresql.org 539 :CBC 219 : ExecAppendInitializeWorker(AppendState *node, ParallelWorkerContext *pwcxt)
540 : : {
541 : 219 : node->as_pstate = shm_toc_lookup(pwcxt->toc, node->ps.plan->plan_node_id, false);
542 : 219 : node->choose_next_subplan = choose_next_subplan_for_worker;
543 : 219 : }
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 : 66501 : choose_next_subplan_locally(AppendState *node)
554 : : {
555 : 66501 : int whichplan = node->as_whichplan;
556 : : int nextplan;
557 : :
558 : : /* We should never be called when there are no subplans */
2544 tgl@sss.pgh.pa.us 559 [ - + ]: 66501 : Assert(node->as_nplans > 0);
560 : :
561 : : /* Nothing to do if syncdone */
2068 efujita@postgresql.o 562 [ + + ]: 66501 : 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 : : */
3157 alvherre@alvh.no-ip. 572 [ + + ]: 66479 : if (whichplan == INVALID_SUBPLAN_INDEX)
573 : : {
2068 efujita@postgresql.o 574 [ + + ]: 20734 : if (node->as_nasyncplans > 0)
575 : : {
576 : : /* We'd have filled as_valid_subplans already */
1367 tgl@sss.pgh.pa.us 577 [ - + ]: 19 : Assert(node->as_valid_subplans_identified);
578 : : }
579 [ + + ]: 20715 : else if (!node->as_valid_subplans_identified)
580 : : {
3157 alvherre@alvh.no-ip. 581 : 2279 : node->as_valid_subplans =
659 amitlan@postgresql.o 582 : 2279 : ExecFindMatchingSubPlans(node->as_prune_state, false, NULL);
1367 tgl@sss.pgh.pa.us 583 : 2279 : node->as_valid_subplans_identified = true;
584 : : }
585 : :
3157 alvherre@alvh.no-ip. 586 : 20734 : whichplan = -1;
587 : : }
588 : :
589 : : /* Ensure whichplan is within the expected range */
590 [ + - - + ]: 66479 : Assert(whichplan >= -1 && whichplan <= node->as_nplans);
591 : :
592 [ + + ]: 66479 : if (ScanDirectionIsForward(node->ps.state->es_direction))
593 : 66467 : nextplan = bms_next_member(node->as_valid_subplans, whichplan);
594 : : else
595 : 12 : nextplan = bms_prev_member(node->as_valid_subplans, whichplan);
596 : :
597 [ + + ]: 66479 : if (nextplan < 0)
598 : : {
599 : : /* Set as_syncdone if in async mode */
2068 efujita@postgresql.o 600 [ + + ]: 20322 : if (node->as_nasyncplans > 0)
601 : 17 : node->as_syncdone = true;
3157 alvherre@alvh.no-ip. 602 : 20322 : return false;
603 : : }
604 : :
605 : 46157 : node->as_whichplan = nextplan;
606 : :
3280 rhaas@postgresql.org 607 : 46157 : 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 : 867 : choose_next_subplan_for_leader(AppendState *node)
620 : : {
621 : 867 : ParallelAppendState *pstate = node->as_pstate;
622 : :
623 : : /* Backward scan is not supported by parallel-aware plans */
624 [ - + ]: 867 : Assert(ScanDirectionIsForward(node->ps.state->es_direction));
625 : :
626 : : /* We should never be called when there are no subplans */
2544 tgl@sss.pgh.pa.us 627 [ - + ]: 867 : Assert(node->as_nplans > 0);
628 : :
3280 rhaas@postgresql.org 629 : 867 : LWLockAcquire(&pstate->pa_lock, LW_EXCLUSIVE);
630 : :
631 [ + + ]: 867 : if (node->as_whichplan != INVALID_SUBPLAN_INDEX)
632 : : {
633 : : /* Mark just-completed subplan as finished. */
634 : 783 : 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 : : */
1367 tgl@sss.pgh.pa.us 646 [ + + ]: 84 : if (!node->as_valid_subplans_identified)
647 : : {
3157 alvherre@alvh.no-ip. 648 : 16 : node->as_valid_subplans =
659 amitlan@postgresql.o 649 : 16 : ExecFindMatchingSubPlans(node->as_prune_state, false, NULL);
1367 tgl@sss.pgh.pa.us 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 : : */
3157 alvherre@alvh.no-ip. 656 : 16 : mark_invalid_subplans_as_finished(node);
657 : : }
658 : : }
659 : :
660 : : /* Loop until we find a subplan to execute. */
3280 rhaas@postgresql.org 661 [ + + ]: 1615 : 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. */
3147 alvherre@alvh.no-ip. 679 [ + + ]: 783 : if (node->as_whichplan < node->as_first_partial_plan)
3280 rhaas@postgresql.org 680 : 61 : node->as_pstate->pa_finished[node->as_whichplan] = true;
681 : :
682 : 783 : LWLockRelease(&pstate->pa_lock);
683 : :
684 : 783 : 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 : 339 : choose_next_subplan_for_worker(AppendState *node)
702 : : {
703 : 339 : ParallelAppendState *pstate = node->as_pstate;
704 : :
705 : : /* Backward scan is not supported by parallel-aware plans */
706 [ - + ]: 339 : Assert(ScanDirectionIsForward(node->ps.state->es_direction));
707 : :
708 : : /* We should never be called when there are no subplans */
2544 tgl@sss.pgh.pa.us 709 [ - + ]: 339 : Assert(node->as_nplans > 0);
710 : :
3280 rhaas@postgresql.org 711 : 339 : LWLockAcquire(&pstate->pa_lock, LW_EXCLUSIVE);
712 : :
713 : : /* Mark just-completed subplan as finished. */
714 [ + + ]: 339 : if (node->as_whichplan != INVALID_SUBPLAN_INDEX)
715 : 144 : 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 : : */
1367 tgl@sss.pgh.pa.us 722 [ + + ]: 195 : else if (!node->as_valid_subplans_identified)
723 : : {
3157 alvherre@alvh.no-ip. 724 : 16 : node->as_valid_subplans =
659 amitlan@postgresql.o 725 : 16 : ExecFindMatchingSubPlans(node->as_prune_state, false, NULL);
1367 tgl@sss.pgh.pa.us 726 : 16 : node->as_valid_subplans_identified = true;
727 : :
3157 alvherre@alvh.no-ip. 728 : 16 : mark_invalid_subplans_as_finished(node);
729 : : }
730 : :
731 : : /* If all the plans are already done, we have nothing to do */
3280 rhaas@postgresql.org 732 [ + + ]: 339 : if (pstate->pa_next_plan == INVALID_SUBPLAN_INDEX)
733 : : {
734 : 171 : LWLockRelease(&pstate->pa_lock);
735 : 171 : return false;
736 : : }
737 : :
738 : : /* Save the plan from which we are starting the search. */
3215 739 : 168 : node->as_whichplan = pstate->pa_next_plan;
740 : :
741 : : /* Loop until we find a valid subplan to execute. */
3280 742 [ + + ]: 289 : while (pstate->pa_finished[pstate->pa_next_plan])
743 : : {
744 : : int nextplan;
745 : :
3155 alvherre@alvh.no-ip. 746 : 145 : nextplan = bms_next_member(node->as_valid_subplans,
747 : : pstate->pa_next_plan);
748 [ + + ]: 145 : if (nextplan >= 0)
749 : : {
750 : : /* Advance to the next valid plan. */
751 : 99 : pstate->pa_next_plan = nextplan;
752 : : }
3147 753 [ + + ]: 46 : 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 : : */
3155 759 : 39 : nextplan = bms_next_member(node->as_valid_subplans,
3147 760 : 39 : node->as_first_partial_plan - 1);
3155 761 : 39 : pstate->pa_next_plan =
762 [ - + ]: 39 : 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 : : */
3280 rhaas@postgresql.org 770 : 7 : pstate->pa_next_plan = node->as_whichplan;
771 : : }
772 : :
773 [ + + ]: 145 : if (pstate->pa_next_plan == node->as_whichplan)
774 : : {
775 : : /* We've tried everything! */
776 : 24 : pstate->pa_next_plan = INVALID_SUBPLAN_INDEX;
777 : 24 : LWLockRelease(&pstate->pa_lock);
778 : 24 : return false;
779 : : }
780 : : }
781 : :
782 : : /* Pick the plan we found, and advance pa_next_plan one more time. */
3155 alvherre@alvh.no-ip. 783 : 144 : node->as_whichplan = pstate->pa_next_plan;
784 : 144 : 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 [ + + ]: 144 : if (pstate->pa_next_plan < 0)
792 : : {
793 : 17 : int nextplan = bms_next_member(node->as_valid_subplans,
3147 794 : 17 : node->as_first_partial_plan - 1);
795 : :
3155 796 [ + - ]: 17 : if (nextplan >= 0)
797 : 17 : 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 : : */
3279 rhaas@postgresql.org 805 :UBC 0 : pstate->pa_next_plan = INVALID_SUBPLAN_INDEX;
806 : : }
807 : : }
808 : :
809 : : /* If non-partial, immediately mark as finished. */
3147 alvherre@alvh.no-ip. 810 [ + + ]:CBC 144 : if (node->as_whichplan < node->as_first_partial_plan)
3280 rhaas@postgresql.org 811 : 31 : node->as_pstate->pa_finished[node->as_whichplan] = true;
812 : :
813 : 144 : LWLockRelease(&pstate->pa_lock);
814 : :
815 : 144 : 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
3157 alvherre@alvh.no-ip. 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 [ - + ]: 32 : Assert(node->as_pstate);
833 : :
834 : : /* Shouldn't have been called when run-time pruning is not enabled */
835 [ - + ]: 32 : 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)
3157 alvherre@alvh.no-ip. 839 :UBC 0 : return;
840 : :
841 : : /* Mark all non-valid plans as finished */
3157 alvherre@alvh.no-ip. 842 [ + + ]:CBC 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
2068 efujita@postgresql.o 861 : 41 : ExecAppendAsyncBegin(AppendState *node)
862 : : {
863 : : int i;
864 : :
865 : : /* Backward scan is not supported by async-aware Appends. */
866 [ - + ]: 41 : Assert(ScanDirectionIsForward(node->ps.state->es_direction));
867 : :
868 : : /* We should never be called when there are no subplans */
2000 869 [ - + ]: 41 : Assert(node->as_nplans > 0);
870 : :
871 : : /* We should never be called when there are no async subplans. */
2068 872 [ - + ]: 41 : Assert(node->as_nasyncplans > 0);
873 : :
874 : : /* If we've yet to determine the valid subplans then do so now. */
1367 tgl@sss.pgh.pa.us 875 [ + + ]: 41 : if (!node->as_valid_subplans_identified)
876 : : {
2068 efujita@postgresql.o 877 : 6 : node->as_valid_subplans =
659 amitlan@postgresql.o 878 : 6 : ExecFindMatchingSubPlans(node->as_prune_state, false, NULL);
1367 tgl@sss.pgh.pa.us 879 : 6 : node->as_valid_subplans_identified = true;
880 : :
2000 efujita@postgresql.o 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. */
2068 889 [ - + ]: 41 : if (node->as_nasyncremain == 0)
2068 efujita@postgresql.o 890 :UBC 0 : return;
891 : :
892 : : /* Make a request for each of the valid async subplans. */
2068 efujita@postgresql.o 893 :CBC 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 [ - + ]: 80 : Assert(areq->request_index == i);
899 [ - + ]: 80 : 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 : 6141 : ExecAppendAsyncGetNext(AppendState *node, TupleTableSlot **result)
914 : : {
915 : 6141 : *result = NULL;
916 : :
917 : : /* We should never be called when there are no valid async subplans. */
918 [ - + ]: 6141 : Assert(node->as_nasyncremain > 0);
919 : :
920 : : /* Request a tuple asynchronously. */
921 [ + + ]: 6141 : if (ExecAppendAsyncRequest(node, result))
922 : 6042 : return true;
923 : :
924 [ + + ]: 140 : while (node->as_nasyncremain > 0)
925 : : {
926 [ - + ]: 110 : CHECK_FOR_INTERRUPTS();
927 : :
928 : : /* Wait or poll for async events. */
929 : 110 : ExecAppendAsyncEventWait(node);
930 : :
931 : : /* Request a tuple asynchronously. */
932 [ + + ]: 109 : if (ExecAppendAsyncRequest(node, result))
933 : 68 : return true;
934 : :
935 : : /* Break from loop if there's any sync subplan that isn't complete. */
936 [ - + ]: 41 : if (!node->as_syncdone)
2068 efujita@postgresql.o 937 :UBC 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 : : */
2068 efujita@postgresql.o 945 [ + - ]:CBC 30 : if (node->as_syncdone)
946 : : {
947 [ - + ]: 30 : Assert(node->as_nasyncremain == 0);
948 : 30 : *result = ExecClearTuple(node->ps.ps_ResultTupleSlot);
949 : 30 : return true;
950 : : }
951 : :
2068 efujita@postgresql.o 952 :UBC 0 : return false;
953 : : }
954 : :
955 : : /* ----------------------------------------------------------------
956 : : * ExecAppendAsyncRequest
957 : : *
958 : : * Request a tuple asynchronously.
959 : : * ----------------------------------------------------------------
960 : : */
961 : : static bool
2068 efujita@postgresql.o 962 :CBC 6250 : 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 [ + + ]: 6250 : if (bms_is_empty(node->as_needrequest))
969 : : {
2045 970 [ - + ]: 65 : Assert(node->as_nasyncresults == 0);
2068 971 : 65 : 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 [ + + ]: 6185 : if (node->as_nasyncresults > 0)
979 : : {
980 : 1302 : --node->as_nasyncresults;
981 : 1302 : *result = node->as_asyncresults[node->as_nasyncresults];
982 : 1302 : return true;
983 : : }
984 : :
985 : : /* Make a new request for each of the async subplans that need it. */
986 : 4883 : needrequest = node->as_needrequest;
987 : 4883 : node->as_needrequest = NULL;
988 : 4883 : i = -1;
989 [ + + ]: 10986 : 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 : 4883 : bms_free(needrequest);
997 : :
998 : : /* Return one of the asynchronously-generated results if any. */
999 [ + + ]: 4883 : if (node->as_nasyncresults > 0)
1000 : : {
1001 : 4808 : --node->as_nasyncresults;
1002 : 4808 : *result = node->as_asyncresults[node->as_nasyncresults];
1003 : 4808 : return true;
1004 : : }
1005 : :
1006 : 75 : 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 : 130 : ExecAppendAsyncEventWait(AppendState *node)
1021 : : {
626 heikki.linnakangas@i 1022 : 130 : int nevents = node->as_nasyncplans + 2;
2068 efujita@postgresql.o 1023 [ + + ]: 130 : long timeout = node->as_syncdone ? -1 : 0;
1024 : :
1025 : : /* We should never be called when there are no valid async subplans. */
1026 [ - + ]: 130 : Assert(node->as_nasyncremain > 0);
1027 : :
1101 heikki.linnakangas@i 1028 [ - + ]: 130 : Assert(node->as_eventset == NULL);
1029 : 130 : node->as_eventset = CreateWaitEventSet(CurrentResourceOwner, nevents);
11 efujita@postgresql.o 1030 :GNC 130 : ExecAsyncEventWait(node->as_asyncplans,
1031 : : node->as_asyncrequests,
1032 : 130 : node->as_eventset,
1033 : : nevents,
1034 : : timeout,
1035 : : WAIT_EVENT_APPEND_READY);
2068 efujita@postgresql.o 1036 :CBC 129 : FreeWaitEventSet(node->as_eventset);
1037 : 129 : node->as_eventset = NULL;
2068 efujita@postgresql.o 1038 :GIC 129 : }
1039 : :
1040 : : /* ----------------------------------------------------------------
1041 : : * ExecAppendAsyncReset
1042 : : *
1043 : : * Reset asynchronous requests made for async-capable subplans.
1044 : : * ----------------------------------------------------------------
1045 : : */
1046 : : static void
113 efujita@postgresql.o 1047 :CBC 21 : ExecAppendAsyncReset(AppendState *node)
1048 : : {
1049 : : int i;
1050 : :
1051 : : /* We should never be called when there are no async subplans. */
1052 [ - + ]: 21 : 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 [ + + ]: 73 : while ((i = bms_next_member(node->as_asyncplans, i)) >= 0)
1078 : : {
1079 : 52 : AsyncRequest *areq = node->as_asyncrequests[i];
1080 : :
1081 [ + + ]: 52 : 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 [ - + ]: 46 : Assert(!areq->callback_pending);
1103 : 46 : areq->request_complete = false;
1104 : 46 : areq->result = NULL;
1105 : : }
1106 : :
1107 : : /* Reset state variables. */
1108 [ - + ]: 21 : Assert(node->as_nasyncresults == 0);
1109 [ - + ]: 21 : 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
2068 1120 : 6345 : ExecAsyncAppendResponse(AsyncRequest *areq)
1121 : : {
1122 : 6345 : AppendState *node = (AppendState *) areq->requestor;
1123 : 6345 : TupleTableSlot *slot = areq->result;
1124 : :
1125 : : /* The result should be a TupleTableSlot or NULL. */
1126 [ + + - + ]: 6345 : Assert(slot == NULL || IsA(slot, TupleTableSlot));
1127 : :
1128 : : /* Nothing to do if the request is pending. */
1129 [ + + ]: 6345 : if (!areq->request_complete)
1130 : : {
1131 : : /* The request would have been pending for a callback. */
1132 [ - + ]: 168 : 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 [ + + - + ]: 6177 : if (TupIsNull(slot))
1138 : : {
1139 : : /* The ending subplan wouldn't have been pending for a callback. */
1140 [ - + ]: 61 : Assert(!areq->callback_pending);
1141 : 61 : --node->as_nasyncremain;
1142 : 61 : return;
1143 : : }
1144 : :
1145 : : /* Save result so we can return it. */
1146 [ - + ]: 6116 : Assert(node->as_nasyncresults < node->as_nasyncplans);
1147 : 6116 : 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 : 6116 : 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 : :
1367 tgl@sss.pgh.pa.us 1170 [ - + ]: 50 : Assert(node->as_valid_subplans_identified);
2068 efujita@postgresql.o 1171 [ - + ]: 50 : 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 : : {
2068 efujita@postgresql.o 1176 :UBC 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. */
2068 efujita@postgresql.o 1182 [ - + ]:CBC 50 : if (!bms_overlap(node->as_valid_subplans, node->as_asyncplans))
1183 : : {
2068 efujita@postgresql.o 1184 :UBC 0 : node->as_nasyncremain = 0;
1185 : 0 : return;
1186 : : }
1187 : :
1188 : : /* Get valid async subplans. */
1367 tgl@sss.pgh.pa.us 1189 :CBC 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. */
2068 efujita@postgresql.o 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 : : }
|