PostgreSQL Source Code  git master
execParallel.c File Reference
Include dependency graph for execParallel.c:

Go to the source code of this file.

Data Structures

struct  FixedParallelExecutorState
 
struct  SharedExecutorInstrumentation
 
struct  ExecParallelEstimateContext
 
struct  ExecParallelInitializeDSMContext
 

Macros

#define PARALLEL_KEY_EXECUTOR_FIXED   UINT64CONST(0xE000000000000001)
 
#define PARALLEL_KEY_PLANNEDSTMT   UINT64CONST(0xE000000000000002)
 
#define PARALLEL_KEY_PARAMLISTINFO   UINT64CONST(0xE000000000000003)
 
#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xE000000000000004)
 
#define PARALLEL_KEY_TUPLE_QUEUE   UINT64CONST(0xE000000000000005)
 
#define PARALLEL_KEY_INSTRUMENTATION   UINT64CONST(0xE000000000000006)
 
#define PARALLEL_KEY_DSA   UINT64CONST(0xE000000000000007)
 
#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xE000000000000008)
 
#define PARALLEL_KEY_JIT_INSTRUMENTATION   UINT64CONST(0xE000000000000009)
 
#define PARALLEL_KEY_WAL_USAGE   UINT64CONST(0xE00000000000000A)
 
#define PARALLEL_TUPLE_QUEUE_SIZE   65536
 
#define GetInstrumentationArray(sei)
 

Typedefs

typedef struct FixedParallelExecutorState FixedParallelExecutorState
 
typedef struct ExecParallelEstimateContext ExecParallelEstimateContext
 
typedef struct ExecParallelInitializeDSMContext ExecParallelInitializeDSMContext
 

Functions

static char * ExecSerializePlan (Plan *plan, EState *estate)
 
static bool ExecParallelEstimate (PlanState *node, ExecParallelEstimateContext *e)
 
static bool ExecParallelInitializeDSM (PlanState *node, ExecParallelInitializeDSMContext *d)
 
static shm_mq_handle ** ExecParallelSetupTupleQueues (ParallelContext *pcxt, bool reinitialize)
 
static bool ExecParallelReInitializeDSM (PlanState *planstate, ParallelContext *pcxt)
 
static bool ExecParallelRetrieveInstrumentation (PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
 
static DestReceiverExecParallelGetReceiver (dsm_segment *seg, shm_toc *toc)
 
static Size EstimateParamExecSpace (EState *estate, Bitmapset *params)
 
static dsa_pointer SerializeParamExecParams (EState *estate, Bitmapset *params, dsa_area *area)
 
static void RestoreParamExecParams (char *start_address, EState *estate)
 
ParallelExecutorInfoExecInitParallelPlan (PlanState *planstate, EState *estate, Bitmapset *sendParams, int nworkers, int64 tuples_needed)
 
void ExecParallelCreateReaders (ParallelExecutorInfo *pei)
 
void ExecParallelReinitialize (PlanState *planstate, ParallelExecutorInfo *pei, Bitmapset *sendParams)
 
static void ExecParallelRetrieveJitInstrumentation (PlanState *planstate, SharedJitInstrumentation *shared_jit)
 
void ExecParallelFinish (ParallelExecutorInfo *pei)
 
void ExecParallelCleanup (ParallelExecutorInfo *pei)
 
static QueryDescExecParallelGetQueryDesc (shm_toc *toc, DestReceiver *receiver, int instrument_options)
 
static bool ExecParallelReportInstrumentation (PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
 
static bool ExecParallelInitializeWorker (PlanState *planstate, ParallelWorkerContext *pwcxt)
 
void ParallelQueryMain (dsm_segment *seg, shm_toc *toc)
 

Macro Definition Documentation

◆ GetInstrumentationArray

#define GetInstrumentationArray (   sei)
Value:
(Instrumentation *) (((char *) sei) + sei->instrument_offset))
#define AssertVariableIsOfTypeMacro(varname, typename)
Definition: c.h:908

Definition at line 107 of file execParallel.c.

Referenced by ExecInitParallelPlan(), ExecParallelReportInstrumentation(), and ExecParallelRetrieveInstrumentation().

◆ PARALLEL_KEY_BUFFER_USAGE

#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xE000000000000004)

Definition at line 61 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

◆ PARALLEL_KEY_DSA

#define PARALLEL_KEY_DSA   UINT64CONST(0xE000000000000007)

Definition at line 64 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

◆ PARALLEL_KEY_EXECUTOR_FIXED

#define PARALLEL_KEY_EXECUTOR_FIXED   UINT64CONST(0xE000000000000001)

◆ PARALLEL_KEY_INSTRUMENTATION

#define PARALLEL_KEY_INSTRUMENTATION   UINT64CONST(0xE000000000000006)

Definition at line 63 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

◆ PARALLEL_KEY_JIT_INSTRUMENTATION

#define PARALLEL_KEY_JIT_INSTRUMENTATION   UINT64CONST(0xE000000000000009)

Definition at line 66 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

◆ PARALLEL_KEY_PARAMLISTINFO

#define PARALLEL_KEY_PARAMLISTINFO   UINT64CONST(0xE000000000000003)

Definition at line 60 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

◆ PARALLEL_KEY_PLANNEDSTMT

#define PARALLEL_KEY_PLANNEDSTMT   UINT64CONST(0xE000000000000002)

Definition at line 59 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

◆ PARALLEL_KEY_QUERY_TEXT

#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xE000000000000008)

Definition at line 65 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

◆ PARALLEL_KEY_TUPLE_QUEUE

#define PARALLEL_KEY_TUPLE_QUEUE   UINT64CONST(0xE000000000000005)

Definition at line 62 of file execParallel.c.

Referenced by ExecParallelGetReceiver(), and ExecParallelSetupTupleQueues().

◆ PARALLEL_KEY_WAL_USAGE

#define PARALLEL_KEY_WAL_USAGE   UINT64CONST(0xE00000000000000A)

Definition at line 67 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

◆ PARALLEL_TUPLE_QUEUE_SIZE

#define PARALLEL_TUPLE_QUEUE_SIZE   65536

Typedef Documentation

◆ ExecParallelEstimateContext

◆ ExecParallelInitializeDSMContext

◆ FixedParallelExecutorState

Function Documentation

◆ EstimateParamExecSpace()

static Size EstimateParamExecSpace ( EState estate,
Bitmapset params 
)
static

Definition at line 306 of file execParallel.c.

References add_size(), bms_next_member(), datumEstimateSpace(), EState::es_param_exec_vals, EState::es_plannedstmt, get_typlenbyval(), ParamExecData::isnull, list_nth_oid(), OidIsValid, PlannedStmt::paramExecTypes, and ParamExecData::value.

Referenced by SerializeParamExecParams().

307 {
308  int paramid;
309  Size sz = sizeof(int);
310 
311  paramid = -1;
312  while ((paramid = bms_next_member(params, paramid)) >= 0)
313  {
314  Oid typeOid;
315  int16 typLen;
316  bool typByVal;
317  ParamExecData *prm;
318 
319  prm = &(estate->es_param_exec_vals[paramid]);
320  typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
321  paramid);
322 
323  sz = add_size(sz, sizeof(int)); /* space for paramid */
324 
325  /* space for datum/isnull */
326  if (OidIsValid(typeOid))
327  get_typlenbyval(typeOid, &typLen, &typByVal);
328  else
329  {
330  /* If no type OID, assume by-value, like copyParamList does. */
331  typLen = sizeof(Datum);
332  typByVal = true;
333  }
334  sz = add_size(sz,
335  datumEstimateSpace(prm->value, prm->isnull,
336  typByVal, typLen));
337  }
338  return sz;
339 }
signed short int16
Definition: c.h:362
List * paramExecTypes
Definition: plannodes.h:84
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
PlannedStmt * es_plannedstmt
Definition: execnodes.h:525
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:288
ParamExecData * es_param_exec_vals
Definition: execnodes.h:554
bool isnull
Definition: params.h:150
Size datumEstimateSpace(Datum value, bool isnull, bool typByVal, int typLen)
Definition: datum.c:360
uintptr_t Datum
Definition: postgres.h:367
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
size_t Size
Definition: c.h:474
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2139
Datum value
Definition: params.h:149

