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