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

Go to the source code of this file.

Data Structures

struct  FixedParallelExecutorState
 
struct  SharedExecutorInstrumentation
 
struct  ExecParallelEstimateContext
 
struct  ExecParallelInitializeDSMContext
 

Macros

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

Typedefs

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

Functions

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

Macro Definition Documentation

◆ GetInstrumentationArray

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

Definition at line 106 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_JIT_INSTRUMENTATION

#define PARALLEL_KEY_JIT_INSTRUMENTATION   UINT64CONST(0xE000000000000009)

Definition at line 65 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_KEY_WAL_USAGE

#define PARALLEL_KEY_WAL_USAGE   UINT64CONST(0xE00000000000000A)

Definition at line 66 of file execParallel.c.

Referenced by ExecInitParallelPlan(), and ParallelQueryMain().

◆ PARALLEL_TUPLE_QUEUE_SIZE

#define PARALLEL_TUPLE_QUEUE_SIZE   65536

Typedef Documentation

◆ ExecParallelEstimateContext

◆ ExecParallelInitializeDSMContext

◆ FixedParallelExecutorState

Function Documentation

◆ EstimateParamExecSpace()

static Size EstimateParamExecSpace ( EState estate,
Bitmapset params 
)
static

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

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

◆ ExecInitParallelPlan()

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

Definition at line 573 of file execParallel.c.

References ParallelExecutorInfo::area, bms_is_empty(), ParallelExecutorInfo::buffer_usage, CreateParallelContext(), dsa_create_in_place(), dsa_minimum_size(), FixedParallelExecutorState::eflags, elog, ERROR, EState::es_instrument, EState::es_jit_flags, EState::es_param_list_info, EState::es_query_dsa, EState::es_sourceText, EState::es_top_eflags, EstimateParamListSpace(), ParallelContext::estimator, ExecParallelEstimate(), ExecParallelInitializeDSM(), ExecParallelSetupTupleQueues(), ExecSerializePlan(), ExecSetParamPlanMulti(), ParallelExecutorInfo::finished, GetInstrumentationArray, GetPerTupleExprContext, i, InitializeParallelDSM(), InstrInit(), SharedExecutorInstrumentation::instrument_offset, SharedExecutorInstrumentation::instrument_options, ParallelExecutorInfo::instrumentation, ExecParallelInitializeDSMContext::instrumentation, InvalidDsaPointer, FixedParallelExecutorState::jit_flags, SharedJitInstrumentation::jit_instr, ParallelExecutorInfo::jit_instrumentation, LWTRANCHE_PARALLEL_QUERY_DSA, MAXALIGN, mul_size(), ExecParallelEstimateContext::nnodes, ExecParallelInitializeDSMContext::nnodes, SharedExecutorInstrumentation::num_plan_nodes, SharedJitInstrumentation::num_workers, SharedExecutorInstrumentation::num_workers, ParallelContext::nworkers, offsetof, palloc0(), PARALLEL_KEY_BUFFER_USAGE, PARALLEL_KEY_DSA, PARALLEL_KEY_EXECUTOR_FIXED, PARALLEL_KEY_INSTRUMENTATION, PARALLEL_KEY_JIT_INSTRUMENTATION, PARALLEL_KEY_PARAMLISTINFO, PARALLEL_KEY_PLANNEDSTMT, PARALLEL_KEY_QUERY_TEXT, PARALLEL_KEY_WAL_USAGE, PARALLEL_TUPLE_QUEUE_SIZE, ParallelExecutorInfo::param_exec, FixedParallelExecutorState::param_exec, ParallelExecutorInfo::pcxt, ExecParallelEstimateContext::pcxt, ExecParallelInitializeDSMContext::pcxt, PGJIT_NONE, PlanState::plan, ParallelExecutorInfo::planstate, ParallelExecutorInfo::reader, ParallelContext::seg, SerializeParamExecParams(), SerializeParamList(), shm_toc_allocate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_insert(), ParallelContext::toc, ParallelExecutorInfo::tqueue, FixedParallelExecutorState::tuples_needed, and ParallelExecutorInfo::wal_usage.