◆ ExecInitParallelPlan()

ParallelExecutorInfo* ExecInitParallelPlan ( PlanState planstate,
EState estate,
Bitmapset sendParams,
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

◆ ExecParallelEstimate()

static bool ExecParallelEstimate ( PlanState node,
ExecParallelEstimateContext e 
)
static

Definition at line 229 of file execParallel.c.

References ExecAggEstimate(), ExecAppendEstimate(), ExecBitmapHeapEstimate(), ExecCustomScanEstimate(), ExecForeignScanEstimate(), ExecHashEstimate(), ExecHashJoinEstimate(), ExecIncrementalSortEstimate(), ExecIndexOnlyScanEstimate(), ExecIndexScanEstimate(), ExecSeqScanEstimate(), ExecSortEstimate(), ExecParallelEstimateContext::nnodes, nodeTag, Plan::parallel_aware, ExecParallelEstimateContext::pcxt, PlanState::plan, planstate_tree_walker(), T_AggState, T_AppendState, T_BitmapHeapScanState, T_CustomScanState, T_ForeignScanState, T_HashJoinState, T_HashState, T_IncrementalSortState, T_IndexOnlyScanState, T_IndexScanState, T_SeqScanState, and T_SortState.

Referenced by ExecInitParallelPlan().

230 {
231  if (planstate == NULL)
232  return false;
233 
234  /* Count this node. */
235  e->nnodes++;
236 
237  switch (nodeTag(planstate))
238  {
239  case T_SeqScanState:
240  if (planstate->plan->parallel_aware)
241  ExecSeqScanEstimate((SeqScanState *) planstate,
242  e->pcxt);
243  break;
244  case T_IndexScanState:
245  if (planstate->plan->parallel_aware)
247  e->pcxt);
248  break;
250  if (planstate->plan->parallel_aware)
252  e->pcxt);
253  break;
254  case T_ForeignScanState:
255  if (planstate->plan->parallel_aware)
257  e->pcxt);
258  break;
259  case T_AppendState:
260  if (planstate->plan->parallel_aware)
261  ExecAppendEstimate((AppendState *) planstate,
262  e->pcxt);
263  break;
264  case T_CustomScanState:
265  if (planstate->plan->parallel_aware)
267  e->pcxt);
268  break;
270  if (planstate->plan->parallel_aware)
272  e->pcxt);
273  break;
274  case T_HashJoinState:
275  if (planstate->plan->parallel_aware)
276  ExecHashJoinEstimate((HashJoinState *) planstate,
277  e->pcxt);
278  break;
279  case T_HashState:
280  /* even when not parallel-aware, for EXPLAIN ANALYZE */
281  ExecHashEstimate((HashState *) planstate, e->pcxt);
282  break;
283  case T_SortState:
284  /* even when not parallel-aware, for EXPLAIN ANALYZE */
285  ExecSortEstimate((SortState *) planstate, e->pcxt);
286  break;
288  /* even when not parallel-aware, for EXPLAIN ANALYZE */
290  break;
291  case T_AggState:
292  /* even when not parallel-aware, for EXPLAIN ANALYZE */
293  ExecAggEstimate((AggState *) planstate, e->pcxt);
294  break;
295  default:
296  break;
297  }
298 
299  return planstate_tree_walker(planstate, ExecParallelEstimate, e);
300 }
void ExecSortEstimate(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:357
ParallelContext * pcxt
Definition: execParallel.c:114
void ExecIndexOnlyScanEstimate(IndexOnlyScanState *node, ParallelContext *pcxt)
void ExecAggEstimate(AggState *node, ParallelContext *pcxt)
Definition: nodeAgg.c:4968
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:229
void ExecHashJoinEstimate(HashJoinState *state, ParallelContext *pcxt)
void ExecHashEstimate(HashState *node, ParallelContext *pcxt)
Definition: nodeHash.c:2574
void ExecAppendEstimate(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:367
void ExecForeignScanEstimate(ForeignScanState *node, ParallelContext *pcxt)
void ExecSeqScanEstimate(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:250
#define nodeTag(nodeptr)
Definition: nodes.h:533
void ExecBitmapHeapEstimate(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecCustomScanEstimate(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:162
void ExecIncrementalSortEstimate(IncrementalSortState *node, ParallelContext *pcxt)
void ExecIndexScanEstimate(IndexScanState *node, ParallelContext *pcxt)
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3931

◆ 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

◆ ExecParallelGetQueryDesc()

static QueryDesc* ExecParallelGetQueryDesc ( shm_toc toc,
DestReceiver receiver,
int  instrument_options 
)
static

Definition at line 1217 of file execParallel.c.

References CreateQueryDesc(), GetActiveSnapshot(), InvalidSnapshot, PARALLEL_KEY_PARAMLISTINFO, PARALLEL_KEY_PLANNEDSTMT, PARALLEL_KEY_QUERY_TEXT, RestoreParamList(), shm_toc_lookup(), and stringToNode().

Referenced by ParallelQueryMain().

1219 {
1220  char *pstmtspace;
1221  char *paramspace;
1222  PlannedStmt *pstmt;
1223  ParamListInfo paramLI;
1224  char *queryString;
1225 
1226  /* Get the query string from shared memory */
1227  queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1228 
1229  /* Reconstruct leader-supplied PlannedStmt. */
1230  pstmtspace = shm_toc_lookup(toc, PARALLEL_KEY_PLANNEDSTMT, false);
1231  pstmt = (PlannedStmt *) stringToNode(pstmtspace);
1232 
1233  /* Reconstruct ParamListInfo. */
1234  paramspace = shm_toc_lookup(toc, PARALLEL_KEY_PARAMLISTINFO, false);
1235  paramLI = RestoreParamList(&paramspace);
1236 
1237  /* Create a QueryDesc for the query. */
1238  return CreateQueryDesc(pstmt,
1239  queryString,
1241  receiver, paramLI, NULL, instrument_options);
1242 }
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:786
void * stringToNode(const char *str)
Definition: read.c:89
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition: pquery.c:67
#define PARALLEL_KEY_PARAMLISTINFO
Definition: execParallel.c:60
#define PARALLEL_KEY_PLANNEDSTMT
Definition: execParallel.c:59
ParamListInfo RestoreParamList(char **start_address)
Definition: params.c:292
#define InvalidSnapshot
Definition: snapshot.h:123
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:65
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

◆ ExecParallelGetReceiver()

static DestReceiver * ExecParallelGetReceiver ( dsm_segment seg,
shm_toc toc 
)
static

Definition at line 1201 of file execParallel.c.

References CreateTupleQueueDestReceiver(), MyProc, PARALLEL_KEY_TUPLE_QUEUE, PARALLEL_TUPLE_QUEUE_SIZE, ParallelWorkerNumber, shm_mq_attach(), shm_mq_set_sender(), and shm_toc_lookup().

Referenced by ParallelQueryMain().

1202 {
1203  char *mqspace;
1204  shm_mq *mq;
1205 
1206  mqspace = shm_toc_lookup(toc, PARALLEL_KEY_TUPLE_QUEUE, false);
1208  mq = (shm_mq *) mqspace;
1210  return CreateTupleQueueDestReceiver(shm_mq_attach(mq, seg, NULL));
1211 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:62
PGPROC * MyProc
Definition: proc.c:67
int ParallelWorkerNumber
Definition: parallel.c:112
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:69
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:217
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:283
Definition: shm_mq.c:71
DestReceiver * CreateTupleQueueDestReceiver(shm_mq_handle *handle)
Definition: tqueue.c:119
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

◆ ExecParallelInitializeDSM()

static bool ExecParallelInitializeDSM ( PlanState node,
ExecParallelInitializeDSMContext d 
)
static

Definition at line 434 of file execParallel.c.

References ExecAggInitializeDSM(), ExecAppendInitializeDSM(), ExecBitmapHeapInitializeDSM(), ExecCustomScanInitializeDSM(), ExecForeignScanInitializeDSM(), ExecHashInitializeDSM(), ExecHashJoinInitializeDSM(), ExecIncrementalSortInitializeDSM(), ExecIndexOnlyScanInitializeDSM(), ExecIndexScanInitializeDSM(), ExecSeqScanInitializeDSM(), ExecSortInitializeDSM(), ExecParallelInitializeDSMContext::instrumentation, ExecParallelInitializeDSMContext::nnodes, nodeTag, Plan::parallel_aware, ExecParallelInitializeDSMContext::pcxt, PlanState::plan, SharedExecutorInstrumentation::plan_node_id, Plan::plan_node_id, planstate_tree_walker(), T_AggState, T_AppendState, T_BitmapHeapScanState, T_CustomScanState, T_ForeignScanState, T_HashJoinState, T_HashState, T_IncrementalSortState, T_IndexOnlyScanState, T_IndexScanState, T_SeqScanState, and T_SortState.

Referenced by ExecInitParallelPlan().

436 {
437  if (planstate == NULL)
438  return false;
439 
440  /* If instrumentation is enabled, initialize slot for this node. */
441  if (d->instrumentation != NULL)
443  planstate->plan->plan_node_id;
444 
445  /* Count this node. */
446  d->nnodes++;
447 
448  /*
449  * Call initializers for DSM-using plan nodes.
450  *
451  * Most plan nodes won't do anything here, but plan nodes that allocated
452  * DSM may need to initialize shared state in the DSM before parallel
453  * workers are launched. They can allocate the space they previously
454  * estimated using shm_toc_allocate, and add the keys they previously
455  * estimated using shm_toc_insert, in each case targeting pcxt->toc.
456  */
457  switch (nodeTag(planstate))
458  {
459  case T_SeqScanState:
460  if (planstate->plan->parallel_aware)
462  d->pcxt);
463  break;
464  case T_IndexScanState:
465  if (planstate->plan->parallel_aware)
467  d->pcxt);
468  break;
470  if (planstate->plan->parallel_aware)
472  d->pcxt);
473  break;
474  case T_ForeignScanState:
475  if (planstate->plan->parallel_aware)
477  d->pcxt);
478  break;
479  case T_AppendState:
480  if (planstate->plan->parallel_aware)
481  ExecAppendInitializeDSM((AppendState *) planstate,
482  d->pcxt);
483  break;
484  case T_CustomScanState:
485  if (planstate->plan->parallel_aware)
487  d->pcxt);
488  break;
490  if (planstate->plan->parallel_aware)
492  d->pcxt);
493  break;
494  case T_HashJoinState:
495  if (planstate->plan->parallel_aware)
497  d->pcxt);
498  break;
499  case T_HashState:
500  /* even when not parallel-aware, for EXPLAIN ANALYZE */
501  ExecHashInitializeDSM((HashState *) planstate, d->pcxt);
502  break;
503  case T_SortState:
504  /* even when not parallel-aware, for EXPLAIN ANALYZE */
505  ExecSortInitializeDSM((SortState *) planstate, d->pcxt);
506  break;
508  /* even when not parallel-aware, for EXPLAIN ANALYZE */
510  break;
511  case T_AggState:
512  /* even when not parallel-aware, for EXPLAIN ANALYZE */
513  ExecAggInitializeDSM((AggState *) planstate, d->pcxt);
514  break;
515  default:
516  break;
517  }
518 
519  return planstate_tree_walker(planstate, ExecParallelInitializeDSM, d);
520 }
void ExecIndexScanInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:104
void ExecAggInitializeDSM(AggState *node, ParallelContext *pcxt)
Definition: nodeAgg.c:4989
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:122
void ExecSortInitializeDSM(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:378
void ExecSeqScanInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:268
static bool ExecParallelInitializeDSM(PlanState *node, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:434
void ExecBitmapHeapInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecHashInitializeDSM(HashState *node, ParallelContext *pcxt)
Definition: nodeHash.c:2593
void ExecIncrementalSortInitializeDSM(IncrementalSortState *node, ParallelContext *pcxt)
void ExecForeignScanInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:175
void ExecAppendInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:386
#define nodeTag(nodeptr)
Definition: nodes.h:533
void ExecHashJoinInitializeDSM(HashJoinState *state, ParallelContext *pcxt)
void ExecIndexOnlyScanInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3931

◆ ExecParallelInitializeWorker()

static bool ExecParallelInitializeWorker ( PlanState planstate,
ParallelWorkerContext pwcxt 
)
static

Definition at line 1290 of file execParallel.c.

References ExecAggInitializeWorker(), ExecAppendInitializeWorker(), ExecBitmapHeapInitializeWorker(), ExecCustomScanInitializeWorker(), ExecForeignScanInitializeWorker(), ExecHashInitializeWorker(), ExecHashJoinInitializeWorker(), ExecIncrementalSortInitializeWorker(), ExecIndexOnlyScanInitializeWorker(), ExecIndexScanInitializeWorker(), ExecSeqScanInitializeWorker(), ExecSortInitializeWorker(), nodeTag, Plan::parallel_aware, PlanState::plan, planstate_tree_walker(), T_AggState, T_AppendState, T_BitmapHeapScanState, T_CustomScanState, T_ForeignScanState, T_HashJoinState, T_HashState, T_IncrementalSortState, T_IndexOnlyScanState, T_IndexScanState, T_SeqScanState, and T_SortState.

Referenced by ParallelQueryMain().

1291 {
1292  if (planstate == NULL)
1293  return false;
1294 
1295  switch (nodeTag(planstate))
1296  {
1297  case T_SeqScanState:
1298  if (planstate->plan->parallel_aware)
1299  ExecSeqScanInitializeWorker((SeqScanState *) planstate, pwcxt);
1300  break;
1301  case T_IndexScanState:
1302  if (planstate->plan->parallel_aware)
1304  pwcxt);
1305  break;
1306  case T_IndexOnlyScanState:
1307  if (planstate->plan->parallel_aware)
1309  pwcxt);
1310  break;
1311  case T_ForeignScanState:
1312  if (planstate->plan->parallel_aware)
1314  pwcxt);
1315  break;
1316  case T_AppendState:
1317  if (planstate->plan->parallel_aware)
1318  ExecAppendInitializeWorker((AppendState *) planstate, pwcxt);
1319  break;
1320  case T_CustomScanState:
1321  if (planstate->plan->parallel_aware)
1323  pwcxt);
1324  break;
1325  case T_BitmapHeapScanState:
1326  if (planstate->plan->parallel_aware)
1328  pwcxt);
1329  break;
1330  case T_HashJoinState:
1331  if (planstate->plan->parallel_aware)
1333  pwcxt);
1334  break;
1335  case T_HashState:
1336  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1337  ExecHashInitializeWorker((HashState *) planstate, pwcxt);
1338  break;
1339  case T_SortState:
1340  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1341  ExecSortInitializeWorker((SortState *) planstate, pwcxt);
1342  break;
1344  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1346  pwcxt);
1347  break;
1348  case T_AggState:
1349  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1350  ExecAggInitializeWorker((AggState *) planstate, pwcxt);
1351  break;
1352  default:
1353  break;
1354  }
1355 
1357  pwcxt);
1358 }
void ExecForeignScanInitializeWorker(ForeignScanState *node, ParallelWorkerContext *pwcxt)
void ExecAppendInitializeWorker(AppendState *node, ParallelWorkerContext *pwcxt)
Definition: nodeAppend.c:423
void ExecCustomScanInitializeWorker(CustomScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeCustom.c:206
void ExecSortInitializeWorker(SortState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSort.c:403
void ExecIndexScanInitializeWorker(IndexScanState *node, ParallelWorkerContext *pwcxt)
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
bool parallel_aware
Definition: plannodes.h:129
void ExecIncrementalSortInitializeWorker(IncrementalSortState *node, ParallelWorkerContext *pwcxt)
void ExecHashJoinInitializeWorker(HashJoinState *state, ParallelWorkerContext *pwcxt)
Plan * plan
Definition: execnodes.h:939
void ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
#define nodeTag(nodeptr)
Definition: nodes.h:533
void ExecAggInitializeWorker(AggState *node, ParallelWorkerContext *pwcxt)
Definition: nodeAgg.c:5014
void ExecHashInitializeWorker(HashState *node, ParallelWorkerContext *pwcxt)
Definition: nodeHash.c:2618
void ExecBitmapHeapInitializeWorker(BitmapHeapScanState *node, ParallelWorkerContext *pwcxt)
void ExecSeqScanInitializeWorker(SeqScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSeqscan.c:306
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3931

◆ ExecParallelReinitialize()

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

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

◆ ExecParallelReInitializeDSM()

static bool ExecParallelReInitializeDSM ( PlanState planstate,
ParallelContext pcxt 
)
static

Definition at line 938 of file execParallel.c.

References ExecAppendReInitializeDSM(), ExecBitmapHeapReInitializeDSM(), ExecCustomScanReInitializeDSM(), ExecForeignScanReInitializeDSM(), ExecHashJoinReInitializeDSM(), ExecIndexOnlyScanReInitializeDSM(), ExecIndexScanReInitializeDSM(), ExecSeqScanReInitializeDSM(), nodeTag, Plan::parallel_aware, PlanState::plan, planstate_tree_walker(), T_AppendState, T_BitmapHeapScanState, T_CustomScanState, T_ForeignScanState, T_HashJoinState, T_HashState, T_IncrementalSortState, T_IndexOnlyScanState, T_IndexScanState, T_SeqScanState, and T_SortState.

Referenced by ExecParallelReinitialize().

940 {
941  if (planstate == NULL)
942  return false;
943 
944  /*
945  * Call reinitializers for DSM-using plan nodes.
946  */
947  switch (nodeTag(planstate))
948  {
949  case T_SeqScanState:
950  if (planstate->plan->parallel_aware)
952  pcxt);
953  break;
954  case T_IndexScanState:
955  if (planstate->plan->parallel_aware)
957  pcxt);
958  break;
960  if (planstate->plan->parallel_aware)
962  pcxt);
963  break;
964  case T_ForeignScanState:
965  if (planstate->plan->parallel_aware)
967  pcxt);
968  break;
969  case T_AppendState:
970  if (planstate->plan->parallel_aware)
971  ExecAppendReInitializeDSM((AppendState *) planstate, pcxt);
972  break;
973  case T_CustomScanState:
974  if (planstate->plan->parallel_aware)
976  pcxt);
977  break;
979  if (planstate->plan->parallel_aware)
981  pcxt);
982  break;
983  case T_HashJoinState:
984  if (planstate->plan->parallel_aware)
986  pcxt);
987  break;
988  case T_HashState:
989  case T_SortState:
991  /* these nodes have DSM state, but no reinitialization is required */
992  break;
993 
994  default:
995  break;
996  }
997 
998  return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt);
999 }
void ExecCustomScanReInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:191
void ExecAppendReInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:407
void ExecHashJoinReInitializeDSM(HashJoinState *state, ParallelContext *cxt)
void ExecIndexScanReInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:938
void ExecForeignScanReInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
bool parallel_aware
Definition: plannodes.h:129
void ExecBitmapHeapReInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
Plan * plan
Definition: execnodes.h:939
void ExecIndexOnlyScanReInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
#define nodeTag(nodeptr)
Definition: nodes.h:533
void ExecSeqScanReInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:290
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3931

