MySQL的多层SP中Cursor的m_max_cursor_index相关BUG分析
一、的多层问题发现
在一次开发中在sp中使用多层cursor的分析时候想知道每层的m_max_cursor_index值分别是多少,以用来做后续开发。的多层于是分析做了以下的试验,但是的多层发现第一个level=2那层的m_max_cursor_index的值有点问题。
注:本次使用的分析MySQL数据库版本为最新的debug版本。

SQL语句示例:
复制greatsql> CREATE TABLE t1 (a INT,的多层 b VARCHAR(10)); 以下注释里面是该层sp_pcontext的参数值。 DELIMITER $$ CREATE PROCEDURE processnames() -- level=0,分析m_max_cursor_index=1+8+1 BEGIN DECLARE nameCursor0 CURSOR FOR SELECT * FROM t1; -- level=1,的多层m_cursor_offset=0,分析m_max_cursor_index=1+8+1 begin DECLARE nameCursor1 CURSOR FOR SELECT * FROM t1; -- level=2,的多层m_cursor_offset=1,分析m_max_cursor_index=1+8 ☆问题点 begin DECLARE nameCursor2 CURSOR FOR SELECT * FROM t1; -- level=3,的多层m_cursor_offset=2,分析m_max_cursor_index=1 DECLARE nameCursor3 CURSOR FOR SELECT * FROM t1; -- level=3,b2b信息网的多层m_cursor_offset=2,m_max_cursor_index=2 DECLARE nameCursor4 CURSOR FOR SELECT * FROM t1; -- level=3,m_cursor_offset=2,m_max_cursor_index=3 DECLARE nameCursor5 CURSOR FOR SELECT * FROM t1; -- level=3,m_cursor_offset=2,m_max_cursor_index=4 end; end; begin DECLARE nameCursor6 CURSOR FOR SELECT * FROM t1; -- level=2,m_cursor_offset=1,m_max_cursor_index=1 end; END $$ DELIMITER ;1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.首先查看上面的sp的code,可以发现nameCursor6和nameCursor1属于同一层,因此他们的offset值一样。
复制greatsql> show procedure code processnames; +-----+---------------------------------------+ | Pos | Instruction | +-----+---------------------------------------+ | 0 | cpush nameCursor0@0: SELECT * FROM t1 | | 1 | cpush nameCursor1@1: SELECT * FROM t1 | | 2 | cpush nameCursor2@2: SELECT * FROM t1 | | 3 | cpush nameCursor3@3: SELECT * FROM t1 | | 4 | cpush nameCursor4@4: SELECT * FROM t1 | | 5 | cpush nameCursor5@5: SELECT * FROM t1 | | 6 | cpop 4 | | 7 | cpop 1 | | 8 | cpush nameCursor6@1: SELECT * FROM t1 | | 9 | cpop 1 | | 10 | cpop 1 | +-----+---------------------------------------+ 11 rows in set (6.02 sec)1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.然后通过debug查看每层sp_pcontext的参数值(相关参数值已经在上面标识出),发现第一个level=2的sp_pcontext的m_max_cursor_index值多了很多,预期值应该是4+1,但是实际是8+1,而上面的层都没错,这说明代码最里面那层m_max_cursor_index赋值错了。
二、问题调查过程
1、发现了问题点就看看代码里面对于每层的m_max_cursor_index是怎么赋值的。云南idc服务商
复制1、初始化sp_pcontext的时候所有的参数都为0 sp_pcontext::sp_pcontext(THD *thd) : m_level(0), m_max_var_index(0), m_max_cursor_index(0)...{init(0, 0, 0, 0);} 2、每加一层sp_pcontext,当前的m_cursor_offset=上一层cursor个数 sp_pcontext::sp_pcontext(THD *thd, sp_pcontext *prev, sp_pcontext::enum_scope scope) : m_level(prev->m_level + 1), m_max_var_index(0), m_max_cursor_index(0)... {init(prev->current_cursor_count());} void sp_pcontext::init(uint cursor_offset) {m_cursor_offset = cursor_offset;} uint current_cursor_count() const { return m_cursor_offset + static_cast<uint>(m_cursors.size()); } 3、退出当前sp_pcontext层,需要把当前的max_cursor_index()信息值赋值给上一层的m_max_cursor_index,即当前的cursor数量累加给上一层 sp_pcontext *sp_pcontext::pop_context() { uint submax = max_cursor_index(); if (submax > m_parent->m_max_cursor_index) m_parent->m_max_cursor_index = submax; } uint max_cursor_index() const { return m_max_cursor_index + static_cast<uint>(m_cursors.size()); } 4、每次增加一个cursor,m_max_cursor_index值递增,m_max_cursor_index是计数器。 bool sp_pcontext::add_cursor(LEX_STRING name) { if (m_cursors.size() == m_max_cursor_index) ++m_max_cursor_index; return m_cursors.push_back(name); }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.2、根据第一步的分析,只在最里面那层的m_max_cursor_index累加出来计算错误,看看上面的累加过程,是用max_cursor_index()值来累加的,于是查看max_cursor_index()函数的实现:
复制uint max_cursor_index() const { return m_max_cursor_index + static_cast<uint>(m_cursors.size()); }1.2.3.这里是把当前层的m_max_cursor_index值加上m_cursors.size(),但是亿华云在函数add_cursor里面,m_cursors数组每增加一个cursor,m_max_cursor_index都要加1,也就是说在最里面那层sp_pcontext的计算重复了,计算了2遍m_cursors.size(),导致上面的level=2那层的m_max_cursor_index值变成2*4=8了。到这里问题点发现。
三、问题解决方案
通过以上代码解析后,可以考虑只对最里面那层sp_pcontext的max_cursor_index()取值进行修改,最里面那层的sp_pcontext没有m_children,因此可以用这个数组值进行判断。代码作如下修改:
复制uint max_cursor_index() const { if(m_children.size() == 0) -- 最里面那层sp_pcontext直接返回m_max_cursor_index的值。 return m_max_cursor_index; -- 可以改为static_cast<uint>(m_cursors.size()),二者值一样。 else -- 上层sp_pcontext返回下层所有sp_pcontext的m_max_cursor_index的值,再加上当前层的m_cursors.size()值。 return m_max_cursor_index + static_cast<uint>(m_cursors.size()); }1.2.3.4.5.6.四、问题总结
在MySQL的sp里面使用cursor的话,因为m_max_cursor_index只用于统计,不用于实际赋值和计算过程,因此不影响使用。但是如果要用这个值用于二次开发,就要注意到这个问题。上面的修改方案只是其中一个解决方案,也可以根据自己的需要去改add_cursor的m_max_cursor_index的赋值过程。
这次发现的问题属于不参与计算的bug,但却影响开源代码的后续开发,在实际开发应用中类似的问题也要注意,一不小心就会踩坑。
相关文章
揭秘巨人江湖游戏工会卡的魅力与功能(了解工会卡的一般情况以及如何获取和使用)
摘要:巨人江湖是一款备受欢迎的多人在线角色扮演游戏,而工会卡则是游戏中重要的组织形式之一。本文将为你揭秘工会卡的魅力与功能,帮助你更好地了解如何获取和使用。什么是工会卡?...2025-11-04
Easycode是idea的一个插件,可以直接对数据的表生成entity,controller,service,dao,mapper,无需任何编码,简单而强大。1、安装(EasyCode)我这里的话是2025-11-04
这次疫情的情况大家也都了解了,各地也都延迟开学或者延迟开工,对于我们来说,正好是一次深入学习的机会。今天,我就带领大家分析一下新型冠状病毒的爆发趋势,也借此作为一次数据分析课程的实战案例,从 数据获取2025-11-04
Excel是日常工作中常用的办公软件之一。在制作表格的时候,有时需要在工作表中插入一个或多个行和列,同时也会遇到需要将工作表中多余的行或列删除的情况。本文就将通过使用Java程序来演示如何删除Exce2025-11-04用电脑做系统XP教程光盘,轻松学习配置系统(自学成才,操作简单,教程详细易懂)
摘要:在数字时代,电脑已经成为我们生活中必不可少的工具。然而,对于许多人来说,电脑的系统配置似乎是一个难题。为了帮助大家更轻松地掌握系统XP的配置技巧,我们推出了这款电脑系统XP教程光盘...2025-11-04
1. 自动断点有时候在执行 JavaScript 代码的时候,可能来不及设置断点,代码就被执行了,其实可以通过在代码中写上 debugger(代码中第 8 行),让代码执行到 debugger 的位置2025-11-04

最新评论