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  FixedParallelExecutorState
 
struct  SharedExecutorInstrumentation
 
struct  ExecParallelEstimateContext
 
struct  ExecParallelInitializeDSMContext
 

Macros

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

Typedefs

typedef struct
FixedParallelExecutorState 
FixedParallelExecutorState
 
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 ExecParallelReInitializeDSM (PlanState *planstate, ParallelContext *pcxt)
 
static bool ExecParallelRetrieveInstrumentation (PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
 
static DestReceiverExecParallelGetReceiver (dsm_segment *seg, shm_toc *toc)
 
ParallelExecutorInfoExecInitParallelPlan (PlanState *planstate, EState *estate, int nworkers, int64 tuples_needed)
 
void ExecParallelCreateReaders (ParallelExecutorInfo *pei)
 
void ExecParallelReinitialize (PlanState *planstate, ParallelExecutorInfo *pei)
 
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:775

Definition at line 95 of file execParallel.c.

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

#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xE000000000000004)

Definition at line 54 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

#define PARALLEL_KEY_DSA   UINT64CONST(0xE000000000000007)

Definition at line 57 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

#define PARALLEL_KEY_EXECUTOR_FIXED   UINT64CONST(0xE000000000000001)

Definition at line 51 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

#define PARALLEL_KEY_INSTRUMENTATION   UINT64CONST(0xE000000000000006)

Definition at line 56 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

#define PARALLEL_KEY_PARAMS   UINT64CONST(0xE000000000000003)

Definition at line 53 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

#define PARALLEL_KEY_PLANNEDSTMT   UINT64CONST(0xE000000000000002)

Definition at line 52 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xE000000000000008)

Definition at line 58 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

#define PARALLEL_KEY_TUPLE_QUEUE   UINT64CONST(0xE000000000000005)

Definition at line 55 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,
int64  tuples_needed 
)

Definition at line 398 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, SharedExecutorInstrumentation::num_plan_nodes, SharedExecutorInstrumentation::num_workers, ParallelContext::nworkers, offsetof, palloc0(), PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_DSA, PARALLEL_KEY_EXECUTOR_FIXED, 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, ParallelExecutorInfo::reader, ParallelContext::seg, SerializeParamList(), shm_toc_allocate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_insert(), ParallelContext::toc, ParallelExecutorInfo::tqueue, and FixedParallelExecutorState::tuples_needed.

Referenced by ExecGather(), and ExecGatherMerge().

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

Definition at line 832 of file execParallel.c.

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

Referenced by ExecShutdownGather(), and ExecShutdownGatherMerge().

833 {
834  if (pei->area != NULL)
835  {
836  dsa_detach(pei->area);
837  pei->area = NULL;
838  }
839  if (pei->pcxt != NULL)
840  {
842  pei->pcxt = NULL;
843  }
844  pfree(pei);
845 }
ParallelContext * pcxt
Definition: execParallel.h:27
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:623
void pfree(void *pointer)
Definition: mcxt.c:949
void dsa_detach(dsa_area *area)
Definition: dsa.c:1884
void ExecParallelCreateReaders ( ParallelExecutorInfo pei)

Definition at line 616 of file execParallel.c.

References Assert, ParallelWorkerInfo::bgwhandle, CreateTupleQueueReader(), i, ParallelContext::nworkers_launched, palloc(), ParallelExecutorInfo::pcxt, ParallelExecutorInfo::reader, shm_mq_set_handle(), ParallelExecutorInfo::tqueue, and ParallelContext::worker.

Referenced by ExecGather(), and ExecGatherMerge().

617 {
618  int nworkers = pei->pcxt->nworkers_launched;
619  int i;
620 
621  Assert(pei->reader == NULL);
622 
623  if (nworkers > 0)
624  {
625  pei->reader = (TupleQueueReader **)
626  palloc(nworkers * sizeof(TupleQueueReader *));
627 
628  for (i = 0; i < nworkers; i++)
629  {
630  shm_mq_set_handle(pei->tqueue[i],
631  pei->pcxt->worker[i].bgwhandle);
632  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
633  }
634  }
635 }
ParallelContext * pcxt
Definition: execParallel.h:27
ParallelWorkerInfo * worker
Definition: parallel.h:45
struct TupleQueueReader ** reader
Definition: execParallel.h:34
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
int nworkers_launched
Definition: parallel.h:37
TupleQueueReader * CreateTupleQueueReader(shm_mq_handle *handle)
Definition: tqueue.c:135
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:315
#define Assert(condition)
Definition: c.h:664
void * palloc(Size size)
Definition: mcxt.c:848
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:33
static bool ExecParallelEstimate ( PlanState node,
ExecParallelEstimateContext e 
)
static

