PostgreSQL Source Code git master
Loading...
Searching...
No Matches
execParallel.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * execParallel.c
4 * Support routines for parallel execution.
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * This file contains routines that are intended to support setting up,
10 * using, and tearing down a ParallelContext from within the PostgreSQL
11 * executor. The ParallelContext machinery will handle starting the
12 * workers and ensuring that their state generally matches that of the
13 * leader; see src/backend/access/transam/README.parallel for details.
14 * However, we must save and restore relevant executor state, such as
15 * any ParamListInfo associated with the query, buffer/WAL usage info, and
16 * the actual plan to be passed down to the worker.
17 *
18 * IDENTIFICATION
19 * src/backend/executor/execParallel.c
20 *
21 *-------------------------------------------------------------------------
22 */
23
24#include "postgres.h"
25
27#include "executor/executor.h"
28#include "executor/nodeAgg.h"
29#include "executor/nodeAppend.h"
32#include "executor/nodeCustom.h"
34#include "executor/nodeHash.h"
41#include "executor/nodeSort.h"
44#include "executor/tqueue.h"
45#include "jit/jit.h"
46#include "nodes/nodeFuncs.h"
47#include "pgstat.h"
48#include "storage/proc.h"
49#include "tcop/tcopprot.h"
50#include "utils/datum.h"
51#include "utils/dsa.h"
52#include "utils/lsyscache.h"
53#include "utils/snapmgr.h"
54
55/*
56 * Magic numbers for parallel executor communication. We use constants
57 * greater than any 32-bit integer here so that values < 2^32 can be used
58 * by individual parallel nodes to store their own state.
59 */
60#define PARALLEL_KEY_EXECUTOR_FIXED UINT64CONST(0xE000000000000001)
61#define PARALLEL_KEY_PLANNEDSTMT UINT64CONST(0xE000000000000002)
62#define PARALLEL_KEY_PARAMLISTINFO UINT64CONST(0xE000000000000003)
63#define PARALLEL_KEY_BUFFER_USAGE UINT64CONST(0xE000000000000004)
64#define PARALLEL_KEY_TUPLE_QUEUE UINT64CONST(0xE000000000000005)
65#define PARALLEL_KEY_INSTRUMENTATION UINT64CONST(0xE000000000000006)
66#define PARALLEL_KEY_DSA UINT64CONST(0xE000000000000007)
67#define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xE000000000000008)
68#define PARALLEL_KEY_JIT_INSTRUMENTATION UINT64CONST(0xE000000000000009)
69#define PARALLEL_KEY_WAL_USAGE UINT64CONST(0xE00000000000000A)
70
71#define PARALLEL_TUPLE_QUEUE_SIZE 65536
72
73/*
74 * Fixed-size random stuff that we need to pass to parallel workers.
75 */
83
84/*
85 * DSM structure for accumulating per-PlanState instrumentation.
86 *
87 * instrument_options: Same meaning here as in instrument.c.
88 *
89 * instrument_offset: Offset, relative to the start of this structure,
90 * of the first Instrumentation object. This will depend on the length of
91 * the plan_node_id array.
92 *
93 * num_workers: Number of workers.
94 *
95 * num_plan_nodes: Number of plan nodes.
96 *
97 * plan_node_id: Array of plan nodes for which we are gathering instrumentation
98 * from parallel workers. The length of this array is given by num_plan_nodes.
99 */
101{
107 /* array of num_plan_nodes * num_workers Instrumentation objects follows */
108};
109#define GetInstrumentationArray(sei) \
110 (StaticAssertVariableIsOfTypeMacro(sei, SharedExecutorInstrumentation *), \
111 (Instrumentation *) (((char *) sei) + sei->instrument_offset))
112
113/* Context object for ExecParallelEstimate. */
119
120/* Context object for ExecParallelInitializeDSM. */
127
128/* Helper functions that run in the parallel leader. */
129static char *ExecSerializePlan(Plan *plan, EState *estate);
130static bool ExecParallelEstimate(PlanState *planstate,
132static bool ExecParallelInitializeDSM(PlanState *planstate,
135 bool reinitialize);
136static bool ExecParallelReInitializeDSM(PlanState *planstate,
137 ParallelContext *pcxt);
139 SharedExecutorInstrumentation *instrumentation);
140
141/* Helper function that runs in the parallel worker. */
143
144/*
145 * Create a serialized representation of the plan to be sent to each worker.
146 */
147static char *
149{
150 PlannedStmt *pstmt;
151 ListCell *lc;
152
153 /* We can't scribble on the original plan, so make a copy. */
155
156 /*
157 * The worker will start its own copy of the executor, and that copy will
158 * insert a junk filter if the toplevel node has any resjunk entries. We
159 * don't want that to happen, because while resjunk columns shouldn't be
160 * sent back to the user, here the tuples are coming back to another
161 * backend which may very well need them. So mutate the target list
162 * accordingly. This is sort of a hack; there might be better ways to do
163 * this...
164 */
165 foreach(lc, plan->targetlist)
166 {
168
169 tle->resjunk = false;
170 }
171
172 /*
173 * Create a dummy PlannedStmt. Most of the fields don't need to be valid
174 * for our purposes, but the worker will need at least a minimal
175 * PlannedStmt to start the executor.
176 */
177 pstmt = makeNode(PlannedStmt);
178 pstmt->commandType = CMD_SELECT;
180 pstmt->planId = pgstat_get_my_plan_id();
181 pstmt->hasReturning = false;
182 pstmt->hasModifyingCTE = false;
183 pstmt->canSetTag = true;
184 pstmt->transientPlan = false;
185 pstmt->dependsOnRole = false;
186 pstmt->parallelModeNeeded = false;
187 pstmt->planTree = plan;
188 pstmt->partPruneInfos = estate->es_part_prune_infos;
189 pstmt->rtable = estate->es_range_table;
190 pstmt->unprunableRelids = estate->es_unpruned_relids;
191 pstmt->permInfos = estate->es_rteperminfos;
192 pstmt->resultRelations = NIL;
193 pstmt->appendRelations = NIL;
195
196 /*
197 * Transfer only parallel-safe subplans, leaving a NULL "hole" in the list
198 * for unsafe ones (so that the list indexes of the safe ones are
199 * preserved). This positively ensures that the worker won't try to run,
200 * or even do ExecInitNode on, an unsafe subplan. That's important to
201 * protect, eg, non-parallel-aware FDWs from getting into trouble.
202 */
203 pstmt->subplans = NIL;
204 foreach(lc, estate->es_plannedstmt->subplans)
205 {
206 Plan *subplan = (Plan *) lfirst(lc);
207
208 if (subplan && !subplan->parallel_safe)
209 subplan = NULL;
210 pstmt->subplans = lappend(pstmt->subplans, subplan);
211 }
212
213 pstmt->rewindPlanIDs = NULL;
214 pstmt->rowMarks = NIL;
215 pstmt->relationOids = NIL;
216 pstmt->invalItems = NIL; /* workers can't replan anyway... */
218 pstmt->utilityStmt = NULL;
219 pstmt->stmt_location = -1;
220 pstmt->stmt_len = -1;
221
222 /* Return serialized copy of our dummy PlannedStmt. */
223 return nodeToString(pstmt);
224}
225
226/*
227 * Parallel-aware plan nodes (and occasionally others) may need some state
228 * which is shared across all parallel workers. Before we size the DSM, give
229 * them a chance to call shm_toc_estimate_chunk or shm_toc_estimate_keys on
230 * &pcxt->estimator.
231 *
232 * While we're at it, count the number of PlanState nodes in the tree, so
233 * we know how many Instrumentation structures we need.
234 */
235static bool
237{
238 if (planstate == NULL)
239 return false;
240
241 /* Count this node. */
242 e->nnodes++;
243
244 switch (nodeTag(planstate))
245 {
246 case T_SeqScanState:
247 if (planstate->plan->parallel_aware)
248 ExecSeqScanEstimate((SeqScanState *) planstate,
249 e->pcxt);
250 break;
251 case T_IndexScanState:
252 /* even when not parallel-aware, for EXPLAIN ANALYZE */
254 e->pcxt);
255 break;
257 /* even when not parallel-aware, for EXPLAIN ANALYZE */
259 e->pcxt);
260 break;
262 /* even when not parallel-aware, for EXPLAIN ANALYZE */
264 e->pcxt);
265 break;
267 if (planstate->plan->parallel_aware)
269 e->pcxt);
270 break;
272 if (planstate->plan->parallel_aware)
274 e->pcxt);
275 break;
276 case T_AppendState:
277 if (planstate->plan->parallel_aware)
278 ExecAppendEstimate((AppendState *) planstate,
279 e->pcxt);
280 break;
282 if (planstate->plan->parallel_aware)
284 e->pcxt);
285 break;
287 if (planstate->plan->parallel_aware)
289 e->pcxt);
290 break;
291 case T_HashJoinState:
292 if (planstate->plan->parallel_aware)
294 e->pcxt);
295 break;
296 case T_HashState:
297 /* even when not parallel-aware, for EXPLAIN ANALYZE */
298 ExecHashEstimate((HashState *) planstate, e->pcxt);
299 break;
300 case T_SortState:
301 /* even when not parallel-aware, for EXPLAIN ANALYZE */
302 ExecSortEstimate((SortState *) planstate, e->pcxt);
303 break;
305 /* even when not parallel-aware, for EXPLAIN ANALYZE */
307 break;
308 case T_AggState:
309 /* even when not parallel-aware, for EXPLAIN ANALYZE */
310 ExecAggEstimate((AggState *) planstate, e->pcxt);
311 break;
312 case T_MemoizeState:
313 /* even when not parallel-aware, for EXPLAIN ANALYZE */
314 ExecMemoizeEstimate((MemoizeState *) planstate, e->pcxt);
315 break;
316 default:
317 break;
318 }
319
320 return planstate_tree_walker(planstate, ExecParallelEstimate, e);
321}
322
323/*
324 * Estimate the amount of space required to serialize the indicated parameters.
325 */
326static Size
328{
329 int paramid;
330 Size sz = sizeof(int);
331
332 paramid = -1;
333 while ((paramid = bms_next_member(params, paramid)) >= 0)
334 {
335 Oid typeOid;
336 int16 typLen;
337 bool typByVal;
339
340 prm = &(estate->es_param_exec_vals[paramid]);
341 typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
342 paramid);
343
344 sz = add_size(sz, sizeof(int)); /* space for paramid */
345
346 /* space for datum/isnull */
347 if (OidIsValid(typeOid))
348 get_typlenbyval(typeOid, &typLen, &typByVal);
349 else
350 {
351 /* If no type OID, assume by-value, like copyParamList does. */
352 typLen = sizeof(Datum);
353 typByVal = true;
354 }
355 sz = add_size(sz,
356 datumEstimateSpace(prm->value, prm->isnull,
357 typByVal, typLen));
358 }
359 return sz;
360}
361
362/*
363 * Serialize specified PARAM_EXEC parameters.
364 *
365 * We write the number of parameters first, as a 4-byte integer, and then
366 * write details for each parameter in turn. The details for each parameter
367 * consist of a 4-byte paramid (location of param in execution time internal
368 * parameter array) and then the datum as serialized by datumSerialize().
369 */
370static dsa_pointer
372{
373 Size size;
374 int nparams;
375 int paramid;
377 dsa_pointer handle;
378 char *start_address;
379
380 /* Allocate enough space for the current parameter values. */
381 size = EstimateParamExecSpace(estate, params);
382 handle = dsa_allocate(area, size);
383 start_address = dsa_get_address(area, handle);
384
385 /* First write the number of parameters as a 4-byte integer. */
386 nparams = bms_num_members(params);
387 memcpy(start_address, &nparams, sizeof(int));
388 start_address += sizeof(int);
389
390 /* Write details for each parameter in turn. */
391 paramid = -1;
392 while ((paramid = bms_next_member(params, paramid)) >= 0)
393 {
394 Oid typeOid;
395 int16 typLen;
396 bool typByVal;
397
398 prm = &(estate->es_param_exec_vals[paramid]);
399 typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
400 paramid);
401
402 /* Write paramid. */
403 memcpy(start_address, &paramid, sizeof(int));
404 start_address += sizeof(int);
405
406 /* Write datum/isnull */
407 if (OidIsValid(typeOid))
408 get_typlenbyval(typeOid, &typLen, &typByVal);
409 else
410 {
411 /* If no type OID, assume by-value, like copyParamList does. */
412 typLen = sizeof(Datum);
413 typByVal = true;
414 }
415 datumSerialize(prm->value, prm->isnull, typByVal, typLen,
417 }
418
419 return handle;
420}
421
422/*
423 * Restore specified PARAM_EXEC parameters.
424 */
425static void
427{
428 int nparams;
429 int i;
430 int paramid;
431
432 memcpy(&nparams, start_address, sizeof(int));
433 start_address += sizeof(int);
434
435 for (i = 0; i < nparams; i++)
436 {
438
439 /* Read paramid */
440 memcpy(&paramid, start_address, sizeof(int));
441 start_address += sizeof(int);
442 prm = &(estate->es_param_exec_vals[paramid]);
443
444 /* Read datum/isnull. */
445 prm->value = datumRestore(&start_address, &prm->isnull);
446 prm->execPlan = NULL;
447 }
448}
449
450/*
451 * Initialize the dynamic shared memory segment that will be used to control
452 * parallel execution.
453 */
454static bool
457{
458 if (planstate == NULL)
459 return false;
460
461 /* If instrumentation is enabled, initialize slot for this node. */
462 if (d->instrumentation != NULL)
464 planstate->plan->plan_node_id;
465
466 /* Count this node. */
467 d->nnodes++;
468
469 /*
470 * Call initializers for DSM-using plan nodes.
471 *
472 * Most plan nodes won't do anything here, but plan nodes that allocated
473 * DSM may need to initialize shared state in the DSM before parallel
474 * workers are launched. They can allocate the space they previously
475 * estimated using shm_toc_allocate, and add the keys they previously
476 * estimated using shm_toc_insert, in each case targeting pcxt->toc.
477 */
478 switch (nodeTag(planstate))
479 {
480 case T_SeqScanState:
481 if (planstate->plan->parallel_aware)
483 d->pcxt);
484 break;
485 case T_IndexScanState:
486 /* even when not parallel-aware, for EXPLAIN ANALYZE */
488 break;
490 /* even when not parallel-aware, for EXPLAIN ANALYZE */
492 d->pcxt);
493 break;
495 /* even when not parallel-aware, for EXPLAIN ANALYZE */
497 break;
499 if (planstate->plan->parallel_aware)
501 d->pcxt);
502 break;
504 if (planstate->plan->parallel_aware)
506 d->pcxt);
507 break;
508 case T_AppendState:
509 if (planstate->plan->parallel_aware)
511 d->pcxt);
512 break;
514 if (planstate->plan->parallel_aware)
516 d->pcxt);
517 break;
519 if (planstate->plan->parallel_aware)
521 d->pcxt);
522 break;
523 case T_HashJoinState:
524 if (planstate->plan->parallel_aware)
526 d->pcxt);
527 break;
528 case T_HashState:
529 /* even when not parallel-aware, for EXPLAIN ANALYZE */
530 ExecHashInitializeDSM((HashState *) planstate, d->pcxt);
531 break;
532 case T_SortState:
533 /* even when not parallel-aware, for EXPLAIN ANALYZE */
534 ExecSortInitializeDSM((SortState *) planstate, d->pcxt);
535 break;
537 /* even when not parallel-aware, for EXPLAIN ANALYZE */
539 break;
540 case T_AggState:
541 /* even when not parallel-aware, for EXPLAIN ANALYZE */
542 ExecAggInitializeDSM((AggState *) planstate, d->pcxt);
543 break;
544 case T_MemoizeState:
545 /* even when not parallel-aware, for EXPLAIN ANALYZE */
546 ExecMemoizeInitializeDSM((MemoizeState *) planstate, d->pcxt);
547 break;
548 default:
549 break;
550 }
551
553}
554
555/*
556 * It sets up the response queues for backend workers to return tuples
557 * to the main backend and start the workers.
558 */
559static shm_mq_handle **
561{
563 char *tqueuespace;
564 int i;
565
566 /* Skip this if no workers. */
567 if (pcxt->nworkers == 0)
568 return NULL;
569
570 /* Allocate memory for shared memory queue handles. */
572 palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
573
574 /*
575 * If not reinitializing, allocate space from the DSM for the queues;
576 * otherwise, find the already allocated space.
577 */
578 if (!reinitialize)
580 shm_toc_allocate(pcxt->toc,
582 pcxt->nworkers));
583 else
585
586 /* Create the queues, and become the receiver for each. */
587 for (i = 0; i < pcxt->nworkers; ++i)
588 {
589 shm_mq *mq;
590
594
596 responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
597 }
598
599 /* Add array of queues to shm_toc, so others can find it. */
600 if (!reinitialize)
602
603 /* Return array of handles. */
604 return responseq;
605}
606
607/*
608 * Sets up the required infrastructure for backend workers to perform
609 * execution and return results to the main backend.
610 */
613 Bitmapset *sendParams, int nworkers,
614 int64 tuples_needed)
615{
617 ParallelContext *pcxt;
621 char *pstmt_data;
622 char *pstmt_space;
626 SharedExecutorInstrumentation *instrumentation = NULL;
627 SharedJitInstrumentation *jit_instrumentation = NULL;
628 int pstmt_len;
630 int instrumentation_len = 0;
632 int instrument_offset = 0;
634 char *query_string;
635 int query_len;
636
637 /*
638 * Force any initplan outputs that we're going to pass to workers to be
639 * evaluated, if they weren't already.
640 *
641 * For simplicity, we use the EState's per-output-tuple ExprContext here.
642 * That risks intra-query memory leakage, since we might pass through here
643 * many times before that ExprContext gets reset; but ExecSetParamPlan
644 * doesn't normally leak any memory in the context (see its comments), so
645 * it doesn't seem worth complicating this function's API to pass it a
646 * shorter-lived ExprContext. This might need to change someday.
647 */
649
650 /* Allocate object for return value. */
652 pei->finished = false;
653 pei->planstate = planstate;
654
655 /* Fix up and serialize plan to be sent to workers. */
656 pstmt_data = ExecSerializePlan(planstate->plan, estate);
657
658 /* Create a parallel context. */
659 pcxt = CreateParallelContext("postgres", "ParallelQueryMain", nworkers);
660 pei->pcxt = pcxt;
661
662 /*
663 * Before telling the parallel context to create a dynamic shared memory
664 * segment, we need to figure out how big it should be. Estimate space
665 * for the various things we need to store.
666 */
667
668 /* Estimate space for fixed-size state. */
672
673 /* Estimate space for query text. */
674 query_len = strlen(estate->es_sourceText);
675 shm_toc_estimate_chunk(&pcxt->estimator, query_len + 1);
677
678 /* Estimate space for serialized PlannedStmt. */
682
683 /* Estimate space for serialized ParamListInfo. */
687
688 /*
689 * Estimate space for BufferUsage.
690 *
691 * If EXPLAIN is not in use and there are no extensions loaded that care,
692 * we could skip this. But we have no way of knowing whether anyone's
693 * looking at pgBufferUsage, so do it unconditionally.
694 */
696 mul_size(sizeof(BufferUsage), pcxt->nworkers));
698
699 /*
700 * Same thing for WalUsage.
701 */
703 mul_size(sizeof(WalUsage), pcxt->nworkers));
705
706 /* Estimate space for tuple queues. */
710
711 /*
712 * Give parallel-aware nodes a chance to add to the estimates, and get a
713 * count of how many PlanState nodes there are.
714 */
715 e.pcxt = pcxt;
716 e.nnodes = 0;
717 ExecParallelEstimate(planstate, &e);
718
719 /* Estimate space for instrumentation, if required. */
720 if (estate->es_instrument)
721 {
724 sizeof(int) * e.nnodes;
726 instrument_offset = instrumentation_len;
729 mul_size(e.nnodes, nworkers));
732
733 /* Estimate space for JIT instrumentation, if required. */
734 if (estate->es_jit_flags != PGJIT_NONE)
735 {
738 sizeof(JitInstrumentation) * nworkers;
741 }
742 }
743
744 /* Estimate space for DSA area. */
747
748 /*
749 * InitializeParallelDSM() passes the active snapshot to the parallel
750 * worker, which uses it to set es_snapshot. Make sure we don't set
751 * es_snapshot differently in the child.
752 */
754
755 /* Everyone's had a chance to ask for space, so now create the DSM. */
757
758 /*
759 * OK, now we have a dynamic shared memory segment, and it should be big
760 * enough to store all of the data we estimated we would want to put into
761 * it, plus whatever general stuff (not specifically executor-related) the
762 * ParallelContext itself needs to store there. None of the space we
763 * asked for has been allocated or initialized yet, though, so do that.
764 */
765
766 /* Store fixed-size state. */
768 fpes->tuples_needed = tuples_needed;
769 fpes->param_exec = InvalidDsaPointer;
770 fpes->eflags = estate->es_top_eflags;
771 fpes->jit_flags = estate->es_jit_flags;
773
774 /* Store query string */
775 query_string = shm_toc_allocate(pcxt->toc, query_len + 1);
776 memcpy(query_string, estate->es_sourceText, query_len + 1);
777 shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string);
778
779 /* Store serialized PlannedStmt. */
783
784 /* Store serialized ParamListInfo. */
788
789 /* Allocate space for each worker's BufferUsage; no need to initialize. */
791 mul_size(sizeof(BufferUsage), pcxt->nworkers));
794
795 /* Same for WalUsage. */
797 mul_size(sizeof(WalUsage), pcxt->nworkers));
800
801 /* Set up the tuple queues that the workers will write into. */
802 pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
803
804 /* We don't need the TupleQueueReaders yet, though. */
805 pei->reader = NULL;
806
807 /*
808 * If instrumentation options were supplied, allocate space for the data.
809 * It only gets partially initialized here; the rest happens during
810 * ExecParallelInitializeDSM.
811 */
812 if (estate->es_instrument)
813 {
814 Instrumentation *instrument;
815 int i;
816
817 instrumentation = shm_toc_allocate(pcxt->toc, instrumentation_len);
818 instrumentation->instrument_options = estate->es_instrument;
819 instrumentation->instrument_offset = instrument_offset;
820 instrumentation->num_workers = nworkers;
821 instrumentation->num_plan_nodes = e.nnodes;
822 instrument = GetInstrumentationArray(instrumentation);
823 for (i = 0; i < nworkers * e.nnodes; ++i)
824 InstrInit(&instrument[i], estate->es_instrument);
826 instrumentation);
827 pei->instrumentation = instrumentation;
828
829 if (estate->es_jit_flags != PGJIT_NONE)
830 {
831 jit_instrumentation = shm_toc_allocate(pcxt->toc,
833 jit_instrumentation->num_workers = nworkers;
834 memset(jit_instrumentation->jit_instr, 0,
835 sizeof(JitInstrumentation) * nworkers);
837 jit_instrumentation);
838 pei->jit_instrumentation = jit_instrumentation;
839 }
840 }
841
842 /*
843 * Create a DSA area that can be used by the leader and all workers.
844 * (However, if we failed to create a DSM and are using private memory
845 * instead, then skip this.)
846 */
847 if (pcxt->seg != NULL)
848 {
849 char *area_space;
850
855 pcxt->seg);
856
857 /*
858 * Serialize parameters, if any, using DSA storage. We don't dare use
859 * the main parallel query DSM for this because we might relaunch
860 * workers after the values have changed (and thus the amount of
861 * storage required has changed).
862 */
864 {
866 pei->area);
867 fpes->param_exec = pei->param_exec;
868 }
869 }
870
871 /*
872 * Give parallel-aware nodes a chance to initialize their shared data.
873 * This also initializes the elements of instrumentation->ps_instrument,
874 * if it exists.
875 */
876 d.pcxt = pcxt;
877 d.instrumentation = instrumentation;
878 d.nnodes = 0;
879
880 /* Install our DSA area while initializing the plan. */
881 estate->es_query_dsa = pei->area;
882 ExecParallelInitializeDSM(planstate, &d);
883 estate->es_query_dsa = NULL;
884
885 /*
886 * Make sure that the world hasn't shifted under our feet. This could
887 * probably just be an Assert(), but let's be conservative for now.
888 */
889 if (e.nnodes != d.nnodes)
890 elog(ERROR, "inconsistent count of PlanState nodes");
891
892 /* OK, we're ready to rock and roll. */
893 return pei;
894}
895
896/*
897 * Set up tuple queue readers to read the results of a parallel subplan.
898 *
899 * This is separate from ExecInitParallelPlan() because we can launch the
900 * worker processes and let them start doing something before we do this.
901 */
902void
904{
905 int nworkers = pei->pcxt->nworkers_launched;
906 int i;
907
908 Assert(pei->reader == NULL);
909
910 if (nworkers > 0)
911 {
912 pei->reader = (TupleQueueReader **)
913 palloc(nworkers * sizeof(TupleQueueReader *));
914
915 for (i = 0; i < nworkers; i++)
916 {
918 pei->pcxt->worker[i].bgwhandle);
919 pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
920 }
921 }
922}
923
924/*
925 * Re-initialize the parallel executor shared memory state before launching
926 * a fresh batch of workers.
927 */
928void
932{
933 EState *estate = planstate->state;
935
936 /* Old workers must already be shut down */
937 Assert(pei->finished);
938
939 /*
940 * Force any initplan outputs that we're going to pass to workers to be
941 * evaluated, if they weren't already (see comments in
942 * ExecInitParallelPlan).
943 */
945
947 pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
948 pei->reader = NULL;
949 pei->finished = false;
950
952
953 /* Free any serialized parameters from the last round. */
954 if (DsaPointerIsValid(fpes->param_exec))
955 {
956 dsa_free(pei->area, fpes->param_exec);
957 fpes->param_exec = InvalidDsaPointer;
958 }
959
960 /* Serialize current parameter values if required. */
962 {
964 pei->area);
965 fpes->param_exec = pei->param_exec;
966 }
967
968 /* Traverse plan tree and let each child node reset associated state. */
969 estate->es_query_dsa = pei->area;
970 ExecParallelReInitializeDSM(planstate, pei->pcxt);
971 estate->es_query_dsa = NULL;
972}
973
974/*
975 * Traverse plan tree to reinitialize per-node dynamic shared memory state
976 */
977static bool
979 ParallelContext *pcxt)
980{
981 if (planstate == NULL)
982 return false;
983
984 /*
985 * Call reinitializers for DSM-using plan nodes.
986 */
987 switch (nodeTag(planstate))
988 {
989 case T_SeqScanState:
990 if (planstate->plan->parallel_aware)
992 pcxt);
993 break;
994 case T_IndexScanState:
995 if (planstate->plan->parallel_aware)
997 pcxt);
998 break;
1000 if (planstate->plan->parallel_aware)
1002 pcxt);
1003 break;
1004 case T_ForeignScanState:
1005 if (planstate->plan->parallel_aware)
1007 pcxt);
1008 break;
1010 if (planstate->plan->parallel_aware)
1012 pcxt);
1013 break;
1014 case T_AppendState:
1015 if (planstate->plan->parallel_aware)
1016 ExecAppendReInitializeDSM((AppendState *) planstate, pcxt);
1017 break;
1018 case T_CustomScanState:
1019 if (planstate->plan->parallel_aware)
1021 pcxt);
1022 break;
1024 if (planstate->plan->parallel_aware)
1026 pcxt);
1027 break;
1028 case T_HashJoinState:
1029 if (planstate->plan->parallel_aware)
1031 pcxt);
1032 break;
1034 case T_HashState:
1035 case T_SortState:
1037 case T_MemoizeState:
1038 /* these nodes have DSM state, but no reinitialization is required */
1039 break;
1040
1041 default:
1042 break;
1043 }
1044
1045 return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt);
1046}
1047
1048/*
1049 * Copy instrumentation information about this node and its descendants from
1050 * dynamic shared memory.
1051 */
1052static bool
1054 SharedExecutorInstrumentation *instrumentation)
1055{
1056 Instrumentation *instrument;
1057 int i;
1058 int n;
1059 int ibytes;
1060 int plan_node_id = planstate->plan->plan_node_id;
1061 MemoryContext oldcontext;
1062
1063 /* Find the instrumentation for this node. */
1064 for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1065 if (instrumentation->plan_node_id[i] == plan_node_id)
1066 break;
1067 if (i >= instrumentation->num_plan_nodes)
1068 elog(ERROR, "plan node %d not found", plan_node_id);
1069
1070 /* Accumulate the statistics from all workers. */
1071 instrument = GetInstrumentationArray(instrumentation);
1072 instrument += i * instrumentation->num_workers;
1073 for (n = 0; n < instrumentation->num_workers; ++n)
1074 InstrAggNode(planstate->instrument, &instrument[n]);
1075
1076 /*
1077 * Also store the per-worker detail.
1078 *
1079 * Worker instrumentation should be allocated in the same context as the
1080 * regular instrumentation information, which is the per-query context.
1081 * Switch into per-query memory context.
1082 */
1083 oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
1084 ibytes = mul_size(instrumentation->num_workers, sizeof(Instrumentation));
1085 planstate->worker_instrument =
1087 MemoryContextSwitchTo(oldcontext);
1088
1089 planstate->worker_instrument->num_workers = instrumentation->num_workers;
1090 memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
1091
1092 /* Perform any node-type-specific work that needs to be done. */
1093 switch (nodeTag(planstate))
1094 {
1095 case T_IndexScanState:
1097 break;
1100 break;
1103 break;
1104 case T_SortState:
1106 break;
1109 break;
1110 case T_HashState:
1112 break;
1113 case T_AggState:
1115 break;
1116 case T_MemoizeState:
1118 break;
1121 break;
1122 default:
1123 break;
1124 }
1125
1127 instrumentation);
1128}
1129
1130/*
1131 * Add up the workers' JIT instrumentation from dynamic shared memory.
1132 */
1133static void
1136{
1138 int ibytes;
1139
1140 int n;
1141
1142 /*
1143 * Accumulate worker JIT instrumentation into the combined JIT
1144 * instrumentation, allocating it if required.
1145 */
1146 if (!planstate->state->es_jit_worker_instr)
1147 planstate->state->es_jit_worker_instr =
1149 combined = planstate->state->es_jit_worker_instr;
1150
1151 /* Accumulate all the workers' instrumentations. */
1152 for (n = 0; n < shared_jit->num_workers; ++n)
1153 InstrJitAgg(combined, &shared_jit->jit_instr[n]);
1154
1155 /*
1156 * Store the per-worker detail.
1157 *
1158 * Similar to ExecParallelRetrieveInstrumentation(), allocate the
1159 * instrumentation in per-query context.
1160 */
1162 + mul_size(shared_jit->num_workers, sizeof(JitInstrumentation));
1163 planstate->worker_jit_instrument =
1165
1167}
1168
1169/*
1170 * Finish parallel execution. We wait for parallel workers to finish, and
1171 * accumulate their buffer/WAL usage.
1172 */
1173void
1175{
1176 int nworkers = pei->pcxt->nworkers_launched;
1177 int i;
1178
1179 /* Make this be a no-op if called twice in a row. */
1180 if (pei->finished)
1181 return;
1182
1183 /*
1184 * Detach from tuple queues ASAP, so that any still-active workers will
1185 * notice that no further results are wanted.
1186 */
1187 if (pei->tqueue != NULL)
1188 {
1189 for (i = 0; i < nworkers; i++)
1190 shm_mq_detach(pei->tqueue[i]);
1191 pfree(pei->tqueue);
1192 pei->tqueue = NULL;
1193 }
1194
1195 /*
1196 * While we're waiting for the workers to finish, let's get rid of the
1197 * tuple queue readers. (Any other local cleanup could be done here too.)
1198 */
1199 if (pei->reader != NULL)
1200 {
1201 for (i = 0; i < nworkers; i++)
1203 pfree(pei->reader);
1204 pei->reader = NULL;
1205 }
1206
1207 /* Now wait for the workers to finish. */
1209
1210 /*
1211 * Next, accumulate buffer/WAL usage. (This must wait for the workers to
1212 * finish, or we might get incomplete data.)
1213 */
1214 for (i = 0; i < nworkers; i++)
1216
1217 pei->finished = true;
1218}
1219
1220/*
1221 * Accumulate instrumentation, and then clean up whatever ParallelExecutorInfo
1222 * resources still exist after ExecParallelFinish. We separate these
1223 * routines because someone might want to examine the contents of the DSM
1224 * after ExecParallelFinish and before calling this routine.
1225 */
1226void
1228{
1229 /* Accumulate instrumentation, if any. */
1230 if (pei->instrumentation)
1232 pei->instrumentation);
1233
1234 /* Accumulate JIT instrumentation, if any. */
1235 if (pei->jit_instrumentation)
1237 pei->jit_instrumentation);
1238
1239 /* Free any serialized parameters. */
1240 if (DsaPointerIsValid(pei->param_exec))
1241 {
1242 dsa_free(pei->area, pei->param_exec);
1244 }
1245 if (pei->area != NULL)
1246 {
1247 dsa_detach(pei->area);
1248 pei->area = NULL;
1249 }
1250 if (pei->pcxt != NULL)
1251 {
1253 pei->pcxt = NULL;
1254 }
1255 pfree(pei);
1256}
1257
1258/*
1259 * Create a DestReceiver to write tuples we produce to the shm_mq designated
1260 * for that purpose.
1261 */
1262static DestReceiver *
1274
1275/*
1276 * Create a QueryDesc for the PlannedStmt we are to execute, and return it.
1277 */
1278static QueryDesc *
1280 int instrument_options)
1281{
1282 char *pstmtspace;
1283 char *paramspace;
1284 PlannedStmt *pstmt;
1285 ParamListInfo paramLI;
1286 char *queryString;
1287
1288 /* Get the query string from shared memory */
1289 queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1290
1291 /* Reconstruct leader-supplied PlannedStmt. */
1293 pstmt = (PlannedStmt *) stringToNode(pstmtspace);
1294
1295 /* Reconstruct ParamListInfo. */
1297 paramLI = RestoreParamList(&paramspace);
1298
1299 /* Create a QueryDesc for the query. */
1300 return CreateQueryDesc(pstmt,
1301 queryString,
1303 receiver, paramLI, NULL, instrument_options);
1304}
1305
1306/*
1307 * Copy instrumentation information from this node and its descendants into
1308 * dynamic shared memory, so that the parallel leader can retrieve it.
1309 */
1310static bool
1312 SharedExecutorInstrumentation *instrumentation)
1313{
1314 int i;
1315 int plan_node_id = planstate->plan->plan_node_id;
1316 Instrumentation *instrument;
1317
1318 InstrEndLoop(planstate->instrument);
1319
1320 /*
1321 * If we shuffled the plan_node_id values in ps_instrument into sorted
1322 * order, we could use binary search here. This might matter someday if
1323 * we're pushing down sufficiently large plan trees. For now, do it the
1324 * slow, dumb way.
1325 */
1326 for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1327 if (instrumentation->plan_node_id[i] == plan_node_id)
1328 break;
1329 if (i >= instrumentation->num_plan_nodes)
1330 elog(ERROR, "plan node %d not found", plan_node_id);
1331
1332 /*
1333 * Add our statistics to the per-node, per-worker totals. It's possible
1334 * that this could happen more than once if we relaunched workers.
1335 */
1336 instrument = GetInstrumentationArray(instrumentation);
1337 instrument += i * instrumentation->num_workers;
1340 InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
1341
1343 instrumentation);
1344}
1345
1346/*
1347 * Initialize the PlanState and its descendants with the information
1348 * retrieved from shared memory. This has to be done once the PlanState
1349 * is allocated and initialized by executor; that is, after ExecutorStart().
1350 */
1351static bool
1353{
1354 if (planstate == NULL)
1355 return false;
1356
1357 switch (nodeTag(planstate))
1358 {
1359 case T_SeqScanState:
1360 if (planstate->plan->parallel_aware)
1362 break;
1363 case T_IndexScanState:
1364 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1366 break;
1368 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1370 pwcxt);
1371 break;
1373 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1375 pwcxt);
1376 break;
1377 case T_ForeignScanState:
1378 if (planstate->plan->parallel_aware)
1380 pwcxt);
1381 break;
1383 if (planstate->plan->parallel_aware)
1385 pwcxt);
1386 break;
1387 case T_AppendState:
1388 if (planstate->plan->parallel_aware)
1390 break;
1391 case T_CustomScanState:
1392 if (planstate->plan->parallel_aware)
1394 pwcxt);
1395 break;
1397 if (planstate->plan->parallel_aware)
1399 pwcxt);
1400 break;
1401 case T_HashJoinState:
1402 if (planstate->plan->parallel_aware)
1404 pwcxt);
1405 break;
1406 case T_HashState:
1407 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1409 break;
1410 case T_SortState:
1411 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1413 break;
1415 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1417 pwcxt);
1418 break;
1419 case T_AggState:
1420 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1421 ExecAggInitializeWorker((AggState *) planstate, pwcxt);
1422 break;
1423 case T_MemoizeState:
1424 /* even when not parallel-aware, for EXPLAIN ANALYZE */
1426 break;
1427 default:
1428 break;
1429 }
1430
1432 pwcxt);
1433}
1434
1435/*
1436 * Main entrypoint for parallel query worker processes.
1437 *
1438 * We reach this function from ParallelWorkerMain, so the setup necessary to
1439 * create a sensible parallel environment has already been done;
1440 * ParallelWorkerMain worries about stuff like the transaction state, combo
1441 * CID mappings, and GUC values, so we don't need to deal with any of that
1442 * here.
1443 *
1444 * Our job is to deal with concerns specific to the executor. The parallel
1445 * group leader will have stored a serialized PlannedStmt, and it's our job
1446 * to execute that plan and write the resulting tuples to the appropriate
1447 * tuple queue. Various bits of supporting information that we need in order
1448 * to do this are also stored in the dsm_segment and can be accessed through
1449 * the shm_toc.
1450 */
1451void
1453{
1455 BufferUsage *buffer_usage;
1456 WalUsage *wal_usage;
1458 QueryDesc *queryDesc;
1459 SharedExecutorInstrumentation *instrumentation;
1460 SharedJitInstrumentation *jit_instrumentation;
1461 int instrument_options = 0;
1462 void *area_space;
1463 dsa_area *area;
1465
1466 /* Get fixed-size state. */
1468
1469 /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1471 instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1472 if (instrumentation != NULL)
1473 instrument_options = instrumentation->instrument_options;
1474 jit_instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
1475 true);
1476 queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1477
1478 /* Setting debug_query_string for individual workers */
1479 debug_query_string = queryDesc->sourceText;
1480
1481 /* Report workers' query for monitoring purposes */
1483
1484 /* Attach to the dynamic shared memory area. */
1486 area = dsa_attach_in_place(area_space, seg);
1487
1488 /* Start up the executor */
1489 queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1490 ExecutorStart(queryDesc, fpes->eflags);
1491
1492 /* Special executor initialization steps for parallel workers */
1493 queryDesc->planstate->state->es_query_dsa = area;
1494 if (DsaPointerIsValid(fpes->param_exec))
1495 {
1496 char *paramexec_space;
1497
1498 paramexec_space = dsa_get_address(area, fpes->param_exec);
1500 }
1501 pwcxt.toc = toc;
1502 pwcxt.seg = seg;
1504
1505 /* Pass down any tuple bound */
1506 ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1507
1508 /*
1509 * Prepare to track buffer/WAL usage during query execution.
1510 *
1511 * We do this after starting up the executor to match what happens in the
1512 * leader, which also doesn't count buffer accesses and WAL activity that
1513 * occur during executor startup.
1514 */
1516
1517 /*
1518 * Run the plan. If we specified a tuple bound, be careful not to demand
1519 * more tuples than that.
1520 */
1521 ExecutorRun(queryDesc,
1523 fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed);
1524
1525 /* Shut down the executor */
1526 ExecutorFinish(queryDesc);
1527
1528 /* Report buffer/WAL usage during parallel execution. */
1529 buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1530 wal_usage = shm_toc_lookup(toc, PARALLEL_KEY_WAL_USAGE, false);
1532 &wal_usage[ParallelWorkerNumber]);
1533
1534 /* Report instrumentation data if any instrumentation options are set. */
1535 if (instrumentation != NULL)
1537 instrumentation);
1538
1539 /* Report JIT instrumentation data if any */
1540 if (queryDesc->estate->es_jit && jit_instrumentation != NULL)
1541 {
1543 jit_instrumentation->jit_instr[ParallelWorkerNumber] =
1544 queryDesc->estate->es_jit->instr;
1545 }
1546
1547 /* Must do this after capturing instrumentation. */
1548 ExecutorEnd(queryDesc);
1549
1550 /* Cleanup. */
1551 dsa_detach(area);
1552 FreeQueryDesc(queryDesc);
1553 receiver->rDestroy(receiver);
1554}
int ParallelWorkerNumber
Definition parallel.c:116
void InitializeParallelDSM(ParallelContext *pcxt)
Definition parallel.c:212
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition parallel.c:804
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition parallel.c:510
void DestroyParallelContext(ParallelContext *pcxt)
Definition parallel.c:958
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
Definition parallel.c:174
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:838
#define Assert(condition)
Definition c.h:885
int64_t int64
Definition c.h:555
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:492
int16_t int16
Definition c.h:553
#define OidIsValid(objectId)
Definition c.h:800
size_t Size
Definition c.h:631
Datum datumRestore(char **start_address, bool *isnull)
Definition datum.c:523
void datumSerialize(Datum value, bool isnull, bool typByVal, int typLen, char **start_address)
Definition datum.c:461
Size datumEstimateSpace(Datum value, bool isnull, bool typByVal, int typLen)
Definition datum.c:414
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:39
#define elog(elevel,...)
Definition elog.h:226
void ExecutorEnd(QueryDesc *queryDesc)
Definition execMain.c:466
void ExecutorFinish(QueryDesc *queryDesc)
Definition execMain.c:406
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition execMain.c:122
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
Definition execMain.c:297
#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:656
#define palloc0_object(type)
Definition fe_memutils.h:75
#define IsParallelWorker()
Definition parallel.h:62
void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition instrument.c:219
void InstrEndLoop(Instrumentation *instr)
Definition instrument.c:144
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition instrument.c:169
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition instrument.c:209
void InstrStartParallelQuery(void)
Definition instrument.c:201
void InstrInit(Instrumentation *instr, int instrument_options)
Definition instrument.c:58
int i
Definition isn.c:77
void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
Definition jit.c:182
#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:2401
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:4779
void ExecAggInitializeWorker(AggState *node, ParallelWorkerContext *pwcxt)
Definition nodeAgg.c:4825
void ExecAggRetrieveInstrumentation(AggState *node)
Definition nodeAgg.c:4838
void ExecAggInitializeDSM(AggState *node, ParallelContext *pcxt)
Definition nodeAgg.c:4800
void ExecAppendReInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition nodeAppend.c:540
void ExecAppendInitializeWorker(AppendState *node, ParallelWorkerContext *pwcxt)
Definition nodeAppend.c:556
void ExecAppendInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition nodeAppend.c:519
void ExecAppendEstimate(AppendState *node, ParallelContext *pcxt)
Definition nodeAppend.c:500
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 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:2779
void ExecHashInitializeWorker(HashState *node, ParallelWorkerContext *pwcxt)
Definition nodeHash.c:2804
void ExecHashEstimate(HashState *node, ParallelContext *pcxt)
Definition nodeHash.c:2760
void ExecHashRetrieveInstrumentation(HashState *node)
Definition nodeHash.c:2845
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 ExecIndexOnlyScanRetrieveInstrumentation(IndexOnlyScanState *node)
void ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
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 ExecIndexScanInitializeDSM(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 ExecSeqScanReInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
void ExecSeqScanInitializeWorker(SeqScanState *node, ParallelWorkerContext *pwcxt)
void ExecSeqScanInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
void ExecSeqScanEstimate(SeqScanState *node, ParallelContext *pcxt)
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 ExecTidRangeScanEstimate(TidRangeScanState *node, ParallelContext *pcxt)
void ExecTidRangeScanInitializeWorker(TidRangeScanState *node, ParallelWorkerContext *pwcxt)
void ExecTidRangeScanInitializeDSM(TidRangeScanState *node, ParallelContext *pcxt)
void ExecTidRangeScanReInitializeDSM(TidRangeScanState *node, ParallelContext *pcxt)
#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:802
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:321
#define plan(x)
Definition pg_regress.c:161
@ PLAN_STMT_INTERNAL
Definition plannodes.h:40
const char * debug_query_string
Definition postgres.c:90
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
void FreeQueryDesc(QueryDesc *qdesc)
Definition pquery.c:106
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition pquery.c:68
e
static int fb(int x)
void * stringToNode(const char *str)
Definition read.c:90
@ ForwardScanDirection
Definition sdir.h:28
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition shm_mq.c:225
shm_mq * shm_mq_create(void *address, Size size)
Definition shm_mq.c:178
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition shm_mq.c:320
void shm_mq_detach(shm_mq_handle *mqh)
Definition shm_mq.c:844
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition shm_mq.c:207
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition shm_mq.c:291
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:232
#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:482
Size mul_size(Size s1, Size s2)
Definition shmem.c:497
Snapshot GetActiveSnapshot(void)
Definition snapmgr.c:800
#define InvalidSnapshot
Definition snapshot.h:119
PGPROC * MyProc
Definition proc.c:67
List * es_part_prune_infos
Definition execnodes.h:673
struct dsa_area * es_query_dsa
Definition execnodes.h:755
int es_top_eflags
Definition execnodes.h:722
struct JitContext * es_jit
Definition execnodes.h:767
int es_instrument
Definition execnodes.h:723
PlannedStmt * es_plannedstmt
Definition execnodes.h:672
struct JitInstrumentation * es_jit_worker_instr
Definition execnodes.h:768
ParamExecData * es_param_exec_vals
Definition execnodes.h:708
List * es_range_table
Definition execnodes.h:665
List * es_rteperminfos
Definition execnodes.h:671
Bitmapset * es_unpruned_relids
Definition execnodes.h:676
ParamListInfo es_param_list_info
Definition execnodes.h:707
MemoryContext es_query_cxt
Definition execnodes.h:713
int es_jit_flags
Definition execnodes.h:766
const char * es_sourceText
Definition execnodes.h:680
Snapshot es_snapshot
Definition execnodes.h:663
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:1182
Instrumentation * instrument
Definition execnodes.h:1178
Plan * plan
Definition execnodes.h:1168
EState * state
Definition execnodes.h:1170
WorkerInstrumentation * worker_instrument
Definition execnodes.h:1179
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:101
bool hasModifyingCTE
Definition plannodes.h:83
List * appendRelations
Definition plannodes.h:127
List * permInfos
Definition plannodes.h:120
bool canSetTag
Definition plannodes.h:86
List * rowMarks
Definition plannodes.h:141
int64 planId
Definition plannodes.h:74
Bitmapset * rewindPlanIDs
Definition plannodes.h:138
int64 queryId
Definition plannodes.h:71
ParseLoc stmt_len
Definition plannodes.h:171
PlannedStmtOrigin planOrigin
Definition plannodes.h:77
bool hasReturning
Definition plannodes.h:80
ParseLoc stmt_location
Definition plannodes.h:169
List * invalItems
Definition plannodes.h:147
bool transientPlan
Definition plannodes.h:89
List * resultRelations
Definition plannodes.h:124
List * subplans
Definition plannodes.h:132
List * relationOids
Definition plannodes.h:144
bool dependsOnRole
Definition plannodes.h:92
Bitmapset * unprunableRelids
Definition plannodes.h:115
CmdType commandType
Definition plannodes.h:68
Node * utilityStmt
Definition plannodes.h:153
List * rtable
Definition plannodes.h:109
List * partPruneInfos
Definition plannodes.h:106
List * paramExecTypes
Definition plannodes.h:150
bool parallelModeNeeded
Definition plannodes.h:95
const char * sourceText
Definition execdesc.h:38
EState * estate
Definition execdesc.h:48
PlannedStmt * plannedstmt
Definition execdesc.h:37
PlanState * planstate
Definition execdesc.h:49
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER]
Definition jit.h:54
Instrumentation instrument[FLEXIBLE_ARRAY_MEMBER]
Definition instrument.h:100
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