MySQL Forums
Forum List  »  Replication

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
Re: Unsafe statement written to the binary log
3927
March 27, 2012 08: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.