◆ ExecParallelReportInstrumentation()

static bool ExecParallelReportInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 1249 of file execParallel.c.

References Assert, elog, ERROR, GetInstrumentationArray, i, InstrAggNode(), InstrEndLoop(), PlanState::instrument, IsParallelWorker, SharedExecutorInstrumentation::num_plan_nodes, SharedExecutorInstrumentation::num_workers, ParallelWorkerNumber, PlanState::plan, SharedExecutorInstrumentation::plan_node_id, Plan::plan_node_id, and planstate_tree_walker().

Referenced by ParallelQueryMain().

1251 {
1252  int i;
1253  int plan_node_id = planstate->plan->plan_node_id;
1254  Instrumentation *instrument;
1255 
1256  InstrEndLoop(planstate->instrument);
1257 
1258  /*
1259  * If we shuffled the plan_node_id values in ps_instrument into sorted
1260  * order, we could use binary search here. This might matter someday if
1261  * we're pushing down sufficiently large plan trees. For now, do it the
1262  * slow, dumb way.
1263  */
1264  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1265  if (instrumentation->plan_node_id[i] == plan_node_id)
1266  break;
1267  if (i >= instrumentation->num_plan_nodes)
1268  elog(ERROR, "plan node %d not found", plan_node_id);
1269 
1270  /*
1271  * Add our statistics to the per-node, per-worker totals. It's possible
1272  * that this could happen more than once if we relaunched workers.
1273  */
1274  instrument = GetInstrumentationArray(instrumentation);
1275  instrument += i * instrumentation->num_workers;
1277  Assert(ParallelWorkerNumber < instrumentation->num_workers);
1278  InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
1279 
1281  instrumentation);
1282 }
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:104
Instrumentation * instrument
Definition: execnodes.h:949
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:150
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
int plan_node_id
Definition: plannodes.h:135
void InstrEndLoop(Instrumentation *instr)
Definition: instrument.c:121
#define ERROR
Definition: elog.h:43
int ParallelWorkerNumber
Definition: parallel.c:112
#define IsParallelWorker()
Definition: parallel.h:61
Plan * plan
Definition: execnodes.h:939
#define Assert(condition)
Definition: c.h:746
#define elog(elevel,...)
Definition: elog.h:214
int i
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3931
#define GetInstrumentationArray(sei)
Definition: execParallel.c:107

