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:806

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

296 {
297  int paramid;
298  Size sz = sizeof(int);
299 
300  paramid = -1;
301  while ((paramid = bms_next_member(params, paramid)) >= 0)
302  {
303  Oid typeOid;
304  int16 typLen;
305  bool typByVal;
306  ParamExecData *prm;
307 
308  prm = &(estate->es_param_exec_vals[paramid]);
309  typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
310  paramid);
311 
312  sz = add_size(sz, sizeof(int)); /* space for paramid */
313 
314  /* space for datum/isnull */
315  if (OidIsValid(typeOid))
316  get_typlenbyval(typeOid, &typLen, &typByVal);
317  else
318  {
319  /* If no type OID, assume by-value, like copyParamList does. */
320  typLen = sizeof(Datum);
321  typByVal = true;
322  }
323  sz = add_size(sz,
324  datumEstimateSpace(prm->value, prm->isnull,
325  typByVal, typLen));
326  }
327  return sz;
328 }
signed short int16
Definition: c.h:293
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:1053
PlannedStmt * es_plannedstmt
Definition: execnodes.h:443
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:586
ParamExecData * es_param_exec_vals
Definition: execnodes.h:477
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:372
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
size_t Size
Definition: c.h:414
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2020
Datum value
Definition: params.h:148

◆ ExecInitParallelPlan()

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

Definition at line 561 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_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, 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().

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

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

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

1049 {
1050  /* Accumulate instrumentation, if any. */
1051  if (pei->instrumentation)
1053  pei->instrumentation);
1054 
1055  /* Free any serialized parameters. */
1056  if (DsaPointerIsValid(pei->param_exec))
1057  {
1058  dsa_free(pei->area, pei->param_exec);
1060  }
1061  if (pei->area != NULL)
1062  {
1063  dsa_detach(pei->area);
1064  pei->area = NULL;
1065  }
1066  if (pei->pcxt != NULL)
1067  {
1069  pei->pcxt = NULL;
1070  }
1071  pfree(pei);
1072 }
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:629
void pfree(void *pointer)
Definition: mcxt.c:936
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:934
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 796 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().

797 {
798  int nworkers = pei->pcxt->nworkers_launched;
799  int i;
800 
801  Assert(pei->reader == NULL);
802 
803  if (nworkers > 0)
804  {
805  pei->reader = (TupleQueueReader **)
806  palloc(nworkers * sizeof(TupleQueueReader *));
807 
808  for (i = 0; i < nworkers; i++)
809  {
810  shm_mq_set_handle(pei->tqueue[i],
811  pei->pcxt->worker[i].bgwhandle);
812  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
813  }
814  }
815 }
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:680
void * palloc(Size size)
Definition: mcxt.c:835
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:34

◆ ExecParallelEstimate()

static bool ExecParallelEstimate ( PlanState node,
ExecParallelEstimateContext e 
)
static

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

