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)
 
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:782

Definition at line 99 of file execParallel.c.

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

◆ PARALLEL_KEY_BUFFER_USAGE

#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xE000000000000004)

Definition at line 57 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

◆ PARALLEL_KEY_DSA

#define PARALLEL_KEY_DSA   UINT64CONST(0xE000000000000007)

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

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

◆ PARALLEL_KEY_PARAMLISTINFO

#define PARALLEL_KEY_PARAMLISTINFO   UINT64CONST(0xE000000000000003)

Definition at line 56 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

◆ PARALLEL_KEY_PLANNEDSTMT

#define PARALLEL_KEY_PLANNEDSTMT   UINT64CONST(0xE000000000000002)

Definition at line 55 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

◆ PARALLEL_KEY_QUERY_TEXT

#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xE000000000000008)

Definition at line 61 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ExecParallelGetQueryDesc().

◆ PARALLEL_KEY_TUPLE_QUEUE

#define PARALLEL_KEY_TUPLE_QUEUE   UINT64CONST(0xE000000000000005)

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

278 {
279  int paramid;
280  Size sz = sizeof(int);
281 
282  paramid = -1;
283  while ((paramid = bms_next_member(params, paramid)) >= 0)
284  {
285  Oid typeOid;
286  int16 typLen;
287  bool typByVal;
288  ParamExecData *prm;
289 
290  prm = &(estate->es_param_exec_vals[paramid]);
291  typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
292  paramid);
293 
294  sz = add_size(sz, sizeof(int)); /* space for paramid */
295 
296  /* space for datum/isnull */
297  if (OidIsValid(typeOid))
298  get_typlenbyval(typeOid, &typLen, &typByVal);
299  else
300  {
301  /* If no type OID, assume by-value, like copyParamList does. */
302  typLen = sizeof(Datum);
303  typByVal = true;
304  }
305  sz = add_size(sz,
306  datumEstimateSpace(prm->value, prm->isnull,
307  typByVal, typLen));
308  }
309  return sz;
310 }
signed short int16
Definition: c.h:283
List * paramExecTypes
Definition: plannodes.h:92
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:937
PlannedStmt * es_plannedstmt
Definition: execnodes.h:432
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:576
ParamExecData * es_param_exec_vals
Definition: execnodes.h:466
bool isnull
Definition: params.h:101
Size datumEstimateSpace(Datum value, bool isnull, bool typByVal, int typLen)
Definition: datum.c:262
uintptr_t Datum
Definition: postgres.h:372
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
size_t Size
Definition: c.h:404
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2001
Datum value
Definition: params.h:100

◆ ExecInitParallelPlan()

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

Definition at line 529 of file execParallel.c.

References ParallelExecutorInfo::area, bms_is_empty(), ParallelExecutorInfo::buffer_usage, CreateParallelContext(), dsa_create_in_place(), dsa_minimum_size(), elog, ERROR, EState::es_instrument, EState::es_param_list_info, EState::es_query_dsa, EState::es_sourceText, 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, 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().

