[MDEV-22711] Assertion `nr != 0' failed in handler::update_auto_increment Created: 2020-05-26  Updated: 2021-10-26  Resolved: 2021-10-26

Status: Closed
Project: MariaDB Server
Component/s: Partitioning
Affects Version/s: 10.2, 10.3, 10.4, 10.5
Fix Version/s: 10.2.41

Type: Bug Priority: Critical
Reporter: Roel Van de Paar Assignee: Alexey Botchkov
Resolution: Fixed Votes: 0
Labels: affects-tests, not-10.1

Issue Links:
Duplicate
duplicates MDEV-22118 handler::update_auto_increment(): Ass... Closed
Relates
relates to MDEV-15800 Assertion `next_insert_id >= auto_inc... Closed

 Description   

USE test;
SET SQL_MODE='';
SET SESSION insert_id=0;
CREATE TABLE t (a INT AUTO_INCREMENT KEY) PARTITION BY HASH (a) PARTITIONS 3;
INSERT INTO t VALUES ('');

Leads to:

10.5.4 8569dac1ec9f6853a0b2f3ea9bcbda67644ead24

mysqld: /test/10.5_dbg/sql/handler.cc:3574: int handler::update_auto_increment(): Assertion `nr != 0' failed.

10.5.4 8569dac1ec9f6853a0b2f3ea9bcbda67644ead24

Core was generated by `/test/MD260520-mariadb-10.5.4-linux-x86_64-dbg/bin/mysqld --no-defaults --core-'.
Program terminated with signal SIGABRT, Aborted.
#0  __pthread_kill (threadid=<optimized out>, signo=signo@entry=6)
    at ../sysdeps/unix/sysv/linux/pthread_kill.c:57
[Current thread is 1 (Thread 0x14b44ee82700 (LWP 1168779))]
(gdb) bt
#0  __pthread_kill (threadid=<optimized out>, signo=signo@entry=6) at ../sysdeps/unix/sysv/linux/pthread_kill.c:57
#1  0x0000558649c61d7a in my_write_core (sig=sig@entry=6) at /test/10.5_dbg/mysys/stacktrace.c:518
#2  0x0000558649407385 in handle_fatal_signal (sig=6) at /test/10.5_dbg/sql/signal_handler.cc:330
#3  <signal handler called>
#4  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#5  0x000014b44d5c6801 in __GI_abort () at abort.c:79
#6  0x000014b44d5b639a in __assert_fail_base (fmt=0x14b44d73d7d8 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=assertion@entry=0x558649f72840 "nr != 0", file=file@entry=0x558649f72162 "/test/10.5_dbg/sql/handler.cc", line=line@entry=3574, function=function@entry=0x558649f76460 <handler::update_auto_increment()::__PRETTY_FUNCTION__> "int handler::update_auto_increment()") at assert.c:92
#7  0x000014b44d5b6412 in __GI___assert_fail (assertion=assertion@entry=0x558649f72840 "nr != 0", file=file@entry=0x558649f72162 "/test/10.5_dbg/sql/handler.cc", line=line@entry=3574, function=function@entry=0x558649f76460 <handler::update_auto_increment()::__PRETTY_FUNCTION__> "int handler::update_auto_increment()") at assert.c:101
#8  0x00005586494131d2 in handler::update_auto_increment (this=this@entry=0x14b42c0380a0) at /test/10.5_dbg/sql/handler.cc:3574
#9  0x00005586497080ea in ha_partition::write_row (this=0x14b42c0380a0, buf=0x14b42c0511c8 "\377") at /test/10.5_dbg/sql/ha_partition.cc:4378
#10 0x000055864941b142 in handler::ha_write_row (this=0x14b42c0380a0, buf=0x14b42c0511c8 "\377") at /test/10.5_dbg/sql/handler.cc:6986
#11 0x000055864911ca66 in write_record (thd=thd@entry=0x14b42c015088, table=table@entry=0x14b42c0fa088, info=info@entry=0x14b44ee80a60, sink=sink@entry=0x0) at /test/10.5_dbg/sql/sql_insert.cc:2091
#12 0x0000558649127aa0 in mysql_insert (thd=thd@entry=0x14b42c015088, table_list=0x14b42c074180, fields=..., values_list=..., update_fields=..., update_values=..., duplic=DUP_ERROR, ignore=false, result=0x0) at /test/10.5_dbg/sql/sql_insert.cc:1088
#13 0x000055864915ea46 in mysql_execute_command (thd=thd@entry=0x14b42c015088) at /test/10.5_dbg/sql/sql_parse.cc:4553
#14 0x000055864916a02e in mysql_parse (thd=thd@entry=0x14b42c015088, rawbuf=<optimized out>, length=<optimized out>, parser_state=parser_state@entry=0x14b44ee813d0, is_com_multi=is_com_multi@entry=false, is_next_command=is_next_command@entry=false) at /test/10.5_dbg/sql/sql_parse.cc:7991
#15 0x0000558649156b42 in dispatch_command (command=command@entry=COM_QUERY, thd=thd@entry=0x14b42c015088, packet=packet@entry=0x14b42c067089 "INSERT INTO t VALUES ('')", packet_length=packet_length@entry=25, is_com_multi=is_com_multi@entry=false, is_next_command=is_next_command@entry=false) at /test/10.5_dbg/sql/sql_parse.cc:1874
#16 0x000055864915531c in do_command (thd=0x14b42c015088) at /test/10.5_dbg/sql/sql_parse.cc:1355
#17 0x00005586492af73f in do_handle_one_connection (connect=<optimized out>, connect@entry=0x14b42d8453a8, put_in_cache=put_in_cache@entry=true) at /test/10.5_dbg/sql/sql_connect.cc:1411
#18 0x00005586492afe5b in handle_one_connection (arg=arg@entry=0x14b42d8453a8) at /test/10.5_dbg/sql/sql_connect.cc:1313
#19 0x000055864970f14e in pfs_spawn_thread (arg=0x14b44c845888) at /test/10.5_dbg/storage/perfschema/pfs.cc:2201
#20 0x000014b44e2a96db in start_thread (arg=0x14b44ee82700) at pthread_create.c:463
#21 0x000014b44d6a788f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Bug confirmed present in:
MariaDB: 10.2.33 (dbg), 10.3.24 (dbg), 10.4.14 (dbg), 10.5.4 (dbg)

