PostgreSQL Source Code  git master
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, Bitmapset *sendParam, int nworkers, int64 tuples_needed)
 
void ExecParallelCreateReaders (ParallelExecutorInfo *pei)
 
void ExecParallelFinish (ParallelExecutorInfo *pei)
 
void ExecParallelCleanup (ParallelExecutorInfo *pei)
 
void ExecParallelReinitialize (PlanState *planstate, ParallelExecutorInfo *pei, Bitmapset *sendParam)
 
void ParallelQueryMain (dsm_segment *seg, shm_toc *toc)
 

Typedef Documentation

◆ ParallelExecutorInfo

◆ SharedExecutorInstrumentation

Function Documentation

◆ ExecInitParallelPlan()

ParallelExecutorInfo* ExecInitParallelPlan ( PlanState planstate,
EState estate,
Bitmapset sendParam,
int  nworkers,
int64  tuples_needed 
)

Definition at line 562 of file execParallel.c.

References ParallelExecutorInfo::area, bms_is_empty(), ParallelExecutorInfo::buffer_usage, CreateParallelContext(), dsa_create_in_place(), dsa_minimum_size(), FixedParallelExecutorState::eflags, elog, ERROR, EState::es_instrument, EState::es_jit_flags, EState::es_param_list_info, EState::es_query_dsa, EState::es_sourceText, EState::es_top_eflags, EstimateParamListSpace(), ParallelContext::estimator, ExecEvalParamExecParams(), ExecParallelEstimate(), ExecParallelInitializeDSM(), ExecParallelSetupTupleQueues(), ExecSerializePlan(), ParallelExecutorInfo::finished, GetInstrumentationArray, i, InitializeParallelDSM(), InstrInit(), SharedExecutorInstrumentation::instrument_offset, SharedExecutorInstrumentation::instrument_options, ParallelExecutorInfo::instrumentation, ExecParallelInitializeDSMContext::instrumentation, InvalidDsaPointer, FixedParallelExecutorState::jit_flags, 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_PARAMLISTINFO, PARALLEL_KEY_PLANNEDSTMT, PARALLEL_KEY_QUERY_TEXT, PARALLEL_TUPLE_QUEUE_SIZE, ParallelExecutorInfo::param_exec, FixedParallelExecutorState::param_exec, ParallelExecutorInfo::pcxt, ExecParallelEstimateContext::pcxt, ExecParallelInitializeDSMContext::pcxt, PlanState::plan, ParallelExecutorInfo::planstate, ParallelExecutorInfo::reader, ParallelContext::seg, SerializeParamExecParams(), 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().

