Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * nodeGather.c
4 : : * Support routines for scanning a plan via multiple workers.
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : * A Gather executor launches parallel workers to run multiple copies of a
10 : : * plan. It can also run the plan itself, if the workers are not available
11 : : * or have not started up yet. It then merges all of the results it produces
12 : : * and the results from the workers into a single output stream. Therefore,
13 : : * it will normally be used with a plan where running multiple copies of the
14 : : * same plan does not produce duplicate output, such as parallel-aware
15 : : * SeqScan.
16 : : *
17 : : * Alternatively, a Gather node can be configured to use just one worker
18 : : * and the single-copy flag can be set. In this case, the Gather node will
19 : : * run the plan in one worker and will not execute the plan itself. In
20 : : * this case, it simply returns whatever tuples were returned by the worker.
21 : : * If a worker cannot be obtained, then it will run the plan itself and
22 : : * return the results. Therefore, a plan used with a single-copy Gather
23 : : * node need not be parallel-aware.
24 : : *
25 : : * IDENTIFICATION
26 : : * src/backend/executor/nodeGather.c
27 : : *
28 : : *-------------------------------------------------------------------------
29 : : */
30 : :
31 : : #include "postgres.h"
32 : :
33 : : #include "executor/execParallel.h"
34 : : #include "executor/executor.h"
35 : : #include "executor/nodeGather.h"
36 : : #include "executor/tqueue.h"
37 : : #include "miscadmin.h"
38 : : #include "optimizer/optimizer.h"
39 : : #include "storage/latch.h"
40 : : #include "utils/wait_event.h"
41 : :
42 : :
43 : : static TupleTableSlot *ExecGather(PlanState *pstate);
44 : : static TupleTableSlot *gather_getnext(GatherState *gatherstate);
45 : : static MinimalTuple gather_readnext(GatherState *gatherstate);
46 : : static void ExecShutdownGatherWorkers(GatherState *node);
47 : :
48 : :
49 : : /* ----------------------------------------------------------------
50 : : * ExecInitGather
51 : : * ----------------------------------------------------------------
52 : : */
53 : : GatherState *
4054 rhaas@postgresql.org 54 :CBC 797 : ExecInitGather(Gather *node, EState *estate, int eflags)
55 : : {
56 : : GatherState *gatherstate;
57 : : Plan *outerNode;
58 : : TupleDesc tupDesc;
59 : :
60 : : /* Gather node doesn't have innerPlan node. */
61 [ - + ]: 797 : Assert(innerPlan(node) == NULL);
62 : :
63 : : /*
64 : : * create state structure
65 : : */
66 : 797 : gatherstate = makeNode(GatherState);
67 : 797 : gatherstate->ps.plan = (Plan *) node;
68 : 797 : gatherstate->ps.state = estate;
3398 andres@anarazel.de 69 : 797 : gatherstate->ps.ExecProcNode = ExecGather;
70 : :
3354 tgl@sss.pgh.pa.us 71 : 797 : gatherstate->initialized = false;
3277 rhaas@postgresql.org 72 : 797 : gatherstate->need_to_scan_locally =
73 [ + + + + ]: 797 : !node->single_copy && parallel_leader_participation;
3355 74 : 797 : gatherstate->tuples_needed = -1;
75 : :
76 : : /*
77 : : * Miscellaneous initialization
78 : : *
79 : : * create expression context for node
80 : : */
4054 81 : 797 : ExecAssignExprContext(estate, &gatherstate->ps);
82 : :
83 : : /*
84 : : * now initialize outer plan
85 : : */
4026 86 : 797 : outerNode = outerPlan(node);
87 : 797 : outerPlanState(gatherstate) = ExecInitNode(outerNode, estate, eflags);
3184 andres@anarazel.de 88 : 797 : tupDesc = ExecGetResultType(outerPlanState(gatherstate));
89 : :
90 : : /*
91 : : * Leader may access ExecProcNode result directly (if
92 : : * need_to_scan_locally), or from workers via tuple queue. So we can't
93 : : * trivially rely on the slot type being fixed for expressions evaluated
94 : : * within this node.
95 : : */
2912 96 : 797 : gatherstate->ps.outeropsset = true;
97 : 797 : gatherstate->ps.outeropsfixed = false;
98 : :
99 : : /*
100 : : * Initialize result type and projection.
101 : : */
2918 102 : 797 : ExecInitResultTypeTL(&gatherstate->ps);
3184 103 : 797 : ExecConditionalAssignProjectionInfo(&gatherstate->ps, tupDesc, OUTER_VAR);
104 : :
105 : : /*
106 : : * Without projections result slot type is not trivially known, see
107 : : * comment above.
108 : : */
2912 109 [ + + ]: 797 : if (gatherstate->ps.ps_ProjInfo == NULL)
110 : : {
111 : 745 : gatherstate->ps.resultopsset = true;
112 : 745 : gatherstate->ps.resultopsfixed = false;
113 : : }
114 : :
115 : : /*
116 : : * Initialize funnel slot to same tuple descriptor as outer plan.
117 : : */
118 : 797 : gatherstate->funnel_slot = ExecInitExtraTupleSlot(estate, tupDesc,
119 : : &TTSOpsMinimalTuple);
120 : :
121 : : /*
122 : : * Gather doesn't support checking a qual (it's always more efficient to
123 : : * do it in the child node).
124 : : */
3184 125 [ - + ]: 797 : Assert(!node->plan.qual);
126 : :
4054 rhaas@postgresql.org 127 : 797 : return gatherstate;
128 : : }
129 : :
130 : : /* ----------------------------------------------------------------
131 : : * ExecGather(node)
132 : : *
133 : : * Scans the relation via multiple workers and returns
134 : : * the next qualifying tuple.
135 : : * ----------------------------------------------------------------
136 : : */
137 : : static TupleTableSlot *
3398 andres@anarazel.de 138 : 2145139 : ExecGather(PlanState *pstate)
139 : : {
140 : 2145139 : GatherState *node = castNode(GatherState, pstate);
141 : : TupleTableSlot *slot;
142 : : ExprContext *econtext;
143 : :
3390 144 [ + + ]: 2145139 : CHECK_FOR_INTERRUPTS();
145 : :
146 : : /*
147 : : * Initialize the parallel context and workers on first execution. We do
148 : : * this on first execution rather than during node initialization, as it
149 : : * needs to allocate a large dynamic segment, so it is better to do it
150 : : * only if it is really needed.
151 : : */
4038 rhaas@postgresql.org 152 [ + + ]: 2145139 : if (!node->initialized)
153 : : {
4054 154 : 608 : EState *estate = node->ps.state;
4038 155 : 608 : Gather *gather = (Gather *) node->ps.plan;
156 : :
157 : : /*
158 : : * Sometimes we might have to run without parallelism; but if parallel
159 : : * mode is active then we can try to fire up some workers.
160 : : */
3296 161 [ + - + + ]: 608 : if (gather->num_workers > 0 && estate->es_use_parallel_mode)
162 : : {
163 : : ParallelContext *pcxt;
164 : :
165 : : /* Initialize, or re-initialize, shared state needed by workers. */
4024 166 [ + + ]: 580 : if (!node->pei)
1582 tgl@sss.pgh.pa.us 167 : 420 : node->pei = ExecInitParallelPlan(outerPlanState(node),
168 : : estate,
169 : : gather->initParam,
170 : : gather->num_workers,
171 : : node->tuples_needed);
172 : : else
173 : 160 : ExecParallelReinitialize(outerPlanState(node),
3276 rhaas@postgresql.org 174 : 160 : node->pei,
175 : : gather->initParam);
176 : :
177 : : /*
178 : : * Register backend workers. We might not get as many as we
179 : : * requested, or indeed any at all.
180 : : */
4017 181 : 580 : pcxt = node->pei->pcxt;
182 : 580 : LaunchParallelWorkers(pcxt);
183 : : /* We save # workers launched for the benefit of EXPLAIN */
3856 184 : 580 : node->nworkers_launched = pcxt->nworkers_launched;
185 : :
186 : : /*
187 : : * Count number of workers originally wanted and actually
188 : : * launched.
189 : : */
757 michael@paquier.xyz 190 : 580 : estate->es_parallel_workers_to_launch += pcxt->nworkers_to_launch;
191 : 580 : estate->es_parallel_workers_launched += pcxt->nworkers_launched;
192 : :
193 : : /* Set up tuple queue readers to read the results. */
3898 rhaas@postgresql.org 194 [ + + ]: 580 : if (pcxt->nworkers_launched > 0)
195 : : {
3339 andres@anarazel.de 196 : 576 : ExecParallelCreateReaders(node->pei);
197 : : /* Make a working array showing the active readers */
3352 tgl@sss.pgh.pa.us 198 : 576 : node->nreaders = pcxt->nworkers_launched;
80 michael@paquier.xyz 199 :GNC 576 : node->reader = palloc_array(TupleQueueReader *, node->nreaders);
3352 tgl@sss.pgh.pa.us 200 :CBC 576 : memcpy(node->reader, node->pei->reader,
201 : 576 : node->nreaders * sizeof(TupleQueueReader *));
202 : : }
203 : : else
204 : : {
205 : : /* No workers? Then never mind. */
206 : 4 : node->nreaders = 0;
207 : 4 : node->reader = NULL;
208 : : }
209 : 580 : node->nextreader = 0;
210 : : }
211 : :
212 : : /* Run plan locally if no workers or enabled and not single-copy. */
213 : 1216 : node->need_to_scan_locally = (node->nreaders == 0)
3277 rhaas@postgresql.org 214 [ + + + + : 608 : || (!gather->single_copy && parallel_leader_participation);
+ + ]
4038 215 : 608 : node->initialized = true;
216 : : }
217 : :
218 : : /*
219 : : * Reset per-tuple memory context to free any expression evaluation
220 : : * storage allocated in the previous tuple cycle.
221 : : */
4026 222 : 2145139 : econtext = node->ps.ps_ExprContext;
223 : 2145139 : ResetExprContext(econtext);
224 : :
225 : : /*
226 : : * Get next tuple, either from one of our workers, or by running the plan
227 : : * ourselves.
228 : : */
3574 tgl@sss.pgh.pa.us 229 : 2145139 : slot = gather_getnext(node);
230 [ + - + + ]: 2145131 : if (TupIsNull(slot))
231 : 576 : return NULL;
232 : :
233 : : /* If no projection is required, we're done. */
3267 rhaas@postgresql.org 234 [ + + ]: 2144555 : if (node->ps.ps_ProjInfo == NULL)
235 : 2144531 : return slot;
236 : :
237 : : /*
238 : : * Form the result tuple using ExecProject(), and return it.
239 : : */
3574 tgl@sss.pgh.pa.us 240 : 24 : econtext->ecxt_outertuple = slot;
241 : 24 : return ExecProject(node->ps.ps_ProjInfo);
242 : : }
243 : :
244 : : /* ----------------------------------------------------------------
245 : : * ExecEndGather
246 : : *
247 : : * frees any storage allocated through C routines.
248 : : * ----------------------------------------------------------------
249 : : */
250 : : void
4054 rhaas@postgresql.org 251 : 789 : ExecEndGather(GatherState *node)
252 : : {
3459 bruce@momjian.us 253 : 789 : ExecEndNode(outerPlanState(node)); /* let children clean up first */
4054 rhaas@postgresql.org 254 : 789 : ExecShutdownGather(node);
255 : 789 : }
256 : :
257 : : /*
258 : : * Read the next tuple. We might fetch a tuple from one of the tuple queues
259 : : * using gather_readnext, or if no tuple queue contains a tuple and the
260 : : * single_copy flag is not set, we might generate one locally instead.
261 : : */
262 : : static TupleTableSlot *
263 : 2145139 : gather_getnext(GatherState *gatherstate)
264 : : {
4026 265 : 2145139 : PlanState *outerPlan = outerPlanState(gatherstate);
266 : : TupleTableSlot *outerTupleSlot;
267 : 2145139 : TupleTableSlot *fslot = gatherstate->funnel_slot;
268 : : MinimalTuple tup;
269 : :
3352 tgl@sss.pgh.pa.us 270 [ + + + + ]: 4291160 : while (gatherstate->nreaders > 0 || gatherstate->need_to_scan_locally)
271 : : {
3390 andres@anarazel.de 272 [ + + ]: 2145445 : CHECK_FOR_INTERRUPTS();
273 : :
3352 tgl@sss.pgh.pa.us 274 [ + + ]: 2145445 : if (gatherstate->nreaders > 0)
275 : : {
4017 rhaas@postgresql.org 276 : 1954937 : tup = gather_readnext(gatherstate);
277 : :
4054 278 [ + + ]: 1954929 : if (HeapTupleIsValid(tup))
279 : : {
2003 tgl@sss.pgh.pa.us 280 : 1590471 : ExecStoreMinimalTuple(tup, /* tuple to store */
281 : : fslot, /* slot to store the tuple */
282 : : false); /* don't pfree tuple */
4026 rhaas@postgresql.org 283 : 1590471 : return fslot;
284 : : }
285 : : }
286 : :
4054 287 [ + + ]: 554966 : if (gatherstate->need_to_scan_locally)
288 : : {
3115 tgl@sss.pgh.pa.us 289 : 554500 : EState *estate = gatherstate->ps.state;
290 : :
291 : : /* Install our DSA area while executing the plan. */
3244 rhaas@postgresql.org 292 : 554500 : estate->es_query_dsa =
293 [ + + ]: 554500 : gatherstate->pei ? gatherstate->pei->area : NULL;
4054 294 : 554500 : outerTupleSlot = ExecProcNode(outerPlan);
3244 295 : 554500 : estate->es_query_dsa = NULL;
296 : :
4054 297 [ + + + + ]: 554500 : if (!TupIsNull(outerTupleSlot))
298 : 554084 : return outerTupleSlot;
299 : :
300 : 416 : gatherstate->need_to_scan_locally = false;
301 : : }
302 : : }
303 : :
4026 304 : 576 : return ExecClearTuple(fslot);
305 : : }
306 : :
307 : : /*
308 : : * Attempt to read a tuple from one of our parallel workers.
309 : : */
310 : : static MinimalTuple
4017 311 : 1954937 : gather_readnext(GatherState *gatherstate)
312 : : {
3751 tgl@sss.pgh.pa.us 313 : 1954937 : int nvisited = 0;
314 : :
315 : : for (;;)
4017 rhaas@postgresql.org 316 : 933848 : {
317 : : TupleQueueReader *reader;
318 : : MinimalTuple tup;
319 : : bool readerdone;
320 : :
321 : : /* Check for async events, particularly messages from workers. */
3748 tgl@sss.pgh.pa.us 322 [ + + ]: 2888785 : CHECK_FOR_INTERRUPTS();
323 : :
324 : : /*
325 : : * Attempt to read a tuple, but don't block if none is available.
326 : : *
327 : : * Note that TupleQueueReaderNext will just return NULL for a worker
328 : : * which fails to initialize. We'll treat that worker as having
329 : : * produced no tuples; WaitForParallelWorkersToFinish will error out
330 : : * when we get there.
331 : : */
3622 rhaas@postgresql.org 332 [ - + ]: 2888777 : Assert(gatherstate->nextreader < gatherstate->nreaders);
4017 333 : 2888777 : reader = gatherstate->reader[gatherstate->nextreader];
334 : 2888777 : tup = TupleQueueReaderNext(reader, true, &readerdone);
335 : :
336 : : /*
337 : : * If this reader is done, remove it from our working array of active
338 : : * readers. If all readers are done, we're outta here.
339 : : */
340 [ + + ]: 2888777 : if (readerdone)
341 : : {
3751 tgl@sss.pgh.pa.us 342 [ - + ]: 1541 : Assert(!tup);
4017 rhaas@postgresql.org 343 : 1541 : --gatherstate->nreaders;
344 [ + + ]: 1541 : if (gatherstate->nreaders == 0)
345 : : {
3016 akapila@postgresql.o 346 : 568 : ExecShutdownGatherWorkers(gatherstate);
4017 rhaas@postgresql.org 347 : 1954929 : return NULL;
348 : : }
3751 tgl@sss.pgh.pa.us 349 : 973 : memmove(&gatherstate->reader[gatherstate->nextreader],
350 : 973 : &gatherstate->reader[gatherstate->nextreader + 1],
351 : : sizeof(TupleQueueReader *)
352 : 973 : * (gatherstate->nreaders - gatherstate->nextreader));
353 [ + + ]: 973 : if (gatherstate->nextreader >= gatherstate->nreaders)
354 : 323 : gatherstate->nextreader = 0;
4017 rhaas@postgresql.org 355 : 973 : continue;
356 : : }
357 : :
358 : : /* If we got a tuple, return it. */
359 [ + + ]: 2887236 : if (tup)
360 : 1590471 : return tup;
361 : :
362 : : /*
363 : : * Advance nextreader pointer in round-robin fashion. Note that we
364 : : * only reach this code if we weren't able to get a tuple from the
365 : : * current worker. We used to advance the nextreader pointer after
366 : : * every tuple, but it turns out to be much more efficient to keep
367 : : * reading from the same queue until that would require blocking.
368 : : */
3751 tgl@sss.pgh.pa.us 369 : 1296765 : gatherstate->nextreader++;
370 [ + + ]: 1296765 : if (gatherstate->nextreader >= gatherstate->nreaders)
371 : 368974 : gatherstate->nextreader = 0;
372 : :
373 : : /* Have we visited every (surviving) TupleQueueReader? */
374 : 1296765 : nvisited++;
375 [ + + ]: 1296765 : if (nvisited >= gatherstate->nreaders)
376 : : {
377 : : /*
378 : : * If (still) running plan locally, return NULL so caller can
379 : : * generate another tuple from the local copy of the plan.
380 : : */
4017 rhaas@postgresql.org 381 [ + + ]: 368446 : if (gatherstate->need_to_scan_locally)
382 : 363890 : return NULL;
383 : :
384 : : /* Nothing to do except wait for developments. */
2904 tmunro@postgresql.or 385 : 4556 : (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0,
386 : : WAIT_EVENT_EXECUTE_GATHER);
4017 rhaas@postgresql.org 387 : 4556 : ResetLatch(MyLatch);
3751 tgl@sss.pgh.pa.us 388 : 4556 : nvisited = 0;
389 : : }
390 : : }
391 : : }
392 : :
393 : : /* ----------------------------------------------------------------
394 : : * ExecShutdownGatherWorkers
395 : : *
396 : : * Stop all the parallel workers.
397 : : * ----------------------------------------------------------------
398 : : */
399 : : static void
4024 rhaas@postgresql.org 400 : 2017 : ExecShutdownGatherWorkers(GatherState *node)
401 : : {
4038 402 [ + + ]: 2017 : if (node->pei != NULL)
403 : 1140 : ExecParallelFinish(node->pei);
404 : :
405 : : /* Flush local copy of reader array */
3352 tgl@sss.pgh.pa.us 406 [ + + ]: 2017 : if (node->reader)
407 : 568 : pfree(node->reader);
408 : 2017 : node->reader = NULL;
4024 rhaas@postgresql.org 409 : 2017 : }
410 : :
411 : : /* ----------------------------------------------------------------
412 : : * ExecShutdownGather
413 : : *
414 : : * Destroy the setup for parallel workers including parallel context.
415 : : * ----------------------------------------------------------------
416 : : */
417 : : void
418 : 1237 : ExecShutdownGather(GatherState *node)
419 : : {
420 : 1237 : ExecShutdownGatherWorkers(node);
421 : :
422 : : /* Now destroy the parallel context. */
423 [ + + ]: 1237 : if (node->pei != NULL)
424 : : {
4038 425 : 412 : ExecParallelCleanup(node->pei);
426 : 412 : node->pei = NULL;
427 : : }
4054 428 : 1237 : }
429 : :
430 : : /* ----------------------------------------------------------------
431 : : * Join Support
432 : : * ----------------------------------------------------------------
433 : : */
434 : :
435 : : /* ----------------------------------------------------------------
436 : : * ExecReScanGather
437 : : *
438 : : * Prepare to re-scan the result of a Gather.
439 : : * ----------------------------------------------------------------
440 : : */
441 : : void
442 : 212 : ExecReScanGather(GatherState *node)
443 : : {
3354 tgl@sss.pgh.pa.us 444 : 212 : Gather *gather = (Gather *) node->ps.plan;
445 : 212 : PlanState *outerPlan = outerPlanState(node);
446 : :
447 : : /* Make sure any existing workers are gracefully shut down */
4024 rhaas@postgresql.org 448 : 212 : ExecShutdownGatherWorkers(node);
449 : :
450 : : /* Mark node so that shared state will be rebuilt at next call */
4038 451 : 212 : node->initialized = false;
452 : :
453 : : /*
454 : : * Set child node's chgParam to tell it that the next scan might deliver a
455 : : * different set of rows within the leader process. (The overall rowset
456 : : * shouldn't change, but the leader process's subset might; hence nodes
457 : : * between here and the parallel table scan node mustn't optimize on the
458 : : * assumption of an unchanging rowset.)
459 : : */
3354 tgl@sss.pgh.pa.us 460 [ + - ]: 212 : if (gather->rescan_param >= 0)
461 : 212 : outerPlan->chgParam = bms_add_member(outerPlan->chgParam,
462 : : gather->rescan_param);
463 : :
464 : : /*
465 : : * If chgParam of subnode is not null then plan will be re-scanned by
466 : : * first ExecProcNode. Note: because this does nothing if we have a
467 : : * rescan_param, it's currently guaranteed that parallel-aware child nodes
468 : : * will not see a ReScan call until after they get a ReInitializeDSM call.
469 : : * That ordering might not be something to rely on, though. A good rule
470 : : * of thumb is that ReInitializeDSM should reset only shared state, ReScan
471 : : * should reset only local state, and anything that depends on both of
472 : : * those steps being finished must wait until the first ExecProcNode call.
473 : : */
474 [ - + ]: 212 : if (outerPlan->chgParam == NULL)
3354 tgl@sss.pgh.pa.us 475 :UBC 0 : ExecReScan(outerPlan);
4054 rhaas@postgresql.org 476 :CBC 212 : }
|