diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 3e0ee87..f0ad851 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -62,7 +62,8 @@ #define PARALLEL_KEY_ACTIVE_SNAPSHOT UINT64CONST(0xFFFFFFFFFFFF0007) #define PARALLEL_KEY_TRANSACTION_STATE UINT64CONST(0xFFFFFFFFFFFF0008) #define PARALLEL_KEY_EXTENSION_TRAMPOLINE UINT64CONST(0xFFFFFFFFFFFF0009) - +/* Save query text to pass on to the workers */ +#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 ce6600b..77e7c58 100644 --- a/src/backend/executor/execMain.c +++ b/src/backend/executor/execMain.c @@ -190,6 +190,9 @@ standard_ExecutorStart(QueryDesc *queryDesc, int eflags) estate->es_param_exec_vals = (ParamExecData *) palloc0(queryDesc->plannedstmt->nParamExec * sizeof(ParamExecData)); + estate->es_queryString = (char *)palloc0(strlen(queryDesc->sourceText) + 1); + estate->es_queryString = queryDesc->sourceText; + /* * If non-read-only query, set the command ID to mark output tuples with */ diff --git a/src/backend/executor/execParallel.c b/src/backend/executor/execParallel.c index e01fe6d..af30b85 100644 --- a/src/backend/executor/execParallel.c +++ b/src/backend/executor/execParallel.c @@ -51,7 +51,8 @@ #define PARALLEL_KEY_DSA UINT64CONST(0xE000000000000006) #define PARALLEL_TUPLE_QUEUE_SIZE 65536 - +/* Save query text to pass on to the workers */ +#define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xE000000000000010) /* * DSM structure for accumulating per-PlanState instrumentation. * @@ -350,6 +351,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 *)palloc0(strlen(estate->es_queryString) + 1); + strcpy(query_data, estate->es_queryString); /* Allocate object for return value. */ pei = palloc0(sizeof(ParallelExecutorInfo)); @@ -369,6 +376,11 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, int nworkers) * for the various things we need to store. */ + /*Estimate space for query text. */ + query_len = strlen(query_data); + shm_toc_estimate_chunk(&pcxt->estimator, query_len); + shm_toc_estimate_keys(&pcxt->estimator, 1); + /* Estimate space for serialized PlannedStmt. */ pstmt_len = strlen(pstmt_data) + 1; shm_toc_estimate_chunk(&pcxt->estimator, pstmt_len); @@ -433,6 +445,11 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, int nworkers) * asked for has been allocated or initialized yet, though, so do that. */ + /* 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); + /* Store serialized PlannedStmt. */ pstmt_space = shm_toc_allocate(pcxt->toc, pstmt_len); memcpy(pstmt_space, pstmt_data, pstmt_len); @@ -643,6 +660,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); @@ -661,7 +682,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 e49feff..51d231d 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 f9bcdd6..588ee1e 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -430,6 +430,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;