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 *planstate, ExecParallelEstimateContext *e)
 
static bool ExecParallelInitializeDSM (PlanState *planstate, 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:971

Definition at line 106 of file execParallel.c.

◆ PARALLEL_KEY_BUFFER_USAGE

#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xE000000000000004)

Definition at line 60 of file execParallel.c.

◆ PARALLEL_KEY_DSA

#define PARALLEL_KEY_DSA   UINT64CONST(0xE000000000000007)

Definition at line 63 of file execParallel.c.

◆ PARALLEL_KEY_EXECUTOR_FIXED

#define PARALLEL_KEY_EXECUTOR_FIXED   UINT64CONST(0xE000000000000001)

Definition at line 57 of file execParallel.c.

◆ PARALLEL_KEY_INSTRUMENTATION

#define PARALLEL_KEY_INSTRUMENTATION   UINT64CONST(0xE000000000000006)

Definition at line 62 of file execParallel.c.

◆ PARALLEL_KEY_JIT_INSTRUMENTATION

#define PARALLEL_KEY_JIT_INSTRUMENTATION   UINT64CONST(0xE000000000000009)

Definition at line 65 of file execParallel.c.

◆ PARALLEL_KEY_PARAMLISTINFO

#define PARALLEL_KEY_PARAMLISTINFO   UINT64CONST(0xE000000000000003)

Definition at line 59 of file execParallel.c.

◆ PARALLEL_KEY_PLANNEDSTMT

#define PARALLEL_KEY_PLANNEDSTMT   UINT64CONST(0xE000000000000002)

Definition at line 58 of file execParallel.c.

◆ PARALLEL_KEY_QUERY_TEXT

#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xE000000000000008)

Definition at line 64 of file execParallel.c.

◆ PARALLEL_KEY_TUPLE_QUEUE

#define PARALLEL_KEY_TUPLE_QUEUE   UINT64CONST(0xE000000000000005)

Definition at line 61 of file execParallel.c.

◆ PARALLEL_KEY_WAL_USAGE

#define PARALLEL_KEY_WAL_USAGE   UINT64CONST(0xE00000000000000A)

Definition at line 66 of file execParallel.c.

◆ PARALLEL_TUPLE_QUEUE_SIZE

#define PARALLEL_TUPLE_QUEUE_SIZE   65536

Definition at line 68 of file execParallel.c.

Typedef Documentation

◆ ExecParallelEstimateContext

◆ ExecParallelInitializeDSMContext

◆ FixedParallelExecutorState

Function Documentation

◆ EstimateParamExecSpace()

static Size EstimateParamExecSpace ( EState estate,
Bitmapset params 
)
static

Definition at line 310 of file execParallel.c.

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

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

◆ ExecInitParallelPlan()

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

Definition at line 587 of file execParallel.c.

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

References ParallelExecutorInfo::area, Assert(), 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_snapshot, EState::es_sourceText, EState::es_top_eflags, EstimateParamListSpace(), ParallelContext::estimator, ExecParallelEstimate(), ExecParallelInitializeDSM(), ExecParallelSetupTupleQueues(), ExecSerializePlan(), ExecSetParamPlanMulti(), ParallelExecutorInfo::finished, GetActiveSnapshot(), GetInstrumentationArray, GetPerTupleExprContext, i, InitializeParallelDSM(), InstrInit(), SharedExecutorInstrumentation::instrument_offset, SharedExecutorInstrumentation::instrument_options, ExecParallelInitializeDSMContext::instrumentation, ParallelExecutorInfo::instrumentation, InvalidDsaPointer, FixedParallelExecutorState::jit_flags, SharedJitInstrumentation::jit_instr, ParallelExecutorInfo::jit_instrumentation, LWTRANCHE_PARALLEL_QUERY_DSA, MAXALIGN, mul_size(), ExecParallelInitializeDSMContext::nnodes, SharedExecutorInstrumentation::num_plan_nodes, SharedExecutorInstrumentation::num_workers, SharedJitInstrumentation::num_workers, ParallelContext::nworkers, 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, FixedParallelExecutorState::param_exec, ParallelExecutorInfo::param_exec, ExecParallelInitializeDSMContext::pcxt, ParallelExecutorInfo::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().

◆ ExecParallelCleanup()

void ExecParallelCleanup ( ParallelExecutorInfo pei)

Definition at line 1184 of file execParallel.c.

1185 {
1186  /* Accumulate instrumentation, if any. */
1187  if (pei->instrumentation)
1189  pei->instrumentation);
1190 
1191  /* Accumulate JIT instrumentation, if any. */
1192  if (pei->jit_instrumentation)
1194  pei->jit_instrumentation);
1195 
1196  /* Free any serialized parameters. */
1197  if (DsaPointerIsValid(pei->param_exec))
1198  {
1199  dsa_free(pei->area, pei->param_exec);
1201  }
1202  if (pei->area != NULL)
1203  {
1204  dsa_detach(pei->area);
1205  pei->area = NULL;
1206  }
1207  if (pei->pcxt != NULL)
1208  {
1210  pei->pcxt = NULL;
1211  }
1212  pfree(pei);
1213 }
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:929
void dsa_detach(dsa_area *area)
Definition: dsa.c:1957
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:839
#define DsaPointerIsValid(x)
Definition: dsa.h:81
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
static void ExecParallelRetrieveJitInstrumentation(PlanState *planstate, SharedJitInstrumentation *shared_jit)
void pfree(void *pointer)
Definition: mcxt.c:1508

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

◆ ExecParallelCreateReaders()

void ExecParallelCreateReaders ( ParallelExecutorInfo pei)