Bug confirmed not present in:
MariaDB: 10.1.46 (dbg), 10.1.46 (opt), 10.2.33 (opt), 10.3.24 (opt), 10.4.14 (opt), 10.5.4 (opt)
MySQL: 5.5.62 (dbg), 5.5.62 (opt), 5.6.47 (dbg), 5.6.47 (opt), 5.7.29 (dbg), 5.7.29 (opt), 8.0.19 (dbg), 8.0.19 (opt)



 Comments   
Comment by Roel Van de Paar [ 2020-05-26 ]

MDEV-15800 link not established, but may be related.

Comment by Roel Van de Paar [ 2021-02-13 ]

SET SESSION insert_id=0;
CREATE TABLE t (c INT) PARTITION BY LINEAR KEY(c) PARTITIONS 5;
INSERT INTO t VALUES(0);
ALTER TABLE t CHANGE c c INT UNSIGNED AUTO_INCREMENT,ADD KEY(c);

Leads to:

10.6.0 bfb4761ca04704d68dba51f76d7c9967f880a6ee (Debug)

mysqld: /test/10.6_dbg/sql/handler.cc:3660: int handler::update_auto_increment(): Assertion `nr != 0' failed.

10.6.0 bfb4761ca04704d68dba51f76d7c9967f880a6ee (Debug)

Core was generated by `/test/MD110221-mariadb-10.6.0-linux-x86_64-dbg/bin/mysqld --no-defaults --core-'.
Program terminated with signal SIGABRT, Aborted.
#0  __pthread_kill (threadid=<optimized out>, signo=signo@entry=6)
    at ../sysdeps/unix/sysv/linux/pthread_kill.c:56
