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_jit_flags, EState::es_param_list_info, EState::es_query_dsa, EState::es_sourceText, EState::es_top_eflags, EstimateParamListSpace(), ParallelContext::estimator, ExecParallelEstimate(), ExecParallelInitializeDSM(), ExecParallelSetupTupleQueues(), ExecSerializePlan(), ExecSetParamPlanMulti(), ParallelExecutorInfo::finished, GetInstrumentationArray, GetPerTupleExprContext, i, InitializeParallelDSM(), InstrInit(), SharedExecutorInstrumentation::instrument_offset, SharedExecutorInstrumentation::instrument_options, ParallelExecutorInfo::instrumentation, ExecParallelInitializeDSMContext::instrumentation, InvalidDsaPointer, FixedParallelExecutorState::jit_flags, SharedJitInstrumentation::jit_instr, ParallelExecutorInfo::jit_instrumentation, LWTRANCHE_PARALLEL_QUERY_DSA, MAXALIGN, mul_size(), ExecParallelEstimateContext::nnodes, ExecParallelInitializeDSMContext::nnodes, SharedExecutorInstrumentation::num_plan_nodes, SharedJitInstrumentation::num_workers, SharedExecutorInstrumentation::num_workers, ParallelContext::nworkers, offsetof, palloc0(), PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_DSA, PARALLEL_KEY_EXECUTOR_FIXED, PARALLEL_KEY_INSTRUMENTATION, PARALLEL_KEY_JIT_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, PGJIT_NONE, 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  SharedJitInstrumentation *jit_instrumentation = NULL;
576  int pstmt_len;
577  int paramlistinfo_len;
578  int instrumentation_len = 0;
579  int jit_instrumentation_len = 0;
580  int instrument_offset = 0;
581  Size dsa_minsize = dsa_minimum_size();
582  char *query_string;
583  int query_len;
584 
585  /*
586  * Force any initplan outputs that we're going to pass to workers to be
587  * evaluated, if they weren't already.
588  *
589  * For simplicity, we use the EState's per-output-tuple ExprContext here.
590  * That risks intra-query memory leakage, since we might pass through here
591  * many times before that ExprContext gets reset; but ExecSetParamPlan
592  * doesn't normally leak any memory in the context (see its comments), so
593  * it doesn't seem worth complicating this function's API to pass it a
594  * shorter-lived ExprContext. This might need to change someday.
595  */
596  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
597 
598  /* Allocate object for return value. */
599  pei = palloc0(sizeof(ParallelExecutorInfo));
600  pei->finished = false;
601  pei->planstate = planstate;
602 
603  /* Fix up and serialize plan to be sent to workers. */
604  pstmt_data = ExecSerializePlan(planstate->plan, estate);
605 
606  /* Create a parallel context. */
607  pcxt = CreateParallelContext("postgres", "ParallelQueryMain", nworkers);
608  pei->pcxt = pcxt;
609 
610  /*
611  * Before telling the parallel context to create a dynamic shared memory
612  * segment, we need to figure out how big it should be. Estimate space
613  * for the various things we need to store.
614  */
615 
616  /* Estimate space for fixed-size state. */
619  shm_toc_estimate_keys(&pcxt->estimator, 1);
620 
621  /* Estimate space for query text. */
622  query_len = strlen(estate->es_sourceText);
623  shm_toc_estimate_chunk(&pcxt->estimator, query_len + 1);
624  shm_toc_estimate_keys(&pcxt->estimator, 1);
625 
626  /* Estimate space for serialized PlannedStmt. */
627  pstmt_len = strlen(pstmt_data) + 1;
628  shm_toc_estimate_chunk(&pcxt->estimator, pstmt_len);
629  shm_toc_estimate_keys(&pcxt->estimator, 1);
630 
631  /* Estimate space for serialized ParamListInfo. */
632  paramlistinfo_len = EstimateParamListSpace(estate->es_param_list_info);
633  shm_toc_estimate_chunk(&pcxt->estimator, paramlistinfo_len);
634  shm_toc_estimate_keys(&pcxt->estimator, 1);
635 
636  /*
637  * Estimate space for BufferUsage.
638  *
639  * If EXPLAIN is not in use and there are no extensions loaded that care,
640  * we could skip this. But we have no way of knowing whether anyone's
641  * looking at pgBufferUsage, so do it unconditionally.
642  */
644  mul_size(sizeof(BufferUsage), pcxt->nworkers));
645  shm_toc_estimate_keys(&pcxt->estimator, 1);
646 
647  /* Estimate space for tuple queues. */
650  shm_toc_estimate_keys(&pcxt->estimator, 1);
651 
652  /*
653  * Give parallel-aware nodes a chance to add to the estimates, and get a
654  * count of how many PlanState nodes there are.
655  */
656  e.pcxt = pcxt;
657  e.nnodes = 0;
658  ExecParallelEstimate(planstate, &e);
659 
660  /* Estimate space for instrumentation, if required. */
661  if (estate->es_instrument)
662  {
663  instrumentation_len =
664  offsetof(SharedExecutorInstrumentation, plan_node_id) +
665  sizeof(int) * e.nnodes;
666  instrumentation_len = MAXALIGN(instrumentation_len);
667  instrument_offset = instrumentation_len;
668  instrumentation_len +=
669  mul_size(sizeof(Instrumentation),
670  mul_size(e.nnodes, nworkers));
671  shm_toc_estimate_chunk(&pcxt->estimator, instrumentation_len);
672  shm_toc_estimate_keys(&pcxt->estimator, 1);
673 
674  /* Estimate space for JIT instrumentation, if required. */
675  if (estate->es_jit_flags != PGJIT_NONE)
676  {
677  jit_instrumentation_len =
678  offsetof(SharedJitInstrumentation, jit_instr) +
679  sizeof(JitInstrumentation) * nworkers;
680  shm_toc_estimate_chunk(&pcxt->estimator, jit_instrumentation_len);
681  shm_toc_estimate_keys(&pcxt->estimator, 1);
682  }
683  }
684 
685  /* Estimate space for DSA area. */
686  shm_toc_estimate_chunk(&pcxt->estimator, dsa_minsize);
687  shm_toc_estimate_keys(&pcxt->estimator, 1);
688 
689  /* Everyone's had a chance to ask for space, so now create the DSM. */
690  InitializeParallelDSM(pcxt);
691 
692  /*
693  * OK, now we have a dynamic shared memory segment, and it should be big
694  * enough to store all of the data we estimated we would want to put into
695  * it, plus whatever general stuff (not specifically executor-related) the
696  * ParallelContext itself needs to store there. None of the space we
697  * asked for has been allocated or initialized yet, though, so do that.
698  */
699 
700  /* Store fixed-size state. */
701  fpes = shm_toc_allocate(pcxt->toc, sizeof(FixedParallelExecutorState));
702  fpes->tuples_needed = tuples_needed;
704  fpes->eflags = estate->es_top_eflags;
705  fpes->jit_flags = estate->es_jit_flags;
707 
708  /* Store query string */
709  query_string = shm_toc_allocate(pcxt->toc, query_len + 1);
710  memcpy(query_string, estate->es_sourceText, query_len + 1);
711  shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string);
712 
713  /* Store serialized PlannedStmt. */
714  pstmt_space = shm_toc_allocate(pcxt->toc, pstmt_len);
715  memcpy(pstmt_space, pstmt_data, pstmt_len);
716  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PLANNEDSTMT, pstmt_space);
717 
718  /* Store serialized ParamListInfo. */
719  paramlistinfo_space = shm_toc_allocate(pcxt->toc, paramlistinfo_len);
720  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PARAMLISTINFO, paramlistinfo_space);
721  SerializeParamList(estate->es_param_list_info, &paramlistinfo_space);
722 
723  /* Allocate space for each worker's BufferUsage; no need to initialize. */
724  bufusage_space = shm_toc_allocate(pcxt->toc,
725  mul_size(sizeof(BufferUsage), pcxt->nworkers));
726  shm_toc_insert(pcxt->toc, PARALLEL_KEY_BUFFER_USAGE, bufusage_space);
727  pei->buffer_usage = bufusage_space;
728 
729  /* Set up the tuple queues that the workers will write into. */
730  pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
731 
732  /* We don't need the TupleQueueReaders yet, though. */
733  pei->reader = NULL;
734 
735  /*
736  * If instrumentation options were supplied, allocate space for the data.
737  * It only gets partially initialized here; the rest happens during
738  * ExecParallelInitializeDSM.
739  */
740  if (estate->es_instrument)
741  {
742  Instrumentation *instrument;
743  int i;
744 
745  instrumentation = shm_toc_allocate(pcxt->toc, instrumentation_len);
746  instrumentation->instrument_options = estate->es_instrument;
747  instrumentation->instrument_offset = instrument_offset;
748  instrumentation->num_workers = nworkers;
749  instrumentation->num_plan_nodes = e.nnodes;
750  instrument = GetInstrumentationArray(instrumentation);
751  for (i = 0; i < nworkers * e.nnodes; ++i)
752  InstrInit(&instrument[i], estate->es_instrument);
754  instrumentation);
755  pei->instrumentation = instrumentation;
756 
757  if (estate->es_jit_flags != PGJIT_NONE)
758  {
759  jit_instrumentation = shm_toc_allocate(pcxt->toc,
760  jit_instrumentation_len);
761  jit_instrumentation->num_workers = nworkers;
762  memset(jit_instrumentation->jit_instr, 0,
763  sizeof(JitInstrumentation) * nworkers);
765  jit_instrumentation);
766  pei->jit_instrumentation = jit_instrumentation;
767  }
768  }
769 
770  /*
771  * Create a DSA area that can be used by the leader and all workers.
772  * (However, if we failed to create a DSM and are using private memory
773  * instead, then skip this.)
774  */
775  if (pcxt->seg != NULL)
776  {
777  char *area_space;
778 
779  area_space = shm_toc_allocate(pcxt->toc, dsa_minsize);
780  shm_toc_insert(pcxt->toc, PARALLEL_KEY_DSA, area_space);
781  pei->area = dsa_create_in_place(area_space, dsa_minsize,
783  pcxt->seg);
784 
785  /*
786  * Serialize parameters, if any, using DSA storage. We don't dare use
787  * the main parallel query DSM for this because we might relaunch
788  * workers after the values have changed (and thus the amount of
789  * storage required has changed).
790  */
791  if (!bms_is_empty(sendParams))
792  {
793  pei->param_exec = SerializeParamExecParams(estate, sendParams,
794  pei->area);
795  fpes->param_exec = pei->param_exec;
796  }
797  }
798 
799  /*
800  * Give parallel-aware nodes a chance to initialize their shared data.
801  * This also initializes the elements of instrumentation->ps_instrument,
802  * if it exists.
803  */
804  d.pcxt = pcxt;
805  d.instrumentation = instrumentation;
806  d.nnodes = 0;
807 
808  /* Install our DSA area while initializing the plan. */
809  estate->es_query_dsa = pei->area;
810  ExecParallelInitializeDSM(planstate, &d);
811  estate->es_query_dsa = NULL;
812 
813  /*
814  * Make sure that the world hasn't shifted under our feet. This could
815  * probably just be an Assert(), but let's be conservative for now.
816  */
817  if (e.nnodes != d.nnodes)
818  elog(ERROR, "inconsistent count of PlanState nodes");
819 
820  /* OK, we're ready to rock and roll. */
821  return pei;
822 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:584
void ExecSetParamPlanMulti(const Bitmapset *params, ExprContext *econtext)
Definition: nodeSubplan.c:1246
ParallelContext * pcxt
Definition: execParallel.h:27
#define PGJIT_NONE
Definition: jit.h:19
#define InvalidDsaPointer
Definition: dsa.h:78
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
Definition: parallel.c:159
struct SharedJitInstrumentation * jit_instrumentation
Definition: execParallel.h:30
dsm_segment * seg
Definition: parallel.h:42
shm_toc_estimator estimator
Definition: parallel.h:41
PlanState * planstate
Definition: execParallel.h:26
size_t dsa_minimum_size(void)
Definition: dsa.c:1169
static char * ExecSerializePlan(Plan *plan, EState *estate)
Definition: execParallel.c:143
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:103
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER]
Definition: jit.h:51
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:339
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:119
ParallelContext * pcxt
Definition: execParallel.c:111
#define GetPerTupleExprContext(estate)
Definition: executor.h:501
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:61
const char * es_sourceText
Definition: execnodes.h:512
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:58
#define PARALLEL_KEY_PLANNEDSTMT
Definition: execParallel.c:57
int es_jit_flags
Definition: execnodes.h:595
struct TupleQueueReader ** reader
Definition: execParallel.h:36
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:66
int es_instrument
Definition: execnodes.h:557
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:56
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:469
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:59
#define PARALLEL_KEY_JIT_INSTRUMENTATION
Definition: execParallel.c:64
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
void InitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:196
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:940
void SerializeParamList(ParamListInfo paramLI, char **start_address)
Definition: params.c:163
size_t Size
Definition: c.h:466
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define MAXALIGN(LEN)
Definition: c.h:685
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
dsa_pointer param_exec
Definition: execParallel.h:32
#define PARALLEL_KEY_DSA
Definition: execParallel.c:62
e
Definition: preproc-init.c:82
struct JitInstrumentation JitInstrumentation
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
int es_top_eflags
Definition: execnodes.h:556
#define elog(elevel,...)
Definition: elog.h:226
int i
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:63
ParamListInfo es_param_list_info
Definition: execnodes.h:544
shm_mq_handle ** tqueue
Definition: execParallel.h:35
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:29
#define offsetof(type, field)
Definition: c.h:655
#define GetInstrumentationArray(sei)
Definition: execParallel.c:104
shm_toc * toc
Definition: parallel.h:44

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

