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

Typedef Documentation

◆ ParallelExecutorInfo

◆ SharedExecutorInstrumentation

Function Documentation

◆ ExecInitParallelPlan()

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

Definition at line 590 of file execParallel.c.

593 {
595  ParallelContext *pcxt;
599  char *pstmt_data;
600  char *pstmt_space;
601  char *paramlistinfo_space;
602  BufferUsage *bufusage_space;
603  WalUsage *walusage_space;
604  SharedExecutorInstrumentation *instrumentation = NULL;
605  SharedJitInstrumentation *jit_instrumentation = NULL;
606  int pstmt_len;
607  int paramlistinfo_len;
608  int instrumentation_len = 0;
609  int jit_instrumentation_len = 0;
610  int instrument_offset = 0;
611  Size dsa_minsize = dsa_minimum_size();
612  char *query_string;
613  int query_len;
614 
615  /*
616  * Force any initplan outputs that we're going to pass to workers to be
617  * evaluated, if they weren't already.
618  *
619  * For simplicity, we use the EState's per-output-tuple ExprContext here.
620  * That risks intra-query memory leakage, since we might pass through here
621  * many times before that ExprContext gets reset; but ExecSetParamPlan
622  * doesn't normally leak any memory in the context (see its comments), so
623  * it doesn't seem worth complicating this function's API to pass it a
624  * shorter-lived ExprContext. This might need to change someday.
625  */
626  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
627 
628  /* Allocate object for return value. */
629  pei = palloc0(sizeof(ParallelExecutorInfo));
630  pei->finished = false;
631  pei->planstate = planstate;
632 
633  /* Fix up and serialize plan to be sent to workers. */
634  pstmt_data = ExecSerializePlan(planstate->plan, estate);
635 
636  /* Create a parallel context. */
637  pcxt = CreateParallelContext("postgres", "ParallelQueryMain", nworkers);
638  pei->pcxt = pcxt;
639 
640  /*
641  * Before telling the parallel context to create a dynamic shared memory
642  * segment, we need to figure out how big it should be. Estimate space
643  * for the various things we need to store.
644  */
645 
646  /* Estimate space for fixed-size state. */
649  shm_toc_estimate_keys(&pcxt->estimator, 1);
650 
651  /* Estimate space for query text. */
652  query_len = strlen(estate->es_sourceText);
653  shm_toc_estimate_chunk(&pcxt->estimator, query_len + 1);
654  shm_toc_estimate_keys(&pcxt->estimator, 1);
655 
656  /* Estimate space for serialized PlannedStmt. */
657  pstmt_len = strlen(pstmt_data) + 1;
658  shm_toc_estimate_chunk(&pcxt->estimator, pstmt_len);
659  shm_toc_estimate_keys(&pcxt->estimator, 1);
660 
661  /* Estimate space for serialized ParamListInfo. */
662  paramlistinfo_len = EstimateParamListSpace(estate->es_param_list_info);
663  shm_toc_estimate_chunk(&pcxt->estimator, paramlistinfo_len);
664  shm_toc_estimate_keys(&pcxt->estimator, 1);
665 
666  /*
667  * Estimate space for BufferUsage.
668  *
669  * If EXPLAIN is not in use and there are no extensions loaded that care,
670  * we could skip this. But we have no way of knowing whether anyone's
671  * looking at pgBufferUsage, so do it unconditionally.
672  */
674  mul_size(sizeof(BufferUsage), pcxt->nworkers));
675  shm_toc_estimate_keys(&pcxt->estimator, 1);
676 
677  /*
678  * Same thing for WalUsage.
679  */
681  mul_size(sizeof(WalUsage), pcxt->nworkers));
682  shm_toc_estimate_keys(&pcxt->estimator, 1);
683 
684  /* Estimate space for tuple queues. */
687  shm_toc_estimate_keys(&pcxt->estimator, 1);
688 
689  /*
690  * Give parallel-aware nodes a chance to add to the estimates, and get a
691  * count of how many PlanState nodes there are.
692  */
693  e.pcxt = pcxt;
694  e.nnodes = 0;
695  ExecParallelEstimate(planstate, &e);
696 
697  /* Estimate space for instrumentation, if required. */
698  if (estate->es_instrument)
699  {
700  instrumentation_len =
701  offsetof(SharedExecutorInstrumentation, plan_node_id) +
702  sizeof(int) * e.nnodes;
703  instrumentation_len = MAXALIGN(instrumentation_len);
704  instrument_offset = instrumentation_len;
705  instrumentation_len +=
706  mul_size(sizeof(Instrumentation),
707  mul_size(e.nnodes, nworkers));
708  shm_toc_estimate_chunk(&pcxt->estimator, instrumentation_len);
709  shm_toc_estimate_keys(&pcxt->estimator, 1);
710 
711  /* Estimate space for JIT instrumentation, if required. */
712  if (estate->es_jit_flags != PGJIT_NONE)
713  {
714  jit_instrumentation_len =
715  offsetof(SharedJitInstrumentation, jit_instr) +
716  sizeof(JitInstrumentation) * nworkers;
717  shm_toc_estimate_chunk(&pcxt->estimator, jit_instrumentation_len);
718  shm_toc_estimate_keys(&pcxt->estimator, 1);
719  }
720  }
721 
722  /* Estimate space for DSA area. */
723  shm_toc_estimate_chunk(&pcxt->estimator, dsa_minsize);
724  shm_toc_estimate_keys(&pcxt->estimator, 1);
725 
726  /* Everyone's had a chance to ask for space, so now create the DSM. */
727  InitializeParallelDSM(pcxt);
728 
729  /*
730  * OK, now we have a dynamic shared memory segment, and it should be big
731  * enough to store all of the data we estimated we would want to put into
732  * it, plus whatever general stuff (not specifically executor-related) the
733  * ParallelContext itself needs to store there. None of the space we
734  * asked for has been allocated or initialized yet, though, so do that.
735  */
736 
737  /* Store fixed-size state. */
738  fpes = shm_toc_allocate(pcxt->toc, sizeof(FixedParallelExecutorState));
739  fpes->tuples_needed = tuples_needed;
741  fpes->eflags = estate->es_top_eflags;
742  fpes->jit_flags = estate->es_jit_flags;
744 
745  /* Store query string */
746  query_string = shm_toc_allocate(pcxt->toc, query_len + 1);
747  memcpy(query_string, estate->es_sourceText, query_len + 1);
748  shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string);
749 
750  /* Store serialized PlannedStmt. */
751  pstmt_space = shm_toc_allocate(pcxt->toc, pstmt_len);
752  memcpy(pstmt_space, pstmt_data, pstmt_len);
753  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PLANNEDSTMT, pstmt_space);
754 
755  /* Store serialized ParamListInfo. */
756  paramlistinfo_space = shm_toc_allocate(pcxt->toc, paramlistinfo_len);
757  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PARAMLISTINFO, paramlistinfo_space);
758  SerializeParamList(estate->es_param_list_info, &paramlistinfo_space);
759 
760  /* Allocate space for each worker's BufferUsage; no need to initialize. */
761  bufusage_space = shm_toc_allocate(pcxt->toc,
762  mul_size(sizeof(BufferUsage), pcxt->nworkers));
763  shm_toc_insert(pcxt->toc, PARALLEL_KEY_BUFFER_USAGE, bufusage_space);
764  pei->buffer_usage = bufusage_space;
765 
766  /* Same for WalUsage. */
767  walusage_space = shm_toc_allocate(pcxt->toc,
768  mul_size(sizeof(WalUsage), pcxt->nworkers));
769  shm_toc_insert(pcxt->toc, PARALLEL_KEY_WAL_USAGE, walusage_space);
770  pei->wal_usage = walusage_space;
771 
772  /* Set up the tuple queues that the workers will write into. */
773  pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
774 
775  /* We don't need the TupleQueueReaders yet, though. */
776  pei->reader = NULL;
777 
778  /*
779  * If instrumentation options were supplied, allocate space for the data.
780  * It only gets partially initialized here; the rest happens during
781  * ExecParallelInitializeDSM.
782  */
783  if (estate->es_instrument)
784  {
785  Instrumentation *instrument;
786  int i;
787 
788  instrumentation = shm_toc_allocate(pcxt->toc, instrumentation_len);
789  instrumentation->instrument_options = estate->es_instrument;
790  instrumentation->instrument_offset = instrument_offset;
791  instrumentation->num_workers = nworkers;
792  instrumentation->num_plan_nodes = e.nnodes;
793  instrument = GetInstrumentationArray(instrumentation);
794  for (i = 0; i < nworkers * e.nnodes; ++i)
795  InstrInit(&instrument[i], estate->es_instrument);
797  instrumentation);
798  pei->instrumentation = instrumentation;
799 
800  if (estate->es_jit_flags != PGJIT_NONE)
801  {
802  jit_instrumentation = shm_toc_allocate(pcxt->toc,
803  jit_instrumentation_len);
804  jit_instrumentation->num_workers = nworkers;
805  memset(jit_instrumentation->jit_instr, 0,
806  sizeof(JitInstrumentation) * nworkers);
808  jit_instrumentation);
809  pei->jit_instrumentation = jit_instrumentation;
810  }
811  }
812 
813  /*
814  * Create a DSA area that can be used by the leader and all workers.
815  * (However, if we failed to create a DSM and are using private memory
816  * instead, then skip this.)
817  */
818  if (pcxt->seg != NULL)
819  {
820  char *area_space;
821 
822  area_space = shm_toc_allocate(pcxt->toc, dsa_minsize);
823  shm_toc_insert(pcxt->toc, PARALLEL_KEY_DSA, area_space);
824  pei->area = dsa_create_in_place(area_space, dsa_minsize,
826  pcxt->seg);
827 
828  /*
829  * Serialize parameters, if any, using DSA storage. We don't dare use
830  * the main parallel query DSM for this because we might relaunch
831  * workers after the values have changed (and thus the amount of
832  * storage required has changed).
833  */
834  if (!bms_is_empty(sendParams))
835  {
836  pei->param_exec = SerializeParamExecParams(estate, sendParams,
837  pei->area);
838  fpes->param_exec = pei->param_exec;
839  }
840  }
841 
842  /*
843  * Give parallel-aware nodes a chance to initialize their shared data.
844  * This also initializes the elements of instrumentation->ps_instrument,
845  * if it exists.
846  */
847  d.pcxt = pcxt;
848  d.instrumentation = instrumentation;
849  d.nnodes = 0;
850 
851  /* Install our DSA area while initializing the plan. */
852  estate->es_query_dsa = pei->area;
853  ExecParallelInitializeDSM(planstate, &d);
854  estate->es_query_dsa = NULL;
855 
856  /*
857  * Make sure that the world hasn't shifted under our feet. This could
858  * probably just be an Assert(), but let's be conservative for now.
859  */
860  if (e.nnodes != d.nnodes)
861  elog(ERROR, "inconsistent count of PlanState nodes");
862 
863  /* OK, we're ready to rock and roll. */
864  return pei;
865 }
void InitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:203
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
Definition: parallel.c:165
#define bms_is_empty(a)
Definition: bitmapset.h:105
#define MAXALIGN(LEN)
Definition: c.h:795
size_t Size
Definition: c.h:589
dsa_area * dsa_create_in_place(void *place, size_t size, int tranche_id, dsm_segment *segment)
Definition: dsa.c:481
size_t dsa_minimum_size(void)
Definition: dsa.c:1181
#define InvalidDsaPointer
Definition: dsa.h:78
#define ERROR
Definition: elog.h:39
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:62
#define PARALLEL_KEY_JIT_INSTRUMENTATION
Definition: execParallel.c:67
#define PARALLEL_KEY_PARAMLISTINFO
Definition: execParallel.c:61
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:70
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:357
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:64
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:538
#define PARALLEL_KEY_PLANNEDSTMT
Definition: execParallel.c:60
static bool ExecParallelEstimate(PlanState *planstate, ExecParallelEstimateContext *e)
Definition: execParallel.c:232
#define GetInstrumentationArray(sei)
Definition: execParallel.c:108
#define PARALLEL_KEY_DSA
Definition: execParallel.c:65
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:59
static char * ExecSerializePlan(Plan *plan, EState *estate)
Definition: execParallel.c:147
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:66
#define PARALLEL_KEY_WAL_USAGE
Definition: execParallel.c:68
static bool ExecParallelInitializeDSM(PlanState *planstate, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:441
#define GetPerTupleExprContext(estate)
Definition: executor.h:538
void InstrInit(Instrumentation *instr, int instrument_options)
Definition: instrument.c:58
int i
Definition: isn.c:73
struct JitInstrumentation JitInstrumentation
#define PGJIT_NONE
Definition: jit.h:19
@ LWTRANCHE_PARALLEL_QUERY_DSA
Definition: lwlock.h:196
void * palloc0(Size size)
Definition: mcxt.c:1241
void ExecSetParamPlanMulti(const Bitmapset *params, ExprContext *econtext)
Definition: nodeSubplan.c:1268
Size EstimateParamListSpace(ParamListInfo paramLI)
Definition: params.c:167
void SerializeParamList(ParamListInfo paramLI, char **start_address)
Definition: params.c:229
e
Definition: preproc-init.c:82
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
struct dsa_area * es_query_dsa
Definition: execnodes.h:694
int es_top_eflags
Definition: execnodes.h:666
int es_instrument
Definition: execnodes.h:667
ParamListInfo es_param_list_info
Definition: execnodes.h:654
int es_jit_flags
Definition: execnodes.h:705
const char * es_sourceText
Definition: execnodes.h:627
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:123
dsm_segment * seg
Definition: parallel.h:43
shm_toc_estimator estimator
Definition: parallel.h:42
shm_toc * toc
Definition: parallel.h:45
PlanState * planstate
Definition: execParallel.h:26
struct SharedJitInstrumentation * jit_instrumentation
Definition: execParallel.h:31
BufferUsage * buffer_usage
Definition: execParallel.h:28
dsa_pointer param_exec
Definition: execParallel.h:33
ParallelContext * pcxt
Definition: execParallel.h:27
WalUsage * wal_usage
Definition: execParallel.h:29
shm_mq_handle ** tqueue
Definition: execParallel.h:36
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:30
struct TupleQueueReader ** reader
Definition: execParallel.h:37
Plan * plan
Definition: execnodes.h:1035
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER]
Definition: jit.h:51

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, ExecParallelInitializeDSMContext::instrumentation, ParallelExecutorInfo::instrumentation, InvalidDsaPointer, FixedParallelExecutorState::jit_flags, SharedJitInstrumentation::jit_instr, ParallelExecutorInfo::jit_instrumentation, LWTRANCHE_PARALLEL_QUERY_DSA, MAXALIGN, mul_size(), ExecParallelInitializeDSMContext::nnodes, SharedExecutorInstrumentation::num_plan_nodes, SharedExecutorInstrumentation::num_workers, SharedJitInstrumentation::num_workers, ParallelContext::nworkers, 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_KEY_WAL_USAGE, PARALLEL_TUPLE_QUEUE_SIZE, FixedParallelExecutorState::param_exec, ParallelExecutorInfo::param_exec, ExecParallelInitializeDSMContext::pcxt, ParallelExecutorInfo::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, FixedParallelExecutorState::tuples_needed, and ParallelExecutorInfo::wal_usage.

