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 void ParallelQueryMain (dsm_segment *seg, shm_toc *toc)
 
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)
 

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 368 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, ParallelQueryMain(), 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().

369 {
371  ParallelContext *pcxt;
374  char *pstmt_data;
375  char *pstmt_space;
376  char *param_space;
377  BufferUsage *bufusage_space;
378  SharedExecutorInstrumentation *instrumentation = NULL;
379  int pstmt_len;
380  int param_len;
381  int instrumentation_len = 0;
382  int instrument_offset = 0;
383  Size dsa_minsize = dsa_minimum_size();
384  char *query_string;
385  int query_len;
386 
387  /* Allocate object for return value. */
388  pei = palloc0(sizeof(ParallelExecutorInfo));
389  pei->finished = false;
390  pei->planstate = planstate;
391 
392  /* Fix up and serialize plan to be sent to workers. */
393  pstmt_data = ExecSerializePlan(planstate->plan, estate);
394 
395  /* Create a parallel context. */
396  pcxt = CreateParallelContext(ParallelQueryMain, nworkers);
397  pei->pcxt = pcxt;
398 
399  /*
400  * Before telling the parallel context to create a dynamic shared memory
401  * segment, we need to figure out how big it should be. Estimate space
402  * for the various things we need to store.
403  */
404 
405  /* Estimate space for query text. */
406  query_len = strlen(estate->es_sourceText);
407  shm_toc_estimate_chunk(&pcxt->estimator, query_len);
408  shm_toc_estimate_keys(&pcxt->estimator, 1);
409 
410  /* Estimate space for serialized PlannedStmt. */
411  pstmt_len = strlen(pstmt_data) + 1;
412  shm_toc_estimate_chunk(&pcxt->estimator, pstmt_len);
413  shm_toc_estimate_keys(&pcxt->estimator, 1);
414 
415  /* Estimate space for serialized ParamListInfo. */
416  param_len = EstimateParamListSpace(estate->es_param_list_info);
417  shm_toc_estimate_chunk(&pcxt->estimator, param_len);
418  shm_toc_estimate_keys(&pcxt->estimator, 1);
419 
420  /*
421  * Estimate space for BufferUsage.
422  *
423  * If EXPLAIN is not in use and there are no extensions loaded that care,
424  * we could skip this. But we have no way of knowing whether anyone's
425  * looking at pgBufferUsage, so do it unconditionally.
426  */
428  mul_size(sizeof(BufferUsage), pcxt->nworkers));
429  shm_toc_estimate_keys(&pcxt->estimator, 1);
430 
431  /* Estimate space for tuple queues. */
434  shm_toc_estimate_keys(&pcxt->estimator, 1);
435 
436  /*
437  * Give parallel-aware nodes a chance to add to the estimates, and get a
438  * count of how many PlanState nodes there are.
439  */
440  e.pcxt = pcxt;
441  e.nnodes = 0;
442  ExecParallelEstimate(planstate, &e);
443 
444  /* Estimate space for instrumentation, if required. */
445  if (estate->es_instrument)
446  {
447  instrumentation_len =
448  offsetof(SharedExecutorInstrumentation, plan_node_id) +
449  sizeof(int) * e.nnodes;
450  instrumentation_len = MAXALIGN(instrumentation_len);
451  instrument_offset = instrumentation_len;
452  instrumentation_len +=
453  mul_size(sizeof(Instrumentation),
454  mul_size(e.nnodes, nworkers));
455  shm_toc_estimate_chunk(&pcxt->estimator, instrumentation_len);
456  shm_toc_estimate_keys(&pcxt->estimator, 1);
457  }
458 
459  /* Estimate space for DSA area. */
460  shm_toc_estimate_chunk(&pcxt->estimator, dsa_minsize);
461  shm_toc_estimate_keys(&pcxt->estimator, 1);
462 
463  /* Everyone's had a chance to ask for space, so now create the DSM. */
464  InitializeParallelDSM(pcxt);
465 
466  /*
467  * OK, now we have a dynamic shared memory segment, and it should be big
468  * enough to store all of the data we estimated we would want to put into
469  * it, plus whatever general stuff (not specifically executor-related) the
470  * ParallelContext itself needs to store there. None of the space we
471  * asked for has been allocated or initialized yet, though, so do that.
472  */
473 
474  /* Store query string */
475  query_string = shm_toc_allocate(pcxt->toc, query_len);
476  memcpy(query_string, estate->es_sourceText, query_len);
477  shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string);
478 
479  /* Store serialized PlannedStmt. */
480  pstmt_space = shm_toc_allocate(pcxt->toc, pstmt_len);
481  memcpy(pstmt_space, pstmt_data, pstmt_len);
482  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PLANNEDSTMT, pstmt_space);
483 
484  /* Store serialized ParamListInfo. */
485  param_space = shm_toc_allocate(pcxt->toc, param_len);
486  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PARAMS, param_space);
487  SerializeParamList(estate->es_param_list_info, &param_space);
488 
489  /* Allocate space for each worker's BufferUsage; no need to initialize. */
490  bufusage_space = shm_toc_allocate(pcxt->toc,
491  mul_size(sizeof(BufferUsage), pcxt->nworkers));
492  shm_toc_insert(pcxt->toc, PARALLEL_KEY_BUFFER_USAGE, bufusage_space);
493  pei->buffer_usage = bufusage_space;
494 
495  /* Set up tuple queues. */
496  pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
497 
498  /*
499  * If instrumentation options were supplied, allocate space for the data.
500  * It only gets partially initialized here; the rest happens during
501  * ExecParallelInitializeDSM.
502  */
503  if (estate->es_instrument)
504  {
505  Instrumentation *instrument;
506  int i;
507 
508  instrumentation = shm_toc_allocate(pcxt->toc, instrumentation_len);
509  instrumentation->instrument_options = estate->es_instrument;
510  instrumentation->instrument_offset = instrument_offset;
511  instrumentation->num_workers = nworkers;
512  instrumentation->num_plan_nodes = e.nnodes;
513  instrument = GetInstrumentationArray(instrumentation);
514  for (i = 0; i < nworkers * e.nnodes; ++i)
515  InstrInit(&instrument[i], estate->es_instrument);
517  instrumentation);
518  pei->instrumentation = instrumentation;
519  }
520 
521  /*
522  * Create a DSA area that can be used by the leader and all workers.
523  * (However, if we failed to create a DSM and are using private memory
524  * instead, then skip this.)
525  */
526  if (pcxt->seg != NULL)
527  {
528  char *area_space;
529 
530  area_space = shm_toc_allocate(pcxt->toc, dsa_minsize);
531  shm_toc_insert(pcxt->toc, PARALLEL_KEY_DSA, area_space);
532  pei->area = dsa_create_in_place(area_space, dsa_minsize,
534  pcxt->seg);
535  }
536 
537  /*
538  * Make the area available to executor nodes running in the leader. See
539  * also ParallelQueryMain which makes it available to workers.
540  */
541  estate->es_query_dsa = pei->area;
542 
543  /*
544  * Give parallel-aware nodes a chance to initialize their shared data.
545  * This also initializes the elements of instrumentation->ps_instrument,
546  * if it exists.
547  */
548  d.pcxt = pcxt;
549  d.instrumentation = instrumentation;
550  d.nnodes = 0;
551  ExecParallelInitializeDSM(planstate, &d);
552 
553  /*
554  * Make sure that the world hasn't shifted under our feat. This could
555  * probably just be an Assert(), but let's be conservative for now.
556  */
557  if (e.nnodes != d.nnodes)
558  elog(ERROR, "inconsistent count of PlanState nodes");
559 
560  /* OK, we're ready to rock and roll. */
561  return pei;
562 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:475
ParallelContext * pcxt
Definition: execParallel.h:27
static void ParallelQueryMain(dsm_segment *seg, shm_toc *toc)
Definition: execParallel.c:818
dsm_segment * seg
Definition: parallel.h:43
shm_toc_estimator estimator
Definition: parallel.h:42
PlanState * planstate
Definition: execParallel.h:26
static char * ExecSerializePlan(Plan *plan, EState *estate)
Definition: execParallel.c:123
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:304
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:49
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:412
static bool ExecParallelInitializeDSM(PlanState *node, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:239
#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:445
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:189
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:200
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:800
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:52
#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:431
shm_mq_handle ** tqueue
Definition: execParallel.h:30
ParallelContext * CreateParallelContext(parallel_worker_main_type entrypoint, int nworkers)
Definition: parallel.c:123
#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:45
void ExecParallelCleanup ( ParallelExecutorInfo pei)

Definition at line 646 of file execParallel.c.

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

Referenced by ExecShutdownGather(), and ExecShutdownGatherMerge().

647 {
648  if (pei->area != NULL)
649  {
650  dsa_detach(pei->area);
651  pei->area = NULL;
652  }
653  if (pei->pcxt != NULL)
654  {
656  pei->pcxt = NULL;
657  }
658  pfree(pei);
659 }
ParallelContext * pcxt
Definition: execParallel.h:27
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:609
void pfree(void *pointer)
Definition: mcxt.c:950
void dsa_detach(dsa_area *area)
Definition: dsa.c:1882
#define NULL
Definition: c.h:229
static bool ExecParallelEstimate ( PlanState node,
ExecParallelEstimateContext e 
)
static

Definition at line 189 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().

190 {
191  if (planstate == NULL)
192  return false;
193 
194  /* Count this node. */
195  e->nnodes++;
196 
197  /* Call estimators for parallel-aware nodes. */
198  if (planstate->plan->parallel_aware)
199  {
200  switch (nodeTag(planstate))
201  {
202  case T_SeqScanState:
203  ExecSeqScanEstimate((SeqScanState *) planstate,
204  e->pcxt);
205  break;
206  case T_IndexScanState:
208  e->pcxt);
209  break;
212  e->pcxt);
213  break;
214  case T_ForeignScanState:
216  e->pcxt);
217  break;
218  case T_CustomScanState:
220  e->pcxt);
221  break;
224  e->pcxt);
225  break;
226  default:
227  break;
228  }
229  }
230 
231  return planstate_tree_walker(planstate, ExecParallelEstimate, e);
232 }
ParallelContext * pcxt
Definition: execParallel.c:92
void ExecIndexOnlyScanEstimate(IndexOnlyScanState *node, ParallelContext *pcxt)
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:189
#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:511
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:3705
void ExecParallelFinish ( ParallelExecutorInfo pei)

