Bug #92471 | Adding FK constraints depends on number of rows table has | ||
---|---|---|---|
Submitted: | 18 Sep 2018 6:37 | Modified: | 10 May 2019 12:26 |
Reporter: | Krunal Bauskar | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: InnoDB storage engine | Severity: | S3 (Non-critical) |
Version: | 5.7.23 | OS: | Any |
Assigned to: | CPU Architecture: | Any |
[18 Sep 2018 6:37]
Krunal Bauskar
[18 Sep 2018 6:37]
Krunal Bauskar
script to reproduce bug
Attachment: load.sql (application/sql, text), 1.41 KiB.
[18 Sep 2018 7:20]
MySQL Verification Team
Hello Krunal, Thank you for the report and test case. Verified as described with 5.7.23 build. regards, Umesh
[25 Apr 2019 7:21]
Dmitry Lenev
Posted by developer: Hello! Indeed the problem is repeatable with recent 5.7 version. I have used simplified test case and 5.7.27-git: CREATE TABLE child (id INT NOT NULL); CREATE TABLE parent (id INT NOT NULL PRIMARY KEY); DELIMITER |; CREATE PROCEDURE load_foo_test_data() BEGIN DECLARE v_max INT UNSIGNED DEFAULT 11000; DECLARE v_counter INT UNSIGNED DEFAULT 0; WHILE v_counter < v_max DO INSERT INTO child (id) VALUES (v_counter); SET v_counter = v_counter+1; END WHILE; END | DELIMITER ;| CALL load_foo_test_data(); SELECT COUNT(*) FROM child; # COUNT(*) # 11000 SET FOREIGN_KEY_CHECKS = 0; # The below ALTER incorrectly fails with error 1452 "Cannot add or update # a child row: a foreign key constraint fails (`test`.`#sql-369b_3`, # CONSTRAINT `#sql-369b_3_ibfk_1` FOREIGN KEY (`id`) REFERENCES `parent` # (`id`))". ALTER TABLE child ADD FOREIGN KEY (id) REFERENCES parent (id), ALGORITHM=COPY; However, the problem doesn't occur with recent 8.0 (I have used 8.0.17-git): CREATE TABLE child (id INT NOT NULL); CREATE TABLE parent (id INT NOT NULL PRIMARY KEY); DELIMITER |; CREATE PROCEDURE load_foo_test_data() BEGIN DECLARE v_max INT UNSIGNED DEFAULT 11000; DECLARE v_counter INT UNSIGNED DEFAULT 0; WHILE v_counter < v_max DO INSERT INTO child (id) VALUES (v_counter); SET v_counter = v_counter+1; END WHILE; END | DELIMITER ;| CALL load_foo_test_data(); SELECT COUNT(*) FROM child; # COUNT(*) # 11000 SET FOREIGN_KEY_CHECKS = 0; # The below ALTER succeeds as expected! ALTER TABLE child ADD FOREIGN KEY (id) REFERENCES parent (id), ALGORITHM=COPY; SHOW CREATE TABLE child; # Table Create Table # child CREATE TABLE `child` ( # `id` int(11) NOT NULL, # KEY `id` (`id`), # CONSTRAINT `child_ibfk_1` FOREIGN KEY (`id`) REFERENCES `parent` (`id`) # ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci This is because intermediate commit which is causing problem in 5.7 was removed by the fix for bug #17479594 "AVOID INTERMEDIATE COMMIT WHILE DOING ALTER TABLE ALGORITHM=COPY" in 8.0.0. So I am closing this bug as fixed in 8.0.0. Moving to documenting state to let Documentation Team to decide if anything needs to be added to Release Notes.
[25 Apr 2019 8:06]
Krunal Bauskar
I saw latest comment "So I am closing this bug as fixed in 8.0.0. ...." right, the bug is fixed in 8.x series since the said patch has been worked with different logic but the said bug is still a valid 5.7 bug and since Oracle support 5.7 I presume it is a good idea to resolve the bug unless there is a workaround that justifies why it should not be solved in 5.7.
[10 May 2019 12:26]
Daniel Price
Posted by developer: Fixed as of the 8.0.0 release, and here's the changelog entry: An ALTER TABLE ... ALGORITHM=COPY operation that added a foreign key constraint failed after due to an intermediate commit that occurred after 10000 rows were copied. The intermediate commit reset the foreign key checks flag, causing the operation to fail. The intermediate commit no longer occurs.