1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-01 03:47:19 +03:00

MDEV-25644 UPDATE not working properly on transaction precise system versioned table

First UPDATE under START TRANSACTION does nothing (nstate= nstate),
but anyway generates history. Since update vector is empty we get into
(!uvect->n_fields) branch which only adds history row, but does not do
update. After that we get current row with wrong (old) row_start value
and because of that second UPDATE tries to insert history row again
because it sees trx->id != row_start which is the guard to avoid
inserting multiple trx_id-based history rows under same transaction
(because we have same trx_id and we get duplicate error and this bug
demostrates that). But this try anyway fails because PK is based on
row_end which is constant under same transaction, so PK didn't change.

The fix moves vers_make_update() to an earlier stage of
calc_row_difference(). Therefore it prepares update vector before
(!uvect->n_fields) check and never gets into that branch, hence no
need to handle versioning inside that condition anymore.

Now trx->id and row_start are equal after first UPDATE and we don't
try to insert second history row.

== Cleanups and improvements ==

ha_innobase::update_row():

vers_set_fields and vers_ins_row are cleaned up into direct condition
check. SQLCOM_ALTER_TABLE check now is not used as this is dead code,
assertion is done instead.

upd_node->is_delete is set in calc_row_difference() just to keep
versioning code as much in one place as possible. vers_make_delete()
is still located in row_update_for_mysql() as this is required for
ha_innodbase::delete_row() as well.

row_ins_duplicate_error_in_clust():

Restrict DB_FOREIGN_DUPLICATE_KEY to the better conditions.
VERSIONED_DELETE is used specifically to help lower stack to
understand what caused current insert. Related to MDEV-29813.
This commit is contained in:
Aleksey Midenkov
2023-07-20 14:14:00 +03:00
parent 21a8d2c313
commit 14cc7e7d6e
10 changed files with 160 additions and 60 deletions

View File

@ -133,18 +133,38 @@ drop table t1;
#
# MDEV-21138 Assertion `col->ord_part' or `f.col->ord_part' failed in row_build_index_entry_low
#
# Check DELETE and multi-DELETE with foreign key
create table t1 (
f1 int, f2 text, f3 int, fulltext (f2), key(f1), key(f3),
foreign key r (f3) references t1 (f1) on delete set null)
foreign key r (f3) references t1 (f1) on delete set null,
row_start SYS_TYPE as row start invisible,
row_end SYS_TYPE as row end invisible,
period for system_time (row_start, row_end))
with system versioning engine innodb;
insert into t1 values (1, repeat('a', 8193), 1), (1, repeat('b', 8193), 1);
select f1, f3, check_row_ts(row_start, row_end) from t1;
f1 f3 check_row_ts(row_start, row_end)
insert into t1 select 2, f2, 2 from t1;
select f1, f3, check_row(row_start, row_end) from t1;
f1 f3 check_row(row_start, row_end)
1 1 CURRENT ROW
1 1 CURRENT ROW
delete from t1;
select f1, f3, check_row_ts(row_start, row_end) from t1 for system_time all;
f1 f3 check_row_ts(row_start, row_end)
2 2 CURRENT ROW
2 2 CURRENT ROW
delete from t1 where f1 = 1;
select f1, f3, check_row(row_start, row_end) from t1 for system_time all order by f1, row_end;
f1 f3 check_row(row_start, row_end)
1 1 HISTORICAL ROW
1 1 HISTORICAL ROW
drop table t1;
2 2 CURRENT ROW
2 2 CURRENT ROW
create table t2 (f1 int);
insert into t2 values (2);
# Multi-delelte
delete t1, t2 from t1 join t2 where t1.f1 = t2.f1;
select f1, f3, check_row(row_start, row_end) from t1 for system_time all order by f1, row_end;
f1 f3 check_row(row_start, row_end)
1 1 HISTORICAL ROW
1 1 HISTORICAL ROW
2 2 HISTORICAL ROW
2 2 HISTORICAL ROW
# Cleanup
drop tables t1, t2;

View File

@ -1,6 +1,6 @@
--- update.result 2018-12-19 13:55:35.873917389 +0300
+++ update,trx_id.reject 2018-12-19 13:55:35.533917399 +0300
@@ -81,12 +81,10 @@
--- update.result
+++ update.reject
@@ -84,12 +84,10 @@
commit;
select x, y, sys_trx_end = MAXVAL as current from t1 for system_time all order by sys_trx_end, x, y;
x y current
@ -14,3 +14,11 @@
1 1 1
2 2 1
3 3 1
@@ -464,7 +462,6 @@
select nid, nstate, check_row(row_start, row_end) from t1 for system_time all order by row_start, row_end;
nid nstate check_row(row_start, row_end)
1 1 HISTORICAL ROW
-1 1 HISTORICAL ROW
1 3 CURRENT ROW
commit;
drop tables t1;

View File