Definition at line 617 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().

618 {
619  int i;
620 
621  if (pei->finished)
622  return;
623 
624  /* First, wait for the workers to finish. */
626 
627  /* Next, accumulate buffer usage. */
628  for (i = 0; i < pei->pcxt->nworkers_launched; ++i)
630 
631  /* Finally, accumulate instrumentation, if any. */
632  if (pei->instrumentation)
634  pei->instrumentation);
635 
636  pei->finished = true;
637 }
ParallelContext * pcxt
Definition: execParallel.h:27
PlanState * planstate
Definition: execParallel.h:26
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:519
BufferUsage * buffer_usage
Definition: execParallel.h:28
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:569
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 682 of file execParallel.c.

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

Referenced by ParallelQueryMain().

684 {
685  char *pstmtspace;
686  char *paramspace;
687  PlannedStmt *pstmt;
688  ParamListInfo paramLI;
689  char *queryString;
690 
691  /* Get the query string from shared memory */
692  queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT);
693 
694  /* Reconstruct leader-supplied PlannedStmt. */
695  pstmtspace = shm_toc_lookup(toc, PARALLEL_KEY_PLANNEDSTMT);
696  pstmt = (PlannedStmt *) stringToNode(pstmtspace);
697 
698  /* Reconstruct ParamListInfo. */
699  paramspace = shm_toc_lookup(toc, PARALLEL_KEY_PARAMS);
700  paramLI = RestoreParamList(&paramspace);
701 
702  /*
703  * Create a QueryDesc for the query.
704  *
705  * It's not obvious how to obtain the query string from here; and even if
706  * we could copying it would take more cycles than not copying it. But
707  * it's a bit unsatisfying to just use a dummy string here, so consider
708  * revising this someday.
709  */
710  return CreateQueryDesc(pstmt,
711  queryString,
713  receiver, paramLI, instrument_options);
714 }
void * stringToNode(char *str)
Definition: read.c:38
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:834
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, int instrument_options)
Definition: pquery.c:66
#define PARALLEL_KEY_PLANNEDSTMT
Definition: execParallel.c:50
ParamListInfo RestoreParamList(char **start_address)
Definition: params.c:224
void * shm_toc_lookup(shm_toc *toc, uint64 key)
Definition: shm_toc.c:218
#define PARALLEL_KEY_PARAMS
Definition: execParallel.c:51
#define InvalidSnapshot
Definition: snapshot.h:25
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:56
static DestReceiver * ExecParallelGetReceiver ( dsm_segment seg,
shm_toc toc 
)
static

