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