565 {
567  ParallelContext *pcxt;
571  char *pstmt_data;
572  char *pstmt_space;
573  char *paramlistinfo_space;
574  BufferUsage *bufusage_space;
575  SharedExecutorInstrumentation *instrumentation = NULL;
576  int pstmt_len;
577  int paramlistinfo_len;
578  int instrumentation_len = 0;
579  int instrument_offset = 0;
580  Size dsa_minsize = dsa_minimum_size();
581  char *query_string;
582  int query_len;
583 
584  /* Force parameters we're going to pass to workers to be evaluated. */
585  ExecEvalParamExecParams(sendParams, estate);
586 
587  /* Allocate object for return value. */
588  pei = palloc0(sizeof(ParallelExecutorInfo));
589  pei->finished = false;
590  pei->planstate = planstate;
591 
592  /* Fix up and serialize plan to be sent to workers. */
593  pstmt_data = ExecSerializePlan(planstate->plan, estate);
594 
595  /* Create a parallel context. */
596  pcxt = CreateParallelContext("postgres", "ParallelQueryMain", nworkers, false);
597  pei->pcxt = pcxt;
598 
599  /*
600  * Before telling the parallel context to create a dynamic shared memory
601  * segment, we need to figure out how big it should be. Estimate space
602  * for the various things we need to store.
603  */
604 
605  /* Estimate space for fixed-size state. */
608  shm_toc_estimate_keys(&pcxt->estimator, 1);
609 
610  /* Estimate space for query text. */
611  query_len = strlen(estate->es_sourceText);
612  shm_toc_estimate_chunk(&pcxt->estimator, query_len + 1);
613  shm_toc_estimate_keys(&pcxt->estimator, 1);
614 
615  /* Estimate space for serialized PlannedStmt. */
616  pstmt_len = strlen(pstmt_data) + 1;
617  shm_toc_estimate_chunk(&pcxt->estimator, pstmt_len);
618  shm_toc_estimate_keys(&pcxt->estimator, 1);
619 
620  /* Estimate space for serialized ParamListInfo. */
621  paramlistinfo_len = EstimateParamListSpace(estate->es_param_list_info);
622  shm_toc_estimate_chunk(&pcxt->estimator, paramlistinfo_len);
623  shm_toc_estimate_keys(&pcxt->estimator, 1);
624 
625  /*
626  * Estimate space for BufferUsage.
627  *
628  * If EXPLAIN is not in use and there are no extensions loaded that care,
629  * we could skip this. But we have no way of knowing whether anyone's
630  * looking at pgBufferUsage, so do it unconditionally.
631  */
633  mul_size(sizeof(BufferUsage), pcxt->nworkers));
634  shm_toc_estimate_keys(&pcxt->estimator, 1);
635 
636  /* Estimate space for tuple queues. */
639  shm_toc_estimate_keys(&pcxt->estimator, 1);
640 
641  /*
642  * Give parallel-aware nodes a chance to add to the estimates, and get a
643  * count of how many PlanState nodes there are.
644  */
645  e.pcxt = pcxt;
646  e.nnodes = 0;
647  ExecParallelEstimate(planstate, &e);
648 
649  /* Estimate space for instrumentation, if required. */
650  if (estate->es_instrument)
651  {
652  instrumentation_len =
653  offsetof(SharedExecutorInstrumentation, plan_node_id) +
654  sizeof(int) * e.nnodes;
655  instrumentation_len = MAXALIGN(instrumentation_len);
656  instrument_offset = instrumentation_len;
657  instrumentation_len +=
658  mul_size(sizeof(Instrumentation),
659  mul_size(e.nnodes, nworkers));
660  shm_toc_estimate_chunk(&pcxt->estimator, instrumentation_len);
661  shm_toc_estimate_keys(&pcxt->estimator, 1);
662  }
663 
664  /* Estimate space for DSA area. */
665  shm_toc_estimate_chunk(&pcxt->estimator, dsa_minsize);
666  shm_toc_estimate_keys(&pcxt->estimator, 1);
667 
668  /* Everyone's had a chance to ask for space, so now create the DSM. */
669  InitializeParallelDSM(pcxt);
670 
671  /*
672  * OK, now we have a dynamic shared memory segment, and it should be big
673  * enough to store all of the data we estimated we would want to put into
674  * it, plus whatever general stuff (not specifically executor-related) the
675  * ParallelContext itself needs to store there. None of the space we
676  * asked for has been allocated or initialized yet, though, so do that.
677  */
678 
679  /* Store fixed-size state. */
680  fpes = shm_toc_allocate(pcxt->toc, sizeof(FixedParallelExecutorState));
681  fpes->tuples_needed = tuples_needed;
683  fpes->eflags = estate->es_top_eflags;
684  fpes->jit_flags = estate->es_jit_flags;
686 
687  /* Store query string */
688  query_string = shm_toc_allocate(pcxt->toc, query_len + 1);
689  memcpy(query_string, estate->es_sourceText, query_len + 1);
690  shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string);
691 
692  /* Store serialized PlannedStmt. */
693  pstmt_space = shm_toc_allocate(pcxt->toc, pstmt_len);
694  memcpy(pstmt_space, pstmt_data, pstmt_len);
695  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PLANNEDSTMT, pstmt_space);
696 
697  /* Store serialized ParamListInfo. */
698  paramlistinfo_space = shm_toc_allocate(pcxt->toc, paramlistinfo_len);
699  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PARAMLISTINFO, paramlistinfo_space);
700  SerializeParamList(estate->es_param_list_info, &paramlistinfo_space);
701 
702  /* Allocate space for each worker's BufferUsage; no need to initialize. */
703  bufusage_space = shm_toc_allocate(pcxt->toc,
704  mul_size(sizeof(BufferUsage), pcxt->nworkers));
705  shm_toc_insert(pcxt->toc, PARALLEL_KEY_BUFFER_USAGE, bufusage_space);
706  pei->buffer_usage = bufusage_space;
707 
708  /* Set up the tuple queues that the workers will write into. */
709  pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
710 
711  /* We don't need the TupleQueueReaders yet, though. */
712  pei->reader = NULL;
713 
714  /*
715  * If instrumentation options were supplied, allocate space for the data.
716  * It only gets partially initialized here; the rest happens during
717  * ExecParallelInitializeDSM.
718  */
719  if (estate->es_instrument)
720  {
721  Instrumentation *instrument;
722  int i;
723 
724  instrumentation = shm_toc_allocate(pcxt->toc, instrumentation_len);
725  instrumentation->instrument_options = estate->es_instrument;
726  instrumentation->instrument_offset = instrument_offset;
727  instrumentation->num_workers = nworkers;
728  instrumentation->num_plan_nodes = e.nnodes;
729  instrument = GetInstrumentationArray(instrumentation);
730  for (i = 0; i < nworkers * e.nnodes; ++i)
731  InstrInit(&instrument[i], estate->es_instrument);
733  instrumentation);
734  pei->instrumentation = instrumentation;
735  }
736 
737  /*
738  * Create a DSA area that can be used by the leader and all workers.
739  * (However, if we failed to create a DSM and are using private memory
740  * instead, then skip this.)
741  */
742  if (pcxt->seg != NULL)
743  {
744  char *area_space;
745 
746  area_space = shm_toc_allocate(pcxt->toc, dsa_minsize);
747  shm_toc_insert(pcxt->toc, PARALLEL_KEY_DSA, area_space);
748  pei->area = dsa_create_in_place(area_space, dsa_minsize,
750  pcxt->seg);
751 
752  /*
753  * Serialize parameters, if any, using DSA storage. We don't dare use
754  * the main parallel query DSM for this because we might relaunch
755  * workers after the values have changed (and thus the amount of
756  * storage required has changed).
757  */
758  if (!bms_is_empty(sendParams))
759  {
760  pei->param_exec = SerializeParamExecParams(estate, sendParams,
761  pei->area);
762  fpes->param_exec = pei->param_exec;
763  }
764  }
765 
766  /*
767  * Give parallel-aware nodes a chance to initialize their shared data.
768  * This also initializes the elements of instrumentation->ps_instrument,
769  * if it exists.
770  */
771  d.pcxt = pcxt;
772  d.instrumentation = instrumentation;
773  d.nnodes = 0;
774 
775  /* Install our DSA area while initializing the plan. */
776  estate->es_query_dsa = pei->area;
777  ExecParallelInitializeDSM(planstate, &d);
778  estate->es_query_dsa = NULL;
779 
780  /*
781  * Make sure that the world hasn't shifted under our feet. This could
782  * probably just be an Assert(), but let's be conservative for now.
783  */
784  if (e.nnodes != d.nnodes)
785  elog(ERROR, "inconsistent count of PlanState nodes");
786 
787  /* OK, we're ready to rock and roll. */
788  return pei;
789 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:565
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
dsm_segment * seg
Definition: parallel.h:42
shm_toc_estimator estimator
Definition: parallel.h:41
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers, bool serializable_okay)
Definition: parallel.c:152
PlanState * planstate
Definition: execParallel.h:26
static char * ExecSerializePlan(Plan *plan, EState *estate)
Definition: execParallel.c:143
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:510
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
Size EstimateParamListSpace(ParamListInfo paramLI)
Definition: params.c:88
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:340
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:119
ParallelContext * pcxt
Definition: execParallel.c:111
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:62
const char * es_sourceText
Definition: execnodes.h:482
static bool ExecParallelInitializeDSM(PlanState *node, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:424
#define ERROR
Definition: elog.h:43
BufferUsage * buffer_usage
Definition: execParallel.h:28
#define PARALLEL_KEY_PARAMLISTINFO
Definition: execParallel.c:59
#define PARALLEL_KEY_PLANNEDSTMT
Definition: execParallel.c:58
int es_jit_flags
Definition: execnodes.h:572
struct TupleQueueReader ** reader
Definition: execParallel.h:35
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:66
int es_instrument
Definition: execnodes.h:533
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:57
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:226
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:60
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:729
void InitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:206
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:955
Plan * plan
Definition: execnodes.h:912
Size dsa_minimum_size(void)
Definition: dsa.c:1160
void SerializeParamList(ParamListInfo paramLI, char **start_address)
Definition: params.c:148
void ExecEvalParamExecParams(Bitmapset *params, EState *estate)
size_t Size
Definition: c.h:433
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define MAXALIGN(LEN)
Definition: c.h:652
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
dsa_pointer param_exec
Definition: execParallel.h:31
#define PARALLEL_KEY_DSA
Definition: execParallel.c:63
e
Definition: preproc-init.c:82
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
int es_top_eflags
Definition: execnodes.h:532
int i
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:64
ParamListInfo es_param_list_info
Definition: execnodes.h:517
shm_mq_handle ** tqueue
Definition: execParallel.h:34
#define elog
Definition: elog.h:219
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:29
#define offsetof(type, field)
Definition: c.h:622
#define GetInstrumentationArray(sei)
Definition: execParallel.c:104
shm_toc * toc
Definition: parallel.h:44

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

Definition at line 1050 of file execParallel.c.

References ParallelExecutorInfo::area, DestroyParallelContext(), dsa_detach(), dsa_free(), DsaPointerIsValid, ExecParallelRetrieveInstrumentation(), ParallelExecutorInfo::instrumentation, InvalidDsaPointer, ParallelExecutorInfo::param_exec, ParallelExecutorInfo::pcxt, pfree(), and ParallelExecutorInfo::planstate.

Referenced by ExecShutdownGather(), and ExecShutdownGatherMerge().

1051 {
1052  /* Accumulate instrumentation, if any. */
1053  if (pei->instrumentation)
1055  pei->instrumentation);
1056 
1057  /* Free any serialized parameters. */
1058  if (DsaPointerIsValid(pei->param_exec))
1059  {
1060  dsa_free(pei->area, pei->param_exec);
1062  }
1063  if (pei->area != NULL)
1064  {
1065  dsa_detach(pei->area);
1066  pei->area = NULL;
1067  }
1068  if (pei->pcxt != NULL)
1069  {
1071  pei->pcxt = NULL;
1072  }
1073  pfree(pei);
1074 }
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
PlanState * planstate
Definition: execParallel.h:26
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:862
void pfree(void *pointer)
Definition: mcxt.c:1031
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:936
void dsa_detach(dsa_area *area)
Definition: dsa.c:1884
dsa_pointer param_exec
Definition: execParallel.h:31
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:812
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:29

