一、問題發現
在一次開發中在sp中使用MySQL PREPARE
以后,使用match AGAINST
陳述句作為prepare stmt
的引數后,發現執行第二遍call會導致資料庫crash,于是開始動手調查問題發生的原因,
注:本次使用的 MySQL 資料庫版本為最新的debug版本,
SQL陳述句示例:
CREATE TABLE t1 (a INT, b VARCHAR(10));
DELIMITER $$
CREATE PROCEDURE p1()
begin
declare a VARCHAR(200);
declare b TEXT;
set a = 'Only MyISAM tables';
set b ='support collections';
set @bb := match(a,b) AGAINST ('collections');
prepare stmt1 from 'select * from t1 where ?';
execute stmt1 using @bb;
end$$
DELIMITER ;
執行結果:
mysql> call p1;
ERROR 1210 (HY000): Incorrect arguments to MATCH
mysql> call p1; 這里發現代碼crash了
ERROR 2013 (HY000): Lost connection to MySQL server during query
二、問題調查程序
1、首先查看錯誤堆疊資訊,可以看到Item_func_match::val_real
函式的item->real_item()->type()
不等于FIELD_ITEM
引起的,列印堆疊看了一下,此時的item->real_item()為Item_splocal
,明顯不是FIELD_ITEM
,
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1 0x00007ffff7568859 in __GI_abort () at abort.c:79
#2 0x00007ffff7568729 in __assert_fail_base (fmt=0x7ffff76fe588 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n",
assertion=0x55555bd2e340 "std::all_of(args, args + arg_count, [](const Item *item) { return item->real_item()->type() == FIELD_ITEM; })", file=0x55555bd2a9e0 "/mysql/sql/item_func.cc",
line=9769, function=<optimized out>) at assert.c:92
#3 0x00007ffff7579fd6 in __GI___assert_fail (
assertion=0x55555bd2e340 "std::all_of(args, args + arg_count, [](const Item *item) { return item->real_item()->type() == FIELD_ITEM; })", file=0x55555bd2a9e0 "/mysql/sql/item_func.cc",
line=9769, function=0x55555bd2e300 "virtual double Item_func_match::val_real()") at assert.c:101
#4 0x0000555558f9e17e in Item_func_match::val_real (this=0x7fff2cc86928) 這里導致的crash
at /mysql/sql/item_func.cc:9769
#5 0x0000555558f97f7e in Item_func_set_user_var::check (this=0x7fff2cc88200, use_result_field=false)
at /mysql/sql/item_func.cc:8238
#6 0x00005555592d74d3 in set_var_user::check (this=0x7fff2cc88388)
at /mysql/sql/set_var.cc:1874
#7 0x00005555592d5cd6 in sql_set_variables (thd=0x7fff2c001050, var_list=0x7fff2cc87210, opened=true)
at /mysql/sql/set_var.cc:1442
#8 0x00005555594d89ed in mysql_execute_command (thd=0x7fff2c001050, first_level=false)
at /mysql/sql/sql_parse.cc:4051
#9 0x000055555930c7a8 in sp_instr_stmt::exec_core (this=0x7fff2cc883d8, thd=0x7fff2c001050,
nextp=0x7fffe02ed8b4) at /mysql/sql/sp_instr.cc:1039
#10 0x000055555930ae0b in sp_lex_instr::reset_lex_and_exec_core (this=0x7fff2cc883d8, thd=0x7fff2c001050,
nextp=0x7fffe02ed8b4, open_tables=false) at /mysql/sql/sp_instr.cc:457
#11 0x000055555930bc74 in sp_lex_instr::validate_lex_and_execute_core (this=0x7fff2cc883d8, thd=0x7fff2c001050,
nextp=0x7fffe02ed8b4, open_tables=false) at /mysql/sql/sp_instr.cc:771
#12 0x000055555930c3ad in sp_instr_stmt::execute (this=0x7fff2cc883d8, thd=0x7fff2c001050, nextp=0x7fffe02ed8b4)
at /mysql/sql/sp_instr.cc:956
#13 0x00005555592fa772 in sp_head::execute (this=0x7fff2cc76da0, thd=0x7fff2c001050, merge_da_on_success=true)
at /mysql/sql/sp_head.cc:2279
#14 0x00005555592fcec2 in sp_head::execute_procedure (this=0x7fff2cc76da0, thd=0x7fff2c001050, args=0x0)
at /mysql/sql/sp_head.cc:2995
#15 0x00005555593661c9 in do_execute_sp (thd=0x7fff2c001050, sp=0x7fff2cc76da0, args=0x0)
at /mysql/sql/sql_call.cc:86
2、要想獲取sp引數的實際item,應該呼叫this_item()
方法,但是也許作者本來就不想讓match
支持sp引數,因此這里的寫法是對的,但是本來代碼不應該運行到這里,因為本來應該直接報錯,
double Item_func_match::val_real() {
assert(fixed);
assert(!has_rollup_expr());
assert(std::all_of(args, args + arg_count, [](const Item *item) {
return item->real_item()->type() == FIELD_ITEM; ==>這里的item->real_item()->type()說明不支持Item_splocal
}));
3、接著繼續調查,查看第一次報錯的地方的代碼,找到Item_func_match::fix_fields
,看到了第一次報錯的地方的代碼item->type() != Item::FIELD_ITEM
,因此代碼運行應該在這里報錯,但是為何第二次執行會運行到Item_func_match::val_real
而不是在Item_func_match::fix_fields
就直接報錯回傳呢?仔細查看下面的代碼,發現下面的代碼有1個地方有錯誤,
bool Item_func_match::fix_fields(THD *thd, Item **ref) {
if (Item_func::fix_fields(thd, ref) || fix_func_arg(thd, &against) ||
上面這里Item_func::fix_fields執行完后使fixed=true
但是如果后面有任何報錯的地方導致回傳的話,這個值沒有修改回false
會導致第二次call sp不會再次執行Item_func_match::fix_fields,
!against->const_for_execution()) {
thd->mark_used_columns = save_mark_used_columns;
my_error(ER_WRONG_ARGUMENTS, MYF(0), "AGAINST");
return true;
}
for (uint i = 0; i < arg_count; i++) {
item = args[i] = args[i]->real_item();
if (item->type() != Item::FIELD_ITEM ||
/* Cannot use FTS index with outer table field */
item->is_outer_reference()) {
my_error(ER_WRONG_ARGUMENTS, MYF(0), "MATCH");
return true;
}
三、問題解決方案
通過以上代碼決議后作如下修改,正確給fixed
賦值,這樣就可以保證每次call sp
的時候如果遇到報錯再次運行還會重新執行fix_fields
,
bool Item_func_match::fix_fields(THD *thd, Item **ref) {
if (Item_func::fix_fields(thd, ref) || fix_func_arg(thd, &against) ||
!against->const_for_execution()) {
fixed = false; ==>這里需要重新把fixed賦值為false
thd->mark_used_columns = save_mark_used_columns;
my_error(ER_WRONG_ARGUMENTS, MYF(0), "AGAINST");
return true;
}
thd->mark_used_columns = save_mark_used_columns;
fixed = false; ==>這里需要重新把fixed賦值為false
for (uint i = 0; i < arg_count; i++) {
item = args[i] = args[i]->real_item()->this_item();
if (item->type() != Item::FIELD_ITEM ||
/* Cannot use FTS index with outer table field */
item->is_outer_reference()) {
my_error(ER_WRONG_ARGUMENTS, MYF(0), "MATCH");
return true;
}
中間省略
fixed = true; ==>最后沒有問題了再賦值為true
return false;
現在重新執行call sp,沒有問題了,
mysql> call p1;
ERROR 1210 (HY000): Incorrect arguments to MATCH
mysql> call p1;
ERROR 1210 (HY000): Incorrect arguments to MATCH
四、問題總結
本次只是解決了match
的fix_fields
問題,但是如果想讓 match
支持 sp 的引數,即Item_splocal
的引數的話,代碼里面還要做相應修改,包括set @bb := match(a,b) AGAINST ('collections');
這里面生成的Item_func_match
會在這句執行完以后被 cleanup 掉,等到下一句 prepare 想再次使用它的時候會因為找不到該item發生問題,這個是重構 match函式
支持 sp 引數需要注意的點,
Enjoy GreatSQL ??
關于 GreatSQL
GreatSQL是由萬里資料庫維護的MySQL分支,專注于提升MGR可靠性及性能,支持InnoDB并行查詢特性,是適用于金融級應用的MySQL分支版本,
相關鏈接: GreatSQL社區 Gitee GitHub Bilibili
GreatSQL社區:
社區博客有獎征稿詳情:https://greatsql.cn/thread-100-1-1.html
技術交流群:
微信:掃碼添加
GreatSQL社區助手
微信好友,發送驗證資訊加群
,
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/556654.html
標籤:MySQL
下一篇:返回列表