226 {
227  if (planstate == NULL)
228  return false;
229 
230  /* Count this node. */
231  e->nnodes++;
232 
233  switch (nodeTag(planstate))
234  {
235  case T_SeqScanState:
236  if (planstate->plan->parallel_aware)
237  ExecSeqScanEstimate((SeqScanState *) planstate,
238  e->pcxt);
239  break;
240  case T_IndexScanState:
241  if (planstate->plan->parallel_aware)
243  e->pcxt);
244  break;
246  if (planstate->plan->parallel_aware)
248  e->pcxt);
249  break;
250  case T_ForeignScanState:
251  if (planstate->plan->parallel_aware)
253  e->pcxt);
254  break;
255  case T_AppendState:
256  if (planstate->plan->parallel_aware)
257  ExecAppendEstimate((AppendState *) planstate,
258  e->pcxt);
259  break;
260  case T_CustomScanState:
261  if (planstate->plan->parallel_aware)
263  e->pcxt);
264  break;
266  if (planstate->plan->parallel_aware)
268  e->pcxt);
269  break;
270  case T_HashJoinState:
271  if (planstate->plan->parallel_aware)
272  ExecHashJoinEstimate((HashJoinState *) planstate,
273  e->pcxt);
274  break;
275  case T_HashState:
276  /* even when not parallel-aware, for EXPLAIN ANALYZE */
277  ExecHashEstimate((HashState *) planstate, e->pcxt);
278  break;
279  case T_SortState:
280  /* even when not parallel-aware, for EXPLAIN ANALYZE */
281  ExecSortEstimate((SortState *) planstate, e->pcxt);
282  break;
283 
284  default:
285  break;
286  }
287 
288  return planstate_tree_walker(planstate, ExecParallelEstimate, e);
289 }
void ExecSortEstimate(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:360
ParallelContext * pcxt
Definition: execParallel.c:110
void ExecIndexOnlyScanEstimate(IndexOnlyScanState *node, ParallelContext *pcxt)
static bool ExecParallelEstimate(PlanState *node, ExecParallelEstimateContext *e)
Definition: execParallel.c:225
void ExecHashJoinEstimate(HashJoinState *state, ParallelContext *pcxt)
void ExecHashEstimate(HashState *node, ParallelContext *pcxt)
Definition: nodeHash.c:2554
void ExecAppendEstimate(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:296
void ExecForeignScanEstimate(ForeignScanState *node, ParallelContext *pcxt)
void ExecSeqScanEstimate(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:297
#define nodeTag(nodeptr)
Definition: nodes.h:517
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 995 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().

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

◆ ExecParallelGetQueryDesc()

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

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

1097 {
1098  char *pstmtspace;
1099  char *paramspace;
1100  PlannedStmt *pstmt;
1101  ParamListInfo paramLI;
1102  char *queryString;
1103 
1104  /* Get the query string from shared memory */
1105  queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1106 
1107  /* Reconstruct leader-supplied PlannedStmt. */
1108  pstmtspace = shm_toc_lookup(toc, PARALLEL_KEY_PLANNEDSTMT, false);
1109  pstmt = (PlannedStmt *) stringToNode(pstmtspace);
1110 
1111  /* Reconstruct ParamListInfo. */
1112  paramspace = shm_toc_lookup(toc, PARALLEL_KEY_PARAMLISTINFO, false);
1113  paramLI = RestoreParamList(&paramspace);
1114 
1115  /*
1116  * Create a QueryDesc for the query.
1117  *
1118  * It's not obvious how to obtain the query string from here; and even if
1119  * we could copying it would take more cycles than not copying it. But
1120  * it's a bit unsatisfying to just use a dummy string here, so consider
1121  * revising this someday.
1122  */
1123  return CreateQueryDesc(pstmt,
1124  queryString,
1126  receiver, paramLI, NULL, instrument_options);
1127 }
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: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 1079 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().

1080 {
1081  char *mqspace;
1082  shm_mq *mq;
1083 
1084  mqspace = shm_toc_lookup(toc, PARALLEL_KEY_TUPLE_QUEUE, false);
1086  mq = (shm_mq *) mqspace;
1088  return CreateTupleQueueDestReceiver(shm_mq_attach(mq, seg, NULL));
1089 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:61
PGPROC * MyProc
Definition: proc.c:67
int ParallelWorkerNumber
Definition: parallel.c:100
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:66
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 423 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().

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

◆ ExecParallelInitializeWorker()

static bool ExecParallelInitializeWorker ( PlanState planstate,
ParallelWorkerContext pwcxt 
)
static

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

1176 {
1177  if (planstate == NULL)
1178  return false;
1179 
1180  switch (nodeTag(planstate))
1181  {
1182  case T_SeqScanState:
1183  if (planstate->plan->parallel_aware)
1184  ExecSeqScanInitializeWorker((SeqScanState *) planstate, pwcxt);
1185  break;
1186  case T_IndexScanState:
1187  if (planstate->plan->parallel_aware)
1189  pwcxt);
1190  break;
1191  case T_IndexOnlyScanState:
1192  if (planstate->plan->parallel_aware)
1194  pwcxt);
1195  break;
1196  case T_ForeignScanState:
1197  if (planstate->plan->parallel_aware)
1199  pwcxt);
1200  break;
1201  case T_AppendState:
1202  if (planstate->plan->parallel_aware)
1203  ExecAppendInitializeWorker((AppendState *) planstate, pwcxt);
1204  break;
1205  case T_CustomScanState:
1206  if (planstate->plan->parallel_aware)
1208  pwcxt);
1209  break;
1210  case T_BitmapHeapScanState:
1211  if (planstate->plan->parallel_aware)
1213  pwcxt);
1214  break;
1215  case T_HashJoinState:
1216  if (planstate->plan->parallel_aware)
1218  pwcxt);
1219  break;
1220  case T_HashState:
1221  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1222  ExecHashInitializeWorker((HashState *) planstate, pwcxt);
1223  break;
1224  case T_SortState:
1225  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1226  ExecSortInitializeWorker((SortState *) planstate, pwcxt);
1227  break;
1228 
1229  default:
1230  break;
1231  }
1232 
1234  pwcxt);
1235 }
void ExecForeignScanInitializeWorker(ForeignScanState *node, ParallelWorkerContext *pwcxt)
void ExecAppendInitializeWorker(AppendState *node, ParallelWorkerContext *pwcxt)
Definition: nodeAppend.c:352
void ExecCustomScanInitializeWorker(CustomScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeCustom.c:213
void ExecSortInitializeWorker(SortState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSort.c:406
void ExecIndexScanInitializeWorker(IndexScanState *node, ParallelWorkerContext *pwcxt)
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
bool parallel_aware
Definition: plannodes.h:137
void ExecHashJoinInitializeWorker(HashJoinState *state, ParallelWorkerContext *pwcxt)
Plan * plan
Definition: execnodes.h:858
void ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
#define nodeTag(nodeptr)
Definition: nodes.h:517
void ExecHashInitializeWorker(HashState *node, ParallelWorkerContext *pwcxt)
Definition: nodeHash.c:2587
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 822 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().

825 {
826  EState *estate = planstate->state;
828 
829  /* Old workers must already be shut down */
830  Assert(pei->finished);
831 
832  /* Force parameters we're going to pass to workers to be evaluated. */
833  ExecEvalParamExecParams(sendParams, estate);
834 
836  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
837  pei->reader = NULL;
838  pei->finished = false;
839 
840  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
841 
842  /* Free any serialized parameters from the last round. */
843  if (DsaPointerIsValid(fpes->param_exec))
844  {
845  dsa_free(pei->area, fpes->param_exec);
847  }
848 
849  /* Serialize current parameter values if required. */
850  if (!bms_is_empty(sendParams))
851  {
852  pei->param_exec = SerializeParamExecParams(estate, sendParams,
853  pei->area);
854  fpes->param_exec = pei->param_exec;
855  }
856 
857  /* Traverse plan tree and let each child node reset associated state. */
858  estate->es_query_dsa = pei->area;
859  ExecParallelReInitializeDSM(planstate, pei->pcxt);
860  estate->es_query_dsa = NULL;
861 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:524
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:509
EState * state
Definition: execnodes.h:860
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:867
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:339
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:707
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:413
#define Assert(condition)
Definition: c.h:680
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 867 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().

869 {
870  if (planstate == NULL)
871  return false;
872 
873  /*
874  * Call reinitializers for DSM-using plan nodes.
875  */
876  switch (nodeTag(planstate))
877  {
878  case T_SeqScanState:
879  if (planstate->plan->parallel_aware)
881  pcxt);
882  break;
883  case T_IndexScanState:
884  if (planstate->plan->parallel_aware)
886  pcxt);
887  break;
889  if (planstate->plan->parallel_aware)
891  pcxt);
892  break;
893  case T_ForeignScanState:
894  if (planstate->plan->parallel_aware)
896  pcxt);
897  break;
898  case T_AppendState:
899  if (planstate->plan->parallel_aware)
900  ExecAppendReInitializeDSM((AppendState *) planstate, pcxt);
901  break;
902  case T_CustomScanState:
903  if (planstate->plan->parallel_aware)
905  pcxt);
906  break;
908  if (planstate->plan->parallel_aware)
910  pcxt);
911  break;
912  case T_HashJoinState:
913  if (planstate->plan->parallel_aware)
915  pcxt);
916  break;
917  case T_HashState:
918  case T_SortState:
919  /* these nodes have DSM state, but no reinitialization is required */
920  break;
921 
922  default:
923  break;
924  }
925 
926  return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt);
927 }
void ExecCustomScanReInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:198
void ExecAppendReInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:336
void ExecHashJoinReInitializeDSM(HashJoinState *state, ParallelContext *cxt)
void ExecIndexScanReInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:867
void ExecForeignScanReInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
bool parallel_aware
Definition: plannodes.h:137
void ExecBitmapHeapReInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
Plan * plan
Definition: execnodes.h:858
void ExecIndexOnlyScanReInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
#define nodeTag(nodeptr)
Definition: nodes.h:517
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 1134 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().

