PostgreSQL Source Code  git master
execParallel.h File Reference
#include "access/parallel.h"
#include "nodes/execnodes.h"
#include "nodes/parsenodes.h"
#include "nodes/plannodes.h"
#include "utils/dsa.h"
Include dependency graph for execParallel.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ParallelExecutorInfo
 

Typedefs

typedef struct SharedExecutorInstrumentation SharedExecutorInstrumentation
 
typedef struct ParallelExecutorInfo ParallelExecutorInfo
 

Functions

ParallelExecutorInfoExecInitParallelPlan (PlanState *planstate, EState *estate, Bitmapset *sendParam, int nworkers, int64 tuples_needed)
 
void ExecParallelCreateReaders (ParallelExecutorInfo *pei)
 
void ExecParallelFinish (ParallelExecutorInfo *pei)
 
void ExecParallelCleanup (ParallelExecutorInfo *pei)
 
void ExecParallelReinitialize (PlanState *planstate, ParallelExecutorInfo *pei, Bitmapset *sendParam)
 
void ParallelQueryMain (dsm_segment *seg, shm_toc *toc)
 

Typedef Documentation

◆ ParallelExecutorInfo

◆ SharedExecutorInstrumentation

Function Documentation

◆ ExecInitParallelPlan()

ParallelExecutorInfo* ExecInitParallelPlan ( PlanState planstate,
EState estate,
Bitmapset sendParam,
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

◆ 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

◆ ExecParallelReinitialize()

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

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

◆ 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