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_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)
 
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:837

Definition at line 104 of file execParallel.c.

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

◆ PARALLEL_KEY_BUFFER_USAGE

#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xE000000000000004)

Definition at line 60 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

◆ PARALLEL_KEY_DSA

#define PARALLEL_KEY_DSA   UINT64CONST(0xE000000000000007)

Definition at line 63 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 62 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

◆ PARALLEL_KEY_PARAMLISTINFO

#define PARALLEL_KEY_PARAMLISTINFO   UINT64CONST(0xE000000000000003)

Definition at line 59 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

◆ PARALLEL_KEY_PLANNEDSTMT

#define PARALLEL_KEY_PLANNEDSTMT   UINT64CONST(0xE000000000000002)

Definition at line 58 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

◆ PARALLEL_KEY_QUERY_TEXT

#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xE000000000000008)

Definition at line 64 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

◆ PARALLEL_KEY_TUPLE_QUEUE

#define PARALLEL_KEY_TUPLE_QUEUE   UINT64CONST(0xE000000000000005)

Definition at line 61 of file execParallel.c.

Referenced by ExecParallelGetReceiver(), and ExecParallelSetupTupleQueues().

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

297 {
298  int paramid;
299  Size sz = sizeof(int);
300 
301  paramid = -1;
302  while ((paramid = bms_next_member(params, paramid)) >= 0)
303  {
304  Oid typeOid;
305  int16 typLen;
306  bool typByVal;
307  ParamExecData *prm;
308 
309  prm = &(estate->es_param_exec_vals[paramid]);
310  typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
311  paramid);
312 
313  sz = add_size(sz, sizeof(int)); /* space for paramid */
314 
315  /* space for datum/isnull */
316  if (OidIsValid(typeOid))
317  get_typlenbyval(typeOid, &typLen, &typByVal);
318  else
319  {
320  /* If no type OID, assume by-value, like copyParamList does. */
321  typLen = sizeof(Datum);
322  typByVal = true;
323  }
324  sz = add_size(sz,
325  datumEstimateSpace(prm->value, prm->isnull,
326  typByVal, typLen));
327  }
328  return sz;
329 }
signed short int16
Definition: c.h:312
List * paramExecTypes
Definition: plannodes.h:94
Oid list_nth_oid(const List *list, int n)
Definition: list.c:432
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1075
PlannedStmt * es_plannedstmt
Definition: execnodes.h:481
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
ParamExecData * es_param_exec_vals
Definition: execnodes.h:518
bool isnull
Definition: params.h:149
Size datumEstimateSpace(Datum value, bool isnull, bool typByVal, int typLen)
Definition: datum.c:262
uintptr_t Datum
Definition: postgres.h:367
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
size_t Size
Definition: c.h:433
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2005
Datum value
Definition: params.h:148

◆ ExecInitParallelPlan()

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

Definition at line 562 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, ExecEvalParamExecParams(), ExecParallelEstimate(), ExecParallelInitializeDSM(), ExecParallelSetupTupleQueues(), ExecSerializePlan(), ParallelExecutorInfo::finished, GetInstrumentationArray, i, InitializeParallelDSM(), InstrInit(), SharedExecutorInstrumentation::instrument_offset, SharedExecutorInstrumentation::instrument_options, ParallelExecutorInfo::instrumentation, ExecParallelInitializeDSMContext::instrumentation, InvalidDsaPointer, FixedParallelExecutorState::jit_flags, LWTRANCHE_PARALLEL_QUERY_DSA, MAXALIGN, mul_size(), ExecParallelEstimateContext::nnodes, ExecParallelInitializeDSMContext::nnodes, SharedExecutorInstrumentation::num_plan_nodes, SharedExecutorInstrumentation::num_workers, ParallelContext::nworkers, offsetof, palloc0(), PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_DSA, PARALLEL_KEY_EXECUTOR_FIXED, PARALLEL_KEY_INSTRUMENTATION, PARALLEL_KEY_PARAMLISTINFO, PARALLEL_KEY_PLANNEDSTMT, PARALLEL_KEY_QUERY_TEXT, PARALLEL_TUPLE_QUEUE_SIZE, ParallelExecutorInfo::param_exec, FixedParallelExecutorState::param_exec, ParallelExecutorInfo::pcxt, ExecParallelEstimateContext::pcxt, ExecParallelInitializeDSMContext::pcxt, PlanState::plan, ParallelExecutorInfo::planstate, ParallelExecutorInfo::reader, ParallelContext::seg, SerializeParamExecParams(), SerializeParamList(), shm_toc_allocate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_insert(), ParallelContext::toc, ParallelExecutorInfo::tqueue, and FixedParallelExecutorState::tuples_needed.

Referenced by ExecGather(), and ExecGatherMerge().