[Current thread is 1 (Thread 0x1503c4095700 (LWP 1861856))]
(gdb) bt
#0  __pthread_kill (threadid=<optimized out>, signo=signo@entry=6) at ../sysdeps/unix/sysv/linux/pthread_kill.c:56
#1  0x00005583a7ff555c in my_write_core (sig=sig@entry=6) at /test/10.6_dbg/mysys/stacktrace.c:424
#2  0x00005583a778d4de in handle_fatal_signal (sig=6) at /test/10.6_dbg/sql/signal_handler.cc:330
#3  <signal handler called>
#4  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#5  0x00001503c4ef7859 in __GI_abort () at abort.c:79
#6  0x00001503c4ef7729 in __assert_fail_base (fmt=0x1503c508d588 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=0x5583a82ee070 "nr != 0", file=0x5583a82ed8ff "/test/10.6_dbg/sql/handler.cc", line=3660, function=<optimized out>) at assert.c:92
#7  0x00001503c4f08f36 in __GI___assert_fail (assertion=assertion@entry=0x5583a82ee070 "nr != 0", file=file@entry=0x5583a82ed8ff "/test/10.6_dbg/sql/handler.cc", line=line@entry=3660, function=function@entry=0x5583a82ef970 "int handler::update_auto_increment()") at assert.c:101
#8  0x00005583a7799419 in handler::update_auto_increment (this=this@entry=0x150374096e00) at /test/10.6_dbg/sql/handler.cc:3660
#9  0x00005583a7acc01a in ha_partition::write_row (this=0x150374096e00, buf=0x150374098b28 "\377") at /test/10.6_dbg/sql/ha_partition.cc:4436
#10 0x00005583a77a1826 in handler::ha_write_row (this=0x150374096e00, buf=0x150374098b28 "\377") at /test/10.6_dbg/sql/handler.cc:7151
#11 0x00005583a75979e6 in copy_data_between_tables (thd=thd@entry=0x150374000db8, from=from@entry=0x15037403ae68, to=to@entry=0x1503740bcd88, create=@0x1503c4093430: {<base_list> = {<Sql_alloc> = {<No data fields>}, first = 0x150374013a70, last = 0x150374013a70, elements = 1}, <No data fields>}, ignore=ignore@entry=false, order_num=order_num@entry=0, order=<optimized out>, copied=<optimized out>, deleted=<optimized out>, keys_onoff=<optimized out>, alter_ctx=<optimized out>) at /test/10.6_dbg/sql/sql_table.cc:11478
#12 0x00005583a75a588b in mysql_alter_table (thd=thd@entry=0x150374000db8, new_db=new_db@entry=0x150374005830, new_name=new_name@entry=0x150374005c30, create_info=create_info@entry=0x1503c40934a0, table_list=<optimized out>, table_list@entry=0x1503740127e0, alter_info=alter_info@entry=0x1503c40933d0, order_num=0, order=0x0, ignore=false, if_exists=false) at /test/10.6_dbg/sql/sql_table.cc:10827
#13 0x00005583a7629fa2 in Sql_cmd_alter_table::execute (this=<optimized out>, thd=0x150374000db8) at /test/10.6_dbg/sql/structs.h:559
#14 0x00005583a74ce60c in mysql_execute_command (thd=thd@entry=0x150374000db8) at /test/10.6_dbg/sql/sql_parse.cc:5880
#15 0x00005583a74b521a in mysql_parse (thd=thd@entry=0x150374000db8, rawbuf=<optimized out>, length=<optimized out>, parser_state=parser_state@entry=0x1503c40943d0) at /test/10.6_dbg/sql/sql_parse.cc:7906
#16 0x00005583a74c330b in dispatch_command (command=command@entry=COM_QUERY, thd=thd@entry=0x150374000db8, packet=packet@entry=0x15037401aac9 "ALTER TABLE t CHANGE c c INT UNSIGNED AUTO_INCREMENT,ADD KEY(c)", packet_length=packet_length@entry=63) at /test/10.6_dbg/sql/sql_class.h:1295
#17 0x00005583a74c663d in do_command (thd=0x150374000db8) at /test/10.6_dbg/sql/sql_parse.cc:1365
#18 0x00005583a76221ab in do_handle_one_connection (connect=<optimized out>, connect@entry=0x5583aa8ff388, put_in_cache=put_in_cache@entry=true) at /test/10.6_dbg/sql/sql_connect.cc:1410
#19 0x00005583a76228af in handle_one_connection (arg=arg@entry=0x5583aa8ff388) at /test/10.6_dbg/sql/sql_connect.cc:1312
#20 0x00005583a7ad627d in pfs_spawn_thread (arg=0x5583aa7ea998) at /test/10.6_dbg/storage/perfschema/pfs.cc:2201
#21 0x00001503c5405609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#22 0x00001503c4ff4293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Bug confirmed present in:
MariaDB: 10.2.37 (dbg), 10.3.28 (dbg), 10.4.18 (dbg), 10.6.0 (dbg)

Bug (or feature/syntax) confirmed not present in:
MariaDB: 10.2.37 (opt), 10.3.28 (opt), 10.4.18 (opt), 10.5.9 (dbg), 10.5.9 (opt), 10.6.0 (opt)
MySQL: 5.5.62 (dbg), 5.5.62 (opt), 5.6.51 (dbg), 5.6.51 (opt), 5.7.33 (dbg), 5.7.33 (opt), 8.0.23 (dbg), 8.0.23 (opt)

