From fc0bd72f7ce2a65532a093a6c0bddae12c00a5b7 Mon Sep 17 00:00:00 2001 From: wangtianqing 00464751 Date: Mon, 21 Jun 2021 14:34:56 +0800 Subject: [PATCH 1/2] fix (xtuner): fix the importing for logging --- src/gausskernel/dbmind/tools/xtuner/tuner/benchmark/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/gausskernel/dbmind/tools/xtuner/tuner/benchmark/__init__.py b/src/gausskernel/dbmind/tools/xtuner/tuner/benchmark/__init__.py index 338a80d3b..585f4f9cd 100644 --- a/src/gausskernel/dbmind/tools/xtuner/tuner/benchmark/__init__.py +++ b/src/gausskernel/dbmind/tools/xtuner/tuner/benchmark/__init__.py @@ -16,6 +16,7 @@ See the Mulan PSL v2 for more details. import importlib import os import types +import logging from tuner.exceptions import ConfigureError from tuner.executor import ExecutorFactory From 161392ce013b7b7e25968a80caf976f1ef3970e3 Mon Sep 17 00:00:00 2001 From: wangtq Date: Tue, 29 Jun 2021 14:18:36 +0800 Subject: [PATCH 2/2] feat (WDR): add GUC track_stmt_parameter to record a statement with parameters --- src/bin/gs_guc/cluster_guc.conf | 1 + src/common/backend/utils/misc/guc.cpp | 10 +++ .../instruments/statement/instr_statement.cpp | 36 +++++++-- .../unique_sql/instr_unique_sql.cpp | 75 ++++++++++++++++++- src/include/instruments/instr_statement.h | 1 + .../knl/knl_guc/knl_session_attr_common.h | 1 + 6 files changed, 117 insertions(+), 7 deletions(-) diff --git a/src/bin/gs_guc/cluster_guc.conf b/src/bin/gs_guc/cluster_guc.conf index af5042c2a..03e2bcbe4 100755 --- a/src/bin/gs_guc/cluster_guc.conf +++ b/src/bin/gs_guc/cluster_guc.conf @@ -346,6 +346,7 @@ wdr_snapshot_query_timeout|int|100,2147483647|s|NULL| enable_wdr_snapshot|bool|0,0|NULL|NULL| enable_asp|bool|0,0|NULL|NULL| enable_stmt_track|bool|0,0|NULL|NULL| +track_stmt_parameter|bool|0,0|NULL|NULL| asp_sample_num|int|10000,100000|NULL|NULL| asp_sample_interval|int|1,10|s|NULL| asp_flush_rate|int|1,10|NULL|NULL| diff --git a/src/common/backend/utils/misc/guc.cpp b/src/common/backend/utils/misc/guc.cpp index 05983a717..7b359e4fe 100644 --- a/src/common/backend/utils/misc/guc.cpp +++ b/src/common/backend/utils/misc/guc.cpp @@ -1264,6 +1264,16 @@ static void InitConfigureNamesBool() NULL, NULL}, + {{"track_stmt_parameter", + PGC_SIGHUP, + INSTRUMENTS_OPTIONS, + gettext_noop("Enable to track the parameter of statements"), NULL}, + &u_sess->attr.attr_common.track_stmt_parameter, + false, + NULL, + NULL, + NULL}, + {{"enable_global_stats", PGC_SUSET, QUERY_TUNING_METHOD, diff --git a/src/gausskernel/cbb/instruments/statement/instr_statement.cpp b/src/gausskernel/cbb/instruments/statement/instr_statement.cpp index 58462c159..9fd435c2d 100644 --- a/src/gausskernel/cbb/instruments/statement/instr_statement.cpp +++ b/src/gausskernel/cbb/instruments/statement/instr_statement.cpp @@ -1453,6 +1453,20 @@ void instr_stmt_report_debug_query_id(uint64 debug_query_id) CURRENT_STMT_METRIC_HANDLE->debug_query_id = debug_query_id; } +inline void instr_stmt_track_param_query(const char *query) +{ + if (CURRENT_STMT_METRIC_HANDLE->params == NULL) { + CURRENT_STMT_METRIC_HANDLE->query = pstrdup(query); + } else { + Size len = strlen(query) + strlen(CURRENT_STMT_METRIC_HANDLE->params) + 2; + CURRENT_STMT_METRIC_HANDLE->query = (char *)palloc(len * sizeof(char)); + errno_t rc = sprintf_s(CURRENT_STMT_METRIC_HANDLE->query, len, "%s;%s", + query, CURRENT_STMT_METRIC_HANDLE->params); + securec_check_ss_c(rc, "\0", "\0"); + pfree_ext(CURRENT_STMT_METRIC_HANDLE->params); + } +} + /* using unique query */ void instr_stmt_report_query(uint64 unique_query_id) { @@ -1460,13 +1474,23 @@ void instr_stmt_report_query(uint64 unique_query_id) CURRENT_STMT_METRIC_HANDLE->unique_query_id = unique_query_id; CURRENT_STMT_METRIC_HANDLE->unique_sql_cn_id = u_sess->unique_sql_cxt.unique_sql_cn_id; - if (is_local_unique_sql()) { - MemoryContext old_ctx = MemoryContextSwitchTo(u_sess->statement_cxt.stmt_stat_cxt); - if (CURRENT_STMT_METRIC_HANDLE->query == NULL) { - CURRENT_STMT_METRIC_HANDLE->query = FindCurrentUniqueSQL(); - } - (void)MemoryContextSwitchTo(old_ctx); + if (likely(!is_local_unique_sql() || CURRENT_STMT_METRIC_HANDLE->query)) { + return; } + + MemoryContext old_ctx = MemoryContextSwitchTo(u_sess->statement_cxt.stmt_stat_cxt); + + if (!u_sess->attr.attr_common.track_stmt_parameter) { + CURRENT_STMT_METRIC_HANDLE->query = FindCurrentUniqueSQL(); + } else { + if (u_sess->unique_sql_cxt.curr_single_unique_sql != NULL) { + instr_stmt_track_param_query(u_sess->unique_sql_cxt.curr_single_unique_sql); + } else { + instr_stmt_track_param_query(t_thrd.postgres_cxt.debug_query_string); + } + } + + (void)MemoryContextSwitchTo(old_ctx); } void instr_stmt_report_txid(uint64 txid) diff --git a/src/gausskernel/cbb/instruments/unique_sql/instr_unique_sql.cpp b/src/gausskernel/cbb/instruments/unique_sql/instr_unique_sql.cpp index a96f8d71d..e6e8d81c2 100644 --- a/src/gausskernel/cbb/instruments/unique_sql/instr_unique_sql.cpp +++ b/src/gausskernel/cbb/instruments/unique_sql/instr_unique_sql.cpp @@ -1714,6 +1714,7 @@ void GenerateUniqueSQLInfo(const char* sql, Query* query) } } + /* * unique_sql_post_parse_analyze - generate sql id */ @@ -1722,7 +1723,7 @@ void UniqueSq::unique_sql_post_parse_analyze(ParseState* pstate, Query* query) Assert(IS_PGXC_COORDINATOR || IS_SINGLE_NODE); if (pstate != NULL) { - /* generate unique sql id */ + /* generate unique sql id */ if (is_unique_sql_enabled()) { GenerateUniqueSQLInfo(pstate->p_sourcetext, query); } @@ -1770,6 +1771,76 @@ static void SetLocalUniqueSQLId(List* query_list) } } +/* Set parameters from portal */ +void SetParamsFromPortal(Portal portal) +{ + CHECK_STMT_HANDLE(); + Assert(portal); + + if (!u_sess->attr.attr_common.track_stmt_parameter || CURRENT_STMT_METRIC_HANDLE->params) { + return; + } + + ParamListInfo params = portal->portalParams; + /* We mustn't call user-defined I/O functions when in an aborted xact */ + if (params && params->numParams > 0 && !IsAbortedTransactionBlockState()) { + StringInfoData param_str; + MemoryContext oldcontext; + int paramno; + + initStringInfo(¶m_str); + + for (paramno = 0; paramno < params->numParams; paramno++) { + ParamExternData* prm = ¶ms->params[paramno]; + Oid typoutput; + bool typisvarlena = false; + char* pstring = NULL; + char* p = NULL; + + appendStringInfo(¶m_str, "%s$%d = ", (paramno > 0) ? ", " : " parameters: ", paramno + 1); + + if (prm->isnull || !OidIsValid(prm->ptype)) { + appendStringInfoString(¶m_str, "NULL"); + continue; + } + + getTypeOutputInfo(prm->ptype, &typoutput, &typisvarlena); + + pstring = OidOutputFunctionCall(typoutput, prm->value); + + appendStringInfoCharMacro(¶m_str, '\''); + for (p = pstring; *p; p++) { + if (*p == '\'') /* double single quotes */ + appendStringInfoCharMacro(¶m_str, *p); + appendStringInfoCharMacro(¶m_str, *p); + } + appendStringInfoCharMacro(¶m_str, '\''); + + pfree(pstring); + } + + oldcontext = MemoryContextSwitchTo(u_sess->statement_cxt.stmt_stat_cxt); + + CURRENT_STMT_METRIC_HANDLE->params = pstrdup(param_str.data); + + pfree(param_str.data); + + if (CURRENT_STMT_METRIC_HANDLE->query) { + Size len = strlen(CURRENT_STMT_METRIC_HANDLE->query) + strlen(CURRENT_STMT_METRIC_HANDLE->params) + 2; + char *tmpstr = (char *)palloc(len * sizeof(char)); + errno_t rc = sprintf_s(tmpstr, len, "%s;%s", CURRENT_STMT_METRIC_HANDLE->query, + CURRENT_STMT_METRIC_HANDLE->params); + securec_check_ss_c(rc, "\0", "\0"); + pfree(CURRENT_STMT_METRIC_HANDLE->query); + pfree_ext(CURRENT_STMT_METRIC_HANDLE->params); + CURRENT_STMT_METRIC_HANDLE->query = tmpstr; + } + + + MemoryContextSwitchTo(oldcontext); + } +} + /* * set current prepared statement's unique sql id * @@ -1787,6 +1858,8 @@ void SetUniqueSQLIdFromPortal(Portal portal, CachedPlanSource* unnamed_psrc) return; } + SetParamsFromPortal(portal); + List* query_list = NULL; if (portal->prepStmtName && portal->prepStmtName[0] != '\0') { /* for named prepared statement */ diff --git a/src/include/instruments/instr_statement.h b/src/include/instruments/instr_statement.h index 009ee360e..7c343374a 100644 --- a/src/include/instruments/instr_statement.h +++ b/src/include/instruments/instr_statement.h @@ -180,6 +180,7 @@ typedef struct StatementStatContext { TransactionId txn_id; UniqueSQLParse parse; char* query_plan; /* query plan */ + char* params; /* params for pbe statements */ uint64 plan_size; LockSummaryStat lock_summary; StatementDetail details; diff --git a/src/include/knl/knl_guc/knl_session_attr_common.h b/src/include/knl/knl_guc/knl_session_attr_common.h index 60becb840..82fff11be 100644 --- a/src/include/knl/knl_guc/knl_session_attr_common.h +++ b/src/include/knl/knl_guc/knl_session_attr_common.h @@ -179,6 +179,7 @@ typedef struct knl_session_attr_common { int instr_rt_percentile_interval; bool enable_instr_rt_percentile; + bool track_stmt_parameter; char* percentile_values; /* instr - full sql/slow sql */