PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
execParallel.c File Reference
Include dependency graph for execParallel.c:

Go to the source code of this file.

Data Structures

struct  SharedExecutorInstrumentation
 
struct  ExecParallelEstimateContext
 
struct  ExecParallelInitializeDSMContext
 

Macros

#define PARALLEL_KEY_PLANNEDSTMT   UINT64CONST(0xE000000000000001)
 
#define PARALLEL_KEY_PARAMS   UINT64CONST(0xE000000000000002)
 
#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xE000000000000003)
 
#define PARALLEL_KEY_TUPLE_QUEUE   UINT64CONST(0xE000000000000004)
 
#define PARALLEL_KEY_INSTRUMENTATION   UINT64CONST(0xE000000000000005)
 
#define PARALLEL_KEY_DSA   UINT64CONST(0xE000000000000006)
 
#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xE000000000000007)
 
#define PARALLEL_TUPLE_QUEUE_SIZE   65536
 
#define GetInstrumentationArray(sei)
 

Typedefs

typedef struct
ExecParallelEstimateContext 
ExecParallelEstimateContext
 
typedef struct
ExecParallelInitializeDSMContext 
ExecParallelInitializeDSMContext
 

Functions

static char * ExecSerializePlan (Plan *plan, EState *estate)
 
static bool ExecParallelEstimate (PlanState *node, ExecParallelEstimateContext *e)
 
static bool ExecParallelInitializeDSM (PlanState *node, ExecParallelInitializeDSMContext *d)
 
static shm_mq_handle ** ExecParallelSetupTupleQueues (ParallelContext *pcxt, bool reinitialize)
 
static bool ExecParallelRetrieveInstrumentation (PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
 
static DestReceiverExecParallelGetReceiver (dsm_segment *seg, shm_toc *toc)
 
void ExecParallelReinitialize (ParallelExecutorInfo *pei)
 
ParallelExecutorInfoExecInitParallelPlan (PlanState *planstate, EState *estate, int nworkers)
 
void ExecParallelFinish (ParallelExecutorInfo *pei)
 
void ExecParallelCleanup (ParallelExecutorInfo *pei)
 
static QueryDescExecParallelGetQueryDesc (shm_toc *toc, DestReceiver *receiver, int instrument_options)
 
static bool ExecParallelReportInstrumentation (PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
 
static bool ExecParallelInitializeWorker (PlanState *planstate, shm_toc *toc)
 
void ParallelQueryMain (dsm_segment *seg, shm_toc *toc)
 

Macro Definition Documentation

#define GetInstrumentationArray (   sei)
Value:
(Instrumentation *) (((char *) sei) + sei->instrument_offset))
#define AssertVariableIsOfTypeMacro(varname, typename)
Definition: c.h:786

Definition at line 85 of file execParallel.c.

Referenced by ExecInitParallelPlan(), ExecParallelReportInstrumentation(), and ExecParallelRetrieveInstrumentation().

#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xE000000000000003)

Definition at line 52 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

#define PARALLEL_KEY_DSA   UINT64CONST(0xE000000000000006)

Definition at line 55 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

#define PARALLEL_KEY_INSTRUMENTATION   UINT64CONST(0xE000000000000005)

Definition at line 54 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

#define PARALLEL_KEY_PARAMS   UINT64CONST(0xE000000000000002)

Definition at line 51 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

#define PARALLEL_KEY_PLANNEDSTMT   UINT64CONST(0xE000000000000001)

Definition at line 50 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xE000000000000007)

Definition at line 56 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

#define PARALLEL_KEY_TUPLE_QUEUE   UINT64CONST(0xE000000000000004)

Definition at line 53 of file execParallel.c.

Referenced by ExecParallelGetReceiver(), and ExecParallelSetupTupleQueues().

#define PARALLEL_TUPLE_QUEUE_SIZE   65536

Typedef Documentation

Function Documentation

ParallelExecutorInfo* ExecInitParallelPlan ( PlanState planstate,
EState estate,
int  nworkers 
)

Definition at line 384 of file execParallel.c.

References ParallelExecutorInfo::area, ParallelExecutorInfo::buffer_usage, CreateParallelContext(), dsa_create_in_place(), dsa_minimum_size(), elog, ERROR, EState::es_instrument, EState::es_param_list_info, EState::es_query_dsa, EState::es_sourceText, EstimateParamListSpace(), ParallelContext::estimator, ExecParallelEstimate(), ExecParallelInitializeDSM(), ExecParallelSetupTupleQueues(), ExecSerializePlan(), ParallelExecutorInfo::finished, GetInstrumentationArray, i, InitializeParallelDSM(), InstrInit(), SharedExecutorInstrumentation::instrument_offset, SharedExecutorInstrumentation::instrument_options, ParallelExecutorInfo::instrumentation, ExecParallelInitializeDSMContext::instrumentation, LWTRANCHE_PARALLEL_QUERY_DSA, MAXALIGN, mul_size(), ExecParallelEstimateContext::nnodes, ExecParallelInitializeDSMContext::nnodes, NULL, SharedExecutorInstrumentation::num_plan_nodes, SharedExecutorInstrumentation::num_workers, ParallelContext::nworkers, offsetof, palloc0(), PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_DSA, PARALLEL_KEY_INSTRUMENTATION, PARALLEL_KEY_PARAMS, PARALLEL_KEY_PLANNEDSTMT, PARALLEL_KEY_QUERY_TEXT, PARALLEL_TUPLE_QUEUE_SIZE, ParallelExecutorInfo::pcxt, ExecParallelEstimateContext::pcxt, ExecParallelInitializeDSMContext::pcxt, PlanState::plan, ParallelExecutorInfo::planstate, ParallelContext::seg, SerializeParamList(), shm_toc_allocate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_insert(), ParallelContext::toc, and ParallelExecutorInfo::tqueue.

