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 *sendParams, int nworkers, int64 tuples_needed)
 
void ExecParallelCreateReaders (ParallelExecutorInfo *pei)
 
void ExecParallelFinish (ParallelExecutorInfo *pei)
 
void ExecParallelCleanup (ParallelExecutorInfo *pei)
 
void ExecParallelReinitialize (PlanState *planstate, ParallelExecutorInfo *pei, Bitmapset *sendParams)
 
void ParallelQueryMain (dsm_segment *seg, shm_toc *toc)
 

Typedef Documentation

◆ ParallelExecutorInfo

◆ SharedExecutorInstrumentation

Function Documentation

◆ ExecInitParallelPlan()

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

Definition at line 598 of file execParallel.c.

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

1209{
1210 /* Accumulate instrumentation, if any. */
1211 if (pei->instrumentation)
1213 pei->instrumentation);
1214
1215 /* Accumulate JIT instrumentation, if any. */
1216 if (pei->jit_instrumentation)
1218 pei->jit_instrumentation);
1219
1220 /* Free any serialized parameters. */
1221 if (DsaPointerIsValid(pei->param_exec))
1222 {
1223 dsa_free(pei->area, pei->param_exec);
1225 }
1226 if (pei->area != NULL)
1227 {
1228 dsa_detach(pei->area);
1229 pei->area = NULL;
1230 }
1231 if (pei->pcxt != NULL)
1232 {
1234 pei->pcxt = NULL;
1235 }
1236 pfree(pei);
1237}
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:950
void dsa_detach(dsa_area *area)
Definition: dsa.c:1952
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:826
#define DsaPointerIsValid(x)
Definition: dsa.h:106
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
static void ExecParallelRetrieveJitInstrumentation(PlanState *planstate, SharedJitInstrumentation *shared_jit)
void pfree(void *pointer)
Definition: mcxt.c:1524

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 889 of file execParallel.c.

890{
891 int nworkers = pei->pcxt->nworkers_launched;
892 int i;
893
894 Assert(pei->reader == NULL);
895
896 if (nworkers > 0)
897 {
898 pei->reader = (TupleQueueReader **)
899 palloc(nworkers * sizeof(TupleQueueReader *));
900
901 for (i = 0; i < nworkers; i++)
902 {
904 pei->pcxt->worker[i].bgwhandle);
905 pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
906 }
907 }
908}
void * palloc(Size size)
Definition: mcxt.c:1317
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().

◆ ExecParallelFinish()

void ExecParallelFinish ( ParallelExecutorInfo pei)

Definition at line 1155 of file execParallel.c.

1156{
1157 int nworkers = pei->pcxt->nworkers_launched;
1158 int i;
1159
1160 /* Make this be a no-op if called twice in a row. */
1161 if (pei->finished)
1162 return;
1163
1164 /*
1165 * Detach from tuple queues ASAP, so that any still-active workers will
1166 * notice that no further results are wanted.
1167 */
1168 if (pei->tqueue != NULL)
1169 {
1170 for (i = 0; i < nworkers; i++)
1171 shm_mq_detach(pei->tqueue[i]);
1172 pfree(pei->tqueue);
1173 pei->tqueue = NULL;
1174 }
1175
1176 /*
1177 * While we're waiting for the workers to finish, let's get rid of the
1178 * tuple queue readers. (Any other local cleanup could be done here too.)
1179 */
1180 if (pei->reader != NULL)
1181 {
1182 for (i = 0; i < nworkers; i++)
1184 pfree(pei->reader);
1185 pei->reader = NULL;
1186 }
1187
1188 /* Now wait for the workers to finish. */
1190
1191 /*
1192 * Next, accumulate buffer/WAL usage. (This must wait for the workers to
1193 * finish, or we might get incomplete data.)
1194 */
1195 for (i = 0; i < nworkers; i++)
1197
1198 pei->finished = true;
1199}
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:796
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().

◆ ExecParallelReinitialize()

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

Definition at line 915 of file execParallel.c.

918{
919 EState *estate = planstate->state;
921
922 /* Old workers must already be shut down */
923 Assert(pei->finished);
924
925 /*
926 * Force any initplan outputs that we're going to pass to workers to be
927 * evaluated, if they weren't already (see comments in
928 * ExecInitParallelPlan).
929 */
931
933 pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
934 pei->reader = NULL;
935 pei->finished = false;
936
938
939 /* Free any serialized parameters from the last round. */
940 if (DsaPointerIsValid(fpes->param_exec))
941 {
942 dsa_free(pei->area, fpes->param_exec);
944 }
945
946 /* Serialize current parameter values if required. */
947 if (!bms_is_empty(sendParams))
948 {
949 pei->param_exec = SerializeParamExecParams(estate, sendParams,
950 pei->area);
951 fpes->param_exec = pei->param_exec;
952 }
953
954 /* Traverse plan tree and let each child node reset associated state. */
955 estate->es_query_dsa = pei->area;
956 ExecParallelReInitializeDSM(planstate, pei->pcxt);
957 estate->es_query_dsa = NULL;
958}
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:508
static bool ExecParallelReInitializeDSM(PlanState *planstate, ParallelContext *pcxt)
Definition: execParallel.c:964
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
EState * state
Definition: execnodes.h:1155

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

