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 563 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().

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

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

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

1129 {
1130  /* Accumulate instrumentation, if any. */
1131  if (pei->instrumentation)
1133  pei->instrumentation);
1134 
1135  /* Accumulate JIT instrumentation, if any. */
1136  if (pei->jit_instrumentation)
1138  pei->jit_instrumentation);
1139 
1140  /* Free any serialized parameters. */
1141  if (DsaPointerIsValid(pei->param_exec))
1142  {
1143  dsa_free(pei->area, pei->param_exec);
1145  }
1146  if (pei->area != NULL)
1147  {
1148  dsa_detach(pei->area);
1149  pei->area = NULL;
1150  }
1151  if (pei->pcxt != NULL)
1152  {
1154  pei->pcxt = NULL;
1155  }
1156  pfree(pei);
1157 }
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:892
void pfree(void *pointer)
Definition: mcxt.c:1056
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:975
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 833 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().

834 {
835  int nworkers = pei->pcxt->nworkers_launched;
836  int i;
837 
838  Assert(pei->reader == NULL);
839 
840  if (nworkers > 0)
841  {
842  pei->reader = (TupleQueueReader **)
843  palloc(nworkers * sizeof(TupleQueueReader *));
844 
845  for (i = 0; i < nworkers; i++)
846  {
847  shm_mq_set_handle(pei->tqueue[i],
848  pei->pcxt->worker[i].bgwhandle);
849  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
850  }
851  }
852 }
ParallelContext * pcxt
Definition: execParallel.h:27
ParallelWorkerInfo * worker
Definition: parallel.h:46
struct TupleQueueReader ** reader
Definition: execParallel.h:36
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
int nworkers_launched
Definition: parallel.h:38
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:728
void * palloc(Size size)
Definition: mcxt.c:949
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:35

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

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

1076 {
1077  int nworkers = pei->pcxt->nworkers_launched;
1078  int i;
1079 
1080  /* Make this be a no-op if called twice in a row. */
1081  if (pei->finished)
1082  return;
1083 
1084  /*
1085  * Detach from tuple queues ASAP, so that any still-active workers will
1086  * notice that no further results are wanted.
1087  */
1088  if (pei->tqueue != NULL)
1089  {
1090  for (i = 0; i < nworkers; i++)
1091  shm_mq_detach(pei->tqueue[i]);
1092  pfree(pei->tqueue);
1093  pei->tqueue = NULL;
1094  }
1095 
1096  /*
1097  * While we're waiting for the workers to finish, let's get rid of the
1098  * tuple queue readers. (Any other local cleanup could be done here too.)
1099  */
1100  if (pei->reader != NULL)
1101  {
1102  for (i = 0; i < nworkers; i++)
1104  pfree(pei->reader);
1105  pei->reader = NULL;
1106  }
1107 
1108  /* Now wait for the workers to finish. */
1110 
1111  /*
1112  * Next, accumulate buffer usage. (This must wait for the workers to
1113  * finish, or we might get incomplete data.)
1114  */
1115  for (i = 0; i < nworkers; i++)
1117 
1118  pei->finished = true;
1119 }
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:738
void pfree(void *pointer)
Definition: mcxt.c:1056
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:38
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:35

◆ ExecParallelReinitialize()

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

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

862 {
863  EState *estate = planstate->state;
865 
866  /* Old workers must already be shut down */
867  Assert(pei->finished);
868 
869  /*
870  * Force any initplan outputs that we're going to pass to workers to be
871  * evaluated, if they weren't already (see comments in
872  * ExecInitParallelPlan).
873  */
874  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
875 
877  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
878  pei->reader = NULL;
879  pei->finished = false;
880 
881  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
882 
883  /* Free any serialized parameters from the last round. */
884  if (DsaPointerIsValid(fpes->param_exec))
885  {
886  dsa_free(pei->area, fpes->param_exec);
888  }
889 
890  /* Serialize current parameter values if required. */
891  if (!bms_is_empty(sendParams))
892  {
893  pei->param_exec = SerializeParamExecParams(estate, sendParams,
894  pei->area);
895  fpes->param_exec = pei->param_exec;
896  }
897 
898  /* Traverse plan tree and let each child node reset associated state. */
899  estate->es_query_dsa = pei->area;
900  ExecParallelReInitializeDSM(planstate, pei->pcxt);
901  estate->es_query_dsa = NULL;
902 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:587
void ExecSetParamPlanMulti(const Bitmapset *params, ExprContext *econtext)
Definition: nodeSubplan.c:1245
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:511
EState * state
Definition: execnodes.h:945
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:908
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:341
#define GetPerTupleExprContext(estate)
Definition: executor.h:506
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:451
#define Assert(condition)
Definition: c.h:728
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:45

◆ ParallelQueryMain()

void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)

Definition at line 1332 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.

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