Referenced by ExecGather(), and ExecGatherMerge().

385 {
387  ParallelContext *pcxt;
390  char *pstmt_data;
391  char *pstmt_space;
392  char *param_space;
393  BufferUsage *bufusage_space;
394  SharedExecutorInstrumentation *instrumentation = NULL;
395  int pstmt_len;
396  int param_len;
397  int instrumentation_len = 0;
398  int instrument_offset = 0;
399  Size dsa_minsize = dsa_minimum_size();
400  char *query_string;
401  int query_len;
402 
403  /* Allocate object for return value. */
404  pei = palloc0(sizeof(ParallelExecutorInfo));
405  pei->finished = false;
406  pei->planstate = planstate;
407 
408  /* Fix up and serialize plan to be sent to workers. */
409  pstmt_data = ExecSerializePlan(planstate->plan, estate);
410 
411  /* Create a parallel context. */
412  pcxt = CreateParallelContext("postgres", "ParallelQueryMain", nworkers);
413  pei->pcxt = pcxt;
414 
415  /*
416  * Before telling the parallel context to create a dynamic shared memory
417  * segment, we need to figure out how big it should be. Estimate space
418  * for the various things we need to store.
419  */
420 
421  /* Estimate space for query text. */
422  query_len = strlen(estate->es_sourceText);
423  shm_toc_estimate_chunk(&pcxt->estimator, query_len);
424  shm_toc_estimate_keys(&pcxt->estimator, 1);
425 
426  /* Estimate space for serialized PlannedStmt. */
427  pstmt_len = strlen(pstmt_data) + 1;
428  shm_toc_estimate_chunk(&pcxt->estimator, pstmt_len);
429  shm_toc_estimate_keys(&pcxt->estimator, 1);
430 
431  /* Estimate space for serialized ParamListInfo. */
432  param_len = EstimateParamListSpace(estate->es_param_list_info);
433  shm_toc_estimate_chunk(&pcxt->estimator, param_len);
434  shm_toc_estimate_keys(&pcxt->estimator, 1);
435 
436  /*
437  * Estimate space for BufferUsage.
438  *
439  * If EXPLAIN is not in use and there are no extensions loaded that care,
440  * we could skip this. But we have no way of knowing whether anyone's
441  * looking at pgBufferUsage, so do it unconditionally.
442  */
444  mul_size(sizeof(BufferUsage), pcxt->nworkers));
445  shm_toc_estimate_keys(&pcxt->estimator, 1);
446 
447  /* Estimate space for tuple queues. */
450  shm_toc_estimate_keys(&pcxt->estimator, 1);
451 
452  /*
453  * Give parallel-aware nodes a chance to add to the estimates, and get a
454  * count of how many PlanState nodes there are.
455  */
456  e.pcxt = pcxt;
457  e.nnodes = 0;
458  ExecParallelEstimate(planstate, &e);
459 
460  /* Estimate space for instrumentation, if required. */
461  if (estate->es_instrument)
462  {
463  instrumentation_len =
464  offsetof(SharedExecutorInstrumentation, plan_node_id) +
465  sizeof(int) * e.nnodes;
466  instrumentation_len = MAXALIGN(instrumentation_len);
467  instrument_offset = instrumentation_len;
468  instrumentation_len +=
469  mul_size(sizeof(Instrumentation),
470  mul_size(e.nnodes, nworkers));
471  shm_toc_estimate_chunk(&pcxt->estimator, instrumentation_len);
472  shm_toc_estimate_keys(&pcxt->estimator, 1);
473  }
474 
475  /* Estimate space for DSA area. */
476  shm_toc_estimate_chunk(&pcxt->estimator, dsa_minsize);
477  shm_toc_estimate_keys(&pcxt->estimator, 1);
478 
479  /* Everyone's had a chance to ask for space, so now create the DSM. */
480  InitializeParallelDSM(pcxt);
481 
482  /*
483  * OK, now we have a dynamic shared memory segment, and it should be big
484  * enough to store all of the data we estimated we would want to put into
485  * it, plus whatever general stuff (not specifically executor-related) the
486  * ParallelContext itself needs to store there. None of the space we
487  * asked for has been allocated or initialized yet, though, so do that.
488  */
489 
490  /* Store query string */
491  query_string = shm_toc_allocate(pcxt->toc, query_len);
492  memcpy(query_string, estate->es_sourceText, query_len);
493  shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string);
494 
495  /* Store serialized PlannedStmt. */
496  pstmt_space = shm_toc_allocate(pcxt->toc, pstmt_len);
497  memcpy(pstmt_space, pstmt_data, pstmt_len);
498  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PLANNEDSTMT, pstmt_space);
499 
500  /* Store serialized ParamListInfo. */
501  param_space = shm_toc_allocate(pcxt->toc, param_len);
502  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PARAMS, param_space);
503  SerializeParamList(estate->es_param_list_info, &param_space);
504 
505  /* Allocate space for each worker's BufferUsage; no need to initialize. */
506  bufusage_space = shm_toc_allocate(pcxt->toc,
507  mul_size(sizeof(BufferUsage), pcxt->nworkers));
508  shm_toc_insert(pcxt->toc, PARALLEL_KEY_BUFFER_USAGE, bufusage_space);
509  pei->buffer_usage = bufusage_space;
510 
511  /* Set up tuple queues. */
512  pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
513 
514  /*
515  * If instrumentation options were supplied, allocate space for the data.
516  * It only gets partially initialized here; the rest happens during
517  * ExecParallelInitializeDSM.
518  */
519  if (estate->es_instrument)
520  {
521  Instrumentation *instrument;
522  int i;
523 
524  instrumentation = shm_toc_allocate(pcxt->toc, instrumentation_len);
525  instrumentation->instrument_options = estate->es_instrument;
526  instrumentation->instrument_offset = instrument_offset;
527  instrumentation->num_workers = nworkers;
528  instrumentation->num_plan_nodes = e.nnodes;
529  instrument = GetInstrumentationArray(instrumentation);
530  for (i = 0; i < nworkers * e.nnodes; ++i)
531  InstrInit(&instrument[i], estate->es_instrument);
533  instrumentation);
534  pei->instrumentation = instrumentation;
535  }
536 
537  /*
538  * Create a DSA area that can be used by the leader and all workers.
539  * (However, if we failed to create a DSM and are using private memory
540  * instead, then skip this.)
541  */
542  if (pcxt->seg != NULL)
543  {
544  char *area_space;
545 
546  area_space = shm_toc_allocate(pcxt->toc, dsa_minsize);
547  shm_toc_insert(pcxt->toc, PARALLEL_KEY_DSA, area_space);
548  pei->area = dsa_create_in_place(area_space, dsa_minsize,
550  pcxt->seg);
551  }
552 
553  /*
554  * Make the area available to executor nodes running in the leader. See
555  * also ParallelQueryMain which makes it available to workers.
556  */
557  estate->es_query_dsa = pei->area;
558 
559  /*
560  * Give parallel-aware nodes a chance to initialize their shared data.
561  * This also initializes the elements of instrumentation->ps_instrument,
562  * if it exists.
563  */
564  d.pcxt = pcxt;
565  d.instrumentation = instrumentation;
566  d.nnodes = 0;
567  ExecParallelInitializeDSM(planstate, &d);
568 
569  /*
570  * Make sure that the world hasn't shifted under our feat. This could
571  * probably just be an Assert(), but let's be conservative for now.
572  */
573  if (e.nnodes != d.nnodes)
574  elog(ERROR, "inconsistent count of PlanState nodes");
575 
576  /* OK, we're ready to rock and roll. */
577  return pei;
578 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:508
ParallelContext * pcxt
Definition: execParallel.h:27
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
Definition: parallel.c:136
dsm_segment * seg
Definition: parallel.h:42
shm_toc_estimator estimator
Definition: parallel.h:41
PlanState * planstate
Definition: execParallel.h:26
static char * ExecSerializePlan(Plan *plan, EState *estate)
Definition: execParallel.c:122
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:320
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
Size EstimateParamListSpace(ParamListInfo paramLI)
Definition: params.c:95
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:100
ParallelContext * pcxt
Definition: execParallel.c:92
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:54
const char * es_sourceText
Definition: execnodes.h:433
static bool ExecParallelInitializeDSM(PlanState *node, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:255
#define ERROR
Definition: elog.h:43
BufferUsage * buffer_usage
Definition: execParallel.h:28
#define PARALLEL_KEY_PLANNEDSTMT
Definition: execParallel.c:50
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:58
int es_instrument
Definition: execnodes.h:478
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:205
dsa_area * dsa_create_in_place(void *place, size_t size, int tranche_id, dsm_segment *segment)
Definition: dsa.c:468
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:52
#define PARALLEL_KEY_PARAMS
Definition: execParallel.c:51
void InitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:188
void InstrInit(Instrumentation *instr, int instrument_options)
Definition: instrument.c:54
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
void * palloc0(Size size)
Definition: mcxt.c:878
Plan * plan
Definition: execnodes.h:832
Size dsa_minimum_size(void)
Definition: dsa.c:1160
#define NULL
Definition: c.h:229
void SerializeParamList(ParamListInfo paramLI, char **start_address)
Definition: params.c:158
size_t Size
Definition: c.h:356
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define MAXALIGN(LEN)
Definition: c.h:588
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:83
#define PARALLEL_KEY_DSA
Definition: execParallel.c:55
e
Definition: preproc-init.c:82
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:161
int i
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:56
ParamListInfo es_param_list_info
Definition: execnodes.h:462
shm_mq_handle ** tqueue
Definition: execParallel.h:30
#define elog
Definition: elog.h:219
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:29
#define offsetof(type, field)
Definition: c.h:555
#define GetInstrumentationArray(sei)
Definition: execParallel.c:85
shm_toc * toc
Definition: parallel.h:44
void ExecParallelCleanup ( ParallelExecutorInfo pei)

Definition at line 662 of file execParallel.c.

References ParallelExecutorInfo::area, DestroyParallelContext(), dsa_detach(), NULL, ParallelExecutorInfo::pcxt, and pfree().

Referenced by ExecShutdownGather(), and ExecShutdownGatherMerge().

663 {
664  if (pei->area != NULL)
665  {
666  dsa_detach(pei->area);
667  pei->area = NULL;
668  }
669  if (pei->pcxt != NULL)
670  {
672  pei->pcxt = NULL;
673  }
674  pfree(pei);
675 }
ParallelContext * pcxt
Definition: execParallel.h:27
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:594
void pfree(void *pointer)
Definition: mcxt.c:950
void dsa_detach(dsa_area *area)
Definition: dsa.c:1884
#define NULL
Definition: c.h:229
static bool ExecParallelEstimate ( PlanState node,
ExecParallelEstimateContext e 
)
static

Definition at line 205 of file execParallel.c.

References ExecBitmapHeapEstimate(), ExecCustomScanEstimate(), ExecForeignScanEstimate(), ExecIndexOnlyScanEstimate(), ExecIndexScanEstimate(), ExecSeqScanEstimate(), ExecParallelEstimateContext::nnodes, nodeTag, NULL, Plan::parallel_aware, ExecParallelEstimateContext::pcxt, PlanState::plan, planstate_tree_walker(), T_BitmapHeapScanState, T_CustomScanState, T_ForeignScanState, T_IndexOnlyScanState, T_IndexScanState, and T_SeqScanState.

Referenced by ExecInitParallelPlan().

206 {
207  if (planstate == NULL)
208  return false;
209 
210  /* Count this node. */
211  e->nnodes++;
212 
213  /* Call estimators for parallel-aware nodes. */
214  if (planstate->plan->parallel_aware)
215  {
216  switch (nodeTag(planstate))
217  {
218  case T_SeqScanState:
219  ExecSeqScanEstimate((SeqScanState *) planstate,
220  e->pcxt);
221  break;
222  case T_IndexScanState:
224  e->pcxt);
225  break;
228  e->pcxt);
229  break;
230  case T_ForeignScanState:
232  e->pcxt);
233  break;
234  case T_CustomScanState:
236  e->pcxt);
237  break;
240  e->pcxt);
241  break;
242  default:
243  break;
244  }
245  }
246 
247  return planstate_tree_walker(planstate, ExecParallelEstimate, e);
248 }
ParallelContext * pcxt
Definition: execParallel.c:92
void ExecIndexOnlyScanEstimate(IndexOnlyScanState *node, ParallelContext *pcxt)
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:205
#define NULL
Definition: c.h:229
void ExecForeignScanEstimate(ForeignScanState *node, ParallelContext *pcxt)
void ExecSeqScanEstimate(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:292
#define nodeTag(nodeptr)
Definition: nodes.h:514
void ExecBitmapHeapEstimate(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecCustomScanEstimate(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:159
void ExecIndexScanEstimate(IndexScanState *node, ParallelContext *pcxt)
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697
void ExecParallelFinish ( ParallelExecutorInfo pei)

Definition at line 633 of file execParallel.c.

References ParallelExecutorInfo::buffer_usage, ExecParallelRetrieveInstrumentation(), ParallelExecutorInfo::finished, i, InstrAccumParallelQuery(), ParallelExecutorInfo::instrumentation, ParallelContext::nworkers_launched, ParallelExecutorInfo::pcxt, ParallelExecutorInfo::planstate, and WaitForParallelWorkersToFinish().

Referenced by ExecShutdownGatherMergeWorkers(), and ExecShutdownGatherWorkers().

634 {
635  int i;
636 
637  if (pei->finished)
638  return;
639 
640  /* First, wait for the workers to finish. */
642 
643  /* Next, accumulate buffer usage. */
644  for (i = 0; i < pei->pcxt->nworkers_launched; ++i)
646 
647  /* Finally, accumulate instrumentation, if any. */
648  if (pei->instrumentation)
650  pei->instrumentation);
651 
652  pei->finished = true;
653 }
ParallelContext * pcxt
Definition: execParallel.h:27
PlanState * planstate
Definition: execParallel.h:26
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:504
BufferUsage * buffer_usage
Definition: execParallel.h:28
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:585
void InstrAccumParallelQuery(BufferUsage *result)
Definition: instrument.c:185
int nworkers_launched
Definition: parallel.h:37
int i
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:29
static QueryDesc* ExecParallelGetQueryDesc ( shm_toc toc,
DestReceiver receiver,
int  instrument_options 
)
static

Definition at line 698 of file execParallel.c.

References CreateQueryDesc(), GetActiveSnapshot(), InvalidSnapshot, NULL, PARALLEL_KEY_PARAMS, PARALLEL_KEY_PLANNEDSTMT, PARALLEL_KEY_QUERY_TEXT, RestoreParamList(), shm_toc_lookup(), and stringToNode().

Referenced by ParallelQueryMain().

700 {
701  char *pstmtspace;
702  char *paramspace;
703  PlannedStmt *pstmt;
704  ParamListInfo paramLI;
705  char *queryString;
706 
707  /* Get the query string from shared memory */
708  queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
709 
710  /* Reconstruct leader-supplied PlannedStmt. */
711  pstmtspace = shm_toc_lookup(toc, PARALLEL_KEY_PLANNEDSTMT, false);
712  pstmt = (PlannedStmt *) stringToNode(pstmtspace);
713 
714  /* Reconstruct ParamListInfo. */
715  paramspace = shm_toc_lookup(toc, PARALLEL_KEY_PARAMS, false);
716  paramLI = RestoreParamList(&paramspace);
717 
718  /*
719  * Create a QueryDesc for the query.
720  *
721  * It's not obvious how to obtain the query string from here; and even if
722  * we could copying it would take more cycles than not copying it. But
723  * it's a bit unsatisfying to just use a dummy string here, so consider
724  * revising this someday.
725  */
726  return CreateQueryDesc(pstmt,
727  queryString,
729  receiver, paramLI, NULL, instrument_options);
730 }
void * stringToNode(char *str)
Definition: read.c:38
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:839
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_PLANNEDSTMT
Definition: execParallel.c:50
ParamListInfo RestoreParamList(char **start_address)
Definition: params.c:224
#define PARALLEL_KEY_PARAMS
Definition: execParallel.c:51
#define InvalidSnapshot
Definition: snapshot.h:25
#define NULL
Definition: c.h:229
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:56
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:222
static DestReceiver * ExecParallelGetReceiver ( dsm_segment seg,
shm_toc toc 
)
static

Definition at line 682 of file execParallel.c.

References CreateTupleQueueDestReceiver(), MyProc, NULL, PARALLEL_KEY_TUPLE_QUEUE, PARALLEL_TUPLE_QUEUE_SIZE, ParallelWorkerNumber, shm_mq_attach(), shm_mq_set_sender(), and shm_toc_lookup().

Referenced by ParallelQueryMain().

683 {
684  char *mqspace;
685  shm_mq *mq;
686 
687  mqspace = shm_toc_lookup(toc, PARALLEL_KEY_TUPLE_QUEUE, false);
689  mq = (shm_mq *) mqspace;
692 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:53
PGPROC * MyProc
Definition: proc.c:67
int ParallelWorkerNumber
Definition: parallel.c:94
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:58
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:215
#define NULL
Definition: c.h:229
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:284
Definition: shm_mq.c:69
DestReceiver * CreateTupleQueueDestReceiver(shm_mq_handle *handle)
Definition: tqueue.c:606
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:222
static bool ExecParallelInitializeDSM ( PlanState node,
ExecParallelInitializeDSMContext d 
)
static

Definition at line 255 of file execParallel.c.

References ExecBitmapHeapInitializeDSM(), ExecCustomScanInitializeDSM(), ExecForeignScanInitializeDSM(), ExecIndexOnlyScanInitializeDSM(), ExecIndexScanInitializeDSM(), ExecSeqScanInitializeDSM(), ExecParallelInitializeDSMContext::instrumentation, ExecParallelInitializeDSMContext::nnodes, nodeTag, NULL, Plan::parallel_aware, ExecParallelInitializeDSMContext::pcxt, PlanState::plan, SharedExecutorInstrumentation::plan_node_id, Plan::plan_node_id, planstate_tree_walker(), T_BitmapHeapScanState, T_CustomScanState, T_ForeignScanState, T_IndexOnlyScanState, T_IndexScanState, and T_SeqScanState.

Referenced by ExecInitParallelPlan().

257 {
258  if (planstate == NULL)
259  return false;
260 
261  /* If instrumentation is enabled, initialize slot for this node. */
262  if (d->instrumentation != NULL)
264  planstate->plan->plan_node_id;
265 
266  /* Count this node. */
267  d->nnodes++;
268 
269  /*
270  * Call initializers for parallel-aware plan nodes.
271  *
272  * Ordinary plan nodes won't do anything here, but parallel-aware plan
273  * nodes may need to initialize shared state in the DSM before parallel
274  * workers are available. They can allocate the space they previously
275  * estimated using shm_toc_allocate, and add the keys they previously
276  * estimated using shm_toc_insert, in each case targeting pcxt->toc.
277  */
278  if (planstate->plan->parallel_aware)
279  {
280  switch (nodeTag(planstate))
281  {
282  case T_SeqScanState:
284  d->pcxt);
285  break;
286  case T_IndexScanState:
288  d->pcxt);
289  break;
292  d->pcxt);
293  break;
294  case T_ForeignScanState:
296  d->pcxt);
297  break;
298  case T_CustomScanState:
300  d->pcxt);
301  break;
304  d->pcxt);
305  break;
306 
307  default:
308  break;
309  }
310  }
311 
312  return planstate_tree_walker(planstate, ExecParallelInitializeDSM, d);
313 }
void ExecIndexScanInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:82
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:100
void ExecSeqScanInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:309
static bool ExecParallelInitializeDSM(PlanState *node, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:255
void ExecBitmapHeapInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecForeignScanInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:172
#define NULL
Definition: c.h:229
#define nodeTag(nodeptr)
Definition: nodes.h:514
void ExecIndexOnlyScanInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697
static bool ExecParallelInitializeWorker ( PlanState planstate,
shm_toc toc 
)
static

Definition at line 778 of file execParallel.c.

References ExecBitmapHeapInitializeWorker(), ExecCustomScanInitializeWorker(), ExecForeignScanInitializeWorker(), ExecIndexOnlyScanInitializeWorker(), ExecIndexScanInitializeWorker(), ExecSeqScanInitializeWorker(), nodeTag, NULL, Plan::parallel_aware, PlanState::plan, planstate_tree_walker(), T_BitmapHeapScanState, T_CustomScanState, T_ForeignScanState, T_IndexOnlyScanState, T_IndexScanState, and T_SeqScanState.

Referenced by ParallelQueryMain().

779 {
780  if (planstate == NULL)
781  return false;
782 
783  /* Call initializers for parallel-aware plan nodes. */
784  if (planstate->plan->parallel_aware)
785  {
786  switch (nodeTag(planstate))
787  {
788  case T_SeqScanState:
789  ExecSeqScanInitializeWorker((SeqScanState *) planstate, toc);
790  break;
791  case T_IndexScanState:
792  ExecIndexScanInitializeWorker((IndexScanState *) planstate, toc);
793  break;
796  break;
797  case T_ForeignScanState:
799  toc);
800  break;
801  case T_CustomScanState:
803  toc);
804  break;
807  (BitmapHeapScanState *) planstate, toc);
808  break;
809  default:
810  break;
811  }
812  }
813 
814  return planstate_tree_walker(planstate, ExecParallelInitializeWorker, toc);
815 }
void ExecIndexScanInitializeWorker(IndexScanState *node, shm_toc *toc)
void ExecSeqScanInitializeWorker(SeqScanState *node, shm_toc *toc)
Definition: nodeSeqscan.c:331
void ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, shm_toc *toc)
void ExecCustomScanInitializeWorker(CustomScanState *node, shm_toc *toc)
Definition: nodeCustom.c:188
void ExecForeignScanInitializeWorker(ForeignScanState *node, shm_toc *toc)
static bool ExecParallelInitializeWorker(PlanState *planstate, shm_toc *toc)
Definition: execParallel.c:778
bool parallel_aware
Definition: plannodes.h:137
Plan * plan
Definition: execnodes.h:832
#define NULL
Definition: c.h:229
void ExecBitmapHeapInitializeWorker(BitmapHeapScanState *node, shm_toc *toc)
#define nodeTag(nodeptr)
Definition: nodes.h:514
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697
void ExecParallelReinitialize ( ParallelExecutorInfo pei)