532 {
534  ParallelContext *pcxt;
538  char *pstmt_data;
539  char *pstmt_space;
540  char *paramlistinfo_space;
541  BufferUsage *bufusage_space;
542  SharedExecutorInstrumentation *instrumentation = NULL;
543  int pstmt_len;
544  int paramlistinfo_len;
545  int instrumentation_len = 0;
546  int instrument_offset = 0;
547  Size dsa_minsize = dsa_minimum_size();
548  char *query_string;
549  int query_len;
550 
551  /* Force parameters we're going to pass to workers to be evaluated. */
552  ExecEvalParamExecParams(sendParams, estate);
553 
554  /* Allocate object for return value. */
555  pei = palloc0(sizeof(ParallelExecutorInfo));
556  pei->finished = false;
557  pei->planstate = planstate;
558 
559  /* Fix up and serialize plan to be sent to workers. */
560  pstmt_data = ExecSerializePlan(planstate->plan, estate);
561 
562  /* Create a parallel context. */
563  pcxt = CreateParallelContext("postgres", "ParallelQueryMain", nworkers);
564  pei->pcxt = pcxt;
565 
566  /*
567  * Before telling the parallel context to create a dynamic shared memory
568  * segment, we need to figure out how big it should be. Estimate space
569  * for the various things we need to store.
570  */
571 
572  /* Estimate space for fixed-size state. */
575  shm_toc_estimate_keys(&pcxt->estimator, 1);
576 
577  /* Estimate space for query text. */
578  query_len = strlen(estate->es_sourceText);
579  shm_toc_estimate_chunk(&pcxt->estimator, query_len);
580  shm_toc_estimate_keys(&pcxt->estimator, 1);
581 
582  /* Estimate space for serialized PlannedStmt. */
583  pstmt_len = strlen(pstmt_data) + 1;
584  shm_toc_estimate_chunk(&pcxt->estimator, pstmt_len);
585  shm_toc_estimate_keys(&pcxt->estimator, 1);
586 
587  /* Estimate space for serialized ParamListInfo. */
588  paramlistinfo_len = EstimateParamListSpace(estate->es_param_list_info);
589  shm_toc_estimate_chunk(&pcxt->estimator, paramlistinfo_len);
590  shm_toc_estimate_keys(&pcxt->estimator, 1);
591 
592  /*
593  * Estimate space for BufferUsage.
594  *
595  * If EXPLAIN is not in use and there are no extensions loaded that care,
596  * we could skip this. But we have no way of knowing whether anyone's
597  * looking at pgBufferUsage, so do it unconditionally.
598  */
600  mul_size(sizeof(BufferUsage), pcxt->nworkers));
601  shm_toc_estimate_keys(&pcxt->estimator, 1);
602 
603  /* Estimate space for tuple queues. */
606  shm_toc_estimate_keys(&pcxt->estimator, 1);
607 
608  /*
609  * Give parallel-aware nodes a chance to add to the estimates, and get a
610  * count of how many PlanState nodes there are.
611  */
612  e.pcxt = pcxt;
613  e.nnodes = 0;
614  ExecParallelEstimate(planstate, &e);
615 
616  /* Estimate space for instrumentation, if required. */
617  if (estate->es_instrument)
618  {
619  instrumentation_len =
620  offsetof(SharedExecutorInstrumentation, plan_node_id) +
621  sizeof(int) * e.nnodes;
622  instrumentation_len = MAXALIGN(instrumentation_len);
623  instrument_offset = instrumentation_len;
624  instrumentation_len +=
625  mul_size(sizeof(Instrumentation),
626  mul_size(e.nnodes, nworkers));
627  shm_toc_estimate_chunk(&pcxt->estimator, instrumentation_len);
628  shm_toc_estimate_keys(&pcxt->estimator, 1);
629  }
630 
631  /* Estimate space for DSA area. */
632  shm_toc_estimate_chunk(&pcxt->estimator, dsa_minsize);
633  shm_toc_estimate_keys(&pcxt->estimator, 1);
634 
635  /* Everyone's had a chance to ask for space, so now create the DSM. */
636  InitializeParallelDSM(pcxt);
637 
638  /*
639  * OK, now we have a dynamic shared memory segment, and it should be big
640  * enough to store all of the data we estimated we would want to put into
641  * it, plus whatever general stuff (not specifically executor-related) the
642  * ParallelContext itself needs to store there. None of the space we
643  * asked for has been allocated or initialized yet, though, so do that.
644  */
645 
646  /* Store fixed-size state. */
647  fpes = shm_toc_allocate(pcxt->toc, sizeof(FixedParallelExecutorState));
648  fpes->tuples_needed = tuples_needed;
651 
652  /* Store query string */
653  query_string = shm_toc_allocate(pcxt->toc, query_len);
654  memcpy(query_string, estate->es_sourceText, query_len);
655  shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string);
656 
657  /* Store serialized PlannedStmt. */
658  pstmt_space = shm_toc_allocate(pcxt->toc, pstmt_len);
659  memcpy(pstmt_space, pstmt_data, pstmt_len);
660  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PLANNEDSTMT, pstmt_space);
661 
662  /* Store serialized ParamListInfo. */
663  paramlistinfo_space = shm_toc_allocate(pcxt->toc, paramlistinfo_len);
664  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PARAMLISTINFO, paramlistinfo_space);
665  SerializeParamList(estate->es_param_list_info, &paramlistinfo_space);
666 
667  /* Allocate space for each worker's BufferUsage; no need to initialize. */
668  bufusage_space = shm_toc_allocate(pcxt->toc,
669  mul_size(sizeof(BufferUsage), pcxt->nworkers));
670  shm_toc_insert(pcxt->toc, PARALLEL_KEY_BUFFER_USAGE, bufusage_space);
671  pei->buffer_usage = bufusage_space;
672 
673  /* Set up the tuple queues that the workers will write into. */
674  pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
675 
676  /* We don't need the TupleQueueReaders yet, though. */
677  pei->reader = NULL;
678 
679  /*
680  * If instrumentation options were supplied, allocate space for the data.
681  * It only gets partially initialized here; the rest happens during
682  * ExecParallelInitializeDSM.
683  */
684  if (estate->es_instrument)
685  {
686  Instrumentation *instrument;
687  int i;
688 
689  instrumentation = shm_toc_allocate(pcxt->toc, instrumentation_len);
690  instrumentation->instrument_options = estate->es_instrument;
691  instrumentation->instrument_offset = instrument_offset;
692  instrumentation->num_workers = nworkers;
693  instrumentation->num_plan_nodes = e.nnodes;
694  instrument = GetInstrumentationArray(instrumentation);
695  for (i = 0; i < nworkers * e.nnodes; ++i)
696  InstrInit(&instrument[i], estate->es_instrument);
698  instrumentation);
699  pei->instrumentation = instrumentation;
700  }
701 
702  /*
703  * Create a DSA area that can be used by the leader and all workers.
704  * (However, if we failed to create a DSM and are using private memory
705  * instead, then skip this.)
706  */
707  if (pcxt->seg != NULL)
708  {
709  char *area_space;
710 
711  area_space = shm_toc_allocate(pcxt->toc, dsa_minsize);
712  shm_toc_insert(pcxt->toc, PARALLEL_KEY_DSA, area_space);
713  pei->area = dsa_create_in_place(area_space, dsa_minsize,
715  pcxt->seg);
716 
717  /*
718  * Make the area available to executor nodes running in the leader.
719  * See also ParallelQueryMain which makes it available to workers.
720  */
721  estate->es_query_dsa = pei->area;
722 
723  /*
724  * Serialize parameters, if any, using DSA storage. We don't dare use
725  * the main parallel query DSM for this because we might relaunch
726  * workers after the values have changed (and thus the amount of
727  * storage required has changed).
728  */
729  if (!bms_is_empty(sendParams))
730  {
731  pei->param_exec = SerializeParamExecParams(estate, sendParams);
732  fpes->param_exec = pei->param_exec;
733  }
734  }
735 
736  /*
737  * Give parallel-aware nodes a chance to initialize their shared data.
738  * This also initializes the elements of instrumentation->ps_instrument,
739  * if it exists.
740  */
741  d.pcxt = pcxt;
742  d.instrumentation = instrumentation;
743  d.nnodes = 0;
744  ExecParallelInitializeDSM(planstate, &d);
745 
746  /*
747  * Make sure that the world hasn't shifted under our feet. This could
748  * probably just be an Assert(), but let's be conservative for now.
749  */
750  if (e.nnodes != d.nnodes)
751  elog(ERROR, "inconsistent count of PlanState nodes");
752 
753  /* OK, we're ready to rock and roll. */
754  return pei;
755 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:513
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
Definition: parallel.c:142
dsm_segment * seg
Definition: parallel.h:42
shm_toc_estimator estimator
Definition: parallel.h:41
PlanState * planstate
Definition: execParallel.h:26
static char * ExecSerializePlan(Plan *plan, EState *estate)
Definition: execParallel.c:138
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:477
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
Size EstimateParamListSpace(ParamListInfo paramLI)
Definition: params.c:95
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:114
ParallelContext * pcxt
Definition: execParallel.c:106
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:59
const char * es_sourceText
Definition: execnodes.h:433
static bool ExecParallelInitializeDSM(PlanState *node, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:405
#define ERROR
Definition: elog.h:43
BufferUsage * buffer_usage
Definition: execParallel.h:28
#define PARALLEL_KEY_PARAMLISTINFO
Definition: execParallel.c:56
#define PARALLEL_KEY_PLANNEDSTMT
Definition: execParallel.c:55
struct TupleQueueReader ** reader
Definition: execParallel.h:35
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:63
int es_instrument
Definition: execnodes.h:481
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:54
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:221
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:57
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
void InitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:194
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:877
Plan * plan
Definition: execnodes.h:849
Size dsa_minimum_size(void)
Definition: dsa.c:1160
void SerializeParamList(ParamListInfo paramLI, char **start_address)
Definition: params.c:158
void ExecEvalParamExecParams(Bitmapset *params, EState *estate)
size_t Size
Definition: c.h:404
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define MAXALIGN(LEN)
Definition: c.h:623
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:60
e
Definition: preproc-init.c:82
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
int i
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:61
ParamListInfo es_param_list_info
Definition: execnodes.h:465
shm_mq_handle ** tqueue
Definition: execParallel.h:34
#define elog
Definition: elog.h:219
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params)
Definition: execParallel.c:321
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:29
#define offsetof(type, field)
Definition: c.h:593
#define GetInstrumentationArray(sei)
Definition: execParallel.c:99
shm_toc * toc
Definition: parallel.h:44

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