Definition at line 1126 of file execParallel.c.

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

Referenced by ExecShutdownGather(), and ExecShutdownGatherMerge().

1127 {
1128  /* Accumulate instrumentation, if any. */
1129  if (pei->instrumentation)
1131  pei->instrumentation);
1132 
1133  /* Accumulate JIT instrumentation, if any. */
1134  if (pei->jit_instrumentation)
1136  pei->jit_instrumentation);
1137 
1138  /* Free any serialized parameters. */
1139  if (DsaPointerIsValid(pei->param_exec))
1140  {
1141  dsa_free(pei->area, pei->param_exec);
1143  }
1144  if (pei->area != NULL)
1145  {
1146  dsa_detach(pei->area);
1147  pei->area = NULL;
1148  }
1149  if (pei->pcxt != NULL)
1150  {
1152  pei->pcxt = NULL;
1153  }
1154  pfree(pei);
1155 }
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
struct SharedJitInstrumentation * jit_instrumentation
Definition: execParallel.h:30
PlanState * planstate
Definition: execParallel.h:26
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:871
void pfree(void *pointer)
Definition: mcxt.c:1031
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:973
void dsa_detach(dsa_area *area)
Definition: dsa.c:1913
static void ExecParallelRetrieveJitInstrumentation(PlanState *planstate, SharedJitInstrumentation *shared_jit)
dsa_pointer param_exec
Definition: execParallel.h:32
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:29