Comment by Roel Van de Paar [ 2021-02-13 ]

Unique ID's seen so far

nr != 0|SIGABRT|handler::update_auto_increment|ha_partition::write_row|handler::ha_write_row|write_record
nr != 0|SIGABRT|handler::update_auto_increment|ha_partition::write_row|handler::ha_write_row|copy_data_between_tables

Comment by Roel Van de Paar [ 2021-03-06 ]

SET sql_mode='';
SET SESSION insert_id=-1;
CREATE TABLE t (a INT AUTO_INCREMENT KEY)PARTITION by KEY(a);
INSERT INTO t VALUES(CONVERT (_ucs2 0x062A064606470627 USING utf8));

Leads to:

10.6.0 03ff588d153f22f00ff00923e82498cbac63505f (Debug)

mysqld: /test/10.6_dbg/sql/handler.cc:3661: int handler::update_auto_increment(): Assertion `nr != 0' failed.

10.6.0 03ff588d153f22f00ff00923e82498cbac63505f (Debug)

Core was generated by `/test/MD060321-mariadb-10.6.0-linux-x86_64-dbg/bin/mysqld --no-defaults --core-'.
Program terminated with signal SIGABRT, Aborted.
#0  __pthread_kill (threadid=<optimized out>, signo=signo@entry=6)
    at ../sysdeps/unix/sysv/linux/pthread_kill.c:56
[Current thread is 1 (Thread 0x15114c5da700 (LWP 3632551))]
(gdb) bt
#0  __pthread_kill (threadid=<optimized out>, signo=signo@entry=6) at ../sysdeps/unix/sysv/linux/pthread_kill.c:56
#1  0x000055701ef8a3f3 in my_write_core (sig=sig@entry=6) at /test/10.6_dbg/mysys/stacktrace.c:424
#2  0x000055701e72f195 in handle_fatal_signal (sig=6) at /test/10.6_dbg/sql/signal_handler.cc:331
#3  <signal handler called>
#4  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#5  0x000015114da3e859 in __GI_abort () at abort.c:79
#6  0x000015114da3e729 in __assert_fail_base (fmt=0x15114dbd4588 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=0x55701f2838b3 "nr != 0", file=0x55701f283117 "/test/10.6_dbg/sql/handler.cc", line=3661, function=<optimized out>) at assert.c:92
#7  0x000015114da4ff36 in __GI___assert_fail (assertion=assertion@entry=0x55701f2838b3 "nr != 0", file=file@entry=0x55701f283117 "/test/10.6_dbg/sql/handler.cc", line=line@entry=3661, function=function@entry=0x55701f285388 "int handler::update_auto_increment()") at assert.c:101
#8  0x000055701e73ad75 in handler::update_auto_increment (this=this@entry=0x1510fc0296d0) at /test/10.6_dbg/sql/handler.cc:3661
#9  0x000055701ea66acc in ha_partition::write_row (this=0x1510fc0296d0, buf=0x1510fc029278 "\377") at /test/10.6_dbg/sql/ha_partition.cc:4436
#10 0x000055701e742f37 in handler::ha_write_row (this=0x1510fc0296d0, buf=0x1510fc029278 "\377") at /test/10.6_dbg/sql/handler.cc:7155
#11 0x000055701e41f519 in write_record (thd=thd@entry=0x1510fc000db8, table=table@entry=0x1510fc028dd8, info=info@entry=0x15114c5d8d10, sink=sink@entry=0x0) at /test/10.6_dbg/sql/sql_insert.cc:2106
#12 0x000055701e42ad7f in mysql_insert (thd=thd@entry=0x1510fc000db8, table_list=0x1510fc013cb0, fields=@0x1510fc005e30: {<base_list> = {<Sql_alloc> = {<No data fields>}, first = 0x55701fa2e240 <end_of_list>, last = 0x1510fc005e30, elements = 0}, <No data fields>}, values_list=@0x1510fc005e78: {<base_list> = {<Sql_alloc> = {<No data fields>}, first = 0x1510fc014a90, last = 0x1510fc014a90, elements = 1}, <No data fields>}, update_fields=@0x1510fc005e60: {<base_list> = {<Sql_alloc> = {<No data fields>}, first = 0x55701fa2e240 <end_of_list>, last = 0x1510fc005e60, elements = 0}, <No data fields>}, update_values=@0x1510fc005e48: {<base_list> = {<Sql_alloc> = {<No data fields>}, first = 0x55701fa2e240 <end_of_list>, last = 0x1510fc005e48, elements = 0}, <No data fields>}, duplic=DUP_ERROR, ignore=false, result=0x0) at /test/10.6_dbg/sql/sql_insert.cc:1099
#13 0x000055701e47142b in mysql_execute_command (thd=thd@entry=0x1510fc000db8) at /test/10.6_dbg/sql/sql_parse.cc:4545
#14 0x000055701e45c1d7 in mysql_parse (thd=thd@entry=0x1510fc000db8, rawbuf=<optimized out>, length=<optimized out>, parser_state=parser_state@entry=0x15114c5d9410) at /test/10.6_dbg/sql/sql_parse.cc:7998
#15 0x000055701e46ab03 in dispatch_command (command=command@entry=COM_QUERY, thd=thd@entry=0x1510fc000db8, packet=packet@entry=0x1510fc00b339 "INSERT INTO t VALUES(CONVERT (_ucs2 0x062A064606470627 USING utf8))", packet_length=packet_length@entry=67, blocking=blocking@entry=true) at /test/10.6_dbg/sql/sql_class.h:1318
#16 0x000055701e46dede in do_command (thd=0x1510fc000db8, blocking=blocking@entry=true) at /test/10.6_dbg/sql/sql_parse.cc:1397
#17 0x000055701e5c6c8e in do_handle_one_connection (connect=<optimized out>, connect@entry=0x5570219e7398, put_in_cache=put_in_cache@entry=true) at /test/10.6_dbg/sql/sql_connect.cc:1410
#18 0x000055701e5c7293 in handle_one_connection (arg=arg@entry=0x5570219e7398) at /test/10.6_dbg/sql/sql_connect.cc:1312
#19 0x000055701ea70ce9 in pfs_spawn_thread (arg=0x55702192fc48) at /test/10.6_dbg/storage/perfschema/pfs.cc:2201
#20 0x000015114df4c609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#21 0x000015114db3b293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Bug confirmed present in:
MariaDB: 10.2.38 (dbg), 10.3.29 (dbg), 10.4.19 (dbg), 10.5.10 (dbg), 10.6.0 (dbg)