Definition at line 666 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().

667 {
668  char *mqspace;
669  shm_mq *mq;
670 
673  mq = (shm_mq *) mqspace;
676 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:53
PGPROC * MyProc
Definition: proc.c:67
void * shm_toc_lookup(shm_toc *toc, uint64 key)
Definition: shm_toc.c:218
int ParallelWorkerNumber
Definition: parallel.c:96
#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
static bool ExecParallelInitializeDSM ( PlanState node,
ExecParallelInitializeDSMContext d 
)
static

Definition at line 239 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().

241 {
242  if (planstate == NULL)
243  return false;
244 
245  /* If instrumentation is enabled, initialize slot for this node. */
246  if (d->instrumentation != NULL)
248  planstate->plan->plan_node_id;
249 
250  /* Count this node. */
251  d->nnodes++;
252 
253  /*
254  * Call initializers for parallel-aware plan nodes.
255  *
256  * Ordinary plan nodes won't do anything here, but parallel-aware plan
257  * nodes may need to initialize shared state in the DSM before parallel
258  * workers are available. They can allocate the space they previously
259  * estimated using shm_toc_allocate, and add the keys they previously
260  * estimated using shm_toc_insert, in each case targeting pcxt->toc.
261  */
262  if (planstate->plan->parallel_aware)
263  {
264  switch (nodeTag(planstate))
265  {
266  case T_SeqScanState:
268  d->pcxt);
269  break;
270  case T_IndexScanState:
272  d->pcxt);
273  break;
276  d->pcxt);
277  break;
278  case T_ForeignScanState:
280  d->pcxt);
281  break;
282  case T_CustomScanState:
284  d->pcxt);
285  break;
288  d->pcxt);
289  break;
290 
291  default:
292  break;
293  }
294  }
295 
296  return planstate_tree_walker(planstate, ExecParallelInitializeDSM, d);
297 }
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:239
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:511
void ExecIndexOnlyScanInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3705
static bool ExecParallelInitializeWorker ( PlanState planstate,
shm_toc toc 
)
static

