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 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:1875
#define NULL
Definition: c.h:229
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
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