Definition at line 878 of file execParallel.c.

879 {
880  int nworkers = pei->pcxt->nworkers_launched;
881  int i;
882 
883  Assert(pei->reader == NULL);
884 
885  if (nworkers > 0)
886  {
887  pei->reader = (TupleQueueReader **)
888  palloc(nworkers * sizeof(TupleQueueReader *));
889 
890  for (i = 0; i < nworkers; i++)
891  {
892  shm_mq_set_handle(pei->tqueue[i],
893  pei->pcxt->worker[i].bgwhandle);
894  pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
895  }
896  }
897 }
void * palloc(Size size)
Definition: mcxt.c:1304
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:319
ParallelWorkerInfo * worker
Definition: parallel.h:45
int nworkers_launched
Definition: parallel.h:37
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
TupleQueueReader * CreateTupleQueueReader(shm_mq_handle *handle)
Definition: tqueue.c:139

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

◆ ExecParallelEstimate()

static bool ExecParallelEstimate ( PlanState planstate,
ExecParallelEstimateContext e 
)
static

Definition at line 229 of file execParallel.c.

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;
249  case T_IndexOnlyScanState:
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;
269  case T_BitmapHeapScanState:
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;
287  case T_IncrementalSortState:
288  /* even when not parallel-aware, for EXPLAIN ANALYZE */
289  ExecIncrementalSortEstimate((IncrementalSortState *) planstate, e->pcxt);
290  break;
291  case T_AggState:
292  /* even when not parallel-aware, for EXPLAIN ANALYZE */
293  ExecAggEstimate((AggState *) planstate, e->pcxt);
294  break;
295  case T_MemoizeState:
296  /* even when not parallel-aware, for EXPLAIN ANALYZE */
297  ExecMemoizeEstimate((MemoizeState *) planstate, e->pcxt);
298  break;
299  default:
300  break;
301  }
302 
303  return planstate_tree_walker(planstate, ExecParallelEstimate, e);
304 }
void ExecAggEstimate(AggState *node, ParallelContext *pcxt)
Definition: nodeAgg.c:4683
void ExecAppendEstimate(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:484
void ExecBitmapHeapEstimate(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecCustomScanEstimate(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:161
void ExecForeignScanEstimate(ForeignScanState *node, ParallelContext *pcxt)
#define planstate_tree_walker(ps, w, c)
Definition: nodeFuncs.h:177
void ExecHashEstimate(HashState *node, ParallelContext *pcxt)
Definition: nodeHash.c:2734
void ExecHashJoinEstimate(HashJoinState *state, ParallelContext *pcxt)
void ExecIncrementalSortEstimate(IncrementalSortState *node, ParallelContext *pcxt)
void ExecIndexOnlyScanEstimate(IndexOnlyScanState *node, ParallelContext *pcxt)
void ExecIndexScanEstimate(IndexScanState *node, ParallelContext *pcxt)
void ExecMemoizeEstimate(MemoizeState *node, ParallelContext *pcxt)
Definition: nodeMemoize.c:1189
void ExecSeqScanEstimate(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:238
void ExecSortEstimate(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:416
#define nodeTag(nodeptr)
Definition: nodes.h:133
bool parallel_aware
Definition: plannodes.h:140

References ExecAggEstimate(), ExecAppendEstimate(), ExecBitmapHeapEstimate(), ExecCustomScanEstimate(), ExecForeignScanEstimate(), ExecHashEstimate(), ExecHashJoinEstimate(), ExecIncrementalSortEstimate(), ExecIndexOnlyScanEstimate(), ExecIndexScanEstimate(), ExecMemoizeEstimate(), ExecSeqScanEstimate(), ExecSortEstimate(), nodeTag, Plan::parallel_aware, PlanState::plan, and planstate_tree_walker.

Referenced by ExecInitParallelPlan().

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

Definition at line 1131 of file execParallel.c.

1132 {
1133  int nworkers = pei->pcxt->nworkers_launched;
1134  int i;
1135 
1136  /* Make this be a no-op if called twice in a row. */
1137  if (pei->finished)
1138  return;
1139 
1140  /*
1141  * Detach from tuple queues ASAP, so that any still-active workers will
1142  * notice that no further results are wanted.
1143  */
1144  if (pei->tqueue != NULL)
1145  {
1146  for (i = 0; i < nworkers; i++)
1147  shm_mq_detach(pei->tqueue[i]);
1148  pfree(pei->tqueue);
1149  pei->tqueue = NULL;
1150  }
1151 
1152  /*
1153  * While we're waiting for the workers to finish, let's get rid of the
1154  * tuple queue readers. (Any other local cleanup could be done here too.)
1155  */
1156  if (pei->reader != NULL)
1157  {
1158  for (i = 0; i < nworkers; i++)
1160  pfree(pei->reader);
1161  pei->reader = NULL;
1162  }
1163 
1164  /* Now wait for the workers to finish. */
1166 
1167  /*
1168  * Next, accumulate buffer/WAL usage. (This must wait for the workers to
1169  * finish, or we might get incomplete data.)
1170  */
1171  for (i = 0; i < nworkers; i++)
1173 
1174  pei->finished = true;
1175 }
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:775
void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:218
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:843
void DestroyTupleQueueReader(TupleQueueReader *reader)
Definition: tqueue.c:155

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

◆ ExecParallelGetQueryDesc()

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

Definition at line 1236 of file execParallel.c.

1238 {
1239  char *pstmtspace;
1240  char *paramspace;
1241  PlannedStmt *pstmt;
1242  ParamListInfo paramLI;
1243  char *queryString;
1244 
1245  /* Get the query string from shared memory */
1246  queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1247 
1248  /* Reconstruct leader-supplied PlannedStmt. */
1249  pstmtspace = shm_toc_lookup(toc, PARALLEL_KEY_PLANNEDSTMT, false);
1250  pstmt = (PlannedStmt *) stringToNode(pstmtspace);
1251 
1252  /* Reconstruct ParamListInfo. */
1253  paramspace = shm_toc_lookup(toc, PARALLEL_KEY_PARAMLISTINFO, false);
1254  paramLI = RestoreParamList(&paramspace);
1255 
1256  /* Create a QueryDesc for the query. */
1257  return CreateQueryDesc(pstmt,
1258  queryString,
1260  receiver, paramLI, NULL, instrument_options);
1261 }
ParamListInfo RestoreParamList(char **start_address)
Definition: params.c:292
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
void * stringToNode(const char *str)
Definition: read.c:90
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
#define InvalidSnapshot
Definition: snapshot.h:123

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

Referenced by ParallelQueryMain().

◆ ExecParallelGetReceiver()

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

Definition at line 1220 of file execParallel.c.

1221 {
1222  char *mqspace;
1223  shm_mq *mq;
1224 
1225  mqspace = shm_toc_lookup(toc, PARALLEL_KEY_TUPLE_QUEUE, false);
1227  mq = (shm_mq *) mqspace;
1229  return CreateTupleQueueDestReceiver(shm_mq_attach(mq, seg, NULL));
1230 }
int ParallelWorkerNumber
Definition: parallel.c:112
#define PARALLEL_KEY_TUPLE_QUEUE
Definition: execParallel.c:61
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:290
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:224
PGPROC * MyProc
Definition: proc.c:66
Definition: shm_mq.c:72
DestReceiver * CreateTupleQueueDestReceiver(shm_mq_handle *handle)
Definition: tqueue.c:119

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

◆ ExecParallelInitializeDSM()

static bool ExecParallelInitializeDSM ( PlanState planstate,
ExecParallelInitializeDSMContext d 
)
static

Definition at line 438 of file execParallel.c.

440 {
441  if (planstate == NULL)
442  return false;
443 
444  /* If instrumentation is enabled, initialize slot for this node. */
445  if (d->instrumentation != NULL)
447  planstate->plan->plan_node_id;
448 
449  /* Count this node. */
450  d->nnodes++;
451 
452  /*
453  * Call initializers for DSM-using plan nodes.
454  *
455  * Most plan nodes won't do anything here, but plan nodes that allocated
456  * DSM may need to initialize shared state in the DSM before parallel
457  * workers are launched. They can allocate the space they previously
458  * estimated using shm_toc_allocate, and add the keys they previously
459  * estimated using shm_toc_insert, in each case targeting pcxt->toc.
460  */
461  switch (nodeTag(planstate))
462  {
463  case T_SeqScanState:
464  if (planstate->plan->parallel_aware)
466  d->pcxt);
467  break;
468  case T_IndexScanState:
469  if (planstate->plan->parallel_aware)
471  d->pcxt);
472  break;
473  case T_IndexOnlyScanState:
474  if (planstate->plan->parallel_aware)
476  d->pcxt);
477  break;
478  case T_ForeignScanState:
479  if (planstate->plan->parallel_aware)
481  d->pcxt);
482  break;
483  case T_AppendState:
484  if (planstate->plan->parallel_aware)
485  ExecAppendInitializeDSM((AppendState *) planstate,
486  d->pcxt);
487  break;
488  case T_CustomScanState:
489  if (planstate->plan->parallel_aware)
491  d->pcxt);
492  break;
493  case T_BitmapHeapScanState:
494  if (planstate->plan->parallel_aware)
496  d->pcxt);
497  break;
498  case T_HashJoinState:
499  if (planstate->plan->parallel_aware)
501  d->pcxt);
502  break;
503  case T_HashState:
504  /* even when not parallel-aware, for EXPLAIN ANALYZE */
505  ExecHashInitializeDSM((HashState *) planstate, d->pcxt);
506  break;
507  case T_SortState:
508  /* even when not parallel-aware, for EXPLAIN ANALYZE */
509  ExecSortInitializeDSM((SortState *) planstate, d->pcxt);
510  break;
511  case T_IncrementalSortState:
512  /* even when not parallel-aware, for EXPLAIN ANALYZE */
514  break;
515  case T_AggState:
516  /* even when not parallel-aware, for EXPLAIN ANALYZE */
517  ExecAggInitializeDSM((AggState *) planstate, d->pcxt);
518  break;
519  case T_MemoizeState:
520  /* even when not parallel-aware, for EXPLAIN ANALYZE */
521  ExecMemoizeInitializeDSM((MemoizeState *) planstate, d->pcxt);
522  break;
523  default:
524  break;
525  }
526 
527  return planstate_tree_walker(planstate, ExecParallelInitializeDSM, d);
528 }
void ExecAggInitializeDSM(AggState *node, ParallelContext *pcxt)
Definition: nodeAgg.c:4704
void ExecAppendInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:503
void ExecBitmapHeapInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:174
void ExecForeignScanInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecHashInitializeDSM(HashState *node, ParallelContext *pcxt)
Definition: nodeHash.c:2753
void ExecHashJoinInitializeDSM(HashJoinState *state, ParallelContext *pcxt)
void ExecIncrementalSortInitializeDSM(IncrementalSortState *node, ParallelContext *pcxt)
void ExecIndexOnlyScanInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
void ExecIndexScanInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
void ExecMemoizeInitializeDSM(MemoizeState *node, ParallelContext *pcxt)
Definition: nodeMemoize.c:1210
void ExecSeqScanInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:256
void ExecSortInitializeDSM(SortState *node, ParallelContext *pcxt)
Definition: nodeSort.c:437
int plan_node_id
Definition: plannodes.h:151
int plan_node_id[FLEXIBLE_ARRAY_MEMBER]
Definition: execParallel.c:103