Definition at line 762 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().

763 {
764  if (planstate == NULL)
765  return false;
766 
767  /* Call initializers for parallel-aware plan nodes. */
768  if (planstate->plan->parallel_aware)
769  {
770  switch (nodeTag(planstate))
771  {
772  case T_SeqScanState:
773  ExecSeqScanInitializeWorker((SeqScanState *) planstate, toc);
774  break;
775  case T_IndexScanState:
776  ExecIndexScanInitializeWorker((IndexScanState *) planstate, toc);
777  break;
780  break;
781  case T_ForeignScanState:
783  toc);
784  break;
785  case T_CustomScanState:
787  toc);
788  break;
791  (BitmapHeapScanState *) planstate, toc);
792  break;
793  default:
794  break;
795  }
796  }
797 
798  return planstate_tree_walker(planstate, ExecParallelInitializeWorker, toc);
799 }
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:762
bool parallel_aware
Definition: plannodes.h:126
Plan * plan
Definition: execnodes.h:800
#define NULL
Definition: c.h:229
void ExecBitmapHeapInitializeWorker(BitmapHeapScanState *node, shm_toc *toc)
#define nodeTag(nodeptr)
Definition: nodes.h:511
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3705
void ExecParallelReinitialize ( ParallelExecutorInfo pei)