Definition at line 372 of file execParallel.c.

References ExecParallelSetupTupleQueues(), ParallelExecutorInfo::finished, ParallelExecutorInfo::pcxt, ReinitializeParallelDSM(), and ParallelExecutorInfo::tqueue.

Referenced by ExecReScanGather(), and ExecReScanGatherMerge().

373 {
375  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
376  pei->finished = false;
377 }
ParallelContext * pcxt
Definition: execParallel.h:27
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:320
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:380
shm_mq_handle ** tqueue
Definition: execParallel.h:30
static bool ExecParallelReportInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 737 of file execParallel.c.

References Assert, elog, ERROR, GetInstrumentationArray, i, InstrAggNode(), InstrEndLoop(), PlanState::instrument, IsParallelWorker, SharedExecutorInstrumentation::num_plan_nodes, SharedExecutorInstrumentation::num_workers, ParallelWorkerNumber, PlanState::plan, SharedExecutorInstrumentation::plan_node_id, Plan::plan_node_id, and planstate_tree_walker().

Referenced by ParallelQueryMain().

739 {
740  int i;
741  int plan_node_id = planstate->plan->plan_node_id;
742  Instrumentation *instrument;
743 
744  InstrEndLoop(planstate->instrument);
745 
746  /*
747  * If we shuffled the plan_node_id values in ps_instrument into sorted
748  * order, we could use binary search here. This might matter someday if
749  * we're pushing down sufficiently large plan trees. For now, do it the
750  * slow, dumb way.
751  */
752  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
753  if (instrumentation->plan_node_id[i] == plan_node_id)
754  break;
755  if (i >= instrumentation->num_plan_nodes)
756  elog(ERROR, "plan node %d not found", plan_node_id);
757 
758  /*
759  * Add our statistics to the per-node, per-worker totals. It's possible
760  * that this could happen more than once if we relaunched workers.
761  */
762  instrument = GetInstrumentationArray(instrumentation);
763  instrument += i * instrumentation->num_workers;
765  Assert(ParallelWorkerNumber < instrumentation->num_workers);
766  InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
767 
769  instrumentation);
770 }
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:82
Instrumentation * instrument
Definition: execnodes.h:838
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:143
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:737
int plan_node_id
Definition: plannodes.h:143
void InstrEndLoop(Instrumentation *instr)
Definition: instrument.c:114
#define ERROR
Definition: elog.h:43
int ParallelWorkerNumber
Definition: parallel.c:94
#define IsParallelWorker()
Definition: parallel.h:52
Plan * plan
Definition: execnodes.h:832
#define Assert(condition)
Definition: c.h:675
int i
#define elog
Definition: elog.h:219
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697
#define GetInstrumentationArray(sei)
Definition: execParallel.c:85
static bool ExecParallelRetrieveInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 585 of file execParallel.c.