Definition at line 1003 of file execParallel.c.

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

Referenced by ExecShutdownGather(), and ExecShutdownGatherMerge().

1004 {
1005  /* Free any serialized parameters. */
1006  if (DsaPointerIsValid(pei->param_exec))
1007  {
1008  dsa_free(pei->area, pei->param_exec);
1010  }
1011  if (pei->area != NULL)
1012  {
1013  dsa_detach(pei->area);
1014  pei->area = NULL;
1015  }
1016  if (pei->pcxt != NULL)
1017  {
1019  pei->pcxt = NULL;
1020  }
1021  pfree(pei);
1022 }
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:628
void pfree(void *pointer)
Definition: mcxt.c:949
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

◆ ExecParallelCreateReaders()

void ExecParallelCreateReaders ( ParallelExecutorInfo pei)

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

765 {
766  int nworkers = pei->pcxt->nworkers_launched;
767  int i;
768 
769  Assert(pei->reader == NULL);
770 
771  if (nworkers > 0)
772  {
773  pei->reader = (TupleQueueReader **)
774  palloc(nworkers * sizeof(TupleQueueReader *));
775 
776  for (i = 0; i < nworkers; i++)
777  {
778  shm_mq_set_handle(pei->tqueue[i],
779  pei->pcxt->worker[i].bgwhandle);
780  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
781  }
782  }
783 }
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:315
#define Assert(condition)
Definition: c.h:670
void * palloc(Size size)
Definition: mcxt.c:848
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:34

◆ ExecParallelEstimate()

static bool ExecParallelEstimate ( PlanState node,
ExecParallelEstimateContext e 
)
static

Definition at line 221 of file execParallel.c.

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

Referenced by ExecInitParallelPlan().