565 {
567  ParallelContext *pcxt;
571  char *pstmt_data;
572  char *pstmt_space;
573  char *paramlistinfo_space;
574  BufferUsage *bufusage_space;
575  SharedExecutorInstrumentation *instrumentation = NULL;
576  int pstmt_len;
577  int paramlistinfo_len;
578  int instrumentation_len = 0;
579  int instrument_offset = 0;
580  Size dsa_minsize = dsa_minimum_size();
581  char *query_string;
582  int query_len;
583 
584  /* Force parameters we're going to pass to workers to be evaluated. */
585  ExecEvalParamExecParams(sendParams, estate);
586 
587  /* Allocate object for return value. */
588  pei = palloc0(sizeof(ParallelExecutorInfo));
589  pei->finished = false;
590  pei->planstate = planstate;
591 
592  /* Fix up and serialize plan to be sent to workers. */
593  pstmt_data = ExecSerializePlan(planstate->plan, estate);
594 
595  /* Create a parallel context. */
596  pcxt = CreateParallelContext("postgres", "ParallelQueryMain", nworkers, false);
597  pei->pcxt = pcxt;
598 
599  /*
600  * Before telling the parallel context to create a dynamic shared memory
601  * segment, we need to figure out how big it should be. Estimate space
602  * for the various things we need to store.
603  */
604 
605  /* Estimate space for fixed-size state. */
608  shm_toc_estimate_keys(&pcxt->estimator, 1);
609 
610  /* Estimate space for query text. */
611  query_len = strlen(estate->es_sourceText);
612  shm_toc_estimate_chunk(&pcxt->estimator, query_len + 1);
613  shm_toc_estimate_keys(&pcxt->estimator, 1);
614 
615  /* Estimate space for serialized PlannedStmt. */
616  pstmt_len = strlen(pstmt_data) + 1;
617  shm_toc_estimate_chunk(&pcxt->estimator, pstmt_len);
618  shm_toc_estimate_keys(&pcxt->estimator, 1);
619 
620  /* Estimate space for serialized ParamListInfo. */
621  paramlistinfo_len = EstimateParamListSpace(estate->es_param_list_info);
622  shm_toc_estimate_chunk(&pcxt->estimator, paramlistinfo_len);
623  shm_toc_estimate_keys(&pcxt->estimator, 1);
624 
625  /*
626  * Estimate space for BufferUsage.
627  *
628  * If EXPLAIN is not in use and there are no extensions loaded that care,
629  * we could skip this. But we have no way of knowing whether anyone's
630  * looking at pgBufferUsage, so do it unconditionally.
631  */
633  mul_size(sizeof(BufferUsage), pcxt->nworkers));
634  shm_toc_estimate_keys(&pcxt->estimator, 1);
635 
636  /* Estimate space for tuple queues. */
639  shm_toc_estimate_keys(&pcxt->estimator, 1);
640 
641  /*
642  * Give parallel-aware nodes a chance to add to the estimates, and get a
643  * count of how many PlanState nodes there are.
644  */
645  e.pcxt = pcxt;
646  e.nnodes = 0;
647  ExecParallelEstimate(planstate, &e);
648 
649  /* Estimate space for instrumentation, if required. */
650  if (estate->es_instrument)
651  {
652  instrumentation_len =
653  offsetof(SharedExecutorInstrumentation, plan_node_id) +
654  sizeof(int) * e.nnodes;
655  instrumentation_len = MAXALIGN(instrumentation_len);
656  instrument_offset = instrumentation_len;
657  instrumentation_len +=
658  mul_size(sizeof(Instrumentation),
659  mul_size(e.nnodes, nworkers));
660  shm_toc_estimate_chunk(&pcxt->estimator, instrumentation_len);
661  shm_toc_estimate_keys(&pcxt->estimator, 1);
662  }
663 
664  /* Estimate space for DSA area. */
665  shm_toc_estimate_chunk(&pcxt->estimator, dsa_minsize);
666  shm_toc_estimate_keys(&pcxt->estimator, 1);
667 
668  /* Everyone's had a chance to ask for space, so now create the DSM. */
669  InitializeParallelDSM(pcxt);
670 
671  /*
672  * OK, now we have a dynamic shared memory segment, and it should be big
673  * enough to store all of the data we estimated we would want to put into
674  * it, plus whatever general stuff (not specifically executor-related) the
675  * ParallelContext itself needs to store there. None of the space we
676  * asked for has been allocated or initialized yet, though, so do that.
677  */
678 
679  /* Store fixed-size state. */
680  fpes = shm_toc_allocate(pcxt->toc, sizeof(FixedParallelExecutorState));
681  fpes->tuples_needed = tuples_needed;
683  fpes->eflags = estate->es_top_eflags;
684  fpes->jit_flags = estate->es_jit_flags;
686 
687  /* Store query string */
688  query_string = shm_toc_allocate(pcxt->toc, query_len + 1);
689  memcpy(query_string, estate->es_sourceText, query_len + 1);
690  shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string);
691 
692  /* Store serialized PlannedStmt. */
693  pstmt_space = shm_toc_allocate(pcxt->toc, pstmt_len);
694  memcpy(pstmt_space, pstmt_data, pstmt_len);
695  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PLANNEDSTMT, pstmt_space);
696 
697  /* Store serialized ParamListInfo. */
698  paramlistinfo_space = shm_toc_allocate(pcxt->toc, paramlistinfo_len);
699  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PARAMLISTINFO, paramlistinfo_space);
700  SerializeParamList(estate->es_param_list_info, &paramlistinfo_space);
701 
702  /* Allocate space for each worker's BufferUsage; no need to initialize. */
703  bufusage_space = shm_toc_allocate(pcxt->toc,
704  mul_size(sizeof(BufferUsage), pcxt->nworkers));
705  shm_toc_insert(pcxt->toc, PARALLEL_KEY_BUFFER_USAGE, bufusage_space);
706  pei->buffer_usage = bufusage_space;
707 
708  /* Set up the tuple queues that the workers will write into. */
709  pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
710 
711  /* We don't need the TupleQueueReaders yet, though. */
712  pei->reader = NULL;
713 
714  /*
715  * If instrumentation options were supplied, allocate space for the data.
716  * It only gets partially initialized here; the rest happens during
717  * ExecParallelInitializeDSM.
718  */
719  if (estate->es_instrument)
720  {
721  Instrumentation *instrument;
722  int i;
723 
724  instrumentation = shm_toc_allocate(pcxt->toc, instrumentation_len);
725  instrumentation->instrument_options = estate->es_instrument;
726  instrumentation->instrument_offset = instrument_offset;
727  instrumentation->num_workers = nworkers;
728  instrumentation->num_plan_nodes = e.nnodes;
729  instrument = GetInstrumentationArray(instrumentation);
730  for (i = 0; i < nworkers * e.nnodes; ++i)
731  InstrInit(&instrument[i], estate->es_instrument);
733  instrumentation);
734  pei->instrumentation = instrumentation;
735  }
736 
737  /*
738  * Create a DSA area that can be used by the leader and all workers.
739  * (However, if we failed to create a DSM and are using private memory
740  * instead, then skip this.)
741  */
742  if (pcxt->seg != NULL)
743  {
744  char *area_space;
745 
746  area_space = shm_toc_allocate(pcxt->toc, dsa_minsize);
747  shm_toc_insert(pcxt->toc, PARALLEL_KEY_DSA, area_space);
748  pei->area = dsa_create_in_place(area_space, dsa_minsize,
750  pcxt->seg);
751 
752  /*
753  * Serialize parameters, if any, using DSA storage. We don't dare use
754  * the main parallel query DSM for this because we might relaunch
755  * workers after the values have changed (and thus the amount of
756  * storage required has changed).
757  */
758  if (!bms_is_empty(sendParams))
759  {
760  pei->param_exec = SerializeParamExecParams(estate, sendParams,
761  pei->area);
762  fpes->param_exec = pei->param_exec;
763  }
764  }
765 
766  /*
767  * Give parallel-aware nodes a chance to initialize their shared data.
768  * This also initializes the elements of instrumentation->ps_instrument,
769  * if it exists.
770  */
771  d.pcxt = pcxt;
772  d.instrumentation = instrumentation;
773  d.nnodes = 0;
774 
775  /* Install our DSA area while initializing the plan. */
776  estate->es_query_dsa = pei->area;
777  ExecParallelInitializeDSM(planstate, &d);
778  estate->es_query_dsa = NULL;
779 
780  /*
781  * Make sure that the world hasn't shifted under our feet. This could
782  * probably just be an Assert(), but let's be conservative for now.
783  */
784  if (e.nnodes != d.nnodes)
785  elog(ERROR, "inconsistent count of PlanState nodes");
786 
787  /* OK, we're ready to rock and roll. */
788  return pei;
789 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:565
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
dsm_segment * seg
Definition: parallel.h:42
shm_toc_estimator estimator
Definition: parallel.h:41
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers, bool serializable_okay)
Definition: parallel.c:152
PlanState * planstate
Definition: execParallel.h:26
static char * ExecSerializePlan(Plan *plan, EState *estate)
Definition: execParallel.c:143
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:510
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
Size EstimateParamListSpace(ParamListInfo paramLI)
Definition: params.c:88
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:340
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:119
ParallelContext * pcxt
Definition: execParallel.c:111
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:62
const char * es_sourceText
Definition: execnodes.h:482
static bool ExecParallelInitializeDSM(PlanState *node, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:424
#define ERROR
Definition: elog.h:43
BufferUsage * buffer_usage
Definition: execParallel.h:28
#define PARALLEL_KEY_PARAMLISTINFO
Definition: execParallel.c:59
#define PARALLEL_KEY_PLANNEDSTMT
Definition: execParallel.c:58
int es_jit_flags
Definition: execnodes.h:572
struct TupleQueueReader ** reader
Definition: execParallel.h:35
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:66
int es_instrument
Definition: execnodes.h:533
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:57
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:226
dsa_area * dsa_create_in_place(void *place, size_t size, int tranche_id, dsm_segment *segment)
Definition: dsa.c:468
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:60
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:729
void InitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:206
void InstrInit(Instrumentation *instr, int instrument_options)
Definition: instrument.c:54
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
void * palloc0(Size size)
Definition: mcxt.c:955
Plan * plan
Definition: execnodes.h:912
Size dsa_minimum_size(void)
Definition: dsa.c:1160
void SerializeParamList(ParamListInfo paramLI, char **start_address)
Definition: params.c:148
void ExecEvalParamExecParams(Bitmapset *params, EState *estate)
size_t Size
Definition: c.h:433
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define MAXALIGN(LEN)
Definition: c.h:652
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
dsa_pointer param_exec
Definition: execParallel.h:31
#define PARALLEL_KEY_DSA
Definition: execParallel.c:63
e
Definition: preproc-init.c:82
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
int es_top_eflags
Definition: execnodes.h:532
int i
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:64
ParamListInfo es_param_list_info
Definition: execnodes.h:517
shm_mq_handle ** tqueue
Definition: execParallel.h:34
#define elog
Definition: elog.h:219
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:29
#define offsetof(type, field)
Definition: c.h:622
#define GetInstrumentationArray(sei)
Definition: execParallel.c:104
shm_toc * toc
Definition: parallel.h:44

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

Definition at line 1050 of file execParallel.c.

References ParallelExecutorInfo::area, DestroyParallelContext(), dsa_detach(), dsa_free(), DsaPointerIsValid, ExecParallelRetrieveInstrumentation(), ParallelExecutorInfo::instrumentation, InvalidDsaPointer, ParallelExecutorInfo::param_exec, ParallelExecutorInfo::pcxt, pfree(), and ParallelExecutorInfo::planstate.

Referenced by ExecShutdownGather(), and ExecShutdownGatherMerge().

1051 {
1052  /* Accumulate instrumentation, if any. */
1053  if (pei->instrumentation)
1055  pei->instrumentation);
1056 
1057  /* Free any serialized parameters. */
1058  if (DsaPointerIsValid(pei->param_exec))
1059  {
1060  dsa_free(pei->area, pei->param_exec);
1062  }
1063  if (pei->area != NULL)
1064  {
1065  dsa_detach(pei->area);
1066  pei->area = NULL;
1067  }
1068  if (pei->pcxt != NULL)
1069  {
1071  pei->pcxt = NULL;
1072  }
1073  pfree(pei);
1074 }
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
PlanState * planstate
Definition: execParallel.h:26
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:862
void pfree(void *pointer)
Definition: mcxt.c:1031
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:936
void dsa_detach(dsa_area *area)
Definition: dsa.c:1884
dsa_pointer param_exec
Definition: execParallel.h:31
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:812
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:29