Referenced by ExecGather(), and ExecGatherMerge().

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

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

Definition at line 1156 of file execParallel.c.

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

Referenced by ExecShutdownGather(), and ExecShutdownGatherMerge().

1157 {
1158  /* Accumulate instrumentation, if any. */
1159  if (pei->instrumentation)
1161  pei->instrumentation);
1162 
1163  /* Accumulate JIT instrumentation, if any. */
1164  if (pei->jit_instrumentation)
1166  pei->jit_instrumentation);
1167 
1168  /* Free any serialized parameters. */
1169  if (DsaPointerIsValid(pei->param_exec))
1170  {
1171  dsa_free(pei->area, pei->param_exec);
1173  }
1174  if (pei->area != NULL)
1175  {
1176  dsa_detach(pei->area);
1177  pei->area = NULL;
1178  }
1179  if (pei->pcxt != NULL)
1180  {
1182  pei->pcxt = NULL;
1183  }
1184  pfree(pei);
1185 }
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
struct SharedJitInstrumentation * jit_instrumentation
Definition: execParallel.h:31
PlanState * planstate
Definition: execParallel.h:26
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:904
void pfree(void *pointer)
Definition: mcxt.c:1056
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
void dsa_detach(dsa_area *area)
Definition: dsa.c:1913
static void ExecParallelRetrieveJitInstrumentation(PlanState *planstate, SharedJitInstrumentation *shared_jit)
dsa_pointer param_exec
Definition: execParallel.h:33
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.h:30

◆ ExecParallelCreateReaders()

void ExecParallelCreateReaders ( ParallelExecutorInfo pei)

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

858 {
859  int nworkers = pei->pcxt->nworkers_launched;
860  int i;
861 
862  Assert(pei->reader == NULL);
863 
864  if (nworkers > 0)
865  {
866  pei->reader = (TupleQueueReader **)
867  palloc(nworkers * sizeof(TupleQueueReader *));
868 
869  for (i = 0; i < nworkers; i++)
870  {
871  shm_mq_set_handle(pei->tqueue[i],
872  pei->pcxt->worker[i].bgwhandle);
873  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
874  }
875  }
876 }
ParallelContext * pcxt
Definition: execParallel.h:27
ParallelWorkerInfo * worker
Definition: parallel.h:46
struct TupleQueueReader ** reader
Definition: execParallel.h:37
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
int nworkers_launched
Definition: parallel.h:38
TupleQueueReader * CreateTupleQueueReader(shm_mq_handle *handle)
Definition: tqueue.c:139
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:310
#define Assert(condition)
Definition: c.h:738
void * palloc(Size size)
Definition: mcxt.c:949
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:36

◆ ExecParallelEstimate()

static bool ExecParallelEstimate ( PlanState node,
ExecParallelEstimateContext e 
)
static

Definition at line 229 of file execParallel.c.

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

Referenced by ExecInitParallelPlan().

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

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

Definition at line 1103 of file execParallel.c.

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

Referenced by ExecShutdownGatherMergeWorkers(), and ExecShutdownGatherWorkers().