222 {
223  if (planstate == NULL)
224  return false;
225 
226  /* Count this node. */
227  e->nnodes++;
228 
229  switch (nodeTag(planstate))
230  {
231  case T_SeqScanState:
232  if (planstate->plan->parallel_aware)
233  ExecSeqScanEstimate((SeqScanState *) planstate,
234  e->pcxt);
235  break;
236  case T_IndexScanState:
237  if (planstate->plan->parallel_aware)
239  e->pcxt);
240  break;
242  if (planstate->plan->parallel_aware)
244  e->pcxt);
245  break;
246  case T_ForeignScanState:
247  if (planstate->plan->parallel_aware)
249  e->pcxt);
250  break;
251  case T_CustomScanState:
252  if (planstate->plan->parallel_aware)
254  e->pcxt);
255  break;
257  if (planstate->plan->parallel_aware)
259  e->pcxt);
260  break;
261  case T_SortState:
262  /* even when not parallel-aware */
263  ExecSortEstimate((SortState *) planstate, e->pcxt);
264  break;
265 
266  default:
267  break;
268  }
269 
270  return planstate_tree_walker(planstate, ExecParallelEstimate, e);
271 }
void ExecSortEstimate(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:360
ParallelContext * pcxt
Definition: execParallel.c:106
void ExecIndexOnlyScanEstimate(IndexOnlyScanState *node, ParallelContext *pcxt)
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:221
void ExecForeignScanEstimate(ForeignScanState *node, ParallelContext *pcxt)
void ExecSeqScanEstimate(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:297
#define nodeTag(nodeptr)
Definition: nodes.h:515
void ExecBitmapHeapEstimate(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecCustomScanEstimate(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:169
void ExecIndexScanEstimate(IndexScanState *node, ParallelContext *pcxt)
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

Definition at line 945 of file execParallel.c.

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

Referenced by ExecShutdownGatherMergeWorkers(), and ExecShutdownGatherWorkers().

946 {
947  int nworkers = pei->pcxt->nworkers_launched;
948  int i;
949 
950  /* Make this be a no-op if called twice in a row. */
951  if (pei->finished)
952  return;
953 
954  /*
955  * Detach from tuple queues ASAP, so that any still-active workers will
956  * notice that no further results are wanted.
957  */
958  if (pei->tqueue != NULL)
959  {
960  for (i = 0; i < nworkers; i++)
961  shm_mq_detach(pei->tqueue[i]);
962  pfree(pei->tqueue);
963  pei->tqueue = NULL;
964  }
965 
966  /*
967  * While we're waiting for the workers to finish, let's get rid of the
968  * tuple queue readers. (Any other local cleanup could be done here too.)
969  */
970  if (pei->reader != NULL)
971  {
972  for (i = 0; i < nworkers; i++)
974  pfree(pei->reader);
975  pei->reader = NULL;
976  }
977 
978  /* Now wait for the workers to finish. */
980 
981  /*
982  * Next, accumulate buffer usage. (This must wait for the workers to
983  * finish, or we might get incomplete data.)
984  */
985  for (i = 0; i < nworkers; i++)
987 
988  /* Finally, accumulate instrumentation, if any. */
989  if (pei->instrumentation)
991  pei->instrumentation);
992 
993  pei->finished = true;
994 }
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:775
PlanState * planstate
Definition: execParallel.h:26
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:538
void pfree(void *pointer)
Definition: mcxt.c:949
BufferUsage * buffer_usage
Definition: execParallel.h:28
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:890
struct TupleQueueReader ** reader
Definition: execParallel.h:35
void InstrAccumParallelQuery(BufferUsage *result)
Definition: instrument.c:185
int nworkers_launched
Definition: parallel.h:37
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:34
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:29

◆ ExecParallelGetQueryDesc()

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

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

1047 {
1048  char *pstmtspace;
1049  char *paramspace;
1050  PlannedStmt *pstmt;
1051  ParamListInfo paramLI;
1052  char *queryString;
1053 
1054  /* Get the query string from shared memory */
1055  queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1056 
1057  /* Reconstruct leader-supplied PlannedStmt. */
1058  pstmtspace = shm_toc_lookup(toc, PARALLEL_KEY_PLANNEDSTMT, false);
1059  pstmt = (PlannedStmt *) stringToNode(pstmtspace);
1060 
1061  /* Reconstruct ParamListInfo. */
1062  paramspace = shm_toc_lookup(toc, PARALLEL_KEY_PARAMLISTINFO, false);
1063  paramLI = RestoreParamList(&paramspace);
1064 
1065  /*
1066  * Create a QueryDesc for the query.
1067  *
1068  * It's not obvious how to obtain the query string from here; and even if
1069  * we could copying it would take more cycles than not copying it. But
1070  * it's a bit unsatisfying to just use a dummy string here, so consider
1071  * revising this someday.
1072  */
1073  return CreateQueryDesc(pstmt,
1074  queryString,
1076  receiver, paramLI, NULL, instrument_options);
1077 }
void * stringToNode(char *str)
Definition: read.c:38
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:56
#define PARALLEL_KEY_PLANNEDSTMT
Definition: execParallel.c:55
ParamListInfo RestoreParamList(char **start_address)
Definition: params.c:224
#define InvalidSnapshot
Definition: snapshot.h:25
#define PARALLEL_KEY_QUERY_TEXT
Definition: execParallel.c:61
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 1029 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().

1030 {
1031  char *mqspace;
1032  shm_mq *mq;
1033 
1034  mqspace = shm_toc_lookup(toc, PARALLEL_KEY_TUPLE_QUEUE, false);
1036  mq = (shm_mq *) mqspace;
1038  return CreateTupleQueueDestReceiver(shm_mq_attach(mq, seg, NULL));
1039 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:58
PGPROC * MyProc
Definition: proc.c:67
int ParallelWorkerNumber
Definition: parallel.c:100
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:63
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:218
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:287
Definition: shm_mq.c:69
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 405 of file execParallel.c.

References ExecBitmapHeapInitializeDSM(), ExecCustomScanInitializeDSM(), ExecForeignScanInitializeDSM(), 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_BitmapHeapScanState, T_CustomScanState, T_ForeignScanState, T_IndexOnlyScanState, T_IndexScanState, T_SeqScanState, and T_SortState.

Referenced by ExecInitParallelPlan().

407 {
408  if (planstate == NULL)
409  return false;
410 
411  /* If instrumentation is enabled, initialize slot for this node. */
412  if (d->instrumentation != NULL)
414  planstate->plan->plan_node_id;
415 
416  /* Count this node. */
417  d->nnodes++;
418 
419  /*
420  * Call initializers for DSM-using plan nodes.
421  *
422  * Most plan nodes won't do anything here, but plan nodes that allocated
423  * DSM may need to initialize shared state in the DSM before parallel
424  * workers are launched. They can allocate the space they previously
425  * estimated using shm_toc_allocate, and add the keys they previously
426  * estimated using shm_toc_insert, in each case targeting pcxt->toc.
427  */
428  switch (nodeTag(planstate))
429  {
430  case T_SeqScanState:
431  if (planstate->plan->parallel_aware)
433  d->pcxt);
434  break;
435  case T_IndexScanState:
436  if (planstate->plan->parallel_aware)
438  d->pcxt);
439  break;
441  if (planstate->plan->parallel_aware)
443  d->pcxt);
444  break;
445  case T_ForeignScanState:
446  if (planstate->plan->parallel_aware)
448  d->pcxt);
449  break;
450  case T_CustomScanState:
451  if (planstate->plan->parallel_aware)
453  d->pcxt);
454  break;
456  if (planstate->plan->parallel_aware)
458  d->pcxt);
459  break;
460  case T_SortState:
461  /* even when not parallel-aware */
462  ExecSortInitializeDSM((SortState *) planstate, d->pcxt);
463  break;
464 
465  default:
466  break;
467  }
468 
469  return planstate_tree_walker(planstate, ExecParallelInitializeDSM, d);
470 }
void ExecIndexScanInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:96
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:114
void ExecSortInitializeDSM(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:381
void ExecSeqScanInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:314
static bool ExecParallelInitializeDSM(PlanState *node, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:405
void ExecBitmapHeapInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecForeignScanInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:182
#define nodeTag(nodeptr)
Definition: nodes.h:515
void ExecIndexOnlyScanInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697

◆ ExecParallelInitializeWorker()

static bool ExecParallelInitializeWorker ( PlanState planstate,
ParallelWorkerContext pwcxt 
)
static

Definition at line 1125 of file execParallel.c.

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

Referenced by ParallelQueryMain().

1126 {
1127  if (planstate == NULL)
1128  return false;
1129 
1130  switch (nodeTag(planstate))
1131  {
1132  case T_SeqScanState:
1133  if (planstate->plan->parallel_aware)
1134  ExecSeqScanInitializeWorker((SeqScanState *) planstate, pwcxt);
1135  break;
1136  case T_IndexScanState:
1137  if (planstate->plan->parallel_aware)
1139  pwcxt);
1140  break;
1141  case T_IndexOnlyScanState:
1142  if (planstate->plan->parallel_aware)
1144  pwcxt);
1145  break;
1146  case T_ForeignScanState:
1147  if (planstate->plan->parallel_aware)
1149  pwcxt);
1150  break;
1151  case T_CustomScanState:
1152  if (planstate->plan->parallel_aware)
1154  pwcxt);
1155  break;
1156  case T_BitmapHeapScanState:
1157  if (planstate->plan->parallel_aware)
1159  pwcxt);
1160  break;
1161  case T_SortState:
1162  /* even when not parallel-aware */
1163  ExecSortInitializeWorker((SortState *) planstate, pwcxt);
1164  break;
1165 
1166  default:
1167  break;
1168  }
1169 
1171  pwcxt);
1172 }
void ExecForeignScanInitializeWorker(ForeignScanState *node, ParallelWorkerContext *pwcxt)
void ExecCustomScanInitializeWorker(CustomScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeCustom.c:213
void ExecSortInitializeWorker(SortState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSort.c:423
void ExecIndexScanInitializeWorker(IndexScanState *node, ParallelWorkerContext *pwcxt)
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
bool parallel_aware
Definition: plannodes.h:137
Plan * plan
Definition: execnodes.h:849
void ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
#define nodeTag(nodeptr)
Definition: nodes.h:515
void ExecBitmapHeapInitializeWorker(BitmapHeapScanState *node, ParallelWorkerContext *pwcxt)
void ExecSeqScanInitializeWorker(SeqScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSeqscan.c:351
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697

◆ ExecParallelReinitialize()

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

Definition at line 790 of file execParallel.c.

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

793 {
794  EState *estate = planstate->state;
796 
797  /* Old workers must already be shut down */
798  Assert(pei->finished);
799 
800  /* Force parameters we're going to pass to workers to be evaluated. */
801  ExecEvalParamExecParams(sendParams, estate);
802 
804  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
805  pei->reader = NULL;
806  pei->finished = false;
807 
808  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
809 
810  /* Free any serialized parameters from the last round. */
811  if (DsaPointerIsValid(fpes->param_exec))
812  {
813  dsa_free(estate->es_query_dsa, fpes->param_exec);
815  }
816 
817  /* Serialize current parameter values if required. */
818  if (!bms_is_empty(sendParams))
819  {
820  pei->param_exec = SerializeParamExecParams(estate, sendParams);
821  fpes->param_exec = pei->param_exec;
822  }
823 
824  /* Traverse plan tree and let each child node reset associated state. */
825  ExecParallelReInitializeDSM(planstate, pei->pcxt);
826 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:513
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:477
EState * state
Definition: execnodes.h:851
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:832
struct TupleQueueReader ** reader
Definition: execParallel.h:35
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:54
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:413
#define Assert(condition)
Definition: c.h:670
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
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params)
Definition: execParallel.c:321
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 832 of file execParallel.c.

References ExecBitmapHeapReInitializeDSM(), ExecCustomScanReInitializeDSM(), ExecForeignScanReInitializeDSM(), ExecIndexOnlyScanReInitializeDSM(), ExecIndexScanReInitializeDSM(), ExecSeqScanReInitializeDSM(), ExecSortReInitializeDSM(), nodeTag, Plan::parallel_aware, PlanState::plan, planstate_tree_walker(), T_BitmapHeapScanState, T_CustomScanState, T_ForeignScanState, T_IndexOnlyScanState, T_IndexScanState, T_SeqScanState, and T_SortState.

Referenced by ExecParallelReinitialize().

834 {
835  if (planstate == NULL)
836  return false;
837 
838  /*
839  * Call reinitializers for DSM-using plan nodes.
840  */
841  switch (nodeTag(planstate))
842  {
843  case T_SeqScanState:
844  if (planstate->plan->parallel_aware)
846  pcxt);
847  break;
848  case T_IndexScanState:
849  if (planstate->plan->parallel_aware)
851  pcxt);
852  break;
854  if (planstate->plan->parallel_aware)
856  pcxt);
857  break;
858  case T_ForeignScanState:
859  if (planstate->plan->parallel_aware)
861  pcxt);
862  break;
863  case T_CustomScanState:
864  if (planstate->plan->parallel_aware)
866  pcxt);
867  break;
869  if (planstate->plan->parallel_aware)
871  pcxt);
872  break;
873  case T_SortState:
874  /* even when not parallel-aware */
875  ExecSortReInitializeDSM((SortState *) planstate, pcxt);
876  break;
877 
878  default:
879  break;
880  }
881 
882  return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt);
883 }
void ExecCustomScanReInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:198
void ExecIndexScanReInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:832
void ExecForeignScanReInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecSortReInitializeDSM(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:406
bool parallel_aware
Definition: plannodes.h:137
void ExecBitmapHeapReInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
Plan * plan
Definition: execnodes.h:849
void ExecIndexOnlyScanReInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
#define nodeTag(nodeptr)
Definition: nodes.h:515
void ExecSeqScanReInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:336
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697

◆ ExecParallelReportInstrumentation()

static bool ExecParallelReportInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

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

1086 {
1087  int i;
1088  int plan_node_id = planstate->plan->plan_node_id;
1089  Instrumentation *instrument;
1090 
1091  InstrEndLoop(planstate->instrument);
1092 
1093  /*
1094  * If we shuffled the plan_node_id values in ps_instrument into sorted
1095  * order, we could use binary search here. This might matter someday if
1096  * we're pushing down sufficiently large plan trees. For now, do it the
1097  * slow, dumb way.
1098  */
1099  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1100  if (instrumentation->plan_node_id[i] == plan_node_id)
1101  break;
1102  if (i >= instrumentation->num_plan_nodes)
1103  elog(ERROR, "plan node %d not found", plan_node_id);
1104 
1105  /*
1106  * Add our statistics to the per-node, per-worker totals. It's possible
1107  * that this could happen more than once if we relaunched workers.
1108  */
1109  instrument = GetInstrumentationArray(instrumentation);
1110  instrument += i * instrumentation->num_workers;
1112  Assert(ParallelWorkerNumber < instrumentation->num_workers);
1113  InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
1114 
1116  instrumentation);
1117 }
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:96
Instrumentation * instrument
Definition: execnodes.h:859
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:143
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
int plan_node_id
Definition: plannodes.h:143
void InstrEndLoop(Instrumentation *instr)
Definition: instrument.c:114
#define ERROR
Definition: elog.h:43
int ParallelWorkerNumber
Definition: parallel.c:100
#define IsParallelWorker()
Definition: parallel.h:58
Plan * plan
Definition: execnodes.h:849
#define Assert(condition)
Definition: c.h:670
int i
#define elog
Definition: elog.h:219
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697
#define GetInstrumentationArray(sei)
Definition: execParallel.c:99

