PostgreSQL Source Code  git master
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-2020, 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 
26 #include "executor/execParallel.h"
27 #include "executor/executor.h"
28 #include "executor/nodeAppend.h"
30 #include "executor/nodeCustom.h"
32 #include "executor/nodeHash.h"
33 #include "executor/nodeHashjoin.h"
36 #include "executor/nodeIndexscan.h"
37 #include "executor/nodeSeqscan.h"
38 #include "executor/nodeSort.h"
39 #include "executor/nodeSubplan.h"
40 #include "executor/tqueue.h"
41 #include "jit/jit.h"
42 #include "nodes/nodeFuncs.h"
43 #include "pgstat.h"
44 #include "storage/spin.h"
45 #include "tcop/tcopprot.h"
46 #include "utils/datum.h"
47 #include "utils/dsa.h"
48 #include "utils/lsyscache.h"
49 #include "utils/memutils.h"
50 #include "utils/snapmgr.h"
51 
52 /*
53  * Magic numbers for parallel executor communication. We use constants
54  * greater than any 32-bit integer here so that values < 2^32 can be used
55  * by individual parallel nodes to store their own state.
56  */
57 #define PARALLEL_KEY_EXECUTOR_FIXED UINT64CONST(0xE000000000000001)
58 #define PARALLEL_KEY_PLANNEDSTMT UINT64CONST(0xE000000000000002)
59 #define PARALLEL_KEY_PARAMLISTINFO UINT64CONST(0xE000000000000003)
60 #define PARALLEL_KEY_BUFFER_USAGE UINT64CONST(0xE000000000000004)
61 #define PARALLEL_KEY_TUPLE_QUEUE UINT64CONST(0xE000000000000005)
62 #define PARALLEL_KEY_INSTRUMENTATION UINT64CONST(0xE000000000000006)
63 #define PARALLEL_KEY_DSA UINT64CONST(0xE000000000000007)
64 #define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xE000000000000008)
65 #define PARALLEL_KEY_JIT_INSTRUMENTATION UINT64CONST(0xE000000000000009)
66 #define PARALLEL_KEY_WAL_USAGE UINT64CONST(0xE00000000000000A)
67 
68 #define PARALLEL_TUPLE_QUEUE_SIZE 65536
69 
70 /*
71  * Fixed-size random stuff that we need to pass to parallel workers.
72  */
74 {
75  int64 tuples_needed; /* tuple bound, see ExecSetTupleBound */
77  int eflags;
78  int jit_flags;
80 
81 /*
82  * DSM structure for accumulating per-PlanState instrumentation.
83  *
84  * instrument_options: Same meaning here as in instrument.c.
85  *
86  * instrument_offset: Offset, relative to the start of this structure,
87  * of the first Instrumentation object. This will depend on the length of
88  * the plan_node_id array.
89  *
90  * num_workers: Number of workers.
91  *
92  * num_plan_nodes: Number of plan nodes.
93  *
94  * plan_node_id: Array of plan nodes for which we are gathering instrumentation
95  * from parallel workers. The length of this array is given by num_plan_nodes.
96  */
98 {
103  int plan_node_id[FLEXIBLE_ARRAY_MEMBER];
104  /* array of num_plan_nodes * num_workers Instrumentation objects follows */
105 };
106 #define GetInstrumentationArray(sei) \
107  (AssertVariableIsOfTypeMacro(sei, SharedExecutorInstrumentation *), \
108  (Instrumentation *) (((char *) sei) + sei->instrument_offset))
109 
110 /* Context object for ExecParallelEstimate. */
112 {
114  int nnodes;
116 
117 /* Context object for ExecParallelInitializeDSM. */
119 {
122  int nnodes;
124 
125 /* Helper functions that run in the parallel leader. */
126 static char *ExecSerializePlan(Plan *plan, EState *estate);
127 static bool ExecParallelEstimate(PlanState *node,
129 static bool ExecParallelInitializeDSM(PlanState *node,
132  bool reinitialize);
133 static bool ExecParallelReInitializeDSM(PlanState *planstate,
134  ParallelContext *pcxt);
135 static bool ExecParallelRetrieveInstrumentation(PlanState *planstate,
136  SharedExecutorInstrumentation *instrumentation);
137 
138 /* Helper function that runs in the parallel worker. */
140 
141 /*
142  * Create a serialized representation of the plan to be sent to each worker.
143  */
144 static char *
146 {
147  PlannedStmt *pstmt;
148  ListCell *lc;
149 
150  /* We can't scribble on the original plan, so make a copy. */
151  plan = copyObject(plan);
152 
153  /*
154  * The worker will start its own copy of the executor, and that copy will
155  * insert a junk filter if the toplevel node has any resjunk entries. We
156  * don't want that to happen, because while resjunk columns shouldn't be
157  * sent back to the user, here the tuples are coming back to another
158  * backend which may very well need them. So mutate the target list
159  * accordingly. This is sort of a hack; there might be better ways to do
160  * this...
161  */
162  foreach(lc, plan->targetlist)
163  {
164  TargetEntry *tle = lfirst_node(TargetEntry, lc);
165 
166  tle->resjunk = false;
167  }
168 
169  /*
170  * Create a dummy PlannedStmt. Most of the fields don't need to be valid
171  * for our purposes, but the worker will need at least a minimal
172  * PlannedStmt to start the executor.
173  */
174  pstmt = makeNode(PlannedStmt);
175  pstmt->commandType = CMD_SELECT;
176  pstmt->queryId = UINT64CONST(0);
177  pstmt->hasReturning = false;
178  pstmt->hasModifyingCTE = false;
179  pstmt->canSetTag = true;
180  pstmt->transientPlan = false;
181  pstmt->dependsOnRole = false;
182  pstmt->parallelModeNeeded = false;
183  pstmt->planTree = plan;
184  pstmt->rtable = estate->es_range_table;
185  pstmt->resultRelations = NIL;
186  pstmt->rootResultRelations = NIL;
187  pstmt->appendRelations = NIL;
188 
189  /*
190  * Transfer only parallel-safe subplans, leaving a NULL "hole" in the list
191  * for unsafe ones (so that the list indexes of the safe ones are
192  * preserved). This positively ensures that the worker won't try to run,
193  * or even do ExecInitNode on, an unsafe subplan. That's important to
194  * protect, eg, non-parallel-aware FDWs from getting into trouble.
195  */
196  pstmt->subplans = NIL;
197  foreach(lc, estate->es_plannedstmt->subplans)
198  {
199  Plan *subplan = (Plan *) lfirst(lc);
200 
201  if (subplan && !subplan->parallel_safe)
202  subplan = NULL;
203  pstmt->subplans = lappend(pstmt->subplans, subplan);
204  }
205 
206  pstmt->rewindPlanIDs = NULL;
207  pstmt->rowMarks = NIL;
208  pstmt->relationOids = NIL;
209  pstmt->invalItems = NIL; /* workers can't replan anyway... */
210  pstmt->paramExecTypes = estate->es_plannedstmt->paramExecTypes;
211  pstmt->utilityStmt = NULL;
212  pstmt->stmt_location = -1;
213  pstmt->stmt_len = -1;
214 
215  /* Return serialized copy of our dummy PlannedStmt. */
216  return nodeToString(pstmt);
217 }
218 
219 /*
220  * Parallel-aware plan nodes (and occasionally others) may need some state
221  * which is shared across all parallel workers. Before we size the DSM, give
222  * them a chance to call shm_toc_estimate_chunk or shm_toc_estimate_keys on
223  * &pcxt->estimator.
224  *
225  * While we're at it, count the number of PlanState nodes in the tree, so
226  * we know how many Instrumentation structures we need.
227  */
228 static bool
230 {
231  if (planstate == NULL)
232  return false;
233 
234  /* Count this node. */
235  e->nnodes++;
236 
237  switch (nodeTag(planstate))
238  {
239  case T_SeqScanState:
240  if (planstate->plan->parallel_aware)
241  ExecSeqScanEstimate((SeqScanState *) planstate,
242  e->pcxt);
243  break;
244  case T_IndexScanState:
245  if (planstate->plan->parallel_aware)
247  e->pcxt);
248  break;
250  if (planstate->plan->parallel_aware)
252  e->pcxt);
253  break;
254  case T_ForeignScanState:
255  if (planstate->plan->parallel_aware)
257  e->pcxt);
258  break;
259  case T_AppendState:
260  if (planstate->plan->parallel_aware)
261  ExecAppendEstimate((AppendState *) planstate,
262  e->pcxt);
263  break;
264  case T_CustomScanState:
265  if (planstate->plan->parallel_aware)
267  e->pcxt);
268  break;
270  if (planstate->plan->parallel_aware)
272  e->pcxt);
273  break;
274  case T_HashJoinState:
275  if (planstate->plan->parallel_aware)
276  ExecHashJoinEstimate((HashJoinState *) planstate,
277  e->pcxt);
278  break;
279  case T_HashState:
280  /* even when not parallel-aware, for EXPLAIN ANALYZE */
281  ExecHashEstimate((HashState *) planstate, e->pcxt);
282  break;
283  case T_SortState:
284  /* even when not parallel-aware, for EXPLAIN ANALYZE */
285  ExecSortEstimate((SortState *) planstate, e->pcxt);
286  break;
288  /* even when not parallel-aware, for EXPLAIN ANALYZE */
290  break;
291 
292  default:
293  break;
294  }
295 
296  return planstate_tree_walker(planstate, ExecParallelEstimate, e);
297 }
298 
299 /*
300  * Estimate the amount of space required to serialize the indicated parameters.
301  */
302 static Size
304 {
305  int paramid;
306  Size sz = sizeof(int);
307 
308  paramid = -1;
309  while ((paramid = bms_next_member(params, paramid)) >= 0)
310  {
311  Oid typeOid;
312  int16 typLen;
313  bool typByVal;
314  ParamExecData *prm;
315 
316  prm = &(estate->es_param_exec_vals[paramid]);
317  typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
318  paramid);
319 
320  sz = add_size(sz, sizeof(int)); /* space for paramid */
321 
322  /* space for datum/isnull */
323  if (OidIsValid(typeOid))
324  get_typlenbyval(typeOid, &typLen, &typByVal);
325  else
326  {
327  /* If no type OID, assume by-value, like copyParamList does. */
328  typLen = sizeof(Datum);
329  typByVal = true;
330  }
331  sz = add_size(sz,
332  datumEstimateSpace(prm->value, prm->isnull,
333  typByVal, typLen));
334  }
335  return sz;
336 }
337 
338 /*
339  * Serialize specified PARAM_EXEC parameters.
340  *
341  * We write the number of parameters first, as a 4-byte integer, and then
342  * write details for each parameter in turn. The details for each parameter
343  * consist of a 4-byte paramid (location of param in execution time internal
344  * parameter array) and then the datum as serialized by datumSerialize().
345  */
346 static dsa_pointer
348 {
349  Size size;
350  int nparams;
351  int paramid;
352  ParamExecData *prm;
353  dsa_pointer handle;
354  char *start_address;
355 
356  /* Allocate enough space for the current parameter values. */
357  size = EstimateParamExecSpace(estate, params);
358  handle = dsa_allocate(area, size);
359  start_address = dsa_get_address(area, handle);
360 
361  /* First write the number of parameters as a 4-byte integer. */
362  nparams = bms_num_members(params);
363  memcpy(start_address, &nparams, sizeof(int));
364  start_address += sizeof(int);
365 
366  /* Write details for each parameter in turn. */
367  paramid = -1;
368  while ((paramid = bms_next_member(params, paramid)) >= 0)
369  {
370  Oid typeOid;
371  int16 typLen;
372  bool typByVal;
373 
374  prm = &(estate->es_param_exec_vals[paramid]);
375  typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
376  paramid);
377 
378  /* Write paramid. */
379  memcpy(start_address, &paramid, sizeof(int));
380  start_address += sizeof(int);
381 
382  /* Write datum/isnull */
383  if (OidIsValid(typeOid))
384  get_typlenbyval(typeOid, &typLen, &typByVal);
385  else
386  {
387  /* If no type OID, assume by-value, like copyParamList does. */
388  typLen = sizeof(Datum);
389  typByVal = true;
390  }
391  datumSerialize(prm->value, prm->isnull, typByVal, typLen,
392  &start_address);
393  }
394 
395  return handle;
396 }
397 
398 /*
399  * Restore specified PARAM_EXEC parameters.
400  */
401 static void
402 RestoreParamExecParams(char *start_address, EState *estate)
403 {
404  int nparams;
405  int i;
406  int paramid;
407 
408  memcpy(&nparams, start_address, sizeof(int));
409  start_address += sizeof(int);
410 
411  for (i = 0; i < nparams; i++)
412  {
413  ParamExecData *prm;
414 
415  /* Read paramid */
416  memcpy(&paramid, start_address, sizeof(int));
417  start_address += sizeof(int);
418  prm = &(estate->es_param_exec_vals[paramid]);
419 
420  /* Read datum/isnull. */
421  prm->value = datumRestore(&start_address, &prm->isnull);
422  prm->execPlan = NULL;
423  }
424 }
425 
426 /*
427  * Initialize the dynamic shared memory segment that will be used to control
428  * parallel execution.
429  */
430 static bool
433 {
434  if (planstate == NULL)
435  return false;
436 
437  /* If instrumentation is enabled, initialize slot for this node. */
438  if (d->instrumentation != NULL)
440  planstate->plan->plan_node_id;
441 
442  /* Count this node. */
443  d->nnodes++;
444 
445  /*
446  * Call initializers for DSM-using plan nodes.
447  *
448  * Most plan nodes won't do anything here, but plan nodes that allocated
449  * DSM may need to initialize shared state in the DSM before parallel
450  * workers are launched. They can allocate the space they previously
451  * estimated using shm_toc_allocate, and add the keys they previously
452  * estimated using shm_toc_insert, in each case targeting pcxt->toc.
453  */
454  switch (nodeTag(planstate))
455  {
456  case T_SeqScanState:
457  if (planstate->plan->parallel_aware)
459  d->pcxt);
460  break;
461  case T_IndexScanState:
462  if (planstate->plan->parallel_aware)
464  d->pcxt);
465  break;
467  if (planstate->plan->parallel_aware)
469  d->pcxt);
470  break;
471  case T_ForeignScanState:
472  if (planstate->plan->parallel_aware)
474  d->pcxt);
475  break;
476  case T_AppendState:
477  if (planstate->plan->parallel_aware)
478  ExecAppendInitializeDSM((AppendState *) planstate,
479  d->pcxt);
480  break;
481  case T_CustomScanState:
482  if (planstate->plan->parallel_aware)
484  d->pcxt);
485  break;
487  if (planstate->plan->parallel_aware)
489  d->pcxt);
490  break;
491  case T_HashJoinState:
492  if (planstate->plan->parallel_aware)
494  d->pcxt);
495  break;
496  case T_HashState:
497  /* even when not parallel-aware, for EXPLAIN ANALYZE */
498  ExecHashInitializeDSM((HashState *) planstate, d->pcxt);
499  break;
500  case T_SortState:
501  /* even when not parallel-aware, for EXPLAIN ANALYZE */
502  ExecSortInitializeDSM((SortState *) planstate, d->pcxt);
503  break;
505  /* even when not parallel-aware, for EXPLAIN ANALYZE */
507  break;
508 
509  default:
510  break;
511  }
512 
513  return planstate_tree_walker(planstate, ExecParallelInitializeDSM, d);
514 }
515 
516 /*
517  * It sets up the response queues for backend workers to return tuples
518  * to the main backend and start the workers.
519  */
520 static shm_mq_handle **
522 {
523  shm_mq_handle **responseq;
524  char *tqueuespace;
525  int i;
526 
527  /* Skip this if no workers. */
528  if (pcxt->nworkers == 0)
529  return NULL;
530 
531  /* Allocate memory for shared memory queue handles. */
532  responseq = (shm_mq_handle **)
533  palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
534 
535  /*
536  * If not reinitializing, allocate space from the DSM for the queues;
537  * otherwise, find the already allocated space.
538  */
539  if (!reinitialize)
540  tqueuespace =
541  shm_toc_allocate(pcxt->toc,
543  pcxt->nworkers));
544  else
545  tqueuespace = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, false);
546 
547  /* Create the queues, and become the receiver for each. */
548  for (i = 0; i < pcxt->nworkers; ++i)
549  {
550  shm_mq *mq;
551 
552  mq = shm_mq_create(tqueuespace +
554  (Size) PARALLEL_TUPLE_QUEUE_SIZE);
555 
557  responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
558  }
559 
560  /* Add array of queues to shm_toc, so others can find it. */
561  if (!reinitialize)
562  shm_toc_insert(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, tqueuespace);
563 
564  /* Return array of handles. */
565  return responseq;
566 }
567 
568 /*
569  * Sets up the required infrastructure for backend workers to perform
570  * execution and return results to the main backend.
571  */
574  Bitmapset *sendParams, int nworkers,
575  int64 tuples_needed)
576 {
578  ParallelContext *pcxt;
582  char *pstmt_data;
583  char *pstmt_space;
584  char *paramlistinfo_space;
585  BufferUsage *bufusage_space;
586  WalUsage *walusage_space;
587  SharedExecutorInstrumentation *instrumentation = NULL;
588  SharedJitInstrumentation *jit_instrumentation = NULL;
589  int pstmt_len;
590  int paramlistinfo_len;
591  int instrumentation_len = 0;
592  int jit_instrumentation_len = 0;
593  int instrument_offset = 0;
594  Size dsa_minsize = dsa_minimum_size();
595  char *query_string;
596  int query_len;
597 
598  /*
599  * Force any initplan outputs that we're going to pass to workers to be
600  * evaluated, if they weren't already.
601  *
602  * For simplicity, we use the EState's per-output-tuple ExprContext here.
603  * That risks intra-query memory leakage, since we might pass through here
604  * many times before that ExprContext gets reset; but ExecSetParamPlan
605  * doesn't normally leak any memory in the context (see its comments), so
606  * it doesn't seem worth complicating this function's API to pass it a
607  * shorter-lived ExprContext. This might need to change someday.
608  */
609  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
610 
611  /* Allocate object for return value. */
612  pei = palloc0(sizeof(ParallelExecutorInfo));
613  pei->finished = false;
614  pei->planstate = planstate;
615 
616  /* Fix up and serialize plan to be sent to workers. */
617  pstmt_data = ExecSerializePlan(planstate->plan, estate);
618 
619  /* Create a parallel context. */
620  pcxt = CreateParallelContext("postgres", "ParallelQueryMain", nworkers);
621  pei->pcxt = pcxt;
622 
623  /*
624  * Before telling the parallel context to create a dynamic shared memory
625  * segment, we need to figure out how big it should be. Estimate space
626  * for the various things we need to store.
627  */
628 
629  /* Estimate space for fixed-size state. */
632  shm_toc_estimate_keys(&pcxt->estimator, 1);
633 
634  /* Estimate space for query text. */
635  query_len = strlen(estate->es_sourceText);
636  shm_toc_estimate_chunk(&pcxt->estimator, query_len + 1);
637  shm_toc_estimate_keys(&pcxt->estimator, 1);
638 
639  /* Estimate space for serialized PlannedStmt. */
640  pstmt_len = strlen(pstmt_data) + 1;
641  shm_toc_estimate_chunk(&pcxt->estimator, pstmt_len);
642  shm_toc_estimate_keys(&pcxt->estimator, 1);
643 
644  /* Estimate space for serialized ParamListInfo. */
645  paramlistinfo_len = EstimateParamListSpace(estate->es_param_list_info);
646  shm_toc_estimate_chunk(&pcxt->estimator, paramlistinfo_len);
647  shm_toc_estimate_keys(&pcxt->estimator, 1);
648 
649  /*
650  * Estimate space for BufferUsage.
651  *
652  * If EXPLAIN is not in use and there are no extensions loaded that care,
653  * we could skip this. But we have no way of knowing whether anyone's
654  * looking at pgBufferUsage, so do it unconditionally.
655  */
657  mul_size(sizeof(BufferUsage), pcxt->nworkers));
658  shm_toc_estimate_keys(&pcxt->estimator, 1);
659 
660  /*
661  * Same thing for WalUsage.
662  */
664  mul_size(sizeof(WalUsage), pcxt->nworkers));
665  shm_toc_estimate_keys(&pcxt->estimator, 1);
666 
667  /* Estimate space for tuple queues. */
670  shm_toc_estimate_keys(&pcxt->estimator, 1);
671 
672  /*
673  * Give parallel-aware nodes a chance to add to the estimates, and get a
674  * count of how many PlanState nodes there are.
675  */
676  e.pcxt = pcxt;
677  e.nnodes = 0;
678  ExecParallelEstimate(planstate, &e);
679 
680  /* Estimate space for instrumentation, if required. */
681  if (estate->es_instrument)
682  {
683  instrumentation_len =
684  offsetof(SharedExecutorInstrumentation, plan_node_id) +
685  sizeof(int) * e.nnodes;
686  instrumentation_len = MAXALIGN(instrumentation_len);
687  instrument_offset = instrumentation_len;
688  instrumentation_len +=
689  mul_size(sizeof(Instrumentation),
690  mul_size(e.nnodes, nworkers));
691  shm_toc_estimate_chunk(&pcxt->estimator, instrumentation_len);
692  shm_toc_estimate_keys(&pcxt->estimator, 1);
693 
694  /* Estimate space for JIT instrumentation, if required. */
695  if (estate->es_jit_flags != PGJIT_NONE)
696  {
697  jit_instrumentation_len =
698  offsetof(SharedJitInstrumentation, jit_instr) +
699  sizeof(JitInstrumentation) * nworkers;
700  shm_toc_estimate_chunk(&pcxt->estimator, jit_instrumentation_len);
701  shm_toc_estimate_keys(&pcxt->estimator, 1);
702  }
703  }
704 
705  /* Estimate space for DSA area. */
706  shm_toc_estimate_chunk(&pcxt->estimator, dsa_minsize);
707  shm_toc_estimate_keys(&pcxt->estimator, 1);
708 
709  /* Everyone's had a chance to ask for space, so now create the DSM. */
710  InitializeParallelDSM(pcxt);
711 
712  /*
713  * OK, now we have a dynamic shared memory segment, and it should be big
714  * enough to store all of the data we estimated we would want to put into
715  * it, plus whatever general stuff (not specifically executor-related) the
716  * ParallelContext itself needs to store there. None of the space we
717  * asked for has been allocated or initialized yet, though, so do that.
718  */
719 
720  /* Store fixed-size state. */
721  fpes = shm_toc_allocate(pcxt->toc, sizeof(FixedParallelExecutorState));
724  fpes->eflags = estate->es_top_eflags;
725  fpes->jit_flags = estate->es_jit_flags;
727 
728  /* Store query string */
729  query_string = shm_toc_allocate(pcxt->toc, query_len + 1);
730  memcpy(query_string, estate->es_sourceText, query_len + 1);
731  shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string);
732 
733  /* Store serialized PlannedStmt. */
734  pstmt_space = shm_toc_allocate(pcxt->toc, pstmt_len);
735  memcpy(pstmt_space, pstmt_data, pstmt_len);
736  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PLANNEDSTMT, pstmt_space);
737 
738  /* Store serialized ParamListInfo. */
739  paramlistinfo_space = shm_toc_allocate(pcxt->toc, paramlistinfo_len);
740  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PARAMLISTINFO, paramlistinfo_space);
741  SerializeParamList(estate->es_param_list_info, &paramlistinfo_space);
742 
743  /* Allocate space for each worker's BufferUsage; no need to initialize. */
744  bufusage_space = shm_toc_allocate(pcxt->toc,
745  mul_size(sizeof(BufferUsage), pcxt->nworkers));
746  shm_toc_insert(pcxt->toc, PARALLEL_KEY_BUFFER_USAGE, bufusage_space);
747  pei->buffer_usage = bufusage_space;
748 
749  /* Same for WalUsage. */
750  walusage_space = shm_toc_allocate(pcxt->toc,
751  mul_size(sizeof(WalUsage), pcxt->nworkers));
752  shm_toc_insert(pcxt->toc, PARALLEL_KEY_WAL_USAGE, walusage_space);
753  pei->wal_usage = walusage_space;
754 
755  /* Set up the tuple queues that the workers will write into. */
756  pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
757 
758  /* We don't need the TupleQueueReaders yet, though. */
759  pei->reader = NULL;
760 
761  /*
762  * If instrumentation options were supplied, allocate space for the data.
763  * It only gets partially initialized here; the rest happens during
764  * ExecParallelInitializeDSM.
765  */
766  if (estate->es_instrument)
767  {
768  Instrumentation *instrument;
769  int i;
770 
771  instrumentation = shm_toc_allocate(pcxt->toc, instrumentation_len);
772  instrumentation->instrument_options = estate->es_instrument;
773  instrumentation->instrument_offset = instrument_offset;
774  instrumentation->num_workers = nworkers;
775  instrumentation->num_plan_nodes = e.nnodes;
776  instrument = GetInstrumentationArray(instrumentation);
777  for (i = 0; i < nworkers * e.nnodes; ++i)
778  InstrInit(&instrument[i], estate->es_instrument);
780  instrumentation);
781  pei->instrumentation = instrumentation;
782 
783  if (estate->es_jit_flags != PGJIT_NONE)
784  {
785  jit_instrumentation = shm_toc_allocate(pcxt->toc,
786  jit_instrumentation_len);
787  jit_instrumentation->num_workers = nworkers;
788  memset(jit_instrumentation->jit_instr, 0,
789  sizeof(JitInstrumentation) * nworkers);
791  jit_instrumentation);
792  pei->jit_instrumentation = jit_instrumentation;
793  }
794  }
795 
796  /*
797  * Create a DSA area that can be used by the leader and all workers.
798  * (However, if we failed to create a DSM and are using private memory
799  * instead, then skip this.)
800  */
801  if (pcxt->seg != NULL)
802  {
803  char *area_space;
804 
805  area_space = shm_toc_allocate(pcxt->toc, dsa_minsize);
806  shm_toc_insert(pcxt->toc, PARALLEL_KEY_DSA, area_space);
807  pei->area = dsa_create_in_place(area_space, dsa_minsize,
809  pcxt->seg);
810 
811  /*
812  * Serialize parameters, if any, using DSA storage. We don't dare use
813  * the main parallel query DSM for this because we might relaunch
814  * workers after the values have changed (and thus the amount of
815  * storage required has changed).
816  */
817  if (!bms_is_empty(sendParams))
818  {
819  pei->param_exec = SerializeParamExecParams(estate, sendParams,
820  pei->area);
821  fpes->param_exec = pei->param_exec;
822  }
823  }
824 
825  /*
826  * Give parallel-aware nodes a chance to initialize their shared data.
827  * This also initializes the elements of instrumentation->ps_instrument,
828  * if it exists.
829  */
830  d.pcxt = pcxt;
831  d.instrumentation = instrumentation;
832  d.nnodes = 0;
833 
834  /* Install our DSA area while initializing the plan. */
835  estate->es_query_dsa = pei->area;
836  ExecParallelInitializeDSM(planstate, &d);
837  estate->es_query_dsa = NULL;
838 
839  /*
840  * Make sure that the world hasn't shifted under our feet. This could
841  * probably just be an Assert(), but let's be conservative for now.
842  */
843  if (e.nnodes != d.nnodes)
844  elog(ERROR, "inconsistent count of PlanState nodes");
845 
846  /* OK, we're ready to rock and roll. */
847  return pei;
848 }
849 
850 /*
851  * Set up tuple queue readers to read the results of a parallel subplan.
852  *
853  * This is separate from ExecInitParallelPlan() because we can launch the
854  * worker processes and let them start doing something before we do this.
855  */
856 void
858 {
859  int nworkers = pei->pcxt->nworkers_launched;
860  int i;
861 
862  Assert(pei->reader == NULL);
863 
864  if (nworkers > 0)
865  {
866  pei->reader = (TupleQueueReader **)
867  palloc(nworkers * sizeof(TupleQueueReader *));
868 
869  for (i = 0; i < nworkers; i++)
870  {
871  shm_mq_set_handle(pei->tqueue[i],
872  pei->pcxt->worker[i].bgwhandle);
873  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
874  }
875  }
876 }
877 
878 /*
879  * Re-initialize the parallel executor shared memory state before launching
880  * a fresh batch of workers.
881  */
882 void
885  Bitmapset *sendParams)
886 {
887  EState *estate = planstate->state;
889 
890  /* Old workers must already be shut down */
891  Assert(pei->finished);
892 
893  /*
894  * Force any initplan outputs that we're going to pass to workers to be
895  * evaluated, if they weren't already (see comments in
896  * ExecInitParallelPlan).
897  */
898  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
899 
901  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
902  pei->reader = NULL;
903  pei->finished = false;
904 
905  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
906 
907  /* Free any serialized parameters from the last round. */
908  if (DsaPointerIsValid(fpes->param_exec))
909  {
910  dsa_free(pei->area, fpes->param_exec);
912  }
913 
914  /* Serialize current parameter values if required. */
915  if (!bms_is_empty(sendParams))
916  {
917  pei->param_exec = SerializeParamExecParams(estate, sendParams,
918  pei->area);
919  fpes->param_exec = pei->param_exec;
920  }
921 
922  /* Traverse plan tree and let each child node reset associated state. */
923  estate->es_query_dsa = pei->area;
924  ExecParallelReInitializeDSM(planstate, pei->pcxt);
925  estate->es_query_dsa = NULL;
926 }
927 
928 /*
929  * Traverse plan tree to reinitialize per-node dynamic shared memory state
930  */
931 static bool
933  ParallelContext *pcxt)
934 {
935  if (planstate == NULL)
936  return false;
937 
938  /*
939  * Call reinitializers for DSM-using plan nodes.
940  */
941  switch (nodeTag(planstate))
942  {
943  case T_SeqScanState:
944  if (planstate->plan->parallel_aware)
946  pcxt);
947  break;
948  case T_IndexScanState:
949  if (planstate->plan->parallel_aware)
951  pcxt);
952  break;
954  if (planstate->plan->parallel_aware)
956  pcxt);
957  break;
958  case T_ForeignScanState:
959  if (planstate->plan->parallel_aware)
961  pcxt);
962  break;
963  case T_AppendState:
964  if (planstate->plan->parallel_aware)
965  ExecAppendReInitializeDSM((AppendState *) planstate, pcxt);
966  break;
967  case T_CustomScanState:
968  if (planstate->plan->parallel_aware)
970  pcxt);
971  break;
973  if (planstate->plan->parallel_aware)
975  pcxt);
976  break;
977  case T_HashJoinState:
978  if (planstate->plan->parallel_aware)
980  pcxt);
981  break;
982  case T_HashState:
983  case T_SortState:
985  /* these nodes have DSM state, but no reinitialization is required */
986  break;
987 
988  default:
989  break;
990  }
991 
992  return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt);
993 }
994 
995 /*
996  * Copy instrumentation information about this node and its descendants from
997  * dynamic shared memory.
998  */
999 static bool
1001  SharedExecutorInstrumentation *instrumentation)
1002 {
1003  Instrumentation *instrument;
1004  int i;
1005  int n;
1006  int ibytes;
1007  int plan_node_id = planstate->plan->plan_node_id;
1008  MemoryContext oldcontext;
1009 
1010  /* Find the instrumentation for this node. */
1011  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1012  if (instrumentation->plan_node_id[i] == plan_node_id)
1013  break;
1014  if (i >= instrumentation->num_plan_nodes)
1015  elog(ERROR, "plan node %d not found", plan_node_id);
1016 
1017  /* Accumulate the statistics from all workers. */
1018  instrument = GetInstrumentationArray(instrumentation);
1019  instrument += i * instrumentation->num_workers;
1020  for (n = 0; n < instrumentation->num_workers; ++n)
1021  InstrAggNode(planstate->instrument, &instrument[n]);
1022 
1023  /*
1024  * Also store the per-worker detail.
1025  *
1026  * Worker instrumentation should be allocated in the same context as the
1027  * regular instrumentation information, which is the per-query context.
1028  * Switch into per-query memory context.
1029  */
1030  oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
1031  ibytes = mul_size(instrumentation->num_workers, sizeof(Instrumentation));
1032  planstate->worker_instrument =
1033  palloc(ibytes + offsetof(WorkerInstrumentation, instrument));
1034  MemoryContextSwitchTo(oldcontext);
1035 
1036  planstate->worker_instrument->num_workers = instrumentation->num_workers;
1037  memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
1038 
1039  /* Perform any node-type-specific work that needs to be done. */
1040  switch (nodeTag(planstate))
1041  {
1042  case T_SortState:
1044  break;
1047  break;
1048  case T_HashState:
1050  break;
1051  default:
1052  break;
1053  }
1054 
1056  instrumentation);
1057 }
1058 
1059 /*
1060  * Add up the workers' JIT instrumentation from dynamic shared memory.
1061  */
1062 static void
1064  SharedJitInstrumentation *shared_jit)
1065 {
1066  JitInstrumentation *combined;
1067  int ibytes;
1068 
1069  int n;
1070 
1071  /*
1072  * Accumulate worker JIT instrumentation into the combined JIT
1073  * instrumentation, allocating it if required.
1074  */
1075  if (!planstate->state->es_jit_worker_instr)
1076  planstate->state->es_jit_worker_instr =
1078  combined = planstate->state->es_jit_worker_instr;
1079 
1080  /* Accumulate all the workers' instrumentations. */
1081  for (n = 0; n < shared_jit->num_workers; ++n)
1082  InstrJitAgg(combined, &shared_jit->jit_instr[n]);
1083 
1084  /*
1085  * Store the per-worker detail.
1086  *
1087  * Similar to ExecParallelRetrieveInstrumentation(), allocate the
1088  * instrumentation in per-query context.
1089  */
1090  ibytes = offsetof(SharedJitInstrumentation, jit_instr)
1091  + mul_size(shared_jit->num_workers, sizeof(JitInstrumentation));
1092  planstate->worker_jit_instrument =
1093  MemoryContextAlloc(planstate->state->es_query_cxt, ibytes);
1094 
1095  memcpy(planstate->worker_jit_instrument, shared_jit, ibytes);
1096 }
1097 
1098 /*
1099  * Finish parallel execution. We wait for parallel workers to finish, and
1100  * accumulate their buffer/WAL usage.
1101  */
1102 void
1104 {
1105  int nworkers = pei->pcxt->nworkers_launched;
1106  int i;
1107 
1108  /* Make this be a no-op if called twice in a row. */
1109  if (pei->finished)
1110  return;
1111 
1112  /*
1113  * Detach from tuple queues ASAP, so that any still-active workers will
1114  * notice that no further results are wanted.
1115  */
1116  if (pei->tqueue != NULL)
1117  {
1118  for (i = 0; i < nworkers; i++)
1119  shm_mq_detach(pei->tqueue[i]);
1120  pfree(pei->tqueue);
1121  pei->tqueue = NULL;
1122  }
1123 
1124  /*
1125  * While we're waiting for the workers to finish, let's get rid of the
1126  * tuple queue readers. (Any other local cleanup could be done here too.)
1127  */
1128  if (pei->reader != NULL)
1129  {
1130  for (i = 0; i < nworkers; i++)
1132  pfree(pei->reader);
1133  pei->reader = NULL;
1134  }
1135 
1136  /* Now wait for the workers to finish. */
1138 
1139  /*
1140  * Next, accumulate buffer/WAL usage. (This must wait for the workers to
1141  * finish, or we might get incomplete data.)
1142  */
1143  for (i = 0; i < nworkers; i++)
1144  InstrAccumParallelQuery(&pei->buffer_usage[i], &pei->wal_usage[i]);
1145 
1146  pei->finished = true;
1147 }
1148 
1149 /*
1150  * Accumulate instrumentation, and then clean up whatever ParallelExecutorInfo
1151  * resources still exist after ExecParallelFinish. We separate these
1152  * routines because someone might want to examine the contents of the DSM
1153  * after ExecParallelFinish and before calling this routine.
1154  */
1155 void
1157 {
1158  /* Accumulate instrumentation, if any. */
1159  if (pei->instrumentation)
1161  pei->instrumentation);
1162 
1163  /* Accumulate JIT instrumentation, if any. */
1164  if (pei->jit_instrumentation)
1166  pei->jit_instrumentation);
1167 
1168  /* Free any serialized parameters. */
1169  if (DsaPointerIsValid(pei->param_exec))
1170  {
1171  dsa_free(pei->area, pei->param_exec);
1173  }
1174  if (pei->area != NULL)
1175  {
1176  dsa_detach(pei->area);
1177  pei->area = NULL;
1178  }
1179  if (pei->pcxt != NULL)
1180  {
1182  pei->pcxt = NULL;
1183  }
1184  pfree(pei);
1185 }
1186 
1187 /*
1188  * Create a DestReceiver to write tuples we produce to the shm_mq designated
1189  * for that purpose.
1190  */
1191 static DestReceiver *
1193 {
1194  char *mqspace;
1195  shm_mq *mq;
1196 
1197  mqspace = shm_toc_lookup(toc, PARALLEL_KEY_TUPLE_QUEUE, false);
1199  mq = (shm_mq *) mqspace;
1201  return CreateTupleQueueDestReceiver(shm_mq_attach(mq, seg, NULL));
1202 }
1203 
1204 /*
1205  * Create a QueryDesc for the PlannedStmt we are to execute, and return it.
1206  */
1207 static QueryDesc *
1209  int instrument_options)
1210 {
1211  char *pstmtspace;
1212  char *paramspace;
1213  PlannedStmt *pstmt;
1214  ParamListInfo paramLI;
1215  char *queryString;
1216 
1217  /* Get the query string from shared memory */
1218  queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1219 
1220  /* Reconstruct leader-supplied PlannedStmt. */
1221  pstmtspace = shm_toc_lookup(toc, PARALLEL_KEY_PLANNEDSTMT, false);
1222  pstmt = (PlannedStmt *) stringToNode(pstmtspace);
1223 
1224  /* Reconstruct ParamListInfo. */
1225  paramspace = shm_toc_lookup(toc, PARALLEL_KEY_PARAMLISTINFO, false);
1226  paramLI = RestoreParamList(&paramspace);
1227 
1228  /* Create a QueryDesc for the query. */
1229  return CreateQueryDesc(pstmt,
1230  queryString,
1232  receiver, paramLI, NULL, instrument_options);
1233 }
1234 
1235 /*
1236  * Copy instrumentation information from this node and its descendants into
1237  * dynamic shared memory, so that the parallel leader can retrieve it.
1238  */
1239 static bool
1241  SharedExecutorInstrumentation *instrumentation)
1242 {
1243  int i;
1244  int plan_node_id = planstate->plan->plan_node_id;
1245  Instrumentation *instrument;
1246 
1247  InstrEndLoop(planstate->instrument);
1248 
1249  /*
1250  * If we shuffled the plan_node_id values in ps_instrument into sorted
1251  * order, we could use binary search here. This might matter someday if
1252  * we're pushing down sufficiently large plan trees. For now, do it the
1253  * slow, dumb way.
1254  */
1255  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1256  if (instrumentation->plan_node_id[i] == plan_node_id)
1257  break;
1258  if (i >= instrumentation->num_plan_nodes)
1259  elog(ERROR, "plan node %d not found", plan_node_id);
1260 
1261  /*
1262  * Add our statistics to the per-node, per-worker totals. It's possible
1263  * that this could happen more than once if we relaunched workers.
1264  */
1265  instrument = GetInstrumentationArray(instrumentation);
1266  instrument += i * instrumentation->num_workers;
1268  Assert(ParallelWorkerNumber < instrumentation->num_workers);
1269  InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
1270 
1272  instrumentation);
1273 }
1274 
1275 /*
1276  * Initialize the PlanState and its descendants with the information
1277  * retrieved from shared memory. This has to be done once the PlanState
1278  * is allocated and initialized by executor; that is, after ExecutorStart().
1279  */
1280 static bool
1282 {
1283  if (planstate == NULL)
1284  return false;
1285 
1286  switch (nodeTag(planstate))
1287  {
1288  case T_SeqScanState:
1289  if (planstate->plan->parallel_aware)
1290  ExecSeqScanInitializeWorker((SeqScanState *) planstate, pwcxt);
1291  break;
1292  case T_IndexScanState:
1293  if (planstate->plan->parallel_aware)
1295  pwcxt);
1296  break;
1297  case T_IndexOnlyScanState:
1298  if (planstate->plan->parallel_aware)
1300  pwcxt);
1301  break;
1302  case T_ForeignScanState:
1303  if (planstate->plan->parallel_aware)
1305  pwcxt);
1306  break;
1307  case T_AppendState:
1308  if (planstate->plan->parallel_aware)
1309  ExecAppendInitializeWorker((AppendState *) planstate, pwcxt);
1310  break;
1311  case T_CustomScanState:
1312  if (planstate->plan->parallel_aware)
1314  pwcxt);
1315  break;
1316  case T_BitmapHeapScanState:
1317  if (planstate->plan->parallel_aware)
1319  pwcxt);
1320  break;
1321  case T_HashJoinState:
1322  if (planstate->plan->parallel_aware)
1324  pwcxt);
1325  break;
1326  case T_HashState:
1327  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1328  ExecHashInitializeWorker((HashState *) planstate, pwcxt);
1329  break;
1330  case T_SortState:
1331  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1332  ExecSortInitializeWorker((SortState *) planstate, pwcxt);
1333  break;
1335  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1337  pwcxt);
1338  break;
1339 
1340  default:
1341  break;
1342  }
1343 
1345  pwcxt);
1346 }
1347 
1348 /*
1349  * Main entrypoint for parallel query worker processes.
1350  *
1351  * We reach this function from ParallelWorkerMain, so the setup necessary to
1352  * create a sensible parallel environment has already been done;
1353  * ParallelWorkerMain worries about stuff like the transaction state, combo
1354  * CID mappings, and GUC values, so we don't need to deal with any of that
1355  * here.
1356  *
1357  * Our job is to deal with concerns specific to the executor. The parallel
1358  * group leader will have stored a serialized PlannedStmt, and it's our job
1359  * to execute that plan and write the resulting tuples to the appropriate
1360  * tuple queue. Various bits of supporting information that we need in order
1361  * to do this are also stored in the dsm_segment and can be accessed through
1362  * the shm_toc.
1363  */
1364 void
1366 {
1368  BufferUsage *buffer_usage;
1369  WalUsage *wal_usage;
1370  DestReceiver *receiver;
1371  QueryDesc *queryDesc;
1372  SharedExecutorInstrumentation *instrumentation;
1373  SharedJitInstrumentation *jit_instrumentation;
1374  int instrument_options = 0;
1375  void *area_space;
1376  dsa_area *area;
1377  ParallelWorkerContext pwcxt;
1378 
1379  /* Get fixed-size state. */
1380  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1381 
1382  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1383  receiver = ExecParallelGetReceiver(seg, toc);
1384  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1385  if (instrumentation != NULL)
1386  instrument_options = instrumentation->instrument_options;
1387  jit_instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
1388  true);
1389  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1390 
1391  /* Setting debug_query_string for individual workers */
1392  debug_query_string = queryDesc->sourceText;
1393 
1394  /* Report workers' query for monitoring purposes */
1396 
1397  /* Attach to the dynamic shared memory area. */
1398  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1399  area = dsa_attach_in_place(area_space, seg);
1400 
1401  /* Start up the executor */
1402  queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1403  ExecutorStart(queryDesc, fpes->eflags);
1404 
1405  /* Special executor initialization steps for parallel workers */
1406  queryDesc->planstate->state->es_query_dsa = area;
1407  if (DsaPointerIsValid(fpes->param_exec))
1408  {
1409  char *paramexec_space;
1410 
1411  paramexec_space = dsa_get_address(area, fpes->param_exec);
1412  RestoreParamExecParams(paramexec_space, queryDesc->estate);
1413 
1414  }
1415  pwcxt.toc = toc;
1416  pwcxt.seg = seg;
1417  ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1418 
1419  /* Pass down any tuple bound */
1420  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1421 
1422  /*
1423  * Prepare to track buffer/WAL usage during query execution.
1424  *
1425  * We do this after starting up the executor to match what happens in the
1426  * leader, which also doesn't count buffer accesses and WAL activity that
1427  * occur during executor startup.
1428  */
1430 
1431  /*
1432  * Run the plan. If we specified a tuple bound, be careful not to demand
1433  * more tuples than that.
1434  */
1435  ExecutorRun(queryDesc,
1437  fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
1438  true);
1439 
1440  /* Shut down the executor */
1441  ExecutorFinish(queryDesc);
1442 
1443  /* Report buffer/WAL usage during parallel execution. */
1444  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1445  wal_usage = shm_toc_lookup(toc, PARALLEL_KEY_WAL_USAGE, false);
1447  &wal_usage[ParallelWorkerNumber]);
1448 
1449  /* Report instrumentation data if any instrumentation options are set. */
1450  if (instrumentation != NULL)
1452  instrumentation);
1453 
1454  /* Report JIT instrumentation data if any */
1455  if (queryDesc->estate->es_jit && jit_instrumentation != NULL)
1456  {
1457  Assert(ParallelWorkerNumber < jit_instrumentation->num_workers);
1458  jit_instrumentation->jit_instr[ParallelWorkerNumber] =
1459  queryDesc->estate->es_jit->instr;
1460  }
1461 
1462  /* Must do this after capturing instrumentation. */
1463  ExecutorEnd(queryDesc);
1464 
1465  /* Cleanup. */
1466  dsa_detach(area);
1467  FreeQueryDesc(queryDesc);
1468  receiver->rDestroy(receiver);
1469 }
void ExecForeignScanInitializeWorker(ForeignScanState *node, ParallelWorkerContext *pwcxt)
signed short int16
Definition: c.h:354
bool dependsOnRole
Definition: plannodes.h:58
List * paramExecTypes
Definition: plannodes.h:90
#define NIL
Definition: pg_list.h:65
void ExecIndexScanInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
struct dsa_area * es_query_dsa
Definition: execnodes.h:589
void ExecSetParamPlanMulti(const Bitmapset *params, ExprContext *econtext)
Definition: nodeSubplan.c:1245
void * execPlan
Definition: params.h:148
ParallelContext * pcxt
Definition: execParallel.h:27
void DestroyTupleQueueReader(TupleQueueReader *reader)
Definition: tqueue.c:155
void ExecAppendInitializeWorker(AppendState *node, ParallelWorkerContext *pwcxt)
Definition: nodeAppend.c:423
#define PGJIT_NONE
Definition: jit.h:19
WorkerInstrumentation * worker_instrument
Definition: execnodes.h:956
struct ExecParallelEstimateContext ExecParallelEstimateContext
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:103
EState * estate
Definition: execdesc.h:48
void ExecCustomScanInitializeWorker(CustomScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeCustom.c:206
Instrumentation * instrument
Definition: execnodes.h:955
#define InvalidDsaPointer
Definition: dsa.h:78
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
Definition: parallel.c:164
struct JitContext * es_jit
Definition: execnodes.h:601
void ExecSortEstimate(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:357
void ExecParallelFinish(ParallelExecutorInfo *pei)
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:61
void ExecCustomScanReInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:191
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
void pgstat_report_activity(BackendState state, const char *cmd_str)
Definition: pgstat.c:3130
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:793
PGPROC * MyProc
Definition: proc.c:67
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:150
struct SharedJitInstrumentation * jit_instrumentation
Definition: execParallel.h:31
dsm_segment * seg
Definition: parallel.h:43
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
void ExecAppendReInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:407
List * relationOids
Definition: plannodes.h:86
shm_toc_estimator estimator
Definition: parallel.h:42
void ExecHashJoinReInitializeDSM(HashJoinState *state, ParallelContext *cxt)
struct JitInstrumentation * es_jit_worker_instr
Definition: execnodes.h:602
void ExecSortInitializeWorker(SortState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSort.c:403
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:143
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:541
int plan_node_id
Definition: plannodes.h:141
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:841
PlannedStmt * es_plannedstmt
Definition: execnodes.h:516
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:276
PlanState * planstate
Definition: execParallel.h:26
void * stringToNode(const char *str)
Definition: read.c:89
size_t dsa_minimum_size(void)
Definition: dsa.c:1169
static char * ExecSerializePlan(Plan *plan, EState *estate)
Definition: execParallel.c:145
void ExecIndexScanReInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:521
EState * state
Definition: execnodes.h:947
void ExecIndexScanInitializeWorker(IndexScanState *node, ParallelWorkerContext *pwcxt)
void ExecHashRetrieveInstrumentation(HashState *node)
Definition: nodeHash.c:2642
List * es_range_table
Definition: execnodes.h:510
unsigned int Oid
Definition: postgres_ext.h:31
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:189
bool transientPlan
Definition: plannodes.h:56
WalUsage * wal_usage
Definition: execParallel.h:29
Size EstimateParamListSpace(ParamListInfo paramLI)
Definition: params.c:109
#define OidIsValid(objectId)
Definition: c.h:644
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER]
Definition: jit.h:51
int stmt_len
Definition: plannodes.h:96
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:932
uint64 dsa_pointer
Definition: dsa.h:62
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:347
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:821
struct Plan * planTree
Definition: plannodes.h:64
List * invalItems
Definition: plannodes.h:88
JitInstrumentation instr
Definition: jit.h:61
void ExecForeignScanReInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:299
void InstrEndLoop(Instrumentation *instr)
Definition: instrument.c:121
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:121
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:462
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
ParallelContext * pcxt
Definition: execParallel.c:113
#define GetPerTupleExprContext(estate)
Definition: executor.h:507
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:750
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:62
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:904
const char * es_sourceText
Definition: execnodes.h:517
void ExecSortInitializeDSM(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:378
ParallelWorkerInfo * worker
Definition: parallel.h:46
ParamExecData * es_param_exec_vals
Definition: execnodes.h:550
void pfree(void *pointer)
Definition: mcxt.c:1056
MemoryContext es_query_cxt
Definition: execnodes.h:555
void ExecSeqScanInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:268
static bool ExecParallelInitializeDSM(PlanState *node, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:431
bool resjunk
Definition: primnodes.h:1414
#define ERROR
Definition: elog.h:43
PlanState * planstate
Definition: execdesc.h:49
void ParallelQueryMain(dsm_segment *seg, shm_toc *toc)
void ExecBitmapHeapInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
#define PARALLEL_KEY_WAL_USAGE
Definition: execParallel.c:66
BufferUsage * buffer_usage
Definition: execParallel.h:28
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:301
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
bool isnull
Definition: params.h:150
void ExecIndexOnlyScanEstimate(IndexOnlyScanState *node, ParallelContext *pcxt)
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition: pquery.c:67
#define PARALLEL_KEY_PARAMLISTINFO
Definition: execParallel.c:59
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:169
#define lfirst_node(type, lc)
Definition: pg_list.h:193
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
void ExecParallelCreateReaders(ParallelExecutorInfo *pei)
Definition: execParallel.c:857
#define PARALLEL_KEY_PLANNEDSTMT
Definition: execParallel.c:58
ParamListInfo RestoreParamList(char **start_address)
Definition: params.c:234
int stmt_location
Definition: plannodes.h:95
int es_jit_flags
Definition: execnodes.h:600
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
bool hasReturning
Definition: plannodes.h:50
struct TupleQueueReader ** reader
Definition: execParallel.h:37
struct ExecParallelInitializeDSMContext ExecParallelInitializeDSMContext
void dsa_detach(dsa_area *area)
Definition: dsa.c:1913
List * rootResultRelations
Definition: plannodes.h:75
Node * utilityStmt
Definition: plannodes.h:92
int ParallelWorkerNumber
Definition: parallel.c:112
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
bool parallel_aware
Definition: plannodes.h:135
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:68
int jitFlags
Definition: plannodes.h:62
int es_instrument
Definition: execnodes.h:562
static Size EstimateParamExecSpace(EState *estate, Bitmapset *params)
Definition: execParallel.c:303
int nworkers_launched
Definition: parallel.h:38
void ExecHashInitializeDSM(HashState *node, ParallelContext *pcxt)
Definition: nodeHash.c:2589
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:57
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:229
dsa_area * dsa_create_in_place(void *place, size_t size, int tranche_id, dsm_segment *segment)
Definition: dsa.c:469
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:216
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:60
void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:202
#define IsParallelWorker()
Definition: parallel.h:61
struct SharedJitInstrumentation * worker_jit_instrument
Definition: execnodes.h:959
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:402
void ExecBitmapHeapReInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
List * lappend(List *list, void *datum)
Definition: list.c:321
#define PARALLEL_KEY_JIT_INSTRUMENTATION
Definition: execParallel.c:65
Size datumEstimateSpace(Datum value, bool isnull, bool typByVal, int typLen)
Definition: datum.c:360
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
const char * debug_query_string
Definition: postgres.c:88
void InitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:202
void ExecHashJoinEstimate(HashJoinState *state, ParallelContext *pcxt)
#define InvalidSnapshot
Definition: snapshot.h:123
TupleQueueReader * CreateTupleQueueReader(shm_mq_handle *handle)
Definition: tqueue.c:139
void ExecIncrementalSortInitializeWorker(IncrementalSortState *node, ParallelWorkerContext *pwcxt)
void InstrStartParallelQuery(void)
Definition: instrument.c:181
void InstrInit(Instrumentation *instr, int instrument_options)
Definition: instrument.c:57
void ExecIncrementalSortInitializeDSM(IncrementalSortState *node, ParallelContext *pcxt)
static void ExecParallelRetrieveJitInstrumentation(PlanState *planstate, SharedJitInstrumentation *shared_jit)
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
bool canSetTag
Definition: plannodes.h:54
Instrumentation instrument[FLEXIBLE_ARRAY_MEMBER]
Definition: instrument.h:81
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
CmdType commandType
Definition: plannodes.h:46
Datum datumRestore(char **start_address, bool *isnull)
Definition: datum.c:469
void ExecHashEstimate(HashState *node, ParallelContext *pcxt)
Definition: nodeHash.c:2570
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
Definition: jit.c:184
void ExecForeignScanInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:463
void ExecHashJoinInitializeWorker(HashJoinState *state, ParallelWorkerContext *pwcxt)
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:310
List * rowMarks
Definition: plannodes.h:84
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:839
Plan * plan
Definition: execnodes.h:945
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:175
void ExecAppendInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:386
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:127
void ExecAppendEstimate(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:367
void ExecIncrementalSortRetrieveInstrumentation(IncrementalSortState *node)
#define makeNode(_type_)
Definition: nodes.h:577
void ExecParallelCleanup(ParallelExecutorInfo *pei)
List * subplans
Definition: plannodes.h:79
void SerializeParamList(ParamListInfo paramLI, char **start_address)
Definition: params.c:171
#define Assert(condition)
Definition: c.h:738
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * rewindPlanIDs
Definition: plannodes.h:82
bool hasModifyingCTE
Definition: plannodes.h:52
struct FixedParallelExecutorState FixedParallelExecutorState
void ExecForeignScanEstimate(ForeignScanState *node, ParallelContext *pcxt)
size_t Size
Definition: c.h:466
void ExecSeqScanEstimate(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:250
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2090
#define MAXALIGN(LEN)
Definition: c.h:691
void ExecParallelReinitialize(PlanState *planstate, ParallelExecutorInfo *pei, Bitmapset *sendParams)
Definition: execParallel.c:883
List * rtable
Definition: plannodes.h:66
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
void ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
void ExecIndexOnlyScanReInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
#define nodeTag(nodeptr)
Definition: nodes.h:534
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:282
List * targetlist
Definition: plannodes.h:142
dsa_pointer param_exec
Definition: execParallel.h:33
const char * sourceText
Definition: execdesc.h:38
#define DsaPointerIsValid(x)
Definition: dsa.h:81
#define PARALLEL_KEY_DSA
Definition: execParallel.c:63
void ExecSeqScanReInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:290
void ExecHashJoinInitializeDSM(HashJoinState *state, ParallelContext *pcxt)
e
Definition: preproc-init.c:82
void ExecHashInitializeWorker(HashState *node, ParallelWorkerContext *pwcxt)
Definition: nodeHash.c:2611
Definition: dsa.c:354
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
struct JitInstrumentation JitInstrumentation
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * palloc(Size size)
Definition: mcxt.c:949
uint64 queryId
Definition: plannodes.h:48
int es_top_eflags
Definition: execnodes.h:561
List * resultRelations
Definition: plannodes.h:69
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
bool parallelModeNeeded
Definition: plannodes.h:60
#define elog(elevel,...)
Definition: elog.h:214
void ExecBitmapHeapEstimate(BitmapHeapScanState *node, ParallelContext *pcxt)
int i
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:64
void datumSerialize(Datum value, bool isnull, bool typByVal, int typLen, char **start_address)
Definition: datum.c:407
Definition: shm_mq.c:70
Datum value
Definition: params.h:149
char * nodeToString(const void *obj)
Definition: outfuncs.c:4359
void ExecCustomScanEstimate(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:162
ParamListInfo es_param_list_info
Definition: execnodes.h:549
void ExecIncrementalSortEstimate(IncrementalSortState *node, ParallelContext *pcxt)
void ExecIndexOnlyScanInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
bool parallel_safe
Definition: plannodes.h:136
shm_mq_handle ** tqueue
Definition: execParallel.h:36
ParallelExecutorInfo * ExecInitParallelPlan(PlanState *planstate, EState *estate, Bitmapset *sendParams, int nworkers, int64 tuples_needed)
Definition: execParallel.c:573
static DestReceiver * ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:198
static void RestoreParamExecParams(char *start_address, EState *estate)
Definition: execParallel.c:402
void ExecSortRetrieveInstrumentation(SortState *node)
Definition: nodeSort.c:417
DestReceiver * CreateTupleQueueDestReceiver(shm_mq_handle *handle)
Definition: tqueue.c:119
PlannedStmt * plannedstmt
Definition: execdesc.h:37
#define copyObject(obj)
Definition: nodes.h:645
void ExecBitmapHeapInitializeWorker(BitmapHeapScanState *node, ParallelWorkerContext *pwcxt)
void ExecIndexScanEstimate(IndexScanState *node, ParallelContext *pcxt)
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:30
void ExecSeqScanInitializeWorker(SeqScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSeqscan.c:306
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3882
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
static QueryDesc * ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver, int instrument_options)
dsm_segment * seg
Definition: parallel.h:53
List * appendRelations
Definition: plannodes.h:77
#define offsetof(type, field)
Definition: c.h:661
#define GetInstrumentationArray(sei)
Definition: execParallel.c:106
shm_toc * toc
Definition: parallel.h:45
#define dsa_allocate(area, size)
Definition: dsa.h:84