PostgreSQL Source Code git master
Data Structures | Typedefs | Functions
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

 

Typedefs

 
 

Functions

ParallelExecutorInfoExecInitParallelPlan (PlanState *planstate, EState *estate, Bitmapset *sendParams, int nworkers, int64 tuples_needed)
 
 
 
 
void  ExecParallelReinitialize (PlanState *planstate, ParallelExecutorInfo *pei, Bitmapset *sendParams)
 
 

Typedef Documentation

ParallelExecutorInfo

SharedExecutorInstrumentation

Definition at line 22 of file execParallel.h.

Function Documentation

ExecInitParallelPlan()

ParallelExecutorInfo * ExecInitParallelPlan ( PlanStateplanstate,
EStateestate,
BitmapsetsendParams,
int  nworkers,
int64  tuples_needed 
)

Definition at line 600 of file execParallel.c.

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

Definition at line 1210 of file execParallel.c.

1211{
1212 /* Accumulate instrumentation, if any. */
1213 if (pei->instrumentation)
1215 pei->instrumentation);
1216
1217 /* Accumulate JIT instrumentation, if any. */
1218 if (pei->jit_instrumentation)
1220 pei->jit_instrumentation);
1221
1222 /* Free any serialized parameters. */
1223 if (DsaPointerIsValid(pei->param_exec))
1224 {
1225 dsa_free(pei->area, pei->param_exec);
1227 }
1228 if (pei->area != NULL)
1229 {
1230 dsa_detach(pei->area);
1231 pei->area = NULL;
1232 }
1233 if (pei->pcxt != NULL)
1234 {
1236 pei->pcxt = NULL;
1237 }
1238 pfree(pei);
1239}
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:950
void dsa_detach(dsa_area *area)
Definition: dsa.c:1967
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:841
#define DsaPointerIsValid(x)
Definition: dsa.h:106
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate, SharedExecutorInstrumentation *instrumentation)
Definition: execParallel.c:1036
static void ExecParallelRetrieveJitInstrumentation(PlanState *planstate, SharedJitInstrumentation *shared_jit)
Definition: execParallel.c:1117
void pfree(void *pointer)
Definition: mcxt.c:1594

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

Definition at line 891 of file execParallel.c.

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

Definition at line 1157 of file execParallel.c.

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

Definition at line 917 of file execParallel.c.

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

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_segmentseg,
shm_toctoc 
)

Definition at line 1430 of file execParallel.c.

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

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.

AltStyle によって変換されたページ (->オリジナル) /