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

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

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

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

1179 {
1180  /* Accumulate instrumentation, if any. */
1181  if (pei->instrumentation)
1183  pei->instrumentation);
1184 
1185  /* Accumulate JIT instrumentation, if any. */
1186  if (pei->jit_instrumentation)
1188  pei->jit_instrumentation);
1189 
1190  /* Free any serialized parameters. */
1191  if (DsaPointerIsValid(pei->param_exec))
1192  {
1193  dsa_free(pei->area, pei->param_exec);
1195  }
1196  if (pei->area != NULL)
1197  {
1198  dsa_detach(pei->area);
1199  pei->area = NULL;
1200  }
1201  if (pei->pcxt != NULL)
1202  {
1204  pei->pcxt = NULL;
1205  }
1206  pfree(pei);
1207 }
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:905
void pfree(void *pointer)
Definition: mcxt.c:1169
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 872 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().

873 {
874  int nworkers = pei->pcxt->nworkers_launched;
875  int i;
876 
877  Assert(pei->reader == NULL);
878 
879  if (nworkers > 0)
880  {
881  pei->reader = (TupleQueueReader **)
882  palloc(nworkers * sizeof(TupleQueueReader *));
883 
884  for (i = 0; i < nworkers; i++)
885  {
886  shm_mq_set_handle(pei->tqueue[i],
887  pei->pcxt->worker[i].bgwhandle);
888  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
889  }
890  }
891 }
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:804
void * palloc(Size size)
Definition: mcxt.c:1062
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:36

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

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

1126 {
1127  int nworkers = pei->pcxt->nworkers_launched;
1128  int i;
1129 
1130  /* Make this be a no-op if called twice in a row. */
1131  if (pei->finished)
1132  return;
1133 
1134  /*
1135  * Detach from tuple queues ASAP, so that any still-active workers will
1136  * notice that no further results are wanted.
1137  */
1138  if (pei->tqueue != NULL)
1139  {
1140  for (i = 0; i < nworkers; i++)
1141  shm_mq_detach(pei->tqueue[i]);
1142  pfree(pei->tqueue);
1143  pei->tqueue = NULL;
1144  }
1145 
1146  /*
1147  * While we're waiting for the workers to finish, let's get rid of the
1148  * tuple queue readers. (Any other local cleanup could be done here too.)
1149  */
1150  if (pei->reader != NULL)
1151  {
1152  for (i = 0; i < nworkers; i++)
1154  pfree(pei->reader);
1155  pei->reader = NULL;
1156  }
1157 
1158  /* Now wait for the workers to finish. */
1160 
1161  /*
1162  * Next, accumulate buffer/WAL usage. (This must wait for the workers to
1163  * finish, or we might get incomplete data.)
1164  */
1165  for (i = 0; i < nworkers; i++)
1166  InstrAccumParallelQuery(&pei->buffer_usage[i], &pei->wal_usage[i]);
1167 
1168  pei->finished = true;
1169 }
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:751
void pfree(void *pointer)
Definition: mcxt.c:1169
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:218
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:36

◆ ExecParallelReinitialize()

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

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

901 {
902  EState *estate = planstate->state;
904 
905  /* Old workers must already be shut down */
906  Assert(pei->finished);
907 
908  /*
909  * Force any initplan outputs that we're going to pass to workers to be
910  * evaluated, if they weren't already (see comments in
911  * ExecInitParallelPlan).
912  */
913  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
914 
916  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
917  pei->reader = NULL;
918  pei->finished = false;
919 
920  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
921 
922  /* Free any serialized parameters from the last round. */
923  if (DsaPointerIsValid(fpes->param_exec))
924  {
925  dsa_free(pei->area, fpes->param_exec);
927  }
928 
929  /* Serialize current parameter values if required. */
930  if (!bms_is_empty(sendParams))
931  {
932  pei->param_exec = SerializeParamExecParams(estate, sendParams,
933  pei->area);
934  fpes->param_exec = pei->param_exec;
935  }
936 
937  /* Traverse plan tree and let each child node reset associated state. */
938  estate->es_query_dsa = pei->area;
939  ExecParallelReInitializeDSM(planstate, pei->pcxt);
940  estate->es_query_dsa = NULL;
941 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:633
void ExecSetParamPlanMulti(const Bitmapset *params, ExprContext *econtext)
Definition: nodeSubplan.c:1253
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:536
EState * state
Definition: execnodes.h:967
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:947
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:355
#define GetPerTupleExprContext(estate)
Definition: executor.h:533
struct TupleQueueReader ** reader
Definition: execParallel.h:37
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:59
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:464
#define Assert(condition)
Definition: c.h:804
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 1395 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.

1396 {
1398  BufferUsage *buffer_usage;
1399  WalUsage *wal_usage;
1400  DestReceiver *receiver;
1401  QueryDesc *queryDesc;
1402  SharedExecutorInstrumentation *instrumentation;
1403  SharedJitInstrumentation *jit_instrumentation;
1404  int instrument_options = 0;
1405  void *area_space;
1406  dsa_area *area;
1407  ParallelWorkerContext pwcxt;
1408 
1409  /* Get fixed-size state. */
1410  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1411 
1412  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1413  receiver = ExecParallelGetReceiver(seg, toc);
1414  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1415  if (instrumentation != NULL)
1416  instrument_options = instrumentation->instrument_options;
1417  jit_instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
1418  true);
1419  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1420 
1421  /* Setting debug_query_string for individual workers */
1422  debug_query_string = queryDesc->sourceText;
1423 
1424  /* Report workers' query and queryId for monitoring purposes */
1426 
1427  /* Attach to the dynamic shared memory area. */
1428  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1429  area = dsa_attach_in_place(area_space, seg);
1430 
1431  /* Start up the executor */
1432  queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1433  ExecutorStart(queryDesc, fpes->eflags);
1434 
1435  /* Special executor initialization steps for parallel workers */
1436  queryDesc->planstate->state->es_query_dsa = area;
1437  if (DsaPointerIsValid(fpes->param_exec))
1438  {
1439  char *paramexec_space;
1440 
1441  paramexec_space = dsa_get_address(area, fpes->param_exec);
1442  RestoreParamExecParams(paramexec_space, queryDesc->estate);
1443 
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 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:633
EState * estate
Definition: execdesc.h:48
struct JitContext * es_jit
Definition: execnodes.h:645
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:130
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:541
EState * state
Definition: execnodes.h:967
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:208
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER]
Definition: jit.h:51
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:842
JitInstrumentation instr
Definition: jit.h:61
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:459
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:64
PlanState * planstate
Definition: execdesc.h:49
#define PARALLEL_KEY_WAL_USAGE
Definition: execParallel.c:68
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:298
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:59
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:62
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:399
#define PARALLEL_KEY_JIT_INSTRUMENTATION
Definition: execParallel.c:67
const char * debug_query_string
Definition: postgres.c:89
void InstrStartParallelQuery(void)
Definition: instrument.c:200
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:127
#define Assert(condition)
Definition: c.h:804
const char * sourceText
Definition: execdesc.h:38
#define DsaPointerIsValid(x)
Definition: dsa.h:81
#define PARALLEL_KEY_DSA
Definition: execParallel.c:65
Definition: dsa.c:354
void pgstat_report_activity(BackendState state, const char *cmd_str)
static DestReceiver * ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
static void RestoreParamExecParams(char *start_address, EState *estate)
Definition: execParallel.c:410
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