References elog, ERROR, EState::es_query_cxt, GetInstrumentationArray, i, InstrAggNode(), WorkerInstrumentation::instrument, PlanState::instrument, MemoryContextSwitchTo(), mul_size(), SharedExecutorInstrumentation::num_plan_nodes, WorkerInstrumentation::num_workers, SharedExecutorInstrumentation::num_workers, offsetof, palloc(), PlanState::plan, SharedExecutorInstrumentation::plan_node_id, Plan::plan_node_id, planstate_tree_walker(), PlanState::state, and PlanState::worker_instrument.

Referenced by ExecParallelFinish().

587 {
588  Instrumentation *instrument;
589  int i;
590  int n;
591  int ibytes;
592  int plan_node_id = planstate->plan->plan_node_id;
593  MemoryContext oldcontext;
594 
595  /* Find the instrumentation for this node. */
596  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
597  if (instrumentation->plan_node_id[i] == plan_node_id)
598  break;
599  if (i >= instrumentation->num_plan_nodes)
600  elog(ERROR, "plan node %d not found", plan_node_id);
601 
602  /* Accumulate the statistics from all workers. */
603  instrument = GetInstrumentationArray(instrumentation);
604  instrument += i * instrumentation->num_workers;
605  for (n = 0; n < instrumentation->num_workers; ++n)
606  InstrAggNode(planstate->instrument, &instrument[n]);
607 
608  /*
609  * Also store the per-worker detail.
610  *
611  * Worker instrumentation should be allocated in the same context as the
612  * regular instrumentation information, which is the per-query context.
613  * Switch into per-query memory context.
614  */
615  oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
616  ibytes = mul_size(instrumentation->num_workers, sizeof(Instrumentation));
617  planstate->worker_instrument =
618  palloc(ibytes + offsetof(WorkerInstrumentation, instrument));
619  MemoryContextSwitchTo(oldcontext);
620 
621  planstate->worker_instrument->num_workers = instrumentation->num_workers;
622  memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
623 
625  instrumentation);
626 }
WorkerInstrumentation * worker_instrument
Definition: execnodes.h:839
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:82
Instrumentation * instrument
Definition: execnodes.h:838
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:143
int plan_node_id
Definition: plannodes.h:143
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
EState * state
Definition: execnodes.h:834
MemoryContext es_query_cxt
Definition: execnodes.h:468
#define ERROR
Definition: elog.h:43
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:585
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Instrumentation instrument[FLEXIBLE_ARRAY_MEMBER]
Definition: instrument.h:69
Plan * plan
Definition: execnodes.h:832
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define elog
Definition: elog.h:219
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697
#define offsetof(type, field)
Definition: c.h:555
#define GetInstrumentationArray(sei)
Definition: execParallel.c:85
static shm_mq_handle ** ExecParallelSetupTupleQueues ( ParallelContext pcxt,
bool  reinitialize 
)
static