◆ ExecParallelRetrieveInstrumentation()

static bool ExecParallelRetrieveInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 890 of file execParallel.c.

References elog, ERROR, EState::es_query_cxt, ExecSortRetrieveInstrumentation(), GetInstrumentationArray, i, InstrAggNode(), WorkerInstrumentation::instrument, PlanState::instrument, IsA, MemoryContextSwitchTo(), mul_size(), 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, and PlanState::worker_instrument.

Referenced by ExecParallelFinish().

892 {
893  Instrumentation *instrument;
894  int i;
895  int n;
896  int ibytes;
897  int plan_node_id = planstate->plan->plan_node_id;
898  MemoryContext oldcontext;
899 
900  /* Find the instrumentation for this node. */
901  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
902  if (instrumentation->plan_node_id[i] == plan_node_id)
903  break;
904  if (i >= instrumentation->num_plan_nodes)
905  elog(ERROR, "plan node %d not found", plan_node_id);
906 
907  /* Accumulate the statistics from all workers. */
908  instrument = GetInstrumentationArray(instrumentation);
909  instrument += i * instrumentation->num_workers;
910  for (n = 0; n < instrumentation->num_workers; ++n)
911  InstrAggNode(planstate->instrument, &instrument[n]);
912 
913  /*
914  * Also store the per-worker detail.
915  *
916  * Worker instrumentation should be allocated in the same context as the
917  * regular instrumentation information, which is the per-query context.
918  * Switch into per-query memory context.
919  */
920  oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
921  ibytes = mul_size(instrumentation->num_workers, sizeof(Instrumentation));
922  planstate->worker_instrument =
923  palloc(ibytes + offsetof(WorkerInstrumentation, instrument));
924  MemoryContextSwitchTo(oldcontext);
925 
926  planstate->worker_instrument->num_workers = instrumentation->num_workers;
927  memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
928 
929  /*
930  * Perform any node-type-specific work that needs to be done. Currently,
931  * only Sort nodes need to do anything here.
932  */
933  if (IsA(planstate, SortState))
935 
937  instrumentation);
938 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
WorkerInstrumentation * worker_instrument
Definition: execnodes.h:860
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:96
Instrumentation * instrument
Definition: execnodes.h:859
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:143
int plan_node_id
Definition: plannodes.h:143
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
EState * state
Definition: execnodes.h:851
MemoryContext es_query_cxt
Definition: execnodes.h:471
#define ERROR
Definition: elog.h:43
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:890
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Instrumentation instrument[FLEXIBLE_ARRAY_MEMBER]
Definition: instrument.h:69
Plan * plan
Definition: execnodes.h:849
void * palloc(Size size)
Definition: mcxt.c:848
int i
#define elog
Definition: elog.h:219
void ExecSortRetrieveInstrumentation(SortState *node)
Definition: nodeSort.c:437
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697
#define offsetof(type, field)
Definition: c.h:593
#define GetInstrumentationArray(sei)
Definition: execParallel.c:99

