Bug #23520 Duplicate key error on INSERT with SBR with null for AUTO_INCREMENT column
Submitted: 20 Oct 2006 23:28 Modified: 30 Oct 2006 9:30
Reporter: Todd Farmer (OCA) Email Updates:
Status: Won't fix Impact on me:
None 
Category:MySQL Cluster: Cluster (NDB) storage engine Severity:S2 (Serious)
Version:5.1.11 OS:Linux (Linux)
Assigned to: CPU Architecture:Any
Tags: auto_increment, cluster, duplicate key, ndb, replication

[20 Oct 2006 23:28] Todd Farmer
Description:
Replication errors are generated on the slave when an INSERT is executed on the master that uses NULL for an auto_increment column and the target table on the slave is NDB.  The slave error is:

Last_Error: Error 'Can't write; duplicate key in table 'null_inserts_1'' on query. Default database: 'test'. Query: 'INSERT INTO null_inserts_1 SELECT null FROM null_inserts_1'

Note that taking the INSERT statement out of the error message and executing it directly against the slave does not result in errors:

mysql> INSERT INTO null_inserts_1 SELECT null FROM null_inserts_1;
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

However, the values that are inserted show gaps in the auto_increment column value:

mysql> SELECT * FROM null_inserts_1 ORDER BY 1;
+----+
| i  |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
| 36 |
| 37 |
| 38 |
| 39 |
+----+
8 rows in set (0.00 sec)

 

How to repeat:
Execute in master:

CREATE TABLE null_inserts_1 (i INT AUTO_INCREMENT PRIMARY KEY) ENGINE = MYISAM;
INSERT INTO null_inserts_1 VALUES (null); # inserts 1
INSERT INTO null_inserts_1 SELECT null FROM null_inserts_1; # inserts 2
INSERT INTO null_inserts_1 SELECT null FROM null_inserts_1; # inserts 3 and 4

Execute in slave:
SELECT * FROM null_inserts_1;  # 1,2,3,4

ALTER TABLE null_inserts_1 ENGINE = NDBCLUSTER;

SELECT * FROM null_inserts_1;  # 1,2,3,4

Execute in master:

INSERT INTO null_inserts_1 SELECT null FROM null_inserts_1; # inserts 5,6,7,8
INSERT INTO null_inserts_1 SELECT null FROM null_inserts_1; # inserts 9-16

SELECT * FROM null_inserts_1;  # 1-16

Execut in slave:

SELECT * FROM null_inserts_1;  # 1,2,3,4

SHOW SLAVE STATUS\G # Slave thread error on duplicate key

INSERT INTO null_inserts_1 SELECT null FROM null_inserts_1; # executes successfully, but with auto-increment value gaps

Suggested fix:
Correct handling of insert of NULL values in AUTO_INCREMENT columns when using statement-based replication.
[30 Oct 2006 9:30] Martin Skold
There are no plans to support SBR for cluster since
it does not cater for changes originating from different
MySQL servers (not only the one with binlog running) and
also not from application connected directly through the
NDB API.