【问题标题】:Why does this MySQL trigger cause a stack overflow?为什么这个 MySQL 触发器会导致堆栈溢出?
【发布时间】:2011-02-16 20:51:50
【问题描述】:

我认为有人试图模拟具有第二个 auto_increment 值。刚刚升级到 MySQL 5.5.9

CREATE TABLE `job_title` (
  `job_id` int(11) NOT NULL AUTO_INCREMENT,
  `position_id` int(11) DEFAULT NULL,
  `title` varchar(255) COLLATE latin1_general_cs NOT NULL,
  `selectable` tinyint(4) NOT NULL DEFAULT '0',
  PRIMARY KEY (`job_id`),
  UNIQUE KEY `title` (`title`)
) ENGINE=InnoDB;

create trigger job_position_trigger
  before insert on job_title for each row
 begin
   if new.position_id is null then 
     set @position = (select max(position_id)+1 from job_title);
     set new.position_id = @position;
   end if;
 end

错误:Thread stack overrun: 9024 bytes used of a 131072 byte stack, and 128000 bytes needed. Use 'mysqld --thread_stack=#' to specify a bigger stack.' on query. Default database: 'mydb'. Query: 'insert ignore into job_title (title) values ('Morning Show Personality')

【问题讨论】:

  • 我运行了同样的东西,没有抱怨
  • 你用的是mysql 5.5吗?

标签: sql mysql triggers


【解决方案1】:

我今天遇到了同样的问题,每次触发器都会导致堆栈溢出。结果我的 Zend 社区服务器安装附带了一个默认的 my.cnf 文件,其中 thread_stack 大小设置为 128K,这导致每个线程中的堆栈可用 131072 字节:

mysql> show variables where `Variable_name` = 'thread_stack';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| thread_stack  | 131072 |
+---------------+--------+

所以我把 /usr/local/zend/mysql/data/my.cnf 中的那行注释掉了,重启了 mysql 守护进程,瞧! default 192K 是

mysql> show variables where `Variable_name` = 'thread_stack';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| thread_stack  | 196608 |
+---------------+--------+

现在您的表和 tchester 的触发器可以正常工作了 :)(请注意分隔符)

mysql> CREATE TABLE `job_title` (
    ->   `job_id` int(11) NOT NULL AUTO_INCREMENT,
    ->   `position_id` int(11) DEFAULT NULL,
    ->   `title` varchar(255) COLLATE latin1_general_cs NOT NULL,
    ->   `selectable` tinyint(4) NOT NULL DEFAULT '0',
    ->   PRIMARY KEY (`job_id`),
    ->   UNIQUE KEY `title` (`title`)
    -> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.14 sec)

mysql> DELIMITER &&
mysql> create trigger job_position_trigger   
    ->   before insert on job_title for each row  
    -> begin    
    ->     if new.position_id is null then       
    ->        set @position = (select max(position_id)+1 from job_title);
    ->        if @position is null then set @position = 1; end if;
    ->        set new.position_id = @position;    
    ->     end if;  
    -> end; 
    -> &&
Query OK, 0 rows affected (0.29 sec)

mysql> DELIMITER ;
mysql> insert into job_title (title, selectable) values ("test", 1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into job_title (title, selectable) values ("test2", 3);
Query OK, 1 row affected (0.00 sec)

mysql> select * from job_title;
+--------+-------------+-------+------------+
| job_id | position_id | title | selectable |
+--------+-------------+-------+------------+
|      1 |           1 | test  |          1 |
|      2 |           2 | test2 |          3 |
+--------+-------------+-------+------------+
2 rows in set (0.00 sec)

您得到的错误,131072 字节堆栈使用了 9024 字节,需要 128000 字节,这是有道理的:9024 + 128000 > 131072。

【讨论】:

    【解决方案2】:

    在 MySQL 5.1 下,我无法导致堆栈溢出,但触发器从未设置 [position_id] 字段。从未设置它的原因是,当您对空表或 [position_id] 列中只有 NULL 的表执行 MAX(position_id) + 1 时,您的 @position 变量将设置为 NULL。我想知道这是否导致触发器重新评估自身(看到 [position_id] 仍然为空),因此它反复调用自身导致堆栈溢出。您可以尝试的一种选择是更改触发器以检查 null @position 值并在分配之前将其强制为“1”。

    create trigger job_position_trigger   
      before insert on job_title for each row  
    begin    
        if new.position_id is null then       
           set @position = (select max(position_id)+1 from job_title);
           if @position is null then set @position = 1; end if;
           set new.position_id = @position;    
        end if;  
    end; 
    

    【讨论】:

    • 这也是我的第一个想法。
    猜你喜欢
    • 2014-12-20
    • 1970-01-01
    • 2018-07-01
    • 2011-01-13
    • 1970-01-01
    • 2016-02-24
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多