Bug (or feature/syntax) confirmed not present in:
MariaDB: 10.2.38 (opt), 10.3.29 (opt), 10.4.19 (opt), 10.5.10 (opt), 10.6.0 (opt)
MySQL: 5.5.62 (dbg), 5.5.62 (opt), 5.6.51 (dbg), 5.6.51 (opt), 5.7.33 (dbg), 5.7.33 (opt), 8.0.23 (dbg), 8.0.23 (opt)

Comment by Alexey Botchkov [ 2021-10-20 ]

Proposed fix
https://github.com/MariaDB/server/commit/1915f8028c0c630ed24a0a5c931282b335404bdf

Comment by Michael Widenius [ 2021-10-21 ]

The patch is wrong. 'nr == 0' for insert id, means that we should generate a new insert id.

Here is an example of how things works:

MariaDB [test]> CREATE TABLE t (a INT AUTO_INCREMENT KEY) PARTITION BY HASH (a) PARTITIONS 3;
MariaDB [test]> INSERT INTO t VALUES (0);
MariaDB [test]> select * from t;
+---+
| a |
+---+
| 1 |
+---+

In other words, one cannot insert a row with value 0 with INSERT.
Allowing one to set insert_id to 0, would allow one to insert rows with impossible/not allowed insert id's.
One can in theory do that with UPDATE, but one should not be able to do that with INSERT.
Another reason for not allowing this, is that a mysqldump would not be able to restore the original data (0 would be converted to max used value + 1)

The proper fix is to give an error if one tries to set insert_id to <= 0

Comment by Alexey Botchkov [ 2021-10-22 ]

Just disallowing the insert_id=0 leads to many changes in the present behaviour, breaks tests.
It probably requires changing the default value for the insert_id, since it is 0 now.
So i propose the different fix. Setting 0 to the insert_id just empties the interval's list and the following INSERT just takes the next auto_increment counter.
https://github.com/MariaDB/server/commit/8295b36fd7a5bb50b924384a2832dee2e17b3bce

Comment by Sergei Golubchik [ 2021-10-24 ]

Yes, this makes sense to me. I think it's ok to push

Generated at Thu Feb 08 09:16:52 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.