1104 {
1105  int nworkers = pei->pcxt->nworkers_launched;
1106  int i;
1107 
1108  /* Make this be a no-op if called twice in a row. */
1109  if (pei->finished)
1110  return;
1111 
1112  /*
1113  * Detach from tuple queues ASAP, so that any still-active workers will
1114  * notice that no further results are wanted.
1115  */
1116  if (pei->tqueue != NULL)
1117  {
1118  for (i = 0; i < nworkers; i++)
1119  shm_mq_detach(pei->tqueue[i]);
1120  pfree(pei->tqueue);
1121  pei->tqueue = NULL;
1122  }
1123 
1124  /*
1125  * While we're waiting for the workers to finish, let's get rid of the
1126  * tuple queue readers. (Any other local cleanup could be done here too.)
1127  */
1128  if (pei->reader != NULL)
1129  {
1130  for (i = 0; i < nworkers; i++)
1132  pfree(pei->reader);
1133  pei->reader = NULL;
1134  }
1135 
1136  /* Now wait for the workers to finish. */
1138 
1139  /*
1140  * Next, accumulate buffer/WAL usage. (This must wait for the workers to
1141  * finish, or we might get incomplete data.)
1142  */
1143  for (i = 0; i < nworkers; i++)
1144  InstrAccumParallelQuery(&pei->buffer_usage[i], &pei->wal_usage[i]);
1145 
1146  pei->finished = true;
1147 }
ParallelContext * pcxt
Definition: execParallel.h:27
void DestroyTupleQueueReader(TupleQueueReader *reader)
Definition: tqueue.c:155
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:793
WalUsage * wal_usage
Definition: execParallel.h:29
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:750
void pfree(void *pointer)
Definition: mcxt.c:1056
BufferUsage * buffer_usage
Definition: execParallel.h:28
struct TupleQueueReader ** reader
Definition: execParallel.h:37
int nworkers_launched
Definition: parallel.h:38
void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:199
int i
shm_mq_handle ** tqueue
Definition: execParallel.h:36

◆ ExecParallelGetQueryDesc()

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

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

1210 {
1211  char *pstmtspace;
1212  char *paramspace;
1213  PlannedStmt *pstmt;
1214  ParamListInfo paramLI;
1215  char *queryString;
1216 
1217  /* Get the query string from shared memory */
1218  queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1219 
1220  /* Reconstruct leader-supplied PlannedStmt. */
1221  pstmtspace = shm_toc_lookup(toc, PARALLEL_KEY_PLANNEDSTMT, false);
1222  pstmt = (PlannedStmt *) stringToNode(pstmtspace);
1223 
1224  /* Reconstruct ParamListInfo. */
1225  paramspace = shm_toc_lookup(toc, PARALLEL_KEY_PARAMLISTINFO, false);
1226  paramLI = RestoreParamList(&paramspace);
1227 
1228  /* Create a QueryDesc for the query. */
1229  return CreateQueryDesc(pstmt,
1230  queryString,
1232  receiver, paramLI, NULL, instrument_options);
1233 }
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:841
void * stringToNode(const char *str)
Definition: read.c:89
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition: pquery.c:67
#define PARALLEL_KEY_PARAMLISTINFO
Definition: execParallel.c:59
#define PARALLEL_KEY_PLANNEDSTMT
Definition: execParallel.c:58
ParamListInfo RestoreParamList(char **start_address)
Definition: params.c:234
#define InvalidSnapshot
Definition: snapshot.h:123
#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 1192 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().

1193 {
1194  char *mqspace;
1195  shm_mq *mq;
1196 
1197  mqspace = shm_toc_lookup(toc, PARALLEL_KEY_TUPLE_QUEUE, false);
1199  mq = (shm_mq *) mqspace;
1201  return CreateTupleQueueDestReceiver(shm_mq_attach(mq, seg, NULL));
1202 }
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:61
PGPROC * MyProc
Definition: proc.c:67
int ParallelWorkerNumber
Definition: parallel.c:112
#define PARALLEL_TUPLE_QUEUE_SIZE
Definition: execParallel.c:68
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:216
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:282
Definition: shm_mq.c:70
DestReceiver * CreateTupleQueueDestReceiver(shm_mq_handle *handle)
Definition: tqueue.c:119
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

◆ ExecParallelInitializeDSM()

static bool ExecParallelInitializeDSM ( PlanState node,
ExecParallelInitializeDSMContext d 
)
static

Definition at line 431 of file execParallel.c.

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

Referenced by ExecInitParallelPlan().

