Skip to content

Commit 1b45e05

Browse files
committed
MDEV-21555 Assertion secondary index is out of sync on delete from versioned table
Delete-marked record is on the secondary index and the clustered index already purged the corresponding record. We cannot detect if such record is historical and we should not: the algorithm of row_ins_check_foreign_constraint() skips such record anyway.
1 parent dc3a350 commit 1b45e05

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

mysql-test/suite/versioning/r/foreign.result

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,16 @@ pk f1 f2 left(f3, 4) check_row_ts(row_start, row_end)
443443
1 8 8 SHOR HISTORICAL ROW
444444
2 8 8 LONG HISTORICAL ROW
445445
drop table t1;
446+
#
447+
# MDEV-21555 Assertion secondary index is out of sync on delete from versioned table
448+
#
449+
create table t1 (a int, b int as (a + 1) virtual, key(a)) engine=innodb with system versioning;
450+
set foreign_key_checks= off;
451+
insert into t1 (a) values (1), (2);
452+
alter table t1 add foreign key (b) references t1 (a), algorithm=copy;
453+
update t1 set a= null where a = 1;
454+
delete from t1 where a is null;
455+
set foreign_key_checks= on;
456+
delete history from t1;
457+
delete from t1;
458+
drop table t1;

mysql-test/suite/versioning/t/foreign.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,4 +476,22 @@ select pk, f1, f2, left(f3, 4), check_row_ts(row_start, row_end) from t1 for sys
476476
# cleanup
477477
drop table t1;
478478

479+
--echo #
480+
--echo # MDEV-21555 Assertion secondary index is out of sync on delete from versioned table
481+
--echo #
482+
create table t1 (a int, b int as (a + 1) virtual, key(a)) engine=innodb with system versioning;
483+
484+
set foreign_key_checks= off;
485+
insert into t1 (a) values (1), (2);
486+
alter table t1 add foreign key (b) references t1 (a), algorithm=copy;
487+
update t1 set a= null where a = 1;
488+
delete from t1 where a is null;
489+
set foreign_key_checks= on;
490+
491+
delete history from t1;
492+
delete from t1;
493+
494+
# cleanup
495+
drop table t1;
496+
479497
--source suite/versioning/common_finish.inc

storage/innobase/row/row0ins.cc

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,23 +1666,6 @@ row_ins_check_foreign_constraint(
16661666
cmp = cmp_dtuple_rec(entry, rec, offsets);
16671667

16681668
if (cmp == 0) {
1669-
if (check_table->versioned()) {
1670-
bool history_row = false;
1671-
1672-
if (check_index->is_primary()) {
1673-
history_row = check_index->
1674-
vers_history_row(rec, offsets);
1675-
} else if (check_index->
1676-
vers_history_row(rec, history_row))
1677-
{
1678-
break;
1679-
}
1680-
1681-
if (history_row) {
1682-
continue;
1683-
}
1684-
}
1685-
16861669
if (rec_get_deleted_flag(rec,
16871670
rec_offs_comp(offsets))) {
16881671
/* In delete-marked records, DB_TRX_ID must
@@ -1704,6 +1687,23 @@ row_ins_check_foreign_constraint(
17041687
goto end_scan;
17051688
}
17061689
} else {
1690+
if (check_table->versioned()) {
1691+
bool history_row = false;
1692+
1693+
if (check_index->is_primary()) {
1694+
history_row = check_index->
1695+
vers_history_row(rec,
1696+
offsets);
1697+
} else if (check_index->
1698+
vers_history_row(rec,
1699+
history_row)) {
1700+
break;
1701+
}
1702+
1703+
if (history_row) {
1704+
continue;
1705+
}
1706+
}
17071707
/* Found a matching record. Lock only
17081708
a record because we can allow inserts
17091709
into gaps */

0 commit comments

Comments
 (0)