PostgreSQL Source Code git master
Loading...
Searching...
No Matches
execParallel.c File Reference
Include dependency graph for execParallel.c:

Go to the source code of this file.

Data Structures

struct  FixedParallelExecutorState
 
struct  SharedExecutorInstrumentation
 
struct  ExecParallelEstimateContext
 
struct  ExecParallelInitializeDSMContext
 

Macros

#define PARALLEL_KEY_EXECUTOR_FIXED   UINT64CONST(0xE000000000000001)
 
#define PARALLEL_KEY_PLANNEDSTMT   UINT64CONST(0xE000000000000002)
 
#define PARALLEL_KEY_PARAMLISTINFO   UINT64CONST(0xE000000000000003)
 
#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xE000000000000004)
 
#define PARALLEL_KEY_TUPLE_QUEUE   UINT64CONST(0xE000000000000005)
 
#define PARALLEL_KEY_INSTRUMENTATION   UINT64CONST(0xE000000000000006)
 
#define PARALLEL_KEY_DSA   UINT64CONST(0xE000000000000007)
 
#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xE000000000000008)
 
#define PARALLEL_KEY_JIT_INSTRUMENTATION   UINT64CONST(0xE000000000000009)
 
#define PARALLEL_KEY_WAL_USAGE   UINT64CONST(0xE00000000000000A)
 
#define PARALLEL_TUPLE_QUEUE_SIZE   65536
 
#define GetInstrumentationArray(sei)
 

Typedefs

typedef struct FixedParallelExecutorState FixedParallelExecutorState
 
typedef struct ExecParallelEstimateContext ExecParallelEstimateContext
 
typedef struct ExecParallelInitializeDSMContext ExecParallelInitializeDSMContext
 

Functions

static charExecSerializePlan (Plan *plan, EState *estate)
 
static bool ExecParallelEstimate (PlanState *planstate, ExecParallelEstimateContext *e)
 
static bool ExecParallelInitializeDSM (PlanState *planstate, ExecParallelInitializeDSMContext *d)
 
static shm_mq_handle ** ExecParallelSetupTupleQueues (ParallelContext *pcxt, bool reinitialize)
 
static bool ExecParallelReInitializeDSM (PlanState *planstate, ParallelContext *pcxt)
 
