PostgreSQL Source Code git master
Loading...
Searching...
No Matches
execParallel.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * execParallel.c
4 * Support routines for parallel execution.
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * This file contains routines that are intended to support setting up,
10 * using, and tearing down a ParallelContext from within the PostgreSQL
11 * executor. The ParallelContext machinery will handle starting the
12 * workers and ensuring that their state generally matches that of the
13 * leader; see src/backend/access/transam/README.parallel for details.
14 * However, we must save and restore relevant executor state, such as
15 * any ParamListInfo associated with the query, buffer/WAL usage info, and
16 * the actual plan to be passed down to the worker.
17 *
18 * IDENTIFICATION
19 * src/backend/executor/execParallel.c
20 *
21 *-------------------------------------------------------------------------
22 */
23
24#include "postgres.h"
25
27#include "executor/executor.h"
28#include "executor/nodeAgg.h"
29#include "executor/nodeAppend.h"
32#include "executor/nodeCustom.h"
34#include "executor/nodeHash.h"
41#include "executor/nodeSort.h"
44#include "executor/tqueue.h"
45#include "jit/jit.h"
46#include "nodes/nodeFuncs.h"
47#include "pgstat.h"
48#include "storage/proc.h"
49#include "tcop/tcopprot.h"
50#include "utils/datum.h"
51#include "utils/dsa.h"
52#include "utils/lsyscache.h"
53#include "utils/snapmgr.h"
54
55/*
56 * Magic numbers for parallel executor communication. We use constants
57 * greater than any 32-bit integer here so that values < 2^32 can be used
58 * by individual parallel nodes to store their own state.
59 */
60#define PARALLEL_KEY_EXECUTOR_FIXED UINT64CONST(0xE000000000000001)
61#define PARALLEL_KEY_PLANNEDSTMT UINT64CONST(0xE000000000000002)
62#define PARALLEL_KEY_PARAMLISTINFO UINT64CONST(0xE000000000000003)
63#define PARALLEL_KEY_BUFFER_USAGE UINT64CONST(0xE000000000000004)
64#define PARALLEL_KEY_TUPLE_QUEUE UINT64CONST(0xE000000000000005)
65#define PARALLEL_KEY_INSTRUMENTATION UINT64CONST(0xE000000000000006)
66#define PARALLEL_KEY_DSA UINT64CONST(0xE000000000000007)
67#define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xE000000000000008)
68#define PARALLEL_KEY_JIT_INSTRUMENTATION UINT64CONST(0xE000000000000009)
69#define PARALLEL_KEY_WAL_USAGE UINT64CONST(0xE00000000000000A)
70
71#define PARALLEL_TUPLE_QUEUE_SIZE 65536
72
73/*
74 * Fixed-size random stuff that we need to pass to parallel workers.
75 */
83
84/*
85 * DSM structure for accumulating per-PlanState instrumentation.
86 *
87 * instrument_options: Same meaning here as in instrument.c.
88 *
89 * instrument_offset: Offset, relative to the start of this structure,
90 * of the first NodeInstrumentation object. This will depend on the length of
91 * the plan_node_id array.
92 *
93 * num_workers: Number of workers.
94 *
95 * num_plan_nodes: Number of plan nodes.
96 *
97 * plan_node_id: Array of plan nodes for which we are gathering instrumentation
98 * from parallel workers. The length of this array is given by num_plan_nodes.
99 */
101{
107
108 /*
109 * Array of num_plan_nodes * num_workers NodeInstrumentation objects
110 * follows.
111 */
112};
113#define GetInstrumentationArray(sei) \
114 (StaticAssertVariableIsOfTypeMacro(sei, SharedExecutorInstrumentation *), \
115 (NodeInstrumentation *) (((char *) sei) + sei->instrument_offset))
116
117/* Context object for ExecParallelEstimate. */
123
124/* Context object for ExecParallelInitializeDSM. */
131
132/* Helper functions that run in the parallel leader. */
133static char *ExecSerializePlan(Plan *plan, EState *estate);
134static bool ExecParallelEstimate(PlanState *planstate,
136static bool ExecParallelInitializeDSM(PlanState *planstate,
139 bool reinitialize);
140static bool ExecParallelReInitializeDSM(PlanState *planstate,
141 ParallelContext *pcxt);
143 SharedExecutorInstrumentation *instrumentation);
144
145/* Helper function that runs in the parallel worker. */
147
148/*
149 * Create a serialized representation of the plan to be sent to each worker.
150 */
151static char *
153{
154 PlannedStmt *pstmt;
155 ListCell *lc;
156
157 /* We can't scribble on the original plan, so make a copy. */
159
160 /*
161 * The worker will start its own copy of the executor, and that copy will
162 * insert a junk filter if the toplevel node has any resjunk entries. We
163 * don't want that to happen, because while resjunk columns shouldn't be
164 * sent back to the user, here the tuples are coming back to another
165 * backend which may very well need them. So mutate the target list
166 * accordingly. This is sort of a hack; there might be better ways to do
167 * this...
168 */
169 foreach(lc, plan->targetlist)
170 {
172
173 tle->resjunk = false;
174 }
175
176 /*
177 * Create a dummy PlannedStmt. Most of the fields don't need to be valid
178 * for our purposes, but the worker will need at least a minimal
179 * PlannedStmt to start the executor.
180 */
181 pstmt = makeNode(PlannedStmt);
182 pstmt->commandType = CMD_SELECT;
184 pstmt->planId = pgstat_get_my_plan_id();
185 pstmt->hasReturning = false;
186 pstmt->hasModifyingCTE = false;
187 pstmt->canSetTag = true;
188 pstmt->transientPlan = false;
189 pstmt->dependsOnRole = false;
190 pstmt->parallelModeNeeded = false;
191 pstmt->planTree = plan;
192 pstmt->partPruneInfos = estate->es_part_prune_infos;
193 pstmt->rtable = estate->es_range_table;
194 pstmt->unprunableRelids = estate->es_unpruned_relids;
195 pstmt->permInfos = estate->es_rteperminfos;
196 pstmt->appendRelations = NIL;
198
199 /*
200 * Transfer only parallel-safe subplans, leaving a NULL "hole" in the list
201 * for unsafe ones (so that the list indexes of the safe ones are
202 * preserved). This positively ensures that the worker won't try to run,
203 * or even do ExecInitNode on, an unsafe subplan. That's important to
204 * protect, eg, non-parallel-aware FDWs from getting into trouble.
205 */
206 pstmt->subplans = NIL;
207 foreach(lc, estate->es_plannedstmt->subplans)
208 {
209 Plan *subplan = (Plan *) lfirst(lc);
210
211 if (subplan && !subplan->parallel_safe)
212 subplan = NULL;
213 pstmt->subplans = lappend(pstmt->subplans, subplan);
214 }
215
216 pstmt->rewindPlanIDs = NULL;
217 pstmt->rowMarks = NIL;
218
219 /*
220 * Pass the row mark and result relation relids to parallel workers. They
221 * may need to check them to inform heuristics.
222 */
225 pstmt->relationOids = NIL;
226 pstmt->invalItems = NIL; /* workers can't replan anyway... */
228 pstmt->utilityStmt = NULL;
229 pstmt->stmt_location = -1;
230 pstmt->stmt_len = -1;
231
232 /* Return serialized copy of our dummy PlannedStmt. */
233 return nodeToString(pstmt);
234}
235
236/*
237 * Parallel-aware plan nodes (and occasionally others) may need some state
238 * which is shared across all parallel workers. Before we size the DSM, give
239 * them a chance to call shm_toc_estimate_chunk or shm_toc_estimate_keys on
240 * &pcxt->estimator.
241 *
242 * While we're at it, count the number of PlanState nodes in the tree, so
243 * we know how many Instrumentation structures we need.
244 */
245static bool
247{
248 if (planstate == NULL)
249 return false;
250
251 /* Count this node. */
252 e->nnodes++;
253
254 switch (nodeTag(planstate))
255 {
256 case T_SeqScanState:
257 if (planstate->plan->parallel_aware)
258 ExecSeqScanEstimate((SeqScanState *) planstate,
259 e->pcxt);
260 /* even when not parallel-aware, for EXPLAIN ANALYZE */
262 e->pcxt);
263 break;
264 case T_IndexScanState:
265 if (planstate->plan->parallel_aware)
267 e->pcxt);
268 /* even when not parallel-aware, for EXPLAIN ANALYZE */
270 e->pcxt);
271 break;
273 if (planstate->plan->parallel_aware)
275 e->pcxt);
276 /* even when not parallel-aware, for EXPLAIN ANALYZE */
278 e->pcxt);
279 break;
281 /* even when not parallel-aware, for EXPLAIN ANALYZE */
283 e->pcxt);
284 break;
286 if (planstate->plan->parallel_aware)
288 e->pcxt);
289 break;
291 if (planstate->plan->parallel_aware)
293 e->pcxt);
294 /* even when not parallel-aware, for EXPLAIN ANALYZE */
296 e->pcxt);
297 break;
298 case T_AppendState:
299 if (planstate->plan->parallel_aware)
300 ExecAppendEstimate((AppendState *) planstate,
301 e->pcxt);
302 break;
304 if (planstate->plan->parallel_aware)
306 e->pcxt);
307 break;
309 if (planstate->plan->parallel_aware)
311 e->pcxt);
312 /* even when not parallel-aware, for EXPLAIN ANALYZE */
314 e->pcxt);
315 break;
316 case T_HashJoinState:
317 if (planstate->plan->parallel_aware)
319 e->pcxt);
320 break;
321 case T_HashState:
322 /* even when not parallel-aware, for EXPLAIN ANALYZE */
323 ExecHashEstimate((HashState *) planstate, e->pcxt);
324 break;
325 case T_SortState:
326 /* even when not parallel-aware, for EXPLAIN ANALYZE */
327 ExecSortEstimate((SortState *) planstate, e->pcxt);
328 break;
330 /* even when not parallel-aware, for EXPLAIN ANALYZE */
332 break;
333 case T_AggState:
334 /* even when not parallel-aware, for EXPLAIN ANALYZE */
335 ExecAggEstimate((AggState *) planstate, e->pcxt);
336 break;
337 case T_MemoizeState:
338 /* even when not parallel-aware, for EXPLAIN ANALYZE */
339 ExecMemoizeEstimate((MemoizeState *) planstate, e->pcxt);
340 break;
341 default:
342 break;
343 }
344
345 return planstate_tree_walker(planstate, ExecParallelEstimate, e);
346}
347
348/*
349 * Estimate the amount of space required to serialize the indicated parameters.
350 */
351static Size
353{
354 int paramid;
355 Size sz = sizeof(int);
356
357 paramid = -1;
358 while ((paramid = bms_next_member(params, paramid)) >= 0)
359 {
360 Oid typeOid;
361 int16 typLen;
362 bool typByVal;
364
365 prm = &(estate->es_param_exec_vals[paramid]);
366 typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
367 paramid);
368
369 sz = add_size(sz, sizeof(int)); /* space for paramid */
370
371 /* space for datum/isnull */
372 if (OidIsValid(typeOid))
373 get_typlenbyval(typeOid, &typLen, &typByVal);
374 else
375 {
376 /* If no type OID, assume by-value, like copyParamList does. */
377 typLen = sizeof(Datum);
378 typByVal = true;
379 }
380 sz = add_size(sz,
381 datumEstimateSpace(prm->value, prm->isnull,
382 typByVal, typLen));
383 }
384 return sz;
385}
386
387/*
388 * Serialize specified PARAM_EXEC parameters.
389 *
390 * We write the number of parameters first, as a 4-byte integer, and then
391 * write details for each parameter in turn. The details for each parameter
392 * consist of a 4-byte paramid (location of param in execution time internal
393 * parameter array) and then the datum as serialized by datumSerialize().
394 */
395static dsa_pointer
397{
398 Size size;
399 int nparams;
400 int paramid;
402 dsa_pointer handle;
403 char *start_address;
404
405 /* Allocate enough space for the current parameter values. */
406 size = EstimateParamExecSpace(estate, params);
407 handle = dsa_allocate(area, size);
408 start_address = dsa_get_address(area, handle);
409
410 /* First write the number of parameters as a 4-byte integer. */
411 nparams = bms_num_members(params);
412 memcpy(start_address, &nparams, sizeof(int));
413 start_address += sizeof(int);
414
415 /* Write details for each parameter in turn. */
416 paramid = -1;
417 while ((paramid = bms_next_member(params, paramid)) >= 0)
418 {
419 Oid typeOid;
420 int16 typLen;
421 bool typByVal;
422
423 prm = &(estate->es_param_exec_vals[paramid]);
424 typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
425 paramid);
426
427 /* Write paramid. */
428 memcpy(start_address, &paramid, sizeof(int));
429 start_address += sizeof(int);
430
431 /* Write datum/isnull */
432 if (OidIsValid(typeOid))
433 get_typlenbyval(typeOid, &typLen, &typByVal);
434 else
435 {
436 /* If no type OID, assume by-value, like copyParamList does. */
437 typLen = sizeof(Datum);
438 typByVal = true;
439 }
440 datumSerialize(prm->value, prm->isnull, typByVal, typLen,
442 }
443
444 return handle;
445}
446
447/*
448 * Restore specified PARAM_EXEC parameters.
449 */
450static void
452{
453 int nparams;
454 int i;
455 int paramid;
456
457 memcpy(&nparams, start_address, sizeof(int));
458 start_address += sizeof(int);
459
460 for (i = 0; i < nparams; i++)
461 {
463
464 /* Read paramid */
465 memcpy(&paramid, start_address, sizeof(int));
466 start_address += sizeof(int);
467 prm = &(estate->es_param_exec_vals[paramid]);
468
469 /* Read datum/isnull. */
470 prm->value = datumRestore(&start_address, &prm->isnull);
471 prm->execPlan = NULL;
472 }
473}
474
475/*
476 * Initialize the dynamic shared memory segment that will be used to control
477 * parallel execution.
478 */
479static bool
482{
483 if (planstate == NULL)
484 return false;
485
486 /* If instrumentation is enabled, initialize slot for this node. */
487 if (d->instrumentation != NULL)
489 planstate->plan->plan_node_id;
490
491 /* Count this node. */
492 d->nnodes++;
493
494 /*
495 * Call initializers for DSM-using plan nodes.
496 *
497 * Most plan nodes won't do anything here, but plan nodes that allocated
498 * DSM may need to initialize shared state in the DSM before parallel
499 * workers are launched. They can allocate the space they previously
500 * estimated using shm_toc_allocate, and add the keys they previously
501 * estimated using shm_toc_insert, in each case targeting pcxt->toc.
502 */
503 switch (nodeTag(planstate))
504 {
505 case T_SeqScanState:
506 if (planstate->plan->parallel_aware)
508 d->pcxt);
509 /* even when not parallel-aware, for EXPLAIN ANALYZE */
511 d->pcxt);
512 break;
513 case T_IndexScanState:
514 if (planstate->plan->parallel_aware)
516 d->pcxt);
517 /* even when not parallel-aware, for EXPLAIN ANALYZE */
519 d->pcxt);
520 break;
522 if (planstate->plan->parallel_aware)
524 d->pcxt);
525 /* even when not parallel-aware, for EXPLAIN ANALYZE */
527 d->pcxt);
528 break;
530 /* even when not parallel-aware, for EXPLAIN ANALYZE */
532 break;
534 if (planstate->plan->parallel_aware)
536 d->pcxt);
537 break;
539 if (planstate->plan->parallel_aware)
541 d->pcxt);
542 /* even when not parallel-aware, for EXPLAIN ANALYZE */
544 d->pcxt);
545 break;
546 case T_AppendState:
547 if (planstate->plan->parallel_aware)
549 d->pcxt);
550 break;
552 if (planstate->plan->parallel_aware)
554 d->pcxt);
555 break;
557 if (planstate->plan->parallel_aware)
559 d->pcxt);
560 /* even when not parallel-aware, for EXPLAIN ANALYZE */
562 d->pcxt);
563 break;
564 case T_HashJoinState:
565 if (planstate->plan->parallel_aware)
567 d->pcxt);
568 break;
569 case T_HashState:
570 /* even when not parallel-aware, for EXPLAIN ANALYZE */
571 ExecHashInitializeDSM((HashState *) planstate, d->pcxt);
572 break;
573 case T_SortState:
574 /* even when not parallel-aware, for EXPLAIN ANALYZE */
575 ExecSortInitializeDSM((SortState *) planstate, d->pcxt);
576 break;
578 /* even when not parallel-aware, for EXPLAIN ANALYZE */
580 break;
581 case T_AggState:
582 /* even when not parallel-aware, for EXPLAIN ANALYZE */
583 ExecAggInitializeDSM((AggState *) planstate, d->pcxt);
584 break;
585 case T_MemoizeState:
586 /* even when not parallel-aware, for EXPLAIN ANALYZE */
587 ExecMemoizeInitializeDSM((MemoizeState *) planstate, d->pcxt);
588 break;
589 default:
590 break;
591 }
592
594}
595
596/*
597 * It sets up the response queues for backend workers to return tuples
598 * to the main backend and start the workers.
599 */
600static shm_mq_handle **
602{
604 char *tqueuespace;
605 int i;
606
607 /* Skip this if no workers. */
608 if (pcxt->nworkers == 0)
609 return NULL;
610
611 /* Allocate memory for shared memory queue handles. */
613 palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
614
615 /*
616 * If not reinitializing, allocate space from the DSM for the queues;
617 * otherwise, find the already allocated space.
618 */
619 if (!reinitialize)
621 shm_toc_allocate(pcxt->toc,
623 pcxt->nworkers));
624 else
626
627 /* Create the queues, and become the receiver for each. */
628 for (i = 0; i < pcxt->nworkers; ++i)
629 {
630 shm_mq *mq;
631
635
637 responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
638 }
639
640 /* Add array of queues to shm_toc, so others can find it. */
641 if (!reinitialize)
643
644 /* Return array of handles. */
645 return responseq;
646}
647
648/*
649 * Sets up the required infrastructure for backend workers to perform
650 * execution and return results to the main backend.
651 */
654 Bitmapset *sendParams, int nworkers,
655 int64 tuples_needed)
656{
658 ParallelContext *pcxt;
662 char *pstmt_data;
663 char *pstmt_space;
667 SharedExecutorInstrumentation *instrumentation = NULL;
668 SharedJitInstrumentation *jit_instrumentation = NULL;
669 int pstmt_len;
671 int instrumentation_len = 0;
673 int instrument_offset = 0;
675 char *query_string;
676 int query_len;
677
678 /*
679 * Force any initplan outputs that we're going to pass to workers to be
680 * evaluated, if they weren't already.
681 *
682 * For simplicity, we use the EState's per-output-tuple ExprContext here.
683 * That risks intra-query memory leakage, since we might pass through here
684 * many times before that ExprContext gets reset; but ExecSetParamPlan
685 * doesn't normally leak any memory in the context (see its comments), so
686 * it doesn't seem worth complicating this function's API to pass it a
687 * shorter-lived ExprContext. This might need to change someday.
688 */
690
691 /* Allocate object for return value. */
693 pei->finished = false;
694 pei->planstate = planstate;
695
696 /* Fix up and serialize plan to be sent to workers. */
697 pstmt_data = ExecSerializePlan(planstate->plan, estate);
698
699 /* Create a parallel context. */
700 pcxt = CreateParallelContext("postgres", "ParallelQueryMain", nworkers);
701 pei->pcxt = pcxt;
702
703 /*
704 * Before telling the parallel context to create a dynamic shared memory
705 * segment, we need to figure out how big it should be. Estimate space
706 * for the various things we need to store.
707 */
708
709 /* Estimate space for fixed-size state. */
713
714 /* Estimate space for query text. */
715 query_len = strlen(estate->es_sourceText);
716 shm_toc_estimate_chunk(&pcxt->estimator, query_len + 1);
718
719 /* Estimate space for serialized PlannedStmt. */
723
724 /* Estimate space for serialized ParamListInfo. */
728
729 /*
730 * Estimate space for BufferUsage.
731 *
732 * If EXPLAIN is not in use and there are no extensions loaded that care,
733 * we could skip this. But we have no way of knowing whether anyone's
734 * looking at pgBufferUsage, so do it unconditionally.
735 */
737 mul_size(sizeof(BufferUsage), pcxt->nworkers));
739
740 /*
741 * Same thing for WalUsage.
742 */
744 mul_size(sizeof(WalUsage), pcxt->nworkers));
746
747 /* Estimate space for tuple queues. */
751
752 /*
753 * Give parallel-aware nodes a chance to add to the estimates, and get a
754 * count of how many PlanState nodes there are.
755 */
756 e.pcxt = pcxt;
757 e.nnodes = 0;
758 ExecParallelEstimate(planstate, &e);
759
760 /* Estimate space for instrumentation, if required. */
761 if (estate->es_instrument)
762 {
765 sizeof(int) * e.nnodes;
767 instrument_offset = instrumentation_len;
770 mul_size(e.nnodes, nworkers));
773
774 /* Estimate space for JIT instrumentation, if required. */
775 if (estate->es_jit_flags != PGJIT_NONE)
776 {
779 sizeof(JitInstrumentation) * nworkers;
782 }
783 }
784
785 /* Estimate space for DSA area. */
788
789 /*
790 * InitializeParallelDSM() passes the active snapshot to the parallel
791 * worker, which uses it to set es_snapshot. Make sure we don't set
792 * es_snapshot differently in the child.
793 */
795
796 /* Everyone's had a chance to ask for space, so now create the DSM. */
798
799 /*
800 * OK, now we have a dynamic shared memory segment, and it should be big
801 * enough to store all of the data we estimated we would want to put into
802 * it, plus whatever general stuff (not specifically executor-related) the
803 * ParallelContext itself needs to store there. None of the space we
804 * asked for has been allocated or initialized yet, though, so do that.
805 */
806
807 /* Store fixed-size state. */
809 fpes->tuples_needed = tuples_needed;
810 fpes->param_exec = InvalidDsaPointer;
811 fpes->eflags = estate->es_top_eflags;
812 fpes->jit_flags = estate->es_jit_flags;
814
815 /* Store query string */
816 query_string = shm_toc_allocate(pcxt->toc, query_len + 1);
817 memcpy(query_string, estate->es_sourceText, query_len + 1);
818 shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string);
819
820 /* Store serialized PlannedStmt. */
824
825 /* Store serialized ParamListInfo. */
829
830 /* Allocate space for each worker's BufferUsage; no need to initialize. */
832 mul_size(sizeof(BufferUsage), pcxt->nworkers));
835
836 /* Same for WalUsage. */
838 mul_size(sizeof(WalUsage), pcxt->nworkers));
841
842 /* Set up the tuple queues that the workers will write into. */
843 pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
844
845 /* We don't need the TupleQueueReaders yet, though. */
846 pei->reader = NULL;
847
848 /*
849 * If instrumentation options were supplied, allocate space for the data.
850 * It only gets partially initialized here; the rest happens during
851 * ExecParallelInitializeDSM.
852 */
853 if (estate->es_instrument)
854 {
855 NodeInstrumentation *instrument;
856 int i;
857
858 instrumentation = shm_toc_allocate(pcxt->toc, instrumentation_len);
859 instrumentation->instrument_options = estate->es_instrument;
860 instrumentation->instrument_offset = instrument_offset;
861 instrumentation->num_workers = nworkers;
862 instrumentation->num_plan_nodes = e.nnodes;
863 instrument = GetInstrumentationArray(instrumentation);
864 for (i = 0; i < nworkers * e.nnodes; ++i)
865 InstrInitNode(&instrument[i], estate->es_instrument, false);
867 instrumentation);
868 pei->instrumentation = instrumentation;
869
870 if (estate->es_jit_flags != PGJIT_NONE)
871 {
872 jit_instrumentation = shm_toc_allocate(pcxt->toc,
874 jit_instrumentation->num_workers = nworkers;
875 memset(jit_instrumentation->jit_instr, 0,
876 sizeof(JitInstrumentation) * nworkers);
878 jit_instrumentation);
879 pei->jit_instrumentation = jit_instrumentation;
880 }
881 }
882
883 /*
884 * Create a DSA area that can be used by the leader and all workers.
885 * (However, if we failed to create a DSM and are using private memory
886 * instead, then skip this.)
887 */
888 if (pcxt->seg != NULL)
889 {
890 char *area_space;
891
896 pcxt->seg);
897
898 /*
899 * Serialize parameters, if any, using DSA storage. We don't dare use
900 * the main parallel query DSM for this because we might relaunch
901 * workers after the values have changed (and thus the amount of
902 * storage required has changed).
903 */
905 {
907 pei->area);
908 fpes->param_exec = pei->param_exec;
909 }
910 }
911
912 /*
913 * Give parallel-aware nodes a chance to initialize their shared data.
914 * This also initializes the elements of instrumentation->ps_instrument,
915 * if it exists.
916 */
917 d.pcxt = pcxt;
918 d.instrumentation = instrumentation;
919 d.nnodes = 0;
920
921 /* Install our DSA area while initializing the plan. */
922 estate->es_query_dsa = pei->area;
923 ExecParallelInitializeDSM(planstate, &d);
924 estate->es_query_dsa = NULL;
925
926 /*
927 * Make sure that the world hasn't shifted under our feet. This could
928 * probably just be an Assert(), but let's be conservative for now.
929 */
930 if (e.nnodes != d.nnodes)
931 elog(ERROR, "inconsistent count of PlanState nodes");
932
933 /* OK, we're ready to rock and roll. */
934 return pei;
935}
936
937/*
938 * Set up tuple queue readers to read the results of a parallel subplan.
939 *
940 * This is separate from ExecInitParallelPlan() because we can launch the
941 * worker processes and let them start doing something before we do this.
942 */
943void
945{
946 int nworkers = pei->pcxt->nworkers_launched;
947 int i;
948
949 Assert(pei->reader == NULL);
950
951 if (nworkers > 0)
952 {
953 pei->reader = (TupleQueueReader **)
954 palloc(nworkers * sizeof(TupleQueueReader *));
955
956 for (i = 0; i < nworkers; i++)
957 {
959 pei->pcxt->worker[i].bgwhandle);
960 pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
961 }
962 }
963}
964
965/*
966 * Re-initialize the parallel executor shared memory state before launching
967 * a fresh batch of workers.
968 */
969void
973{
974 EState *estate = planstate->state;
976
977 /* Old workers must already be shut down */
978 Assert(pei->finished);
979
980 /*
981 * Force any initplan outputs that we're going to pass to workers to be
982 * evaluated, if they weren't already (see comments in
983 * ExecInitParallelPlan).
984 */
986
988 pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
989 pei->reader = NULL;
990 pei->finished = false;
991
993
994 /* Free any serialized parameters from the last round. */
995 if (DsaPointerIsValid(fpes->param_exec))
996 {
997 dsa_free(pei->area, fpes->param_exec);
998 fpes->param_exec = InvalidDsaPointer;
999 }
1000
1001 /* Serialize current parameter values if required. */
1003 {
1005 pei->area);
1006 fpes->param_exec = pei->param_exec;
1007 }
1008
1009 /* Traverse plan tree and let each child node reset associated state. */
1010 estate->es_query_dsa = pei->area;
1011 ExecParallelReInitializeDSM(planstate, pei->pcxt);
1012 estate->es_query_dsa = NULL;
1013}
1014
1015/*
1016 * Traverse plan tree to reinitialize per-node dynamic shared memory state
1017 */
1018static bool
1020 ParallelContext *pcxt)
1021{
1022 if (planstate == NULL)
1023 return false;
1024
1025 /*
1026 * Call reinitializers for DSM-using plan nodes.
1027 */
1028 switch (nodeTag(planstate))
1029 {
1030 case T_SeqScanState:
1031 if (planstate->plan->parallel_aware)
1033 pcxt);
1034 break;
1035 case T_IndexScanState:
1036 if (planstate->plan->parallel_aware)
1038 pcxt);
1039 break;
1041 if (planstate->plan->parallel_aware)
1043 pcxt);
1044 break;
1045 case T_ForeignScanState:
1046 if (planstate->plan->parallel_aware)
1048 pcxt);
1049 break;
1051 if (planstate->plan->parallel_aware)
1053 pcxt);
1054 break;
1055 case T_AppendState:
1056 if (planstate->plan->parallel_aware)
1057 ExecAppendReInitializeDSM((AppendState *) planstate, pcxt);
1058 break;
1059 case T_CustomScanState:
1060 if (planstate->plan->parallel_aware)
1062 pcxt);
1063 break;
1065 if (planstate->plan->parallel_aware)
1067 pcxt);
1068 break;
1069 case T_HashJoinState:
1070 if (planstate->plan->parallel_aware)
1072 pcxt);
1073 break;
1075 case T_HashState:
1076 case T_SortState:
1078 case T_MemoizeState:
1079 /* these nodes have DSM state, but no reinitialization is required */
1080 break;
1081
1082 default:
1083 break;
1084 }
1085
1086 return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt);
1087}
1088
1089/*
1090 * Copy instrumentation information about this node and its descendants from
1091 * dynamic shared memory.
1092 */
1093static bool
1095 SharedExecutorInstrumentation *instrumentation)
1096{
1097 NodeInstrumentation *instrument;
1098 int i;
1099 int n;
1100 int ibytes;
1101 int plan_node_id = planstate->plan->plan_node_id;
1102 MemoryContext oldcontext;
1103
1104 /* Find the instrumentation for this node. */
1105 for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1106 if (instrumentation->plan_node_id[i] == plan_node_id)
1107 break;
1108 if (i >= instrumentation->num_plan_nodes)
1109 elog(ERROR, "plan node %d not found", plan_node_id);
1110
1111 /* Accumulate the statistics from all workers. */
1112 instrument = GetInstrumentationArray(instrumentation);
1113 instrument += i * instrumentation->num_workers;
1114 for (n = 0; n < instrumentation->num_workers; ++n)
1115 InstrAggNode(planstate->instrument, &instrument[n]);
1116
1117 /*
1118 * Also store the per-worker detail.
1119 *
1120 * Worker instrumentation should be allocated in the same context as the
1121 * regular instrumentation information, which is the per-query context.
1122 * Switch into per-query memory context.
1123 */
1124 oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
1125 ibytes = mul_size(instrumentation->num_workers, sizeof(NodeInstrumentation));
1126 planstate->worker_instrument =
1128 MemoryContextSwitchTo(oldcontext);
1129
1130 planstate->worker_instrument->num_workers = instrumentation->num_workers;
1131 memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
1132
1133 /* Perform any node-type-specific work that needs to be done. */
1134 switch (nodeTag(planstate))
1135 {
1136 case T_IndexScanState:
1138 break;
1141 break;
1144 break;
1145 case T_SortState:
1147 break;
1150 break;
1151 case T_HashState:
1153 break;
1154 case T_AggState:
1156 break;
1157 case T_MemoizeState:
1159 break;
1162 break;
1163 case T_SeqScanState:
1165 break;
1168 break;
1169 default:
1170 break;
1171 }
1172
1174 instrumentation);
1175}
1176
1177/*
1178 * Add up the workers' JIT instrumentation from dynamic shared memory.
1179 */
1180static void
1183{
1185 int ibytes;
1186
1187 int n;
1188
1189 /*
1190 * Accumulate worker JIT instrumentation into the combined JIT
1191 * instrumentation, allocating it if required.
1192 */
1193 if (!planstate->state->es_jit_worker_instr)
1194 planstate->state->es_jit_worker_instr =
1196 combined = planstate->state->es_jit_worker_instr;
1197
1198 /* Accumulate all the workers' instrumentations. */
1199 for (n = 0; n < shared_jit->num_workers; ++n)
1200 InstrJitAgg(combined, &shared_jit->jit_instr[n]);
1201
1202 /*
1203 * Store the per-worker detail.
1204 *
1205 * Similar to ExecParallelRetrieveInstrumentation(), allocate the
1206 * instrumentation in per-query context.
1207 */
1209 + mul_size(shared_jit->num_workers, sizeof(JitInstrumentation));
1210 planstate->worker_jit_instrument =
1212
1214}
1215
1216/*
1217 * Finish parallel execution. We wait for parallel workers to finish, and
1218 * accumulate their buffer/WAL usage.
1219 */
1220void
1222{
1223 int nworkers = pei->pcxt->nworkers_launched;
1224 int i;
1225
1226 /* Make this be a no-op if called twice in a row. */
1227 if (pei->finished)
1228 return;
1229
1230 /*
1231 * Detach from tuple queues ASAP, so that any still-active workers will
1232 * notice that no further results are wanted.
1233 */
1234 if (pei->tqueue != NULL)
1235 {
1236 for (i = 0; i < nworkers; i++)
1237 shm_mq_detach(pei->tqueue[i]);
1238 pfree(pei->tqueue);
1239 pei->tqueue = NULL;
1240 }
1241
1242 /*
1243 * While we're waiting for the workers to finish, let's get rid of the
1244 * tuple queue readers. (Any other local cleanup could be done here too.)
1245 */
1246 if (pei->reader != NULL)
1247 {
1248 for (i = 0; i < nworkers; i++)
1250 pfree(pei->reader);
1251 pei->reader = NULL;
1252 }
1253
1254 /* Now wait for the workers to finish. */
1256
1257 /*
1258 * Next, accumulate buffer/WAL usage. (This must wait for the workers to
1259 * finish, or we might get incomplete data.)
1260 */
1261 for (i = 0; i < nworkers; i++)
1263
1264 pei->finished = true;
1265}
1266
1267/*
1268 * Accumulate instrumentation, and then clean up whatever ParallelExecutorInfo
1269 * resources still exist after ExecParallelFinish. We separate these
1270 * routines because someone might want to examine the contents of the DSM
1271 * after ExecParallelFinish and before calling this routine.
1272 */
1273void
1275{
1276 /* Accumulate instrumentation, if any. */
1277 if (pei->instrumentation)
1279 pei->instrumentation);
1280
1281 /* Accumulate JIT instrumentation, if any. */
1282 if (pei->jit_instrumentation)
1284 pei->jit_instrumentation);
1285
1286 /* Free any serialized parameters. */
1287 if (DsaPointerIsValid(pei->param_exec))
1288 {
1289 dsa_free(pei->area, pei->param_exec);
1291 }
1292 if (pei->area != NULL)
1293 {
1294 dsa_detach(pei->area);
1295 pei->area = NULL;
1296 }
1297 if (pei->pcxt != NULL)
1298 {
1300 pei->pcxt = NULL;
1301 }
1302 pfree(pei);
1303}
1304
1305/*
1306 * Create a DestReceiver to write tuples we produce to the shm_mq designated
1307 * for that purpose.
1308 */
1309static DestReceiver *
1321
1322/*
1323 * Create a QueryDesc for the PlannedStmt we are to execute, and return it.
1324 */
1325static QueryDesc *
1327 int instrument_options)
1328{
1329 char *pstmtspace;
1330 char *paramspace;
1331 PlannedStmt *pstmt;
1332 ParamListInfo paramLI;
1333 char *queryString;
1334
1335 /* Get the query string from shared memory */
1336 queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1337
1338 /* Reconstruct leader-supplied PlannedStmt. */
1340 pstmt = (PlannedStmt *) stringToNode(pstmtspace);
1341
1342 /* Reconstruct ParamListInfo. */
1344 paramLI = RestoreParamList(&paramspace);
1345
1346 /* Create a QueryDesc for the query. */
1347 return CreateQueryDesc(pstmt,
1348 queryString,
1350 receiver, paramLI, NULL, instrument_options);
1351}
1352
1353/*
1354 * Copy instrumentation information from this node and its descendants into
1355 * dynamic shared memory, so that the parallel leader can retrieve it.
1356 */
1357static bool
1359 SharedExecutorInstrumentation *instrumentation)
1360{
1361 int i;
1362 int plan_node_id = planstate->plan->plan_node_id;
1363 NodeInstrumentation *instrument;
1364
1365 InstrEndLoop(planstate->instrument);
1366
1367 /*
1368 * If we shuffled the plan_node_id values in ps_instrument into sorted
1369 * order, we could use binary search here. This might matter someday if
1370 * we're pushing down sufficiently large plan trees. For now, do it the
1371 * slow, dumb way.
1372 */
1373 for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1374 if (instrumentation->plan_node_id[i] == plan_node_id)
1375 break;
1376 if (i >= instrumentation->num_plan_nodes)
1377 elog(ERROR, "plan node %d not found", plan_node_id);
1378
1379 /*
1380 * Add our statistics to the per-node, per-worker totals. It's possible
1381 * that this could happen more than once if we relaunched workers.
1382 */
1383 instrument = GetInstrumentationArray(instrumentation);
1384 instrument += i * instrumentation->num_workers;
1387 InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
1388
1390 instrumentation);
1391}
1392
1393/*
1394 * Initialize the PlanState and its descendants with the information
1395 * retrieved from shared memory. This has to be done once the PlanState
1396 * is allocated and initialized by executor; that is, after ExecutorStart().
1397 */
1398static bool
1400{
1401 if (planstate == NULL)
1402 return false;
1403
1404 switch (nodeTag(planstate))
1405 {
1406 case T_SeqScanState:
1407 if (planstate->plan->parallel_aware)
1409 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1411 break;
1412 case T_IndexScanState:
1413 if (planstate->plan->parallel_aware)
1415 pwcxt);
1416 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1418 pwcxt);
1419 break;
1421 if (planstate->plan->parallel_aware)
1423 pwcxt);
1424 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1426 pwcxt);
1427 break;
1429 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1431 pwcxt);
1432 break;
1433 case T_ForeignScanState:
1434 if (planstate->plan->parallel_aware)
1436 pwcxt);
1437 break;
1439 if (planstate->plan->parallel_aware)
1441 pwcxt);
1442 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1444 pwcxt);
1445 break;
1446 case T_AppendState:
1447 if (planstate->plan->parallel_aware)
1449 break;
1450 case T_CustomScanState:
1451 if (planstate->plan->parallel_aware)
1453 pwcxt);
1454 break;
1456 if (planstate->plan->parallel_aware)
1458 pwcxt);
1459 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1461 pwcxt);
1462 break;
1463 case T_HashJoinState:
1464 if (planstate->plan->parallel_aware)
1466 pwcxt);
1467 break;
1468 case T_HashState:
1469 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1471 break;
1472 case T_SortState:
1473 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1475 break;
1477 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1479 pwcxt);
1480 break;
1481 case T_AggState:
1482 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1483 ExecAggInitializeWorker((AggState *) planstate, pwcxt);
1484 break;
1485 case T_MemoizeState:
1486 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1488 break;
1489 default:
1490 break;
1491 }
1492
1494 pwcxt);
1495}
1496
1497/*
1498 * Main entrypoint for parallel query worker processes.
1499 *
1500 * We reach this function from ParallelWorkerMain, so the setup necessary to
1501 * create a sensible parallel environment has already been done;
1502 * ParallelWorkerMain worries about stuff like the transaction state, combo
1503 * CID mappings, and GUC values, so we don't need to deal with any of that
1504 * here.
1505 *
1506 * Our job is to deal with concerns specific to the executor. The parallel
1507 * group leader will have stored a serialized PlannedStmt, and it's our job
1508 * to execute that plan and write the resulting tuples to the appropriate
1509 * tuple queue. Various bits of supporting information that we need in order
1510 * to do this are also stored in the dsm_segment and can be accessed through
1511 * the shm_toc.
1512 */
1513void
1515{
1517 BufferUsage *buffer_usage;
1518 WalUsage *wal_usage;
1520 QueryDesc *queryDesc;
1521 SharedExecutorInstrumentation *instrumentation;
1522 SharedJitInstrumentation *jit_instrumentation;
1523 int instrument_options = 0;
1524 void *area_space;
1525 dsa_area *area;
1527
1528 /* Get fixed-size state. */
1530
1531 /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1533 instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1534 if (instrumentation != NULL)
1535 instrument_options = instrumentation->instrument_options;
1536 jit_instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
1537 true);
1538 queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1539
1540 /* Setting debug_query_string for individual workers */
1541 debug_query_string = queryDesc->sourceText;
1542
1543 /* Report workers' query for monitoring purposes */
1545
1546 /* Attach to the dynamic shared memory area. */
1548 area = dsa_attach_in_place(area_space, seg);
1549
1550 /* Start up the executor */
1551 queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1552 ExecutorStart(queryDesc, fpes->eflags);
1553
1554 /* Special executor initialization steps for parallel workers */
1555 queryDesc->planstate->state->es_query_dsa = area;
1556 if (DsaPointerIsValid(fpes->param_exec))
1557 {
1558 char *paramexec_space;
1559
1560 paramexec_space = dsa_get_address(area, fpes->param_exec);
1562 }
1563 pwcxt.toc = toc;
1564 pwcxt.seg = seg;
1566
1567 /* Pass down any tuple bound */
1568 ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1569
1570 /*
1571 * Prepare to track buffer/WAL usage during query execution.
1572 *
1573 * We do this after starting up the executor to match what happens in the
1574 * leader, which also doesn't count buffer accesses and WAL activity that
1575 * occur during executor startup.
1576 */
1578
1579 /*
1580 * Run the plan. If we specified a tuple bound, be careful not to demand
1581 * more tuples than that.
1582 */
1583 ExecutorRun(queryDesc,
1585 fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed);
1586
1587 /* Shut down the executor */
1588 ExecutorFinish(queryDesc);
1589
1590 /* Report buffer/WAL usage during parallel execution. */
1591 buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1592 wal_usage = shm_toc_lookup(toc, PARALLEL_KEY_WAL_USAGE, false);
1594 &wal_usage[ParallelWorkerNumber]);
1595
1596 /* Report instrumentation data if any instrumentation options are set. */
1597 if (instrumentation != NULL)
1599 instrumentation);
1600
1601 /* Report JIT instrumentation data if any */
1602 if (queryDesc->estate->es_jit && jit_instrumentation != NULL)
1603 {
1605 jit_instrumentation->jit_instr[ParallelWorkerNumber] =
1606 queryDesc->estate->es_jit->instr;
1607 }
1608
1609 /* Must do this after capturing instrumentation. */
1610 ExecutorEnd(queryDesc);
1611
1612 /* Cleanup. */
1613 dsa_detach(area);
1614 FreeQueryDesc(queryDesc);
1615 receiver->rDestroy(receiver);
1616}
int ParallelWorkerNumber
Definition parallel.c:117
void InitializeParallelDSM(ParallelContext *pcxt)
Definition parallel.c:213
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition parallel.c:805
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition parallel.c:511
void DestroyParallelContext(ParallelContext *pcxt)
Definition parallel.c:959
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
Definition parallel.c:175
int64 pgstat_get_my_query_id(void)
void pgstat_report_activity(BackendState state, const char *cmd_str)
int64 pgstat_get_my_plan_id(void)
@ STATE_RUNNING
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
int bms_num_members(const Bitmapset *a)
Definition bitmapset.c:744
#define bms_is_empty(a)
Definition bitmapset.h:118
#define MAXALIGN(LEN)
Definition c.h:896
#define Assert(condition)
Definition c.h:943
int64_t int64
Definition c.h:621
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:558
int16_t int16
Definition c.h:619
#define OidIsValid(objectId)
Definition c.h:858
size_t Size
Definition c.h:689
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
Datum datumRestore(char **start_address, bool *isnull)
Definition datum.c:559
void datumSerialize(Datum value, bool isnull, bool typByVal, int typLen, char **start_address)
Definition datum.c:497
Size datumEstimateSpace(Datum value, bool isnull, bool typByVal, int typLen)
Definition datum.c:450
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition dsa.c:560
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition dsa.c:957
void dsa_detach(dsa_area *area)
Definition dsa.c:2002
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition dsa.c:841
size_t dsa_minimum_size(void)
Definition dsa.c:1246
uint64 dsa_pointer
Definition dsa.h:62
#define dsa_allocate(area, size)
Definition dsa.h:109
#define dsa_create_in_place(place, size, tranche_id, segment)
Definition dsa.h:122
#define InvalidDsaPointer
Definition dsa.h:78
#define DsaPointerIsValid(x)
Definition dsa.h:106
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
void ExecutorEnd(QueryDesc *queryDesc)
Definition execMain.c:477
void ExecutorFinish(QueryDesc *queryDesc)
Definition execMain.c:417
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition execMain.c:124
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
Definition execMain.c:308
#define PARALLEL_KEY_BUFFER_USAGE
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
#define PARALLEL_KEY_JIT_INSTRUMENTATION
#define PARALLEL_KEY_PARAMLISTINFO
#define PARALLEL_TUPLE_QUEUE_SIZE
static QueryDesc * ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver, int instrument_options)
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
void ExecParallelCleanup(ParallelExecutorInfo *pei)
#define PARALLEL_KEY_INSTRUMENTATION
static DestReceiver * ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
void ParallelQueryMain(dsm_segment *seg, shm_toc *toc)
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
#define PARALLEL_KEY_PLANNEDSTMT
static bool ExecParallelEstimate(PlanState *planstate, ExecParallelEstimateContext *e)
#define GetInstrumentationArray(sei)
void ExecParallelReinitialize(PlanState *planstate, ParallelExecutorInfo *pei, Bitmapset *sendParams)
#define PARALLEL_KEY_DSA
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
void ExecParallelCreateReaders(ParallelExecutorInfo *pei)
#define PARALLEL_KEY_TUPLE_QUEUE
#define PARALLEL_KEY_EXECUTOR_FIXED
static char * ExecSerializePlan(Plan *plan, EState *estate)
ParallelExecutorInfo * ExecInitParallelPlan(PlanState *planstate, EState *estate, Bitmapset *sendParams, int nworkers, int64 tuples_needed)
#define PARALLEL_KEY_QUERY_TEXT
static Size EstimateParamExecSpace(EState *estate, Bitmapset *params)
void ExecParallelFinish(ParallelExecutorInfo *pei)
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
#define PARALLEL_KEY_WAL_USAGE
static void ExecParallelRetrieveJitInstrumentation(PlanState *planstate, SharedJitInstrumentation *shared_jit)
static bool ExecParallelInitializeDSM(PlanState *planstate, ExecParallelInitializeDSMContext *d)
static void RestoreParamExecParams(char *start_address, EState *estate)
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
#define GetPerTupleExprContext(estate)
Definition executor.h:667
#define palloc0_object(type)
Definition fe_memutils.h:75
#define IsParallelWorker()
Definition parallel.h:62
void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition instrument.c:297
void InstrInitNode(NodeInstrumentation *instr, int instrument_options, bool async_mode)
Definition instrument.c:123
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition instrument.c:287
void InstrStartParallelQuery(void)
Definition instrument.c:279
void InstrEndLoop(NodeInstrumentation *instr)
Definition instrument.c:204
void InstrAggNode(NodeInstrumentation *dst, NodeInstrumentation *add)
Definition instrument.c:232
int i
Definition isn.c:77
void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
Definition jit.c:183
#define PGJIT_NONE
Definition jit.h:19
List * lappend(List *list, void *datum)
Definition list.c:339
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition lsyscache.c:2471
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1266
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc(Size size)
Definition mcxt.c:1387
void ExecAggEstimate(AggState *node, ParallelContext *pcxt)
Definition nodeAgg.c:4781
void ExecAggInitializeWorker(AggState *node, ParallelWorkerContext *pwcxt)
Definition nodeAgg.c:4827
void ExecAggRetrieveInstrumentation(AggState *node)
Definition nodeAgg.c:4840
void ExecAggInitializeDSM(AggState *node, ParallelContext *pcxt)
Definition nodeAgg.c:4802
void ExecAppendReInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition nodeAppend.c:541
void ExecAppendInitializeWorker(AppendState *node, ParallelWorkerContext *pwcxt)
Definition nodeAppend.c:557
void ExecAppendInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition nodeAppend.c:520
void ExecAppendEstimate(AppendState *node, ParallelContext *pcxt)
Definition nodeAppend.c:501
void ExecBitmapHeapInstrumentInitWorker(BitmapHeapScanState *node, ParallelWorkerContext *pwcxt)
void ExecBitmapHeapInstrumentInitDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecBitmapHeapInitializeWorker(BitmapHeapScanState *node, ParallelWorkerContext *pwcxt)
void ExecBitmapHeapEstimate(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecBitmapHeapRetrieveInstrumentation(BitmapHeapScanState *node)
void ExecBitmapHeapInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecBitmapHeapReInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecBitmapHeapInstrumentEstimate(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecBitmapIndexScanEstimate(BitmapIndexScanState *node, ParallelContext *pcxt)
void ExecBitmapIndexScanInitializeDSM(BitmapIndexScanState *node, ParallelContext *pcxt)
void ExecBitmapIndexScanRetrieveInstrumentation(BitmapIndexScanState *node)
void ExecBitmapIndexScanInitializeWorker(BitmapIndexScanState *node, ParallelWorkerContext *pwcxt)
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition nodeCustom.c:174
void ExecCustomScanEstimate(CustomScanState *node, ParallelContext *pcxt)
Definition nodeCustom.c:161
void ExecCustomScanReInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition nodeCustom.c:190
void ExecCustomScanInitializeWorker(CustomScanState *node, ParallelWorkerContext *pwcxt)
Definition nodeCustom.c:205
void ExecForeignScanInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecForeignScanReInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecForeignScanEstimate(ForeignScanState *node, ParallelContext *pcxt)
void ExecForeignScanInitializeWorker(ForeignScanState *node, ParallelWorkerContext *pwcxt)
#define planstate_tree_walker(ps, w, c)
Definition nodeFuncs.h:179
void ExecHashInitializeDSM(HashState *node, ParallelContext *pcxt)
Definition nodeHash.c:2838
void ExecHashInitializeWorker(HashState *node, ParallelWorkerContext *pwcxt)
Definition nodeHash.c:2863
void ExecHashEstimate(HashState *node, ParallelContext *pcxt)
Definition nodeHash.c:2819
void ExecHashRetrieveInstrumentation(HashState *node)
Definition nodeHash.c:2904
void ExecHashJoinInitializeDSM(HashJoinState *state, ParallelContext *pcxt)
void ExecHashJoinEstimate(HashJoinState *state, ParallelContext *pcxt)
void ExecHashJoinReInitializeDSM(HashJoinState *state, ParallelContext *pcxt)
void ExecHashJoinInitializeWorker(HashJoinState *state, ParallelWorkerContext *pwcxt)
void ExecIncrementalSortEstimate(IncrementalSortState *node, ParallelContext *pcxt)
void ExecIncrementalSortInitializeDSM(IncrementalSortState *node, ParallelContext *pcxt)
void ExecIncrementalSortRetrieveInstrumentation(IncrementalSortState *node)
void ExecIncrementalSortInitializeWorker(IncrementalSortState *node, ParallelWorkerContext *pwcxt)
void ExecIndexOnlyScanEstimate(IndexOnlyScanState *node, ParallelContext *pcxt)
void ExecIndexOnlyScanInstrumentInitWorker(IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
void ExecIndexOnlyScanRetrieveInstrumentation(IndexOnlyScanState *node)
void ExecIndexOnlyScanInstrumentInitDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
void ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
void ExecIndexOnlyScanInstrumentEstimate(IndexOnlyScanState *node, ParallelContext *pcxt)
void ExecIndexOnlyScanReInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
void ExecIndexOnlyScanInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
void ExecIndexScanRetrieveInstrumentation(IndexScanState *node)
void ExecIndexScanEstimate(IndexScanState *node, ParallelContext *pcxt)
void ExecIndexScanReInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
void ExecIndexScanInstrumentEstimate(IndexScanState *node, ParallelContext *pcxt)
void ExecIndexScanInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
void ExecIndexScanInstrumentInitWorker(IndexScanState *node, ParallelWorkerContext *pwcxt)
void ExecIndexScanInstrumentInitDSM(IndexScanState *node, ParallelContext *pcxt)
void ExecIndexScanInitializeWorker(IndexScanState *node, ParallelWorkerContext *pwcxt)
void ExecMemoizeInitializeDSM(MemoizeState *node, ParallelContext *pcxt)
void ExecMemoizeEstimate(MemoizeState *node, ParallelContext *pcxt)
void ExecMemoizeRetrieveInstrumentation(MemoizeState *node)
void ExecMemoizeInitializeWorker(MemoizeState *node, ParallelWorkerContext *pwcxt)
void ExecSeqScanRetrieveInstrumentation(SeqScanState *node)
void ExecSeqScanReInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
void ExecSeqScanInstrumentInitDSM(SeqScanState *node, ParallelContext *pcxt)
void ExecSeqScanInstrumentEstimate(SeqScanState *node, ParallelContext *pcxt)
void ExecSeqScanInitializeWorker(SeqScanState *node, ParallelWorkerContext *pwcxt)
void ExecSeqScanInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
void ExecSeqScanEstimate(SeqScanState *node, ParallelContext *pcxt)
void ExecSeqScanInstrumentInitWorker(SeqScanState *node, ParallelWorkerContext *pwcxt)
void ExecSortInitializeWorker(SortState *node, ParallelWorkerContext *pwcxt)
Definition nodeSort.c:462
void ExecSortEstimate(SortState *node, ParallelContext *pcxt)
Definition nodeSort.c:416
void ExecSortInitializeDSM(SortState *node, ParallelContext *pcxt)
Definition nodeSort.c:437
void ExecSortRetrieveInstrumentation(SortState *node)
Definition nodeSort.c:476
void ExecSetParamPlanMulti(const Bitmapset *params, ExprContext *econtext)
void ExecTidRangeScanInstrumentEstimate(TidRangeScanState *node, ParallelContext *pcxt)
void ExecTidRangeScanEstimate(TidRangeScanState *node, ParallelContext *pcxt)
void ExecTidRangeScanInstrumentInitDSM(TidRangeScanState *node, ParallelContext *pcxt)
void ExecTidRangeScanInitializeWorker(TidRangeScanState *node, ParallelWorkerContext *pwcxt)
void ExecTidRangeScanInitializeDSM(TidRangeScanState *node, ParallelContext *pcxt)
void ExecTidRangeScanReInitializeDSM(TidRangeScanState *node, ParallelContext *pcxt)
void ExecTidRangeScanRetrieveInstrumentation(TidRangeScanState *node)
void ExecTidRangeScanInstrumentInitWorker(TidRangeScanState *node, ParallelWorkerContext *pwcxt)
#define copyObject(obj)
Definition nodes.h:232
#define nodeTag(nodeptr)
Definition nodes.h:139
@ CMD_SELECT
Definition nodes.h:275
#define makeNode(_type_)
Definition nodes.h:161
char * nodeToString(const void *obj)
Definition outfuncs.c:811
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
Size EstimateParamListSpace(ParamListInfo paramLI)
Definition params.c:167
void SerializeParamList(ParamListInfo paramLI, char **start_address)
Definition params.c:228
ParamListInfo RestoreParamList(char **start_address)
Definition params.c:290
#define lfirst(lc)
Definition pg_list.h:172
#define lfirst_node(type, lc)
Definition pg_list.h:176
#define NIL
Definition pg_list.h:68
static Oid list_nth_oid(const List *list, int n)
Definition pg_list.h:353
#define plan(x)
Definition pg_regress.c:164
@ PLAN_STMT_INTERNAL
Definition plannodes.h:38
const char * debug_query_string
Definition postgres.c:94
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
void FreeQueryDesc(QueryDesc *qdesc)
Definition pquery.c:107
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition pquery.c:68
e
static int fb(int x)
void * stringToNode(const char *str)
Definition read.c:90
@ ForwardScanDirection
Definition sdir.h:28
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition shm_mq.c:226
shm_mq * shm_mq_create(void *address, Size size)
Definition shm_mq.c:179
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition shm_mq.c:321
void shm_mq_detach(shm_mq_handle *mqh)
Definition shm_mq.c:845
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition shm_mq.c:208
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition shm_mq.c:292
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition shm_toc.c:171
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition shm_toc.c:239
#define shm_toc_estimate_chunk(e, sz)
Definition shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition shm_toc.h:53
Size add_size(Size s1, Size s2)
Definition shmem.c:1048
Size mul_size(Size s1, Size s2)
Definition shmem.c:1063
Snapshot GetActiveSnapshot(void)
Definition snapmgr.c:800
#define InvalidSnapshot
Definition snapshot.h:119
PGPROC * MyProc
Definition proc.c:71
List * es_part_prune_infos
Definition execnodes.h:706
struct dsa_area * es_query_dsa
Definition execnodes.h:788
int es_top_eflags
Definition execnodes.h:755
struct JitContext * es_jit
Definition execnodes.h:800
int es_instrument
Definition execnodes.h:756
PlannedStmt * es_plannedstmt
Definition execnodes.h:705
struct JitInstrumentation * es_jit_worker_instr
Definition execnodes.h:801
ParamExecData * es_param_exec_vals
Definition execnodes.h:741
List * es_range_table
Definition execnodes.h:698
List * es_rteperminfos
Definition execnodes.h:704
Bitmapset * es_unpruned_relids
Definition execnodes.h:709
ParamListInfo es_param_list_info
Definition execnodes.h:740
MemoryContext es_query_cxt
Definition execnodes.h:746
int es_jit_flags
Definition execnodes.h:799
const char * es_sourceText
Definition execnodes.h:713
Snapshot es_snapshot
Definition execnodes.h:696
SharedExecutorInstrumentation * instrumentation
JitInstrumentation instr
Definition jit.h:62
dsm_segment * seg
Definition parallel.h:44
shm_toc_estimator estimator
Definition parallel.h:43
ParallelWorkerInfo * worker
Definition parallel.h:47
shm_toc * toc
Definition parallel.h:46
int nworkers_launched
Definition parallel.h:39
PlanState * planstate
struct SharedJitInstrumentation * jit_instrumentation
BufferUsage * buffer_usage
dsa_pointer param_exec
ParallelContext * pcxt
shm_mq_handle ** tqueue
SharedExecutorInstrumentation * instrumentation
struct TupleQueueReader ** reader
BackgroundWorkerHandle * bgwhandle
Definition parallel.h:29
struct SharedJitInstrumentation * worker_jit_instrument
Definition execnodes.h:1217
Plan * plan
Definition execnodes.h:1201
EState * state
Definition execnodes.h:1203
NodeInstrumentation * instrument
Definition execnodes.h:1211
WorkerNodeInstrumentation * worker_instrument
Definition execnodes.h:1213
bool parallel_aware
Definition plannodes.h:219
bool parallel_safe
Definition plannodes.h:221
int plan_node_id
Definition plannodes.h:233
struct Plan * planTree
Definition plannodes.h:99
bool hasModifyingCTE
Definition plannodes.h:81
List * appendRelations
Definition plannodes.h:124
List * permInfos
Definition plannodes.h:118
bool canSetTag
Definition plannodes.h:84
List * rowMarks
Definition plannodes.h:138
int64 planId
Definition plannodes.h:72
Bitmapset * rewindPlanIDs
Definition plannodes.h:135
int64 queryId
Definition plannodes.h:69
ParseLoc stmt_len
Definition plannodes.h:171
PlannedStmtOrigin planOrigin
Definition plannodes.h:75
bool hasReturning
Definition plannodes.h:78
ParseLoc stmt_location
Definition plannodes.h:169
Bitmapset * resultRelationRelids
Definition plannodes.h:121
List * invalItems
Definition plannodes.h:147
bool transientPlan
Definition plannodes.h:87
Bitmapset * rowMarkRelids
Definition plannodes.h:141
List * subplans
Definition plannodes.h:129
List * relationOids
Definition plannodes.h:144
bool dependsOnRole
Definition plannodes.h:90
Bitmapset * unprunableRelids
Definition plannodes.h:113
CmdType commandType
Definition plannodes.h:66
Node * utilityStmt
Definition plannodes.h:153
List * rtable
Definition plannodes.h:107
List * partPruneInfos
Definition plannodes.h:104
List * paramExecTypes
Definition plannodes.h:150
bool parallelModeNeeded
Definition plannodes.h:93
const char * sourceText
Definition execdesc.h:38
EState * estate
Definition execdesc.h:50
PlannedStmt * plannedstmt
Definition execdesc.h:37
PlanState * planstate
Definition execdesc.h:51
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER]
Definition jit.h:54
NodeInstrumentation instrument[FLEXIBLE_ARRAY_MEMBER]
Definition instrument.h:118
DestReceiver * CreateTupleQueueDestReceiver(shm_mq_handle *handle)
Definition tqueue.c:119
TupleQueueReader * CreateTupleQueueReader(shm_mq_handle *handle)
Definition tqueue.c:139
void DestroyTupleQueueReader(TupleQueueReader *reader)
Definition tqueue.c:155