References ExecAggInitializeDSM(), ExecAppendInitializeDSM(), ExecBitmapHeapInitializeDSM(), ExecCustomScanInitializeDSM(), ExecForeignScanInitializeDSM(), ExecHashInitializeDSM(), ExecHashJoinInitializeDSM(), ExecIncrementalSortInitializeDSM(), ExecIndexOnlyScanInitializeDSM(), ExecIndexScanInitializeDSM(), ExecMemoizeInitializeDSM(), ExecSeqScanInitializeDSM(), ExecSortInitializeDSM(), ExecParallelInitializeDSMContext::instrumentation, ExecParallelInitializeDSMContext::nnodes, nodeTag, Plan::parallel_aware, ExecParallelInitializeDSMContext::pcxt, PlanState::plan, SharedExecutorInstrumentation::plan_node_id, Plan::plan_node_id, and planstate_tree_walker.

Referenced by ExecInitParallelPlan().

◆ ExecParallelInitializeWorker()

static bool ExecParallelInitializeWorker ( PlanState planstate,
ParallelWorkerContext pwcxt 
)
static

Definition at line 1309 of file execParallel.c.

1310 {
1311  if (planstate == NULL)
1312  return false;
1313 
1314  switch (nodeTag(planstate))
1315  {
1316  case T_SeqScanState:
1317  if (planstate->plan->parallel_aware)
1318  ExecSeqScanInitializeWorker((SeqScanState *) planstate, pwcxt);
1319  break;
1320  case T_IndexScanState:
1321  if (planstate->plan->parallel_aware)
1323  pwcxt);
1324  break;
1325  case T_IndexOnlyScanState:
1326  if (planstate->plan->parallel_aware)
1328  pwcxt);
1329  break;
1330  case T_ForeignScanState:
1331  if (planstate->plan->parallel_aware)
1333  pwcxt);
1334  break;
1335  case T_AppendState:
1336  if (planstate->plan->parallel_aware)
1337  ExecAppendInitializeWorker((AppendState *) planstate, pwcxt);
1338  break;
1339  case T_CustomScanState:
1340  if (planstate->plan->parallel_aware)
1342  pwcxt);
1343  break;
1344  case T_BitmapHeapScanState:
1345  if (planstate->plan->parallel_aware)
1347  pwcxt);
1348  break;
1349  case T_HashJoinState:
1350  if (planstate->plan->parallel_aware)
1352  pwcxt);
1353  break;
1354  case T_HashState:
1355  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1356  ExecHashInitializeWorker((HashState *) planstate, pwcxt);
1357  break;
1358  case T_SortState:
1359  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1360  ExecSortInitializeWorker((SortState *) planstate, pwcxt);
1361  break;
1362  case T_IncrementalSortState:
1363  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1365  pwcxt);
1366  break;
1367  case T_AggState:
1368  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1369  ExecAggInitializeWorker((AggState *) planstate, pwcxt);
1370  break;
1371  case T_MemoizeState:
1372  /* even when not parallel-aware, for EXPLAIN ANALYZE */
1373  ExecMemoizeInitializeWorker((MemoizeState *) planstate, pwcxt);
1374  break;
1375  default:
1376  break;
1377  }
1378 
1380  pwcxt);
1381 }
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
void ExecAggInitializeWorker(AggState *node, ParallelWorkerContext *pwcxt)
Definition: nodeAgg.c:4729
void ExecAppendInitializeWorker(AppendState *node, ParallelWorkerContext *pwcxt)
Definition: nodeAppend.c:540
void ExecBitmapHeapInitializeWorker(BitmapHeapScanState *node, ParallelWorkerContext *pwcxt)
void ExecCustomScanInitializeWorker(CustomScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeCustom.c:205
void ExecForeignScanInitializeWorker(ForeignScanState *node, ParallelWorkerContext *pwcxt)
void ExecHashInitializeWorker(HashState *node, ParallelWorkerContext *pwcxt)
Definition: nodeHash.c:2778
void ExecHashJoinInitializeWorker(HashJoinState *state, ParallelWorkerContext *pwcxt)
void ExecIncrementalSortInitializeWorker(IncrementalSortState *node, ParallelWorkerContext *pwcxt)
void ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, ParallelWorkerContext *pwcxt)
void ExecIndexScanInitializeWorker(IndexScanState *node, ParallelWorkerContext *pwcxt)
void ExecMemoizeInitializeWorker(MemoizeState *node, ParallelWorkerContext *pwcxt)
Definition: nodeMemoize.c:1235
void ExecSeqScanInitializeWorker(SeqScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSeqscan.c:294
void ExecSortInitializeWorker(SortState *node, ParallelWorkerContext *pwcxt)
Definition: nodeSort.c:462

References ExecAggInitializeWorker(), ExecAppendInitializeWorker(), ExecBitmapHeapInitializeWorker(), ExecCustomScanInitializeWorker(), ExecForeignScanInitializeWorker(), ExecHashInitializeWorker(), ExecHashJoinInitializeWorker(), ExecIncrementalSortInitializeWorker(), ExecIndexOnlyScanInitializeWorker(), ExecIndexScanInitializeWorker(), ExecMemoizeInitializeWorker(), ExecSeqScanInitializeWorker(), ExecSortInitializeWorker(), nodeTag, Plan::parallel_aware, PlanState::plan, and planstate_tree_walker.

Referenced by ParallelQueryMain().

◆ ExecParallelReinitialize()

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

Definition at line 904 of file execParallel.c.

907 {
908  EState *estate = planstate->state;
910 
911  /* Old workers must already be shut down */
912  Assert(pei->finished);
913 
914  /*
915  * Force any initplan outputs that we're going to pass to workers to be
916  * evaluated, if they weren't already (see comments in
917  * ExecInitParallelPlan).
918  */
919  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
920 
922  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
923  pei->reader = NULL;
924  pei->finished = false;
925 
926  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
927 
928  /* Free any serialized parameters from the last round. */
929  if (DsaPointerIsValid(fpes->param_exec))
930  {
931  dsa_free(pei->area, fpes->param_exec);
933  }
934 
935  /* Serialize current parameter values if required. */
936  if (!bms_is_empty(sendParams))
937  {
938  pei->param_exec = SerializeParamExecParams(estate, sendParams,
939  pei->area);
940  fpes->param_exec = pei->param_exec;
941  }
942 
943  /* Traverse plan tree and let each child node reset associated state. */
944  estate->es_query_dsa = pei->area;
945  ExecParallelReInitializeDSM(planstate, pei->pcxt);
946  estate->es_query_dsa = NULL;
947 }
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:488
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:953
EState * state
Definition: execnodes.h:1045

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, FixedParallelExecutorState::param_exec, ParallelExecutorInfo::param_exec, ParallelExecutorInfo::pcxt, ParallelExecutorInfo::reader, ReinitializeParallelDSM(), SerializeParamExecParams(), shm_toc_lookup(), PlanState::state, ParallelContext::toc, and ParallelExecutorInfo::tqueue.

Referenced by ExecGather(), and ExecGatherMerge().

◆ ExecParallelReInitializeDSM()

static bool ExecParallelReInitializeDSM ( PlanState planstate,
ParallelContext pcxt 
)
static

Definition at line 953 of file execParallel.c.

955 {
956  if (planstate == NULL)
957  return false;
958 
959  /*
960  * Call reinitializers for DSM-using plan nodes.
961  */
962  switch (nodeTag(planstate))
963  {
964  case T_SeqScanState:
965  if (planstate->plan->parallel_aware)
967  pcxt);
968  break;
969  case T_IndexScanState:
970  if (planstate->plan->parallel_aware)
972  pcxt);
973  break;
974  case T_IndexOnlyScanState:
975  if (planstate->plan->parallel_aware)
977  pcxt);
978  break;
979  case T_ForeignScanState:
980  if (planstate->plan->parallel_aware)
982  pcxt);
983  break;
984  case T_AppendState:
985  if (planstate->plan->parallel_aware)
986  ExecAppendReInitializeDSM((AppendState *) planstate, pcxt);
987  break;
988  case T_CustomScanState:
989  if (planstate->plan->parallel_aware)
991  pcxt);
992  break;
993  case T_BitmapHeapScanState:
994  if (planstate->plan->parallel_aware)
996  pcxt);
997  break;
998  case T_HashJoinState:
999  if (planstate->plan->parallel_aware)
1001  pcxt);
1002  break;
1003  case T_HashState:
1004  case T_SortState:
1005  case T_IncrementalSortState:
1006  case T_MemoizeState:
1007  /* these nodes have DSM state, but no reinitialization is required */
1008  break;
1009 
1010  default:
1011  break;
1012  }
1013 
1014  return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt);
1015 }
void ExecAppendReInitializeDSM(AppendState *node, ParallelContext *pcxt)
Definition: nodeAppend.c:524
void ExecBitmapHeapReInitializeDSM(BitmapHeapScanState *node, ParallelContext *pcxt)
void ExecCustomScanReInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:190
void ExecForeignScanReInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecHashJoinReInitializeDSM(HashJoinState *state, ParallelContext *pcxt)
void ExecIndexOnlyScanReInitializeDSM(IndexOnlyScanState *node, ParallelContext *pcxt)
void ExecIndexScanReInitializeDSM(IndexScanState *node, ParallelContext *pcxt)
void ExecSeqScanReInitializeDSM(SeqScanState *node, ParallelContext *pcxt)
Definition: nodeSeqscan.c:278

