src/backend/executor/execParallel.c | 8 ++++++-- src/backend/tcop/postgres.c | 41 +++++++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/backend/executor/execParallel.c b/src/backend/executor/execParallel.c index 382e78fb7f..178311c2e6 100644 --- a/src/backend/executor/execParallel.c +++ b/src/backend/executor/execParallel.c @@ -639,7 +639,11 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, shm_toc_estimate_keys(&pcxt->estimator, 1); /* Estimate space for query text. */ - query_len = strlen(estate->es_sourceText); + if (estate->es_plannedstmt->stmt_len == 0) + query_len = strlen(estate->es_sourceText) - estate->es_plannedstmt->stmt_location; + else + query_len = estate->es_plannedstmt->stmt_len; + shm_toc_estimate_chunk(&pcxt->estimator, query_len + 1); shm_toc_estimate_keys(&pcxt->estimator, 1); @@ -734,7 +738,7 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, /* Store query string */ query_string = shm_toc_allocate(pcxt->toc, query_len + 1); - memcpy(query_string, estate->es_sourceText, query_len + 1); + memcpy(query_string, estate->es_sourceText + estate->es_plannedstmt->stmt_location, query_len + 1); shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string); /* Store serialized PlannedStmt. */ diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index c9424f167c..6799d110ef 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -167,6 +167,9 @@ static ProcSignalReason RecoveryConflictReason; static MemoryContext row_description_context = NULL; static StringInfoData row_description_buf; +/* reused buffer to pass the individual queries */ +static StringInfoData individual_query_buf; + /* ---------------------------------------------------------------- * decls for routines only used in this file * ---------------------------------------------------------------- @@ -991,14 +994,6 @@ exec_simple_query(const char *query_string) bool use_implicit_block; char msec_str[32]; - /* - * Report query to various monitoring facilities. - */ - debug_query_string = query_string; - - pgstat_report_activity(STATE_RUNNING, query_string); - - TRACE_POSTGRESQL_QUERY_START(query_string); /* * We use save_log_statement_stats so ShowUsage doesn't report incorrect @@ -1075,7 +1070,35 @@ exec_simple_query(const char *query_string) Portal portal; DestReceiver *receiver; int16 format; + char *individual_query; + int individual_query_length; + /* if statement does not end with ; + * then parsetree->stmt_len == 0 + */ + if (parsetree->stmt_len == 0) + individual_query_length = strlen(query_string) - parsetree->stmt_location; + else + individual_query_length = parsetree->stmt_len + 1; + + /* extract the query text */ + individual_query = palloc(individual_query_length + 1); + strncpy(individual_query, query_string + parsetree->stmt_location, individual_query_length); + individual_query[individual_query_length] = '\0'; + /* + * Report query to various monitoring facilities. + */ + + resetStringInfo(&individual_query_buf); + appendStringInfoString(&individual_query_buf, individual_query); + + debug_query_string = individual_query_buf.data; + + pgstat_report_activity(STATE_RUNNING, debug_query_string); + + TRACE_POSTGRESQL_QUERY_START(debug_query_string); + + pfree(individual_query); /* * Get the command name for use in status display (it also becomes the * default completion tag, down inside PortalRun). Set ps_status and @@ -4011,6 +4034,8 @@ PostgresMain(int argc, char *argv[], initStringInfo(&row_description_buf); MemoryContextSwitchTo(TopMemoryContext); + initStringInfo(&individual_query_buf); + /* * Remember stand-alone backend startup time */