◆ ExecParallelRetrieveInstrumentation()

static bool ExecParallelRetrieveInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 1006 of file execParallel.c.

References elog, ERROR, EState::es_query_cxt, ExecAggRetrieveInstrumentation(), ExecHashRetrieveInstrumentation(), ExecIncrementalSortRetrieveInstrumentation(), ExecSortRetrieveInstrumentation(), GetInstrumentationArray, i, InstrAggNode(), WorkerInstrumentation::instrument, PlanState::instrument, MemoryContextSwitchTo(), mul_size(), nodeTag, SharedExecutorInstrumentation::num_plan_nodes, WorkerInstrumentation::num_workers, SharedExecutorInstrumentation::num_workers, offsetof, palloc(), PlanState::plan, SharedExecutorInstrumentation::plan_node_id, Plan::plan_node_id, planstate_tree_walker(), PlanState::state, T_AggState, T_HashState, T_IncrementalSortState, T_SortState, and PlanState::worker_instrument.

Referenced by ExecParallelCleanup().

1008 {
1009  Instrumentation *instrument;
1010  int i;
1011  int n;
1012  int ibytes;
1013  int plan_node_id = planstate->plan->plan_node_id;
1014  MemoryContext oldcontext;
1015 
1016  /* Find the instrumentation for this node. */
1017  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1018  if (instrumentation->plan_node_id[i] == plan_node_id)
1019  break;
1020  if (i >= instrumentation->num_plan_nodes)
1021  elog(ERROR, "plan node %d not found", plan_node_id);
1022 
1023  /* Accumulate the statistics from all workers. */
1024  instrument = GetInstrumentationArray(instrumentation);
1025  instrument += i * instrumentation->num_workers;
1026  for (n = 0; n < instrumentation->num_workers; ++n)
1027  InstrAggNode(planstate->instrument, &instrument[n]);
1028 
1029  /*
1030  * Also store the per-worker detail.
1031  *
1032  * Worker instrumentation should be allocated in the same context as the
1033  * regular instrumentation information, which is the per-query context.
1034  * Switch into per-query memory context.
1035  */
1036  oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
1037  ibytes = mul_size(instrumentation->num_workers, sizeof(Instrumentation));
1038  planstate->worker_instrument =
1039  palloc(ibytes + offsetof(WorkerInstrumentation, instrument));
1040  MemoryContextSwitchTo(oldcontext);
1041 
1042  planstate->worker_instrument->num_workers = instrumentation->num_workers;
1043  memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
1044 
1045  /* Perform any node-type-specific work that needs to be done. */
1046  switch (nodeTag(planstate))
1047  {
1048  case T_SortState:
1050  break;
1053  break;
1054  case T_HashState:
1056  break;
1057  case T_AggState:
1059  break;
1060  default:
1061  break;
1062  }
1063 
1065  instrumentation);
1066 }
WorkerInstrumentation * worker_instrument
Definition: execnodes.h:950
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:104
Instrumentation * instrument
Definition: execnodes.h:949
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:150
int plan_node_id
Definition: plannodes.h:135
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
EState * state
Definition: execnodes.h:941
void ExecHashRetrieveInstrumentation(HashState *node)
Definition: nodeHash.c:2660
MemoryContext es_query_cxt
Definition: execnodes.h:559
#define ERROR
Definition: elog.h:43
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
void ExecAggRetrieveInstrumentation(AggState *node)
Definition: nodeAgg.c:5027
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
Instrumentation instrument[FLEXIBLE_ARRAY_MEMBER]
Definition: instrument.h:81
Plan * plan
Definition: execnodes.h:939
void ExecIncrementalSortRetrieveInstrumentation(IncrementalSortState *node)
#define nodeTag(nodeptr)
Definition: nodes.h:533
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:214
int i
void ExecSortRetrieveInstrumentation(SortState *node)
Definition: nodeSort.c:417
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3931
#define offsetof(type, field)
Definition: c.h:669
#define GetInstrumentationArray(sei)
Definition: execParallel.c:107

