Skip to content
/ server Public

MDEV-32688 Add innodb_foreign_key_errors status variable#4631

Open
y2kwak wants to merge 1 commit intoMariaDB:mainfrom
y2kwak:MDEV-32688
Open

MDEV-32688 Add innodb_foreign_key_errors status variable#4631
y2kwak wants to merge 1 commit intoMariaDB:mainfrom
y2kwak:MDEV-32688

Conversation

@y2kwak
Copy link
Contributor

@y2kwak y2kwak commented Feb 6, 2026

Description

Add a new status variable innodb_foreign_key_errors that tracks the total number of foreign key constraint violations in InnoDB, similar to innodb_deadlocks.

The counter increments for both child-side violations and parent-side violations. Counter resets to 0 on server restart.

Added test coverage in mysql-test/suite/innodb/t/foreign_key_errors.test.

How can this PR be tested?

Run the MTR test foreign_key_error.test

It can also be tested manually.

  • Count starts at 0
MariaDB [(none)]> SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors';
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| Innodb_foreign_key_errors | 0     |
+---------------------------+-------+
1 row in set (0.001 sec)
  • Count increments after error
MariaDB [testdb]> INSERT INTO child VALUES (1, 999);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`child`, CONSTRAINT `1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`))

MariaDB [testdb]> SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors';
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| Innodb_foreign_key_errors | 1     |
+---------------------------+-------+
1 row in set (0.000 sec)

MariaDB [testdb]> UPDATE child SET parent_id = 999 WHERE id = 1;
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`child`, CONSTRAINT `1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`))

MariaDB [testdb]> SHOW GLOBAL STATUS LIKE 'innodb_foreign_key_errors';
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| Innodb_foreign_key_errors | 2     |
+---------------------------+-------+
1 row in set (0.000 sec)

Basing the PR against the correct MariaDB version

  •  This is a new feature and the PR is based against the latest MariaDB development branch.

Copyright

All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services, Inc.

Add a new status variable that tracks the total number of foreign key
constraint violations in InnoDB, similar to innodb_deadlocks.

The counter increments for both child-side violations (INSERT/UPDATE with
non-existent parent) and parent-side violations (DELETE/UPDATE parent with
existing children). Counter resets to 0 on server restart.

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Feb 9, 2026
@gkodinov gkodinov self-assigned this Feb 12, 2026
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';
Copy link
Member

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.

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
Copy link
Member

Choose a reason for hiding this comment

The 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.

DROP TABLE parent_cascade;
DROP TABLE grandparent;
# Counter resets after restart
# restart
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test reset too: FLUSH status.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

3 participants