References ExecAppendReInitializeDSM(), ExecBitmapHeapReInitializeDSM(), ExecCustomScanReInitializeDSM(), ExecForeignScanReInitializeDSM(), ExecHashJoinReInitializeDSM(), ExecIndexOnlyScanReInitializeDSM(), ExecIndexScanReInitializeDSM(), ExecSeqScanReInitializeDSM(), nodeTag, Plan::parallel_aware, PlanState::plan, and planstate_tree_walker.

Referenced by ExecParallelReinitialize().

◆ ExecParallelReportInstrumentation()

static bool ExecParallelReportInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 1268 of file execParallel.c.

1270 {
1271  int i;
1272  int plan_node_id = planstate->plan->plan_node_id;
1273  Instrumentation *instrument;
1274 
1275  InstrEndLoop(planstate->instrument);
1276 
1277  /*
1278  * If we shuffled the plan_node_id values in ps_instrument into sorted
1279  * order, we could use binary search here. This might matter someday if
1280  * we're pushing down sufficiently large plan trees. For now, do it the
1281  * slow, dumb way.
1282  */
1283  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1284  if (instrumentation->plan_node_id[i] == plan_node_id)
1285  break;
1286  if (i >= instrumentation->num_plan_nodes)
1287  elog(ERROR, "plan node %d not found", plan_node_id);
1288 
1289  /*
1290  * Add our statistics to the per-node, per-worker totals. It's possible
1291  * that this could happen more than once if we relaunched workers.
1292  */
1293  instrument = GetInstrumentationArray(instrumentation);
1294  instrument += i * instrumentation->num_workers;
1296  Assert(ParallelWorkerNumber < instrumentation->num_workers);
1297  InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
1298 
1300  instrumentation);
1301 }
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
#define IsParallelWorker()
Definition: parallel.h:60
void InstrEndLoop(Instrumentation *instr)
Definition: instrument.c:140
void InstrAggNode(Instrumentation *dst, Instrumentation *add)
Definition: instrument.c:169
Instrumentation * instrument
Definition: execnodes.h:1053

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

