Bug #67500 Online DDL create unique index is incorrect
Submitted: 7 Nov 2012 9:25 Modified: 7 Nov 2012 9:34
Reporter: Lixun Peng (OCA) Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S1 (Critical)
Version:5.6 OS:Any
Assigned to: CPU Architecture:Any
Tags: Online Add Index, unique

[7 Nov 2012 9:25] Lixun Peng
Description:
Online add unique index is not incorrect in some cases.

SESSION1:
root@localhost : test 04:26:56> alter table t add unique key b(b);
Query OK, 0 rows affected (3 min 36.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

SESSION2:
root@localhost : test 04:26:45> insert into t (b,c) values (8,repeat('a',16));
Query OK, 1 row affected (38.86 sec)

root@localhost : test 04:28:21> update t set b=4 where a=1;
Query OK, 1 row affected (4.74 sec)
Rows matched: 1  Changed: 1  Warnings: 0

root@localhost : test 04:29:35> show create table t\G
*************************** 1. row ***************************
       Table: t
Create Table: CREATE TABLE `t` (
  `a` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `b` int(11) DEFAULT NULL,
  `c` char(255) DEFAULT NULL,
  PRIMARY KEY (`a`),
  UNIQUE KEY `b` (`b`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8
1 row in set (3.49 sec)

root@localhost : test 04:30:55> select * from t;
+----+------+------------------+
| a  | b    | c                |
+----+------+------------------+
|  1 |    4 | aaaaaaaaaaaaaaaa |
|  2 |    2 | aaaaaaaaaaaaaaaa |
|  3 |    3 | aaaaaaaaaaaaaaaa |
|  4 |    4 | aaaaaaaaaaaaaaaa |
|  5 |    5 | aaaaaaaaaaaaaaaa |
| 6 |    8 | aaaaaaaaaaaaaaaa |
+----+------+------------------+
6 rows in set (0.01 sec)

How to repeat:
CREATE TABLE `t` (
  `a` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `b` int(11) DEFAULT NULL,
  `c` char(255) DEFAULT NULL,
  PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO t (b,c) values (1,1,repeat("a",16)),(2,2,repeat("a",16)),....

SESSION1:
you can set a breakpoint at row_merge_insert_index_tuples() or row_log_apply();
run: alter table t add unique key b(b);
wait at breakpoint, and then run SESSION2;

SESSION2:
insert into t (b,c) values (8,repeat('a',16));update t set b=4 where a=1;

SESSION1;
continue;

and then, you can get the table have a unique index in column "b", but "b" have two value "4", it's duplicated.

Suggested fix:
I think after read all data in cluster index, and before row_log_apply(), InnoDB should do more duplicating check.
[7 Nov 2012 9:34] Marko Mäkelä
I believe that this is a duplicate of Oracle Bug#14630263 CREATE UNIQUE INDEX CREATES CORRUPTED INDEX ON NULLABLE COLUMN which will be fixed in MySQL 5.6.8.