Definition at line 217 of file execParallel.c.

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

Referenced by ExecInitParallelPlan().

218 {
219  if (planstate == NULL)
220  return false;
221 
222  /* Count this node. */
223  e->nnodes++;
224 
225  switch (nodeTag(planstate))
226  {
227  case T_SeqScanState:
228  if (planstate->plan->parallel_aware)
229  ExecSeqScanEstimate((SeqScanState *) planstate,
230  e->pcxt);
231  break;
232  case T_IndexScanState:
233  if (planstate->plan->parallel_aware)
235  e->pcxt);
236  break;
238  if (planstate->plan->parallel_aware)
240  e->pcxt);
241  break;
242  case T_ForeignScanState:
243  if (planstate->plan->parallel_aware)
245  e->pcxt);
246  break;
247  case T_CustomScanState:
248  if (planstate->plan->parallel_aware)
250  e->pcxt);
251  break;
253  if (planstate->plan->parallel_aware)
255  e->pcxt);
256  break;
257  case T_SortState:
258  /* even when not parallel-aware */
259  ExecSortEstimate((SortState *) planstate, e->pcxt);
260  break;
261 
262  default:
263  break;
264  }
265 
266  return planstate_tree_walker(planstate, ExecParallelEstimate, e);
267 }
void ExecSortEstimate(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:360
ParallelContext * pcxt
Definition: execParallel.c:102
void ExecIndexOnlyScanEstimate(IndexOnlyScanState *node, ParallelContext *pcxt)
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:217
void ExecForeignScanEstimate(ForeignScanState *node, ParallelContext *pcxt)
void ExecSeqScanEstimate(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:296
#define nodeTag(nodeptr)
Definition: nodes.h:514
void ExecBitmapHeapEstimate(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecCustomScanEstimate(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:169
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 774 of file execParallel.c.

References ParallelExecutorInfo::buffer_usage, DestroyTupleQueueReader(), ExecParallelRetrieveInstrumentation(), ParallelExecutorInfo::finished, i, InstrAccumParallelQuery(), ParallelExecutorInfo::instrumentation, ParallelContext::nworkers_launched, ParallelExecutorInfo::pcxt, pfree(), ParallelExecutorInfo::planstate, ParallelExecutorInfo::reader, shm_mq_detach(), ParallelExecutorInfo::tqueue, and WaitForParallelWorkersToFinish().

Referenced by ExecShutdownGatherMergeWorkers(), and ExecShutdownGatherWorkers().

775 {
776  int nworkers = pei->pcxt->nworkers_launched;
777  int i;
778 
779  /* Make this be a no-op if called twice in a row. */
780  if (pei->finished)
781  return;
782 
783  /*
784  * Detach from tuple queues ASAP, so that any still-active workers will
785  * notice that no further results are wanted.
786  */
787  if (pei->tqueue != NULL)
788  {
789  for (i = 0; i < nworkers; i++)
790  shm_mq_detach(pei->tqueue[i]);
791  pfree(pei->tqueue);
792  pei->tqueue = NULL;
793  }
794 
795  /*
796  * While we're waiting for the workers to finish, let's get rid of the
797  * tuple queue readers. (Any other local cleanup could be done here too.)
798  */
799  if (pei->reader != NULL)
800  {
801  for (i = 0; i < nworkers; i++)
803  pfree(pei->reader);
804  pei->reader = NULL;
805  }
806 
807  /* Now wait for the workers to finish. */
809 
810  /*
811  * Next, accumulate buffer usage. (This must wait for the workers to
812  * finish, or we might get incomplete data.)
813  */
814  for (i = 0; i < nworkers; i++)
816 
817  /* Finally, accumulate instrumentation, if any. */
818  if (pei->instrumentation)
820  pei->instrumentation);
821 
822  pei->finished = true;
823 }
ParallelContext * pcxt
Definition: execParallel.h:27
void DestroyTupleQueueReader(TupleQueueReader *reader)
Definition: tqueue.c:151
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:775
PlanState * planstate
Definition: execParallel.h:26
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:533
void pfree(void *pointer)
Definition: mcxt.c:949
BufferUsage * buffer_usage
Definition: execParallel.h:28
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:719
struct TupleQueueReader ** reader
Definition: execParallel.h:34
void InstrAccumParallelQuery(BufferUsage *result)
Definition: instrument.c:185
int nworkers_launched
Definition: parallel.h:37
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:33
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:29
static QueryDesc* ExecParallelGetQueryDesc ( shm_toc toc,
DestReceiver receiver,
int  instrument_options 
)
static

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

870 {
871  char *pstmtspace;
872  char *paramspace;
873  PlannedStmt *pstmt;
874  ParamListInfo paramLI;
875  char *queryString;
876 
877  /* Get the query string from shared memory */
878  queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
879 
880  /* Reconstruct leader-supplied PlannedStmt. */
881  pstmtspace = shm_toc_lookup(toc, PARALLEL_KEY_PLANNEDSTMT, false);
882  pstmt = (PlannedStmt *) stringToNode(pstmtspace);
883 
884  /* Reconstruct ParamListInfo. */
885  paramspace = shm_toc_lookup(toc, PARALLEL_KEY_PARAMS, false);
886  paramLI = RestoreParamList(&paramspace);
887 
888  /*
889  * Create a QueryDesc for the query.
890  *
891  * It's not obvious how to obtain the query string from here; and even if
892  * we could copying it would take more cycles than not copying it. But
893  * it's a bit unsatisfying to just use a dummy string here, so consider
894  * revising this someday.
895  */
896  return CreateQueryDesc(pstmt,
897  queryString,
899  receiver, paramLI, NULL, instrument_options);
900 }
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:52
ParamListInfo RestoreParamList(char **start_address)
Definition: params.c:224
#define PARALLEL_KEY_PARAMS
Definition: execParallel.c:53
#define InvalidSnapshot
Definition: snapshot.h:25
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:58
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
static DestReceiver * ExecParallelGetReceiver ( dsm_segment seg,
shm_toc toc 
)
static

Definition at line 852 of file execParallel.c.

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

Referenced by ParallelQueryMain().

853 {
854  char *mqspace;
855  shm_mq *mq;
856 
857  mqspace = shm_toc_lookup(toc, PARALLEL_KEY_TUPLE_QUEUE, false);
859  mq = (shm_mq *) mqspace;
861  return CreateTupleQueueDestReceiver(shm_mq_attach(mq, seg, NULL));
862 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:55
PGPROC * MyProc
Definition: proc.c:67
int ParallelWorkerNumber
Definition: parallel.c:98
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:60
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:218
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:287
Definition: shm_mq.c:69
DestReceiver * CreateTupleQueueDestReceiver(shm_mq_handle *handle)
Definition: tqueue.c:115
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
static bool ExecParallelInitializeDSM ( PlanState node,
ExecParallelInitializeDSMContext d 
)
static

Definition at line 274 of file execParallel.c.

References ExecBitmapHeapInitializeDSM(), ExecCustomScanInitializeDSM(), ExecForeignScanInitializeDSM(), ExecIndexOnlyScanInitializeDSM(), ExecIndexScanInitializeDSM(), ExecSeqScanInitializeDSM(), ExecSortInitializeDSM(), ExecParallelInitializeDSMContext::instrumentation, ExecParallelInitializeDSMContext::nnodes, nodeTag, 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, T_SeqScanState, and T_SortState.

Referenced by ExecInitParallelPlan().

276 {
277  if (planstate == NULL)
278  return false;
279 
280  /* If instrumentation is enabled, initialize slot for this node. */
281  if (d->instrumentation != NULL)
283  planstate->plan->plan_node_id;
284 
285  /* Count this node. */
286  d->nnodes++;
287 
288  /*
289  * Call initializers for DSM-using plan nodes.
290  *
291  * Most plan nodes won't do anything here, but plan nodes that allocated
292  * DSM may need to initialize shared state in the DSM before parallel
293  * workers are launched. They can allocate the space they previously
294  * estimated using shm_toc_allocate, and add the keys they previously
295  * estimated using shm_toc_insert, in each case targeting pcxt->toc.
296  */
297  switch (nodeTag(planstate))
298  {
299  case T_SeqScanState:
300  if (planstate->plan->parallel_aware)
302  d->pcxt);
303  break;
304  case T_IndexScanState:
305  if (planstate->plan->parallel_aware)
307  d->pcxt);
308  break;
310  if (planstate->plan->parallel_aware)
312  d->pcxt);
313  break;
314  case T_ForeignScanState:
315  if (planstate->plan->parallel_aware)
317  d->pcxt);
318  break;
319  case T_CustomScanState:
320  if (planstate->plan->parallel_aware)
322  d->pcxt);
323  break;
325  if (planstate->plan->parallel_aware)
327  d->pcxt);
328  break;
329  case T_SortState:
330  /* even when not parallel-aware */
331  ExecSortInitializeDSM((SortState *) planstate, d->pcxt);
332  break;
333 
334  default:
335  break;
336  }
337 
338  return planstate_tree_walker(planstate, ExecParallelInitializeDSM, d);
339 }
void ExecIndexScanInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:92
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:110
void ExecSortInitializeDSM(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:381
void ExecSeqScanInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:313
static bool ExecParallelInitializeDSM(PlanState *node, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:274
void ExecBitmapHeapInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecForeignScanInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:182
#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 948 of file execParallel.c.

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

Referenced by ParallelQueryMain().

949 {
950  if (planstate == NULL)
951  return false;
952 
953  switch (nodeTag(planstate))
954  {
955  case T_SeqScanState:
956  if (planstate->plan->parallel_aware)
957  ExecSeqScanInitializeWorker((SeqScanState *) planstate, toc);
958  break;
959  case T_IndexScanState:
960  if (planstate->plan->parallel_aware)
961  ExecIndexScanInitializeWorker((IndexScanState *) planstate, toc);
962  break;
964  if (planstate->plan->parallel_aware)
966  break;
967  case T_ForeignScanState:
968  if (planstate->plan->parallel_aware)
970  toc);
971  break;
972  case T_CustomScanState:
973  if (planstate->plan->parallel_aware)
975  toc);
976  break;
978  if (planstate->plan->parallel_aware)
980  break;
981  case T_SortState:
982  /* even when not parallel-aware */
983  ExecSortInitializeWorker((SortState *) planstate, toc);
984  break;
985 
986  default:
987  break;
988  }
989 
990  return planstate_tree_walker(planstate, ExecParallelInitializeWorker, toc);
991 }
void ExecIndexScanInitializeWorker(IndexScanState *node, shm_toc *toc)
void ExecSeqScanInitializeWorker(SeqScanState *node, shm_toc *toc)
Definition: nodeSeqscan.c:350
void ExecSortInitializeWorker(SortState *node, shm_toc *toc)
Definition: nodeSort.c:423
void ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, shm_toc *toc)
void ExecCustomScanInitializeWorker(CustomScanState *node, shm_toc *toc)
Definition: nodeCustom.c:213
void ExecForeignScanInitializeWorker(ForeignScanState *node, shm_toc *toc)
static bool ExecParallelInitializeWorker(PlanState *planstate, shm_toc *toc)
Definition: execParallel.c:948
bool parallel_aware
Definition: plannodes.h:137
Plan * plan
Definition: execnodes.h:847
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 ( PlanState planstate,
ParallelExecutorInfo pei 
)