433 {
434  if (planstate == NULL)
435  return false;
436 
437  /* If instrumentation is enabled, initialize slot for this node. */
438  if (d->instrumentation != NULL)
440  planstate->plan->plan_node_id;
441 
442  /* Count this node. */
443  d->nnodes++;
444 
445  /*
446  * Call initializers for DSM-using plan nodes.
447  *
448  * Most plan nodes won't do anything here, but plan nodes that allocated
449  * DSM may need to initialize shared state in the DSM before parallel
450  * workers are launched. They can allocate the space they previously
451  * estimated using shm_toc_allocate, and add the keys they previously
452  * estimated using shm_toc_insert, in each case targeting pcxt->toc.
453  */
454  switch (nodeTag(planstate))
455  {
456  case T_SeqScanState:
457  if (planstate->plan->parallel_aware)
459  d->pcxt);
460  break;
461  case T_IndexScanState:
462  if (planstate->plan->parallel_aware)
464  d->pcxt);
465  break;
467  if (planstate->plan->parallel_aware)
469  d->pcxt);
470  break;
471  case T_ForeignScanState:
472  if (planstate->plan->parallel_aware)
474  d->pcxt);
475  break;
476  case T_AppendState:
477  if (planstate->plan->parallel_aware)
478  ExecAppendInitializeDSM((AppendState *) planstate,
479  d->pcxt);
480  break;
481  case T_CustomScanState:
482  if (planstate->plan->parallel_aware)
484  d->pcxt);
485  break;
487  if (planstate->plan->parallel_aware)
489  d->pcxt);
490  break;
491  case T_HashJoinState:
492  if (planstate->plan->parallel_aware)
494  d->pcxt);
495  break;
496  case T_HashState:
497  /* even when not parallel-aware, for EXPLAIN ANALYZE */
498  ExecHashInitializeDSM((HashState *) planstate, d->pcxt);
499  break;
500  case T_SortState:
501  /* even when not parallel-aware, for EXPLAIN ANALYZE */
502  ExecSortInitializeDSM((SortState *) planstate, d->pcxt);
503  break;
505  /* even when not parallel-aware, for EXPLAIN ANALYZE */
507  break;
508 
509  default:
510  break;
511  }
512 
513  return planstate_tree_walker(planstate, ExecParallelInitializeDSM, d);
514 }
void ExecIndexScanInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:103
SharedExecutorInstrumentation * instrumentation
Definition: execParallel.c:121
void ExecSortInitializeDSM(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:378
void ExecSeqScanInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:268
static bool ExecParallelInitializeDSM(PlanState *node, ExecParallelInitializeDSMContext *d)
Definition: execParallel.c:431
void ExecBitmapHeapInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecHashInitializeDSM(HashState *node, ParallelContext *pcxt)
Definition: nodeHash.c:2589
void ExecIncrementalSortInitializeDSM(IncrementalSortState *node, ParallelContext *pcxt)
void ExecForeignScanInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:175
void ExecAppendInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:386
#define nodeTag(nodeptr)
Definition: nodes.h:534
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:3882

◆ ExecParallelInitializeWorker()

static bool ExecParallelInitializeWorker ( PlanState planstate,
ParallelWorkerContext pwcxt 
)
static

Definition at line 1281 of file execParallel.c.

References ExecAppendInitializeWorker(), ExecBitmapHeapInitializeWorker(), ExecCustomScanInitializeWorker(), ExecForeignScanInitializeWorker(), ExecHashInitializeWorker(), ExecHashJoinInitializeWorker(), ExecIncrementalSortInitializeWorker(), 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_IncrementalSortState, T_IndexOnlyScanState, T_IndexScanState, T_SeqScanState, and T_SortState.

Referenced by ParallelQueryMain().

1282 {
1283  if (planstate == NULL)
1284  return false;
1285 
1286  switch (nodeTag(planstate))
1287  {
1288  case T_SeqScanState:
1289  if (planstate->plan->parallel_aware)
1290  ExecSeqScanInitializeWorker((SeqScanState *) planstate, pwcxt);
1291  break;
1292  case T_IndexScanState:
1293  if (planstate->plan->parallel_aware)
1295  pwcxt);
1296  break;
1297  case T_IndexOnlyScanState:
1298  if (planstate->plan->parallel_aware)
1300  pwcxt);
1301  break;
1302  case T_ForeignScanState:
1303  if (planstate->plan->parallel_aware)
1305  pwcxt);
1306  break;
1307  case T_AppendState:
1308  if (planstate->plan->parallel_aware)
1309  ExecAppendInitializeWorker((AppendState *) planstate, pwcxt);
1310  break;
1311  case T_CustomScanState:
1312  if (planstate->plan->parallel_aware)
1314  pwcxt);
1315  break;
1316  case T_BitmapHeapScanState:
1317  if (planstate->plan->parallel_aware)
1319  pwcxt);
1320  break;
1321  case T_HashJoinState:
1322  if (planstate->plan->parallel_aware)
1324  pwcxt);
1325  break;
1326  case T_HashState:
1327  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1328  ExecHashInitializeWorker((HashState *) planstate, pwcxt);
1329  break;
1330  case T_SortState:
1331  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1332  ExecSortInitializeWorker((SortState *) planstate, pwcxt);
1333  break;
1335  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1337  pwcxt);
1338  break;
1339 
1340  default:
1341  break;
1342  }
1343 
1345  pwcxt);
1346 }
void ExecForeignScanInitializeWorker(ForeignScanState *node, ParallelWorkerContext *pwcxt)
void ExecAppendInitializeWorker(AppendState *node, ParallelWorkerContext *pwcxt)
Definition: nodeAppend.c:423
void ExecCustomScanInitializeWorker(CustomScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeCustom.c:206
void ExecSortInitializeWorker(SortState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSort.c:403
void ExecIndexScanInitializeWorker(IndexScanState *node, ParallelWorkerContext *pwcxt)
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
bool parallel_aware
Definition: plannodes.h:135
void ExecIncrementalSortInitializeWorker(IncrementalSortState *node, ParallelWorkerContext *pwcxt)
void ExecHashJoinInitializeWorker(HashJoinState *state, ParallelWorkerContext *pwcxt)
Plan * plan
Definition: execnodes.h:945
void ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
#define nodeTag(nodeptr)
Definition: nodes.h:534
void ExecHashInitializeWorker(HashState *node, ParallelWorkerContext *pwcxt)
Definition: nodeHash.c:2614
void ExecBitmapHeapInitializeWorker(BitmapHeapScanState *node, ParallelWorkerContext *pwcxt)
void ExecSeqScanInitializeWorker(SeqScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSeqscan.c:306
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3882

◆ ExecParallelReinitialize()

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

Definition at line 883 of file execParallel.c.

References ParallelExecutorInfo::area, Assert, bms_is_empty(), dsa_free(), DsaPointerIsValid, EState::es_query_dsa, ExecParallelReInitializeDSM(), ExecParallelSetupTupleQueues(), ExecSetParamPlanMulti(), ParallelExecutorInfo::finished, GetPerTupleExprContext, InvalidDsaPointer, PARALLEL_KEY_EXECUTOR_FIXED, ParallelExecutorInfo::param_exec, FixedParallelExecutorState::param_exec, ParallelExecutorInfo::pcxt, ParallelExecutorInfo::reader, ReinitializeParallelDSM(), SerializeParamExecParams(), shm_toc_lookup(), PlanState::state, ParallelContext::toc, and ParallelExecutorInfo::tqueue.

Referenced by ExecGather(), and ExecGatherMerge().

886 {
887  EState *estate = planstate->state;
889 
890  /* Old workers must already be shut down */
891  Assert(pei->finished);
892 
893  /*
894  * Force any initplan outputs that we're going to pass to workers to be
895  * evaluated, if they weren't already (see comments in
896  * ExecInitParallelPlan).
897  */
898  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
899 
901  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
902  pei->reader = NULL;
903  pei->finished = false;
904 
905  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
906 
907  /* Free any serialized parameters from the last round. */
908  if (DsaPointerIsValid(fpes->param_exec))
909  {
910  dsa_free(pei->area, fpes->param_exec);
912  }
913 
914  /* Serialize current parameter values if required. */
915  if (!bms_is_empty(sendParams))
916  {
917  pei->param_exec = SerializeParamExecParams(estate, sendParams,
918  pei->area);
919  fpes->param_exec = pei->param_exec;
920  }
921 
922  /* Traverse plan tree and let each child node reset associated state. */
923  estate->es_query_dsa = pei->area;
924  ExecParallelReInitializeDSM(planstate, pei->pcxt);
925  estate->es_query_dsa = NULL;
926 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:589
void ExecSetParamPlanMulti(const Bitmapset *params, ExprContext *econtext)
Definition: nodeSubplan.c:1245
ParallelContext * pcxt
Definition: execParallel.h:27
#define InvalidDsaPointer
Definition: dsa.h:78
static shm_mq_handle ** ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
Definition: execParallel.c:521
EState * state
Definition: execnodes.h:947
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:932
static dsa_pointer SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
Definition: execParallel.c:347
#define GetPerTupleExprContext(estate)
Definition: executor.h:507
struct TupleQueueReader ** reader
Definition: execParallel.h:37
#define PARALLEL_KEY_EXECUTOR_FIXED
Definition: execParallel.c:57
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:463
#define Assert(condition)
Definition: c.h:738
dsa_pointer param_exec
Definition: execParallel.h:33
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
shm_mq_handle ** tqueue
Definition: execParallel.h:36
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
shm_toc * toc
Definition: parallel.h:45

◆ ExecParallelReInitializeDSM()

static bool ExecParallelReInitializeDSM ( PlanState planstate,
ParallelContext pcxt 
)
static

Definition at line 932 of file execParallel.c.

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

Referenced by ExecParallelReinitialize().

934 {
935  if (planstate == NULL)
936  return false;
937 
938  /*
939  * Call reinitializers for DSM-using plan nodes.
940  */
941  switch (nodeTag(planstate))
942  {
943  case T_SeqScanState:
944  if (planstate->plan->parallel_aware)
946  pcxt);
947  break;
948  case T_IndexScanState:
949  if (planstate->plan->parallel_aware)
951  pcxt);
952  break;
954  if (planstate->plan->parallel_aware)
956  pcxt);
957  break;
958  case T_ForeignScanState:
959  if (planstate->plan->parallel_aware)
961  pcxt);
962  break;
963  case T_AppendState:
964  if (planstate->plan->parallel_aware)
965  ExecAppendReInitializeDSM((AppendState *) planstate, pcxt);
966  break;
967  case T_CustomScanState:
968  if (planstate->plan->parallel_aware)
970  pcxt);
971  break;
973  if (planstate->plan->parallel_aware)
975  pcxt);
976  break;
977  case T_HashJoinState:
978  if (planstate->plan->parallel_aware)
980  pcxt);
981  break;
982  case T_HashState:
983  case T_SortState:
985  /* these nodes have DSM state, but no reinitialization is required */
986  break;
987 
988  default:
989  break;
990  }
991 
992  return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt);
993 }
void ExecCustomScanReInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:191
void ExecAppendReInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:407
void ExecHashJoinReInitializeDSM(HashJoinState *state, ParallelContext *cxt)
void ExecIndexScanReInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:932
void ExecForeignScanReInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
bool parallel_aware
Definition: plannodes.h:135
void ExecBitmapHeapReInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
Plan * plan
Definition: execnodes.h:945
void ExecIndexOnlyScanReInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
#define nodeTag(nodeptr)
Definition: nodes.h:534
void ExecSeqScanReInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:290
bool planstate_tree_walker(PlanState *planstate, bool(*walker)(), void *context)
Definition: nodeFuncs.c:3882

