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