PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
execParallel.h File Reference
#include "access/parallel.h"
#include "nodes/execnodes.h"
#include "nodes/parsenodes.h"
#include "nodes/plannodes.h"
#include "utils/dsa.h"
Include dependency graph for execParallel.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ParallelExecutorInfo
 

Typedefs

typedef struct
SharedExecutorInstrumentation 
SharedExecutorInstrumentation
 
typedef struct ParallelExecutorInfo ParallelExecutorInfo
 

Functions

ParallelExecutorInfoExecInitParallelPlan (PlanState *planstate, EState *estate, int nworkers)
 
void ExecParallelFinish (ParallelExecutorInfo *pei)
 
void ExecParallelCleanup (ParallelExecutorInfo *pei)
 
void ExecParallelReinitialize (ParallelExecutorInfo *pei)
 

Typedef Documentation

Function Documentation

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

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

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

Definition at line 635 of file execParallel.c.

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

Referenced by ExecShutdownGather().

636 {
637  if (pei->area != NULL)
638  {
639  dsa_detach(pei->area);
640  pei->area = NULL;
641  }
642  if (pei->pcxt != NULL)
643  {
645  pei->pcxt = NULL;
646  }
647  pfree(pei);
648 }
ParallelContext * pcxt
Definition: execParallel.h:27
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:609
void pfree(void *pointer)
Definition: mcxt.c:992
void dsa_detach(dsa_area *area)
Definition: dsa.c:1875
#define NULL
Definition: c.h:226
void ExecParallelFinish ( ParallelExecutorInfo pei)

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

607 {
608  int i;
609 
610  if (pei->finished)
611  return;
612 
613  /* First, wait for the workers to finish. */
615 
616  /* Next, accumulate buffer usage. */
617  for (i = 0; i < pei->pcxt->nworkers_launched; ++i)
619 
620  /* Finally, accumulate instrumentation, if any. */
621  if (pei->instrumentation)
623  pei->instrumentation);
624 
625  pei->finished = true;
626 }
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:558
void InstrAccumParallelQuery(BufferUsage *result)
Definition: instrument.c:185
int nworkers_launched
Definition: parallel.h:37
int i
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:29
void ExecParallelReinitialize ( ParallelExecutorInfo pei)

Definition at line 345 of file execParallel.c.

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

Referenced by ExecReScanGather().

346 {
348  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
349  pei->finished = false;
350 }
ParallelContext * pcxt
Definition: execParallel.h:27
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:293
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:396
shm_mq_handle ** tqueue
Definition: execParallel.h:30