static bool ExecParallelRetrieveInstrumentation (PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
 
static DestReceiverExecParallelGetReceiver (dsm_segment *seg, shm_toc *toc)
 
static Size EstimateParamExecSpace (EState *estate, Bitmapset *params)
 
static dsa_pointer SerializeParamExecParams (EState *estate, Bitmapset *params, dsa_area *area)
 
static void RestoreParamExecParams (char *start_address, EState *estate)
 
ParallelExecutorInfoExecInitParallelPlan (PlanState *planstate, EState *estate, Bitmapset *sendParams, int nworkers, int64 tuples_needed)
 
void ExecParallelCreateReaders (ParallelExecutorInfo *pei)
 
void ExecParallelReinitialize (PlanState *planstate, ParallelExecutorInfo *pei, Bitmapset *sendParams)
 
static void ExecParallelRetrieveJitInstrumentation (PlanState *planstate, SharedJitInstrumentation *shared_jit)
 
void ExecParallelFinish (ParallelExecutorInfo *pei)
 
void ExecParallelCleanup (ParallelExecutorInfo *pei)
 
static QueryDescExecParallelGetQueryDesc (shm_toc *toc, DestReceiver *receiver, int instrument_options)
 
static bool ExecParallelReportInstrumentation (PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
 
static bool ExecParallelInitializeWorker (PlanState *planstate, ParallelWorkerContext *pwcxt)
 
void ParallelQueryMain (dsm_segment *seg, shm_toc *toc)
 

Macro Definition Documentation

◆ GetInstrumentationArray

#define GetInstrumentationArray (   sei)
Value:
(NodeInstrumentation *) (((char *) sei) + sei->instrument_offset))
#define StaticAssertVariableIsOfTypeMacro(varname, typename)
Definition c.h:1071
static int fb(int x)

Definition at line 113 of file execParallel.c.

118{
119 ParallelContext *pcxt;
120 int nnodes;
122
123/* Context object for ExecParallelInitializeDSM. */
125{
128 int nnodes;
130
131/* Helper functions that run in the parallel leader. */
132static char *ExecSerializePlan(Plan *plan, EState *estate);
133static bool ExecParallelEstimate(PlanState *planstate,
135static bool ExecParallelInitializeDSM(PlanState *planstate,
138 bool reinitialize);
139static bool ExecParallelReInitializeDSM(PlanState *planstate,
140 ParallelContext *pcxt);
142 SharedExecutorInstrumentation *instrumentation);
143
144/* Helper function that runs in the parallel worker. */
146
147/*
148 * Create a serialized representation of the plan to be sent to each worker.
149 */
150static char *
152{
153 PlannedStmt *pstmt;
154 ListCell *lc;
155
156 /* We can't scribble on the original plan, so make a copy. */
158
159 /*
160 * The worker will start its own copy of the executor, and that copy will
161 * insert a junk filter if the toplevel node has any resjunk entries. We
162 * don't want that to happen, because while resjunk columns shouldn't be
163 * sent back to the user, here the tuples are coming back to another
164 * backend which may very well need them. So mutate the target list
165 * accordingly. This is sort of a hack; there might be better ways to do
166 * this...
167 */
168 foreach(lc, plan->targetlist)
169 {
171
172 tle->resjunk = false;
173 }
174
175 /*
176 * Create a dummy PlannedStmt. Most of the fields don't need to be valid
177 * for our purposes, but the worker will need at least a minimal
178 * PlannedStmt to start the executor.
179 */
180 pstmt = makeNode(PlannedStmt);
181 pstmt->commandType = CMD_SELECT;
183 pstmt->planId = pgstat_get_my_plan_id();
184 pstmt->hasReturning = false;
185 pstmt->hasModifyingCTE = false;
186 pstmt->canSetTag = true;
187 pstmt->transientPlan = false;
188 pstmt->dependsOnRole = false;
189 pstmt->parallelModeNeeded = false;
190 pstmt->planTree = plan;
191 pstmt->partPruneInfos = estate->es_part_prune_infos;
192 pstmt->rtable = estate->es_range_table;
193 pstmt->unprunableRelids = estate->es_unpruned_relids;
194 pstmt->permInfos = estate->es_rteperminfos;
195 pstmt->appendRelations = NIL;
197
198 /*
199 * Transfer only parallel-safe subplans, leaving a NULL "hole" in the list
200 * for unsafe ones (so that the list indexes of the safe ones are
201 * preserved). This positively ensures that the worker won't try to run,
202 * or even do ExecInitNode on, an unsafe subplan. That's important to
203 * protect, eg, non-parallel-aware FDWs from getting into trouble.
204 */
205 pstmt->subplans = NIL;
206 foreach(lc, estate->es_plannedstmt->subplans)
207 {
208 Plan *subplan = (Plan *) lfirst(lc);
209
210 if (subplan && !subplan->parallel_safe)
211 subplan = NULL;
212 pstmt->subplans = lappend(pstmt->subplans, subplan);
213 }
214
215 pstmt->rewindPlanIDs = NULL;
216 pstmt->rowMarks = NIL;
217
218 /*
219 * Pass the row mark and result relation relids to parallel workers. They
220 * may need to check them to inform heuristics.
221 */
224 pstmt->relationOids = NIL;
225 pstmt->invalItems = NIL; /* workers can't replan anyway... */
227 pstmt->utilityStmt = NULL;
228 pstmt->stmt_location = -1;
229 pstmt->stmt_len = -1;
230
231 /* Return serialized copy of our dummy PlannedStmt. */
232 return nodeToString(pstmt);
233}
234
235/*
236 * Parallel-aware plan nodes (and occasionally others) may need some state
237 * which is shared across all parallel workers. Before we size the DSM, give
238 * them a chance to call shm_toc_estimate_chunk or shm_toc_estimate_keys on
239 * &pcxt->estimator.
240 *
241 * While we're at it, count the number of PlanState nodes in the tree, so
242 * we know how many Instrumentation structures we need.
243 */
244static bool
246{
247 if (planstate == NULL)
248 return false;
249
250 /* Count this node. */
251 e->nnodes++;
252
253 switch (nodeTag(planstate))
254 {
255 case T_SeqScanState:
256 if (planstate->plan->parallel_aware)
257 ExecSeqScanEstimate((SeqScanState *) planstate,
258 e->pcxt);
259 /* even when not parallel-aware, for EXPLAIN ANALYZE */
261 e->pcxt);
262 break;
263 case T_IndexScanState:
264 if (planstate->plan->parallel_aware)
266 e->pcxt);
267 /* even when not parallel-aware, for EXPLAIN ANALYZE */
269 e->pcxt);
270 break;
272 if (planstate->plan->parallel_aware)
274 e->pcxt);
275 /* even when not parallel-aware, for EXPLAIN ANALYZE */
277 e->pcxt);
278 break;
280 /* even when not parallel-aware, for EXPLAIN ANALYZE */
282 e->pcxt);
283 break;
285 if (planstate->plan->parallel_aware)
287 e->pcxt);
288 break;
290 if (planstate->plan->parallel_aware)
292 e->pcxt);
293 /* even when not parallel-aware, for EXPLAIN ANALYZE */
295 e->pcxt);
296 break;
297 case T_AppendState:
298 if (planstate->plan->parallel_aware)
299 ExecAppendEstimate((AppendState *) planstate,
300 e->pcxt);
301 break;
303 if (planstate->plan->parallel_aware)
305 e->pcxt);
306 break;
308 if (planstate->plan->parallel_aware)
310 e->pcxt);
311 /* even when not parallel-aware, for EXPLAIN ANALYZE */
313 e->pcxt);
314 break;
315 case T_HashJoinState:
316 if (planstate->plan->parallel_aware)
318 e->pcxt);
319 break;
320 case T_HashState:
321 /* even when not parallel-aware, for EXPLAIN ANALYZE */
322 ExecHashEstimate((HashState *) planstate, e->pcxt);
323 break;
324 case T_SortState:
325 /* even when not parallel-aware, for EXPLAIN ANALYZE */
326 ExecSortEstimate((SortState *) planstate, e->pcxt);
327 break;
329 /* even when not parallel-aware, for EXPLAIN ANALYZE */
331 break;
332 case T_AggState:
333 /* even when not parallel-aware, for EXPLAIN ANALYZE */
334 ExecAggEstimate((AggState *) planstate, e->pcxt);
335 break;
336 case T_MemoizeState:
337 /* even when not parallel-aware, for EXPLAIN ANALYZE */
338 ExecMemoizeEstimate((MemoizeState *) planstate, e->pcxt);
339 break;
340 default:
341 break;
342 }
343
344 return planstate_tree_walker(planstate, ExecParallelEstimate, e);
345}
346
347/*
348 * Estimate the amount of space required to serialize the indicated parameters.
349 */
350static Size
352{
353 int paramid;
354 Size sz = sizeof(int);
355
356 paramid = -1;
357 while ((paramid = bms_next_member(params, paramid)) >= 0)
358 {
359 Oid typeOid;
360 int16 typLen;
361 bool typByVal;
363
364 prm = &(estate->es_param_exec_vals[paramid]);
365 typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
366 paramid);
367
368 sz = add_size(sz, sizeof(int)); /* space for paramid */
369
370 /* space for datum/isnull */
371 if (OidIsValid(typeOid))
372 get_typlenbyval(typeOid, &typLen, &typByVal);
373 else
374 {
375 /* If no type OID, assume by-value, like copyParamList does. */
376 typLen = sizeof(Datum);
377 typByVal = true;
378 }
379 sz = add_size(sz,
380 datumEstimateSpace(prm->value, prm->isnull,
381 typByVal, typLen));
382 }
383 return sz;
384}
385
386/*
387 * Serialize specified PARAM_EXEC parameters.
388 *
389 * We write the number of parameters first, as a 4-byte integer, and then
390 * write details for each parameter in turn. The details for each parameter
391 * consist of a 4-byte paramid (location of param in execution time internal
392 * parameter array) and then the datum as serialized by datumSerialize().
393 */
394static dsa_pointer
395SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
396{
397 Size size;
398 int nparams;
399 int paramid;
401 dsa_pointer handle;
402 char *start_address;
403
404 /* Allocate enough space for the current parameter values. */
405 size = EstimateParamExecSpace(estate, params);
406 handle = dsa_allocate(area, size);
407 start_address = dsa_get_address(area, handle);
408
409 /* First write the number of parameters as a 4-byte integer. */
410 nparams = bms_num_members(params);
411 memcpy(start_address, &nparams, sizeof(int));
412 start_address += sizeof(int);
413
414 /* Write details for each parameter in turn. */
415 paramid = -1;
416 while ((paramid = bms_next_member(params, paramid)) >= 0)
417 {
418 Oid typeOid;
419 int16 typLen;
420 bool typByVal;
421
422 prm = &(estate->es_param_exec_vals[paramid]);
423 typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
424 paramid);
425
426 /* Write paramid. */
427 memcpy(start_address, &paramid, sizeof(int));
428 start_address += sizeof(int);
429
430 /* Write datum/isnull */
431 if (OidIsValid(typeOid))
432 get_typlenbyval(typeOid, &typLen, &typByVal);
433 else
434 {
435 /* If no type OID, assume by-value, like copyParamList does. */
436 typLen = sizeof(Datum);
437 typByVal = true;
438 }
439 datumSerialize(prm->value, prm->isnull, typByVal, typLen,
441 }
442
443 return handle;
444}
445
446/*
447 * Restore specified PARAM_EXEC parameters.
448 */
449static void
451{
452 int nparams;
453 int i;
454 int paramid;
455
456 memcpy(&nparams, start_address, sizeof(int));
457 start_address += sizeof(int);
458
459 for (i = 0; i < nparams; i++)
460 {
462
463 /* Read paramid */
464 memcpy(&paramid, start_address, sizeof(int));
465 start_address += sizeof(int);
466 prm = &(estate->es_param_exec_vals[paramid]);
467
468 /* Read datum/isnull. */
469 prm->value = datumRestore(&start_address, &prm->isnull);
470 prm->execPlan = NULL;
471 }
472}
473
474/*
475 * Initialize the dynamic shared memory segment that will be used to control
476 * parallel execution.
477 */
478static bool
481{
482 if (planstate == NULL)
483 return false;
484
485 /* If instrumentation is enabled, initialize slot for this node. */
486 if (d->instrumentation != NULL)
488 planstate->plan->plan_node_id;
489
490 /* Count this node. */
491 d->nnodes++;
492
493 /*
494 * Call initializers for DSM-using plan nodes.
495 *
496 * Most plan nodes won't do anything here, but plan nodes that allocated
497 * DSM may need to initialize shared state in the DSM before parallel
498 * workers are launched. They can allocate the space they previously
499 * estimated using shm_toc_allocate, and add the keys they previously
500 * estimated using shm_toc_insert, in each case targeting pcxt->toc.
501 */
502 switch (nodeTag(planstate))
503 {
504 case T_SeqScanState:
505 if (planstate->plan->parallel_aware)
507 d->pcxt);
508 /* even when not parallel-aware, for EXPLAIN ANALYZE */
510 d->pcxt);
511 break;
512 case T_IndexScanState:
513 if (planstate->plan->parallel_aware)
515 d->pcxt);
516 /* even when not parallel-aware, for EXPLAIN ANALYZE */
518 d->pcxt);
519 break;
521 if (planstate->plan->parallel_aware)
523 d->pcxt);
524 /* even when not parallel-aware, for EXPLAIN ANALYZE */
526 d->pcxt);
527 break;
529 /* even when not parallel-aware, for EXPLAIN ANALYZE */
531 break;
533 if (planstate->plan->parallel_aware)
535 d->pcxt);
536 break;
538 if (planstate->plan->parallel_aware)
540 d->pcxt);
541 /* even when not parallel-aware, for EXPLAIN ANALYZE */
543 d->pcxt);
544 break;
545 case T_AppendState:
546 if (planstate->plan->parallel_aware)
548 d->pcxt);
549 break;
551 if (planstate->plan->parallel_aware)
553 d->pcxt);
554 break;
556 if (planstate->plan->parallel_aware)
558 d->pcxt);
559 /* even when not parallel-aware, for EXPLAIN ANALYZE */
561 d->pcxt);
562 break;
563 case T_HashJoinState:
564 if (planstate->plan->parallel_aware)
566 d->pcxt);
567 break;
568 case T_HashState:
569 /* even when not parallel-aware, for EXPLAIN ANALYZE */
570 ExecHashInitializeDSM((HashState *) planstate, d->pcxt);
571 break;
572 case T_SortState:
573 /* even when not parallel-aware, for EXPLAIN ANALYZE */
574 ExecSortInitializeDSM((SortState *) planstate, d->pcxt);
575 break;
577 /* even when not parallel-aware, for EXPLAIN ANALYZE */
579 break;
580 case T_AggState:
581 /* even when not parallel-aware, for EXPLAIN ANALYZE */
582 ExecAggInitializeDSM((AggState *) planstate, d->pcxt);
583 break;
584 case T_MemoizeState:
585 /* even when not parallel-aware, for EXPLAIN ANALYZE */
586 ExecMemoizeInitializeDSM((MemoizeState *) planstate, d->pcxt);
587 break;
588 default:
589 break;
590 }
591
593}
594
595/*
596 * It sets up the response queues for backend workers to return tuples
597 * to the main backend and start the workers.
598 */
599static shm_mq_handle **
601{
603 char *tqueuespace;
604 int i;
605
606 /* Skip this if no workers. */
607 if (pcxt->nworkers == 0)
608 return NULL;
609
610 /* Allocate memory for shared memory queue handles. */
612 palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
613
614 /*
615 * If not reinitializing, allocate space from the DSM for the queues;
616 * otherwise, find the already allocated space.
617 */
618 if (!reinitialize)
620 shm_toc_allocate(pcxt->toc,
622 pcxt->nworkers));
623 else
625
626 /* Create the queues, and become the receiver for each. */
627 for (i = 0; i < pcxt->nworkers; ++i)
628 {
629 shm_mq *mq;
630
634
636 responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
637 }
638
639 /* Add array of queues to shm_toc, so others can find it. */
640 if (!reinitialize)
642
643 /* Return array of handles. */
644 return responseq;
645}
646
647/*
648 * Sets up the required infrastructure for backend workers to perform
649 * execution and return results to the main backend.
650 */
652ExecInitParallelPlan(PlanState *planstate, EState *estate,
653 Bitmapset *sendParams, int nworkers,
654 int64 tuples_needed)
655{
657 ParallelContext *pcxt;
661 char *pstmt_data;
662 char *pstmt_space;
666 SharedExecutorInstrumentation *instrumentation = NULL;
667 SharedJitInstrumentation *jit_instrumentation = NULL;
668 int pstmt_len;
670 int instrumentation_len = 0;
672 int instrument_offset = 0;
674 char *query_string;
675 int query_len;
676
677 /*
678 * Force any initplan outputs that we're going to pass to workers to be
679 * evaluated, if they weren't already.
680 *
681 * For simplicity, we use the EState's per-output-tuple ExprContext here.
682 * That risks intra-query memory leakage, since we might pass through here
683 * many times before that ExprContext gets reset; but ExecSetParamPlan
684 * doesn't normally leak any memory in the context (see its comments), so
685 * it doesn't seem worth complicating this function's API to pass it a
686 * shorter-lived ExprContext. This might need to change someday.
687 */
689
690 /* Allocate object for return value. */
692 pei->finished = false;
693 pei->planstate = planstate;
694
695 /* Fix up and serialize plan to be sent to workers. */
696 pstmt_data = ExecSerializePlan(planstate->plan, estate);
697
698 /* Create a parallel context. */
699 pcxt = CreateParallelContext("postgres", "ParallelQueryMain", nworkers);
700 pei->pcxt = pcxt;
701
702 /*
703 * Before telling the parallel context to create a dynamic shared memory
704 * segment, we need to figure out how big it should be. Estimate space
705 * for the various things we need to store.
706 */
707
708 /* Estimate space for fixed-size state. */
712
713 /* Estimate space for query text. */
714 query_len = strlen(estate->es_sourceText);
715 shm_toc_estimate_chunk(&pcxt->estimator, query_len + 1);
717
718 /* Estimate space for serialized PlannedStmt. */
722
723 /* Estimate space for serialized ParamListInfo. */
727
728 /*
729 * Estimate space for BufferUsage.
730 *
731 * If EXPLAIN is not in use and there are no extensions loaded that care,
732 * we could skip this. But we have no way of knowing whether anyone's
733 * looking at pgBufferUsage, so do it unconditionally.
734 */
736 mul_size(sizeof(BufferUsage), pcxt->nworkers));
738
739 /*
740 * Same thing for WalUsage.
741 */
743 mul_size(sizeof(WalUsage), pcxt->nworkers));
745
746 /* Estimate space for tuple queues. */
750
751 /*
752 * Give parallel-aware nodes a chance to add to the estimates, and get a
753 * count of how many PlanState nodes there are.
754 */
755 e.pcxt = pcxt;
756 e.nnodes = 0;
757 ExecParallelEstimate(planstate, &e);
758
759 /* Estimate space for instrumentation, if required. */
760 if (estate->es_instrument)
761 {
764 sizeof(int) * e.nnodes;
766 instrument_offset = instrumentation_len;
769 mul_size(e.nnodes, nworkers));
772
773 /* Estimate space for JIT instrumentation, if required. */
774 if (estate->es_jit_flags != PGJIT_NONE)
775 {
778 sizeof(JitInstrumentation) * nworkers;
781 }
782 }
783
784 /* Estimate space for DSA area. */
787
788 /*
789 * InitializeParallelDSM() passes the active snapshot to the parallel
790 * worker, which uses it to set es_snapshot. Make sure we don't set
791 * es_snapshot differently in the child.
792 */
794
795 /* Everyone's had a chance to ask for space, so now create the DSM. */
797
798 /*
799 * OK, now we have a dynamic shared memory segment, and it should be big
800 * enough to store all of the data we estimated we would want to put into
801 * it, plus whatever general stuff (not specifically executor-related) the
802 * ParallelContext itself needs to store there. None of the space we
803 * asked for has been allocated or initialized yet, though, so do that.
804 */
805
806 /* Store fixed-size state. */
808 fpes->tuples_needed = tuples_needed;
809 fpes->param_exec = InvalidDsaPointer;
810 fpes->eflags = estate->es_top_eflags;
811 fpes->jit_flags = estate->es_jit_flags;
813
814 /* Store query string */
815 query_string = shm_toc_allocate(pcxt->toc, query_len + 1);
816 memcpy(query_string, estate->es_sourceText, query_len + 1);
817 shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string);
818
819 /* Store serialized PlannedStmt. */
823
824 /* Store serialized ParamListInfo. */
828
829 /* Allocate space for each worker's BufferUsage; no need to initialize. */
831 mul_size(sizeof(BufferUsage), pcxt->nworkers));
834
835 /* Same for WalUsage. */
837 mul_size(sizeof(WalUsage), pcxt->nworkers));
840
841 /* Set up the tuple queues that the workers will write into. */
842 pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
843
844 /* We don't need the TupleQueueReaders yet, though. */
845 pei->reader = NULL;
846
847 /*
848 * If instrumentation options were supplied, allocate space for the data.
849 * It only gets partially initialized here; the rest happens during
850 * ExecParallelInitializeDSM.
851 */
852 if (estate->es_instrument)
853 {
854 NodeInstrumentation *instrument;
855 int i;
856
857 instrumentation = shm_toc_allocate(pcxt->toc, instrumentation_len);
858 instrumentation->instrument_options = estate->es_instrument;
859 instrumentation->instrument_offset = instrument_offset;
860 instrumentation->num_workers = nworkers;
861 instrumentation->num_plan_nodes = e.nnodes;
862 instrument = GetInstrumentationArray(instrumentation);
863 for (i = 0; i < nworkers * e.nnodes; ++i)
864 InstrInitNode(&instrument[i], estate->es_instrument, false);
866 instrumentation);
867 pei->instrumentation = instrumentation;
868
869 if (estate->es_jit_flags != PGJIT_NONE)
870 {
871 jit_instrumentation = shm_toc_allocate(pcxt->toc,
873 jit_instrumentation->num_workers = nworkers;
874 memset(jit_instrumentation->jit_instr, 0,
875 sizeof(JitInstrumentation) * nworkers);
877 jit_instrumentation);
878 pei->jit_instrumentation = jit_instrumentation;
879 }
880 }
881
882 /*
883 * Create a DSA area that can be used by the leader and all workers.
884 * (However, if we failed to create a DSM and are using private memory
885 * instead, then skip this.)
886 */
887 if (pcxt->seg != NULL)
888 {
889 char *area_space;
890
895 pcxt->seg);
896
897 /*
898 * Serialize parameters, if any, using DSA storage. We don't dare use
899 * the main parallel query DSM for this because we might relaunch
900 * workers after the values have changed (and thus the amount of
901 * storage required has changed).
902 */
904 {
906 pei->area);
907 fpes->param_exec = pei->param_exec;
908 }
909 }
910
911 /*
912 * Give parallel-aware nodes a chance to initialize their shared data.
913 * This also initializes the elements of instrumentation->ps_instrument,
914 * if it exists.
915 */
916 d.pcxt = pcxt;
917 d.instrumentation = instrumentation;
918 d.nnodes = 0;
919
920 /* Install our DSA area while initializing the plan. */
921 estate->es_query_dsa = pei->area;
922 ExecParallelInitializeDSM(planstate, &d);
923 estate->es_query_dsa = NULL;
924
925 /*
926 * Make sure that the world hasn't shifted under our feet. This could
927 * probably just be an Assert(), but let's be conservative for now.
928 */
929 if (e.nnodes != d.nnodes)
930 elog(ERROR, "inconsistent count of PlanState nodes");
931
932 /* OK, we're ready to rock and roll. */
933 return pei;
934}
935
936/*
937 * Set up tuple queue readers to read the results of a parallel subplan.
938 *
939 * This is separate from ExecInitParallelPlan() because we can launch the
940 * worker processes and let them start doing something before we do this.
941 */
942void
944{
945 int nworkers = pei->pcxt->nworkers_launched;
946 int i;
947
948 Assert(pei->reader == NULL);
949
950 if (nworkers > 0)
951 {
952 pei->reader = (TupleQueueReader **)
953 palloc(nworkers * sizeof(TupleQueueReader *));
954
955 for (i = 0; i < nworkers; i++)
956 {
958 pei->pcxt->worker[i].bgwhandle);
959 pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
960 }
961 }
962}
963
964/*
965 * Re-initialize the parallel executor shared memory state before launching
966 * a fresh batch of workers.
967 */
968void
972{
973 EState *estate = planstate->state;
975
976 /* Old workers must already be shut down */
977 Assert(pei->finished);
978
979 /*
980 * Force any initplan outputs that we're going to pass to workers to be
981 * evaluated, if they weren't already (see comments in
982 * ExecInitParallelPlan).
983 */
985
987 pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
988 pei->reader = NULL;
989 pei->finished = false;
990
992
993 /* Free any serialized parameters from the last round. */
994 if (DsaPointerIsValid(fpes->param_exec))
995 {
996 dsa_free(pei->area, fpes->param_exec);
997 fpes->param_exec = InvalidDsaPointer;
998 }
999
1000 /* Serialize current parameter values if required. */
1002 {
1004 pei->area);
1005 fpes->param_exec = pei->param_exec;
1006 }
1007
1008 /* Traverse plan tree and let each child node reset associated state. */
1009 estate->es_query_dsa = pei->area;
1010 ExecParallelReInitializeDSM(planstate, pei->pcxt);
1011 estate->es_query_dsa = NULL;
1012}
1013
1014/*
1015 * Traverse plan tree to reinitialize per-node dynamic shared memory state
1016 */
1017static bool
1019 ParallelContext *pcxt)
1020{
1021 if (planstate == NULL)
1022 return false;
1023
1024 /*
1025 * Call reinitializers for DSM-using plan nodes.
1026 */
1027 switch (nodeTag(planstate))
1028 {
1029 case T_SeqScanState:
1030 if (planstate->plan->parallel_aware)
1032 pcxt);
1033 break;
1034 case T_IndexScanState:
1035 if (planstate->plan->parallel_aware)
1037 pcxt);
1038 break;
1040 if (planstate->plan->parallel_aware)
1042 pcxt);
1043 break;
1044 case T_ForeignScanState:
1045 if (planstate->plan->parallel_aware)
1047 pcxt);
1048 break;
1050 if (planstate->plan->parallel_aware)
1052 pcxt);
1053 break;
1054 case T_AppendState:
1055 if (planstate->plan->parallel_aware)
1056 ExecAppendReInitializeDSM((AppendState *) planstate, pcxt);
1057 break;
1058 case T_CustomScanState:
1059 if (planstate->plan->parallel_aware)
1061 pcxt);
1062 break;
1064 if (planstate->plan->parallel_aware)
1066 pcxt);
1067 break;
1068 case T_HashJoinState:
1069 if (planstate->plan->parallel_aware)
1071 pcxt);
1072 break;
1074 case T_HashState:
1075 case T_SortState:
1077 case T_MemoizeState:
1078 /* these nodes have DSM state, but no reinitialization is required */
1079 break;
1080
1081 default:
1082 break;
1083 }
1084
1085 return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt);
1086}
1087
1088/*
1089 * Copy instrumentation information about this node and its descendants from
1090 * dynamic shared memory.
1091 */
1092static bool
1094 SharedExecutorInstrumentation *instrumentation)
1095{
1096 NodeInstrumentation *instrument;
1097 int i;
1098 int n;
1099 int ibytes;
1100 int plan_node_id = planstate->plan->plan_node_id;
1101 MemoryContext oldcontext;
1102
1103 /* Find the instrumentation for this node. */
1104 for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1105 if (instrumentation->plan_node_id[i] == plan_node_id)
1106 break;
1107 if (i >= instrumentation->num_plan_nodes)
1108 elog(ERROR, "plan node %d not found", plan_node_id);
1109
1110 /* Accumulate the statistics from all workers. */
1111 instrument = GetInstrumentationArray(instrumentation);
1112 instrument += i * instrumentation->num_workers;
1113 for (n = 0; n < instrumentation->num_workers; ++n)
1114 InstrAggNode(planstate->instrument, &instrument[n]);
1115
1116 /*
1117 * Also store the per-worker detail.
1118 *
1119 * Worker instrumentation should be allocated in the same context as the
1120 * regular instrumentation information, which is the per-query context.
1121 * Switch into per-query memory context.
1122 */
1123 oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
1124 ibytes = mul_size(instrumentation->num_workers, sizeof(NodeInstrumentation));
1125 planstate->worker_instrument =
1127 MemoryContextSwitchTo(oldcontext);
1128
1129 planstate->worker_instrument->num_workers = instrumentation->num_workers;
1130 memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
1131
1132 /* Perform any node-type-specific work that needs to be done. */
1133 switch (nodeTag(planstate))
1134 {
1135 case T_IndexScanState:
1137 break;
1140 break;
1143 break;
1144 case T_SortState:
1146 break;
1149 break;
1150 case T_HashState:
1152 break;
1153 case T_AggState:
1155 break;
1156 case T_MemoizeState:
1158 break;
1161 break;
1162 case T_SeqScanState:
1164 break;
1167 break;
1168 default:
1169 break;
1170 }
1171
1173 instrumentation);
1174}
1175
1176/*
1177 * Add up the workers' JIT instrumentation from dynamic shared memory.
1178 */
1179static void
1182{
1184 int ibytes;
1185
1186 int n;
1187
1188 /*
1189 * Accumulate worker JIT instrumentation into the combined JIT
1190 * instrumentation, allocating it if required.
1191 */
1192 if (!planstate->state->es_jit_worker_instr)
1193 planstate->state->es_jit_worker_instr =
1195 combined = planstate->state->es_jit_worker_instr;
1196
1197 /* Accumulate all the workers' instrumentations. */
1198 for (n = 0; n < shared_jit->num_workers; ++n)
1199 InstrJitAgg(combined, &shared_jit->jit_instr[n]);
1200
1201 /*
1202 * Store the per-worker detail.
1203 *
1204 * Similar to ExecParallelRetrieveInstrumentation(), allocate the
1205 * instrumentation in per-query context.
1206 */
1208 + mul_size(shared_jit->num_workers, sizeof(JitInstrumentation));
1209 planstate->worker_jit_instrument =
1211
1213}
1214
1215/*
1216 * Finish parallel execution. We wait for parallel workers to finish, and
1217 * accumulate their buffer/WAL usage.
1218 */
1219void
1221{
1222 int nworkers = pei->pcxt->nworkers_launched;
1223 int i;
1224
1225 /* Make this be a no-op if called twice in a row. */
1226 if (pei->finished)
1227 return;
1228
1229 /*
1230 * Detach from tuple queues ASAP, so that any still-active workers will
1231 * notice that no further results are wanted.
1232 */
1233 if (pei->tqueue != NULL)
1234 {
1235 for (i = 0; i < nworkers; i++)
1236 shm_mq_detach(pei->tqueue[i]);
1237 pfree(pei->tqueue);
1238 pei->tqueue = NULL;
1239 }
1240
1241 /*
1242 * While we're waiting for the workers to finish, let's get rid of the
1243 * tuple queue readers. (Any other local cleanup could be done here too.)
1244 */
1245 if (pei->reader != NULL)
1246 {
1247 for (i = 0; i < nworkers; i++)
1249 pfree(pei->reader);
1250 pei->reader = NULL;
1251 }
1252
1253 /* Now wait for the workers to finish. */
1255
1256 /*
1257 * Next, accumulate buffer/WAL usage. (This must wait for the workers to
1258 * finish, or we might get incomplete data.)
1259 */
1260 for (i = 0; i < nworkers; i++)
1262
1263 pei->finished = true;
1264}
1265
1266/*
1267 * Accumulate instrumentation, and then clean up whatever ParallelExecutorInfo
1268 * resources still exist after ExecParallelFinish. We separate these
1269 * routines because someone might want to examine the contents of the DSM
1270 * after ExecParallelFinish and before calling this routine.
1271 */
1272void
1274{
1275 /* Accumulate instrumentation, if any. */
1276 if (pei->instrumentation)
1278 pei->instrumentation);
1279
1280 /* Accumulate JIT instrumentation, if any. */
1281 if (pei->jit_instrumentation)
1283 pei->jit_instrumentation);
1284
1285 /* Free any serialized parameters. */
1286 if (DsaPointerIsValid(pei->param_exec))
1287 {
1288 dsa_free(pei->area, pei->param_exec);
1290 }
1291 if (pei->area != NULL)
1292 {
1293 dsa_detach(pei->area);
1294 pei->area = NULL;
1295 }
1296 if (pei->pcxt != NULL)
1297 {
1299 pei->pcxt = NULL;
1300 }
1301 pfree(pei);
1302}
1303
1304/*
1305 * Create a DestReceiver to write tuples we produce to the shm_mq designated
1306 * for that purpose.
1307 */
1308static DestReceiver *
1310{
1311 char *mqspace;
1312 shm_mq *mq;
1313
1316 mq = (shm_mq *) mqspace;
1319}
1320
1321/*
1322 * Create a QueryDesc for the PlannedStmt we are to execute, and return it.
1323 */
1324static QueryDesc *
1326 int instrument_options)
1327{
1328 char *pstmtspace;
1329 char *paramspace;
1330 PlannedStmt *pstmt;
1331 ParamListInfo paramLI;
1332 char *queryString;
1333
1334 /* Get the query string from shared memory */
1335 queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1336
1337 /* Reconstruct leader-supplied PlannedStmt. */
1339 pstmt = (PlannedStmt *) stringToNode(pstmtspace);
1340
1341 /* Reconstruct ParamListInfo. */
1343 paramLI = RestoreParamList(&paramspace);
1344
1345 /* Create a QueryDesc for the query. */
1346 return CreateQueryDesc(pstmt,
1347 queryString,
1349 receiver, paramLI, NULL, instrument_options);
1350}
1351
1352/*
1353 * Copy instrumentation information from this node and its descendants into
1354 * dynamic shared memory, so that the parallel leader can retrieve it.
1355 */
1356static bool
1358 SharedExecutorInstrumentation *instrumentation)
1359{
1360 int i;
1361 int plan_node_id = planstate->plan->plan_node_id;
1362 NodeInstrumentation *instrument;
1363
1364 InstrEndLoop(planstate->instrument);
1365
1366 /*
1367 * If we shuffled the plan_node_id values in ps_instrument into sorted
1368 * order, we could use binary search here. This might matter someday if
1369 * we're pushing down sufficiently large plan trees. For now, do it the
1370 * slow, dumb way.
1371 */
1372 for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1373 if (instrumentation->plan_node_id[i] == plan_node_id)
1374 break;
1375 if (i >= instrumentation->num_plan_nodes)
1376 elog(ERROR, "plan node %d not found", plan_node_id);
1377
1378 /*
1379 * Add our statistics to the per-node, per-worker totals. It's possible
1380 * that this could happen more than once if we relaunched workers.
1381 */
1382 instrument = GetInstrumentationArray(instrumentation);
1383 instrument += i * instrumentation->num_workers;
1386 InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
1387
1389 instrumentation);
1390}
1391
1392/*
1393 * Initialize the PlanState and its descendants with the information
1394 * retrieved from shared memory. This has to be done once the PlanState
1395 * is allocated and initialized by executor; that is, after ExecutorStart().
1396 */
1397static bool
1399{
1400 if (planstate == NULL)
1401 return false;
1402
1403 switch (nodeTag(planstate))
1404 {
1405 case T_SeqScanState:
1406 if (planstate->plan->parallel_aware)
1408 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1410 break;
1411 case T_IndexScanState:
1412 if (planstate->plan->parallel_aware)
1414 pwcxt);
1415 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1417 pwcxt);
1418 break;
1420 if (planstate->plan->parallel_aware)
1422 pwcxt);
1423 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1425 pwcxt);
1426 break;
1428 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1430 pwcxt);
1431 break;
1432 case T_ForeignScanState:
1433 if (planstate->plan->parallel_aware)
1435 pwcxt);
1436 break;
1438 if (planstate->plan->parallel_aware)
1440 pwcxt);
1441 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1443 pwcxt);
1444 break;
1445 case T_AppendState:
1446 if (planstate->plan->parallel_aware)
1448 break;
1449 case T_CustomScanState:
1450 if (planstate->plan->parallel_aware)
1452 pwcxt);
1453 break;
1455 if (planstate->plan->parallel_aware)
1457 pwcxt);
1458 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1460 pwcxt);
1461 break;
1462 case T_HashJoinState:
1463 if (planstate->plan->parallel_aware)
1465 pwcxt);
1466 break;
1467 case T_HashState:
1468 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1470 break;
1471 case T_SortState:
1472 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1474 break;
1476 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1478 pwcxt);
1479 break;
1480 case T_AggState:
1481 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1482 ExecAggInitializeWorker((AggState *) planstate, pwcxt);
1483 break;
1484 case T_MemoizeState:
1485 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1487 break;
1488 default:
1489 break;
1490 }
1491
1493 pwcxt);
1494}
1495
1496/*
1497 * Main entrypoint for parallel query worker processes.
1498 *
1499 * We reach this function from ParallelWorkerMain, so the setup necessary to
1500 * create a sensible parallel environment has already been done;
1501 * ParallelWorkerMain worries about stuff like the transaction state, combo
1502 * CID mappings, and GUC values, so we don't need to deal with any of that
1503 * here.
1504 *
1505 * Our job is to deal with concerns specific to the executor. The parallel
1506 * group leader will have stored a serialized PlannedStmt, and it's our job
1507 * to execute that plan and write the resulting tuples to the appropriate
1508 * tuple queue. Various bits of supporting information that we need in order
1509 * to do this are also stored in the dsm_segment and can be accessed through
1510 * the shm_toc.
1511 */
1512void
1514{
1516 BufferUsage *buffer_usage;
1517 WalUsage *wal_usage;
1519 QueryDesc *queryDesc;
1520 SharedExecutorInstrumentation *instrumentation;
1521 SharedJitInstrumentation *jit_instrumentation;
1522 int instrument_options = 0;
1523 void *area_space;
1524 dsa_area *area;
1526
1527 /* Get fixed-size state. */
1529
1530 /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1532 instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1533 if (instrumentation != NULL)
1534 instrument_options = instrumentation->instrument_options;
1535 jit_instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
1536 true);
1537 queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1538
1539 /* Setting debug_query_string for individual workers */
1540 debug_query_string = queryDesc->sourceText;
1541
1542 /* Report workers' query for monitoring purposes */
1544
1545 /* Attach to the dynamic shared memory area. */
1547 area = dsa_attach_in_place(area_space, seg);
1548
1549 /* Start up the executor */
1550 queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1551 ExecutorStart(queryDesc, fpes->eflags);
1552
1553 /* Special executor initialization steps for parallel workers */
1554 queryDesc->planstate->state->es_query_dsa = area;
1555 if (DsaPointerIsValid(fpes->param_exec))
1556 {
1557 char *paramexec_space;
1558
1559 paramexec_space = dsa_get_address(area, fpes->param_exec);
1561 }
1562 pwcxt.toc = toc;
1563 pwcxt.seg = seg;
1565
1566 /* Pass down any tuple bound */
1567 ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1568
1569 /*
1570 * Prepare to track buffer/WAL usage during query execution.
1571 *
1572 * We do this after starting up the executor to match what happens in the
1573 * leader, which also doesn't count buffer accesses and WAL activity that
1574 * occur during executor startup.
1575 */
1577
1578 /*
1579 * Run the plan. If we specified a tuple bound, be careful not to demand
1580 * more tuples than that.
1581 */
1582 ExecutorRun(queryDesc,
1584 fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed);
1585
1586 /* Shut down the executor */
1587 ExecutorFinish(queryDesc);
1588
1589 /* Report buffer/WAL usage during parallel execution. */
1590 buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1591 wal_usage = shm_toc_lookup(toc, PARALLEL_KEY_WAL_USAGE, false);
1593 &wal_usage[ParallelWorkerNumber]);
1594
1595 /* Report instrumentation data if any instrumentation options are set. */
1596 if (instrumentation != NULL)
1598 instrumentation);
1599
1600 /* Report JIT instrumentation data if any */
1601 if (queryDesc->estate->es_jit && jit_instrumentation != NULL)
1602 {
1604 jit_instrumentation->jit_instr[ParallelWorkerNumber] =
1605 queryDesc->estate->es_jit->instr;
1606 }
1607
1608 /* Must do this after capturing instrumentation. */
1609 ExecutorEnd(queryDesc);
1610
1611 /* Cleanup. */
1612 dsa_detach(area);
1613 FreeQueryDesc(queryDesc);
1614 receiver->rDestroy(receiver);
1615}
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
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
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