◆ ExecParallelCreateReaders()

void ExecParallelCreateReaders ( ParallelExecutorInfo pei)

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

799 {
800  int nworkers = pei->pcxt->nworkers_launched;
801  int i;
802 
803  Assert(pei->reader == NULL);
804 
805  if (nworkers > 0)
806  {
807  pei->reader = (TupleQueueReader **)
808  palloc(nworkers * sizeof(TupleQueueReader *));
809 
810  for (i = 0; i < nworkers; i++)
811  {
812  shm_mq_set_handle(pei->tqueue[i],
813  pei->pcxt->worker[i].bgwhandle);
814  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
815  }
816  }
817 }
ParallelContext * pcxt
Definition: execParallel.h:27
ParallelWorkerInfo * worker
Definition: parallel.h:45
struct TupleQueueReader ** reader
Definition: execParallel.h:35
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:310
#define Assert(condition)
Definition: c.h:699
void * palloc(Size size)
Definition: mcxt.c:924
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:34

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

Definition at line 997 of file execParallel.c.

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

Referenced by ExecShutdownGatherMergeWorkers(), and ExecShutdownGatherWorkers().

998 {
999  int nworkers = pei->pcxt->nworkers_launched;
1000  int i;
1001 
1002  /* Make this be a no-op if called twice in a row. */
1003  if (pei->finished)
1004  return;
1005 
1006  /*
1007  * Detach from tuple queues ASAP, so that any still-active workers will
1008  * notice that no further results are wanted.
1009  */
1010  if (pei->tqueue != NULL)
1011  {
1012  for (i = 0; i < nworkers; i++)
1013  shm_mq_detach(pei->tqueue[i]);
1014  pfree(pei->tqueue);
1015  pei->tqueue = NULL;
1016  }
1017 
1018  /*
1019  * While we're waiting for the workers to finish, let's get rid of the
1020  * tuple queue readers. (Any other local cleanup could be done here too.)
1021  */
1022  if (pei->reader != NULL)
1023  {
1024  for (i = 0; i < nworkers; i++)
1026  pfree(pei->reader);
1027  pei->reader = NULL;
1028  }
1029 
1030  /* Now wait for the workers to finish. */
1032 
1033  /*
1034  * Next, accumulate buffer usage. (This must wait for the workers to
1035  * finish, or we might get incomplete data.)
1036  */
1037  for (i = 0; i < nworkers; i++)
1039 
1040  pei->finished = true;
1041 }
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:793
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:708
void pfree(void *pointer)
Definition: mcxt.c:1031
BufferUsage * buffer_usage
Definition: execParallel.h:28
struct TupleQueueReader ** reader
Definition: execParallel.h:35
void InstrAccumParallelQuery(BufferUsage *result)
Definition: instrument.c:186
int nworkers_launched
Definition: parallel.h:37
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:34