Definition at line 356 of file execParallel.c.

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

Referenced by ExecReScanGather(), and ExecReScanGatherMerge().

357 {
359  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
360  pei->finished = false;
361 }
ParallelContext * pcxt
Definition: execParallel.h:27
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:304
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:396
shm_mq_handle ** tqueue
Definition: execParallel.h:30
static bool ExecParallelReportInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 721 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().

723 {
724  int i;
725  int plan_node_id = planstate->plan->plan_node_id;
726  Instrumentation *instrument;
727 
728  InstrEndLoop(planstate->instrument);
729 
730  /*
731  * If we shuffled the plan_node_id values in ps_instrument into sorted
732  * order, we could use binary search here. This might matter someday if
733  * we're pushing down sufficiently large plan trees. For now, do it the
734  * slow, dumb way.
735  */
736  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
737  if (instrumentation->plan_node_id[i] == plan_node_id)
738  break;
739  if (i >= instrumentation->num_plan_nodes)
740  elog(ERROR, "plan node %d not found", plan_node_id);
741 
742  /*
743  * Add our statistics to the per-node, per-worker totals. It's possible
744  * that this could happen more than once if we relaunched workers.
745  */
746  instrument = GetInstrumentationArray(instrumentation);
747  instrument += i * instrumentation->num_workers;
749  Assert(ParallelWorkerNumber < instrumentation->num_workers);
750  InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
751 
753  instrumentation);
754 }
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:82
Instrumentation * instrument
Definition: execnodes.h:806
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:143
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:721
int plan_node_id
Definition: plannodes.h:131
void InstrEndLoop(Instrumentation *instr)
Definition: instrument.c:114
#define ERROR
Definition: elog.h:43
int ParallelWorkerNumber
Definition: parallel.c:96
#define IsParallelWorker()
Definition: parallel.h:53
Plan * plan
Definition: execnodes.h:800
#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:3705
#define GetInstrumentationArray(sei)
Definition: execParallel.c:85
static bool ExecParallelRetrieveInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 569 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().

571 {
572  Instrumentation *instrument;
573  int i;
574  int n;
575  int ibytes;
576  int plan_node_id = planstate->plan->plan_node_id;
577  MemoryContext oldcontext;
578 
579  /* Find the instrumentation for this node. */
580  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
581  if (instrumentation->plan_node_id[i] == plan_node_id)
582  break;
583  if (i >= instrumentation->num_plan_nodes)
584  elog(ERROR, "plan node %d not found", plan_node_id);
585 
586  /* Accumulate the statistics from all workers. */
587  instrument = GetInstrumentationArray(instrumentation);
588  instrument += i * instrumentation->num_workers;
589  for (n = 0; n < instrumentation->num_workers; ++n)
590  InstrAggNode(planstate->instrument, &instrument[n]);
591 
592  /*
593  * Also store the per-worker detail.
594  *
595  * Worker instrumentation should be allocated in the same context as
596  * the regular instrumentation information, which is the per-query
597  * context. Switch into per-query memory context.
598  */
599  oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
600  ibytes = mul_size(instrumentation->num_workers, sizeof(Instrumentation));
601  planstate->worker_instrument =
602  palloc(ibytes + offsetof(WorkerInstrumentation, instrument));
603  MemoryContextSwitchTo(oldcontext);
604 
605  planstate->worker_instrument->num_workers = instrumentation->num_workers;
606  memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
607 
609  instrumentation);
610 }
WorkerInstrumentation * worker_instrument
Definition: execnodes.h:807
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:82
Instrumentation * instrument
Definition: execnodes.h:806
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:143
int plan_node_id
Definition: plannodes.h:131
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
EState * state
Definition: execnodes.h:802
MemoryContext es_query_cxt
Definition: execnodes.h:435
#define ERROR
Definition: elog.h:43
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:569
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:800
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:3705
#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 304 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().