◆ ExecParallelRetrieveInstrumentation()

static bool ExecParallelRetrieveInstrumentation ( PlanState planstate,
SharedExecutorInstrumentation instrumentation 
)
static

Definition at line 1022 of file execParallel.c.

1024 {
1025  Instrumentation *instrument;
1026  int i;
1027  int n;
1028  int ibytes;
1029  int plan_node_id = planstate->plan->plan_node_id;
1030  MemoryContext oldcontext;
1031 
1032  /* Find the instrumentation for this node. */
1033  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1034  if (instrumentation->plan_node_id[i] == plan_node_id)
1035  break;
1036  if (i >= instrumentation->num_plan_nodes)
1037  elog(ERROR, "plan node %d not found", plan_node_id);
1038 
1039  /* Accumulate the statistics from all workers. */
1040  instrument = GetInstrumentationArray(instrumentation);
1041  instrument += i * instrumentation->num_workers;
1042  for (n = 0; n < instrumentation->num_workers; ++n)
1043  InstrAggNode(planstate->instrument, &instrument[n]);
1044 
1045  /*
1046  * Also store the per-worker detail.
1047  *
1048  * Worker instrumentation should be allocated in the same context as the
1049  * regular instrumentation information, which is the per-query context.
1050  * Switch into per-query memory context.
1051  */
1052  oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
1053  ibytes = mul_size(instrumentation->num_workers, sizeof(Instrumentation));
1054  planstate->worker_instrument =
1055  palloc(ibytes + offsetof(WorkerInstrumentation, instrument));
1056  MemoryContextSwitchTo(oldcontext);
1057 
1058  planstate->worker_instrument->num_workers = instrumentation->num_workers;
1059  memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
1060 
1061  /* Perform any node-type-specific work that needs to be done. */
1062  switch (nodeTag(planstate))
1063  {
1064  case T_SortState:
1066  break;
1067  case T_IncrementalSortState:
1069  break;
1070  case T_HashState:
1072  break;
1073  case T_AggState:
1075  break;
1076  case T_MemoizeState:
1078  break;
1079  default:
1080  break;
1081  }
1082 
1084  instrumentation);
1085 }
void ExecAggRetrieveInstrumentation(AggState *node)
Definition: nodeAgg.c:4742
void ExecHashRetrieveInstrumentation(HashState *node)
Definition: nodeHash.c:2819
void ExecIncrementalSortRetrieveInstrumentation(IncrementalSortState *node)
void ExecMemoizeRetrieveInstrumentation(MemoizeState *node)
Definition: nodeMemoize.c:1248
void ExecSortRetrieveInstrumentation(SortState *node)
Definition: nodeSort.c:476
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
MemoryContext es_query_cxt
Definition: execnodes.h:665
WorkerInstrumentation * worker_instrument
Definition: execnodes.h:1054
Instrumentation instrument[FLEXIBLE_ARRAY_MEMBER]
Definition: instrument.h:98

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