◆ ExecParallelReinitialize()

void ExecParallelReinitialize ( PlanState planstate,
ParallelExecutorInfo pei,
Bitmapset sendParam 
)

Definition at line 824 of file execParallel.c.

References ParallelExecutorInfo::area, Assert, bms_is_empty(), dsa_free(), DsaPointerIsValid, EState::es_query_dsa, ExecEvalParamExecParams(), ExecParallelReInitializeDSM(), ExecParallelSetupTupleQueues(), ParallelExecutorInfo::finished, InvalidDsaPointer, PARALLEL_KEY_EXECUTOR_FIXED, ParallelExecutorInfo::param_exec, FixedParallelExecutorState::param_exec, ParallelExecutorInfo::pcxt, ParallelExecutorInfo::reader, ReinitializeParallelDSM(), SerializeParamExecParams(), shm_toc_lookup(), PlanState::state, ParallelContext::toc, and ParallelExecutorInfo::tqueue.

Referenced by ExecGather(), and ExecGatherMerge().

827 {
828  EState *estate = planstate->state;
830 
831  /* Old workers must already be shut down */
832  Assert(pei->finished);
833 
834  /* Force parameters we're going to pass to workers to be evaluated. */
835  ExecEvalParamExecParams(sendParams, estate);
836 
838  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
839  pei->reader = NULL;
840  pei->finished = false;
841 
842  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
843 
844  /* Free any serialized parameters from the last round. */
845  if (DsaPointerIsValid(fpes->param_exec))
846  {
847  dsa_free(pei->area, fpes->param_exec);
849  }
850 
851  /* Serialize current parameter values if required. */
852  if (!bms_is_empty(sendParams))
853  {
854  pei->param_exec = SerializeParamExecParams(estate, sendParams,
855  pei->area);
856  fpes->param_exec = pei->param_exec;
857  }
858 
859  /* Traverse plan tree and let each child node reset associated state. */
860  estate->es_query_dsa = pei->area;
861  ExecParallelReInitializeDSM(planstate, pei->pcxt);
862  estate->es_query_dsa = NULL;
863 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:565
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:510
EState * state
Definition: execnodes.h:914
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:869
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:340
struct TupleQueueReader ** reader
Definition: execParallel.h:35
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:57
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:729
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:434
#define Assert(condition)
Definition: c.h:699
void ExecEvalParamExecParams(Bitmapset *params, EState *estate)
dsa_pointer param_exec
Definition: execParallel.h:31
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:812
shm_mq_handle ** tqueue
Definition: execParallel.h:34
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
shm_toc * toc
Definition: parallel.h:44

◆ ParallelQueryMain()

void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)

