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

Typedef Documentation

Function Documentation

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

Definition at line 384 of file execParallel.c.

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

Referenced by ExecGather(), and ExecGatherMerge().

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

Definition at line 662 of file execParallel.c.

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

Referenced by ExecShutdownGather(), and ExecShutdownGatherMerge().

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

Definition at line 633 of file execParallel.c.

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

Referenced by ExecShutdownGatherMergeWorkers(), and ExecShutdownGatherWorkers().

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

Definition at line 372 of file execParallel.c.

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

Referenced by ExecReScanGather(), and ExecReScanGatherMerge().

373 {
375  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
376  pei->finished = false;
377 }
ParallelContext * pcxt
Definition: execParallel.h:27
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:320
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:380
shm_mq_handle ** tqueue
Definition: execParallel.h:30
void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)

Definition at line 834 of file execParallel.c.

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

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