-
-
Notifications
You must be signed in to change notification settings - Fork 2k
MDEV-32688 Add innodb_foreign_key_errors status variable #4631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # | ||
| # MDEV-32688: Test innodb_foreign_key_errors status variable | ||
| # | ||
| # Setup | ||
| CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB; | ||
| CREATE TABLE child ( | ||
| id INT PRIMARY KEY, | ||
| parent_id INT, | ||
| FOREIGN KEY (parent_id) REFERENCES parent(id) | ||
| ) ENGINE=InnoDB; | ||
| INSERT INTO parent VALUES (1), (2), (3); | ||
| INSERT INTO child VALUES (1, 1), (2, 2); | ||
| # Counter starts at 0 | ||
| SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors'; | ||
| Variable_name Value | ||
| Innodb_foreign_key_errors 0 | ||
| # Child-side violations (INSERT, UPDATE, REPLACE, INSERT...SELECT) | ||
| INSERT INTO child VALUES (10, 999); | ||
| ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) | ||
| UPDATE child SET parent_id = 888 WHERE id = 1; | ||
| ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) | ||
| REPLACE INTO child VALUES (30, 777); | ||
| ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) | ||
| INSERT INTO child (id, parent_id) SELECT 40, 555; | ||
| ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) | ||
| # Parent-side violations (DELETE, UPDATE) | ||
| DELETE FROM parent WHERE id = 1; | ||
| ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) | ||
| UPDATE parent SET id = 100 WHERE id = 1; | ||
| ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)) | ||
| # Counter after 6 violations | ||
| SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors'; | ||
| Variable_name Value | ||
| Innodb_foreign_key_errors 6 | ||
| # Successful operations should NOT increment | ||
| INSERT INTO child VALUES (50, 3); | ||
| UPDATE child SET parent_id = 1 WHERE id = 50; | ||
| DELETE FROM child WHERE id = 50; | ||
| INSERT INTO child VALUES (60, NULL); | ||
| DELETE FROM child WHERE id = 60; | ||
| # Counter unchanged after successful operations | ||
| SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors'; | ||
| Variable_name Value | ||
| Innodb_foreign_key_errors 6 | ||
| # FOREIGN_KEY_CHECKS=0 should NOT increment | ||
| SET FOREIGN_KEY_CHECKS = 0; | ||
| INSERT INTO child VALUES (70, 999); | ||
| SET FOREIGN_KEY_CHECKS = 1; | ||
| DELETE FROM child WHERE id = 70; | ||
| # Counter unchanged with FK checks disabled | ||
| SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors'; | ||
| Variable_name Value | ||
| Innodb_foreign_key_errors 6 | ||
| # CASCADE chain failure | ||
| CREATE TABLE grandparent (id INT PRIMARY KEY) ENGINE=InnoDB; | ||
| CREATE TABLE parent_cascade ( | ||
| id INT PRIMARY KEY, | ||
| gp_id INT, | ||
| FOREIGN KEY (gp_id) REFERENCES grandparent(id) ON DELETE RESTRICT | ||
| ) ENGINE=InnoDB; | ||
| CREATE TABLE child_cascade ( | ||
| id INT PRIMARY KEY, | ||
| parent_id INT, | ||
| FOREIGN KEY (parent_id) REFERENCES parent_cascade(id) ON DELETE CASCADE | ||
| ) ENGINE=InnoDB; | ||
| INSERT INTO grandparent VALUES (1); | ||
| INSERT INTO parent_cascade VALUES (1, 1); | ||
| INSERT INTO child_cascade VALUES (1, 1); | ||
| DELETE FROM grandparent WHERE id = 1; | ||
| ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`parent_cascade`, CONSTRAINT `1` FOREIGN KEY (`gp_id`) REFERENCES `grandparent` (`id`)) | ||
| # Counter after CASCADE chain failure | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. say what the value needs to be please. makes it easier to read and check. |
||
| SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors'; | ||
| Variable_name Value | ||
| Innodb_foreign_key_errors 7 | ||
| DROP TABLE child_cascade; | ||
| DROP TABLE parent_cascade; | ||
| DROP TABLE grandparent; | ||
| # Counter resets after restart | ||
| # restart | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test reset too: FLUSH status.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, please, fix the commit message: do not mention the license in it. And remove the "similar to ..." part: it's not relevant. |
||
| SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors'; | ||
| Variable_name Value | ||
| Innodb_foreign_key_errors 0 | ||
| # Cleanup | ||
| DROP TABLE child; | ||
| DROP TABLE parent; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| --source include/have_innodb.inc | ||
|
|
||
| --echo # | ||
| --echo # MDEV-32688: Test innodb_foreign_key_errors status variable | ||
| --echo # | ||
|
|
||
| --echo # Setup | ||
| CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB; | ||
| CREATE TABLE child ( | ||
| id INT PRIMARY KEY, | ||
| parent_id INT, | ||
| FOREIGN KEY (parent_id) REFERENCES parent(id) | ||
| ) ENGINE=InnoDB; | ||
|
|
||
| INSERT INTO parent VALUES (1), (2), (3); | ||
| INSERT INTO child VALUES (1, 1), (2, 2); | ||
|
|
||
| --echo # Counter starts at 0 | ||
| SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors'; | ||
|
|
||
| --echo # Child-side violations (INSERT, UPDATE, REPLACE, INSERT...SELECT) | ||
| --error ER_NO_REFERENCED_ROW_2 | ||
| INSERT INTO child VALUES (10, 999); | ||
|
|
||
| --error ER_NO_REFERENCED_ROW_2 | ||
| UPDATE child SET parent_id = 888 WHERE id = 1; | ||
|
|
||
| --error ER_NO_REFERENCED_ROW_2 | ||
| REPLACE INTO child VALUES (30, 777); | ||
|
|
||
| --error ER_NO_REFERENCED_ROW_2 | ||
| INSERT INTO child (id, parent_id) SELECT 40, 555; | ||
|
|
||
| --echo # Parent-side violations (DELETE, UPDATE) | ||
| --error ER_ROW_IS_REFERENCED_2 | ||
| DELETE FROM parent WHERE id = 1; | ||
|
|
||
| --error ER_ROW_IS_REFERENCED_2 | ||
| UPDATE parent SET id = 100 WHERE id = 1; | ||
|
|
||
| --echo # Counter after 6 violations | ||
| SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors'; | ||
|
|
||
| --echo # Successful operations should NOT increment | ||
| INSERT INTO child VALUES (50, 3); | ||
| UPDATE child SET parent_id = 1 WHERE id = 50; | ||
| DELETE FROM child WHERE id = 50; | ||
| INSERT INTO child VALUES (60, NULL); | ||
| DELETE FROM child WHERE id = 60; | ||
|
|
||
| --echo # Counter unchanged after successful operations | ||
| SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors'; | ||
|
|
||
| --echo # FOREIGN_KEY_CHECKS=0 should NOT increment | ||
| SET FOREIGN_KEY_CHECKS = 0; | ||
| INSERT INTO child VALUES (70, 999); | ||
| SET FOREIGN_KEY_CHECKS = 1; | ||
| DELETE FROM child WHERE id = 70; | ||
|
|
||
| --echo # Counter unchanged with FK checks disabled | ||
| SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors'; | ||
|
|
||
| --echo # CASCADE chain failure | ||
| CREATE TABLE grandparent (id INT PRIMARY KEY) ENGINE=InnoDB; | ||
| CREATE TABLE parent_cascade ( | ||
| id INT PRIMARY KEY, | ||
| gp_id INT, | ||
| FOREIGN KEY (gp_id) REFERENCES grandparent(id) ON DELETE RESTRICT | ||
| ) ENGINE=InnoDB; | ||
| CREATE TABLE child_cascade ( | ||
| id INT PRIMARY KEY, | ||
| parent_id INT, | ||
| FOREIGN KEY (parent_id) REFERENCES parent_cascade(id) ON DELETE CASCADE | ||
| ) ENGINE=InnoDB; | ||
|
|
||
| INSERT INTO grandparent VALUES (1); | ||
| INSERT INTO parent_cascade VALUES (1, 1); | ||
| INSERT INTO child_cascade VALUES (1, 1); | ||
|
|
||
| --error ER_ROW_IS_REFERENCED_2 | ||
| DELETE FROM grandparent WHERE id = 1; | ||
|
|
||
| --echo # Counter after CASCADE chain failure | ||
| SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors'; | ||
|
|
||
| DROP TABLE child_cascade; | ||
| DROP TABLE parent_cascade; | ||
| DROP TABLE grandparent; | ||
|
|
||
| --echo # Counter resets after restart | ||
| --source include/restart_mysqld.inc | ||
|
|
||
| SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors'; | ||
|
|
||
| --echo # Cleanup | ||
| DROP TABLE child; | ||
| DROP TABLE parent; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to ensure that it indeed starts at 0, please make sure to either explicitly reset it (as mtr sometimes uses a running server instance that matches the start parameters for the current tests) or make sure that the test is always run on a freshly started server.
I'd do the former: saves one needless restart and lots of mtr run time.
This is why some of the build bot hosts are failing on your test btw.