-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Order foreign keys DML operations to get correct rows affected #15779
Conversation
Signed-off-by: Manan Gupta <[email protected]>
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #15779 +/- ##
==========================================
+ Coverage 68.41% 68.42% +0.01%
==========================================
Files 1558 1559 +1
Lines 196353 196514 +161
==========================================
+ Hits 134337 134468 +131
- Misses 62016 62046 +30 ☔ View full report in Codecov by Sentry. |
Signed-off-by: Manan Gupta <[email protected]>
Signed-off-by: Manan Gupta <[email protected]>
…n back to original package Signed-off-by: Manan Gupta <[email protected]>
Signed-off-by: Manan Gupta <[email protected]>
It was noticed that rearranging order of deletes and updates is actually incorrect and can lead to different results. Here is an example for update reordering - /*
* fk_multicol_t15
* │
* │
* On Delete Cascade │
* On Update Cascade │
* │
* ▼
* fk_multicol_t16
* │
* On Delete Set Null │
* On Update Set Null │
* │
* ▼
* fk_multicol_t17──────────────────┐
* │ │
* │ │ On Delete Set Null
* On Delete Cascade │ │ On Update Set Null
* On Update Cascade │ │
* │ │
* ▼ ▼
* fk_multicol_t18 fk_multicol_t19
*/
mysql [localhost:8032] {msandbox} (fk_test) > insert into fk_multicol_t15(id, cola, colb) values (1, 7, 1), (2, 9, 1), (3, 12, 1);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql [localhost:8032] {msandbox} (fk_test) > insert into fk_multicol_t16(id, cola, colb) values (1, 7, 1), (2, 9, 1), (3, 12, 1);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql [localhost:8032] {msandbox} (fk_test) > insert into fk_multicol_t17(id, cola, colb) values (1, 7, 1), (2, 9, 1);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql [localhost:8032] {msandbox} (fk_test) > insert into fk_multicol_t19(id, cola, colb) values (1, 7, 1);
Query OK, 1 row affected (0.01 sec)
mysql [localhost:8032] {msandbox} (fk_test) > update fk_multicol_t17 join fk_multicol_t15 m1 on m1.id = fk_multicol_t17.id set m1.cola = m1.id + 8, fk_multicol_t17.colb = 32 where m1.id < 3;
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`fk_test`.`fk_multicol_t17`, CONSTRAINT `fk_multicol_t17_ibfk_1` FOREIGN KEY (`cola`, `colb`) REFERENCES `fk_multicol_t16` (`cola`, `colb`) ON DELETE SET NULL ON UPDATE SET NULL)
mysql [localhost:8032] {msandbox} (fk_test) > update fk_multicol_t15 m1 join fk_multicol_t17 on m1.id = fk_multicol_t17.id set m1.cola = m1.id + 8, fk_multicol_t17.colb = 32 where m1.id < 3;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0 |
Similarly, here is an example where reordering deletes leads to different results - create table fk_t17
(
id bigint,
col varchar(10),
primary key (id),
index(col)
) Engine = InnoDB;
create table fk_t18
(
id bigint,
col varchar(10),
primary key (id),
index(col),
foreign key (col) references fk_t17(col) on delete set null on update set null
) Engine = InnoDB;
create table fk_t19
(
id bigint,
col varchar(10),
primary key (id),
index(col),
foreign key (col) references fk_t18(col) on delete restrict on update cascade
) Engine = InnoDB;
mysql [localhost:8032] {msandbox} (temp) > insert into fk_t17 values (1, 1);
Query OK, 1 row affected (0.01 sec)
mysql [localhost:8032] {msandbox} (temp) > insert into fk_t18 values (1, 1);
Query OK, 1 row affected (0.00 sec)
mysql [localhost:8032] {msandbox} (temp) > insert into fk_t19 values (1, 1);
Query OK, 1 row affected (0.00 sec)
mysql [localhost:8032] {msandbox} (temp) > delete fk_t18, fk_t17 from fk_t18 join fk_t17 using (id) where fk_t18.id = 1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`temp`.`fk_t19`, CONSTRAINT `fk_t19_ibfk_1` FOREIGN KEY (`col`) REFERENCES `fk_t18` (`col`) ON DELETE RESTRICT ON UPDATE CASCADE)
mysql [localhost:8032] {msandbox} (temp) > delete fk_t17, fk_t18 from fk_t17 join fk_t18 using (id) where fk_t18.id = 1;
Query OK, 2 rows affected (0.00 sec) |
Closing the PR because of the issues pointed ⬆️ |
Description
This PR fixes the bug described in #15826.
As described in #15826 (comment), the problem arises from the ordering of dml operations such that we end up deleting from the parent table first, causing an unintended delete on the child table.
Whats interesting to note is that if the original query has its tables switched around, then the rows affected are correct since we delete from the child first and then the parent! So the query
delete fk_t11, fk_t12 from fk_t11 join fk_t12 using (id) where fk_t11.id = 5
fails with incorrect rows affected, butdelete fk_t12, fk_t11 from fk_t12 join fk_t11 using (id) where fk_t11.id = 5
works correctly.This PR fixes this issue. We realized after some discussion that the problem can be fixed if we are able to order the dml operations such that the delete on the child happens before the parent table.
After some more thinking and experimenting, I realized we are already building a graph when we update the foreign keys information to check for cycles in foreign keys. We can use topological sorting (https://en.wikipedia.org/wiki/Topological_sorting) on the graph to find an ordering of the tables in the keyspace such that all CASCADE foreign keys have parent tables coming before child tables.
Once we have this sorting, we store the order of the tables in the Vschema as part of the Table information in the newly introduced
FkOrder
field.We can then use this field to order the dml operations for foreign key tables and ensure that the delete operations happen on the child table first. This ensures that the rows affected are calculated properly.
Related Issue(s)
Checklist
Deployment Notes