◆ ExecParallelRetrieveJitInstrumentation()

static void ExecParallelRetrieveJitInstrumentation ( PlanState planstate,
SharedJitInstrumentation shared_jit 
)
static

Definition at line 1072 of file execParallel.c.

References EState::es_jit_worker_instr, EState::es_query_cxt, InstrJitAgg(), SharedJitInstrumentation::jit_instr, MemoryContextAlloc(), MemoryContextAllocZero(), mul_size(), SharedJitInstrumentation::num_workers, offsetof, PlanState::state, and PlanState::worker_jit_instrument.

Referenced by ExecParallelCleanup().

1074 {
1075  JitInstrumentation *combined;
1076  int ibytes;
1077 
1078  int n;
1079 
1080  /*
1081  * Accumulate worker JIT instrumentation into the combined JIT
1082  * instrumentation, allocating it if required.
1083  */
1084  if (!planstate->state->es_jit_worker_instr)
1085  planstate->state->es_jit_worker_instr =
1087  combined = planstate->state->es_jit_worker_instr;
1088 
1089  /* Accumulate all the workers' instrumentations. */
1090  for (n = 0; n < shared_jit->num_workers; ++n)
1091  InstrJitAgg(combined, &shared_jit->jit_instr[n]);
1092 
1093  /*
1094  * Store the per-worker detail.
1095  *
1096  * Similar to ExecParallelRetrieveInstrumentation(), allocate the
1097  * instrumentation in per-query context.
1098  */
1099  ibytes = offsetof(SharedJitInstrumentation, jit_instr)
1100  + mul_size(shared_jit->num_workers, sizeof(JitInstrumentation));
1101  planstate->worker_jit_instrument =
1102  MemoryContextAlloc(planstate->state->es_query_cxt, ibytes);
1103 
1104  memcpy(planstate->worker_jit_instrument, shared_jit, ibytes);
1105 }
struct JitInstrumentation * es_jit_worker_instr
Definition: execnodes.h:606
EState * state
Definition: execnodes.h:941
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER]
Definition: jit.h:51
MemoryContext es_query_cxt
Definition: execnodes.h:559
struct SharedJitInstrumentation * worker_jit_instrument
Definition: execnodes.h:953
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
Definition: jit.c:184
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:840
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
#define offsetof(type, field)
Definition: c.h:669