305 {
306  shm_mq_handle **responseq;
307  char *tqueuespace;
308  int i;
309 
310  /* Skip this if no workers. */
311  if (pcxt->nworkers == 0)
312  return NULL;
313 
314  /* Allocate memory for shared memory queue handles. */
315  responseq = (shm_mq_handle **)
316  palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
317 
318  /*
319  * If not reinitializing, allocate space from the DSM for the queues;
320  * otherwise, find the already allocated space.
321  */
322  if (!reinitialize)
323  tqueuespace =
324  shm_toc_allocate(pcxt->toc,
326  pcxt->nworkers));
327  else
328  tqueuespace = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE);
329 
330  /* Create the queues, and become the receiver for each. */
331  for (i = 0; i < pcxt->nworkers; ++i)
332  {
333  shm_mq *mq;
334 
335  mq = shm_mq_create(tqueuespace +
337  (Size) PARALLEL_TUPLE_QUEUE_SIZE);
338 
340  responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
341  }
342 
343  /* Add array of queues to shm_toc, so others can find it. */
344  if (!reinitialize)
345  shm_toc_insert(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, tqueuespace);
346 
347  /* Return array of handles. */
348  return responseq;
349 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:53
PGPROC * MyProc
Definition: proc.c:67
dsm_segment * seg
Definition: parallel.h:43
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:167
void * shm_toc_lookup(shm_toc *toc, uint64 key)
Definition: shm_toc.c:218
#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
shm_toc * toc
Definition: parallel.h:45
static char * ExecSerializePlan ( Plan plan,
EState estate 
)
static

Definition at line 123 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, lfirst, makeNode, NIL, nodeToString(), PlannedStmt::nonleafResultRelations, PlannedStmt::nParamExec, NULL, 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().

124 {
125  PlannedStmt *pstmt;
126  ListCell *tlist;
127 
128  /* We can't scribble on the original plan, so make a copy. */
129  plan = copyObject(plan);
130 
131  /*
132  * The worker will start its own copy of the executor, and that copy will
133  * insert a junk filter if the toplevel node has any resjunk entries. We
134  * don't want that to happen, because while resjunk columns shouldn't be
135  * sent back to the user, here the tuples are coming back to another
136  * backend which may very well need them. So mutate the target list
137  * accordingly. This is sort of a hack; there might be better ways to do
138  * this...
139  */
140  foreach(tlist, plan->targetlist)
141  {
142  TargetEntry *tle = (TargetEntry *) lfirst(tlist);
143 
144  tle->resjunk = false;
145  }
146 
147  /*
148  * Create a dummy PlannedStmt. Most of the fields don't need to be valid
149  * for our purposes, but the worker will need at least a minimal
150  * PlannedStmt to start the executor.
151  */
152  pstmt = makeNode(PlannedStmt);
153  pstmt->commandType = CMD_SELECT;
154  pstmt->queryId = 0;
155  pstmt->hasReturning = false;
156  pstmt->hasModifyingCTE = false;
157  pstmt->canSetTag = true;
158  pstmt->transientPlan = false;
159  pstmt->dependsOnRole = false;
160  pstmt->parallelModeNeeded = false;
161  pstmt->planTree = plan;
162  pstmt->rtable = estate->es_range_table;
163  pstmt->resultRelations = NIL;
164  pstmt->nonleafResultRelations = NIL;
165  pstmt->subplans = estate->es_plannedstmt->subplans;
166  pstmt->rewindPlanIDs = NULL;
167  pstmt->rowMarks = NIL;
168  pstmt->relationOids = NIL;
169  pstmt->invalItems = NIL; /* workers can't replan anyway... */
170  pstmt->nParamExec = estate->es_plannedstmt->nParamExec;
171  pstmt->utilityStmt = NULL;
172  pstmt->stmt_location = -1;
173  pstmt->stmt_len = -1;
174 
175  /* Return serialized copy of our dummy PlannedStmt. */
176  return nodeToString(pstmt);
177 }
bool dependsOnRole
Definition: plannodes.h:57
#define NIL
Definition: pg_list.h:69
uint32 queryId
Definition: plannodes.h:47
List * nonleafResultRelations
Definition: plannodes.h:69
List * relationOids
Definition: plannodes.h:77
PlannedStmt * es_plannedstmt
Definition: execnodes.h:411
List * es_range_table
Definition: execnodes.h:410
bool transientPlan
Definition: plannodes.h:55
int stmt_len
Definition: plannodes.h:87
struct Plan * planTree
Definition: plannodes.h:61
List * invalItems
Definition: plannodes.h:79
int nParamExec
Definition: plannodes.h:81
bool resjunk
Definition: primnodes.h:1359
int stmt_location
Definition: plannodes.h:86
bool hasReturning
Definition: plannodes.h:49
Node * utilityStmt
Definition: plannodes.h:83
bool canSetTag
Definition: plannodes.h:53
CmdType commandType
Definition: plannodes.h:45
List * rowMarks
Definition: plannodes.h:75
#define makeNode(_type_)
Definition: nodes.h:554
List * subplans
Definition: plannodes.h:71
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * rewindPlanIDs
Definition: plannodes.h:73
bool hasModifyingCTE
Definition: plannodes.h:51
List * rtable
Definition: plannodes.h:63
List * targetlist
Definition: plannodes.h:132
List * resultRelations
Definition: plannodes.h:66
bool parallelModeNeeded
Definition: plannodes.h:59
char * nodeToString(const void *obj)
Definition: outfuncs.c:4180
#define copyObject(obj)
Definition: nodes.h:618
static void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)
static

