!1059 Add GUC track_stmt_parameter to record a statement with parameters

Merge pull request !1059 from wangtq/master
This commit is contained in:
opengauss-bot
2021-06-30 06:37:33 +00:00
committed by Gitee
7 changed files with 118 additions and 7 deletions

View File

@ -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|

View File

@ -1270,6 +1270,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,

View File

@ -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)

View File

@ -1759,6 +1759,7 @@ void GenerateUniqueSQLInfo(const char* sql, Query* query)
}
}
/*
* unique_sql_post_parse_analyze - generate sql id
*/
@ -1767,7 +1768,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);
}
@ -1823,6 +1824,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(&param_str);
for (paramno = 0; paramno < params->numParams; paramno++) {
ParamExternData* prm = &params->params[paramno];
Oid typoutput;
bool typisvarlena = false;
char* pstring = NULL;
char* p = NULL;
appendStringInfo(&param_str, "%s$%d = ", (paramno > 0) ? ", " : " parameters: ", paramno + 1);
if (prm->isnull || !OidIsValid(prm->ptype)) {
appendStringInfoString(&param_str, "NULL");
continue;
}
getTypeOutputInfo(prm->ptype, &typoutput, &typisvarlena);
pstring = OidOutputFunctionCall(typoutput, prm->value);
appendStringInfoCharMacro(&param_str, '\'');
for (p = pstring; *p; p++) {
if (*p == '\'') /* double single quotes */
appendStringInfoCharMacro(&param_str, *p);
appendStringInfoCharMacro(&param_str, *p);
}
appendStringInfoCharMacro(&param_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
*
@ -1840,6 +1911,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 */

View File

@ -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

View File

@ -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;

View File

@ -182,6 +182,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 */