Referenced by ExecGather(), and ExecGatherMerge().

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

Definition at line 1180 of file execParallel.c.

1181 {
1182  /* Accumulate instrumentation, if any. */
1183  if (pei->instrumentation)
1185  pei->instrumentation);
1186 
1187  /* Accumulate JIT instrumentation, if any. */
1188  if (pei->jit_instrumentation)
1190  pei->jit_instrumentation);
1191 
1192  /* Free any serialized parameters. */
1193  if (DsaPointerIsValid(pei->param_exec))
1194  {
1195  dsa_free(pei->area, pei->param_exec);
1197  }
1198  if (pei->area != NULL)
1199  {
1200  dsa_detach(pei->area);
1201  pei->area = NULL;
1202  }
1203  if (pei->pcxt != NULL)
1204  {
1206  pei->pcxt = NULL;
1207  }
1208  pfree(pei);
1209 }
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:927
void dsa_detach(dsa_area *area)
Definition: dsa.c:1922
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:832
#define DsaPointerIsValid(x)
Definition: dsa.h:81
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
static void ExecParallelRetrieveJitInstrumentation(PlanState *planstate, SharedJitInstrumentation *shared_jit)
void pfree(void *pointer)
Definition: mcxt.c:1436

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

◆ ExecParallelCreateReaders()