◆ ExecParallelCreateReaders()

void ExecParallelCreateReaders ( ParallelExecutorInfo pei)

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

799 {
800  int nworkers = pei->pcxt->nworkers_launched;
801  int i;
802 
803  Assert(pei->reader == NULL);
804 
805  if (nworkers > 0)
806  {
807  pei->reader = (TupleQueueReader **)
808  palloc(nworkers * sizeof(TupleQueueReader *));
809 
810  for (i = 0; i < nworkers; i++)
811  {
812  shm_mq_set_handle(pei->tqueue[i],
813  pei->pcxt->worker[i].bgwhandle);
814  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
815  }
816  }
817 }
ParallelContext * pcxt
Definition: execParallel.h:27
ParallelWorkerInfo * worker
Definition: parallel.h:45
struct TupleQueueReader ** reader
Definition: execParallel.h:35
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
int nworkers_launched
Definition: parallel.h:37
TupleQueueReader * CreateTupleQueueReader(shm_mq_handle *handle)
Definition: tqueue.c:135
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:310
#define Assert(condition)
Definition: c.h:699
void * palloc(Size size)
Definition: mcxt.c:924
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:34

◆ ExecParallelEstimate()

static bool ExecParallelEstimate ( PlanState node,
ExecParallelEstimateContext e 
)
static

Definition at line 226 of file execParallel.c.

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

Referenced by ExecInitParallelPlan().