1136 {
1137  int i;
1138  int plan_node_id = planstate->plan->plan_node_id;
1139  Instrumentation *instrument;
1140 
1141  InstrEndLoop(planstate->instrument);
1142 
1143  /*
1144  * If we shuffled the plan_node_id values in ps_instrument into sorted
1145  * order, we could use binary search here. This might matter someday if
1146  * we're pushing down sufficiently large plan trees. For now, do it the
1147  * slow, dumb way.
1148  */
1149  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1150  if (instrumentation->plan_node_id[i] == plan_node_id)
1151  break;
1152  if (i >= instrumentation->num_plan_nodes)
1153  elog(ERROR, "plan node %d not found", plan_node_id);
1154 
1155  /*
1156  * Add our statistics to the per-node, per-worker totals. It's possible
1157  * that this could happen more than once if we relaunched workers.
1158  */
1159  instrument = GetInstrumentationArray(instrumentation);
1160  instrument += i * instrumentation->num_workers;
1162  Assert(ParallelWorkerNumber < instrumentation->num_workers);
1163  InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
1164 
1166  instrumentation);
1167 }
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:100
Instrumentation * instrument
Definition: execnodes.h:868
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:858
#define Assert(condition)
Definition: c.h:680
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:103

◆ ExecParallelRetrieveInstrumentation()