◆ PARALLEL_KEY_BUFFER_USAGE

#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xE000000000000004)

Definition at line 63 of file execParallel.c.

◆ PARALLEL_KEY_DSA

#define PARALLEL_KEY_DSA   UINT64CONST(0xE000000000000007)

Definition at line 66 of file execParallel.c.

◆ PARALLEL_KEY_EXECUTOR_FIXED

#define PARALLEL_KEY_EXECUTOR_FIXED   UINT64CONST(0xE000000000000001)

Definition at line 60 of file execParallel.c.

◆ PARALLEL_KEY_INSTRUMENTATION

#define PARALLEL_KEY_INSTRUMENTATION   UINT64CONST(0xE000000000000006)

Definition at line 65 of file execParallel.c.

◆ PARALLEL_KEY_JIT_INSTRUMENTATION

#define PARALLEL_KEY_JIT_INSTRUMENTATION   UINT64CONST(0xE000000000000009)

Definition at line 68 of file execParallel.c.

◆ PARALLEL_KEY_PARAMLISTINFO

#define PARALLEL_KEY_PARAMLISTINFO   UINT64CONST(0xE000000000000003)

Definition at line 62 of file execParallel.c.

◆ PARALLEL_KEY_PLANNEDSTMT

#define PARALLEL_KEY_PLANNEDSTMT   UINT64CONST(0xE000000000000002)

