MySQL派生表查询引发崩溃的根源剖析及应对之策

MySQL派生表查询崩溃根源剖析与解决策略

一、问题的发现

在使用MySQL 8.0.32时,发现执行包含派生表的以下SQL会导致MySQL崩溃,其中sequence_table(2)替换为任意非常量表都会出现此情况:

仅MySQL 8.0.32版本受影响。

EXPLAIN FORMAT=TREE 
select  
    trim(ref_15.c_ogj),
    0<>0 as c_lrcm63eani
  from 
    (select 
    0<>0 as c_ogj
  from 
   sequence_table(2) t1
  where 0<>0
  order by c_ogj asc) as ref_15;

崩溃时的堆栈信息如下:

Thread 55 "mysqld" received signal SIGSEGV, Segmentation fault.
Item_view_ref::used_tables (this=0x7fff2418f410)
    at sql/item.h:6670
6670            table_map inner_map = ref_item()->used_tables(); ==> ref_item()为空指针,因此crash了
(gdb) bt
#0  Item_view_ref::used_tables (this=0x7fff2418f410)
    at sql/item.h:6670
#1  0x0000555558e978d1 in Item::const_item (this=0x7fff2418f410)
    at sql/item.h:2342
#2  0x0000555558ecc765 in Item_ref::print (this=0x7fff2418f410, thd=0x7fff24001050, 
    str=0x7fffc83ee7e0, query_type=(QT_TO_SYSTEM_CHARSET | QT_SHOW_SELECT_NUMBER))
    at sql/item.cc:9993
#3  0x000055555903b839 in Item_func_trim::print (this=0x7fff24120d20, thd=0x7fff24001050, 
    str=0x7fffc83ee7e0, query_type=(QT_TO_SYSTEM_CHARSET | QT_SHOW_SELECT_NUMBER))
    at sql/item_strfunc.cc:3244
#4  0x0000555558ea7fc5 in Item::print_item_w_name (this=0x7fff24120d20, thd=0x7fff24001050, 
    str=0x7fffc83ee7e0, query_type=(QT_TO_SYSTEM_CHARSET | QT_SHOW_SELECT_NUMBER))
    at sql/item.cc:727
#5  0x00005555593f18c0 in Query_block::print_item_list (this=0x7fff24120768, thd=0x7fff24001050, 
    str=0x7fffc83ee7e0, query_type=(QT_TO_SYSTEM_CHARSET | QT_SHOW_SELECT_NUMBER))
    at sql/sql_lex.cc:4041
#6  0x00005555593efb50 in Query_block::print_query_block (this=0x7fff24120768, 
    thd=0x7fff24001050, str=0x7fffc83ee7e0, 
    query_type=(QT_TO_SYSTEM_CHARSET | QT_SHOW_SELECT_NUMBER))
    at sql/sql_lex.cc:3614
#7  0x00005555593efa3d in Query_block::print (this=0x7fff24120768, thd=0x7fff24001050, 
    str=0x7fffc83ee7e0, query_type=(QT_TO_SYSTEM_CHARSET | QT_SHOW_SELECT_NUMBER))
    at sql/sql_lex.cc:3598
#8  0x00005555593ee556 in Query_expression::print (this=0x7fff24120670, thd=0x7fff24001050, 
    str=0x7fffc83ee7e0, query_type=(QT_TO_SYSTEM_CHARSET | QT_SHOW_SELECT_NUMBER))
    at sql/sql_lex.cc:3232
#9  0x0000555559a89c2c in print_query_for_explain (query_thd=0x7fff24001050, 
    unit=0x7fff24120670, str=0x7fffc83ee7e0)
    at sql/opt_explain.cc:2288
#10 0x0000555559a10b11 in PrintQueryPlan[abi:cxx11](THD*, THD const*, Query_expression*) (
    ethd=0x7fff24001050, query_thd=0x7fff24001050, unit=0x7fff24120670)
    at sql/join_optimizer/explain_access_path.cc:1894