void ExecParallelCreateReaders ( ParallelExecutorInfo pei)

Definition at line 874 of file execParallel.c.

875 {
876  int nworkers = pei->pcxt->nworkers_launched;
877  int i;
878 
879  Assert(pei->reader == NULL);
880 
881  if (nworkers > 0)
882  {
883  pei->reader = (TupleQueueReader **)
884  palloc(nworkers * sizeof(TupleQueueReader *));
885 
886  for (i = 0; i < nworkers; i++)
887  {
888  shm_mq_set_handle(pei->tqueue[i],
889  pei->pcxt->worker[i].bgwhandle);
890  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
891  }
892  }
893 }
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc(Size size)
Definition: mcxt.c:1210
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:320
ParallelWorkerInfo * worker
Definition: parallel.h:46
int nworkers_launched
Definition: parallel.h:38
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
TupleQueueReader * CreateTupleQueueReader(shm_mq_handle *handle)
Definition: tqueue.c:139

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

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

Definition at line 1127 of file execParallel.c.

1128 {
1129  int nworkers = pei->pcxt->nworkers_launched;
1130  int i;
1131 
1132  /* Make this be a no-op if called twice in a row. */
1133  if (pei->finished)
1134  return;
1135 
1136  /*
1137  * Detach from tuple queues ASAP, so that any still-active workers will
1138  * notice that no further results are wanted.
1139  */
1140  if (pei->tqueue != NULL)
1141  {
1142  for (i = 0; i < nworkers; i++)
1143  shm_mq_detach(pei->tqueue[i]);
1144  pfree(pei->tqueue);
1145  pei->tqueue = NULL;
1146  }
1147 
1148  /*
1149  * While we're waiting for the workers to finish, let's get rid of the
1150  * tuple queue readers. (Any other local cleanup could be done here too.)
1151  */
1152  if (pei->reader != NULL)
1153  {
1154  for (i = 0; i < nworkers; i++)
1156  pfree(pei->reader);
1157  pei->reader = NULL;
1158  }
1159 
1160  /* Now wait for the workers to finish. */
1162 
1163  /*
1164  * Next, accumulate buffer/WAL usage. (This must wait for the workers to
1165  * finish, or we might get incomplete data.)
1166  */
1167  for (i = 0; i < nworkers; i++)
1169 
1170  pei->finished = true;
1171 }
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:773
void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:218
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:844
void DestroyTupleQueueReader(TupleQueueReader *reader)
Definition: tqueue.c:155

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