◆ ExecParallelCreateReaders()

void ExecParallelCreateReaders ( ParallelExecutorInfo pei)

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

832 {
833  int nworkers = pei->pcxt->nworkers_launched;
834  int i;
835 
836  Assert(pei->reader == NULL);
837 
838  if (nworkers > 0)
839  {
840  pei->reader = (TupleQueueReader **)
841  palloc(nworkers * sizeof(TupleQueueReader *));
842 
843  for (i = 0; i < nworkers; i++)
844  {
845  shm_mq_set_handle(pei->tqueue[i],
846  pei->pcxt->worker[i].bgwhandle);
847  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
848  }
849  }
850 }
ParallelContext * pcxt
Definition: execParallel.h:27
ParallelWorkerInfo * worker
Definition: parallel.h:45
struct TupleQueueReader ** reader
Definition: execParallel.h:36
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
int nworkers_launched
Definition: parallel.h:37
TupleQueueReader * CreateTupleQueueReader(shm_mq_handle *handle)
Definition: tqueue.c:139
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:310
#define Assert(condition)
Definition: c.h:732
void * palloc(Size size)
Definition: mcxt.c:924
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:35

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

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

1074 {
1075  int nworkers = pei->pcxt->nworkers_launched;
1076  int i;
1077 
1078  /* Make this be a no-op if called twice in a row. */
1079  if (pei->finished)
1080  return;
1081 
1082  /*
1083  * Detach from tuple queues ASAP, so that any still-active workers will
1084  * notice that no further results are wanted.
1085  */
1086  if (pei->tqueue != NULL)
1087  {
1088  for (i = 0; i < nworkers; i++)
1089  shm_mq_detach(pei->tqueue[i]);
1090  pfree(pei->tqueue);
1091  pei->tqueue = NULL;
1092  }
1093 
1094  /*
1095  * While we're waiting for the workers to finish, let's get rid of the
1096  * tuple queue readers. (Any other local cleanup could be done here too.)
1097  */
1098  if (pei->reader != NULL)
1099  {
1100  for (i = 0; i < nworkers; i++)
1102  pfree(pei->reader);
1103  pei->reader = NULL;
1104  }
1105 
1106  /* Now wait for the workers to finish. */
1108 
1109  /*
1110  * Next, accumulate buffer usage. (This must wait for the workers to
1111  * finish, or we might get incomplete data.)
1112  */
1113  for (i = 0; i < nworkers; i++)
1115 
1116  pei->finished = true;
1117 }
ParallelContext * pcxt
Definition: execParallel.h:27
void DestroyTupleQueueReader(TupleQueueReader *reader)
Definition: tqueue.c:155
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:793
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:717
void pfree(void *pointer)
Definition: mcxt.c:1031
BufferUsage * buffer_usage
Definition: execParallel.h:28
struct TupleQueueReader ** reader
Definition: execParallel.h:36
void InstrAccumParallelQuery(BufferUsage *result)
Definition: instrument.c:182
int nworkers_launched
Definition: parallel.h:37
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:35