#11 0x0000555559a8985a in ExplainIterator (ethd=0x7fff24001050, query_thd=0x7fff24001050, 
    unit=0x7fff24120670) at sql/opt_explain.cc:2205
#12 0x0000555559a89e91 in explain_query (explain_thd=0x7fff24001050, query_thd=0x7fff24001050, 
    unit=0x7fff24120670) at sql/opt_explain.cc:2359
#13 0x000055555955cd46 in Sql_cmd_dml::execute_inner (this=0x7fff24165630, thd=0x7fff24001050)

二、问题调查过程

对执行该SQL的优化过程进行调查,发现该SQL的SQL变换情况如下:

trim(ref_15.c_ogj)执行完find_order_in_list后,Item_func_trimargs[0]->m_ref_item[0]变为0<>0 as c_lrcm63eani,而非0<>0 as c_ogj,这是因为c_lrcm63eanic_ogj名字相同,在find_order_in_list函数中因名字相同导致内层字段被外层替代。随后在Item::clean_up_after_removal执行时,Item_func_ne即c_lrcm63eani因出现2次,执行了2次decrement_ref_count(),但在Query_block::delete_unused_merged_columns函数中,0<>0 as c_lrcm63eani的Item被置为空,因为此时item->decrement_ref_count()ref_count()为0,继续执行Item::clean_up_after_removal

EXPLAIN FORMAT=TREE 
select  
    trim(ref_15.c_ogj),
    0<>0 as c_lrcm63eani
  from 
    (select 
    0<>0 as c_ogj
  from 
   sequence_table(2) t1
  where 0<>0
  order by c_ogj asc) as ref_15;

查看函数调用过程发现,Query_block在prepare时执行了delete_unused_merged_columns,相关函数调用过程如下:

-- 函数调用过程: Query_block::prepare -> Query_block::apply_local_transforms -> Query_block::delete_unused_merged_columns

bool find_order_in_list() {
  if (select_item != not_found_item) {
      if ((*order->item)->real_item() != (*select_item)->real_item()) {
        Item::Cleanup_after_removal_context ctx(
            thd->lex->current_query_block());

        (*order->item)
            ->walk(&Item::clean_up_after_removal, walk_options,  ==>Item_func_ne执行了2次,也执行了2次decrement_ref_count()
                   pointer_cast<uchar *>(&ctx));
      }
  }
}

bool Query_block::apply_local_transforms(THD *thd, bool prune) {
  DBUG_TRACE;

  assert(first_execution);
  -- 这个函数把((Item_func *)&fields[0][0])->args[0]->m_ref_item[0]给删了
  if (derived_table_count) delete_unused_merged_columns(&m_table_nest);
}

void Query_block::delete_unused_merged_columns(
    mem_root_deque<Table_ref *> *tables) {
  DBUG_TRACE;

  for (Table_ref *tl : *tables) {
    if (tl->nested_join == nullptr) continue;
    if (tl->is_merged()) {
      for (Field_translator *transl = tl->field_translation;
           transl < tl->field_translation_end; transl++) {
        Item *const item = transl->item;
        // Decrement the ref count as its no more used in
        // select list.
        if (item->decrement_ref_count()) continue; -- 因为执行完decrement_ref_count()以后返回的m_ref_count=0因此不会跳出这个循环

        // Cleanup the item since its not referenced from
        // anywhere.
        assert(item->fixed);
        Item::Cleanup_after_removal_context ctx(this);
        item->walk(&Item::clean_up_after_removal, walk_options,
                   pointer_cast<uchar *>(&ctx));
        transl->item = nullptr; -- 这个地方把Item_view_ref引用的Item_func_ne对象置为空了,即把trim函数参数的c_lrcm63eani列删除了
      }
    }
    delete_unused_merged_columns(&tl->nested_join->m_tables);
  }
}

三、解决方案