Definition at line 1256 of file execParallel.c.

References debug_query_string, dsa_attach_in_place(), dsa_detach(), dsa_get_address(), DsaPointerIsValid, FixedParallelExecutorState::eflags, EState::es_query_dsa, QueryDesc::estate, ExecParallelGetQueryDesc(), ExecParallelGetReceiver(), ExecParallelInitializeWorker(), ExecParallelReportInstrumentation(), ExecSetTupleBound(), ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), ForwardScanDirection, FreeQueryDesc(), InstrEndParallelQuery(), InstrStartParallelQuery(), SharedExecutorInstrumentation::instrument_options, FixedParallelExecutorState::jit_flags, PlannedStmt::jitFlags, PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_DSA, PARALLEL_KEY_EXECUTOR_FIXED, PARALLEL_KEY_INSTRUMENTATION, ParallelWorkerNumber, FixedParallelExecutorState::param_exec, pgstat_report_activity(), QueryDesc::plannedstmt, QueryDesc::planstate, _DestReceiver::rDestroy, RestoreParamExecParams(), ParallelWorkerContext::seg, shm_toc_lookup(), QueryDesc::sourceText, PlanState::state, STATE_RUNNING, ParallelWorkerContext::toc, and FixedParallelExecutorState::tuples_needed.

