[MDEV-16210] FK constraints on versioned tables use historical rows, which may cause constraint violation Created: 2018-05-17  Updated: 2019-10-15  Resolved: 2019-10-09

Status: Closed
Project: MariaDB Server
Component/s: Server, Versioned Tables
Affects Version/s: 10.3
Fix Version/s: 10.3.19, 10.4.9

Type: Bug Priority: Major
Reporter: Elena Stepanova Assignee: Aleksey Midenkov
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Problem/Incident
causes MDEV-20812 Unexpected ER_ROW_IS_REFERENCED_2 or ... Closed
Relates
relates to MDEV-18057 Assertion `(node->state == 5) || (nod... Closed

 Description   

create or replace table t1 (a int, key(a)) with system versioning;
create or replace table t2 (b int, foreign key (b) references t1(a));
insert into t1 values (1),(2);
insert into t2 values (1);
delete from t1 where a = 1;
alter table t1 drop system versioning;

MariaDB [test]> delete from t1 where a = 1;
Query OK, 1 row affected (0.07 sec)
 
MariaDB [test]> select * from t1;
+------+
| a    |
+------+
|    2 |
+------+
1 row in set (0.00 sec)
 
MariaDB [test]> select * from t2;
+------+
| b    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)
 
MariaDB [test]> select * from t1 for system_time all;
+------+
| a    |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)

At this point the result is already questionable – I would think that foreign key constraints shouldn't take into account historical rows; but maybe it's meant to be so by design or by standard. The following, however, is most certainly a problem:

MariaDB [test]> alter table t1 drop system versioning;
Query OK, 1 row affected (1.29 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
MariaDB [test]> select * from t1;
+------+
| a    |
+------+
|    2 |
+------+
1 row in set (0.00 sec)
 
MariaDB [test]> select * from t2;
+------+
| b    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

So, now we have a non-versioned table t1 and a broken constraint on t2.



 Comments   
Comment by Aleksey Midenkov [ 2018-05-30 ]

Analysis

Comment by Marko Mäkelä [ 2019-09-05 ]

OK to push.

Comment by Marko Mäkelä [ 2019-09-23 ]

The follow-up change looks OK (but will have to be rebased).

Generated at Thu Feb 08 08:27:12 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.