经分析,问题源于多执行了一次Item::clean_up_after_removal。在MySQL最新代码中,该BUG已被修复,相关修复代码如下:

相关commit ID号为: 2171a1260e2cdbbd379646be8ff6413a92fd48f4

-- 相关修复代码如下:
@@ -7575,7 +7865,6 @@ bool Item::clean_up_after_removal(uchar *arg) {

   if (reference_count() > 1) {
     (void)decrement_ref_count();
    +    ctx->stop_at(this);
   }
   return false;
 }

修改后查看函数堆栈信息:

#0  Item::clean_up_after_removal (this=0x2, 
    arg=0x41 <error: Cannot access memory at address 0x41>)
    at sql/item.cc:9236
#1  0x0000555558fea5a8 in Item::walk (this=0x7fff2c338db8, processor=&virtual table offset 864, 
    walk=7, arg=0x7fffc83ee4b0 "") at sql/item.h:2543
#2  0x00005555596cc6f2 in find_order_in_list (thd=0x7fff2c001070, ref_item_array=..., 
    tables=0x7fff2c330b90, order=0x7fff2c32eae8, fields=0x7fff2c32fb20, is_group_field=false, 
    is_window_order=false) at sql/sql_resolver.cc:4625
#3  0x00005555596cd0ae in setup_order (thd=0x7fff2c001070, ref_item_array=..., 
    tables=0x7fff2c330b90, fields=0x7fff2c32fb20, order=0x7fff2c32eae8)
    at sql/sql_resolver.cc:4811
#4  0x00005555596bf528 in Query_block::prepare (this=0x7fff2c32fae0, thd=0x7fff2c001070, 
    insert_field_list=0x0) at sql/sql_resolver.cc:400
#5  0x00005555597d035d in Query_expression::prepare (this=0x7fff2c32f9e8, thd=0x7fff2c001070, 
    sel_result=0x7fff2c33b2a8, insert_field_list=0x0, added_options=0, removed_options=0)
    at sql/sql_union.cc:758
#6  0x0000555559590772 in Table_ref::resolve_derived (this=0x7fff2c339790, thd=0x7fff2c001070, 
    apply_semijoin=true) at sql/sql_derived.cc:451
#7  0x00005555596c2a80 in Query_block::resolve_placeholder_tables (this=0x7fff2c333f08, 
    thd=0x7fff2c001070, apply_semijoin=true)
    at sql/sql_resolver.cc:1408
#8  0x00005555596bea62 in Query_block::prepare (this=0x7fff2c333f08, thd=0x7fff2c001070, 
    insert_field_list=0x0) at sql/sql_resolver.cc:265

对于0<>0 as c_lrcm63eani这个Item_func_ne对象,执行Item::clean_up_after_removal时,因reference_count() > 1会执行ctx->stop_at(this),下次执行该函数时,因ctx->is_stopped(this)直接返回,不再执行decrement_ref_count(),避免了transl->item = nullptr的执行。

bool find_order_in_list() {
  if (select_item != not_found_item) {
      if ((*order->item)->real_item() != (*select_item)->real_item()) {
        Item::Cleanup_after_removal_context ctx(
            thd->lex->current_query_block());

        (*order->item)
            ->walk(&Item::clean_up_after_removal, walk_options,  -- Item_func_ne执行了2次,而只执行了一次decrement_ref_count()
                   pointer_cast<uchar *>(&ctx));
      }
  }
}

