3
# Some statements can not be written to the binlog in a safe manner
4
# with statement-based replication, either because they rely on
5
# features that are local to the server they are replicated from
6
# (e.g., @@variables), or because they include nondeterministic
7
# queries (e.g., LIMIT), or because the time at which the query is
8
# executed cannot be determined (e.g., INSERT DELAYED). Such
9
# statements should be marked unsafe. All unsafe statements should
12
# This test verifies that a warning is generated for statements that
13
# should be unsafe, when they are executed under statement mode
16
# All variables should be unsafe, with some exceptions. Therefore,
17
# this test also verifies that the exceptions do *not* generare a
23
# We try an INSERT DELAYED statement and verify that a warning is
26
# We try to insert unsafe variables into a table in several ways:
27
# directly with an INSERT statement, from a stored procedure, from a
28
# stored function, from a trigger, from a prepared statement, and from
29
# a complicated nesting of triggers, functions, procedures, and
30
# prepared statements. In all cases, a warning should be issued.
32
# We try to insert the variables that should not be unsafe into a
33
# table, and verify that *no* warning is issued.
36
# ==== Related bugs and worklogs ====
38
# WL#3339: Issue warnings when statement-based replication may fail
39
# BUG#31168: @@hostname does not replicate
40
# BUG#34732: mysqlbinlog does not print default values for auto_increment variables
41
# BUG#34768: nondeterministic INSERT using LIMIT logged in stmt mode if binlog_format=mixed
44
# ==== Related test cases ====
46
# rpl.rpl_variables verifies that variables which cannot be replicated
47
# safely in statement mode are replicated correctly in mixed or row
50
# rpl.rpl_variables_stm tests the small subset of variables that
51
# actually can be replicated safely in statement mode.
56
# There are several other ways to create unsafe statements: see, e.g.,
59
source include/have_log_bin.inc;
60
source include/have_binlog_format_statement.inc;
62
--echo ==== Setup tables ====
64
CREATE TABLE t1 (a INT);
65
CREATE TABLE t2 (a CHAR(40));
66
CREATE TABLE t3 (a INT AUTO_INCREMENT PRIMARY KEY);
67
CREATE TABLE trigger_table (a CHAR(7));
68
CREATE TABLE trigger_table2 (a INT);
71
--echo ==== Non-deterministic statements ====
73
INSERT DELAYED INTO t1 VALUES (5);
76
--echo ==== Some variables that *should* be unsafe ====
78
--echo ---- Insert directly ----
80
INSERT INTO t1 VALUES (@@global.sync_binlog);
81
INSERT INTO t1 VALUES (@@session.insert_id);
82
INSERT INTO t1 VALUES (@@global.auto_increment_increment);
83
INSERT INTO t2 SELECT UUID();
84
INSERT INTO t2 VALUES (@@session.sql_mode);
85
INSERT INTO t2 VALUES (@@global.init_slave);
86
INSERT INTO t2 VALUES (@@hostname);
88
--echo ---- Insert from stored procedure ----
91
CREATE PROCEDURE proc()
93
INSERT INTO t1 VALUES (@@global.sync_binlog);
94
INSERT INTO t1 VALUES (@@session.insert_id);
95
INSERT INTO t1 VALUES (@@global.auto_increment_increment);
96
INSERT INTO t2 SELECT UUID();
97
INSERT INTO t2 VALUES (@@session.sql_mode);
98
INSERT INTO t2 VALUES (@@global.init_slave);
99
INSERT INTO t2 VALUES (@@hostname);
105
--echo ---- Insert from stored function ----
108
CREATE FUNCTION func()
111
INSERT INTO t1 VALUES (@@global.sync_binlog);
112
INSERT INTO t1 VALUES (@@session.insert_id);
113
INSERT INTO t1 VALUES (@@global.auto_increment_increment);
114
INSERT INTO t2 SELECT UUID();
115
INSERT INTO t2 VALUES (@@session.sql_mode);
116
INSERT INTO t2 VALUES (@@global.init_slave);
117
INSERT INTO t2 VALUES (@@hostname);
124
--echo ---- Insert from trigger ----
128
BEFORE INSERT ON trigger_table
131
INSERT INTO t1 VALUES (@@global.sync_binlog);
132
INSERT INTO t1 VALUES (@@session.insert_id);
133
INSERT INTO t1 VALUES (@@global.auto_increment_increment);
134
INSERT INTO t2 SELECT UUID();
135
INSERT INTO t2 VALUES (@@session.sql_mode);
136
INSERT INTO t2 VALUES (@@global.init_slave);
137
INSERT INTO t2 VALUES (@@hostname);
141
INSERT INTO trigger_table VALUES ('bye.');
143
--echo ---- Insert from prepared statement ----
145
PREPARE p1 FROM 'INSERT INTO t1 VALUES (@@global.sync_binlog)';
146
PREPARE p2 FROM 'INSERT INTO t1 VALUES (@@session.insert_id)';
147
PREPARE p3 FROM 'INSERT INTO t1 VALUES (@@global.auto_increment_increment)';
148
PREPARE p4 FROM 'INSERT INTO t2 SELECT UUID()';
149
PREPARE p5 FROM 'INSERT INTO t2 VALUES (@@session.sql_mode)';
150
PREPARE p6 FROM 'INSERT INTO t2 VALUES (@@global.init_slave)';
151
PREPARE p7 FROM 'INSERT INTO t2 VALUES (@@hostname)';
153
EXECUTE p1; EXECUTE p2; EXECUTE p3; EXECUTE p4; EXECUTE p5;
154
EXECUTE p6; EXECUTE p7;
156
--echo ---- Insert from nested call of triggers / functions / procedures ----
160
# proc1: cause trigger 'trig' above to be triggered.
161
CREATE PROCEDURE proc1()
162
INSERT INTO trigger_table VALUES ('ha!')|
164
# func2: call proc1 above.
165
CREATE FUNCTION func2()
172
# trig3: call func2 above
174
BEFORE INSERT ON trigger_table2
178
SELECT func2() INTO tmp;
181
# proc4: cause trig3 above to be triggered.
182
CREATE PROCEDURE proc4()
183
INSERT INTO trigger_table2 VALUES (1)|
185
# func5: call proc4 above.
186
CREATE FUNCTION func5()
193
# prep6: call func5() above.
194
PREPARE prep6 FROM 'SELECT func5()'|
198
# try a complicated call path to trigger 'trig'.
202
--echo ==== Variables that should *not* be unsafe ====
204
INSERT INTO t1 VALUES (@@session.pseudo_thread_id);
205
INSERT INTO t1 VALUES (@@session.pseudo_thread_id);
206
INSERT INTO t1 VALUES (@@session.foreign_key_checks);
207
INSERT INTO t1 VALUES (@@session.sql_auto_is_null);
208
INSERT INTO t1 VALUES (@@session.unique_checks);
209
INSERT INTO t1 VALUES (@@session.auto_increment_increment);
210
INSERT INTO t1 VALUES (@@session.auto_increment_offset);
211
INSERT INTO t2 VALUES (@@session.character_set_client);
212
INSERT INTO t2 VALUES (@@session.collation_connection);
213
INSERT INTO t2 VALUES (@@session.collation_server);
214
INSERT INTO t2 VALUES (@@session.time_zone);
215
INSERT INTO t2 VALUES (@@session.lc_time_names);
216
INSERT INTO t2 VALUES (@@session.collation_database);
217
INSERT INTO t2 VALUES (@@session.timestamp);
218
INSERT INTO t2 VALUES (@@session.last_insert_id);
220
INSERT INTO t1 VALUES (@my_var);
222
# using insert_id implicitly should be ok.
224
INSERT INTO t3 VALUES (NULL);
227
--echo ==== Clean up ====
232
DROP PROCEDURE proc1;
235
DROP PROCEDURE proc4;
238
DROP TABLE t1, t2, t3, trigger_table, trigger_table2;
240
# BUG#34768 - nondeterministic INSERT using LIMIT logged in stmt mode if
241
# binlog_format=mixed
243
CREATE TABLE t1(a INT, b INT, KEY(a), PRIMARY KEY(b));
244
INSERT INTO t1 SELECT * FROM t1 LIMIT 1;
245
REPLACE INTO t1 SELECT * FROM t1 LIMIT 1;
246
UPDATE t1 SET a=1 LIMIT 1;
247
DELETE FROM t1 LIMIT 1;
249
CREATE PROCEDURE p1()
251
INSERT INTO t1 SELECT * FROM t1 LIMIT 1;
252
REPLACE INTO t1 SELECT * FROM t1 LIMIT 1;
253
UPDATE t1 SET a=1 LIMIT 1;
254
DELETE FROM t1 LIMIT 1;