227 {
228  if (planstate == NULL)
229  return false;
230 
231  /* Count this node. */
232  e->nnodes++;
233 
234  switch (nodeTag(planstate))
235  {
236  case T_SeqScanState:
237  if (planstate->plan->parallel_aware)
238  ExecSeqScanEstimate((SeqScanState *) planstate,
239  e->pcxt);
240  break;
241  case T_IndexScanState:
242  if (planstate->plan->parallel_aware)
244  e->pcxt);
245  break;
247  if (planstate->plan->parallel_aware)
249  e->pcxt);
250  break;
251  case T_ForeignScanState:
252  if (planstate->plan->parallel_aware)
254  e->pcxt);
255  break;
256  case T_AppendState:
257  if (planstate->plan->parallel_aware)
258  ExecAppendEstimate((AppendState *) planstate,
259  e->pcxt);
260  break;
261  case T_CustomScanState:
262  if (planstate->plan->parallel_aware)
264  e->pcxt);
265  break;
267  if (planstate->plan->parallel_aware)
269  e->pcxt);
270  break;
271  case T_HashJoinState:
272  if (planstate->plan->parallel_aware)
273  ExecHashJoinEstimate((HashJoinState *) planstate,
274  e->pcxt);
275  break;
276  case T_HashState:
277  /* even when not parallel-aware, for EXPLAIN ANALYZE */
278  ExecHashEstimate((HashState *) planstate, e->pcxt);
279  break;
280  case T_SortState:
281  /* even when not parallel-aware, for EXPLAIN ANALYZE */
282  ExecSortEstimate((SortState *) planstate, e->pcxt);
283  break;
284 
285  default:
286  break;
287  }
288 
289  return planstate_tree_walker(planstate, ExecParallelEstimate, e);
290 }
void ExecSortEstimate(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:356
ParallelContext * pcxt
Definition: execParallel.c:111
void ExecIndexOnlyScanEstimate(IndexOnlyScanState *node, ParallelContext *pcxt)
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:226
void ExecHashJoinEstimate(HashJoinState *state, ParallelContext *pcxt)
void ExecHashEstimate(HashState *node, ParallelContext *pcxt)
Definition: nodeHash.c:2544
void ExecAppendEstimate(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:392
void ExecForeignScanEstimate(ForeignScanState *node, ParallelContext *pcxt)
void ExecSeqScanEstimate(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:275
#define nodeTag(nodeptr)
Definition: nodes.h:522
void ExecBitmapHeapEstimate(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecCustomScanEstimate(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:165
void ExecIndexScanEstimate(IndexScanState *node, ParallelContext *pcxt)
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3722

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

Definition at line 997 of file execParallel.c.

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

Referenced by ExecShutdownGatherMergeWorkers(), and ExecShutdownGatherWorkers().

998 {
999  int nworkers = pei->pcxt->nworkers_launched;
1000  int i;
1001 
1002  /* Make this be a no-op if called twice in a row. */
1003  if (pei->finished)
1004  return;
1005 
1006  /*
1007  * Detach from tuple queues ASAP, so that any still-active workers will
1008  * notice that no further results are wanted.
1009  */
1010  if (pei->tqueue != NULL)
1011  {
1012  for (i = 0; i < nworkers; i++)
1013  shm_mq_detach(pei->tqueue[i]);
1014  pfree(pei->tqueue);
1015  pei->tqueue = NULL;
1016  }
1017 
1018  /*
1019  * While we're waiting for the workers to finish, let's get rid of the
1020  * tuple queue readers. (Any other local cleanup could be done here too.)
1021  */
1022  if (pei->reader != NULL)
1023  {
1024  for (i = 0; i < nworkers; i++)
1026  pfree(pei->reader);
1027  pei->reader = NULL;
1028  }
1029 
1030  /* Now wait for the workers to finish. */
1032 
1033  /*
1034  * Next, accumulate buffer usage. (This must wait for the workers to
1035  * finish, or we might get incomplete data.)
1036  */
1037  for (i = 0; i < nworkers; i++)
1039 
1040  pei->finished = true;
1041 }
ParallelContext * pcxt
Definition: execParallel.h:27
void DestroyTupleQueueReader(TupleQueueReader *reader)
Definition: tqueue.c:151
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:793
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:708
void pfree(void *pointer)
Definition: mcxt.c:1031
BufferUsage * buffer_usage
Definition: execParallel.h:28
struct TupleQueueReader ** reader
Definition: execParallel.h:35
void InstrAccumParallelQuery(BufferUsage *result)
Definition: instrument.c:186
int nworkers_launched
Definition: parallel.h:37
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:34

◆ ExecParallelGetQueryDesc()

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

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

1099 {
1100  char *pstmtspace;
1101  char *paramspace;
1102  PlannedStmt *pstmt;
1103  ParamListInfo paramLI;
1104  char *queryString;
1105 
1106  /* Get the query string from shared memory */
1107  queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1108 
1109  /* Reconstruct leader-supplied PlannedStmt. */
1110  pstmtspace = shm_toc_lookup(toc, PARALLEL_KEY_PLANNEDSTMT, false);
1111  pstmt = (PlannedStmt *) stringToNode(pstmtspace);
1112 
1113  /* Reconstruct ParamListInfo. */
1114  paramspace = shm_toc_lookup(toc, PARALLEL_KEY_PARAMLISTINFO, false);
1115  paramLI = RestoreParamList(&paramspace);
1116 
1117  /*
1118  * Create a QueryDesc for the query.
1119  *
1120  * It's not obvious how to obtain the query string from here; and even if
1121  * we could copying it would take more cycles than not copying it. But
1122  * it's a bit unsatisfying to just use a dummy string here, so consider
1123  * revising this someday.
1124  */
1125  return CreateQueryDesc(pstmt,
1126  queryString,
1128  receiver, paramLI, NULL, instrument_options);
1129 }
void * stringToNode(char *str)
Definition: read.c:39
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:839
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:59
#define PARALLEL_KEY_PLANNEDSTMT
Definition: execParallel.c:58
ParamListInfo RestoreParamList(char **start_address)
Definition: params.c:211
#define InvalidSnapshot
Definition: snapshot.h:25
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:64
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 1081 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().

1082 {
1083  char *mqspace;
1084  shm_mq *mq;
1085 
1086  mqspace = shm_toc_lookup(toc, PARALLEL_KEY_TUPLE_QUEUE, false);
1088  mq = (shm_mq *) mqspace;
1090  return CreateTupleQueueDestReceiver(shm_mq_attach(mq, seg, NULL));
1091 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:61
PGPROC * MyProc
Definition: proc.c:67
int ParallelWorkerNumber
Definition: parallel.c:103
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:66
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:216
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:282
Definition: shm_mq.c:70
DestReceiver * CreateTupleQueueDestReceiver(shm_mq_handle *handle)
Definition: tqueue.c:115
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 424 of file execParallel.c.

References ExecAppendInitializeDSM(), ExecBitmapHeapInitializeDSM(), ExecCustomScanInitializeDSM(), ExecForeignScanInitializeDSM(), ExecHashInitializeDSM(), ExecHashJoinInitializeDSM(), 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_AppendState, T_BitmapHeapScanState, T_CustomScanState, T_ForeignScanState, T_HashJoinState, T_HashState, T_IndexOnlyScanState, T_IndexScanState, T_SeqScanState, and T_SortState.

Referenced by ExecInitParallelPlan().

426 {
427  if (planstate == NULL)
428  return false;
429 
430  /* If instrumentation is enabled, initialize slot for this node. */
431  if (d->instrumentation != NULL)
433  planstate->plan->plan_node_id;
434 
435  /* Count this node. */
436  d->nnodes++;
437 
438  /*
439  * Call initializers for DSM-using plan nodes.
440  *
441  * Most plan nodes won't do anything here, but plan nodes that allocated
442  * DSM may need to initialize shared state in the DSM before parallel
443  * workers are launched. They can allocate the space they previously
444  * estimated using shm_toc_allocate, and add the keys they previously
445  * estimated using shm_toc_insert, in each case targeting pcxt->toc.
446  */
447  switch (nodeTag(planstate))
448  {
449  case T_SeqScanState:
450  if (planstate->plan->parallel_aware)
452  d->pcxt);
453  break;
454  case T_IndexScanState:
455  if (planstate->plan->parallel_aware)
457  d->pcxt);
458  break;
460  if (planstate->plan->parallel_aware)
462  d->pcxt);
463  break;
464  case T_ForeignScanState:
465  if (planstate->plan->parallel_aware)
467  d->pcxt);
468  break;
469  case T_AppendState:
470  if (planstate->plan->parallel_aware)
471  ExecAppendInitializeDSM((AppendState *) planstate,
472  d->pcxt);
473  break;
474  case T_CustomScanState:
475  if (planstate->plan->parallel_aware)
477  d->pcxt);
478  break;
480  if (planstate->plan->parallel_aware)
482  d->pcxt);
483  break;
484  case T_HashJoinState:
485  if (planstate->plan->parallel_aware)
487  d->pcxt);
488  break;
489  case T_HashState:
490  /* even when not parallel-aware, for EXPLAIN ANALYZE */
491  ExecHashInitializeDSM((HashState *) planstate, d->pcxt);
492  break;
493  case T_SortState:
494  /* even when not parallel-aware, for EXPLAIN ANALYZE */
495  ExecSortInitializeDSM((SortState *) planstate, d->pcxt);
496  break;
497 
498  default:
499  break;
500  }
501 
502  return planstate_tree_walker(planstate, ExecParallelInitializeDSM, d);
503 }
void ExecIndexScanInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:101
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:119
void ExecSortInitializeDSM(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:377
void ExecSeqScanInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:292
static bool ExecParallelInitializeDSM(PlanState *node, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:424
void ExecBitmapHeapInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecHashInitializeDSM(HashState *node, ParallelContext *pcxt)
Definition: nodeHash.c:2563
void ExecForeignScanInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:178
void ExecAppendInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:411
#define nodeTag(nodeptr)
Definition: nodes.h:522
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:3722

◆ ExecParallelInitializeWorker()

static bool ExecParallelInitializeWorker ( PlanState planstate,
ParallelWorkerContext pwcxt 
)
static

Definition at line 1177 of file execParallel.c.

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

Referenced by ParallelQueryMain().

1178 {
1179  if (planstate == NULL)
1180  return false;
1181 
1182  switch (nodeTag(planstate))
1183  {
1184  case T_SeqScanState:
1185  if (planstate->plan->parallel_aware)
1186  ExecSeqScanInitializeWorker((SeqScanState *) planstate, pwcxt);
1187  break;
1188  case T_IndexScanState:
1189  if (planstate->plan->parallel_aware)
1191  pwcxt);
1192  break;
1193  case T_IndexOnlyScanState:
1194  if (planstate->plan->parallel_aware)
1196  pwcxt);
1197  break;
1198  case T_ForeignScanState:
1199  if (planstate->plan->parallel_aware)
1201  pwcxt);
1202  break;
1203  case T_AppendState:
1204  if (planstate->plan->parallel_aware)
1205  ExecAppendInitializeWorker((AppendState *) planstate, pwcxt);
1206  break;
1207  case T_CustomScanState:
1208  if (planstate->plan->parallel_aware)
1210  pwcxt);
1211  break;
1212  case T_BitmapHeapScanState:
1213  if (planstate->plan->parallel_aware)
1215  pwcxt);
1216  break;
1217  case T_HashJoinState:
1218  if (planstate->plan->parallel_aware)
1220  pwcxt);
1221  break;
1222  case T_HashState:
1223  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1224  ExecHashInitializeWorker((HashState *) planstate, pwcxt);
1225  break;
1226  case T_SortState:
1227  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1228  ExecSortInitializeWorker((SortState *) planstate, pwcxt);
1229  break;
1230 
1231  default:
1232  break;
1233  }
1234 
1236  pwcxt);
1237 }
void ExecForeignScanInitializeWorker(ForeignScanState *node, ParallelWorkerContext *pwcxt)
void ExecAppendInitializeWorker(AppendState *node, ParallelWorkerContext *pwcxt)
Definition: nodeAppend.c:448
void ExecCustomScanInitializeWorker(CustomScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeCustom.c:209
void ExecSortInitializeWorker(SortState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSort.c:402
void ExecIndexScanInitializeWorker(IndexScanState *node, ParallelWorkerContext *pwcxt)
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
bool parallel_aware
Definition: plannodes.h:139
void ExecHashJoinInitializeWorker(HashJoinState *state, ParallelWorkerContext *pwcxt)
Plan * plan
Definition: execnodes.h:912
void ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
#define nodeTag(nodeptr)
Definition: nodes.h:522
void ExecHashInitializeWorker(HashState *node, ParallelWorkerContext *pwcxt)
Definition: nodeHash.c:2585
void ExecBitmapHeapInitializeWorker(BitmapHeapScanState *node, ParallelWorkerContext *pwcxt)
void ExecSeqScanInitializeWorker(SeqScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSeqscan.c:329
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3722

◆ ExecParallelReinitialize()

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

Definition at line 824 of file execParallel.c.

References ParallelExecutorInfo::area, Assert, bms_is_empty(), dsa_free(), DsaPointerIsValid, EState::es_query_dsa, ExecEvalParamExecParams(), ExecParallelReInitializeDSM(), ExecParallelSetupTupleQueues(), ParallelExecutorInfo::finished, 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().

827 {
828  EState *estate = planstate->state;
830 
831  /* Old workers must already be shut down */
832  Assert(pei->finished);
833 
834  /* Force parameters we're going to pass to workers to be evaluated. */
835  ExecEvalParamExecParams(sendParams, estate);
836 
838  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
839  pei->reader = NULL;
840  pei->finished = false;
841 
842  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
843 
844  /* Free any serialized parameters from the last round. */
845  if (DsaPointerIsValid(fpes->param_exec))
846  {
847  dsa_free(pei->area, fpes->param_exec);
849  }
850 
851  /* Serialize current parameter values if required. */
852  if (!bms_is_empty(sendParams))
853  {
854  pei->param_exec = SerializeParamExecParams(estate, sendParams,
855  pei->area);
856  fpes->param_exec = pei->param_exec;
857  }
858 
859  /* Traverse plan tree and let each child node reset associated state. */
860  estate->es_query_dsa = pei->area;
861  ExecParallelReInitializeDSM(planstate, pei->pcxt);
862  estate->es_query_dsa = NULL;
863 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:565
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:510
EState * state
Definition: execnodes.h:914
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:869
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:340
struct TupleQueueReader ** reader
Definition: execParallel.h:35
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:57
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:729
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:434
#define Assert(condition)
Definition: c.h:699
void ExecEvalParamExecParams(Bitmapset *params, EState *estate)
dsa_pointer param_exec
Definition: execParallel.h:31
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:812
shm_mq_handle ** tqueue
Definition: execParallel.h:34
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
shm_toc * toc
Definition: parallel.h:44

◆ ExecParallelReInitializeDSM()

static bool ExecParallelReInitializeDSM ( PlanState planstate,
ParallelContext pcxt 
)
static

Definition at line 869 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_IndexOnlyScanState, T_IndexScanState, T_SeqScanState, and T_SortState.

Referenced by ExecParallelReinitialize().

871 {
872  if (planstate == NULL)
873  return false;
874 
875  /*
876  * Call reinitializers for DSM-using plan nodes.
877  */
878  switch (nodeTag(planstate))
879  {
880  case T_SeqScanState:
881  if (planstate->plan->parallel_aware)
883  pcxt);
884  break;
885  case T_IndexScanState:
886  if (planstate->plan->parallel_aware)
888  pcxt);
889  break;
891  if (planstate->plan->parallel_aware)
893  pcxt);
894  break;
895  case T_ForeignScanState:
896  if (planstate->plan->parallel_aware)
898  pcxt);
899  break;
900  case T_AppendState:
901  if (planstate->plan->parallel_aware)
902  ExecAppendReInitializeDSM((AppendState *) planstate, pcxt);
903  break;
904  case T_CustomScanState:
905  if (planstate->plan->parallel_aware)
907  pcxt);
908  break;
910  if (planstate->plan->parallel_aware)
912  pcxt);
913  break;
914  case T_HashJoinState:
915  if (planstate->plan->parallel_aware)
917  pcxt);
918  break;
919  case T_HashState:
920  case T_SortState:
921  /* these nodes have DSM state, but no reinitialization is required */
922  break;
923 
924  default:
925  break;
926  }
927 
928  return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt);
929 }
void ExecCustomScanReInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:194
void ExecAppendReInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:432
void ExecHashJoinReInitializeDSM(HashJoinState *state, ParallelContext *cxt)
void ExecIndexScanReInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:869
void ExecForeignScanReInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
bool parallel_aware
Definition: plannodes.h:139
void ExecBitmapHeapReInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
Plan * plan
Definition: execnodes.h:912
void ExecIndexOnlyScanReInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
#define nodeTag(nodeptr)
Definition: nodes.h:522
void ExecSeqScanReInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:314
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3722

