I am trying to implement some parts of MERGE in the MySQL driver in Drupal. Of course, Drupal has something but in truth it only works because the most frequent MERGE issuer just eats exceptions.
So, whatever we try, deadlocks occur. What we do, we start a transaction, then SELECT ... FOR UPDATE, try an INSERT and if it causes an 23xxx integrity error try an UPDATE instead. Deadlocks. We removed the FOR UPDATE cos we decided that for our use, it's OK. Still deadlocks.
I can't just switch isolation levels because READ COMMITTED needs row logging per http://dev.mysql.com/doc/refman/5.1/en/set-transaction.html
As of MySQL 5.1, if you use READ COMMITTED [...] you must use row-based binary logging.
And per http://stackoverflow.com/a/2032096/308851 READ UNCOMMITTED also needs row logging. And here comes http://dev.mysql.com/doc/refman/5.1/en/binary-log-setting.html
To change the global binlog_format value, you must have the SUPER privilege. This is also true for the session value as of MySQL 5.1.29.
I can't require every Drupal setup to have SUPER nor we can say that Drupal is incompatible with statement based binlogs when that's the default and the most widespread.
INSERT ... ON DUPLICATE KEY is neither versatile enough nor is it deadlock free http://bugs.mysql.com/bug.php?id=52020
So what now?
MERGEthat is not at all clear. Examples of what you are specifically trying to do would be helpful, including what you mean when you sayINSERT ... ON DUPLICATE KEY UPDATEisn't versatile enough. What can that construct not do, that you are trying to do? It sounds like that's what you're trying to do -- insert, or update if a key exists. – Michael - sqlbot Dec 31 '12 at 23:11MERGEis ANSI SQL standard and I am trying to emulate it, as much as I can. – chx Jan 1 at 2:20INSERT ... ON DUPLICATE KEY UPDATEdoesn't take you where you want to go. I make extensive use of it, and can't think of any examples where I've had to fight deadlocks related to it. – Michael - sqlbot Jan 1 at 3:35