Posted on 5 Comments

Ladies and gentlemen, check your assumptions

I spent some time earlier this week trying to debug a permissions problem in Drupal.

After a lot of head-scratching, it turned out that Drupal assumes that when you run INSERT queries sequentially on a table with an auto_increment integer column, the values that are assigned to this column will also be sequential, ie: 1, 2, 3, …

This might be a valid assumption when you are the only user doing inserts on a single MySQL server, but unfortunately that is not always the situation in which an application runs.

I run MySQL in a dual-master setup, which means that two sequential INSERT statements will never return sequential integers.  The value will always be determined by the  auto_increment_increment and auto_increment_offset settings in the configuration file.

In my case, one master will only assign even numbers, the other only uneven ones.

My patch was accepted, so this problem is now fixed in the Drupal 7 (and hopefully soon in 6 as well) codebase.

The moral of the story is that your application should never make such assumptions about auto_increment columns.  A user may run the application on a completely different architecture, and it may break in interesting and subtle ways.

If you want to use defined integers like Drupal does, make sure you explicitly insert them. Otherwise, you can retrieve the assigned number via the mysql_insert_id() function in PHP or via SELECT LAST_INSERT_ID() in MySQL itself.

Have you checked your code today?

Posted on 5 Comments
Posted on 8 Comments

Quiz: Enabling an application for MySQL Replication

A little challenge for you… given an existing app that does not know about separate master/slave connections, and you want to enable working in a replicated infrastructure. Simply redirecting all SELECTs to the slave connection will not work. Why?

Hint: there are at least two reasons, depending on other factors. There may be more.

Comments are set to be moderated so providing answers will not spoil it for others. I’ll leave it run for a bit and then approve all comments.

Posted on 8 Comments
Posted on 1 Comment

MySQL Replication Heartbeat

Well isn’t that interesting, hidden all the way at the end of the MySQL 5.4 information are two words that really peaked my interest: Replication Heartbeat. And it wasn’t even using caps or other highlighting in the original text. Reading through the feature list of 5.4, I’m very impressed. All necessary/useful stuff for the real world, no marketing or enterprise blah.

Of course we’ll have to explore it in detail to have more opinion. Proof is in production, not paper. As this is the first most of us have heard/seen of it, it’ll take time to explore. Someone who tried to install the tarball this morning got an assertion during the system table installation. That’s not the best first impression, but that might be a build issue. I’m really pretty excited about the lineup of actual useful features.

Update… ok so at the end of the announcement it refers to a Preview Release (Edwin, can you please murder this ghastly oxymoronic abomination in OSS Marketing?) and then a note that apart from some InnoDB performance patches the described enhancements are not actually in the version that was just made available. There will be future announcements/releases. Was I just giving compliments for actual delivery, and it turns out it’s vaporware after all. Aaargh!!!

So, we’ll be perusing https://launchpad.net/mysql-server/5.4 instead. The old “show us the code” works best. Let’s hope the code is actually there.

Posted on 1 Comment