◆ ExecParallelReportInstrumentation()

static bool ExecParallelReportInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

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

1138 {
1139  int i;
1140  int plan_node_id = planstate->plan->plan_node_id;
1141  Instrumentation *instrument;
1142 
1143  InstrEndLoop(planstate->instrument);
1144 
1145  /*
1146  * If we shuffled the plan_node_id values in ps_instrument into sorted
1147  * order, we could use binary search here. This might matter someday if
1148  * we're pushing down sufficiently large plan trees. For now, do it the
1149  * slow, dumb way.
1150  */
1151  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1152  if (instrumentation->plan_node_id[i] == plan_node_id)
1153  break;
1154  if (i >= instrumentation->num_plan_nodes)
1155  elog(ERROR, "plan node %d not found", plan_node_id);
1156 
1157  /*
1158  * Add our statistics to the per-node, per-worker totals. It's possible
1159  * that this could happen more than once if we relaunched workers.
1160  */
1161  instrument = GetInstrumentationArray(instrumentation);
1162  instrument += i * instrumentation->num_workers;
1164  Assert(ParallelWorkerNumber < instrumentation->num_workers);
1165  InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
1166 
1168  instrumentation);
1169 }
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:101
Instrumentation * instrument
Definition: execnodes.h:922
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:143
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
int plan_node_id
Definition: plannodes.h:145
void InstrEndLoop(Instrumentation *instr)
Definition: instrument.c:114
#define ERROR
Definition: elog.h:43
int ParallelWorkerNumber
Definition: parallel.c:103
#define IsParallelWorker()
Definition: parallel.h:60
Plan * plan
Definition: execnodes.h:912
#define Assert(condition)
Definition: c.h:699
int i
#define elog
Definition: elog.h:219
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3722
#define GetInstrumentationArray(sei)
Definition: execParallel.c:104