◆ ExecParallelSetupTupleQueues()

static shm_mq_handle ** ExecParallelSetupTupleQueues ( ParallelContext pcxt,
bool  reinitialize 
)
static

Definition at line 527 of file execParallel.c.

References i, mul_size(), MyProc, ParallelContext::nworkers, palloc(), PARALLEL_KEY_TUPLE_QUEUE, PARALLEL_TUPLE_QUEUE_SIZE, ParallelContext::seg, shm_mq_attach(), shm_mq_create(), shm_mq_set_receiver(), shm_toc_allocate(), shm_toc_insert(), shm_toc_lookup(), and ParallelContext::toc.

Referenced by ExecInitParallelPlan(), and ExecParallelReinitialize().

528 {
529  shm_mq_handle **responseq;
530  char *tqueuespace;
531  int i;
532 
533  /* Skip this if no workers. */
534  if (pcxt->nworkers == 0)
535  return NULL;
536 
537  /* Allocate memory for shared memory queue handles. */
538  responseq = (shm_mq_handle **)
539  palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
540 
541  /*
542  * If not reinitializing, allocate space from the DSM for the queues;
543  * otherwise, find the already allocated space.
544  */
545  if (!reinitialize)
546  tqueuespace =
547  shm_toc_allocate(pcxt->toc,
549  pcxt->nworkers));
550  else
551  tqueuespace = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, false);
552 
553  /* Create the queues, and become the receiver for each. */
554  for (i = 0; i < pcxt->nworkers; ++i)
555  {
556  shm_mq *mq;
557 
558  mq = shm_mq_create(tqueuespace +
560  (Size) PARALLEL_TUPLE_QUEUE_SIZE);
561 
563  responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
564  }
565 
566  /* Add array of queues to shm_toc, so others can find it. */
567  if (!reinitialize)
568  shm_toc_insert(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, tqueuespace);
569 
570  /* Return array of handles. */
571  return responseq;
572 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:62
PGPROC * MyProc
Definition: proc.c:67
dsm_segment * seg
Definition: parallel.h:43
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:170
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:69
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
size_t Size
Definition: c.h:474
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:283
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * palloc(Size size)
Definition: mcxt.c:950
int i
Definition: shm_mq.c:71
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:199
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
shm_toc * toc
Definition: parallel.h:45

