diff --git a/mysql-test/main/long_unique_innodb.result b/mysql-test/main/long_unique_innodb.result index fd2b3daf8e5..8c23d61f2bc 100644 --- a/mysql-test/main/long_unique_innodb.result +++ b/mysql-test/main/long_unique_innodb.result @@ -169,4 +169,15 @@ id f 1 foo 2 foo drop table t1; +# +# MDEV-39846 UNIQUE BLOB contains duplicate records after ALTER IGNORE with READ COMMITTED +# +create table t1 (pk int primary key, b blob) engine=innodb; +insert into t1 values (1,'foo'),(2,'foo'); +set transaction isolation level read committed; +alter ignore table t1 add unique (b); +select * from t1; +pk b +1 foo +drop table t1; # End of 10.6 tests diff --git a/mysql-test/main/long_unique_innodb.test b/mysql-test/main/long_unique_innodb.test index cbe2d7431fe..fc51817e45a 100644 --- a/mysql-test/main/long_unique_innodb.test +++ b/mysql-test/main/long_unique_innodb.test @@ -176,4 +176,14 @@ update ignore t1 set id = 2; select * from t1; drop table t1; +--echo # +--echo # MDEV-39846 UNIQUE BLOB contains duplicate records after ALTER IGNORE with READ COMMITTED +--echo # +create table t1 (pk int primary key, b blob) engine=innodb; +insert into t1 values (1,'foo'),(2,'foo'); +set transaction isolation level read committed; +alter ignore table t1 add unique (b); +select * from t1; +drop table t1; + --echo # End of 10.6 tests diff --git a/sql/sql_table.cc b/sql/sql_table.cc index bb4993bec96..533c9d720ff 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -94,6 +94,7 @@ class Enable_wsrep_ctas_guard #endif /* WITH_WSREP */ #include "sql_debug.h" +#include "scope.h" #ifdef _WIN32 #include @@ -10647,6 +10648,12 @@ bool mysql_alter_table(THD *thd, const LEX_CSTRING *new_db, */ table_list->required_type= TABLE_TYPE_NORMAL; + enum_tx_isolation iso_level_initial= thd->tx_isolation; + SCOPE_EXIT([thd, iso_level_initial](){ + thd->tx_isolation= iso_level_initial; + }); + thd->tx_isolation= ISO_REPEATABLE_READ; + DEBUG_SYNC(thd, "alter_table_before_open_tables"); thd->open_options|= HA_OPEN_FOR_ALTER;