Referenced by ExecShutdownGatherMergeWorkers(), and ExecShutdownGatherWorkers().

◆ ExecParallelReinitialize()

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

Definition at line 900 of file execParallel.c.

903 {
904  EState *estate = planstate->state;
906 
907  /* Old workers must already be shut down */
908  Assert(pei->finished);
909 
910  /*
911  * Force any initplan outputs that we're going to pass to workers to be
912  * evaluated, if they weren't already (see comments in
913  * ExecInitParallelPlan).
914  */
915  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
916 
918  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
919  pei->reader = NULL;
920  pei->finished = false;
921 
922  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
923 
924  /* Free any serialized parameters from the last round. */
925  if (DsaPointerIsValid(fpes->param_exec))
926  {
927  dsa_free(pei->area, fpes->param_exec);
929  }
930 
931  /* Serialize current parameter values if required. */
932  if (!bms_is_empty(sendParams))
933  {
934  pei->param_exec = SerializeParamExecParams(estate, sendParams,
935  pei->area);
936  fpes->param_exec = pei->param_exec;
937  }
938 
939  /* Traverse plan tree and let each child node reset associated state. */
940  estate->es_query_dsa = pei->area;
941  ExecParallelReInitializeDSM(planstate, pei->pcxt);
942  estate->es_query_dsa = NULL;
943 }
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:486
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:949
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
EState * state
Definition: execnodes.h:1037

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, FixedParallelExecutorState::param_exec, ParallelExecutorInfo::param_exec, ParallelExecutorInfo::pcxt, ParallelExecutorInfo::reader, ReinitializeParallelDSM(), SerializeParamExecParams(), shm_toc_lookup(), PlanState::state, ParallelContext::toc, and ParallelExecutorInfo::tqueue.