◆ ExecParallelSetupTupleQueues()

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

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

478 {
479  shm_mq_handle **responseq;
480  char *tqueuespace;
481  int i;
482 
483  /* Skip this if no workers. */
484  if (pcxt->nworkers == 0)
485  return NULL;
486 
487  /* Allocate memory for shared memory queue handles. */
488  responseq = (shm_mq_handle **)
489  palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
490 
491  /*
492  * If not reinitializing, allocate space from the DSM for the queues;
493  * otherwise, find the already allocated space.
494  */
495  if (!reinitialize)
496  tqueuespace =
497  shm_toc_allocate(pcxt->toc,
499  pcxt->nworkers));
500  else
501  tqueuespace = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, false);
502 
503  /* Create the queues, and become the receiver for each. */
504  for (i = 0; i < pcxt->nworkers; ++i)
505  {
506  shm_mq *mq;
507 
508  mq = shm_mq_create(tqueuespace +
510  (Size) PARALLEL_TUPLE_QUEUE_SIZE);
511 
513  responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
514  }
515 
516  /* Add array of queues to shm_toc, so others can find it. */
517  if (!reinitialize)
518  shm_toc_insert(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, tqueuespace);
519 
520  /* Return array of handles. */
521  return responseq;
522 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:58
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:170
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:63
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
size_t Size
Definition: c.h:404
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:287
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * palloc(Size size)
Definition: mcxt.c:848
int i
Definition: shm_mq.c:69
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:44