Definition at line 320 of file execParallel.c.

References i, mul_size(), MyProc, NULL, ParallelContext::nworkers, palloc(), PARALLEL_KEY_TUPLE_QUEUE, PARALLEL_TUPLE_QUEUE_SIZE, ParallelContext::seg, shm_mq_attach(), shm_mq_create(), shm_mq_set_receiver(), shm_toc_allocate(), shm_toc_insert(), shm_toc_lookup(), and ParallelContext::toc.

Referenced by ExecInitParallelPlan(), and ExecParallelReinitialize().

321 {
322  shm_mq_handle **responseq;
323  char *tqueuespace;
324  int i;
325 
326  /* Skip this if no workers. */
327  if (pcxt->nworkers == 0)
328  return NULL;
329 
330  /* Allocate memory for shared memory queue handles. */
331  responseq = (shm_mq_handle **)
332  palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
333 
334  /*
335  * If not reinitializing, allocate space from the DSM for the queues;
336  * otherwise, find the already allocated space.
337  */
338  if (!reinitialize)
339  tqueuespace =
340  shm_toc_allocate(pcxt->toc,
342  pcxt->nworkers));
343  else
344  tqueuespace = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, false);
345 
346  /* Create the queues, and become the receiver for each. */
347  for (i = 0; i < pcxt->nworkers; ++i)
348  {
349  shm_mq *mq;
350 
351  mq = shm_mq_create(tqueuespace +
353  (Size) PARALLEL_TUPLE_QUEUE_SIZE);
354 
356  responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
357  }
358 
359  /* Add array of queues to shm_toc, so others can find it. */
360  if (!reinitialize)
361  shm_toc_insert(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, tqueuespace);
362 
363  /* Return array of handles. */
364  return responseq;
365 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:53
PGPROC * MyProc
Definition: proc.c:67
dsm_segment * seg
Definition: parallel.h:42
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:167
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:58
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
#define NULL
Definition: c.h:229
size_t Size
Definition: c.h:356
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:83
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:284
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:161
void * palloc(Size size)
Definition: mcxt.c:849
int i
Definition: shm_mq.c:69
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:196
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:222
shm_toc * toc
Definition: parallel.h:44
static char * ExecSerializePlan ( Plan plan,
EState estate 
)
static