Definition at line 61 of file execParallel.c.

◆ PARALLEL_KEY_QUERY_TEXT

#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xE000000000000008)

Definition at line 67 of file execParallel.c.

◆ PARALLEL_KEY_TUPLE_QUEUE

#define PARALLEL_KEY_TUPLE_QUEUE   UINT64CONST(0xE000000000000005)

Definition at line 64 of file execParallel.c.

◆ PARALLEL_KEY_WAL_USAGE

#define PARALLEL_KEY_WAL_USAGE   UINT64CONST(0xE00000000000000A)

Definition at line 69 of file execParallel.c.

◆ PARALLEL_TUPLE_QUEUE_SIZE

#define PARALLEL_TUPLE_QUEUE_SIZE   65536

Definition at line 71 of file execParallel.c.

Typedef Documentation

◆ ExecParallelEstimateContext

◆ ExecParallelInitializeDSMContext

◆ FixedParallelExecutorState

Function Documentation

◆ EstimateParamExecSpace()

static Size EstimateParamExecSpace ( EState estate,
Bitmapset params 
)
static

Definition at line 352 of file execParallel.c.

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}

References add_size(), bms_next_member(), datumEstimateSpace(), EState::es_param_exec_vals, EState::es_plannedstmt, fb(), get_typlenbyval(), list_nth_oid(), OidIsValid, and PlannedStmt::paramExecTypes.