Definition at line 642 of file execParallel.c.

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

Referenced by ExecGather(), and ExecGatherMerge().

644 {
645  /* Old workers must already be shut down */
646  Assert(pei->finished);
647 
649  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
650  pei->reader = NULL;
651  pei->finished = false;
652 
653  /* Traverse plan tree and let each child node reset associated state. */
654  ExecParallelReInitializeDSM(planstate, pei->pcxt);
655 }
ParallelContext * pcxt
Definition: execParallel.h:27
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:346
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:661
struct TupleQueueReader ** reader
Definition: execParallel.h:34
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:409
#define Assert(condition)
Definition: c.h:664
shm_mq_handle ** tqueue
Definition: execParallel.h:33
static bool ExecParallelReInitializeDSM ( PlanState planstate,
ParallelContext pcxt 
)
static

Definition at line 661 of file execParallel.c.

References ExecBitmapHeapReInitializeDSM(), ExecCustomScanReInitializeDSM(), ExecForeignScanReInitializeDSM(), ExecIndexOnlyScanReInitializeDSM(), ExecIndexScanReInitializeDSM(), ExecSeqScanReInitializeDSM(), ExecSortReInitializeDSM(), nodeTag, Plan::parallel_aware, PlanState::plan, planstate_tree_walker(), T_BitmapHeapScanState, T_CustomScanState, T_ForeignScanState, T_IndexOnlyScanState, T_IndexScanState, T_SeqScanState, and T_SortState.