◆ ParallelQueryMain()

void ParallelQueryMain ( dsm_segment seg,
shm_toc toc 
)

Definition at line 1435 of file execParallel.c.

1436{
1438 BufferUsage *buffer_usage;
1439 WalUsage *wal_usage;
1440 DestReceiver *receiver;
1441 QueryDesc *queryDesc;
1442 SharedExecutorInstrumentation *instrumentation;
1443 SharedJitInstrumentation *jit_instrumentation;
1444 int instrument_options = 0;
1445 void *area_space;
1446 dsa_area *area;
1448
1449 /* Get fixed-size state. */
1450 fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1451
1452 /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1453 receiver = ExecParallelGetReceiver(seg, toc);
1454 instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1455 if (instrumentation != NULL)
1456 instrument_options = instrumentation->instrument_options;
1457 jit_instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
1458 true);
1459 queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1460
1461 /* Setting debug_query_string for individual workers */
1462 debug_query_string = queryDesc->sourceText;
1463
1464 /* Report workers' query for monitoring purposes */
1466
1467 /* Attach to the dynamic shared memory area. */
1468 area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1469 area = dsa_attach_in_place(area_space, seg);
1470
1471 /* Start up the executor */
1472 queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1473 if (!ExecutorStart(queryDesc, fpes->eflags))
1474 elog(ERROR, "ExecutorStart() failed unexpectedly");
1475
1476 /* Special executor initialization steps for parallel workers */
1477 queryDesc->planstate->state->es_query_dsa = area;
1478 if (DsaPointerIsValid(fpes->param_exec))
1479 {
1480 char *paramexec_space;
1481
1482 paramexec_space = dsa_get_address(area, fpes->param_exec);
1483 RestoreParamExecParams(paramexec_space, queryDesc->estate);
1484 }
1485 pwcxt.toc = toc;
1486 pwcxt.seg = seg;
1487 ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1488
1489 /* Pass down any tuple bound */
1490 ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1491
1492 /*
1493 * Prepare to track buffer/WAL usage during query execution.
1494 *
1495 * We do this after starting up the executor to match what happens in the
1496 * leader, which also doesn't count buffer accesses and WAL activity that
1497 * occur during executor startup.
1498 */
1500
1501 /*
1502 * Run the plan. If we specified a tuple bound, be careful not to demand
1503 * more tuples than that.
1504 */
1505 ExecutorRun(queryDesc,
1507 fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed);
1508
1509 /* Shut down the executor */
1510 ExecutorFinish(queryDesc);
1511
1512 /* Report buffer/WAL usage during parallel execution. */
1513 buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1514 wal_usage = shm_toc_lookup(toc, PARALLEL_KEY_WAL_USAGE, false);
1516 &wal_usage[ParallelWorkerNumber]);
1517
1518 /* Report instrumentation data if any instrumentation options are set. */
1519 if (instrumentation != NULL)
1521 instrumentation);
1522
1523 /* Report JIT instrumentation data if any */
1524 if (queryDesc->estate->es_jit && jit_instrumentation != NULL)
1525 {
1526 Assert(ParallelWorkerNumber < jit_instrumentation->num_workers);
1527 jit_instrumentation->jit_instr[ParallelWorkerNumber] =
1528 queryDesc->estate->es_jit->instr;
1529 }
1530
1531 /* Must do this after capturing instrumentation. */
1532 ExecutorEnd(queryDesc);
1533
1534 /* Cleanup. */
1535 dsa_detach(area);
1536 FreeQueryDesc(queryDesc);
1537 receiver->rDestroy(receiver);
1538}
int ParallelWorkerNumber
Definition: parallel.c:115
void pgstat_report_activity(BackendState state, const char *cmd_str)
@ STATE_RUNNING
int64_t int64
Definition: c.h:499
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:545
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:942
bool ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:125
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:535
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:472
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
Definition: execMain.c:362
static QueryDesc * ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver, int instrument_options)
static DestReceiver * ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
static bool ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
static bool ExecParallelReportInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
static void RestoreParamExecParams(char *start_address, EState *estate)
Definition: execParallel.c:417
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:848
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:88
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:112
@ ForwardScanDirection
Definition: sdir.h:28
struct JitContext * es_jit
Definition: execnodes.h:757
JitInstrumentation instr
Definition: jit.h:62
dsm_segment * seg
Definition: parallel.h:52
int jitFlags
Definition: plannodes.h:77
const char * sourceText
Definition: execdesc.h:39
EState * estate
Definition: execdesc.h:49
PlannedStmt * plannedstmt
Definition: execdesc.h:37
PlanState * planstate
Definition: execdesc.h:50
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:126
Definition: dsa.c:348

References Assert(), debug_query_string, dsa_attach_in_place(), dsa_detach(), dsa_get_address(), DsaPointerIsValid, FixedParallelExecutorState::eflags, elog, ERROR, 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.