Referenced by ExecParallelCleanup().

◆ ExecParallelRetrieveJitInstrumentation()

static void ExecParallelRetrieveJitInstrumentation ( PlanState planstate,
SharedJitInstrumentation shared_jit 
)
static

Definition at line 1091 of file execParallel.c.

1093 {
1094  JitInstrumentation *combined;
1095  int ibytes;
1096 
1097  int n;
1098 
1099  /*
1100  * Accumulate worker JIT instrumentation into the combined JIT
1101  * instrumentation, allocating it if required.
1102  */
1103  if (!planstate->state->es_jit_worker_instr)
1104  planstate->state->es_jit_worker_instr =
1106  combined = planstate->state->es_jit_worker_instr;
1107 
1108  /* Accumulate all the workers' instrumentations. */
1109  for (n = 0; n < shared_jit->num_workers; ++n)
1110  InstrJitAgg(combined, &shared_jit->jit_instr[n]);
1111 
1112  /*
1113  * Store the per-worker detail.
1114  *
1115  * Similar to ExecParallelRetrieveInstrumentation(), allocate the
1116  * instrumentation in per-query context.
1117  */
1118  ibytes = offsetof(SharedJitInstrumentation, jit_instr)
1119  + mul_size(shared_jit->num_workers, sizeof(JitInstrumentation));
1120  planstate->worker_jit_instrument =
1121  MemoryContextAlloc(planstate->state->es_query_cxt, ibytes);
1122 
1123  memcpy(planstate->worker_jit_instrument, shared_jit, ibytes);
1124 }
void InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
Definition: jit.c:182
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1202
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1168
struct JitInstrumentation * es_jit_worker_instr
Definition: execnodes.h:715
struct SharedJitInstrumentation * worker_jit_instrument
Definition: execnodes.h:1057

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

Referenced by ExecParallelCleanup().

◆ ExecParallelSetupTupleQueues()

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

Definition at line 535 of file execParallel.c.