Referenced by ExecParallelReinitialize().

663 {
664  if (planstate == NULL)
665  return false;
666 
667  /*
668  * Call reinitializers for DSM-using plan nodes.
669  */
670  switch (nodeTag(planstate))
671  {
672  case T_SeqScanState:
673  if (planstate->plan->parallel_aware)
675  pcxt);
676  break;
677  case T_IndexScanState:
678  if (planstate->plan->parallel_aware)
680  pcxt);
681  break;
683  if (planstate->plan->parallel_aware)
685  pcxt);
686  break;
687  case T_ForeignScanState:
688  if (planstate->plan->parallel_aware)
690  pcxt);
691  break;
692  case T_CustomScanState:
693  if (planstate->plan->parallel_aware)
695  pcxt);
696  break;
698  if (planstate->plan->parallel_aware)
700  pcxt);
701  break;
702  case T_SortState:
703  /* even when not parallel-aware */
704  ExecSortReInitializeDSM((SortState *) planstate, pcxt);
705  break;
706 
707  default:
708  break;
709  }
710 
711  return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt);
712 }
void ExecCustomScanReInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:198
void ExecIndexScanReInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:661
void ExecForeignScanReInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecSortReInitializeDSM(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:406
bool parallel_aware
Definition: plannodes.h:137
void ExecBitmapHeapReInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
Plan * plan
Definition: execnodes.h:847
void ExecIndexOnlyScanReInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
#define nodeTag(nodeptr)
Definition: nodes.h:514
void ExecSeqScanReInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:335
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697
static bool ExecParallelReportInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

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

909 {
910  int i;
911  int plan_node_id = planstate->plan->plan_node_id;
912  Instrumentation *instrument;
913 
914  InstrEndLoop(planstate->instrument);
915 
916  /*
917  * If we shuffled the plan_node_id values in ps_instrument into sorted
918  * order, we could use binary search here. This might matter someday if
919  * we're pushing down sufficiently large plan trees. For now, do it the
920  * slow, dumb way.
921  */
922  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
923  if (instrumentation->plan_node_id[i] == plan_node_id)
924  break;
925  if (i >= instrumentation->num_plan_nodes)
926  elog(ERROR, "plan node %d not found", plan_node_id);
927 
928  /*
929  * Add our statistics to the per-node, per-worker totals. It's possible
930  * that this could happen more than once if we relaunched workers.
931  */
932  instrument = GetInstrumentationArray(instrumentation);
933  instrument += i * instrumentation->num_workers;
935  Assert(ParallelWorkerNumber < instrumentation->num_workers);
936  InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
937 
939  instrumentation);
940 }
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:92
Instrumentation * instrument
Definition: execnodes.h:857
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:143
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:907
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:98
#define IsParallelWorker()
Definition: parallel.h:52
Plan * plan
Definition: execnodes.h:847
#define Assert(condition)
Definition: c.h:664
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:95
static bool ExecParallelRetrieveInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 719 of file execParallel.c.