Definition at line 122 of file execParallel.c.

References PlannedStmt::canSetTag, CMD_SELECT, PlannedStmt::commandType, copyObject, PlannedStmt::dependsOnRole, EState::es_plannedstmt, EState::es_range_table, PlannedStmt::hasModifyingCTE, PlannedStmt::hasReturning, PlannedStmt::invalItems, lappend(), lfirst, lfirst_node, makeNode, NIL, nodeToString(), PlannedStmt::nonleafResultRelations, PlannedStmt::nParamExec, NULL, Plan::parallel_safe, PlannedStmt::parallelModeNeeded, PlannedStmt::planTree, PlannedStmt::queryId, PlannedStmt::relationOids, TargetEntry::resjunk, PlannedStmt::resultRelations, PlannedStmt::rewindPlanIDs, PlannedStmt::rowMarks, PlannedStmt::rtable, PlannedStmt::stmt_len, PlannedStmt::stmt_location, PlannedStmt::subplans, Plan::targetlist, PlannedStmt::transientPlan, and PlannedStmt::utilityStmt.

Referenced by ExecInitParallelPlan().

123 {
124  PlannedStmt *pstmt;
125  ListCell *lc;
126 
127  /* We can't scribble on the original plan, so make a copy. */
128  plan = copyObject(plan);
129 
130  /*
131  * The worker will start its own copy of the executor, and that copy will
132  * insert a junk filter if the toplevel node has any resjunk entries. We
133  * don't want that to happen, because while resjunk columns shouldn't be
134  * sent back to the user, here the tuples are coming back to another
135  * backend which may very well need them. So mutate the target list
136  * accordingly. This is sort of a hack; there might be better ways to do
137  * this...
138  */
139  foreach(lc, plan->targetlist)
140  {
141  TargetEntry *tle = lfirst_node(TargetEntry, lc);
142 
143  tle->resjunk = false;
144  }
145 
146  /*
147  * Create a dummy PlannedStmt. Most of the fields don't need to be valid
148  * for our purposes, but the worker will need at least a minimal
149  * PlannedStmt to start the executor.
150  */
151  pstmt = makeNode(PlannedStmt);
152  pstmt->commandType = CMD_SELECT;
153  pstmt->queryId = 0;
154  pstmt->hasReturning = false;
155  pstmt->hasModifyingCTE = false;
156  pstmt->canSetTag = true;
157  pstmt->transientPlan = false;
158  pstmt->dependsOnRole = false;
159  pstmt->parallelModeNeeded = false;
160  pstmt->planTree = plan;
161  pstmt->rtable = estate->es_range_table;
162  pstmt->resultRelations = NIL;
163  pstmt->nonleafResultRelations = NIL;
164 
165  /*
166  * Transfer only parallel-safe subplans, leaving a NULL "hole" in the list
167  * for unsafe ones (so that the list indexes of the safe ones are
168  * preserved). This positively ensures that the worker won't try to run,
169  * or even do ExecInitNode on, an unsafe subplan. That's important to
170  * protect, eg, non-parallel-aware FDWs from getting into trouble.
171  */
172  pstmt->subplans = NIL;
173  foreach(lc, estate->es_plannedstmt->subplans)
174  {
175  Plan *subplan = (Plan *) lfirst(lc);
176 
177  if (subplan && !subplan->parallel_safe)
178  subplan = NULL;
179  pstmt->subplans = lappend(pstmt->subplans, subplan);
180  }
181 
182  pstmt->rewindPlanIDs = NULL;
183  pstmt->rowMarks = NIL;
184  pstmt->relationOids = NIL;
185  pstmt->invalItems = NIL; /* workers can't replan anyway... */
186  pstmt->nParamExec = estate->es_plannedstmt->nParamExec;
187  pstmt->utilityStmt = NULL;
188  pstmt->stmt_location = -1;
189  pstmt->stmt_len = -1;
190 
191  /* Return serialized copy of our dummy PlannedStmt. */
192  return nodeToString(pstmt);
193 }
bool dependsOnRole
Definition: plannodes.h:57
#define NIL
Definition: pg_list.h:69
uint32 queryId
Definition: plannodes.h:47
List * nonleafResultRelations
Definition: plannodes.h:72
List * relationOids
Definition: plannodes.h:88
PlannedStmt * es_plannedstmt
Definition: execnodes.h:432
List * es_range_table
Definition: execnodes.h:431
bool transientPlan
Definition: plannodes.h:55
int stmt_len
Definition: plannodes.h:98
struct Plan * planTree
Definition: plannodes.h:61
List * invalItems
Definition: plannodes.h:90
int nParamExec
Definition: plannodes.h:92
bool resjunk
Definition: primnodes.h:1375
#define lfirst_node(type, lc)
Definition: pg_list.h:109
int stmt_location
Definition: plannodes.h:97
bool hasReturning
Definition: plannodes.h:49
Node * utilityStmt
Definition: plannodes.h:94
List * lappend(List *list, void *datum)
Definition: list.c:128
bool canSetTag
Definition: plannodes.h:53
CmdType commandType
Definition: plannodes.h:45
List * rowMarks
Definition: plannodes.h:86
#define makeNode(_type_)
Definition: nodes.h:557
List * subplans
Definition: plannodes.h:81
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * rewindPlanIDs
Definition: plannodes.h:84
bool hasModifyingCTE
Definition: plannodes.h:51
List * rtable
Definition: plannodes.h:63
List * targetlist
Definition: plannodes.h:144
List * resultRelations
Definition: plannodes.h:66
bool parallelModeNeeded
Definition: plannodes.h:59
char * nodeToString(const void *obj)
Definition: outfuncs.c:4252
bool parallel_safe
Definition: plannodes.h:138
#define copyObject(obj)
Definition: nodes.h:622
void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)