@ -51,19 +51,22 @@ sys_trx_start SYS_DATATYPE as row start invisible,
sys_trx_end SYS_DATATYPE as row end invisible,
period for system_time (sys_trx_start, sys_trx_end))
with system versioning;
set timestamp= unix_timestamp('2000-01-01 00:00:00');
insert into t1 values(1, 1, 1);
set @ins_t= now(6);
select sys_trx_start into @tmp1 from t1;
set timestamp= unix_timestamp('2000-01-01 01:00:00');
update t1 set x= 11, y= 11 where id = 1;
select @tmp1 < sys_trx_start as A1, x, y from t1;
A1 x y
1 11 11
select sys_trx_start into @tmp1 from t1;
set timestamp= unix_timestamp('2000-01-01 02:00:00');
update t1 set y= 1 where id = 1;
select @tmp1 = sys_trx_start as A2, x from t1;
A2 x
1 11
drop table t1;
set timestamp= default;
create table t1 (
x int,
y int,
@ -451,4 +454,32 @@ select x, y, check_row_ts(row_start, row_end) from t1 for system_time all order
x y check_row_ts(row_start, row_end)
1 3 CURRENT ROW
drop table t1;
#
# MDEV-25644 UPDATE not working properly on transaction precise system versioned table
#
create or replace table t1 (nid int primary key, nstate int, ntype int) engine innodb;
alter table t1 add
row_start SYS_DATATYPE generated always as row start invisible,
add row_end SYS_DATATYPE generated always as row end invisible,
add period for system_time(row_start, row_end),
add system versioning;
insert into t1 values (1, 1, 1);
select nid, nstate, check_row(row_start, row_end) from t1 for system_time all order by row_start, row_end;
nid nstate check_row(row_start, row_end)
1 1 CURRENT ROW
start transaction;
update t1 set nstate= nstate where nid = 1;
select nid, nstate, check_row(row_start, row_end) from t1 for system_time all order by row_start, row_end;
nid nstate check_row(row_start, row_end)
1 1 HISTORICAL ROW
1 1 CURRENT ROW
# Bug: ERROR 1761 (23000): Foreign key constraint for table 'xxx', record '1-18446744073709551615' would lead to a duplicate entry in table 'xxx', key 'PRIMARY'
update t1 set nstate= 3 where nid= 1;
select nid, nstate, check_row(row_start, row_end) from t1 for system_time all order by row_start, row_end;
nid nstate check_row(row_start, row_end)
1 1 HISTORICAL ROW
1 1 HISTORICAL ROW
1 3 CURRENT ROW
commit;
drop tables t1;
# End of 10.4 tests

View File

@ -97,16 +97,26 @@ drop table t1;
--echo #
--echo # MDEV-21138 Assertion `col->ord_part' or `f.col->ord_part' failed in row_build_index_entry_low
--echo #
create table t1 (
--echo # Check DELETE and multi-DELETE with foreign key
replace_result $sys_datatype_expl SYS_TYPE;
eval create table t1 (
f1 int, f2 text, f3 int, fulltext (f2), key(f1), key(f3),
foreign key r (f3) references t1 (f1) on delete set null)
foreign key r (f3) references t1 (f1) on delete set null,
row_start $sys_datatype_expl as row start invisible,
row_end $sys_datatype_expl as row end invisible,
period for system_time (row_start, row_end))
with system versioning engine innodb;
insert into t1 values (1, repeat('a', 8193), 1), (1, repeat('b', 8193), 1);
select f1, f3, check_row_ts(row_start, row_end) from t1;
delete from t1;
select f1, f3, check_row_ts(row_start, row_end) from t1 for system_time all;
# cleanup
drop table t1;
insert into t1 select 2, f2, 2 from t1;
select f1, f3, check_row(row_start, row_end) from t1;
delete from t1 where f1 = 1;
select f1, f3, check_row(row_start, row_end) from t1 for system_time all order by f1, row_end;
create table t2 (f1 int);
insert into t2 values (2);
--echo # Multi-delelte
delete t1, t2 from t1 join t2 where t1.f1 = t2.f1;
select f1, f3, check_row(row_start, row_end) from t1 for system_time all order by f1, row_end;
--echo # Cleanup
drop tables t1, t2;
--source suite/versioning/common_finish.inc

View File

@ -26,15 +26,18 @@ eval create table t1 (
sys_trx_end $sys_datatype_expl as row end invisible,
period for system_time (sys_trx_start, sys_trx_end))
with system versioning;
set timestamp= unix_timestamp('2000-01-01 00:00:00');
insert into t1 values(1, 1, 1);
set @ins_t= now(6);
select sys_trx_start into @tmp1 from t1;
set timestamp= unix_timestamp('2000-01-01 01:00:00');
update t1 set x= 11, y= 11 where id = 1;
select @tmp1 < sys_trx_start as A1, x, y from t1;
select sys_trx_start into @tmp1 from t1;
set timestamp= unix_timestamp('2000-01-01 02:00:00');
update t1 set y= 1 where id = 1;
select @tmp1 = sys_trx_start as A2, x from t1;
drop table t1;
set timestamp= default;
replace_result $sys_datatype_expl SYS_DATATYPE;
eval create table t1 (
@ -389,6 +392,28 @@ select x, y, check_row_ts(row_start, row_end) from t1 for system_time all order
drop table t1;
--echo #
--echo # MDEV-25644 UPDATE not working properly on transaction precise system versioned table
--echo #
create or replace table t1 (nid int primary key, nstate int, ntype int) engine innodb;
--replace_result $sys_datatype_expl SYS_DATATYPE
eval alter table t1 add
row_start $sys_datatype_expl generated always as row start invisible,
add row_end $sys_datatype_expl generated always as row end invisible,
add period for system_time(row_start, row_end),
add system versioning;
insert into t1 values (1, 1, 1);
select nid, nstate, check_row(row_start, row_end) from t1 for system_time all order by row_start, row_end;
start transaction;
update t1 set nstate= nstate where nid = 1;
select nid, nstate, check_row(row_start, row_end) from t1 for system_time all order by row_start, row_end;
--echo # Bug: ERROR 1761 (23000): Foreign key constraint for table 'xxx', record '1-18446744073709551615' would lead to a duplicate entry in table 'xxx', key 'PRIMARY'
update t1 set nstate= 3 where nid= 1;
# Under one transaction trx_id generates only one history row, that differs from timestamp
select nid, nstate, check_row(row_start, row_end) from t1 for system_time all order by row_start, row_end;
commit;
drop tables t1;
--echo # End of 10.4 tests
source suite/versioning/common_finish.inc;