References elog, ERROR, EState::es_query_cxt, ExecSortRetrieveInstrumentation(), GetInstrumentationArray, i, InstrAggNode(), WorkerInstrumentation::instrument, PlanState::instrument, IsA, 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().

721 {
722  Instrumentation *instrument;
723  int i;
724  int n;
725  int ibytes;
726  int plan_node_id = planstate->plan->plan_node_id;
727  MemoryContext oldcontext;
728 
729  /* Find the instrumentation for this node. */
730  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
731  if (instrumentation->plan_node_id[i] == plan_node_id)
732  break;
733  if (i >= instrumentation->num_plan_nodes)
734  elog(ERROR, "plan node %d not found", plan_node_id);
735 
736  /* Accumulate the statistics from all workers. */
737  instrument = GetInstrumentationArray(instrumentation);
738  instrument += i * instrumentation->num_workers;
739  for (n = 0; n < instrumentation->num_workers; ++n)
740  InstrAggNode(planstate->instrument, &instrument[n]);
741 
742  /*
743  * Also store the per-worker detail.
744  *
745  * Worker instrumentation should be allocated in the same context as the
746  * regular instrumentation information, which is the per-query context.
747  * Switch into per-query memory context.
748  */
749  oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
750  ibytes = mul_size(instrumentation->num_workers, sizeof(Instrumentation));
751  planstate->worker_instrument =
752  palloc(ibytes + offsetof(WorkerInstrumentation, instrument));
753  MemoryContextSwitchTo(oldcontext);
754 
755  planstate->worker_instrument->num_workers = instrumentation->num_workers;
756  memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
757 
758  /*
759  * Perform any node-type-specific work that needs to be done. Currently,
760  * only Sort nodes need to do anything here.
761  */
762  if (IsA(planstate, SortState))
764 
766  instrumentation);
767 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
WorkerInstrumentation * worker_instrument
Definition: execnodes.h:858
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:92
Instrumentation * instrument
Definition: execnodes.h:857
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:849
MemoryContext es_query_cxt
Definition: execnodes.h:471
#define ERROR
Definition: elog.h:43
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:719
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:847
void * palloc(Size size)
Definition: mcxt.c:848
int i
#define elog
Definition: elog.h:219
void ExecSortRetrieveInstrumentation(SortState *node)
Definition: nodeSort.c:437
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697
#define offsetof(type, field)
Definition: c.h:549
#define GetInstrumentationArray(sei)
Definition: execParallel.c:95
static shm_mq_handle ** ExecParallelSetupTupleQueues ( ParallelContext pcxt,
bool  reinitialize 
)
static