Referenced by SerializeParamExecParams().

◆ ExecInitParallelPlan()

ParallelExecutorInfo * ExecInitParallelPlan ( PlanState planstate,
EState estate,
Bitmapset sendParams,
int  nworkers,
int64  tuples_needed 
)

Definition at line 653 of file execParallel.c.

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}

References ParallelExecutorInfo::area, Assert, bms_is_empty, ParallelExecutorInfo::buffer_usage, CreateParallelContext(), dsa_create_in_place, dsa_minimum_size(), elog, ERROR, EState::es_instrument, EState::es_jit_flags, EState::es_param_list_info, EState::es_query_dsa, EState::es_snapshot, EState::es_sourceText, EState::es_top_eflags, EstimateParamListSpace(), ParallelContext::estimator, ExecParallelEstimate(), ExecParallelInitializeDSM(), ExecParallelSetupTupleQueues(), ExecSerializePlan(), ExecSetParamPlanMulti(), fb(), ParallelExecutorInfo::finished, GetActiveSnapshot(), GetInstrumentationArray, GetPerTupleExprContext, i, InitializeParallelDSM(), InstrInitNode(), SharedExecutorInstrumentation::instrument_offset, SharedExecutorInstrumentation::instrument_options, ExecParallelInitializeDSMContext::instrumentation, ParallelExecutorInfo::instrumentation, InvalidDsaPointer, SharedJitInstrumentation::jit_instr, ParallelExecutorInfo::jit_instrumentation, MAXALIGN, memcpy(), mul_size(), ExecParallelInitializeDSMContext::nnodes, SharedExecutorInstrumentation::num_plan_nodes, SharedExecutorInstrumentation::num_workers, SharedJitInstrumentation::num_workers, ParallelContext::nworkers, palloc0_object, PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_DSA, PARALLEL_KEY_EXECUTOR_FIXED, PARALLEL_KEY_INSTRUMENTATION, PARALLEL_KEY_JIT_INSTRUMENTATION, PARALLEL_KEY_PARAMLISTINFO, PARALLEL_KEY_PLANNEDSTMT, PARALLEL_KEY_QUERY_TEXT, PARALLEL_KEY_WAL_USAGE, PARALLEL_TUPLE_QUEUE_SIZE, ParallelExecutorInfo::param_exec, ExecParallelInitializeDSMContext::pcxt, ParallelExecutorInfo::pcxt, PGJIT_NONE, PlanState::plan, ParallelExecutorInfo::planstate, ParallelExecutorInfo::reader, ParallelContext::seg, SerializeParamExecParams(), SerializeParamList(), shm_toc_allocate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_insert(), ParallelContext::toc, ParallelExecutorInfo::tqueue, and ParallelExecutorInfo::wal_usage.

