3939#include "utils/dsa.h"
4040#include "utils/memutils.h"
4141#include "utils/snapmgr.h"
42+ #include "pgstat.h"
4243
4344/*
4445 * Magic numbers for parallel executor communication. We use constants
5152#define PARALLEL_KEY_TUPLE_QUEUE UINT64CONST(0xE000000000000004)
5253#define PARALLEL_KEY_INSTRUMENTATION UINT64CONST(0xE000000000000005)
5354#define PARALLEL_KEY_DSA UINT64CONST(0xE000000000000006)
55+ #define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xE000000000000007)
5456
5557#define PARALLEL_TUPLE_QUEUE_SIZE 65536
5658
@@ -368,6 +370,8 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, int nworkers)
368370 int instrumentation_len = 0 ;
369371 int instrument_offset = 0 ;
370372 Size dsa_minsize = dsa_minimum_size ();
373+ char * query_string ;
374+ int query_len ;
371375
372376 /* Allocate object for return value. */
373377 pei = palloc0 (sizeof (ParallelExecutorInfo ));
@@ -387,6 +391,11 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, int nworkers)
387391 * for the various things we need to store.
388392 */
389393
394+ /* Estimate space for query text. */
395+ query_len = strlen (estate -> es_sourceText );
396+ shm_toc_estimate_chunk (& pcxt -> estimator , query_len );
397+ shm_toc_estimate_keys (& pcxt -> estimator , 1 );
398+
390399 /* Estimate space for serialized PlannedStmt. */
391400 pstmt_len = strlen (pstmt_data ) + 1 ;
392401 shm_toc_estimate_chunk (& pcxt -> estimator , pstmt_len );
@@ -451,6 +460,11 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, int nworkers)
451460 * asked for has been allocated or initialized yet, though, so do that.
452461 */
453462
463+ /* Store query string */
464+ query_string = shm_toc_allocate (pcxt -> toc , query_len );
465+ memcpy (query_string , estate -> es_sourceText , query_len );
466+ shm_toc_insert (pcxt -> toc , PARALLEL_KEY_QUERY_TEXT , query_string );
467+
454468 /* Store serialized PlannedStmt. */
455469 pstmt_space = shm_toc_allocate (pcxt -> toc , pstmt_len );
456470 memcpy (pstmt_space , pstmt_data , pstmt_len );
@@ -661,6 +675,10 @@ ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver,
661675 char * paramspace ;
662676 PlannedStmt * pstmt ;
663677 ParamListInfo paramLI ;
678+ char * queryString ;
679+
680+ /* Get the query string from shared memory */
681+ queryString = shm_toc_lookup (toc , PARALLEL_KEY_QUERY_TEXT );
664682
665683 /* Reconstruct leader-supplied PlannedStmt. */
666684 pstmtspace = shm_toc_lookup (toc , PARALLEL_KEY_PLANNEDSTMT );
@@ -679,7 +697,7 @@ ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver,
679697 * revising this someday.
680698 */
681699 return CreateQueryDesc (pstmt ,
682- "<parallel query>" ,
700+ queryString ,
683701 GetActiveSnapshot (), InvalidSnapshot ,
684702 receiver , paramLI , instrument_options );
685703}
@@ -799,6 +817,12 @@ ParallelQueryMain(dsm_segment *seg, shm_toc *toc)
799817 instrument_options = instrumentation -> instrument_options ;
800818 queryDesc = ExecParallelGetQueryDesc (toc , receiver , instrument_options );
801819
820+ /* Setting debug_query_string for individual workers */
821+ debug_query_string = queryDesc -> sourceText ;
822+
823+ /* Report workers' query for monitoring purposes */
824+ pgstat_report_activity (STATE_RUNNING , debug_query_string );
825+
802826 /* Prepare to track buffer usage during query execution. */
803827 InstrStartParallelQuery ();
804828
0 commit comments