1257 {
1259  BufferUsage *buffer_usage;
1260  DestReceiver *receiver;
1261  QueryDesc *queryDesc;
1262  SharedExecutorInstrumentation *instrumentation;
1263  int instrument_options = 0;
1264  void *area_space;
1265  dsa_area *area;
1266  ParallelWorkerContext pwcxt;
1267 
1268  /* Get fixed-size state. */
1269  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1270 
1271  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1272  receiver = ExecParallelGetReceiver(seg, toc);
1273  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1274  if (instrumentation != NULL)
1275  instrument_options = instrumentation->instrument_options;
1276  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1277 
1278  /* Setting debug_query_string for individual workers */
1279  debug_query_string = queryDesc->sourceText;
1280 
1281  /* Report workers' query for monitoring purposes */
1283 
1284  /* Prepare to track buffer usage during query execution. */
1286 
1287  /* Attach to the dynamic shared memory area. */
1288  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1289  area = dsa_attach_in_place(area_space, seg);
1290 
1291  /* Start up the executor */
1292  queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1293  ExecutorStart(queryDesc, fpes->eflags);
1294 
1295  /* Special executor initialization steps for parallel workers */
1296  queryDesc->planstate->state->es_query_dsa = area;
1297  if (DsaPointerIsValid(fpes->param_exec))
1298  {
1299  char *paramexec_space;
1300 
1301  paramexec_space = dsa_get_address(area, fpes->param_exec);
1302  RestoreParamExecParams(paramexec_space, queryDesc->estate);
1303 
1304  }
1305  pwcxt.toc = toc;
1306  pwcxt.seg = seg;
1307  ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1308 
1309  /* Pass down any tuple bound */
1310  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1311 
1312  /*
1313  * Run the plan. If we specified a tuple bound, be careful not to demand
1314  * more tuples than that.
1315  */
1316  ExecutorRun(queryDesc,
1318  fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
1319  true);
1320 
1321  /* Shut down the executor */
1322  ExecutorFinish(queryDesc);
1323 
1324  /* Report buffer usage during parallel execution. */
1325  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1327 
1328  /* Report instrumentation data if any instrumentation options are set. */
1329  if (instrumentation != NULL)
1331  instrumentation);
1332 
1333  /* Must do this after capturing instrumentation. */
1334  ExecutorEnd(queryDesc);
1335 
1336  /* Cleanup. */
1337  dsa_detach(area);
1338  FreeQueryDesc(queryDesc);
1339  receiver->rDestroy(receiver);
1340 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:565
EState * estate
Definition: execdesc.h:48
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)
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:141
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:540
void InstrEndParallelQuery(BufferUsage *result)
Definition: instrument.c:178
EState * state
Definition: execnodes.h:914
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:799
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:461
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:62
PlanState * planstate
Definition: execdesc.h:49
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:299
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
void dsa_detach(dsa_area *area)
Definition: dsa.c:1884
int ParallelWorkerNumber
Definition: parallel.c:103
int jitFlags
Definition: plannodes.h:62
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:57
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:60
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:401
const char * debug_query_string
Definition: postgres.c:87
void InstrStartParallelQuery(void)
Definition: instrument.c:171
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:126
const char * sourceText
Definition: execdesc.h:38
#define DsaPointerIsValid(x)
Definition: dsa.h:81
#define PARALLEL_KEY_DSA
Definition: execParallel.c:63
Definition: dsa.c:354
static DestReceiver * ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
static void RestoreParamExecParams(char *start_address, EState *estate)
Definition: execParallel.c:395
PlannedStmt * plannedstmt
Definition: execdesc.h:37
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)
dsm_segment * seg
Definition: parallel.h:52