Referenced by ExecGather(), and ExecGatherMerge().

◆ ParallelQueryMain()

void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)

Definition at line 1396 of file execParallel.c.

1397 {
1399  BufferUsage *buffer_usage;
1400  WalUsage *wal_usage;
1401  DestReceiver *receiver;
1402  QueryDesc *queryDesc;
1403  SharedExecutorInstrumentation *instrumentation;
1404  SharedJitInstrumentation *jit_instrumentation;
1405  int instrument_options = 0;
1406  void *area_space;
1407  dsa_area *area;
1408  ParallelWorkerContext pwcxt;
1409 
1410  /* Get fixed-size state. */
1411  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1412 
1413  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1414  receiver = ExecParallelGetReceiver(seg, toc);
1415  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1416  if (instrumentation != NULL)
1417  instrument_options = instrumentation->instrument_options;
1418  jit_instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
1419  true);
1420  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1421 
1422  /* Setting debug_query_string for individual workers */
1423  debug_query_string = queryDesc->sourceText;
1424 
1425  /* Report workers' query for monitoring purposes */
1427 
1428  /* Attach to the dynamic shared memory area. */
1429  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1430  area = dsa_attach_in_place(area_space, seg);
1431 
1432  /* Start up the executor */
1433  queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1434  ExecutorStart(queryDesc, fpes->eflags);
1435 
1436  /* Special executor initialization steps for parallel workers */
1437  queryDesc->planstate->state->es_query_dsa = area;
1438  if (DsaPointerIsValid(fpes->param_exec))
1439  {
1440  char *paramexec_space;
1441 
1442  paramexec_space = dsa_get_address(area, fpes->param_exec);
1443  RestoreParamExecParams(paramexec_space, queryDesc->estate);
1444  }
1445  pwcxt.toc = toc;
1446  pwcxt.seg = seg;
1447  ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1448 
1449  /* Pass down any tuple bound */
1450  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1451 
1452  /*
1453  * Prepare to track buffer/WAL usage during query execution.
1454  *
1455  * We do this after starting up the executor to match what happens in the
1456  * leader, which also doesn't count buffer accesses and WAL activity that
1457  * occur during executor startup.
1458  */
1460 
1461  /*
1462  * Run the plan. If we specified a tuple bound, be careful not to demand
1463  * more tuples than that.
1464  */
1465  ExecutorRun(queryDesc,
1467  fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
1468  true);
1469 
1470  /* Shut down the executor */
1471  ExecutorFinish(queryDesc);
1472 
1473  /* Report buffer/WAL usage during parallel execution. */
1474  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1475  wal_usage = shm_toc_lookup(toc, PARALLEL_KEY_WAL_USAGE, false);
1477  &wal_usage[ParallelWorkerNumber]);
1478 
1479  /* Report instrumentation data if any instrumentation options are set. */
1480  if (instrumentation != NULL)
1482  instrumentation);
1483 
1484  /* Report JIT instrumentation data if any */
1485  if (queryDesc->estate->es_jit && jit_instrumentation != NULL)
1486  {
1487  Assert(ParallelWorkerNumber < jit_instrumentation->num_workers);
1488  jit_instrumentation->jit_instr[ParallelWorkerNumber] =
1489  queryDesc->estate->es_jit->instr;
1490  }
1491 
1492  /* Must do this after capturing instrumentation. */
1493  ExecutorEnd(queryDesc);
1494 
1495  /* Cleanup. */
1496  dsa_detach(area);
1497  FreeQueryDesc(queryDesc);
1498  receiver->rDestroy(receiver);
1499 }
int ParallelWorkerNumber
Definition: parallel.c:113
void pgstat_report_activity(BackendState state, const char *cmd_str)
@ STATE_RUNNING
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:944
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:553
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:462
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:402
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:132
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:301
static DestReceiver * ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
static QueryDesc * ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver, int instrument_options)
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
static void RestoreParamExecParams(char *start_address, EState *estate)
Definition: execParallel.c:412
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:849
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:208
void InstrStartParallelQuery(void)
Definition: instrument.c:200
const char * debug_query_string
Definition: postgres.c:81
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
@ ForwardScanDirection
Definition: sdir.h:28
struct JitContext * es_jit
Definition: execnodes.h:706
JitInstrumentation instr
Definition: jit.h:61
dsm_segment * seg
Definition: parallel.h:53
int jitFlags
Definition: plannodes.h:69
const char * sourceText
Definition: execdesc.h:38
EState * estate
Definition: execdesc.h:48
PlannedStmt * plannedstmt
Definition: execdesc.h:37
PlanState * planstate
Definition: execdesc.h:49
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:125
Definition: dsa.c:367

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, PARALLEL_KEY_WAL_USAGE, 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.