◆ ExecParallelReinitialize()

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

Definition at line 857 of file execParallel.c.

References ParallelExecutorInfo::area, Assert, bms_is_empty(), dsa_free(), DsaPointerIsValid, EState::es_query_dsa, ExecParallelReInitializeDSM(), ExecParallelSetupTupleQueues(), ExecSetParamPlanMulti(), ParallelExecutorInfo::finished, GetPerTupleExprContext, 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().

860 {
861  EState *estate = planstate->state;
863 
864  /* Old workers must already be shut down */
865  Assert(pei->finished);
866 
867  /*
868  * Force any initplan outputs that we're going to pass to workers to be
869  * evaluated, if they weren't already (see comments in
870  * ExecInitParallelPlan).
871  */
872  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
873 
875  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
876  pei->reader = NULL;
877  pei->finished = false;
878 
879  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
880 
881  /* Free any serialized parameters from the last round. */
882  if (DsaPointerIsValid(fpes->param_exec))
883  {
884  dsa_free(pei->area, fpes->param_exec);
886  }
887 
888  /* Serialize current parameter values if required. */
889  if (!bms_is_empty(sendParams))
890  {
891  pei->param_exec = SerializeParamExecParams(estate, sendParams,
892  pei->area);
893  fpes->param_exec = pei->param_exec;
894  }
895 
896  /* Traverse plan tree and let each child node reset associated state. */
897  estate->es_query_dsa = pei->area;
898  ExecParallelReInitializeDSM(planstate, pei->pcxt);
899  estate->es_query_dsa = NULL;
900 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:584
void ExecSetParamPlanMulti(const Bitmapset *params, ExprContext *econtext)
Definition: nodeSubplan.c:1246
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:942
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:906
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:339
#define GetPerTupleExprContext(estate)
Definition: executor.h:501
struct TupleQueueReader ** reader
Definition: execParallel.h:36
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:56
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:447
#define Assert(condition)
Definition: c.h:732
dsa_pointer param_exec
Definition: execParallel.h:32
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
shm_mq_handle ** tqueue
Definition: execParallel.h:35
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 1330 of file execParallel.c.

References Assert, debug_query_string, dsa_attach_in_place(), dsa_detach(), dsa_get_address(), DsaPointerIsValid, FixedParallelExecutorState::eflags, EState::es_jit, EState::es_query_dsa, QueryDesc::estate, ExecParallelGetQueryDesc(), ExecParallelGetReceiver(), ExecParallelInitializeWorker(), ExecParallelReportInstrumentation(), ExecSetTupleBound(), ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), ForwardScanDirection, FreeQueryDesc(), JitContext::instr, InstrEndParallelQuery(), InstrStartParallelQuery(), SharedExecutorInstrumentation::instrument_options, FixedParallelExecutorState::jit_flags, SharedJitInstrumentation::jit_instr, PlannedStmt::jitFlags, PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_DSA, PARALLEL_KEY_EXECUTOR_FIXED, PARALLEL_KEY_INSTRUMENTATION, PARALLEL_KEY_JIT_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.