void Query_block::delete_unused_merged_columns(
    mem_root_deque<Table_ref *> *tables) {
  DBUG_TRACE;

  for (Table_ref *tl : *tables) {
    if (tl->nested_join == nullptr) continue;
    if (tl->is_merged()) {
      for (Field_translator *transl = tl->field_translation;
           transl < tl->field_translation_end; transl++) {
        Item *const item = transl->item;
        // Decrement the ref count as its no more used in
        // select list.
        if (item->decrement_ref_count()) continue; 因为执行完decrement_ref_count()以后返回的m_ref_count=1因此不会继续执行后面的置空设置

        // Cleanup the item since its not referenced from
        // anywhere.
        assert(item->fixed);
        Item::Cleanup_after_removal_context ctx(this);
        item->walk(&Item::clean_up_after_removal, walk_options,
                   pointer_cast<uchar *>(&ctx));
        transl->item = nullptr; ==>这个地方不会运行到
      }
    }
    delete_unused_merged_columns(&tl->nested_join->m_tables);
  }
}

四、问题总结

通过分析可知,复杂SQL的Item变换和不需要的Item删除过程易引发Crash。分析此类Crash问题需熟悉代码运行流程,具备复杂问题解决经验,才能有效应对。

文章整理自互联网,只做测试使用。发布者:Lomu,转转请注明出处:https://www.it1024doc.com/12601.html

(0)
LomuLomu
上一篇 2025 年 6 月 20 日
下一篇 2025 年 6 月 20 日

相关推荐

  • DataGrip永久激活教程(包含破解补丁及激活码)

    DataGrip永久激活教程(包含破解补丁及激活码) 这篇教程适用于所有JetBrains系列软件,包括DataGrip。接下来,我将向大家展示如何通过简单的图文步骤,成功激活DataGrip至2099年。通过这个方法,你不仅能轻松激活最新版本,也能适用于旧版本的激活。 首先,先看一下成功激活的截图,我们可以看到DataGrip已经成功激活,且有效期延续至2…

    DataGrip破解教程 2025 年 4 月 22 日
    39100
  • 🔥2025最新PyCharm永久激活码分享 | 亲测可用至2099年(附详细破解教程)

    还在为PyCharm试用期到期发愁?🤔 本教程将手把手教你如何永久激活PyCharm至2099年!适用于所有JetBrains全家桶(IDEA、PyCharm、DataGrip、Goland等),无论Windows/Mac/Linux系统,100%成功!💯 先看看最新PyCharm激活成功的效果图👇 🚀 准备工作 1. 下载PyCharm安装包 已安装可跳过…

    PyCharm激活码 2025 年 6 月 28 日
    12.6K00
  • datagrip激活码与datagrip破解区别详解

    本教程适用于IDEA、PyCharm、DataGrip、Goland等,支持Jetbrains全家桶! 废话不多说,先上最新版本破解成功的截图,如下,可以看到已经成功破解到 2099 年辣,舒服! 接下来,我就将通过图文的方式, 来详细讲解如何激活DataGrip至 2099 年。 当然这个激活方法,同样适用于之前的旧版本! 不管你是什么操作系统,什么版本,…

    DataGrip激活码 5天前
    2300
  • 算法奇趣阁——递归中二叉树的精妙裁剪

    文章标题: 算法妙趣屋——递归下二叉树的精巧剪裁 1.前言 二叉树里的深度搜索(展开阐述) 深度优先遍历(DFS,即DepthFirstTraversal),是我们在树或者图这类数据结构中常用的一种遍历手段。该算法会尽可能深入地搜索树或者图的分支,直至一条路径上的所有节点都被遍历完毕,之后回溯到上一层,再去探寻另一条路径进行遍历。在二叉树中,常见的深度优先遍…

    2025 年 7 月 25 日
    16100
  • 2024 DataGrip最新激活码,DataGrip永久免费激活码2025-02-10 更新

    DataGrip 2024最新激活码 以下是最新的DataGrip激活码,更新时间:2025-02-10 🔑 激活码使用说明 1️⃣ 复制下方激活码 2️⃣ 打开 DataGrip 软件 3️⃣ 在菜单栏中选择 Help -> Register 4️⃣ 选择 Activation Code 5️⃣ 粘贴激活码,点击 Activate ⚠️ 必看!必看! 🔥 …

    2025 年 2 月 10 日
    83200

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信