Commit 31c990ca authored by Rohit Kalhans's avatar Rohit Kalhans

BUG#11758263 50440: MARK UNORDERED UPDATE WITH AUTOINC UNSAFE

       
Problem: Statements that write to tables with auto_increment columns
         based on the selection from another table, may lead to master
         and slave going out of sync, as the order in which the rows
         are retrieved from the table may differ on master and slave.
            
Solution: We mark writing to a table with auto_increment table
          based on the rows selected from another table as unsafe. This
          will cause the execution of such statements to throw a warning
          and forces the statement to be logged in ROW if the logging
          format is mixed. 
            
Changes:
       1. All the statements that writes to a table with auto_increment 
          column(s) based on the rows fetched from another table, will now
          be unsafe.
       2. CREATE TABLE with SELECT will now be unsafe.

sql/share/errmsg-utf8.txt:
  Added new warning messages.
sql/sql_base.cc:
  -Created function to check statements that write to 
   tables with auto_increment column and has select.
  -Marked all the statements that write to a table
   with auto_increment column based on rows fetched
   from other table(s) as unsafe.
sql/sql_table.cc:
  mark CREATE TABLE[with auto_increment column] as unsafe.
parent b7430d73
......@@ -10,6 +10,8 @@
# We also check how the foreign_key_check variable is replicated
-- source include/master-slave.inc
call mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.");
#should work for both SBR and RBR
create table t1(a int auto_increment, primary key(a));
......@@ -50,6 +52,7 @@ create table t2(b int auto_increment, c int, primary key(b));
insert into t1 values (10);
insert into t1 values (null),(null),(null);
insert into t2 values (5,0);
--disable_warnings ONCE
insert into t2 (c) select * from t1 ORDER BY a;
select * from t2 ORDER BY b;
sync_slave_with_master;
......
source include/master-slave.inc;
call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.');
eval CREATE TABLE t1 (
a int unsigned not null auto_increment primary key,
b int unsigned
......@@ -11,6 +13,7 @@ eval CREATE TABLE t2 (
) ENGINE=$engine_type;
INSERT INTO t1 VALUES (NULL, 0);
--disable_warnings ONCE
INSERT INTO t1 SELECT NULL, 0 FROM t1;
INSERT INTO t2 VALUES (NULL, 0), (NULL,1);
......@@ -18,6 +21,7 @@ INSERT INTO t2 VALUES (NULL, 0), (NULL,1);
SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
--disable_warnings ONCE
UPDATE t1, t2 SET t1.b = t2.b WHERE t1.a = t2.a;
sync_slave_with_master;
......
......@@ -17,6 +17,8 @@ eval CREATE TABLE t2 (
) ENGINE=$engine_type;
INSERT INTO t1 VALUES (NULL, 0);
--disable_warnings ONCE
INSERT INTO t1 SELECT NULL, 0 FROM t1;
INSERT INTO t2 VALUES (NULL, 0), (NULL,1);
......@@ -24,6 +26,7 @@ INSERT INTO t2 VALUES (NULL, 0), (NULL,1);
SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
--disable_warnings ONCE
UPDATE t1, t2 SET t1.b = (t2.b+4) WHERE t1.a = t2.a;
SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
......
......@@ -18,6 +18,8 @@ eval CREATE TABLE t2 (
) ENGINE=$engine_type;
INSERT INTO t1 VALUES (NULL, 0);
--disable_warnings ONCE
INSERT INTO t1 SELECT NULL, 0 FROM t1;
INSERT INTO t2 VALUES (NULL, 0), (NULL,1);
......@@ -25,6 +27,7 @@ INSERT INTO t2 VALUES (NULL, 0), (NULL,1);
SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
--disable_warnings ONCE
UPDATE t2, (SELECT a FROM t1 ORDER BY a) AS t SET t2.b = t.a+5 ;
SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
......
call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.');
drop table if exists t1,t2,t3,t4;
drop database if exists client_test_db;
create table t1
......
......@@ -233,7 +233,7 @@ while ($unsafe_type < 9) {
--let $value_0=
--let $sel_sidef_0=
--let $sel_retval_0=
--let $CRC_ARG_expected_number_of_warnings= 6
--let $CRC_ARG_expected_number_of_warnings= 7
}
if ($unsafe_type == 8) {
......
include/master-slave.inc
[connection master]
call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.');
drop database if exists test1;
create database test1;
use test1;
......
include/master-slave.inc
[connection master]
call mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.");
create table t1(a int auto_increment, primary key(a));
create table t2(b int auto_increment, c int, primary key(b));
insert into t1 values (1),(2),(3);
......
include/master-slave.inc
[connection master]
call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.');
CREATE TABLE t1 (
a int unsigned not null auto_increment primary key,
b int unsigned
......
include/master-slave.inc
[connection master]
call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT');
drop table if exists t1,t2;
CREATE TABLE t1 (
a int unsigned not null auto_increment primary key,
......
include/master-slave.inc
[connection master]
call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.');
-------- Test for BUG#9361 --------
CREATE TABLE t1 (
......
CALL mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.");
start slave;
Got one of the listed errors
start slave;
......
......@@ -8,6 +8,7 @@
# Test supplied by Are Casilla
source include/master-slave.inc;
call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.');
--disable_warnings
connection master;
drop database if exists test1;
......@@ -42,12 +43,16 @@ CREATE PROCEDURE simpleproc3 ()
$
DELIMITER ;$
--disable_warnings
CALL simpleproc3();
--enable_warnings
select * from t2;
TRUNCATE TABLE `t1`;
--disable_warnings
CALL simpleproc3();
--enable_warnings
select * from t1;
......
......@@ -6,6 +6,7 @@
#######################################################
--source include/not_ndb_default.inc
--source include/master-slave.inc
call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT');
let $engine_type=MyISAM;
--source extra/rpl_tests/rpl_multi_update2.test
--source include/rpl_end.inc
......@@ -6,6 +6,7 @@
#######################################################
--source include/not_ndb_default.inc
--source include/master-slave.inc
call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.');
let $engine_type=MyISAM;
-- source extra/rpl_tests/rpl_multi_update3.test
--source include/rpl_end.inc
......@@ -19,6 +19,7 @@ enable_query_log;
create table t1 (a int not null auto_increment primary key, b int, key(b));
INSERT INTO t1 (a) VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
--disable_warnings
INSERT INTO t1 (a) SELECT null FROM t1;
INSERT INTO t1 (a) SELECT null FROM t1;
INSERT INTO t1 (a) SELECT null FROM t1;
......@@ -32,6 +33,7 @@ INSERT INTO t1 (a) SELECT null FROM t1;
INSERT INTO t1 (a) SELECT null FROM t1;
INSERT INTO t1 (a) SELECT null FROM t1;
INSERT INTO t1 (a) SELECT null FROM t1;
--enable_warnings
save_master_pos;
# a few updates to force OPTIMIZE to do something
--disable_warnings
......
......@@ -27,6 +27,7 @@ EOF
chmod 0000 $MYSQLD_SLAVE_DATADIR/master.info;
connection slave;
CALL mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.");
# START SLAVE will fail because it can't read the file (mode 000)
# (system error 13)
--replace_result $MYSQL_TEST_DIR TESTDIR
......
......@@ -52,9 +52,10 @@ SET GLOBAL event_scheduler = ON;
replace_result $engine_type ENGINE_TYPE;
eval CREATE TABLE t1 (i INT NOT NULL AUTO_INCREMENT PRIMARY KEY, f varchar(8)) ENGINE=$engine_type;
INSERT INTO t1 (f) VALUES ('a'),('a'),('a'),('a'),('a');
--disable_warnings
INSERT INTO t1 SELECT i+5, f FROM t1;
INSERT INTO t1 SELECT i+10, f FROM t1;
--enable_warnings
CREATE EVENT ev1 ON SCHEDULE EVERY 1 SECOND
DO INSERT INTO t1 VALUES (SLEEP(5),CONCAT('ev1_',CONNECTION_ID()));
CREATE EVENT ev2 ON SCHEDULE EVERY 1 SECOND
......
......@@ -13,14 +13,14 @@
# timezone used in CONVERT_TZ is not binlogged. To debug (by Guilhem
# and possibly Konstantin).
source include/master-slave.inc;
--disable_query_log
CALL mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT");
--enable_query_log
--disable_ps_protocol
source include/master-slave.inc;
# Save original timezone
set @my_time_zone= @@global.time_zone;
......@@ -90,6 +90,7 @@ insert into t1 values ('20040101000000',NULL), ('20040611093902',NULL);
# from originally inserted)
#
set time_zone='MET';
--disable_warnings ONCE
insert into t2 (select * from t1);
SELECT * FROM t1 ORDER BY n;
sync_slave_with_master;
......
......@@ -106,3 +106,26 @@ let $wait_condition=
select count(*) = 0 from information_schema.processlist
where db='events_test' and command = 'Connect' and user=current_user();
--source include/wait_condition.inc
--echo #
--echo # Test for bug#11748899 -- EVENT SET TO DISABLED AND ON COMPLETION
--echo # NOT PRESERVE IS DELETED AT SERVER
--echo #
SELECT @@event_scheduler;
USE test;
--disable_warnings
DROP EVENT IF EXISTS e1;
--enable_warnings
CREATE EVENT e1 ON SCHEDULE EVERY 1 SECOND DISABLE DO SELECT 1;
--replace_column 6 # 9 # 10 #
SHOW EVENTS;
--echo "Now we restart the server"
--source include/restart_mysqld.inc
USE test;
SELECT @@event_scheduler;
--replace_column 6 # 9 # 10 #
SHOW EVENTS;
DROP EVENT e1;
--echo # end test for bug#11748899
......@@ -352,6 +352,7 @@ create table `t2` (`c2_id` int(10) unsigned NULL auto_increment, `c2_p_id` int(1
insert into t1 values (0,'A01-Comp',1);
insert into t1 values (0,'B01-Comp',1);
insert into t2 values (0,1,'A Note',1);
--disable_warnings ONCE
update t1 left join t2 on p_id = c2_p_id set c2_note = 'asdf-1' where p_id = 2;
select * from t1;
select * from t2;
......
-- source include/not_embedded.inc
-- source include/have_log_bin.inc
call mtr.add_suppression('Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.');
#
# SQL Syntax for Prepared Statements test
#
......@@ -238,8 +239,10 @@ prepare stmt1 from "select 1 into @var";
execute stmt1;
execute stmt1;
prepare stmt1 from "create table t1 select 1 as i";
--disable_warnings ONCE
execute stmt1;
drop table t1;
--disable_warnings ONCE
execute stmt1;
prepare stmt1 from "insert into t1 select i from t1";
execute stmt1;
......@@ -432,6 +435,7 @@ deallocate prepare stmt;
create table t1 (a int);
insert into t1 values (1),(2),(3);
create table t2 select * from t1;
--disable_warnings
prepare stmt FROM 'create table t2 select * from t1';
drop table t2;
execute stmt;
......@@ -441,6 +445,7 @@ execute stmt;
execute stmt;
drop table t2;
execute stmt;
--enable_warnings
drop table t1,t2;
deallocate prepare stmt;
......@@ -1176,6 +1181,7 @@ create database mysqltest character set utf8;
prepare stmt1 from "create table mysqltest.t1 (c char(10))";
prepare stmt2 from "create table mysqltest.t2 select 'test'";
execute stmt1;
--disable_warnings ONCE
execute stmt2;
show create table mysqltest.t1;
show create table mysqltest.t2;
......@@ -1183,6 +1189,7 @@ drop table mysqltest.t1;
drop table mysqltest.t2;
alter database mysqltest character set latin1;
execute stmt1;
--disable_warnings ONCE
execute stmt2;
show create table mysqltest.t1;
show create table mysqltest.t2;
......
......@@ -6486,3 +6486,11 @@ ER_PLUGIN_NO_UNINSTALL
ER_PLUGIN_NO_INSTALL
eng "Plugin '%s' is marked as not dynamically installable. You have to stop the server to install it."
ER_BINLOG_UNSAFE_WRITE_AUTOINC_SELECT
eng "Statements writing to a table with an auto-increment column after selecting from another table are unsafe because the order in which rows are retrieved determines what (if any) rows will be written. This order cannot be predicted and may differ on master and the slave."
ER_BINLOG_UNSAFE_CREATE_SELECT_AUTOINC
eng "CREATE TABLE... SELECT... on a table with an auto-increment column is unsafe because the order in which rows are retrieved by the SELECT determines which (if any) rows are inserted. This order cannot be predicted and may differ on master and the slave."
......@@ -214,7 +214,8 @@ static bool auto_repair_table(THD *thd, TABLE_LIST *table_list);
static void free_cache_entry(TABLE *entry);
static bool
has_write_table_with_auto_increment(TABLE_LIST *tables);
static bool
has_write_table_with_auto_increment_and_select(TABLE_LIST *tables);
uint cached_open_tables(void)
{
......@@ -5683,6 +5684,17 @@ bool lock_tables(THD *thd, TABLE_LIST *tables, uint count,
/* We have to emulate LOCK TABLES if we are statement needs prelocking. */
if (thd->lex->requires_prelocking())
{
/*
DML statements that modify a table with an auto_increment column based on
rows selected from a table are unsafe as the order in which the rows are
fetched fron the select tables cannot be determined and may differ on
master and slave.
*/
if (thd->variables.binlog_format != BINLOG_FORMAT_ROW && tables &&
has_write_table_with_auto_increment_and_select(tables))
thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_WRITE_AUTOINC_SELECT);
/*
A query that modifies autoinc column in sub-statement can make the
master and slave inconsistent.
......@@ -9080,6 +9092,41 @@ has_write_table_with_auto_increment(TABLE_LIST *tables)
return 0;
}
/*
checks if the tables have select tables in the table list and write tables
with auto-increment column.
SYNOPSIS
has_two_write_locked_tables_with_auto_increment_and_select
tables Table list
RETURN VALUES
-true if the table list has atleast one table with auto-increment column
and atleast one table to select from.
-false otherwise
*/
static bool
has_write_table_with_auto_increment_and_select(TABLE_LIST *tables)
{
bool has_select= false;
bool has_auto_increment_tables = has_write_table_with_auto_increment(tables);
for(TABLE_LIST *table= tables; table; table= table->next_global)
{
if (!table->placeholder() &&
(table->lock_type <= TL_READ_NO_INSERT))
{
has_select= true;
break;
}
}
return(has_select && has_auto_increment_tables);
}
/*
Open and lock system tables for read.
......
......@@ -61,9 +61,11 @@ Query_tables_list::binlog_stmt_unsafe_errcode[BINLOG_STMT_UNSAFE_COUNT] =
ER_BINLOG_UNSAFE_MIXED_STATEMENT,
ER_BINLOG_UNSAFE_INSERT_IGNORE_SELECT,
ER_BINLOG_UNSAFE_INSERT_SELECT_UPDATE,
ER_BINLOG_UNSAFE_WRITE_AUTOINC_SELECT,
ER_BINLOG_UNSAFE_REPLACE_SELECT,
ER_BINLOG_UNSAFE_CREATE_IGNORE_SELECT,
ER_BINLOG_UNSAFE_CREATE_REPLACE_SELECT,
ER_BINLOG_UNSAFE_CREATE_SELECT_AUTOINC,
ER_BINLOG_UNSAFE_UPDATE_IGNORE
};
......
......@@ -1268,6 +1268,13 @@ public:
*/
BINLOG_STMT_UNSAFE_INSERT_SELECT_UPDATE,
/**
Query that writes to a table with auto_inc column after selecting from
other tables are unsafe as the order in which the rows are retrieved by
select may differ on master and slave.
*/
BINLOG_STMT_UNSAFE_WRITE_AUTOINC_SELECT,
/**
INSERT...REPLACE SELECT is unsafe because which rows are replaced depends
on the order that rows are retrieved by SELECT. This order cannot be
......@@ -1289,6 +1296,14 @@ public:
*/
BINLOG_STMT_UNSAFE_CREATE_REPLACE_SELECT,
/**
CREATE TABLE...SELECT on a table with auto-increment column is unsafe
because which rows are replaced depends on the order that rows are
retrieved from SELECT. This order cannot be predicted and may differ on
master and the slave
*/
BINLOG_STMT_UNSAFE_CREATE_SELECT_AUTOINC,
/**
UPDATE...IGNORE is unsafe because which rows are ignored depends on the
order that rows are updated. This order cannot be predicted and may differ
......
......@@ -3121,6 +3121,15 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
DBUG_RETURN(TRUE);
}
/*
CREATE TABLE[with auto_increment column] SELECT is unsafe as the rows
inserted in the created table depends on the order of the rows fetched
from the select tables. This order may differ on master and slave. We
therefore mark it as unsafe.
*/
if (select_field_count > 0 && auto_increment)
thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_CREATE_SELECT_AUTOINC);
/* Create keys */
List_iterator<Key> key_iterator(alter_info->key_list);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment