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 561 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_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, 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().

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

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

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

1049 {
1050  /* Accumulate instrumentation, if any. */
1051  if (pei->instrumentation)
1053  pei->instrumentation);
1054 
1055  /* Free any serialized parameters. */
1056  if (DsaPointerIsValid(pei->param_exec))
1057  {
1058  dsa_free(pei->area, pei->param_exec);
1060  }
1061  if (pei->area != NULL)
1062  {
1063  dsa_detach(pei->area);
1064  pei->area = NULL;
1065  }
1066  if (pei->pcxt != NULL)
1067  {
1069  pei->pcxt = NULL;
1070  }
1071  pfree(pei);
1072 }
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:936
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:934
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 796 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().

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

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

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

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

825 {
826  EState *estate = planstate->state;
828 
829  /* Old workers must already be shut down */
830  Assert(pei->finished);
831 
832  /* Force parameters we're going to pass to workers to be evaluated. */
833  ExecEvalParamExecParams(sendParams, estate);
834 
836  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
837  pei->reader = NULL;
838  pei->finished = false;
839 
840  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
841 
842  /* Free any serialized parameters from the last round. */
843  if (DsaPointerIsValid(fpes->param_exec))
844  {
845  dsa_free(pei->area, fpes->param_exec);
847  }
848 
849  /* Serialize current parameter values if required. */
850  if (!bms_is_empty(sendParams))
851  {
852  pei->param_exec = SerializeParamExecParams(estate, sendParams,
853  pei->area);
854  fpes->param_exec = pei->param_exec;
855  }
856 
857  /* Traverse plan tree and let each child node reset associated state. */
858  estate->es_query_dsa = pei->area;
859  ExecParallelReInitializeDSM(planstate, pei->pcxt);
860  estate->es_query_dsa = NULL;
861 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:530
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:509
EState * state
Definition: execnodes.h:870
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:867
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:339
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:707
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:434
#define Assert(condition)
Definition: c.h:688
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 1254 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, PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_DSA, PARALLEL_KEY_EXECUTOR_FIXED, PARALLEL_KEY_INSTRUMENTATION, ParallelWorkerNumber, FixedParallelExecutorState::param_exec, pgstat_report_activity(), QueryDesc::planstate, _DestReceiver::rDestroy, RestoreParamExecParams(), ParallelWorkerContext::seg, shm_toc_lookup(), QueryDesc::sourceText, PlanState::state, STATE_RUNNING, ParallelWorkerContext::toc, and FixedParallelExecutorState::tuples_needed.

1255 {
1257  BufferUsage *buffer_usage;
1258  DestReceiver *receiver;
1259  QueryDesc *queryDesc;
1260  SharedExecutorInstrumentation *instrumentation;
1261  int instrument_options = 0;
1262  void *area_space;
1263  dsa_area *area;
1264  ParallelWorkerContext pwcxt;
1265 
1266  /* Get fixed-size state. */
1267  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1268 
1269  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1270  receiver = ExecParallelGetReceiver(seg, toc);
1271  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1272  if (instrumentation != NULL)
1273  instrument_options = instrumentation->instrument_options;
1274  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1275 
1276  /* Setting debug_query_string for individual workers */
1277  debug_query_string = queryDesc->sourceText;
1278 
1279  /* Report workers' query for monitoring purposes */
1281 
1282  /* Prepare to track buffer usage during query execution. */
1284 
1285  /* Attach to the dynamic shared memory area. */
1286  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1287  area = dsa_attach_in_place(area_space, seg);
1288 
1289  /* Start up the executor */
1290  ExecutorStart(queryDesc, fpes->eflags);
1291 
1292  /* Special executor initialization steps for parallel workers */
1293  queryDesc->planstate->state->es_query_dsa = area;
1294  if (DsaPointerIsValid(fpes->param_exec))
1295  {
1296  char *paramexec_space;
1297 
1298  paramexec_space = dsa_get_address(area, fpes->param_exec);
1299  RestoreParamExecParams(paramexec_space, queryDesc->estate);
1300 
1301  }
1302  pwcxt.toc = toc;
1303  pwcxt.seg = seg;
1304  ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1305 
1306  /* Pass down any tuple bound */
1307  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1308 
1309  /*
1310  * Run the plan. If we specified a tuple bound, be careful not to demand
1311  * more tuples than that.
1312  */
1313  ExecutorRun(queryDesc,
1315  fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
1316  true);
1317 
1318  /* Shut down the executor */
1319  ExecutorFinish(queryDesc);
1320 
1321  /* Report buffer usage during parallel execution. */
1322  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1324 
1325  /* Report instrumentation data if any instrumentation options are set. */
1326  if (instrumentation != NULL)
1328  instrumentation);
1329 
1330  /* Must do this after capturing instrumentation. */
1331  ExecutorEnd(queryDesc);
1332 
1333  /* Cleanup. */
1334  dsa_detach(area);
1335  FreeQueryDesc(queryDesc);
1336  receiver->rDestroy(receiver);
1337 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:530
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:140
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:870
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:800
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:459
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:297
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
void dsa_detach(dsa_area *area)
Definition: dsa.c:1884
int ParallelWorkerNumber
Definition: parallel.c:103
#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:399
const char * debug_query_string
Definition: postgres.c:85
void InstrStartParallelQuery(void)
Definition: instrument.c:170
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:394
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