=== added file 'mysql-test/suite/innodb/r/innodb-autoinc-61209.result' --- mysql-test/suite/innodb/r/innodb-autoinc-61209.result 1970-01-01 00:00:00 +0000 +++ mysql-test/suite/innodb/r/innodb-autoinc-61209.result 2012-01-26 22:37:01 +0000 @@ -0,0 +1,30 @@ +DROP TABLE IF EXISTS bug_61209; +CREATE TABLE bug_61209 (a INT auto_increment, PRIMARY KEY(a)) ENGINE=InnoDB; +INSERT INTO bug_61209 VALUES (), (); + +# Connect con1 + +# Connect con2 + +# Connection con1 +SET SESSION auto_increment_increment=3; +SET SESSION auto_increment_offset=2; +SELECT GET_LOCK('a', 10); + +# Connection con2 +SET SESSION auto_increment_increment=3; +SET SESSION auto_increment_offset=2; +INSERT INTO bug_61209 (a) VALUES (NULL), (NULL), (NULL + GET_LOCK('a', 10)); + +# Connection con1 reap +GET_LOCK('a', 10) +1 +INSERT INTO bug_61209 (a) VALUES (NULL), (NULL), (NULL); +SELECT RELEASE_LOCK('a'); + +# Connection con2 reap + +# Connection con1 reap +RELEASE_LOCK('a') +1 +DROP TABLE bug_61209; === modified file 'mysql-test/suite/innodb/r/innodb-autoinc.result' --- mysql-test/suite/innodb/r/innodb-autoinc.result 2010-12-13 12:11:16 +0000 +++ mysql-test/suite/innodb/r/innodb-autoinc.result 2012-01-26 22:37:01 +0000 @@ -366,7 +366,7 @@ 310 400 1000 -1010 +1110 DROP TABLE t1; SET @@SESSION.AUTO_INCREMENT_INCREMENT=1, @@SESSION.AUTO_INCREMENT_OFFSET=1; SET @@INSERT_ID=1; @@ -648,198 +648,198 @@ `n` int(10) unsigned NOT NULL, `o` enum('FALSE','TRUE') DEFAULT NULL, PRIMARY KEY (`m`) -) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1 -INSERT INTO t1 (b,c) SELECT n,o FROM t2 ; -SHOW CREATE TABLE t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `a` int(11) unsigned NOT NULL AUTO_INCREMENT, - `b` int(10) unsigned NOT NULL, - `c` enum('FALSE','TRUE') DEFAULT NULL, - PRIMARY KEY (`a`) -) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1 -INSERT INTO t1 (b,c) SELECT n,o FROM t2 ; -SELECT * FROM t1; -a b c -1 1 TRUE -2 1 FALSE -3 2 TRUE -4 2 FALSE -5 3 TRUE -6 3 FALSE -7 4 TRUE -8 4 FALSE -9 5 TRUE -10 5 FALSE -13 1 TRUE -14 1 FALSE -15 2 TRUE -16 2 FALSE -17 3 TRUE -18 3 FALSE -19 4 TRUE -20 4 FALSE -21 5 TRUE -22 5 FALSE -SHOW CREATE TABLE t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `a` int(11) unsigned NOT NULL AUTO_INCREMENT, - `b` int(10) unsigned NOT NULL, - `c` enum('FALSE','TRUE') DEFAULT NULL, - PRIMARY KEY (`a`) -) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1 -INSERT INTO t1 (b,c) SELECT n,o FROM t2 WHERE o = 'false'; -SELECT * FROM t1; -a b c -1 1 TRUE -2 1 FALSE -3 2 TRUE -4 2 FALSE -5 3 TRUE -6 3 FALSE -7 4 TRUE -8 4 FALSE -9 5 TRUE -10 5 FALSE -13 1 TRUE -14 1 FALSE -15 2 TRUE -16 2 FALSE -17 3 TRUE -18 3 FALSE -19 4 TRUE -20 4 FALSE -21 5 TRUE -22 5 FALSE -23 1 FALSE -24 2 FALSE -25 3 FALSE -26 4 FALSE -27 5 FALSE -SHOW CREATE TABLE t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `a` int(11) unsigned NOT NULL AUTO_INCREMENT, - `b` int(10) unsigned NOT NULL, - `c` enum('FALSE','TRUE') DEFAULT NULL, - PRIMARY KEY (`a`) -) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1 -INSERT INTO t1 (b,c) SELECT n,o FROM t2 WHERE o = 'false'; -SELECT * FROM t1; -a b c -1 1 TRUE -2 1 FALSE -3 2 TRUE -4 2 FALSE -5 3 TRUE -6 3 FALSE -7 4 TRUE -8 4 FALSE -9 5 TRUE -10 5 FALSE -13 1 TRUE -14 1 FALSE -15 2 TRUE -16 2 FALSE -17 3 TRUE -18 3 FALSE -19 4 TRUE -20 4 FALSE -21 5 TRUE -22 5 FALSE -23 1 FALSE -24 2 FALSE -25 3 FALSE -26 4 FALSE -27 5 FALSE -30 1 FALSE -31 2 FALSE -32 3 FALSE -33 4 FALSE -34 5 FALSE -SHOW CREATE TABLE t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `a` int(11) unsigned NOT NULL AUTO_INCREMENT, - `b` int(10) unsigned NOT NULL, - `c` enum('FALSE','TRUE') DEFAULT NULL, - PRIMARY KEY (`a`) -) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=latin1 -INSERT INTO t1 (b,c) SELECT n,o FROM t2 WHERE o = 'false'; -SHOW CREATE TABLE t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `a` int(11) unsigned NOT NULL AUTO_INCREMENT, - `b` int(10) unsigned NOT NULL, - `c` enum('FALSE','TRUE') DEFAULT NULL, - PRIMARY KEY (`a`) -) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=latin1 -INSERT INTO t1 (b,c) SELECT n,o FROM t2 WHERE o = 'false'; -SHOW CREATE TABLE t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `a` int(11) unsigned NOT NULL AUTO_INCREMENT, - `b` int(10) unsigned NOT NULL, - `c` enum('FALSE','TRUE') DEFAULT NULL, - PRIMARY KEY (`a`) -) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=latin1 -INSERT INTO t1 (b,c) SELECT n,o FROM t2 WHERE o = 'false'; -SELECT * FROM t1; -a b c -1 1 TRUE -2 1 FALSE -3 2 TRUE -4 2 FALSE -5 3 TRUE -6 3 FALSE -7 4 TRUE -8 4 FALSE -9 5 TRUE -10 5 FALSE -13 1 TRUE -14 1 FALSE -15 2 TRUE -16 2 FALSE -17 3 TRUE -18 3 FALSE -19 4 TRUE -20 4 FALSE -21 5 TRUE -22 5 FALSE -23 1 FALSE -24 2 FALSE -25 3 FALSE -26 4 FALSE -27 5 FALSE -30 1 FALSE -31 2 FALSE -32 3 FALSE -33 4 FALSE -34 5 FALSE -37 1 FALSE -38 2 FALSE -39 3 FALSE -40 4 FALSE -41 5 FALSE -44 1 FALSE -45 2 FALSE -46 3 FALSE -47 4 FALSE -48 5 FALSE -51 1 FALSE -52 2 FALSE -53 3 FALSE -54 4 FALSE -55 5 FALSE -SHOW CREATE TABLE t1; -Table Create Table -t1 CREATE TABLE `t1` ( - `a` int(11) unsigned NOT NULL AUTO_INCREMENT, - `b` int(10) unsigned NOT NULL, - `c` enum('FALSE','TRUE') DEFAULT NULL, - PRIMARY KEY (`a`) -) ENGINE=InnoDB AUTO_INCREMENT=58 DEFAULT CHARSET=latin1 +) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1 +INSERT INTO t1 (b,c) SELECT n,o FROM t2 ; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) unsigned NOT NULL AUTO_INCREMENT, + `b` int(10) unsigned NOT NULL, + `c` enum('FALSE','TRUE') DEFAULT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1 +INSERT INTO t1 (b,c) SELECT n,o FROM t2 ; +SELECT * FROM t1; +a b c +1 1 TRUE +2 1 FALSE +3 2 TRUE +4 2 FALSE +5 3 TRUE +6 3 FALSE +7 4 TRUE +8 4 FALSE +9 5 TRUE +10 5 FALSE +16 1 TRUE +17 1 FALSE +18 2 TRUE +19 2 FALSE +20 3 TRUE +21 3 FALSE +22 4 TRUE +23 4 FALSE +24 5 TRUE +25 5 FALSE +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) unsigned NOT NULL AUTO_INCREMENT, + `b` int(10) unsigned NOT NULL, + `c` enum('FALSE','TRUE') DEFAULT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=latin1 +INSERT INTO t1 (b,c) SELECT n,o FROM t2 WHERE o = 'false'; +SELECT * FROM t1; +a b c +1 1 TRUE +2 1 FALSE +3 2 TRUE +4 2 FALSE +5 3 TRUE +6 3 FALSE +7 4 TRUE +8 4 FALSE +9 5 TRUE +10 5 FALSE +16 1 TRUE +17 1 FALSE +18 2 TRUE +19 2 FALSE +20 3 TRUE +21 3 FALSE +22 4 TRUE +23 4 FALSE +24 5 TRUE +25 5 FALSE +31 1 FALSE +32 2 FALSE +33 3 FALSE +34 4 FALSE +35 5 FALSE +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) unsigned NOT NULL AUTO_INCREMENT, + `b` int(10) unsigned NOT NULL, + `c` enum('FALSE','TRUE') DEFAULT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=latin1 +INSERT INTO t1 (b,c) SELECT n,o FROM t2 WHERE o = 'false'; +SELECT * FROM t1; +a b c +1 1 TRUE +2 1 FALSE +3 2 TRUE +4 2 FALSE +5 3 TRUE +6 3 FALSE +7 4 TRUE +8 4 FALSE +9 5 TRUE +10 5 FALSE +16 1 TRUE +17 1 FALSE +18 2 TRUE +19 2 FALSE +20 3 TRUE +21 3 FALSE +22 4 TRUE +23 4 FALSE +24 5 TRUE +25 5 FALSE +31 1 FALSE +32 2 FALSE +33 3 FALSE +34 4 FALSE +35 5 FALSE +38 1 FALSE +39 2 FALSE +40 3 FALSE +41 4 FALSE +42 5 FALSE +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) unsigned NOT NULL AUTO_INCREMENT, + `b` int(10) unsigned NOT NULL, + `c` enum('FALSE','TRUE') DEFAULT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=45 DEFAULT CHARSET=latin1 +INSERT INTO t1 (b,c) SELECT n,o FROM t2 WHERE o = 'false'; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) unsigned NOT NULL AUTO_INCREMENT, + `b` int(10) unsigned NOT NULL, + `c` enum('FALSE','TRUE') DEFAULT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=latin1 +INSERT INTO t1 (b,c) SELECT n,o FROM t2 WHERE o = 'false'; +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) unsigned NOT NULL AUTO_INCREMENT, + `b` int(10) unsigned NOT NULL, + `c` enum('FALSE','TRUE') DEFAULT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=latin1 +INSERT INTO t1 (b,c) SELECT n,o FROM t2 WHERE o = 'false'; +SELECT * FROM t1; +a b c +1 1 TRUE +2 1 FALSE +3 2 TRUE +4 2 FALSE +5 3 TRUE +6 3 FALSE +7 4 TRUE +8 4 FALSE +9 5 TRUE +10 5 FALSE +16 1 TRUE +17 1 FALSE +18 2 TRUE +19 2 FALSE +20 3 TRUE +21 3 FALSE +22 4 TRUE +23 4 FALSE +24 5 TRUE +25 5 FALSE +31 1 FALSE +32 2 FALSE +33 3 FALSE +34 4 FALSE +35 5 FALSE +38 1 FALSE +39 2 FALSE +40 3 FALSE +41 4 FALSE +42 5 FALSE +45 1 FALSE +46 2 FALSE +47 3 FALSE +48 4 FALSE +49 5 FALSE +52 1 FALSE +53 2 FALSE +54 3 FALSE +55 4 FALSE +56 5 FALSE +59 1 FALSE +60 2 FALSE +61 3 FALSE +62 4 FALSE +63 5 FALSE +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `a` int(11) unsigned NOT NULL AUTO_INCREMENT, + `b` int(10) unsigned NOT NULL, + `c` enum('FALSE','TRUE') DEFAULT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=66 DEFAULT CHARSET=latin1 DROP TABLE t1; DROP TABLE t2; DROP TABLE IF EXISTS t1; === added file 'mysql-test/suite/innodb/t/innodb-autoinc-61209.test' --- mysql-test/suite/innodb/t/innodb-autoinc-61209.test 1970-01-01 00:00:00 +0000 +++ mysql-test/suite/innodb/t/innodb-autoinc-61209.test 2012-01-26 22:37:01 +0000 @@ -0,0 +1,61 @@ +# This is the test case for bug #61209 - duplicate key errors +# when using auto_increment_increment > 1 and auto_increment_offset > 1 +# +--source include/have_innodb.inc + +# +# Precautionary clean up. +# +--disable_warnings +DROP TABLE IF EXISTS bug_61209; +--enable_warnings + +# +# Create test data. +# +CREATE TABLE bug_61209 (a INT auto_increment, PRIMARY KEY(a)) ENGINE=InnoDB; + +INSERT INTO bug_61209 VALUES (), (); + +--echo +--echo # Connect con1 +--connect (con1,localhost,root,,) +--echo +--echo # Connect con2 +--connect (con2,localhost,root,,) + +--echo +--echo # Connection con1 +--connection con1 +SET SESSION auto_increment_increment=3; +SET SESSION auto_increment_offset=2; +send SELECT GET_LOCK('a', 10); + +--echo +--echo # Connection con2 +--connection con2 +SET SESSION auto_increment_increment=3; +SET SESSION auto_increment_offset=2; +send INSERT INTO bug_61209 (a) VALUES (NULL), (NULL), (NULL + GET_LOCK('a', 10)); + +--echo +--echo # Connection con1 reap +--connection con1 +reap; +INSERT INTO bug_61209 (a) VALUES (NULL), (NULL), (NULL); +send SELECT RELEASE_LOCK('a'); + +--echo +--echo # Connection con2 reap +--connection con2 +reap; + +--echo +--echo # Connection con1 reap +--connection con1 +reap; + +# +# Clean up +# +DROP TABLE bug_61209; === modified file 'storage/innobase/handler/ha_innodb.cc' --- storage/innobase/handler/ha_innodb.cc 2012-01-16 05:00:51 +0000 +++ storage/innobase/handler/ha_innodb.cc 2012-01-26 22:37:01 +0000 @@ -1458,7 +1458,8 @@ ulonglong current, /*!< in: Current value */ ulonglong increment, /*!< in: increment current by */ ulonglong offset, /*!< in: AUTOINC offset */ - ulonglong max_value) /*!< in: max value for type */ + ulonglong max_value, /*!< in: max value for type */ + ulonglong reserve) /*!< in: how many values to reserve */ { ulonglong next_value; @@ -1473,46 +1474,18 @@ if (max_value <= current) { next_value = max_value; - } else if (offset <= 1) { - /* Offset 0 and 1 are the same, because there must be at - least one node in the system. */ - if (max_value - current <= increment) { - next_value = max_value; - } else { - next_value = current + increment; - } - } else if (max_value > current) { - if (current > offset) { - next_value = ((current - offset) / increment) + 1; - } else { - next_value = ((offset - current) / increment) + 1; - } - - ut_a(increment > 0); + } else if (increment == 1) { + next_value = current + reserve; + } else { + next_value = (current / increment) + reserve; ut_a(next_value > 0); - - /* Check for multiplication overflow. */ - if (increment > (max_value / next_value)) { - - next_value = max_value; - } else { - next_value *= increment; - - ut_a(max_value >= next_value); - - /* Check for overflow. */ - if (max_value - next_value <= offset) { - next_value = max_value; - } else { - next_value += offset; - } - } - } else { + next_value = next_value * increment + offset; + } + /* Check for overflow. */ + if (next_value < current || next_value > max_value) { next_value = max_value; } - ut_a(next_value <= max_value); - return(next_value); } @@ -3669,8 +3642,7 @@ nor the offset, so use a default increment of 1. */ auto_inc = innobase_next_autoinc( - read_auto_inc, 1, 1, col_max_value); - + read_auto_inc, 1, 1, col_max_value, 1); break; } case DB_RECORD_NOT_FOUND: @@ -5177,7 +5149,7 @@ auto_inc = innobase_next_autoinc( auto_inc, - need, offset, col_max_value); + need, offset, col_max_value, 1); err = innobase_set_max_autoinc( auto_inc); @@ -5452,7 +5424,7 @@ need = prebuilt->autoinc_increment; auto_inc = innobase_next_autoinc( - auto_inc, need, offset, col_max_value); + auto_inc, need, offset, col_max_value, 1); error = innobase_set_max_autoinc(auto_inc); } @@ -10056,16 +10028,14 @@ /* With old style AUTOINC locking we only update the table's AUTOINC counter after attempting to insert the row. */ if (innobase_autoinc_lock_mode != AUTOINC_OLD_STYLE_LOCKING) { - ulonglong need; ulonglong current; ulonglong next_value; current = *first_value > col_max_value ? autoinc : *first_value; - need = *nb_reserved_values * increment; - + /* Compute the last value in the interval */ next_value = innobase_next_autoinc( - current, need, offset, col_max_value); + current, increment, offset, col_max_value, *nb_reserved_values); prebuilt->autoinc_last_value = next_value;