◆ ExecSerializePlan()

static char * ExecSerializePlan ( Plan plan,
EState estate 
)
static

Definition at line 146 of file execParallel.c.

References PlannedStmt::appendRelations, PlannedStmt::canSetTag, CMD_SELECT, PlannedStmt::commandType, copyObject, PlannedStmt::dependsOnRole, EState::es_plannedstmt, EState::es_range_table, PlannedStmt::hasModifyingCTE, PlannedStmt::hasReturning, PlannedStmt::invalItems, lappend(), lfirst, lfirst_node, makeNode, NIL, nodeToString(), Plan::parallel_safe, PlannedStmt::parallelModeNeeded, PlannedStmt::paramExecTypes, PlannedStmt::planTree, PlannedStmt::queryId, PlannedStmt::relationOids, TargetEntry::resjunk, PlannedStmt::resultRelations, PlannedStmt::rewindPlanIDs, PlannedStmt::rowMarks, PlannedStmt::rtable, PlannedStmt::stmt_len, PlannedStmt::stmt_location, PlannedStmt::subplans, Plan::targetlist, PlannedStmt::transientPlan, and PlannedStmt::utilityStmt.

Referenced by ExecInitParallelPlan().

147 {
148  PlannedStmt *pstmt;
149  ListCell *lc;
150 
151  /* We can't scribble on the original plan, so make a copy. */
152  plan = copyObject(plan);
153 
154  /*
155  * The worker will start its own copy of the executor, and that copy will
156  * insert a junk filter if the toplevel node has any resjunk entries. We
157  * don't want that to happen, because while resjunk columns shouldn't be
158  * sent back to the user, here the tuples are coming back to another
159  * backend which may very well need them. So mutate the target list
160  * accordingly. This is sort of a hack; there might be better ways to do
161  * this...
162  */
163  foreach(lc, plan->targetlist)
164  {
165  TargetEntry *tle = lfirst_node(TargetEntry, lc);
166 
167  tle->resjunk = false;
168  }
169 
170  /*
171  * Create a dummy PlannedStmt. Most of the fields don't need to be valid
172  * for our purposes, but the worker will need at least a minimal
173  * PlannedStmt to start the executor.
174  */
175  pstmt = makeNode(PlannedStmt);
176  pstmt->commandType = CMD_SELECT;
177  pstmt->queryId = UINT64CONST(0);
178  pstmt->hasReturning = false;
179  pstmt->hasModifyingCTE = false;
180  pstmt->canSetTag = true;
181  pstmt->transientPlan = false;
182  pstmt->dependsOnRole = false;
183  pstmt->parallelModeNeeded = false;
184  pstmt->planTree = plan;
185  pstmt->rtable = estate->es_range_table;
186  pstmt->resultRelations = NIL;
187  pstmt->appendRelations = NIL;
188 
189  /*
190  * Transfer only parallel-safe subplans, leaving a NULL "hole" in the list
191  * for unsafe ones (so that the list indexes of the safe ones are
192  * preserved). This positively ensures that the worker won't try to run,
193  * or even do ExecInitNode on, an unsafe subplan. That's important to
194  * protect, eg, non-parallel-aware FDWs from getting into trouble.
195  */
196  pstmt->subplans = NIL;
197  foreach(lc, estate->es_plannedstmt->subplans)
198  {
199  Plan *subplan = (Plan *) lfirst(lc);
200 
201  if (subplan && !subplan->parallel_safe)
202  subplan = NULL;
203  pstmt->subplans = lappend(pstmt->subplans, subplan);
204  }
205 
206  pstmt->rewindPlanIDs = NULL;
207  pstmt->rowMarks = NIL;
208  pstmt->relationOids = NIL;
209  pstmt->invalItems = NIL; /* workers can't replan anyway... */
210  pstmt->paramExecTypes = estate->es_plannedstmt->paramExecTypes;
211  pstmt->utilityStmt = NULL;
212  pstmt->stmt_location = -1;
213  pstmt->stmt_len = -1;
214 
215  /* Return serialized copy of our dummy PlannedStmt. */
216  return nodeToString(pstmt);
217 }
bool dependsOnRole
Definition: plannodes.h:58
List * paramExecTypes
Definition: plannodes.h:84
#define NIL
Definition: pg_list.h:65
List * relationOids
Definition: plannodes.h:80
PlannedStmt * es_plannedstmt
Definition: execnodes.h:525
List * es_range_table
Definition: execnodes.h:519
bool transientPlan
Definition: plannodes.h:56
int stmt_len
Definition: plannodes.h:90
struct Plan * planTree
Definition: plannodes.h:64
List * invalItems
Definition: plannodes.h:82
bool resjunk
Definition: primnodes.h:1417
#define lfirst_node(type, lc)
Definition: pg_list.h:172
int stmt_location
Definition: plannodes.h:89
bool hasReturning
Definition: plannodes.h:50
Node * utilityStmt
Definition: plannodes.h:86
List * lappend(List *list, void *datum)
Definition: list.c:321
bool canSetTag
Definition: plannodes.h:54
CmdType commandType
Definition: plannodes.h:46
List * rowMarks
Definition: plannodes.h:78
#define makeNode(_type_)
Definition: nodes.h:576
List * subplans
Definition: plannodes.h:73
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * rewindPlanIDs
Definition: plannodes.h:76
bool hasModifyingCTE
Definition: plannodes.h:52
List * rtable
Definition: plannodes.h:66
List * targetlist
Definition: plannodes.h:136
uint64 queryId
Definition: plannodes.h:48
List * resultRelations
Definition: plannodes.h:69
bool parallelModeNeeded
Definition: plannodes.h:60
char * nodeToString(const void *obj)
Definition: outfuncs.c:4358
bool parallel_safe
Definition: plannodes.h:130
#define copyObject(obj)
Definition: nodes.h:644
List * appendRelations
Definition: plannodes.h:71