◆ ExecSerializePlan()

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

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

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

◆ ParallelQueryMain()

void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)

Definition at line 1191 of file execParallel.c.

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

1192 {
1194  BufferUsage *buffer_usage;
1195  DestReceiver *receiver;
1196  QueryDesc *queryDesc;
1197  SharedExecutorInstrumentation *instrumentation;
1198  int instrument_options = 0;
1199  void *area_space;
1200  dsa_area *area;
1201  ParallelWorkerContext pwcxt;
1202 
1203  /* Get fixed-size state. */
1204  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1205 
1206  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1207  receiver = ExecParallelGetReceiver(seg, toc);
1208  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1209  if (instrumentation != NULL)
1210  instrument_options = instrumentation->instrument_options;
1211  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1212 
1213  /* Setting debug_query_string for individual workers */
1214  debug_query_string = queryDesc->sourceText;
1215 
1216  /* Report workers' query for monitoring purposes */
1218 
1219  /* Prepare to track buffer usage during query execution. */
1221 
1222  /* Attach to the dynamic shared memory area. */
1223  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1224  area = dsa_attach_in_place(area_space, seg);
1225 
1226  /* Start up the executor */
1227  ExecutorStart(queryDesc, 0);
1228 
1229  /* Special executor initialization steps for parallel workers */
1230  queryDesc->planstate->state->es_query_dsa = area;
1231  if (DsaPointerIsValid(fpes->param_exec))
1232  {
1233  char *paramexec_space;
1234 
1235  paramexec_space = dsa_get_address(area, fpes->param_exec);
1236  RestoreParamExecParams(paramexec_space, queryDesc->estate);
1237 
1238  }
1239  pwcxt.toc = toc;
1240  pwcxt.seg = seg;
1241  ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1242 
1243  /* Pass down any tuple bound */
1244  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1245 
1246  /*
1247  * Run the plan. If we specified a tuple bound, be careful not to demand
1248  * more tuples than that.
1249  */
1250  ExecutorRun(queryDesc,
1252  fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
1253  true);
1254 
1255  /* Shut down the executor */
1256  ExecutorFinish(queryDesc);
1257 
1258  /* Report buffer usage during parallel execution. */
1259  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1261 
1262  /* Report instrumentation data if any instrumentation options are set. */
1263  if (instrumentation != NULL)
1265  instrumentation);
1266 
1267  /* Must do this after capturing instrumentation. */
1268  ExecutorEnd(queryDesc);
1269 
1270  /* Cleanup. */
1271  dsa_detach(area);
1272  FreeQueryDesc(queryDesc);
1273  receiver->rDestroy(receiver);
1274 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:513
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:140
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:540
void InstrEndParallelQuery(BufferUsage *result)
Definition: instrument.c:177
EState * state
Definition: execnodes.h:851
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:778
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:459
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:59
PlanState * planstate
Definition: execdesc.h:49
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:297
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
void dsa_detach(dsa_area *area)
Definition: dsa.c:1884
int ParallelWorkerNumber
Definition: parallel.c:100
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:54
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:57
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:399
const char * debug_query_string
Definition: postgres.c:85
void InstrStartParallelQuery(void)
Definition: instrument.c:170
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:60
Definition: dsa.c:354
static DestReceiver * ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
static void RestoreParamExecParams(char *start_address, EState *estate)
Definition: execParallel.c:376
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:50

