[MDEV-776] LP:314570 - update is not changing internal auto increment value Created: 2010-06-02 Updated: 2013-03-27 Resolved: 2013-03-27 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Trivial |
| Reporter: | Aarti Pai (Inactive) | Assignee: | Unassigned |
| Resolution: | Not a Bug | Votes: | 0 |
| Labels: | Launchpad, upstream | ||
| Attachments: |
|
| Description |
|
create table t1 (a int not null auto_increment primary key, val int); leads to: drizzletest: At line 307: query 'insert into t1 (val) values (1)' failed: 1062: Duplicate entry '2' for key 'PRIMARY' For InnDB this fails, for MyISAM, it passes as MyISAM updates the auto increment value. |
| Comments |
| Comment by Aarti Pai (Inactive) [ 2009-01-07 ] | ||||||||||||||||||||
|
update is not changing internal auto increment value drizzletest: At line 307: query 'insert into t1 (val) values (1)' failed: 1062: Duplicate entry '2' for key 'PRIMARY' The result from queries just before the failure was: | ||||||||||||||||||||
| Comment by Rasmus Johansson (Inactive) [ 2009-01-28 ] | ||||||||||||||||||||
|
Re: update is not changing internal auto increment value jpipes@serialcoder:~/repos/drizzle/new-temporal/tests$ ../client/drizzle --user=root --port=9306 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. drizzle> use test drizzle> insert into t1 (val) values (1); drizzle> update t1 set a=2 where a=1; drizzle> insert into t1 (val) values (1); drizzle> create table t1 (a int not null auto_increment primary key, val int)engine=myisam; drizzle> insert into t1 (val) values (1); drizzle> update t1 set a=2 where a=1; drizzle> insert into t1 (val) values (1); | ||||||||||||||||||||
| Comment by Rasmus Johansson (Inactive) [ 2009-01-28 ] | ||||||||||||||||||||
|
Re: update is not changing internal auto increment value | ||||||||||||||||||||
| Comment by Rasmus Johansson (Inactive) [ 2009-04-03 ] | ||||||||||||||||||||
|
Re: update is not changing internal auto increment value The insert query succeeds on the second attempt (after the failure) as seen below. --------- drizzle> use test drizzle> create table t1 (a int not null auto_increment primary key, val int) engine=innodb; drizzle> insert into t1 (val) values (1); drizzle> update t1 set a=2 where a=1; drizzle> insert into t1 (val) values (1); drizzle> insert into t1 (val) values (1); drizzle> select * from t1;
--
-- | ||||||||||||||||||||
| Comment by Rasmus Johansson (Inactive) [ 2009-04-17 ] | ||||||||||||||||||||
|
Re: update is not changing internal auto increment value This looks more serious than I thought, and I've confirmed that this bug is in MySQL as well. As suspected, InnoDB's internal counter isn't updated when we update a row. So the next time we call INSERT, it fails because dict_table_struct's autoinc value isn't updated. For this particular case, the INSERT statement after the failure works because the autoinc counter was updated when ha_innobase::get_auto_increment was called in the failed statement. The next INSERT statement works because the autoinc counter is updated. This SEEMS to have done the correct thing because we only updated the autoinc column by 1 in the bug report but imagine a case where the column is updated by something larger than 1 like the following: -------------- drizzle> insert into t1 (val) values (1); drizzle> update t1 set a=100 where a=1; drizzle> select * from t1;
----
---- drizzle> insert into t1 (val) values (1); drizzle> select * from t1;
----
---- What's wrong in the above output is that the next inserted row should have a autoinc value, larger than 100 (relative to the increment and offset value). This works fine on MyISAM: drizzle> create table t2 (a int not null auto_increment primary key, val int)engine=myisam; drizzle> insert into t2 (val) values (1); drizzle> update t2 set a=100 where a=1; drizzle> select * from t2;
----
---- drizzle> insert into t2 (val) values (1); drizzle> select * from t2;
----
---- I haven't come up with a way to fix this yet but sharing my findings so far. Cheers, | ||||||||||||||||||||
| Comment by Stewart Smith [ 2009-07-21 ] | ||||||||||||||||||||
|
Re: update is not changing internal auto increment value | ||||||||||||||||||||
| Comment by Stewart Smith [ 2010-06-02 ] | ||||||||||||||||||||
|
Re: update is not changing internal auto increment value | ||||||||||||||||||||
| Comment by Stewart Smith [ 2010-06-02 ] | ||||||||||||||||||||
|
Patch that mimicks the MyISAM behaviour in doUpdateRecord() for innobase plugin. | ||||||||||||||||||||
| Comment by Stewart Smith [ 2011-06-07 ] | ||||||||||||||||||||
|
Re: update is not changing internal auto increment value Thanks, | ||||||||||||||||||||
| Comment by Rasmus Johansson (Inactive) [ 2011-06-07 ] | ||||||||||||||||||||
|
Launchpad bug id: 314570 | ||||||||||||||||||||
| Comment by Elena Stepanova [ 2013-03-27 ] | ||||||||||||||||||||
|
It's now documented as a feature in MySQL manual (e.g. http://dev.mysql.com/doc/refman/5.5/en/example-auto-increment.html): For InnoDB tables, be careful if you modify the column containing the auto-increment value in the middle of a sequence of INSERT statements. For example, if you use an UPDATE statement to put a new, larger value in the auto-increment column, a subsequent INSERT could encounter a “Duplicate entry” error. The test whether an auto-increment value is already present occurs if you do a DELETE followed by more INSERT statements, or when you COMMIT the transaction, but not after an UPDATE statement. |