536 {
537  shm_mq_handle **responseq;
538  char *tqueuespace;
539  int i;
540 
541  /* Skip this if no workers. */
542  if (pcxt->nworkers == 0)
543  return NULL;
544 
545  /* Allocate memory for shared memory queue handles. */
546  responseq = (shm_mq_handle **)
547  palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
548 
549  /*
550  * If not reinitializing, allocate space from the DSM for the queues;
551  * otherwise, find the already allocated space.
552  */
553  if (!reinitialize)
554  tqueuespace =
555  shm_toc_allocate(pcxt->toc,
557  pcxt->nworkers));
558  else
559  tqueuespace = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, false);
560 
561  /* Create the queues, and become the receiver for each. */
562  for (i = 0; i < pcxt->nworkers; ++i)
563  {
564  shm_mq *mq;
565 
566  mq = shm_mq_create(tqueuespace +
569 
571  responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
572  }
573 
574  /* Add array of queues to shm_toc, so others can find it. */
575  if (!reinitialize)
576  shm_toc_insert(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, tqueuespace);
577 
578  /* Return array of handles. */
579  return responseq;
580 }
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:177
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:206

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

◆ ExecSerializePlan()

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

Definition at line 145 of file execParallel.c.

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 = pgstat_get_my_query_id();
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->permInfos = estate->es_rteperminfos;
186  pstmt->resultRelations = NIL;
187  pstmt->appendRelations = NIL;
188 
189  /*
190  * Transfer only parallel-safe subplans, leaving a NULL "hole" in the list
191  * for unsafe ones (so that the list indexes of the safe ones are
192  * preserved). This positively ensures that the worker won't try to run,
193  * or even do ExecInitNode on, an unsafe subplan. That's important to
194  * protect, eg, non-parallel-aware FDWs from getting into trouble.
195  */
196  pstmt->subplans = NIL;
197  foreach(lc, estate->es_plannedstmt->subplans)
198  {
199  Plan *subplan = (Plan *) lfirst(lc);
200 
201  if (subplan && !subplan->parallel_safe)
202  subplan = NULL;
203  pstmt->subplans = lappend(pstmt->subplans, subplan);
204  }
205 
206  pstmt->rewindPlanIDs = NULL;
207  pstmt->rowMarks = NIL;
208  pstmt->relationOids = NIL;
209  pstmt->invalItems = NIL; /* workers can't replan anyway... */
210  pstmt->paramExecTypes = estate->es_plannedstmt->paramExecTypes;
211  pstmt->utilityStmt = NULL;
212  pstmt->stmt_location = -1;
213  pstmt->stmt_len = -1;
214 
215  /* Return serialized copy of our dummy PlannedStmt. */
216  return nodeToString(pstmt);
217 }
uint64 pgstat_get_my_query_id(void)
List * lappend(List *list, void *datum)
Definition: list.c:339
#define copyObject(obj)
Definition: nodes.h:223
@ CMD_SELECT
Definition: nodes.h:255
#define makeNode(_type_)
Definition: nodes.h:155
char * nodeToString(const void *obj)
Definition: outfuncs.c:763
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define NIL
Definition: pg_list.h:68
#define plan(x)
Definition: pg_regress.c:162
List * es_range_table
Definition: execnodes.h:624
List * es_rteperminfos
Definition: execnodes.h:630
bool parallel_safe
Definition: plannodes.h:141
struct Plan * planTree
Definition: plannodes.h:70
bool hasModifyingCTE
Definition: plannodes.h:58
List * appendRelations
Definition: plannodes.h:80
List * permInfos
Definition: plannodes.h:74
bool canSetTag
Definition: plannodes.h:60
List * rowMarks
Definition: plannodes.h:87
int stmt_location
Definition: plannodes.h:98
int stmt_len
Definition: plannodes.h:99
Bitmapset * rewindPlanIDs
Definition: plannodes.h:85
bool hasReturning
Definition: plannodes.h:56
List * invalItems
Definition: plannodes.h:91
bool transientPlan
Definition: plannodes.h:62
List * resultRelations
Definition: plannodes.h:78
List * subplans
Definition: plannodes.h:82
List * relationOids
Definition: plannodes.h:89
bool dependsOnRole
Definition: plannodes.h:64
CmdType commandType
Definition: plannodes.h:52
Node * utilityStmt
Definition: plannodes.h:95
List * rtable
Definition: plannodes.h:72
bool parallelModeNeeded
Definition: plannodes.h:66
uint64 queryId
Definition: plannodes.h:54

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

Referenced by ExecInitParallelPlan().

◆ ParallelQueryMain()

void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)

Definition at line 1400 of file execParallel.c.