◆ ExecParallelRetrieveInstrumentation()

static bool ExecParallelRetrieveInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 936 of file execParallel.c.

References elog, ERROR, EState::es_query_cxt, ExecHashRetrieveInstrumentation(), 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_HashState, T_SortState, and PlanState::worker_instrument.

Referenced by ExecParallelCleanup().

938 {
939  Instrumentation *instrument;
940  int i;
941  int n;
942  int ibytes;
943  int plan_node_id = planstate->plan->plan_node_id;
944  MemoryContext oldcontext;
945 
946  /* Find the instrumentation for this node. */
947  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
948  if (instrumentation->plan_node_id[i] == plan_node_id)
949  break;
950  if (i >= instrumentation->num_plan_nodes)
951  elog(ERROR, "plan node %d not found", plan_node_id);
952 
953  /* Accumulate the statistics from all workers. */
954  instrument = GetInstrumentationArray(instrumentation);
955  instrument += i * instrumentation->num_workers;
956  for (n = 0; n < instrumentation->num_workers; ++n)
957  InstrAggNode(planstate->instrument, &instrument[n]);
958 
959  /*
960  * Also store the per-worker detail.
961  *
962  * Worker instrumentation should be allocated in the same context as the
963  * regular instrumentation information, which is the per-query context.
964  * Switch into per-query memory context.
965  */
966  oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
967  ibytes = mul_size(instrumentation->num_workers, sizeof(Instrumentation));
968  planstate->worker_instrument =
969  palloc(ibytes + offsetof(WorkerInstrumentation, instrument));
970  MemoryContextSwitchTo(oldcontext);
971 
972  planstate->worker_instrument->num_workers = instrumentation->num_workers;
973  memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
974 
975  /* Perform any node-type-specific work that needs to be done. */
976  switch (nodeTag(planstate))
977  {
978  case T_SortState:
980  break;
981  case T_HashState:
983  break;
984  default:
985  break;
986  }
987 
989  instrumentation);
990 }
WorkerInstrumentation * worker_instrument
Definition: execnodes.h:923
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:101
Instrumentation * instrument
Definition: execnodes.h:922
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:143
int plan_node_id
Definition: plannodes.h:145
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
EState * state
Definition: execnodes.h:914
void ExecHashRetrieveInstrumentation(HashState *node)
Definition: nodeHash.c:2616
MemoryContext es_query_cxt
Definition: execnodes.h:523
#define ERROR
Definition: elog.h:43
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:936
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Instrumentation instrument[FLEXIBLE_ARRAY_MEMBER]
Definition: instrument.h:70
Plan * plan
Definition: execnodes.h:912
#define nodeTag(nodeptr)
Definition: nodes.h:522
void * palloc(Size size)
Definition: mcxt.c:924
int i
#define elog
Definition: elog.h:219
void ExecSortRetrieveInstrumentation(SortState *node)
Definition: nodeSort.c:416
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3722
#define offsetof(type, field)
Definition: c.h:622
#define GetInstrumentationArray(sei)
Definition: execParallel.c:104