static bool ExecParallelRetrieveInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

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

936 {
937  Instrumentation *instrument;
938  int i;
939  int n;
940  int ibytes;
941  int plan_node_id = planstate->plan->plan_node_id;
942  MemoryContext oldcontext;
943 
944  /* Find the instrumentation for this node. */
945  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
946  if (instrumentation->plan_node_id[i] == plan_node_id)
947  break;
948  if (i >= instrumentation->num_plan_nodes)
949  elog(ERROR, "plan node %d not found", plan_node_id);
950 
951  /* Accumulate the statistics from all workers. */
952  instrument = GetInstrumentationArray(instrumentation);
953  instrument += i * instrumentation->num_workers;
954  for (n = 0; n < instrumentation->num_workers; ++n)
955  InstrAggNode(planstate->instrument, &instrument[n]);
956 
957  /*
958  * Also store the per-worker detail.
959  *
960  * Worker instrumentation should be allocated in the same context as the
961  * regular instrumentation information, which is the per-query context.
962  * Switch into per-query memory context.
963  */
964  oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
965  ibytes = mul_size(instrumentation->num_workers, sizeof(Instrumentation));
966  planstate->worker_instrument =
967  palloc(ibytes + offsetof(WorkerInstrumentation, instrument));
968  MemoryContextSwitchTo(oldcontext);
969 
970  planstate->worker_instrument->num_workers = instrumentation->num_workers;
971  memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
972 
973  /* Perform any node-type-specific work that needs to be done. */
974  switch (nodeTag(planstate))
975  {
976  case T_SortState:
978  break;
979  case T_HashState:
981  break;
982  default:
983  break;
984  }
985 
987  instrumentation);
988 }
WorkerInstrumentation * worker_instrument
Definition: execnodes.h:869
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:100
Instrumentation * instrument
Definition: execnodes.h:868
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:860
void ExecHashRetrieveInstrumentation(HashState *node)
Definition: nodeHash.c:2614
MemoryContext es_query_cxt
Definition: execnodes.h:482
#define ERROR
Definition: elog.h:43
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:934
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:858
#define nodeTag(nodeptr)
Definition: nodes.h:517
void * palloc(Size size)
Definition: mcxt.c:835
int i
#define elog
Definition: elog.h:219
void ExecSortRetrieveInstrumentation(SortState *node)
Definition: nodeSort.c:420
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3697
#define offsetof(type, field)
Definition: c.h:603
#define GetInstrumentationArray(sei)
Definition: execParallel.c:103

◆ ExecParallelSetupTupleQueues()

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

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