Referenced by ExecGather(), and ExecGatherMerge().

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

Definition at line 1274 of file execParallel.c.

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}

References ParallelExecutorInfo::area, DestroyParallelContext(), dsa_detach(), dsa_free(), DsaPointerIsValid, ExecParallelRetrieveInstrumentation(), ExecParallelRetrieveJitInstrumentation(), fb(), ParallelExecutorInfo::instrumentation, InvalidDsaPointer, ParallelExecutorInfo::jit_instrumentation, ParallelExecutorInfo::param_exec, ParallelExecutorInfo::pcxt, pfree(), and ParallelExecutorInfo::planstate.

Referenced by ExecShutdownGather(), and ExecShutdownGatherMerge().

◆ ExecParallelCreateReaders()

void ExecParallelCreateReaders ( ParallelExecutorInfo pei)

Definition at line 944 of file execParallel.c.

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}

References Assert, ParallelWorkerInfo::bgwhandle, CreateTupleQueueReader(), fb(), i, ParallelContext::nworkers_launched, palloc(), ParallelExecutorInfo::pcxt, ParallelExecutorInfo::reader, shm_mq_set_handle(), ParallelExecutorInfo::tqueue, and ParallelContext::worker.

Referenced by ExecGather(), and ExecGatherMerge().

◆ ExecParallelEstimate()

