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 579 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_KEY_WAL_USAGE, 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, FixedParallelExecutorState::tuples_needed, and ParallelExecutorInfo::wal_usage.

Referenced by ExecGather(), and ExecGatherMerge().

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

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

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

1166 {
1167  /* Accumulate instrumentation, if any. */
1168  if (pei->instrumentation)
1170  pei->instrumentation);
1171 
1172  /* Accumulate JIT instrumentation, if any. */
1173  if (pei->jit_instrumentation)
1175  pei->jit_instrumentation);
1176 
1177  /* Free any serialized parameters. */
1178  if (DsaPointerIsValid(pei->param_exec))
1179  {
1180  dsa_free(pei->area, pei->param_exec);
1182  }
1183  if (pei->area != NULL)
1184  {
1185  dsa_detach(pei->area);
1186  pei->area = NULL;
1187  }
1188  if (pei->pcxt != NULL)
1189  {
1191  pei->pcxt = NULL;
1192  }
1193  pfree(pei);
1194 }
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
struct SharedJitInstrumentation * jit_instrumentation
Definition: execParallel.h:31
PlanState * planstate
Definition: execParallel.h:26
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:904
void pfree(void *pointer)
Definition: mcxt.c:1057
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
void dsa_detach(dsa_area *area)
Definition: dsa.c:1910
static void ExecParallelRetrieveJitInstrumentation(PlanState *planstate, SharedJitInstrumentation *shared_jit)
dsa_pointer param_exec
Definition: execParallel.h:33
#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:30

◆ ExecParallelCreateReaders()

void ExecParallelCreateReaders ( ParallelExecutorInfo pei)

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

864 {
865  int nworkers = pei->pcxt->nworkers_launched;
866  int i;
867 
868  Assert(pei->reader == NULL);
869 
870  if (nworkers > 0)
871  {
872  pei->reader = (TupleQueueReader **)
873  palloc(nworkers * sizeof(TupleQueueReader *));
874 
875  for (i = 0; i < nworkers; i++)
876  {
877  shm_mq_set_handle(pei->tqueue[i],
878  pei->pcxt->worker[i].bgwhandle);
879  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
880  }
881  }
882 }
ParallelContext * pcxt
Definition: execParallel.h:27
ParallelWorkerInfo * worker
Definition: parallel.h:46
struct TupleQueueReader ** reader
Definition: execParallel.h:37
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:311
#define Assert(condition)
Definition: c.h:746
void * palloc(Size size)
Definition: mcxt.c:950
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:36

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

Definition at line 1112 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, WaitForParallelWorkersToFinish(), and ParallelExecutorInfo::wal_usage.

Referenced by ExecShutdownGatherMergeWorkers(), and ExecShutdownGatherWorkers().

1113 {
1114  int nworkers = pei->pcxt->nworkers_launched;
1115  int i;
1116 
1117  /* Make this be a no-op if called twice in a row. */
1118  if (pei->finished)
1119  return;
1120 
1121  /*
1122  * Detach from tuple queues ASAP, so that any still-active workers will
1123  * notice that no further results are wanted.
1124  */
1125  if (pei->tqueue != NULL)
1126  {
1127  for (i = 0; i < nworkers; i++)
1128  shm_mq_detach(pei->tqueue[i]);
1129  pfree(pei->tqueue);
1130  pei->tqueue = NULL;
1131  }
1132 
1133  /*
1134  * While we're waiting for the workers to finish, let's get rid of the
1135  * tuple queue readers. (Any other local cleanup could be done here too.)
1136  */
1137  if (pei->reader != NULL)
1138  {
1139  for (i = 0; i < nworkers; i++)
1141  pfree(pei->reader);
1142  pei->reader = NULL;
1143  }
1144 
1145  /* Now wait for the workers to finish. */
1147 
1148  /*
1149  * Next, accumulate buffer/WAL usage. (This must wait for the workers to
1150  * finish, or we might get incomplete data.)
1151  */
1152  for (i = 0; i < nworkers; i++)
1153  InstrAccumParallelQuery(&pei->buffer_usage[i], &pei->wal_usage[i]);
1154 
1155  pei->finished = true;
1156 }
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:817
WalUsage * wal_usage
Definition: execParallel.h:29
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:750
void pfree(void *pointer)
Definition: mcxt.c:1057
BufferUsage * buffer_usage
Definition: execParallel.h:28
struct TupleQueueReader ** reader
Definition: execParallel.h:37
int nworkers_launched
Definition: parallel.h:38
void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:199
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:36

◆ ExecParallelReinitialize()

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

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