◆ 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

◆ RestoreParamExecParams()

static void RestoreParamExecParams ( char *  start_address,
EState estate 
)
static

Definition at line 405 of file execParallel.c.

References datumRestore(), EState::es_param_exec_vals, ParamExecData::execPlan, i, ParamExecData::isnull, and ParamExecData::value.

Referenced by ParallelQueryMain().

406 {
407  int nparams;
408  int i;
409  int paramid;
410 
411  memcpy(&nparams, start_address, sizeof(int));
412  start_address += sizeof(int);
413 
414  for (i = 0; i < nparams; i++)
415  {
416  ParamExecData *prm;
417 
418  /* Read paramid */
419  memcpy(&paramid, start_address, sizeof(int));
420  start_address += sizeof(int);
421  prm = &(estate->es_param_exec_vals[paramid]);
422 
423  /* Read datum/isnull. */
424  prm->value = datumRestore(&start_address, &prm->isnull);
425  prm->execPlan = NULL;
426  }
427 }
void * execPlan
Definition: params.h:148
ParamExecData * es_param_exec_vals
Definition: execnodes.h:554
bool isnull
Definition: params.h:150
Datum datumRestore(char **start_address, bool *isnull)
Definition: datum.c:469
int i
Datum value
Definition: params.h:149

◆ SerializeParamExecParams()

static dsa_pointer SerializeParamExecParams ( EState estate,
Bitmapset params,
dsa_area area 
)
static

Definition at line 350 of file execParallel.c.

References bms_next_member(), bms_num_members(), datumSerialize(), dsa_allocate, dsa_get_address(), EState::es_param_exec_vals, EState::es_plannedstmt, EstimateParamExecSpace(), get_typlenbyval(), ParamExecData::isnull, list_nth_oid(), OidIsValid, PlannedStmt::paramExecTypes, and ParamExecData::value.

Referenced by ExecInitParallelPlan(), and ExecParallelReinitialize().

351 {
352  Size size;
353  int nparams;
354  int paramid;
355  ParamExecData *prm;
356  dsa_pointer handle;
357  char *start_address;
358 
359  /* Allocate enough space for the current parameter values. */
360  size = EstimateParamExecSpace(estate, params);
361  handle = dsa_allocate(area, size);
362  start_address = dsa_get_address(area, handle);
363 
364  /* First write the number of parameters as a 4-byte integer. */
365  nparams = bms_num_members(params);
366  memcpy(start_address, &nparams, sizeof(int));
367  start_address += sizeof(int);
368 
369  /* Write details for each parameter in turn. */
370  paramid = -1;
371  while ((paramid = bms_next_member(params, paramid)) >= 0)
372  {
373  Oid typeOid;
374  int16 typLen;
375  bool typByVal;
376 
377  prm = &(estate->es_param_exec_vals[paramid]);
378  typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
379  paramid);
380 
381  /* Write paramid. */
382  memcpy(start_address, &paramid, sizeof(int));
383  start_address += sizeof(int);
384 
385  /* Write datum/isnull */
386  if (OidIsValid(typeOid))
387  get_typlenbyval(typeOid, &typLen, &typByVal);
388  else
389  {
390  /* If no type OID, assume by-value, like copyParamList does. */
391  typLen = sizeof(Datum);
392  typByVal = true;
393  }
394  datumSerialize(prm->value, prm->isnull, typByVal, typLen,
395  &start_address);
396  }
397 
398  return handle;
399 }
signed short int16
Definition: c.h:362
List * paramExecTypes
Definition: plannodes.h:84
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
PlannedStmt * es_plannedstmt
Definition: execnodes.h:525
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
uint64 dsa_pointer
Definition: dsa.h:62
static Oid list_nth_oid(const List *list, int n)
Definition: pg_list.h:288
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
ParamExecData * es_param_exec_vals
Definition: execnodes.h:554
bool isnull
Definition: params.h:150
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
static Size EstimateParamExecSpace(EState *estate, Bitmapset *params)
Definition: execParallel.c:306
uintptr_t Datum
Definition: postgres.h:367
size_t Size
Definition: c.h:474
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2139
void datumSerialize(Datum value, bool isnull, bool typByVal, int typLen, char **start_address)
Definition: datum.c:407
Datum value
Definition: params.h:149
#define dsa_allocate(area, size)
Definition: dsa.h:84