Definition at line 834 of file execParallel.c.

References debug_query_string, dsa_attach_in_place(), dsa_detach(), EState::es_query_dsa, ExecParallelGetQueryDesc(), ExecParallelGetReceiver(), ExecParallelInitializeWorker(), ExecParallelReportInstrumentation(), ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), ForwardScanDirection, FreeQueryDesc(), InstrEndParallelQuery(), InstrStartParallelQuery(), SharedExecutorInstrumentation::instrument_options, NULL, PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_DSA, PARALLEL_KEY_INSTRUMENTATION, ParallelWorkerNumber, pgstat_report_activity(), QueryDesc::planstate, _DestReceiver::rDestroy, shm_toc_lookup(), QueryDesc::sourceText, PlanState::state, and STATE_RUNNING.

835 {
836  BufferUsage *buffer_usage;
837  DestReceiver *receiver;
838  QueryDesc *queryDesc;
839  SharedExecutorInstrumentation *instrumentation;
840  int instrument_options = 0;
841  void *area_space;
842  dsa_area *area;
843 
844  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
845  receiver = ExecParallelGetReceiver(seg, toc);
846  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
847  if (instrumentation != NULL)
848  instrument_options = instrumentation->instrument_options;
849  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
850 
851  /* Setting debug_query_string for individual workers */
852  debug_query_string = queryDesc->sourceText;
853 
854  /* Report workers' query for monitoring purposes */
856 
857  /* Prepare to track buffer usage during query execution. */
859 
860  /* Attach to the dynamic shared memory area. */
861  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
862  area = dsa_attach_in_place(area_space, seg);
863 
864  /* Start up the executor */
865  ExecutorStart(queryDesc, 0);
866 
867  /* Special executor initialization steps for parallel workers */
868  queryDesc->planstate->state->es_query_dsa = area;
869  ExecParallelInitializeWorker(queryDesc->planstate, toc);
870 
871  /* Run the plan */
872  ExecutorRun(queryDesc, ForwardScanDirection, 0L, true);
873 
874  /* Shut down the executor */
875  ExecutorFinish(queryDesc);
876 
877  /* Report buffer usage during parallel execution. */
878  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
880 
881  /* Report instrumentation data if any instrumentation options are set. */
882  if (instrumentation != NULL)
884  instrumentation);
885 
886  /* Must do this after capturing instrumentation. */
887  ExecutorEnd(queryDesc);
888 
889  /* Cleanup. */
890  dsa_detach(area);
891  FreeQueryDesc(queryDesc);
892  (*receiver->rDestroy) (receiver);
893 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:508
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:126
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
void pgstat_report_activity(BackendState state, const char *cmd_str)
Definition: pgstat.c:2994
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:737
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:146
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:540
void InstrEndParallelQuery(BufferUsage *result)
Definition: instrument.c:177
EState * state
Definition: execnodes.h:834
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:460
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:54
PlanState * planstate
Definition: execdesc.h:49
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:298
void dsa_detach(dsa_area *area)
Definition: dsa.c:1884
int ParallelWorkerNumber
Definition: parallel.c:94
static bool ExecParallelInitializeWorker(PlanState *planstate, shm_toc *toc)
Definition: execParallel.c:778
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:52
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:400
const char * debug_query_string
Definition: postgres.c:85
void InstrStartParallelQuery(void)
Definition: instrument.c:170
#define NULL
Definition: c.h:229
const char * sourceText
Definition: execdesc.h:38
#define PARALLEL_KEY_DSA
Definition: execParallel.c:55
Definition: dsa.c:354
static DestReceiver * ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
Definition: execParallel.c:682
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:222
static QueryDesc * ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver, int instrument_options)
Definition: execParallel.c:698