Skip navigation links

MySQL Forums :: Replication :: Unsafe statement written to the binary log


Advanced Search

Re: Unsafe statement written to the binary log
Posted by: Rick James ()
Date: March 27, 2012 08:43PM

> 1. INSERT... SELECT... ON DUPLICATE KEY UPDATE is unsafe because the order in which rows are retrieved by the SELECT determines which (if any) rows are updated. This order cannot be predicted and may differ on master and the slave.

> 2. The statement is unsafe because it uses a LIMIT clause. This is unsafe because the set of rows included cannot be predicted.

Both of those may be solved by adding an ORDER BY. I say "may be" for two reasons:
* If the ORDER BY does not completely determine the order, it is inadequate.
* There is a case for which ORDER BY is not sufficient. (Sorry, I can't seem to find the item in bugs.mysql.com at the moment.)

One workaround is (roughly, and in pseudo code):
1. $last_id = SELECT id ... ORDER BY id LIMIT 100,1;
2. UPDATE ... WHERE id BETWEEN $first_id AND $last_id;
The idea is to avoid any ambiguity in the replication stream by having a precise range over which do do the operation.

The reason for the error (which is relatively new, in spite of the potential error existing since day 1), is just what it says -- The Slave could pick a different set of rows, thereby making the Master and Slave out of sync.

Suggest you present a small, fully deterministic, test case to bugs.mysql.com.

Options: ReplyQuote


Subject Views Written By Posted
Unsafe statement written to the binary log 6241 Shalini Singh 03/23/2012 02:47AM
Re: Unsafe statement written to the binary log 2924 smriti . 03/23/2012 07:41AM
Re: Unsafe statement written to the binary log 2698 Shalini Singh 03/23/2012 07:43AM
Re: Unsafe statement written to the binary log 2660 Rick James 03/25/2012 03:15PM
Re: Unsafe statement written to the binary log 2999 Shalini Singh 03/26/2012 07:32AM
Re: Unsafe statement written to the binary log 3181 Rick James 03/27/2012 08:43PM
Re: Unsafe statement written to the binary log 2165 Shalini Singh 04/02/2012 04:15AM
Re: Unsafe statement written to the binary log 1990 Rick James 04/02/2012 11:43PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.