Definition at line 346 of file execParallel.c.

References i, mul_size(), MyProc, 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().

347 {
348  shm_mq_handle **responseq;
349  char *tqueuespace;
350  int i;
351 
352  /* Skip this if no workers. */
353  if (pcxt->nworkers == 0)
354  return NULL;
355 
356  /* Allocate memory for shared memory queue handles. */
357  responseq = (shm_mq_handle **)
358  palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
359 
360  /*
361  * If not reinitializing, allocate space from the DSM for the queues;
362  * otherwise, find the already allocated space.
363  */
364  if (!reinitialize)
365  tqueuespace =
366  shm_toc_allocate(pcxt->toc,
368  pcxt->nworkers));
369  else
370  tqueuespace = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, false);
371 
372  /* Create the queues, and become the receiver for each. */
373  for (i = 0; i < pcxt->nworkers; ++i)
374  {
375  shm_mq *mq;
376 
377  mq = shm_mq_create(tqueuespace +
379  (Size) PARALLEL_TUPLE_QUEUE_SIZE);
380 
382  responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
383  }
384 
385  /* Add array of queues to shm_toc, so others can find it. */
386  if (!reinitialize)
387  shm_toc_insert(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, tqueuespace);
388 
389  /* Return array of handles. */
390  return responseq;
391 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:55
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:170
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:60
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
size_t Size
Definition: c.h:350
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:287
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * palloc(Size size)
Definition: mcxt.c:848
int i
Definition: shm_mq.c:69
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:199
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
shm_toc * toc
Definition: parallel.h:44
static char * ExecSerializePlan ( Plan plan,
EState estate 
)
static

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

135 {
136  PlannedStmt *pstmt;
137  ListCell *lc;
138 
139  /* We can't scribble on the original plan, so make a copy. */
140  plan = copyObject(plan);
141 
142  /*
143  * The worker will start its own copy of the executor, and that copy will
144  * insert a junk filter if the toplevel node has any resjunk entries. We
145  * don't want that to happen, because while resjunk columns shouldn't be
146  * sent back to the user, here the tuples are coming back to another
147  * backend which may very well need them. So mutate the target list
148  * accordingly. This is sort of a hack; there might be better ways to do
149  * this...
150  */
151  foreach(lc, plan->targetlist)
152  {
153  TargetEntry *tle = lfirst_node(TargetEntry, lc);
154 
155  tle->resjunk = false;
156  }
157 
158  /*
159  * Create a dummy PlannedStmt. Most of the fields don't need to be valid
160  * for our purposes, but the worker will need at least a minimal
161  * PlannedStmt to start the executor.
162  */
163  pstmt = makeNode(PlannedStmt);
164  pstmt->commandType = CMD_SELECT;
165  pstmt->queryId = 0;
166  pstmt->hasReturning = false;
167  pstmt->hasModifyingCTE = false;
168  pstmt->canSetTag = true;
169  pstmt->transientPlan = false;
170  pstmt->dependsOnRole = false;
171  pstmt->parallelModeNeeded = false;
172  pstmt->planTree = plan;
173  pstmt->rtable = estate->es_range_table;
174  pstmt->resultRelations = NIL;
175  pstmt->nonleafResultRelations = NIL;
176 
177  /*
178  * Transfer only parallel-safe subplans, leaving a NULL "hole" in the list
179  * for unsafe ones (so that the list indexes of the safe ones are
180  * preserved). This positively ensures that the worker won't try to run,
181  * or even do ExecInitNode on, an unsafe subplan. That's important to
182  * protect, eg, non-parallel-aware FDWs from getting into trouble.
183  */
184  pstmt->subplans = NIL;
185  foreach(lc, estate->es_plannedstmt->subplans)
186  {
187  Plan *subplan = (Plan *) lfirst(lc);
188 
189  if (subplan && !subplan->parallel_safe)
190  subplan = NULL;
191  pstmt->subplans = lappend(pstmt->subplans, subplan);
192  }
193 
194  pstmt->rewindPlanIDs = NULL;
195  pstmt->rowMarks = NIL;
196  pstmt->relationOids = NIL;
197  pstmt->invalItems = NIL; /* workers can't replan anyway... */
198  pstmt->nParamExec = estate->es_plannedstmt->nParamExec;
199  pstmt->utilityStmt = NULL;
200  pstmt->stmt_location = -1;
201  pstmt->stmt_len = -1;
202 
203  /* Return serialized copy of our dummy PlannedStmt. */
204  return nodeToString(pstmt);
205 }
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 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:4255
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 1010 of file execParallel.c.

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

