diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 3e0ee87..c644998 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -62,6 +62,7 @@ #define PARALLEL_KEY_ACTIVE_SNAPSHOT UINT64CONST(0xFFFFFFFFFFFF0007) #define PARALLEL_KEY_TRANSACTION_STATE UINT64CONST(0xFFFFFFFFFFFF0008) #define PARALLEL_KEY_EXTENSION_TRAMPOLINE UINT64CONST(0xFFFFFFFFFFFF0009) +#define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xE000000000000010) /* Fixed-size parallel state. */ typedef struct FixedParallelState @@ -1043,6 +1044,7 @@ ParallelWorkerMain(Datum main_arg) /* Restore database connection. */ BackgroundWorkerInitializeConnectionByOid(fps->database_id, fps->authenticated_user_id); + pgstat_report_activity(STATE_RUNNING, shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT)); /* * Set the client encoding to the database encoding, since that is what diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c index ff277d3..49cdafa 100644 --- a/src/backend/executor/execMain.c +++ b/src/backend/executor/execMain.c @@ -176,6 +176,7 @@ standard_ExecutorStart(QueryDesc *queryDesc, int eflags) */ estate = CreateExecutorState(); queryDesc->estate = estate; + estate->es_queryString = queryDesc->sourceText; oldcontext = MemoryContextSwitchTo(estate->es_query_cxt); diff --git a/src/backend/executor/execParallel.c b/src/backend/executor/execParallel.c index 6cf62da..d6446cf 100644 --- a/src/backend/executor/execParallel.c +++ b/src/backend/executor/execParallel.c @@ -51,6 +51,7 @@ #define PARALLEL_KEY_DSA UINT64CONST(0xE000000000000006) #define PARALLEL_TUPLE_QUEUE_SIZE 65536 +#define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xE000000000000010) /* * DSM structure for accumulating per-PlanState instrumentation. @@ -348,6 +349,12 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, int nworkers) int instrumentation_len = 0; int instrument_offset = 0; Size dsa_minsize = dsa_minimum_size(); + char *query_string; + char *query_data; + int query_len; + + query_data = (char *)palloc(strlen(estate->es_queryString)); + strcpy(query_data, estate->es_queryString); /* Allocate object for return value. */ pei = palloc0(sizeof(ParallelExecutorInfo)); @@ -420,6 +427,10 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, int nworkers) shm_toc_estimate_chunk(&pcxt->estimator, dsa_minsize); shm_toc_estimate_keys(&pcxt->estimator, 1); + query_len = strlen(query_data) + 1; + shm_toc_estimate_chunk(&pcxt->estimator, query_len); + shm_toc_estimate_keys(&pcxt->estimator, 1); + /* Everyone's had a chance to ask for space, so now create the DSM. */ InitializeParallelDSM(pcxt); @@ -450,6 +461,11 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, int nworkers) /* Set up tuple queues. */ pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false); + /* Store query string */ + query_string = shm_toc_allocate(pcxt->toc, query_len); + memcpy(query_string, query_data, query_len); + shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string); + /* * If instrumentation options were supplied, allocate space for the data. * It only gets partially initialized here; the rest happens during @@ -641,6 +657,10 @@ ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver, char *paramspace; PlannedStmt *pstmt; ParamListInfo paramLI; + char *queryString; + + /* Get the query string from shared memory */ + queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT); /* Reconstruct leader-supplied PlannedStmt. */ pstmtspace = shm_toc_lookup(toc, PARALLEL_KEY_PLANNEDSTMT); @@ -659,7 +679,7 @@ ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver, * revising this someday. */ return CreateQueryDesc(pstmt, - "", + queryString, GetActiveSnapshot(), InvalidSnapshot, receiver, paramLI, instrument_options); } diff --git a/src/backend/executor/execUtils.c b/src/backend/executor/execUtils.c index 70646fd..82818b7 100644 --- a/src/backend/executor/execUtils.c +++ b/src/backend/executor/execUtils.c @@ -139,6 +139,7 @@ CreateExecutorState(void) estate->es_epqTuple = NULL; estate->es_epqTupleSet = NULL; estate->es_epqScanDone = NULL; + estate->es_queryString = NULL; /* * Return the executor state structure diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index ce13bf7..4a4dafa 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -431,6 +431,7 @@ typedef struct EState /* The per-query shared memory area to use for parallel execution. */ struct dsa_area *es_query_dsa; + const char *es_queryString; /* Query string for passing to workers */ } EState;