510 {
511  shm_mq_handle **responseq;
512  char *tqueuespace;
513  int i;
514 
515  /* Skip this if no workers. */
516  if (pcxt->nworkers == 0)
517  return NULL;
518 
519  /* Allocate memory for shared memory queue handles. */
520  responseq = (shm_mq_handle **)
521  palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
522 
523  /*
524  * If not reinitializing, allocate space from the DSM for the queues;
525  * otherwise, find the already allocated space.
526  */
527  if (!reinitialize)
528  tqueuespace =
529  shm_toc_allocate(pcxt->toc,
531  pcxt->nworkers));
532  else
533  tqueuespace = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, false);
534 
535  /* Create the queues, and become the receiver for each. */
536  for (i = 0; i < pcxt->nworkers; ++i)
537  {
538  shm_mq *mq;
539 
540  mq = shm_mq_create(tqueuespace +
542  (Size) PARALLEL_TUPLE_QUEUE_SIZE);
543 
545  responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
546  }
547 
548  /* Add array of queues to shm_toc, so others can find it. */
549  if (!reinitialize)
550  shm_toc_insert(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, tqueuespace);
551 
552  /* Return array of handles. */
553  return responseq;
554 }
#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:170
#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:414
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:835
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 142 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().

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

◆ ParallelQueryMain()

void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)

Definition at line 1254 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, 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.

1255 {
1257  BufferUsage *buffer_usage;
1258  DestReceiver *receiver;
1259  QueryDesc *queryDesc;
1260  SharedExecutorInstrumentation *instrumentation;
1261  int instrument_options = 0;
1262  void *area_space;
1263  dsa_area *area;
1264  ParallelWorkerContext pwcxt;
1265 
1266  /* Get fixed-size state. */
1267  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1268 
1269  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1270  receiver = ExecParallelGetReceiver(seg, toc);
1271  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1272  if (instrumentation != NULL)
1273  instrument_options = instrumentation->instrument_options;
1274  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1275 
1276  /* Setting debug_query_string for individual workers */
1277  debug_query_string = queryDesc->sourceText;
1278 
1279  /* Report workers' query for monitoring purposes */
1281 
1282  /* Prepare to track buffer usage during query execution. */
1284 
1285  /* Attach to the dynamic shared memory area. */
1286  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1287  area = dsa_attach_in_place(area_space, seg);
1288 
1289  /* Start up the executor */
1290  ExecutorStart(queryDesc, fpes->eflags);
1291 
1292  /* Special executor initialization steps for parallel workers */
1293  queryDesc->planstate->state->es_query_dsa = area;
1294  if (DsaPointerIsValid(fpes->param_exec))
1295  {
1296  char *paramexec_space;
1297 
1298  paramexec_space = dsa_get_address(area, fpes->param_exec);
1299  RestoreParamExecParams(paramexec_space, queryDesc->estate);
1300 
1301  }
1302  pwcxt.toc = toc;
1303  pwcxt.seg = seg;
1304  ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1305 
1306  /* Pass down any tuple bound */
1307  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1308 
1309  /*
1310  * Run the plan. If we specified a tuple bound, be careful not to demand
1311  * more tuples than that.
1312  */
1313  ExecutorRun(queryDesc,
1315  fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
1316  true);
1317 
1318  /* Shut down the executor */
1319  ExecutorFinish(queryDesc);
1320 
1321  /* Report buffer usage during parallel execution. */
1322  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1324 
1325  /* Report instrumentation data if any instrumentation options are set. */
1326  if (instrumentation != NULL)
1328  instrumentation);
1329 
1330  /* Must do this after capturing instrumentation. */
1331  ExecutorEnd(queryDesc);
1332 
1333  /* Cleanup. */
1334  dsa_detach(area);
1335  FreeQueryDesc(queryDesc);
1336  receiver->rDestroy(receiver);
1337 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:524
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:860
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:800
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:62
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:57
#define PARALLEL_KEY_BUFFER_USAGE
Definition: execParallel.c:60
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: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:394
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 394 of file execParallel.c.

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

Referenced by ParallelQueryMain().

395 {
396  int nparams;
397  int i;
398  int paramid;
399 
400  memcpy(&nparams, start_address, sizeof(int));
401  start_address += sizeof(int);
402 
403  for (i = 0; i < nparams; i++)
404  {
405  ParamExecData *prm;
406 
407  /* Read paramid */
408  memcpy(&paramid, start_address, sizeof(int));
409  start_address += sizeof(int);
410  prm = &(estate->es_param_exec_vals[paramid]);
411 
412  /* Read datum/isnull. */
413  prm->value = datumRestore(&start_address, &prm->isnull);
414  prm->execPlan = NULL;
415  }
416 }
void * execPlan
Definition: params.h:147
ParamExecData * es_param_exec_vals
Definition: execnodes.h:477
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 339 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().

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