static bool ExecParallelEstimate ( PlanState planstate,
ExecParallelEstimateContext e 
)
static

Definition at line 246 of file execParallel.c.

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}

References ExecAggEstimate(), ExecAppendEstimate(), ExecBitmapHeapEstimate(), ExecBitmapHeapInstrumentEstimate(), ExecBitmapIndexScanEstimate(), ExecCustomScanEstimate(), ExecForeignScanEstimate(), ExecHashEstimate(), ExecHashJoinEstimate(), ExecIncrementalSortEstimate(), ExecIndexOnlyScanEstimate(), ExecIndexOnlyScanInstrumentEstimate(), ExecIndexScanEstimate(), ExecIndexScanInstrumentEstimate(), ExecMemoizeEstimate(), ExecParallelEstimate(), ExecSeqScanEstimate(), ExecSeqScanInstrumentEstimate(), ExecSortEstimate(), ExecTidRangeScanEstimate(), ExecTidRangeScanInstrumentEstimate(), fb(), nodeTag, Plan::parallel_aware, PlanState::plan, and planstate_tree_walker.

Referenced by ExecInitParallelPlan(), and ExecParallelEstimate().

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

Definition at line 1221 of file execParallel.c.

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}

References ParallelExecutorInfo::buffer_usage, DestroyTupleQueueReader(), fb(), ParallelExecutorInfo::finished, i, InstrAccumParallelQuery(), ParallelContext::nworkers_launched, ParallelExecutorInfo::pcxt, pfree(), ParallelExecutorInfo::reader, shm_mq_detach(), ParallelExecutorInfo::tqueue, WaitForParallelWorkersToFinish(), and ParallelExecutorInfo::wal_usage.

Referenced by ExecShutdownGatherMergeWorkers(), and ExecShutdownGatherWorkers().

◆ ExecParallelGetQueryDesc()

static QueryDesc * ExecParallelGetQueryDesc ( shm_toc toc,
DestReceiver receiver,
int  instrument_options 
)
static

Definition at line 1326 of file execParallel.c.

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}

References CreateQueryDesc(), fb(), GetActiveSnapshot(), InvalidSnapshot, PARALLEL_KEY_PARAMLISTINFO, PARALLEL_KEY_PLANNEDSTMT, PARALLEL_KEY_QUERY_TEXT, RestoreParamList(), shm_toc_lookup(), and stringToNode().

Referenced by ParallelQueryMain().

◆ ExecParallelGetReceiver()

◆ ExecParallelInitializeDSM()

static bool ExecParallelInitializeDSM ( PlanState planstate,
ExecParallelInitializeDSMContext d 
)
static

Definition at line 480 of file execParallel.c.

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}

References ExecAggInitializeDSM(), ExecAppendInitializeDSM(), ExecBitmapHeapInitializeDSM(), ExecBitmapHeapInstrumentInitDSM(), ExecBitmapIndexScanInitializeDSM(), ExecCustomScanInitializeDSM(), ExecForeignScanInitializeDSM(), ExecHashInitializeDSM(), ExecHashJoinInitializeDSM(), ExecIncrementalSortInitializeDSM(), ExecIndexOnlyScanInitializeDSM(), ExecIndexOnlyScanInstrumentInitDSM(), ExecIndexScanInitializeDSM(), ExecIndexScanInstrumentInitDSM(), ExecMemoizeInitializeDSM(), ExecParallelInitializeDSM(), ExecSeqScanInitializeDSM(), ExecSeqScanInstrumentInitDSM(), ExecSortInitializeDSM(), ExecTidRangeScanInitializeDSM(), ExecTidRangeScanInstrumentInitDSM(), fb(), ExecParallelInitializeDSMContext::instrumentation, ExecParallelInitializeDSMContext::nnodes, nodeTag, Plan::parallel_aware, ExecParallelInitializeDSMContext::pcxt, PlanState::plan, SharedExecutorInstrumentation::plan_node_id, Plan::plan_node_id, and planstate_tree_walker.

Referenced by ExecInitParallelPlan(), and ExecParallelInitializeDSM().

◆ ExecParallelInitializeWorker()

static bool ExecParallelInitializeWorker ( PlanState planstate,
ParallelWorkerContext pwcxt 
)
static

Definition at line 1399 of file execParallel.c.

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}

References ExecAggInitializeWorker(), ExecAppendInitializeWorker(), ExecBitmapHeapInitializeWorker(), ExecBitmapHeapInstrumentInitWorker(), ExecBitmapIndexScanInitializeWorker(), ExecCustomScanInitializeWorker(), ExecForeignScanInitializeWorker(), ExecHashInitializeWorker(), ExecHashJoinInitializeWorker(), ExecIncrementalSortInitializeWorker(), ExecIndexOnlyScanInitializeWorker(), ExecIndexOnlyScanInstrumentInitWorker(), ExecIndexScanInitializeWorker(), ExecIndexScanInstrumentInitWorker(), ExecMemoizeInitializeWorker(), ExecParallelInitializeWorker(), ExecSeqScanInitializeWorker(), ExecSeqScanInstrumentInitWorker(), ExecSortInitializeWorker(), ExecTidRangeScanInitializeWorker(), ExecTidRangeScanInstrumentInitWorker(), fb(), nodeTag, Plan::parallel_aware, PlanState::plan, and planstate_tree_walker.

Referenced by ExecParallelInitializeWorker(), and ParallelQueryMain().

◆ ExecParallelReinitialize()

void ExecParallelReinitialize ( PlanState planstate,
ParallelExecutorInfo pei,
Bitmapset sendParams 
)

Definition at line 970 of file execParallel.c.

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}

References ParallelExecutorInfo::area, Assert, bms_is_empty, dsa_free(), DsaPointerIsValid, EState::es_query_dsa, ExecParallelReInitializeDSM(), ExecParallelSetupTupleQueues(), ExecSetParamPlanMulti(), fb(), ParallelExecutorInfo::finished, GetPerTupleExprContext, InvalidDsaPointer, PARALLEL_KEY_EXECUTOR_FIXED, ParallelExecutorInfo::param_exec, ParallelExecutorInfo::pcxt, ParallelExecutorInfo::reader, ReinitializeParallelDSM(), SerializeParamExecParams(), shm_toc_lookup(), PlanState::state, ParallelContext::toc, and ParallelExecutorInfo::tqueue.