Definition at line 818 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.

Referenced by ExecInitParallelPlan().

819 {
820  BufferUsage *buffer_usage;
821  DestReceiver *receiver;
822  QueryDesc *queryDesc;
823  SharedExecutorInstrumentation *instrumentation;
824  int instrument_options = 0;
825  void *area_space;
826  dsa_area *area;
827 
828  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
829  receiver = ExecParallelGetReceiver(seg, toc);
830  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION);
831  if (instrumentation != NULL)
832  instrument_options = instrumentation->instrument_options;
833  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
834 
835  /* Setting debug_query_string for individual workers */
836  debug_query_string = queryDesc->sourceText;
837 
838  /* Report workers' query for monitoring purposes */
840 
841  /* Prepare to track buffer usage during query execution. */
843 
844  /* Attach to the dynamic shared memory area. */
845  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA);
846  area = dsa_attach_in_place(area_space, seg);
847 
848  /* Start up the executor */
849  ExecutorStart(queryDesc, 0);
850 
851  /* Special executor initialization steps for parallel workers */
852  queryDesc->planstate->state->es_query_dsa = area;
853  ExecParallelInitializeWorker(queryDesc->planstate, toc);
854 
855  /* Run the plan */
856  ExecutorRun(queryDesc, ForwardScanDirection, 0L, true);
857 
858  /* Shut down the executor */
859  ExecutorFinish(queryDesc);
860 
861  /* Report buffer usage during parallel execution. */
862  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE);
864 
865  /* Report instrumentation data if any instrumentation options are set. */
866  if (instrumentation != NULL)
868  instrumentation);
869 
870  /* Must do this after capturing instrumentation. */
871  ExecutorEnd(queryDesc);
872 
873  /* Cleanup. */
874  dsa_detach(area);
875  FreeQueryDesc(queryDesc);
876  (*receiver->rDestroy) (receiver);
877 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:475
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:126
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:103
void pgstat_report_activity(BackendState state, const char *cmd_str)
Definition: pgstat.c:2957
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:721
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:144
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:802
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:453
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:54
PlanState * planstate
Definition: execdesc.h:48
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:291
void * shm_toc_lookup(shm_toc *toc, uint64 key)
Definition: shm_toc.c:218
void dsa_detach(dsa_area *area)
Definition: dsa.c:1882
int ParallelWorkerNumber
Definition: parallel.c:96
static bool ExecParallelInitializeWorker(PlanState *planstate, shm_toc *toc)
Definition: execParallel.c:762
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:52
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:393
const char * debug_query_string
Definition: postgres.c:83
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:666
static QueryDesc * ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver, int instrument_options)
Definition: execParallel.c:682