◆ ExecParallelSetupTupleQueues()

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

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

511 {
512  shm_mq_handle **responseq;
513  char *tqueuespace;
514  int i;
515 
516  /* Skip this if no workers. */
517  if (pcxt->nworkers == 0)
518  return NULL;
519 
520  /* Allocate memory for shared memory queue handles. */
521  responseq = (shm_mq_handle **)
522  palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
523 
524  /*
525  * If not reinitializing, allocate space from the DSM for the queues;
526  * otherwise, find the already allocated space.
527  */
528  if (!reinitialize)
529  tqueuespace =
530  shm_toc_allocate(pcxt->toc,
532  pcxt->nworkers));
533  else
534  tqueuespace = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, false);
535 
536  /* Create the queues, and become the receiver for each. */
537  for (i = 0; i < pcxt->nworkers; ++i)
538  {
539  shm_mq *mq;
540 
541  mq = shm_mq_create(tqueuespace +
543  (Size) PARALLEL_TUPLE_QUEUE_SIZE);
544 
546  responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
547  }
548 
549  /* Add array of queues to shm_toc, so others can find it. */
550  if (!reinitialize)
551  shm_toc_insert(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, tqueuespace);
552 
553  /* Return array of handles. */
554  return responseq;
555 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:61
PGPROC * MyProc
Definition: proc.c:67
dsm_segment * seg
Definition: parallel.h:42
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:169
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:66
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
size_t Size
Definition: c.h:433
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:282
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * palloc(Size size)
Definition: mcxt.c:924
int i
Definition: shm_mq.c:70
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:198
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
shm_toc * toc
Definition: parallel.h:44

◆ ExecSerializePlan()

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

Definition at line 143 of file execParallel.c.

References 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(), PlannedStmt::nonleafResultRelations, 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().

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

◆ ParallelQueryMain()

void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)

Definition at line 1256 of file execParallel.c.

References debug_query_string, dsa_attach_in_place(), dsa_detach(), dsa_get_address(), DsaPointerIsValid, FixedParallelExecutorState::eflags, EState::es_query_dsa, QueryDesc::estate, ExecParallelGetQueryDesc(), ExecParallelGetReceiver(), ExecParallelInitializeWorker(), ExecParallelReportInstrumentation(), ExecSetTupleBound(), ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), ForwardScanDirection, FreeQueryDesc(), InstrEndParallelQuery(), InstrStartParallelQuery(), SharedExecutorInstrumentation::instrument_options, FixedParallelExecutorState::jit_flags, PlannedStmt::jitFlags, PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_DSA, PARALLEL_KEY_EXECUTOR_FIXED, PARALLEL_KEY_INSTRUMENTATION, ParallelWorkerNumber, FixedParallelExecutorState::param_exec, pgstat_report_activity(), QueryDesc::plannedstmt, QueryDesc::planstate, _DestReceiver::rDestroy, RestoreParamExecParams(), ParallelWorkerContext::seg, shm_toc_lookup(), QueryDesc::sourceText, PlanState::state, STATE_RUNNING, ParallelWorkerContext::toc, and FixedParallelExecutorState::tuples_needed.