892 {
893  EState *estate = planstate->state;
895 
896  /* Old workers must already be shut down */
897  Assert(pei->finished);
898 
899  /*
900  * Force any initplan outputs that we're going to pass to workers to be
901  * evaluated, if they weren't already (see comments in
902  * ExecInitParallelPlan).
903  */
904  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
905 
907  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
908  pei->reader = NULL;
909  pei->finished = false;
910 
911  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
912 
913  /* Free any serialized parameters from the last round. */
914  if (DsaPointerIsValid(fpes->param_exec))
915  {
916  dsa_free(pei->area, fpes->param_exec);
918  }
919 
920  /* Serialize current parameter values if required. */
921  if (!bms_is_empty(sendParams))
922  {
923  pei->param_exec = SerializeParamExecParams(estate, sendParams,
924  pei->area);
925  fpes->param_exec = pei->param_exec;
926  }
927 
928  /* Traverse plan tree and let each child node reset associated state. */
929  estate->es_query_dsa = pei->area;
930  ExecParallelReInitializeDSM(planstate, pei->pcxt);
931  estate->es_query_dsa = NULL;
932 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:593
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:527
EState * state
Definition: execnodes.h:941
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:938
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:350
#define GetPerTupleExprContext(estate)
Definition: executor.h:506
struct TupleQueueReader ** reader
Definition: execParallel.h:37
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:58
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:463
#define Assert(condition)
Definition: c.h:746
dsa_pointer param_exec
Definition: execParallel.h:33
#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:36
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 1377 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, 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.

1378 {
1380  BufferUsage *buffer_usage;
1381  WalUsage *wal_usage;
1382  DestReceiver *receiver;
1383  QueryDesc *queryDesc;
1384  SharedExecutorInstrumentation *instrumentation;
1385  SharedJitInstrumentation *jit_instrumentation;
1386  int instrument_options = 0;
1387  void *area_space;
1388  dsa_area *area;
1389  ParallelWorkerContext pwcxt;
1390 
1391  /* Get fixed-size state. */
1392  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1393 
1394  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1395  receiver = ExecParallelGetReceiver(seg, toc);
1396  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1397  if (instrumentation != NULL)
1398  instrument_options = instrumentation->instrument_options;
1399  jit_instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
1400  true);
1401  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1402 
1403  /* Setting debug_query_string for individual workers */
1404  debug_query_string = queryDesc->sourceText;
1405 
1406  /* Report workers' query for monitoring purposes */
1408 
1409  /* Attach to the dynamic shared memory area. */
1410  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1411  area = dsa_attach_in_place(area_space, seg);
1412 
1413  /* Start up the executor */
1414  queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1415  ExecutorStart(queryDesc, fpes->eflags);
1416 
1417  /* Special executor initialization steps for parallel workers */
1418  queryDesc->planstate->state->es_query_dsa = area;
1419  if (DsaPointerIsValid(fpes->param_exec))
1420  {
1421  char *paramexec_space;
1422 
1423  paramexec_space = dsa_get_address(area, fpes->param_exec);
1424  RestoreParamExecParams(paramexec_space, queryDesc->estate);
1425 
1426  }
1427  pwcxt.toc = toc;
1428  pwcxt.seg = seg;
1429  ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1430 
1431  /* Pass down any tuple bound */
1432  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1433 
1434  /*
1435  * Prepare to track buffer/WAL usage during query execution.
1436  *
1437  * We do this after starting up the executor to match what happens in the
1438  * leader, which also doesn't count buffer accesses and WAL activity that
1439  * occur during executor startup.
1440  */
1442 
1443  /*
1444  * Run the plan. If we specified a tuple bound, be careful not to demand
1445  * more tuples than that.
1446  */
1447  ExecutorRun(queryDesc,
1449  fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
1450  true);
1451 
1452  /* Shut down the executor */
1453  ExecutorFinish(queryDesc);
1454 
1455  /* Report buffer/WAL usage during parallel execution. */
1456  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1457  wal_usage = shm_toc_lookup(toc, PARALLEL_KEY_WAL_USAGE, false);
1459  &wal_usage[ParallelWorkerNumber]);
1460 
1461  /* Report instrumentation data if any instrumentation options are set. */
1462  if (instrumentation != NULL)
1464  instrumentation);
1465 
1466  /* Report JIT instrumentation data if any */
1467  if (queryDesc->estate->es_jit && jit_instrumentation != NULL)
1468  {
1469  Assert(ParallelWorkerNumber < jit_instrumentation->num_workers);
1470  jit_instrumentation->jit_instr[ParallelWorkerNumber] =
1471  queryDesc->estate->es_jit->instr;
1472  }
1473 
1474  /* Must do this after capturing instrumentation. */
1475  ExecutorEnd(queryDesc);
1476 
1477  /* Cleanup. */
1478  dsa_detach(area);
1479  FreeQueryDesc(queryDesc);
1480  receiver->rDestroy(receiver);
1481 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:593
EState * estate
Definition: execdesc.h:48
struct JitContext * es_jit
Definition: execnodes.h:605
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
void pgstat_report_activity(BackendState state, const char *cmd_str)
Definition: pgstat.c:3275
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
EState * state
Definition: execnodes.h:941
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:189
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER]
Definition: jit.h:51
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:821
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:63
PlanState * planstate
Definition: execdesc.h:49
#define PARALLEL_KEY_WAL_USAGE
Definition: execParallel.c:67
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:1910
int ParallelWorkerNumber
Definition: parallel.c:112
int jitFlags
Definition: plannodes.h:62
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:58
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:61
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:402
#define PARALLEL_KEY_JIT_INSTRUMENTATION
Definition: execParallel.c:66
const char * debug_query_string
Definition: postgres.c:88
void InstrStartParallelQuery(void)
Definition: instrument.c:181
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:127
#define Assert(condition)
Definition: c.h:746
const char * sourceText
Definition: execdesc.h:38
#define DsaPointerIsValid(x)
Definition: dsa.h:81
#define PARALLEL_KEY_DSA
Definition: execParallel.c:64
Definition: dsa.c:354
static DestReceiver * ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
static void RestoreParamExecParams(char *start_address, EState *estate)
Definition: execParallel.c:405
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