1401 {
1403  BufferUsage *buffer_usage;
1404  WalUsage *wal_usage;
1405  DestReceiver *receiver;
1406  QueryDesc *queryDesc;
1407  SharedExecutorInstrumentation *instrumentation;
1408  SharedJitInstrumentation *jit_instrumentation;
1409  int instrument_options = 0;
1410  void *area_space;
1411  dsa_area *area;
1412  ParallelWorkerContext pwcxt;
1413 
1414  /* Get fixed-size state. */
1415  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1416 
1417  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1418  receiver = ExecParallelGetReceiver(seg, toc);
1419  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1420  if (instrumentation != NULL)
1421  instrument_options = instrumentation->instrument_options;
1422  jit_instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
1423  true);
1424  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1425 
1426  /* Setting debug_query_string for individual workers */
1427  debug_query_string = queryDesc->sourceText;
1428 
1429  /* Report workers' query for monitoring purposes */
1431 
1432  /* Attach to the dynamic shared memory area. */
1433  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1434  area = dsa_attach_in_place(area_space, seg);
1435 
1436  /* Start up the executor */
1437  queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1438  ExecutorStart(queryDesc, fpes->eflags);
1439 
1440  /* Special executor initialization steps for parallel workers */
1441  queryDesc->planstate->state->es_query_dsa = area;
1442  if (DsaPointerIsValid(fpes->param_exec))
1443  {
1444  char *paramexec_space;
1445 
1446  paramexec_space = dsa_get_address(area, fpes->param_exec);
1447  RestoreParamExecParams(paramexec_space, queryDesc->estate);
1448  }
1449  pwcxt.toc = toc;
1450  pwcxt.seg = seg;
1451  ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1452 
1453  /* Pass down any tuple bound */
1454  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1455 
1456  /*
1457  * Prepare to track buffer/WAL usage during query execution.
1458  *
1459  * We do this after starting up the executor to match what happens in the
1460  * leader, which also doesn't count buffer accesses and WAL activity that
1461  * occur during executor startup.
1462  */
1464 
1465  /*
1466  * Run the plan. If we specified a tuple bound, be careful not to demand
1467  * more tuples than that.
1468  */
1469  ExecutorRun(queryDesc,
1471  fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed,
1472  true);
1473 
1474  /* Shut down the executor */
1475  ExecutorFinish(queryDesc);
1476 
1477  /* Report buffer/WAL usage during parallel execution. */
1478  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1479  wal_usage = shm_toc_lookup(toc, PARALLEL_KEY_WAL_USAGE, false);
1481  &wal_usage[ParallelWorkerNumber]);
1482 
1483  /* Report instrumentation data if any instrumentation options are set. */
1484  if (instrumentation != NULL)
1486  instrumentation);
1487 
1488  /* Report JIT instrumentation data if any */
1489  if (queryDesc->estate->es_jit && jit_instrumentation != NULL)
1490  {
1491  Assert(ParallelWorkerNumber < jit_instrumentation->num_workers);
1492  jit_instrumentation->jit_instr[ParallelWorkerNumber] =
1493  queryDesc->estate->es_jit->instr;
1494  }
1495 
1496  /* Must do this after capturing instrumentation. */
1497  ExecutorEnd(queryDesc);
1498 
1499  /* Cleanup. */
1500  dsa_detach(area);
1501  FreeQueryDesc(queryDesc);
1502  receiver->rDestroy(receiver);
1503 }
void pgstat_report_activity(BackendState state, const char *cmd_str)
@ STATE_RUNNING
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:955
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:558
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:467
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:407
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:124
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:297
static DestReceiver * ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
static QueryDesc * ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver, int instrument_options)
static void RestoreParamExecParams(char *start_address, EState *estate)
Definition: execParallel.c:409
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:843
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:208
void InstrStartParallelQuery(void)
Definition: instrument.c:200
const char * debug_query_string
Definition: postgres.c:87
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
@ ForwardScanDirection
Definition: sdir.h:28
struct JitContext * es_jit
Definition: execnodes.h:714
JitInstrumentation instr
Definition: jit.h:64
dsm_segment * seg
Definition: parallel.h:52
int jitFlags
Definition: plannodes.h:68
const char * sourceText
Definition: execdesc.h:38
EState * estate
Definition: execdesc.h:48
PlannedStmt * plannedstmt
Definition: execdesc.h:37
PlanState * planstate
Definition: execdesc.h:49
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:125
Definition: dsa.c:366

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.

◆ RestoreParamExecParams()

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

Definition at line 409 of file execParallel.c.

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

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

Referenced by ParallelQueryMain().

◆ SerializeParamExecParams()

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

Definition at line 354 of file execParallel.c.

355 {
356  Size size;
357  int nparams;
358  int paramid;
359  ParamExecData *prm;
360  dsa_pointer handle;
361  char *start_address;
362 
363  /* Allocate enough space for the current parameter values. */
364  size = EstimateParamExecSpace(estate, params);
365  handle = dsa_allocate(area, size);
366  start_address = dsa_get_address(area, handle);
367 
368  /* First write the number of parameters as a 4-byte integer. */
369  nparams = bms_num_members(params);
370  memcpy(start_address, &nparams, sizeof(int));
371  start_address += sizeof(int);
372 
373  /* Write details for each parameter in turn. */
374  paramid = -1;
375  while ((paramid = bms_next_member(params, paramid)) >= 0)
376  {
377  Oid typeOid;
378  int16 typLen;
379  bool typByVal;
380 
381  prm = &(estate->es_param_exec_vals[paramid]);
382  typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
383  paramid);
384 
385  /* Write paramid. */
386  memcpy(start_address, &paramid, sizeof(int));
387  start_address += sizeof(int);
388 
389  /* Write datum/isnull */
390  if (OidIsValid(typeOid))
391  get_typlenbyval(typeOid, &typLen, &typByVal);
392  else
393  {
394  /* If no type OID, assume by-value, like copyParamList does. */
395  typLen = sizeof(Datum);
396  typByVal = true;
397  }
398  datumSerialize(prm->value, prm->isnull, typByVal, typLen,
399  &start_address);
400  }
401 
402  return handle;
403 }
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
void datumSerialize(Datum value, bool isnull, bool typByVal, int typLen, char **start_address)
Definition: datum.c:459
uint64 dsa_pointer
Definition: dsa.h:62
#define dsa_allocate(area, size)
Definition: dsa.h:84
static Size EstimateParamExecSpace(EState *estate, Bitmapset *params)
Definition: execParallel.c:310
static pg_noinline void Size size
Definition: slab.c:607

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, size, and ParamExecData::value.

Referenced by ExecInitParallelPlan(), and ExecParallelReinitialize().