1257 {
1259  BufferUsage *buffer_usage;
1260  DestReceiver *receiver;
1261  QueryDesc *queryDesc;
1262  SharedExecutorInstrumentation *instrumentation;
1263  int instrument_options = 0;
1264  void *area_space;
1265  dsa_area *area;
1266  ParallelWorkerContext pwcxt;
1267 
1268  /* Get fixed-size state. */
1269  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1270 
1271  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1272  receiver = ExecParallelGetReceiver(seg, toc);
1273  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1274  if (instrumentation != NULL)
1275  instrument_options = instrumentation->instrument_options;
1276  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1277 
1278  /* Setting debug_query_string for individual workers */
1279  debug_query_string = queryDesc->sourceText;
1280 
1281  /* Report workers' query for monitoring purposes */
1283 
1284  /* Prepare to track buffer usage during query execution. */
1286 
1287  /* Attach to the dynamic shared memory area. */
1288  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1289  area = dsa_attach_in_place(area_space, seg);
1290 
1291  /* Start up the executor */
1292  queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1293  ExecutorStart(queryDesc, fpes->eflags);
1294 
1295  /* Special executor initialization steps for parallel workers */
1296  queryDesc->planstate->state->es_query_dsa = area;
1297  if (DsaPointerIsValid(fpes->param_exec))
1298  {
1299  char *paramexec_space;
1300 
1301  paramexec_space = dsa_get_address(area, fpes->param_exec);
1302  RestoreParamExecParams(paramexec_space, queryDesc->estate);
1303 
1304  }
1305  pwcxt.toc = toc;
1306  pwcxt.seg = seg;
1307  ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1308 
1309  /* Pass down any tuple bound */
1310  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1311 
1312  /*
1313  * Run the plan. If we specified a tuple bound, be careful not to demand
1314  * more tuples than that.
1315  */
1316  ExecutorRun(queryDesc,
1318  fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
1319  true);
1320 
1321  /* Shut down the executor */
1322  ExecutorFinish(queryDesc);
1323 
1324  /* Report buffer usage during parallel execution. */
1325  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1327 
1328  /* Report instrumentation data if any instrumentation options are set. */
1329  if (instrumentation != NULL)
1331  instrumentation);
1332 
1333  /* Must do this after capturing instrumentation. */
1334  ExecutorEnd(queryDesc);
1335 
1336  /* Cleanup. */
1337  dsa_detach(area);
1338  FreeQueryDesc(queryDesc);
1339  receiver->rDestroy(receiver);
1340 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:565
EState * estate
Definition: execdesc.h:48
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
void pgstat_report_activity(BackendState state, const char *cmd_str)
Definition: pgstat.c:2994
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:141
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:540
void InstrEndParallelQuery(BufferUsage *result)
Definition: instrument.c:178
EState * state
Definition: execnodes.h:914
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:799
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:461
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:62
PlanState * planstate
Definition: execdesc.h:49
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:299
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
void dsa_detach(dsa_area *area)
Definition: dsa.c:1884
int ParallelWorkerNumber
Definition: parallel.c:103
int jitFlags
Definition: plannodes.h:61
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:57
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:60
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:401
const char * debug_query_string
Definition: postgres.c:87
void InstrStartParallelQuery(void)
Definition: instrument.c:171
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:126
const char * sourceText
Definition: execdesc.h:38
#define DsaPointerIsValid(x)
Definition: dsa.h:81
#define PARALLEL_KEY_DSA
Definition: execParallel.c:63
Definition: dsa.c:354
static DestReceiver * ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
static void RestoreParamExecParams(char *start_address, EState *estate)
Definition: execParallel.c:395
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:52

◆ RestoreParamExecParams()

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

Definition at line 395 of file execParallel.c.

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

Referenced by ParallelQueryMain().

396 {
397  int nparams;
398  int i;
399  int paramid;
400 
401  memcpy(&nparams, start_address, sizeof(int));
402  start_address += sizeof(int);
403 
404  for (i = 0; i < nparams; i++)
405  {
406  ParamExecData *prm;
407 
408  /* Read paramid */
409  memcpy(&paramid, start_address, sizeof(int));
410  start_address += sizeof(int);
411  prm = &(estate->es_param_exec_vals[paramid]);
412 
413  /* Read datum/isnull. */
414  prm->value = datumRestore(&start_address, &prm->isnull);
415  prm->execPlan = NULL;
416  }
417 }
void * execPlan
Definition: params.h:147
ParamExecData * es_param_exec_vals
Definition: execnodes.h:518
bool isnull
Definition: params.h:149
Datum datumRestore(char **start_address, bool *isnull)
Definition: datum.c:360
int i
Datum value
Definition: params.h:148

◆ SerializeParamExecParams()

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

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

341 {
342  Size size;
343  int nparams;
344  int paramid;
345  ParamExecData *prm;
346  dsa_pointer handle;
347  char *start_address;
348 
349  /* Allocate enough space for the current parameter values. */
350  size = EstimateParamExecSpace(estate, params);
351  handle = dsa_allocate(area, size);
352  start_address = dsa_get_address(area, handle);
353 
354  /* First write the number of parameters as a 4-byte integer. */
355  nparams = bms_num_members(params);
356  memcpy(start_address, &nparams, sizeof(int));
357  start_address += sizeof(int);
358 
359  /* Write details for each parameter in turn. */
360  paramid = -1;
361  while ((paramid = bms_next_member(params, paramid)) >= 0)
362  {
363  Oid typeOid;
364  int16 typLen;
365  bool typByVal;
366 
367  prm = &(estate->es_param_exec_vals[paramid]);
368  typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
369  paramid);
370 
371  /* Write paramid. */
372  memcpy(start_address, &paramid, sizeof(int));
373  start_address += sizeof(int);
374 
375  /* Write datum/isnull */
376  if (OidIsValid(typeOid))
377  get_typlenbyval(typeOid, &typLen, &typByVal);
378  else
379  {
380  /* If no type OID, assume by-value, like copyParamList does. */
381  typLen = sizeof(Datum);
382  typByVal = true;
383  }
384  datumSerialize(prm->value, prm->isnull, typByVal, typLen,
385  &start_address);
386  }
387 
388  return handle;
389 }
signed short int16
Definition: c.h:312
List * paramExecTypes
Definition: plannodes.h:94
Oid list_nth_oid(const List *list, int n)
Definition: list.c:432
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1075
PlannedStmt * es_plannedstmt
Definition: execnodes.h:481
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
uint64 dsa_pointer
Definition: dsa.h:62
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
ParamExecData * es_param_exec_vals
Definition: execnodes.h:518
bool isnull
Definition: params.h:149
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:671
static Size EstimateParamExecSpace(EState *estate, Bitmapset *params)
Definition: execParallel.c:296
uintptr_t Datum
Definition: postgres.h:367
size_t Size
Definition: c.h:433
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2005
void datumSerialize(Datum value, bool isnull, bool typByVal, int typLen, char **start_address)
Definition: datum.c:309
Datum value
Definition: params.h:148
#define dsa_allocate(area, size)
Definition: dsa.h:84