◆ RestoreParamExecParams()

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

Definition at line 376 of file execParallel.c.

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

Referenced by ParallelQueryMain().

377 {
378  int nparams;
379  int i;
380  int paramid;
381 
382  memcpy(&nparams, start_address, sizeof(int));
383  start_address += sizeof(int);
384 
385  for (i = 0; i < nparams; i++)
386  {
387  ParamExecData *prm;
388 
389  /* Read paramid */
390  memcpy(&paramid, start_address, sizeof(int));
391  start_address += sizeof(int);
392  prm = &(estate->es_param_exec_vals[paramid]);
393 
394  /* Read datum/isnull. */
395  prm->value = datumRestore(&start_address, &prm->isnull);
396  prm->execPlan = NULL;
397  }
398 }
void * execPlan
Definition: params.h:99
ParamExecData * es_param_exec_vals
Definition: execnodes.h:466
bool isnull
Definition: params.h:101
Datum datumRestore(char **start_address, bool *isnull)
Definition: datum.c:360
int i
Datum value
Definition: params.h:100

◆ SerializeParamExecParams()

static dsa_pointer SerializeParamExecParams ( EState estate,
Bitmapset params 
)
static

Definition at line 321 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, EState::es_query_dsa, EstimateParamExecSpace(), get_typlenbyval(), ParamExecData::isnull, list_nth_oid(), OidIsValid, PlannedStmt::paramExecTypes, and ParamExecData::value.

Referenced by ExecInitParallelPlan(), and ExecParallelReinitialize().

322 {
323  Size size;
324  int nparams;
325  int paramid;
326  ParamExecData *prm;
327  dsa_pointer handle;
328  char *start_address;
329 
330  /* Allocate enough space for the current parameter values. */
331  size = EstimateParamExecSpace(estate, params);
332  handle = dsa_allocate(estate->es_query_dsa, size);
333  start_address = dsa_get_address(estate->es_query_dsa, handle);
334 
335  /* First write the number of parameters as a 4-byte integer. */
336  nparams = bms_num_members(params);
337  memcpy(start_address, &nparams, sizeof(int));
338  start_address += sizeof(int);
339 
340  /* Write details for each parameter in turn. */
341  paramid = -1;
342  while ((paramid = bms_next_member(params, paramid)) >= 0)
343  {
344  Oid typeOid;
345  int16 typLen;
346  bool typByVal;
347 
348  prm = &(estate->es_param_exec_vals[paramid]);
349  typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
350  paramid);
351 
352  /* Write paramid. */
353  memcpy(start_address, &paramid, sizeof(int));
354  start_address += sizeof(int);
355 
356  /* Write datum/isnull */
357  if (OidIsValid(typeOid))
358  get_typlenbyval(typeOid, &typLen, &typByVal);
359  else
360  {
361  /* If no type OID, assume by-value, like copyParamList does. */
362  typLen = sizeof(Datum);
363  typByVal = true;
364  }
365  datumSerialize(prm->value, prm->isnull, typByVal, typLen,
366  &start_address);
367  }
368 
369  return handle;
370 }
signed short int16
Definition: c.h:283
List * paramExecTypes
Definition: plannodes.h:92
Oid list_nth_oid(const List *list, int n)
Definition: list.c:432
struct dsa_area * es_query_dsa
Definition: execnodes.h:513
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:937
PlannedStmt * es_plannedstmt
Definition: execnodes.h:432
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:576
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:466
bool isnull
Definition: params.h:101
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:605
static Size EstimateParamExecSpace(EState *estate, Bitmapset *params)
Definition: execParallel.c:277
uintptr_t Datum
Definition: postgres.h:372
size_t Size
Definition: c.h:404
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2001
void datumSerialize(Datum value, bool isnull, bool typByVal, int typLen, char **start_address)
Definition: datum.c:309
Datum value
Definition: params.h:100
#define dsa_allocate(area, size)
Definition: dsa.h:84