Commit 3bfe3b5e authored by Michael Widenius's avatar Michael Widenius

Bug fix for MySQL bug#61209 "auto_increment_offset != 1 +...

Bug fix for MySQL bug#61209 "auto_increment_offset != 1 + innodb_autoinc_lock_mode=1 => bulk inserts fail"
Patch and test case by Patryk Pomykalski

mysql-test/suite/innodb/r/innodb-autoinc.result:
  updated test case
storage/innodb_plugin/handler/ha_innodb.cc:
  Fixed that we properly reserve values for auto_increment
storage/xtradb/handler/ha_innodb.cc:
  Fixed that we properly reserve values for auto_increment
parent 4e96c579
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
Warnings:
Warning 1265 Data truncated for column 'a' at row 3
# Connection con1 reap
RELEASE_LOCK('a')
1
DROP TABLE bug_61209;
......@@ -366,7 +366,7 @@ c1
310
400
1000
1010
1110
DROP TABLE t1;
SET @@SESSION.AUTO_INCREMENT_INCREMENT=1, @@SESSION.AUTO_INCREMENT_OFFSET=1;
SET @@INSERT_ID=1;
......@@ -648,7 +648,7 @@ t2 CREATE TABLE `t2` (
`n` int(10) unsigned NOT NULL,
`o` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`m`)
) ENGINE=InnoDB AUTO_INCREMENT=15 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
......@@ -657,7 +657,7 @@ t1 CREATE TABLE `t1` (
`b` int(10) unsigned NOT NULL,
`c` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1
INSERT INTO t1 (b,c) SELECT n,o FROM t2 ;
SELECT * FROM t1;
a b c
......@@ -671,16 +671,16 @@ a b c
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
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` (
......@@ -688,7 +688,7 @@ t1 CREATE TABLE `t1` (
`b` int(10) unsigned NOT NULL,
`c` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1
) 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
......@@ -702,21 +702,21 @@ a b c
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
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` (
......@@ -724,7 +724,7 @@ t1 CREATE TABLE `t1` (
`b` int(10) unsigned NOT NULL,
`c` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1
) 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
......@@ -738,26 +738,26 @@ a b c
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
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` (
......@@ -765,7 +765,7 @@ t1 CREATE TABLE `t1` (
`b` int(10) unsigned NOT NULL,
`c` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=latin1
) 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
......@@ -774,7 +774,7 @@ t1 CREATE TABLE `t1` (
`b` int(10) unsigned NOT NULL,
`c` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=latin1
) 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
......@@ -783,7 +783,7 @@ t1 CREATE TABLE `t1` (
`b` int(10) unsigned NOT NULL,
`c` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=latin1
) 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
......@@ -797,41 +797,41 @@ a b c
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
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` (
......@@ -839,7 +839,7 @@ t1 CREATE TABLE `t1` (
`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=66 DEFAULT CHARSET=latin1
DROP TABLE t1;
DROP TABLE t2;
DROP TABLE IF EXISTS t1;
......
# 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;
......@@ -366,7 +366,7 @@ c1
310
400
1000
1010
1110
DROP TABLE t1;
SET @@SESSION.AUTO_INCREMENT_INCREMENT=1, @@SESSION.AUTO_INCREMENT_OFFSET=1;
SET @@INSERT_ID=1;
......@@ -648,7 +648,7 @@ t2 CREATE TABLE `t2` (
`n` int(10) unsigned NOT NULL,
`o` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`m`)
) ENGINE=InnoDB AUTO_INCREMENT=15 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
......@@ -657,7 +657,7 @@ t1 CREATE TABLE `t1` (
`b` int(10) unsigned NOT NULL,
`c` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1
INSERT INTO t1 (b,c) SELECT n,o FROM t2 ;
SELECT * FROM t1;
a b c
......@@ -671,16 +671,16 @@ a b c
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
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` (
......@@ -688,7 +688,7 @@ t1 CREATE TABLE `t1` (
`b` int(10) unsigned NOT NULL,
`c` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1
) 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
......@@ -702,21 +702,21 @@ a b c
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
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` (
......@@ -724,7 +724,7 @@ t1 CREATE TABLE `t1` (
`b` int(10) unsigned NOT NULL,
`c` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1
) 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
......@@ -738,26 +738,26 @@ a b c
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
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` (
......@@ -765,7 +765,7 @@ t1 CREATE TABLE `t1` (
`b` int(10) unsigned NOT NULL,
`c` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=latin1
) 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
......@@ -774,7 +774,7 @@ t1 CREATE TABLE `t1` (
`b` int(10) unsigned NOT NULL,
`c` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=latin1
) 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
......@@ -783,7 +783,7 @@ t1 CREATE TABLE `t1` (
`b` int(10) unsigned NOT NULL,
`c` enum('FALSE','TRUE') DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=latin1
) 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
......@@ -797,41 +797,41 @@ a b c
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
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` (
......@@ -839,7 +839,7 @@ t1 CREATE TABLE `t1` (
`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=66 DEFAULT CHARSET=latin1
DROP TABLE t1;
DROP TABLE t2;
DROP TABLE IF EXISTS t1;
......
......@@ -1296,7 +1296,8 @@ innobase_next_autoinc(
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;
......@@ -1305,51 +1306,16 @@ innobase_next_autoinc(
/* According to MySQL documentation, if the offset is greater than
the increment then the offset is ignored. */
if (offset > increment) {
if (offset >= increment)
offset = 0;
}
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);
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 = max_value;
}
ut_a(next_value <= max_value);
if (max_value <= current)
return max_value;
next_value = (current / increment) + reserve;
next_value = next_value * increment + offset;
/* Check for overflow. */
if (next_value < current || next_value > max_value)
next_value = max_value;
return(next_value);
}
......@@ -3470,8 +3436,7 @@ ha_innobase::innobase_initialize_autoinc()
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:
......@@ -4885,7 +4850,7 @@ set_max_autoinc:
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);
......@@ -5144,7 +5109,7 @@ ha_innobase::update_row(
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);
}
......@@ -9632,16 +9597,14 @@ ha_innobase::get_auto_increment(
/* 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;
......
......@@ -1367,7 +1367,8 @@ innobase_next_autoinc(
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;
......@@ -1376,51 +1377,16 @@ innobase_next_autoinc(
/* According to MySQL documentation, if the offset is greater than
the increment then the offset is ignored. */
if (offset > increment) {
if (offset >= increment)
offset = 0;
}
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);
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 = max_value;
}
ut_a(next_value <= max_value);
if (max_value <= current)
return max_value;
next_value = (current / increment) + reserve;
next_value = next_value * increment + offset;
/* Check for overflow. */
if (next_value < current || next_value > max_value)
next_value = max_value;
return(next_value);
}
......@@ -3853,8 +3819,7 @@ ha_innobase::innobase_initialize_autoinc()
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:
......@@ -5537,7 +5502,7 @@ set_max_autoinc:
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);
......@@ -5809,7 +5774,7 @@ ha_innobase::update_row(
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);
}
......@@ -10455,16 +10420,14 @@ ha_innobase::get_auto_increment(
/* 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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment