Bug #57890 Assertion failed: next_insert_id == 0 with on duplicate key update
Submitted: 1 Nov 2010 11:17 Modified: 13 Dec 2010 7:46
Reporter: Shane Bester (Platinum Quality Contributor) Email Updates:
Status: Closed Impact on me:
Category:MySQL Server: Partitions Severity:S1 (Critical)
Version:5.1.53-debug, 5.5.8-debug OS:Any
Assigned to: Mattias Jonsson CPU Architecture:Any

[1 Nov 2010 11:17] Shane Bester
Version: '5.1.51-enterprise-gpl-advanced-debug'  socket: ''  port: 3306  MySQL Enterprise Server - Advanced Editi
Assertion failed: next_insert_id == 0, file .\handler.cc, line 4611


How to repeat:
on debug server:

drop table if exists t;
create table `t` (`a` int auto_increment primary key) engine=myisam
partition by key(a) partitions 2;
set sql_mode='';
insert into `t` set `a` = 0.7 on duplicate key update `a` = 0;
insert into `t` set `a` = 0.7 on duplicate key update `a` = 0;
[1 Nov 2010 11:38] Valeriy Kravchuk
Verified on Ubuntu:

openxs@ubuntu:~/dbs/5.5$ bin/mysql --no-defaults -uroot test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.7-rc-debug Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> drop table if exists t;
Query OK, 0 rows affected, 1 warning (0.05 sec)

mysql> create table `t` (`a` int auto_increment primary key) engine=myisam
    -> partition by key(a) partitions 2;
Query OK, 0 rows affected (0.09 sec)

mysql> set sql_mode='';
Query OK, 0 rows affected (0.00 sec)

mysql> insert into `t` set `a` = 0.7 on duplicate key update `a` = 0;
Query OK, 1 row affected (0.09 sec)

mysql> insert into `t` set `a` = 0.7 on duplicate key update `a` = 0;
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql> 101101 13:36:41 mysqld_safe Number of processes running now: 0
101101 13:36:41 mysqld_safe mysqld restarted

mysql> exit
openxs@ubuntu:~/dbs/5.5$ tail -80 var/ubuntu.err 
101101 12:14:25 [Note] /home/openxs/dbs/5.5/libexec/mysqld: Normal shutdown

101101 12:14:25 [Note] Event Scheduler: Purging the queue. 0 events
101101 12:14:25  InnoDB: Starting shutdown...
101101 12:14:26  InnoDB: Shutdown completed; log sequence number 1606401
101101 12:14:26 [Note] /home/openxs/dbs/5.5/libexec/mysqld: Shutdown complete

101101 12:14:26 mysqld_safe mysqld from pid file /home/openxs/dbs/5.5/var/ubuntu.pid ended
101101 13:36:22 mysqld_safe Starting mysqld daemon with databases from /home/openxs/dbs/5.5/var
101101 13:36:23 [Note] Plugin 'FEDERATED' is disabled.
InnoDB: The InnoDB memory heap is disabled
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Compressed tables use zlib 1.2.3
101101 13:36:23  InnoDB: highest supported file format is Barracuda.
101101 13:36:23 InnoDB 1.1.2 started; log sequence number 1606401
101101 13:36:23 [Note] Event Scheduler: Loaded 0 events
101101 13:36:23 [Note] /home/openxs/dbs/5.5/libexec/mysqld: ready for connections.
Version: '5.5.7-rc-debug'  socket: '/tmp/mysql.sock'  port: 3306  Source distribution
mysqld: handler.cc:4634: int handler::ha_external_lock(THD*, int): Assertion `next_insert_id == 0' failed.
101101 13:36:41 - mysqld got signal 6 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
We will try our best to scrape up some info that will hopefully help diagnose
the problem, but since we have already crashed, something is definitely wrong
and this may fail.

It is possible that mysqld could use up to 
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 337925 K
bytes of memory
Hope that's ok; if not, decrease some variables in the equation.

thd: 0xa07cff8
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong...
stack_bottom = 0xa91fa35c thread_stack 0x30000
Trying to get some variables.
Some pointers may be invalid and cause the dump to abort...
thd->query at 0xa0a4040 = insert into `t` set `a` = 0.7 on duplicate key update `a` = 0
[11 Nov 2010 10:36] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:


3553 Mattias Jonsson	2010-11-11
      Bug#57890: Assertion failed: next_insert_id == 0
                 with on duplicate key update
      There was a missed corner case in the partitioning
      handler, which caused the next_insert_id to be changed
      in the second level handlers (i.e the hander of a partition),
      which caused this debug assertion.
      The solution was to always ensure that only the partitioning
      level generates auto_increment values, since if it was done
      within a partition, it may fail to match the partition
     @ mysql-test/suite/parts/inc/partition_auto_increment.inc
        Added tests
     @ mysql-test/suite/parts/r/partition_auto_increment_blackhole.result
        updated results
     @ mysql-test/suite/parts/r/partition_auto_increment_innodb.result
        updated results
     @ mysql-test/suite/parts/r/partition_auto_increment_memory.result
        updated results
     @ mysql-test/suite/parts/r/partition_auto_increment_myisam.result
        updated results
     @ sql/ha_partition.cc
        In <engine>::write_row the auto_inc value is generated
        through handler::update_auto_increment (which calls <engine>::get_auto_increment() if needed).
        * INSERT_ID was set to 0
        * it was updated to 0 by 'INSERT ... ON DUPLICATE KEY UPDATE' and changed partitions for the row
        Then it would try to generate a auto_increment value in the
        <engine for a specific partition>::write_row, which will
        trigger the assert.
        So the solution is to prevent this by,
        in ha_partition::write_row set auto_inc_field_not_null and
        in ha_partition::update_row (when changing partition) temporary
        set table->next_number_field to NULL which calling the
        partitions ::write_row().
[16 Nov 2010 1:28] Mattias Jonsson
pushed to mysql-5.1-bugteam and merged into mysql-5.5-bugteam and mysql-trunk-bugfixing.
[18 Nov 2010 15:15] Jon Stephens
Documented in the 5.1.54 and 5.5.8 changelogs as follows:

        An INSERT ... ON DUPLICATE KEY UPDATE column = 0 on an
        AUTO_INCREMENT column caused the debug server to crash.

Set Need Merge, waiting for push to -trunk.
[5 Dec 2010 12:43] Bugs System
Pushed into mysql-trunk 5.6.1 (revid:alexander.nozdrin@oracle.com-20101205122447-6x94l4fmslpbttxj) (version source revid:alexander.nozdrin@oracle.com-20101205122447-6x94l4fmslpbttxj) (merge vers: 5.6.1) (pib:23)
[13 Dec 2010 7:46] Jon Stephens
Issue does not occur in a 5.6 release; no additional changelog entry required.

[15 Dec 2010 5:51] Bugs System
Pushed into mysql-5.1 5.1.55 (revid:sunanda.menon@oracle.com-20101215054055-vgwki317xg1wphhh) (version source revid:sunanda.menon@oracle.com-20101215054055-vgwki317xg1wphhh) (merge vers: 5.1.55) (pib:23)
[16 Dec 2010 22:34] Bugs System
Pushed into mysql-5.5 5.5.9 (revid:jonathan.perkin@oracle.com-20101216101358-fyzr1epq95a3yett) (version source revid:jonathan.perkin@oracle.com-20101216101358-fyzr1epq95a3yett) (merge vers: 5.5.9) (pib:24)