This comes from a recent mystery I had. I created a user, it's ID was 13. For whatever reason, I deleted that user and created another. The next ID was 15. I was curious what happened to ID #14, as I could not find it anywhere in the database.

I'm now pretty sure that ID #14 went to a batch job. There's code in batch_process() that looks like this:

    // Assign an arbitrary id: don't rely on a serial column in the 'batch'
    // table, since non-progressive batches skip database storage completely.
    $batch['id'] = db_next_id();

The comment seems to imply that something that skips database storage completely needs to take an ID that would otherwise go to an entity.

My personal feeling is that those entity IDs should only be used by something that's going to stick around for a while. Say, until it is deleted. I wonder if the batch code should use uniqid() or something like that.

I imagine you'll say, "they're just IDs and it doesn't matter what the number is." And that's reasonable. Still, it was a little disconcerting to me that ID #14 went somewhere, and I couldn't find out where. I feel it would just be nicer if the entity IDs were in order.

Comments

Dave Cohen’s picture

Note, I mistakenly thought that each entity (user, node, whatever) was getting it's IDs from db_next_id().

I now see this is wrong. Some entities (i.e. nodes) use an autoincrement. While others (users, actions) use db_next_id(). Why users table does not autoincrement, I do not know.

Dave Cohen’s picture

Status: Active » Closed (works as designed)

I googled a bit more and found #356074: Provide a sequences API. There, I learned that I'm a "client who think they know more then they actually do." That's nice.

I don't like the ID skipping, but I see the powers that be are in favor of it. I know there's nothing I can say gonna change any of this.