◆ ExecParallelReportInstrumentation()

static bool ExecParallelReportInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

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

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

◆ ExecParallelRetrieveInstrumentation()

static bool ExecParallelRetrieveInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 1000 of file execParallel.c.

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

Referenced by ExecParallelCleanup().

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

◆ ExecParallelRetrieveJitInstrumentation()

static void ExecParallelRetrieveJitInstrumentation ( PlanState planstate,
SharedJitInstrumentation shared_jit 
)
static

Definition at line 1063 of file execParallel.c.

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

Referenced by ExecParallelCleanup().

1065 {
1066  JitInstrumentation *combined;
1067  int ibytes;
1068 
1069  int n;
1070 
1071  /*
1072  * Accumulate worker JIT instrumentation into the combined JIT
1073  * instrumentation, allocating it if required.
1074  */
1075  if (!planstate->state->es_jit_worker_instr)
1076  planstate->state->es_jit_worker_instr =
1078  combined = planstate->state->es_jit_worker_instr;
1079 
1080  /* Accumulate all the workers' instrumentations. */
1081  for (n = 0; n < shared_jit->num_workers; ++n)
1082  InstrJitAgg(combined, &shared_jit->jit_instr[n]);
1083 
1084  /*
1085  * Store the per-worker detail.
1086  *
1087  * Similar to ExecParallelRetrieveInstrumentation(), allocate the
1088  * instrumentation in per-query context.
1089  */
1090  ibytes = offsetof(SharedJitInstrumentation, jit_instr)
1091  + mul_size(shared_jit->num_workers, sizeof(JitInstrumentation));
1092  planstate->worker_jit_instrument =
1093  MemoryContextAlloc(planstate->state->es_query_cxt, ibytes);
1094 
1095  memcpy(planstate->worker_jit_instrument, shared_jit, ibytes);
1096 }
struct JitInstrumentation * es_jit_worker_instr
Definition: execnodes.h:602
EState * state
Definition: execnodes.h:947
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER]
Definition: jit.h:51
MemoryContext es_query_cxt
Definition: execnodes.h:555
struct SharedJitInstrumentation * worker_jit_instrument
Definition: execnodes.h:959
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
Definition: jit.c:184
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:839
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
#define offsetof(type, field)
Definition: c.h:661