1011 {
1013  BufferUsage *buffer_usage;
1014  DestReceiver *receiver;
1015  QueryDesc *queryDesc;
1016  SharedExecutorInstrumentation *instrumentation;
1017  int instrument_options = 0;
1018  void *area_space;
1019  dsa_area *area;
1020 
1021  /* Get fixed-size state. */
1022  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1023 
1024  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1025  receiver = ExecParallelGetReceiver(seg, toc);
1026  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1027  if (instrumentation != NULL)
1028  instrument_options = instrumentation->instrument_options;
1029  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1030 
1031  /* Setting debug_query_string for individual workers */
1032  debug_query_string = queryDesc->sourceText;
1033 
1034  /* Report workers' query for monitoring purposes */
1036 
1037  /* Prepare to track buffer usage during query execution. */
1039 
1040  /* Attach to the dynamic shared memory area. */
1041  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1042  area = dsa_attach_in_place(area_space, seg);
1043 
1044  /* Start up the executor */
1045  ExecutorStart(queryDesc, 0);
1046 
1047  /* Special executor initialization steps for parallel workers */
1048  queryDesc->planstate->state->es_query_dsa = area;
1049  ExecParallelInitializeWorker(queryDesc->planstate, toc);
1050 
1051  /* Pass down any tuple bound */
1052  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1053 
1054  /*
1055  * Run the plan. If we specified a tuple bound, be careful not to demand
1056  * more tuples than that.
1057  */
1058  ExecutorRun(queryDesc,
1060  fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
1061  true);
1062 
1063  /* Shut down the executor */
1064  ExecutorFinish(queryDesc);
1065 
1066  /* Report buffer usage during parallel execution. */
1067  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1069 
1070  /* Report instrumentation data if any instrumentation options are set. */
1071  if (instrumentation != NULL)
1073  instrumentation);
1074 
1075  /* Must do this after capturing instrumentation. */
1076  ExecutorEnd(queryDesc);
1077 
1078  /* Cleanup. */
1079  dsa_detach(area);
1080  FreeQueryDesc(queryDesc);
1081  receiver->rDestroy(receiver);
1082 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:511
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:907
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:147
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:849
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:778
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:461
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:56
PlanState * planstate
Definition: execdesc.h:49
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:299
void dsa_detach(dsa_area *area)
Definition: dsa.c:1884
int ParallelWorkerNumber
Definition: parallel.c:98
static bool ExecParallelInitializeWorker(PlanState *planstate, shm_toc *toc)
Definition: execParallel.c:948
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:51
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:54
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:401
const char * debug_query_string
Definition: postgres.c:85
void InstrStartParallelQuery(void)
Definition: instrument.c:170
const char * sourceText
Definition: execdesc.h:38
#define PARALLEL_KEY_DSA
Definition: execParallel.c:57
Definition: dsa.c:354
static DestReceiver * ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
Definition: execParallel.c:852
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
static QueryDesc * ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver, int instrument_options)
Definition: execParallel.c:868