1331 {
1333  BufferUsage *buffer_usage;
1334  DestReceiver *receiver;
1335  QueryDesc *queryDesc;
1336  SharedExecutorInstrumentation *instrumentation;
1337  SharedJitInstrumentation *jit_instrumentation;
1338  int instrument_options = 0;
1339  void *area_space;
1340  dsa_area *area;
1341  ParallelWorkerContext pwcxt;
1342 
1343  /* Get fixed-size state. */
1344  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1345 
1346  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1347  receiver = ExecParallelGetReceiver(seg, toc);
1348  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1349  if (instrumentation != NULL)
1350  instrument_options = instrumentation->instrument_options;
1351  jit_instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
1352  true);
1353  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1354 
1355  /* Setting debug_query_string for individual workers */
1356  debug_query_string = queryDesc->sourceText;
1357 
1358  /* Report workers' query for monitoring purposes */
1360 
1361  /* Attach to the dynamic shared memory area. */
1362  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1363  area = dsa_attach_in_place(area_space, seg);
1364 
1365  /* Start up the executor */
1366  queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1367  ExecutorStart(queryDesc, fpes->eflags);
1368 
1369  /* Special executor initialization steps for parallel workers */
1370  queryDesc->planstate->state->es_query_dsa = area;
1371  if (DsaPointerIsValid(fpes->param_exec))
1372  {
1373  char *paramexec_space;
1374 
1375  paramexec_space = dsa_get_address(area, fpes->param_exec);
1376  RestoreParamExecParams(paramexec_space, queryDesc->estate);
1377 
1378  }
1379  pwcxt.toc = toc;
1380  pwcxt.seg = seg;
1381  ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1382 
1383  /* Pass down any tuple bound */
1384  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1385 
1386  /*
1387  * Prepare to track buffer usage during query execution.
1388  *
1389  * We do this after starting up the executor to match what happens in the
1390  * leader, which also doesn't count buffer accesses that occur during
1391  * executor startup.
1392  */
1394 
1395  /*
1396  * Run the plan. If we specified a tuple bound, be careful not to demand
1397  * more tuples than that.
1398  */
1399  ExecutorRun(queryDesc,
1401  fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
1402  true);
1403 
1404  /* Shut down the executor */
1405  ExecutorFinish(queryDesc);
1406 
1407  /* Report buffer usage during parallel execution. */
1408  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1410 
1411  /* Report instrumentation data if any instrumentation options are set. */
1412  if (instrumentation != NULL)
1414  instrumentation);
1415 
1416  /* Report JIT instrumentation data if any */
1417  if (queryDesc->estate->es_jit && jit_instrumentation != NULL)
1418  {
1419  Assert(ParallelWorkerNumber < jit_instrumentation->num_workers);
1420  jit_instrumentation->jit_instr[ParallelWorkerNumber] =
1421  queryDesc->estate->es_jit->instr;
1422  }
1423 
1424  /* Must do this after capturing instrumentation. */
1425  ExecutorEnd(queryDesc);
1426 
1427  /* Cleanup. */
1428  dsa_detach(area);
1429  FreeQueryDesc(queryDesc);
1430  receiver->rDestroy(receiver);
1431 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:584
EState * estate
Definition: execdesc.h:48
struct JitContext * es_jit
Definition: execnodes.h:596
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
void pgstat_report_activity(BackendState state, const char *cmd_str)
Definition: pgstat.c:3121
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:143
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:541
void InstrEndParallelQuery(BufferUsage *result)
Definition: instrument.c:174
EState * state
Definition: execnodes.h:942
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER]
Definition: jit.h:51
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:812
JitInstrumentation instr
Definition: jit.h:61
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:462
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:61
PlanState * planstate
Definition: execdesc.h:49
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:301
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
void dsa_detach(dsa_area *area)
Definition: dsa.c:1913
int ParallelWorkerNumber
Definition: parallel.c:110
int jitFlags
Definition: plannodes.h:62
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:56
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:59
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:402
#define PARALLEL_KEY_JIT_INSTRUMENTATION
Definition: execParallel.c:64
const char * debug_query_string
Definition: postgres.c:87
void InstrStartParallelQuery(void)
Definition: instrument.c:167
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:126
#define Assert(condition)
Definition: c.h:732
const char * sourceText
Definition: execdesc.h:38
#define DsaPointerIsValid(x)
Definition: dsa.h:81
#define PARALLEL_KEY_DSA
Definition: execParallel.c:62
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
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