◆ ExecParallelSetupTupleQueues()

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

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

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

◆ ExecSerializePlan()

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

Definition at line 145 of file execParallel.c.

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

Referenced by ExecInitParallelPlan().

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

◆ ParallelQueryMain()

void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)

Definition at line 1365 of file execParallel.c.

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

1366 {
1368  BufferUsage *buffer_usage;
1369  WalUsage *wal_usage;
1370  DestReceiver *receiver;
1371  QueryDesc *queryDesc;
1372  SharedExecutorInstrumentation *instrumentation;
1373  SharedJitInstrumentation *jit_instrumentation;
1374  int instrument_options = 0;
1375  void *area_space;
1376  dsa_area *area;
1377  ParallelWorkerContext pwcxt;
1378 
1379  /* Get fixed-size state. */
1380  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1381 
1382  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1383  receiver = ExecParallelGetReceiver(seg, toc);
1384  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1385  if (instrumentation != NULL)
1386  instrument_options = instrumentation->instrument_options;
1387  jit_instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
1388  true);
1389  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1390 
1391  /* Setting debug_query_string for individual workers */
1392  debug_query_string = queryDesc->sourceText;
1393 
1394  /* Report workers' query for monitoring purposes */
1396 
1397  /* Attach to the dynamic shared memory area. */
1398  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1399  area = dsa_attach_in_place(area_space, seg);
1400 
1401  /* Start up the executor */
1402  queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1403  ExecutorStart(queryDesc, fpes->eflags);
1404 
1405  /* Special executor initialization steps for parallel workers */
1406  queryDesc->planstate->state->es_query_dsa = area;
1407  if (DsaPointerIsValid(fpes->param_exec))
1408  {
1409  char *paramexec_space;
1410 
1411  paramexec_space = dsa_get_address(area, fpes->param_exec);
1412  RestoreParamExecParams(paramexec_space, queryDesc->estate);
1413 
1414  }
1415  pwcxt.toc = toc;
1416  pwcxt.seg = seg;
1417  ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1418 
1419  /* Pass down any tuple bound */
1420  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1421 
1422  /*
1423  * Prepare to track buffer/WAL usage during query execution.
1424  *
1425  * We do this after starting up the executor to match what happens in the
1426  * leader, which also doesn't count buffer accesses and WAL activity that
1427  * occur during executor startup.
1428  */
1430 
1431  /*
1432  * Run the plan. If we specified a tuple bound, be careful not to demand
1433  * more tuples than that.
1434  */
1435  ExecutorRun(queryDesc,
1437  fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
1438  true);
1439 
1440  /* Shut down the executor */
1441  ExecutorFinish(queryDesc);
1442 
1443  /* Report buffer/WAL usage during parallel execution. */
1444  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1445  wal_usage = shm_toc_lookup(toc, PARALLEL_KEY_WAL_USAGE, false);
1447  &wal_usage[ParallelWorkerNumber]);
1448 
1449  /* Report instrumentation data if any instrumentation options are set. */
1450  if (instrumentation != NULL)
1452  instrumentation);
1453 
1454  /* Report JIT instrumentation data if any */
1455  if (queryDesc->estate->es_jit && jit_instrumentation != NULL)
1456  {
1457  Assert(ParallelWorkerNumber < jit_instrumentation->num_workers);
1458  jit_instrumentation->jit_instr[ParallelWorkerNumber] =
1459  queryDesc->estate->es_jit->instr;
1460  }
1461 
1462  /* Must do this after capturing instrumentation. */
1463  ExecutorEnd(queryDesc);
1464 
1465  /* Cleanup. */
1466  dsa_detach(area);
1467  FreeQueryDesc(queryDesc);
1468  receiver->rDestroy(receiver);
1469 }
struct dsa_area * es_query_dsa
Definition: execnodes.h:589
EState * estate
Definition: execdesc.h:48
struct JitContext * es_jit
Definition: execnodes.h:601
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
void pgstat_report_activity(BackendState state, const char *cmd_str)
Definition: pgstat.c:3136
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:143
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:541
EState * state
Definition: execnodes.h:947
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:189
JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER]
Definition: jit.h:51
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:821
JitInstrumentation instr
Definition: jit.h:61
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:462
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
#define PARALLEL_KEY_INSTRUMENTATION
Definition: execParallel.c:62
PlanState * planstate
Definition: execdesc.h:49
#define PARALLEL_KEY_WAL_USAGE
Definition: execParallel.c:66
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:301
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
void dsa_detach(dsa_area *area)
Definition: dsa.c:1913
int ParallelWorkerNumber
Definition: parallel.c:112
int jitFlags
Definition: plannodes.h:62
#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:402
#define PARALLEL_KEY_JIT_INSTRUMENTATION
Definition: execParallel.c:65
const char * debug_query_string
Definition: postgres.c:88
void InstrStartParallelQuery(void)
Definition: instrument.c:181
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:127
#define Assert(condition)
Definition: c.h:738
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:402
PlannedStmt * plannedstmt
Definition: execdesc.h:37
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
static QueryDesc * ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver, int instrument_options)
dsm_segment * seg
Definition: parallel.h:53

◆ RestoreParamExecParams()

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

Definition at line 402 of file execParallel.c.

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

Referenced by ParallelQueryMain().

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

◆ SerializeParamExecParams()

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

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

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