diff --git a/src/common/backend/catalog/pg_proc.cpp b/src/common/backend/catalog/pg_proc.cpp index 5ca671a7a815da24c0db90438c62e37038dfa2ee..14356df945855ccde51f76a6757121d2fe4e8dbd 100644 --- a/src/common/backend/catalog/pg_proc.cpp +++ b/src/common/backend/catalog/pg_proc.cpp @@ -1963,7 +1963,7 @@ Datum fmgr_sql_validator(PG_FUNCTION_ARGS) ErrorContextCallback sqlerrcontext; bool haspolyarg = false; int i; - + NodeTag old_node_tag = t_thrd.postgres_cxt.cur_command_tag; bool replace = false; /* * 3 means the number of arguments of function fmgr_sql_validator, while 'is_replace' is the third one, @@ -2049,7 +2049,7 @@ Datum fmgr_sql_validator(PG_FUNCTION_ARGS) foreach (lc, raw_parsetree_list) { Node* parsetree = (Node*)lfirst(lc); List* querytree_sublist = NIL; - + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(parsetree); #ifdef PGXC /* Block CTAS in SQL functions */ if (IsA(parsetree, CreateTableAsStmt)) @@ -2093,6 +2093,7 @@ Datum fmgr_sql_validator(PG_FUNCTION_ARGS) } ReleaseSysCache(tuple); + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; PG_RETURN_VOID(); } diff --git a/src/common/backend/parser/analyze.cpp b/src/common/backend/parser/analyze.cpp index 12c7763ec25b78985f8925289af2f7a26b8d5ed1..b00bca0436d9b3fdcedd458beb8e11650e237797 100644 --- a/src/common/backend/parser/analyze.cpp +++ b/src/common/backend/parser/analyze.cpp @@ -4569,6 +4569,7 @@ static Query* transformExplainStmt(ParseState* pstate, ExplainStmt* stmt) { Query* result = NULL; + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(stmt->query); /* transform contained query, allowing SELECT INTO */ stmt->query = (Node*)transformTopLevelStmt(pstate, stmt->query); diff --git a/src/common/pl/plpgsql/src/pl_comp.cpp b/src/common/pl/plpgsql/src/pl_comp.cpp index 8b76146e481df832aeade4e1725259cb3cb23465..8255d637a0bb4b337c53da00723de08697b3cd67 100644 --- a/src/common/pl/plpgsql/src/pl_comp.cpp +++ b/src/common/pl/plpgsql/src/pl_comp.cpp @@ -4934,6 +4934,7 @@ TupleDesc getCursorTupleDesc(PLpgSQL_expr* expr, bool isOnlySelect, bool isOnlyP expr->func->datums = u_sess->plsql_cxt.curr_compile_context->plpgsql_Datums; expr->func->ndatums = u_sess->plsql_cxt.curr_compile_context->plpgsql_nDatums; TupleDesc tupleDesc = NULL; + NodeTag old_node_tag = t_thrd.postgres_cxt.cur_command_tag; PG_TRY(); { List* parsetreeList = pg_parse_query(expr->query); @@ -4945,6 +4946,7 @@ TupleDesc getCursorTupleDesc(PLpgSQL_expr* expr, bool isOnlySelect, bool isOnlyP List* queryList = NIL; foreach(cell, parsetreeList) { Node *parsetree = (Node *)lfirst(cell); + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(parsetree); if (nodeTag(parsetree) == T_SelectStmt) { if (checkSelectIntoParse((SelectStmt*)parsetree)) { list_free_deep(parsetreeList); @@ -4991,6 +4993,7 @@ TupleDesc getCursorTupleDesc(PLpgSQL_expr* expr, bool isOnlySelect, bool isOnlyP } PG_CATCH(); { + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; /* Save error info */ MemoryContext ecxt = MemoryContextSwitchTo(current_context); ErrorData* edata = CopyErrorData(); @@ -5011,6 +5014,7 @@ TupleDesc getCursorTupleDesc(PLpgSQL_expr* expr, bool isOnlySelect, bool isOnlyP } PG_END_TRY(); + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; return tupleDesc; } static int get_inner_type_ind(Oid typeoid) diff --git a/src/common/pl/plpgsql/src/pl_exec.cpp b/src/common/pl/plpgsql/src/pl_exec.cpp index fde5e6e68df857b06c136ef06cf67dc772589476..ccbd9c319b7433f2759932a3c2b27d9aea5f6bbc 100644 --- a/src/common/pl/plpgsql/src/pl_exec.cpp +++ b/src/common/pl/plpgsql/src/pl_exec.cpp @@ -1303,6 +1303,7 @@ Datum plpgsql_exec_function(PLpgSQL_function* func, FunctionCallInfo fcinfo, boo } #endif + NodeTag old_node_tag = t_thrd.postgres_cxt.cur_command_tag; saved_current_stp_with_exception = plpgsql_get_current_value_stp_with_exception(); /* * Setup error traceback support for ereport() @@ -1532,7 +1533,7 @@ Datum plpgsql_exec_function(PLpgSQL_function* func, FunctionCallInfo fcinfo, boo estate.err_text = gettext_noop("while casting return value to function's return type"); fcinfo->isnull = estate.retisnull; - + t_thrd.postgres_cxt.cur_command_tag = T_CreateStmt; if (estate.retisset) { ReturnSetInfo* rsi = estate.rsi; @@ -1743,7 +1744,7 @@ Datum plpgsql_exec_function(PLpgSQL_function* func, FunctionCallInfo fcinfo, boo /* Clean up any leftover temporary memory */ plpgsql_destroy_econtext(&estate); exec_eval_cleanup(&estate); - + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; /* * Pop the error context stack */ @@ -3915,6 +3916,7 @@ static int exec_stmt(PLpgSQL_execstate* estate, PLpgSQL_stmt* stmt, bool resigna { PLpgSQL_stmt* save_estmt = NULL; int rc = -1; + NodeTag old_command_tag; save_estmt = estate->err_stmt; estate->err_stmt = stmt; @@ -3949,12 +3951,14 @@ static int exec_stmt(PLpgSQL_execstate* estate, PLpgSQL_stmt* stmt, bool resigna } } + old_command_tag = t_thrd.postgres_cxt.cur_command_tag; switch ((enum PLpgSQL_stmt_types)stmt->cmd_type) { case PLPGSQL_STMT_BLOCK: rc = exec_stmt_block(estate, (PLpgSQL_stmt_block*)stmt, NULL, resignal_in_handler); break; case PLPGSQL_STMT_ASSIGN: + t_thrd.postgres_cxt.cur_command_tag = T_CreateStmt; rc = exec_stmt_assign(estate, (PLpgSQL_stmt_assign*)stmt); break; @@ -4011,14 +4015,17 @@ static int exec_stmt(PLpgSQL_execstate* estate, PLpgSQL_stmt* stmt, bool resigna break; case PLPGSQL_STMT_RETURN: + t_thrd.postgres_cxt.cur_command_tag = T_CreateStmt; rc = exec_stmt_return(estate, (PLpgSQL_stmt_return*)stmt); break; case PLPGSQL_STMT_RETURN_NEXT: + t_thrd.postgres_cxt.cur_command_tag = T_CreateStmt; rc = exec_stmt_return_next(estate, (PLpgSQL_stmt_return_next*)stmt); break; case PLPGSQL_STMT_RETURN_QUERY: + t_thrd.postgres_cxt.cur_command_tag = T_SelectStmt; rc = exec_stmt_return_query(estate, (PLpgSQL_stmt_return_query*)stmt); break; @@ -4058,6 +4065,7 @@ static int exec_stmt(PLpgSQL_execstate* estate, PLpgSQL_stmt* stmt, bool resigna } case PLPGSQL_STMT_FETCH: + t_thrd.postgres_cxt.cur_command_tag = T_CreateStmt; rc = exec_stmt_fetch(estate, (PLpgSQL_stmt_fetch*)stmt); break; @@ -4095,6 +4103,7 @@ static int exec_stmt(PLpgSQL_execstate* estate, PLpgSQL_stmt* stmt, bool resigna errmsg("unrecognized statement type: %d for PLSQL function.", stmt->cmd_type))); break; } + t_thrd.postgres_cxt.cur_command_tag = old_command_tag; /* Let the plugin know that we have finished executing this statement */ if (*u_sess->plsql_cxt.plugin_ptr && (*u_sess->plsql_cxt.plugin_ptr)->stmt_end) { @@ -6796,7 +6805,7 @@ static int exec_stmt_execsql(PLpgSQL_execstate* estate, PLpgSQL_stmt_execsql* st Cursor_Data* saved_cursor_data = NULL; bool has_alloc = false; bool multi_res_return = false; - + NodeTag old_node_tag = t_thrd.postgres_cxt.cur_command_tag; TransactionId oldTransactionId = SPI_get_top_transaction_id(); /* @@ -6826,6 +6835,8 @@ static int exec_stmt_execsql(PLpgSQL_execstate* estate, PLpgSQL_stmt_execsql* st } } } + + t_thrd.postgres_cxt.cur_command_tag = stmt->mod_stmt ? T_CreateSetStmt : T_SelectStmt; if (ENABLE_CN_GPC && g_instance.plan_cache->CheckRecreateSPICachePlan(expr->plan)) { g_instance.plan_cache->RecreateSPICachePlan(expr->plan); } @@ -7105,6 +7116,7 @@ static int exec_stmt_execsql(PLpgSQL_execstate* estate, PLpgSQL_stmt_execsql* st estate->cursor_return_data = saved_cursor_data; estate->cursor_return_numbers = saved_cursor_numbers; + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; return PLPGSQL_RC_OK; } diff --git a/src/common/pl/plpgsql/src/pl_handler.cpp b/src/common/pl/plpgsql/src/pl_handler.cpp index 3d1967f08f05502db6f443ab6b438c0e88b60f40..1e0cf297c9fdc62032aaf2586921856c4f69a1ca 100755 --- a/src/common/pl/plpgsql/src/pl_handler.cpp +++ b/src/common/pl/plpgsql/src/pl_handler.cpp @@ -808,6 +808,7 @@ Datum plpgsql_call_handler(PG_FUNCTION_ARGS) /* Must save and restore prior value of cur_estate and debug_info */ save_cur_estate = func->cur_estate; save_debug_info = func->debug; + NodeTag old_node_tag = t_thrd.postgres_cxt.cur_command_tag; // set the procedure's search_path as the current search_path validate_search_path(func); @@ -879,7 +880,7 @@ Datum plpgsql_call_handler(PG_FUNCTION_ARGS) u_sess->plsql_cxt.cur_exception_cxt = NULL; t_thrd.log_cxt.call_stack = saveplcallstack; - + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; #ifndef ENABLE_MULTIPLE_NODES /* for restore parent session and automn session package var values */ @@ -957,6 +958,7 @@ Datum plpgsql_call_handler(PG_FUNCTION_ARGS) DecreasePackageUseCount(func); func->cur_estate = save_cur_estate; func->debug = save_debug_info; + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; // resume the search_path when the procedure has executed PopOverrideSearchPath(); @@ -1127,6 +1129,7 @@ Datum plpgsql_inline_handler(PG_FUNCTION_ARGS) int save_compile_list_length = list_length(u_sess->plsql_cxt.compile_context_list); int save_compile_status = u_sess->plsql_cxt.compile_status; DebugInfo* save_debug_info = func->debug; + NodeTag old_node_tag = t_thrd.postgres_cxt.cur_command_tag; FormatCallStack* saveplcallstack = t_thrd.log_cxt.call_stack; PG_TRY(); { @@ -1163,6 +1166,7 @@ Datum plpgsql_inline_handler(PG_FUNCTION_ARGS) dopControl.ResetSmp(); #endif + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; ereport(DEBUG3, (errmodule(MOD_NEST_COMPILE), errcode(ERRCODE_LOG), errmsg("%s clear curr_compile_context because of error.", __func__))); /* reset nest plpgsql compile */ @@ -1190,6 +1194,7 @@ Datum plpgsql_inline_handler(PG_FUNCTION_ARGS) } func->debug = save_debug_info; #endif + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; if (u_sess->SPI_cxt._connected == 0) { t_thrd.utils_cxt.STPSavedResourceOwner = NULL; } diff --git a/src/gausskernel/optimizer/commands/prepare.cpp b/src/gausskernel/optimizer/commands/prepare.cpp index cd0c556d7a707244a3311cda865db3b1f9bd24de..ca252acc6aed9a6877fe8d012d3f33b80eda077d 100755 --- a/src/gausskernel/optimizer/commands/prepare.cpp +++ b/src/gausskernel/optimizer/commands/prepare.cpp @@ -164,6 +164,7 @@ void PrepareQuery(PrepareStmt* stmt, const char* queryString) stmt->name, #endif CreateCommandTag(stmt->query)); + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(stmt->query); /* Transform list of TypeNames to array of type OIDs */ nargs = list_length(stmt->argtypes); @@ -314,6 +315,7 @@ void ExecuteQuery(ExecuteStmt* stmt, IntoClause* intoClause, const char* querySt /* Look it up in the hash table */ entry = FetchPreparedStatement(stmt->name, true, true); psrc = entry->plansource; + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(psrc->raw_parse_tree); /* Shouldn't find a non-fixed-result cached plan */ if (!entry->plansource->fixed_result) @@ -1761,6 +1763,7 @@ void RePrepareQuery(ExecuteStmt* stmt) */ foreach (parsetree_item, parseTree_list) { Node* parsetree = (Node*)lfirst(parsetree_item); + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(parsetree); List* planTree_list = NIL; queryTree_list = pg_analyze_and_rewrite(parsetree, query_string, NULL, 0); @@ -2152,4 +2155,4 @@ bool quickPlanner(List* querytree_list, Node* parsetree, const char*queryString, if (estate != NULL) FreeExecutorState(estate); return true; -} \ No newline at end of file +} diff --git a/src/gausskernel/process/stream/streamMain.cpp b/src/gausskernel/process/stream/streamMain.cpp index e5d1bb0412c3405fe044acedccce0cc47153d470..222e894b8ca0214ebd594f2ccbe8ca55c26f895e 100755 --- a/src/gausskernel/process/stream/streamMain.cpp +++ b/src/gausskernel/process/stream/streamMain.cpp @@ -141,6 +141,7 @@ int StreamMain() WLMReleaseIoInfoFromHash(); /* Reset here so that we can get debug_query_string when Stream thread is in Sync point */ t_thrd.postgres_cxt.debug_query_string = NULL; + t_thrd.postgres_cxt.cur_command_tag = T_Invalid; /* * Note that parent thread will do commit or abort transaction. @@ -440,6 +441,7 @@ static void execute_stream_plan(StreamProducer* producer) // For now plan shipping is used only for SELECTs, in future // we should remove this hard coding and get the tag automatically commandTag = "SELECT"; + t_thrd.postgres_cxt.cur_command_tag = T_SelectStmt; set_ps_display(commandTag, false); diff --git a/src/gausskernel/process/tcop/postgres.cpp b/src/gausskernel/process/tcop/postgres.cpp index b670a3a6aa173cdc068e642393ac18b64659bb86..b08806c83e89fcc4d045336cf666d22657787276 100755 --- a/src/gausskernel/process/tcop/postgres.cpp +++ b/src/gausskernel/process/tcop/postgres.cpp @@ -2675,6 +2675,7 @@ static void exec_simple_query(const char* query_string, MessageType messageType, * destination. */ commandTag = CreateCommandTag(parsetree); + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(parsetree); set_ps_display(commandTag, false); if (libpqsw_skip_check_readonly()) { @@ -3107,6 +3108,7 @@ static void exec_simple_query(const char* query_string, MessageType messageType, TRACE_POSTGRESQL_QUERY_DONE(query_string); t_thrd.postgres_cxt.debug_query_string = NULL; + t_thrd.postgres_cxt.cur_command_tag = T_Invalid; /* * @hdfs @@ -3694,6 +3696,7 @@ static void exec_parse_message(const char* query_string, /* string to execute */ * Get the command name for possible use in status display. */ commandTag = CreateCommandTag(raw_parse_tree); + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(raw_parse_tree); /* * If we are in an aborted transaction, reject all commands except @@ -3968,6 +3971,7 @@ pass_parsing: ShowUsage("PARSE MESSAGE STATISTICS"); t_thrd.postgres_cxt.debug_query_string = NULL; + t_thrd.postgres_cxt.cur_command_tag = T_Invalid; gstrace_exit(GS_TRC_ID_exec_parse_message); } @@ -4566,6 +4570,7 @@ static void exec_bind_message(StringInfo input_message) * Report query to various monitoring facilities. */ t_thrd.postgres_cxt.debug_query_string = psrc->query_string; + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(psrc->raw_parse_tree); pgstat_report_activity(STATE_RUNNING, psrc->query_string); instr_stmt_report_start_time(); @@ -5143,6 +5148,7 @@ static void exec_bind_message(StringInfo input_message) ShowUsage("BIND MESSAGE STATISTICS"); t_thrd.postgres_cxt.debug_query_string = NULL; + t_thrd.postgres_cxt.cur_command_tag = T_Invalid; gstrace_exit(GS_TRC_ID_exec_bind_message); } @@ -5244,6 +5250,16 @@ static void exec_execute_message(const char* portal_name, long max_rows) * Report query to various monitoring facilities. */ t_thrd.postgres_cxt.debug_query_string = sourceText; + if (strncmp(portal->commandTag, "SELECT", strlen("SELECT")) == 0) { + /* SELECT INTO is select stmt too */ + t_thrd.postgres_cxt.cur_command_tag = T_SelectStmt; + } else if (strcmp(portal->commandTag, "SHOW") == 0) { + t_thrd.postgres_cxt.cur_command_tag = T_VariableShowStmt; + } else if (strcmp(portal->commandTag, "CALL") == 0) { + t_thrd.postgres_cxt.cur_command_tag = T_DolphinCallStmt; + } else { + t_thrd.postgres_cxt.cur_command_tag = T_CreateStmt; + } pgstat_report_activity(STATE_RUNNING, sourceText); @@ -5435,6 +5451,7 @@ static void exec_execute_message(const char* portal_name, long max_rows) ShowUsage("EXECUTE MESSAGE STATISTICS"); t_thrd.postgres_cxt.debug_query_string = NULL; + t_thrd.postgres_cxt.cur_command_tag = T_Invalid; gstrace_exit(GS_TRC_ID_exec_execute_message); } @@ -8400,6 +8417,7 @@ int PostgresMain(int argc, char* argv[], const char* dbname, const char* usernam * the storage it points at. */ t_thrd.postgres_cxt.debug_query_string = NULL; + t_thrd.postgres_cxt.cur_command_tag = T_Invalid; if (u_sess->unique_sql_cxt.need_update_calls && is_unique_sql_enabled() && is_local_unique_sql()) { @@ -8550,6 +8568,7 @@ int PostgresMain(int argc, char* argv[], const char* dbname, const char* usernam initStringInfo(&input_message); t_thrd.postgres_cxt.debug_query_string = NULL; + t_thrd.postgres_cxt.cur_command_tag = T_Invalid; t_thrd.postgres_cxt.g_NoAnalyzeRelNameList = NIL; u_sess->analyze_cxt.is_under_analyze = false; u_sess->exec_cxt.isLockRows = false; @@ -9561,6 +9580,7 @@ int PostgresMain(int argc, char* argv[], const char* dbname, const char* usernam } pfree_ext(completionTag); t_thrd.postgres_cxt.debug_query_string = NULL; + t_thrd.postgres_cxt.cur_command_tag = T_Invalid; if (MEMORY_TRACKING_QUERY_PEAK) ereport(LOG, (errmsg("execute opfusion, peak memory %ld(kb)", (int64)(t_thrd.utils_cxt.peakedBytesInQueryLifeCycle/1024)))); @@ -10838,6 +10858,7 @@ static void exec_one_in_batch(CachedPlanSource* psrc, ParamListInfo params, int } #endif + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(psrc->raw_parse_tree); if (psrc->opFusionObj != NULL) { (void)RevalidateCachedQuery(psrc); OpFusion *opFusionObj = (OpFusion *)(psrc->opFusionObj); @@ -11002,6 +11023,7 @@ static void exec_one_in_batch(CachedPlanSource* psrc, ParamListInfo params, int psrc->stmt_name, psrc->query_string))); } + t_thrd.postgres_cxt.cur_command_tag = T_Invalid; } /* @@ -11461,6 +11483,7 @@ static void exec_batch_bind_execute(StringInfo input_message) * Report query to various monitoring facilities. */ t_thrd.postgres_cxt.debug_query_string = psrc->query_string; + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(psrc->raw_parse_tree); pgstat_report_activity(STATE_RUNNING, psrc->query_string); set_ps_display(psrc->commandTag, false); @@ -12101,6 +12124,7 @@ static void exec_batch_bind_execute(StringInfo input_message) } t_thrd.postgres_cxt.debug_query_string = NULL; + t_thrd.postgres_cxt.cur_command_tag = T_Invalid; } /* lock function for g_instance.codegen_IRload_process_count Addition */ diff --git a/src/gausskernel/process/threadpool/knl_thread.cpp b/src/gausskernel/process/threadpool/knl_thread.cpp index 3c26794b790f118bdbf55a6ff30c5ed3611e9dc7..4a604a2390e9502395f84a8bf73b0d25dc50c9cb 100755 --- a/src/gausskernel/process/threadpool/knl_thread.cpp +++ b/src/gausskernel/process/threadpool/knl_thread.cpp @@ -850,6 +850,7 @@ static void knl_t_postgres_init(knl_t_postgres_context* postgres_cxt) { postgres_cxt->clear_key_memory = false; postgres_cxt->debug_query_string = NULL; + postgres_cxt->cur_command_tag = T_Invalid; postgres_cxt->isInResetUserName = false; postgres_cxt->whereToSendOutput = DestDebug; postgres_cxt->local_foreign_respool = NULL; diff --git a/src/gausskernel/process/threadpool/threadpool_stream.cpp b/src/gausskernel/process/threadpool/threadpool_stream.cpp index 5ba6d6284700f2227dc74b35c2a3fa8544b53684..22012f5997d20af0099cfe976167f20e9ceaa8f7 100644 --- a/src/gausskernel/process/threadpool/threadpool_stream.cpp +++ b/src/gausskernel/process/threadpool/threadpool_stream.cpp @@ -182,6 +182,7 @@ static void ResetStreamStatus() /* Add the pg_delete_audit operation to audit log */ t_thrd.audit.Audit_delete = false; t_thrd.postgres_cxt.debug_query_string = NULL; + t_thrd.postgres_cxt.cur_command_tag = T_Invalid; t_thrd.postgres_cxt.g_NoAnalyzeRelNameList = NIL; t_thrd.postgres_cxt.mark_explain_analyze = false; t_thrd.postgres_cxt.mark_explain_only = false; diff --git a/src/gausskernel/runtime/executor/functions.cpp b/src/gausskernel/runtime/executor/functions.cpp index 7fc99bd9b55a4c36a58459106741b198248964ce..98c043b2e25e48edb317a49d53984f210943a990 100644 --- a/src/gausskernel/runtime/executor/functions.cpp +++ b/src/gausskernel/runtime/executor/functions.cpp @@ -589,6 +589,7 @@ static void init_sql_fcache(FmgrInfo* finfo, Oid collation, bool lazy_eval_ok) ListCell* lc = NULL; Datum tmp; bool is_null = false; + NodeTag old_node_tag = t_thrd.postgres_cxt.cur_command_tag; /* * Create memory context that holds all the SQLFunctionCache data. It @@ -704,6 +705,7 @@ static void init_sql_fcache(FmgrInfo* finfo, Oid collation, bool lazy_eval_ok) foreach (lc, raw_parsetree_list) { Node* parsetree = (Node*)lfirst(lc); List* queryTree_sublist = NIL; + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(parsetree); queryTree_sublist = pg_analyze_and_rewrite_params(parsetree, fcache->src, (ParserSetupHook)sql_fn_parser_setup, fcache->pinfo); @@ -751,6 +753,7 @@ static void init_sql_fcache(FmgrInfo* finfo, Oid collation, bool lazy_eval_ok) /* Mark fcache with time of creation to show it's valid */ fcache->lxid = t_thrd.proc->lxid; fcache->subxid = GetCurrentSubTransactionId(); + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; ReleaseSysCache(procedure_tuple); @@ -1052,6 +1055,7 @@ Datum fmgr_sql(PG_FUNCTION_ARGS) bool old_running_in_fmgr = t_thrd.codegen_cxt.g_runningInFmgr; t_thrd.codegen_cxt.g_runningInFmgr = true; bool need_snapshot = !ActiveSnapshotSet(); + NodeTag old_node_tag = t_thrd.postgres_cxt.cur_command_tag; #ifdef ENABLE_MULTIPLE_NODES bool outer_is_stream = false; @@ -1211,6 +1215,7 @@ Datum fmgr_sql(PG_FUNCTION_ARGS) pushed_snapshot = true; } + t_thrd.postgres_cxt.cur_command_tag = es->qd->operation == CMD_SELECT ? T_SelectStmt : T_CreateStmt; completed = postquel_getnext(es, fcache); /* * If we ran the command to completion, we can shut it down now. Any @@ -1411,6 +1416,7 @@ Datum fmgr_sql(PG_FUNCTION_ARGS) u_sess->opt_cxt.query_dop = outerDop; #endif t_thrd.codegen_cxt.g_runningInFmgr = old_running_in_fmgr; + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; return result; } diff --git a/src/gausskernel/runtime/executor/spi.cpp b/src/gausskernel/runtime/executor/spi.cpp index 6c76256e880c67a56631f34ee7ece65ff9e2c9d0..76c7f4111ea657b544ef0d8a2b19a3410a892bc7 100644 --- a/src/gausskernel/runtime/executor/spi.cpp +++ b/src/gausskernel/runtime/executor/spi.cpp @@ -1646,6 +1646,7 @@ static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan, ParamL AutoDopControl dopControl; dopControl.CloseSmp(); #endif + NodeTag old_node_tag = t_thrd.postgres_cxt.cur_command_tag; /* * Check that the plan is something the Portal code will special-case as @@ -1666,6 +1667,7 @@ static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan, ParamL Assert(list_length(plan->plancache_list) == 1); plansource = (CachedPlanSource *)linitial(plan->plancache_list); + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(plansource->raw_parse_tree); SPI_STACK_LOG("begin", NULL, plan); /* Push the SPI stack */ @@ -1830,6 +1832,7 @@ static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan, ParamL /* reset flag */ u_sess->SPI_cxt.has_stream_in_cursor_or_forloop_sql = false; + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; _SPI_end_call(true); @@ -2300,6 +2303,7 @@ static void _SPI_pgxc_prepare_plan(const char *src, List *src_parsetree, SPIPlan spi_err_context.arg = (void *)src; spi_err_context.previous = t_thrd.log_cxt.error_context_stack; t_thrd.log_cxt.error_context_stack = &spi_err_context; + NodeTag old_node_tag = t_thrd.postgres_cxt.cur_command_tag; /* * Parse the request string into a list of raw parse trees. @@ -2322,6 +2326,7 @@ static void _SPI_pgxc_prepare_plan(const char *src, List *src_parsetree, SPIPlan Node *parsetree = (Node *)lfirst(list_item); List *stmt_list = NIL; CachedPlanSource *plansource = NULL; + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(parsetree); // get cachedplan if has any enable_spi_gpc = false; if (ENABLE_CN_GPC && u_sess->SPI_cxt._current->spi_hash_key != INVALID_SPI_KEY @@ -2413,6 +2418,7 @@ static void _SPI_pgxc_prepare_plan(const char *src, List *src_parsetree, SPIPlan plan->plancache_list = plancache_list; plan->oneshot = false; u_sess->SPI_cxt._current->plan_id = -1; + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; /* * Pop the error context stack @@ -2471,6 +2477,7 @@ void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan, parse_query_fun ErrorContextCallback spi_err_context; List *query_string_locationlist = NIL; int stmt_num = 0; + NodeTag old_node_tag = t_thrd.postgres_cxt.cur_command_tag; /* * Setup error traceback support for ereport() */ @@ -2493,6 +2500,7 @@ void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan, parse_query_fun foreach (list_item, raw_parsetree_list) { Node *parsetree = (Node *)lfirst(list_item); CachedPlanSource *plansource = NULL; + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(parsetree); #ifndef ENABLE_MULTIPLE_NODES if (g_instance.attr.attr_sql.enableRemoteExcute) { @@ -2518,6 +2526,7 @@ void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan, parse_query_fun plan->plancache_list = plancache_list; plan->oneshot = true; + t_thrd.postgres_cxt.cur_command_tag = old_node_tag; /* * Pop the error context stack @@ -2630,6 +2639,7 @@ static int _SPI_execute_plan0(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot s CachedPlan *cplan = NULL; ListCell *lc1 = NULL; bool tmp_enable_light_proxy = u_sess->attr.attr_sql.enable_light_proxy; + NodeTag old_command_tag = t_thrd.postgres_cxt.cur_command_tag; TransactionId oldTransactionId = SPI_get_top_transaction_id(); bool need_remember_cplan = false; @@ -2690,6 +2700,7 @@ static int _SPI_execute_plan0(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot s List *stmt_list = NIL; spi_err_context.arg = (void *)plansource->query_string; + t_thrd.postgres_cxt.cur_command_tag = transform_node_tag(plansource->raw_parse_tree); /* * If this is a one-shot plan, we still need to do parse analysis. @@ -2980,6 +2991,7 @@ fail: } u_sess->attr.attr_sql.enable_light_proxy = tmp_enable_light_proxy; + t_thrd.postgres_cxt.cur_command_tag = old_command_tag; return my_res; } diff --git a/src/include/knl/knl_thread.h b/src/include/knl/knl_thread.h index 088c655bbf0a3430ffcdb8ea42d2990798a10723..5934204940f8bffea4ddf6a399a7c429505548cc 100755 --- a/src/include/knl/knl_thread.h +++ b/src/include/knl/knl_thread.h @@ -1695,6 +1695,7 @@ typedef struct knl_t_postgres_context { bool clear_key_memory; const char* debug_query_string; /* client-supplied query string */ + NodeTag cur_command_tag; /* current execute sql nodetag */ bool isInResetUserName; /* Note: whereToSendOutput is initialized for the bootstrap/standalone case */ diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index fe7e86ae93d49cc538b0e07e94d30d1c6141ee97..0482e009c95cbd62022b1b03155a272bba9c1372 100755 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -2385,5 +2385,24 @@ typedef struct GetDiagStmt { bool hasCondNum; List *condNum; } GetDiagStmt; + +extern inline NodeTag transform_node_tag(Node* raw_parse_tree) +{ + if (!raw_parse_tree) { + return T_Invalid; + } + if (nodeTag(raw_parse_tree) == T_SelectStmt) { + SelectStmt *stmt = (SelectStmt *)raw_parse_tree; + /* treat select into @var and select into file as common select */ + if (stmt->intoClause == NULL || stmt->intoClause->userVarList != NIL || stmt->intoClause->filename != NULL) { + return T_SelectStmt; + } + return T_CreateStmt; + } else if (nodeTag(raw_parse_tree) == T_ExplainStmt) { + return transform_node_tag(((ExplainStmt*)raw_parse_tree)->query); + } + return nodeTag(raw_parse_tree); +} + #endif /* PARSENODES_H */