Referenced by ExecGather(), and ExecGatherMerge().

◆ ExecParallelReInitializeDSM()

static bool ExecParallelReInitializeDSM ( PlanState planstate,
ParallelContext pcxt 
)
static

Definition at line 1019 of file execParallel.c.

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}

References ExecAppendReInitializeDSM(), ExecBitmapHeapReInitializeDSM(), ExecCustomScanReInitializeDSM(), ExecForeignScanReInitializeDSM(), ExecHashJoinReInitializeDSM(), ExecIndexOnlyScanReInitializeDSM(), ExecIndexScanReInitializeDSM(), ExecParallelReInitializeDSM(), ExecSeqScanReInitializeDSM(), ExecTidRangeScanReInitializeDSM(), fb(), nodeTag, Plan::parallel_aware, PlanState::plan, and planstate_tree_walker.

Referenced by ExecParallelReinitialize(), and ExecParallelReInitializeDSM().

◆ ExecParallelReportInstrumentation()

static bool ExecParallelReportInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 1358 of file execParallel.c.

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}

References Assert, elog, ERROR, ExecParallelReportInstrumentation(), fb(), GetInstrumentationArray, i, InstrAggNode(), InstrEndLoop(), PlanState::instrument, IsParallelWorker, SharedExecutorInstrumentation::num_plan_nodes, SharedExecutorInstrumentation::num_workers, ParallelWorkerNumber, PlanState::plan, SharedExecutorInstrumentation::plan_node_id, Plan::plan_node_id, and planstate_tree_walker.

Referenced by ExecParallelReportInstrumentation(), and ParallelQueryMain().

◆ ExecParallelRetrieveInstrumentation()

static bool ExecParallelRetrieveInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 1094 of file execParallel.c.

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}

References elog, ERROR, EState::es_query_cxt, ExecAggRetrieveInstrumentation(), ExecBitmapHeapRetrieveInstrumentation(), ExecBitmapIndexScanRetrieveInstrumentation(), ExecHashRetrieveInstrumentation(), ExecIncrementalSortRetrieveInstrumentation(), ExecIndexOnlyScanRetrieveInstrumentation(), ExecIndexScanRetrieveInstrumentation(), ExecMemoizeRetrieveInstrumentation(), ExecParallelRetrieveInstrumentation(), ExecSeqScanRetrieveInstrumentation(), ExecSortRetrieveInstrumentation(), ExecTidRangeScanRetrieveInstrumentation(), fb(), GetInstrumentationArray, i, InstrAggNode(), WorkerNodeInstrumentation::instrument, PlanState::instrument, memcpy(), MemoryContextSwitchTo(), mul_size(), nodeTag, SharedExecutorInstrumentation::num_plan_nodes, SharedExecutorInstrumentation::num_workers, WorkerNodeInstrumentation::num_workers, palloc(), PlanState::plan, SharedExecutorInstrumentation::plan_node_id, Plan::plan_node_id, planstate_tree_walker, PlanState::state, and PlanState::worker_instrument.

Referenced by ExecParallelCleanup(), and ExecParallelRetrieveInstrumentation().

◆ ExecParallelRetrieveJitInstrumentation()

static void ExecParallelRetrieveJitInstrumentation ( PlanState planstate,
SharedJitInstrumentation shared_jit 
)
static

Definition at line 1181 of file execParallel.c.

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}

References EState::es_jit_worker_instr, EState::es_query_cxt, fb(), InstrJitAgg(), memcpy(), MemoryContextAlloc(), MemoryContextAllocZero(), mul_size(), PlanState::state, and PlanState::worker_jit_instrument.

Referenced by ExecParallelCleanup().

◆ ExecParallelSetupTupleQueues()

static shm_mq_handle ** ExecParallelSetupTupleQueues ( ParallelContext pcxt,
bool  reinitialize 
)
static

Definition at line 601 of file execParallel.c.

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}

References fb(), i, mul_size(), MyProc, ParallelContext::nworkers, palloc(), PARALLEL_KEY_TUPLE_QUEUE, PARALLEL_TUPLE_QUEUE_SIZE, ParallelContext::seg, shm_mq_attach(), shm_mq_create(), shm_mq_set_receiver(), shm_toc_allocate(), shm_toc_insert(), shm_toc_lookup(), and ParallelContext::toc.

Referenced by ExecInitParallelPlan(), and ExecParallelReinitialize().

◆ ExecSerializePlan()

static char * ExecSerializePlan ( Plan plan,
EState estate 
)
static

Definition at line 152 of file execParallel.c.

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}

References PlannedStmt::appendRelations, PlannedStmt::canSetTag, CMD_SELECT, PlannedStmt::commandType, copyObject, PlannedStmt::dependsOnRole, EState::es_part_prune_infos, EState::es_plannedstmt, EState::es_range_table, EState::es_rteperminfos, EState::es_unpruned_relids, fb(), PlannedStmt::hasModifyingCTE, PlannedStmt::hasReturning, PlannedStmt::invalItems, lappend(), lfirst, lfirst_node, makeNode, NIL, nodeToString(), Plan::parallel_safe, PlannedStmt::parallelModeNeeded, PlannedStmt::paramExecTypes, PlannedStmt::partPruneInfos, PlannedStmt::permInfos, pgstat_get_my_plan_id(), pgstat_get_my_query_id(), plan, PLAN_STMT_INTERNAL, PlannedStmt::planId, PlannedStmt::planOrigin, PlannedStmt::planTree, PlannedStmt::queryId, PlannedStmt::relationOids, PlannedStmt::resultRelationRelids, PlannedStmt::rewindPlanIDs, PlannedStmt::rowMarkRelids, PlannedStmt::rowMarks, PlannedStmt::rtable, PlannedStmt::stmt_len, PlannedStmt::stmt_location, PlannedStmt::subplans, PlannedStmt::transientPlan, PlannedStmt::unprunableRelids, and PlannedStmt::utilityStmt.

Referenced by ExecInitParallelPlan().

◆ ParallelQueryMain()

void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)

Definition at line 1514 of file execParallel.c.

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}

References Assert, debug_query_string, dsa_attach_in_place(), dsa_detach(), dsa_get_address(), DsaPointerIsValid, EState::es_jit, EState::es_query_dsa, QueryDesc::estate, ExecParallelGetQueryDesc(), ExecParallelGetReceiver(), ExecParallelInitializeWorker(), ExecParallelReportInstrumentation(), ExecSetTupleBound(), ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), fb(), ForwardScanDirection, FreeQueryDesc(), JitContext::instr, InstrEndParallelQuery(), InstrStartParallelQuery(), SharedExecutorInstrumentation::instrument_options, SharedJitInstrumentation::jit_instr, PlannedStmt::jitFlags, PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_DSA, PARALLEL_KEY_EXECUTOR_FIXED, PARALLEL_KEY_INSTRUMENTATION, PARALLEL_KEY_JIT_INSTRUMENTATION, PARALLEL_KEY_WAL_USAGE, ParallelWorkerNumber, pgstat_report_activity(), QueryDesc::plannedstmt, QueryDesc::planstate, RestoreParamExecParams(), shm_toc_lookup(), QueryDesc::sourceText, PlanState::state, and STATE_RUNNING.

◆ RestoreParamExecParams()

static void RestoreParamExecParams ( char start_address,
EState estate 
)
static

Definition at line 451 of file execParallel.c.

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}

References datumRestore(), EState::es_param_exec_vals, fb(), i, and memcpy().

Referenced by ParallelQueryMain().

◆ SerializeParamExecParams()

static dsa_pointer SerializeParamExecParams ( EState estate,
Bitmapset params,
dsa_area area 
)
static

Definition at line 396 of file execParallel.c.

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}

References bms_next_member(), bms_num_members(), datumSerialize(), dsa_allocate, dsa_get_address(), EState::es_param_exec_vals, EState::es_plannedstmt, EstimateParamExecSpace(), fb(), get_typlenbyval(), list_nth_oid(), memcpy(), OidIsValid, and PlannedStmt::paramExecTypes.

Referenced by ExecInitParallelPlan(), and ExecParallelReinitialize().