Can failure to increase new user's UID consistently by one be related to this line inside user.module file? I have noticed at my Drupal 7 that there are gaps between the uid's of new users. The UID of a new user should increase by one from the highest assigned UID in database. However, I have noticed gaps of even a thousand.

Inside user.module file, there is the following code:

// Allow 'uid' to be set by the caller. There is no danger of writing an
      // existing user as drupal_write_record will do an INSERT.
      if (empty($account-&gt;uid)) {
        $account-&gt;uid = db_next_id(db_query('SELECT MAX(uid) FROM {users}')-&gt;fetchField());
      }


I have made the QB query read like this:

$account-&gt;uid = db_next_id(db_query('SELECT MAX(uid) FROM users')-&gt;fetchField());


It appears that this solves the issue. My question, is there really a mistake inside the core user.module file? Is it safe to modify the file the way I did? Thanks.

UPDATE:

The above does not make a difference after all. I have changed the line to this:

$account-&gt;uid = db_query('SELECT MAX(uid) FROM users')-&gt;fetchField() + 1;


This works with no reference to db_next_id. For me it is very important to increase the uid's always by one and this is one way to do it.
uid and other identifiers are not sequential since Drupal 7. Identifiers are generated using a sequence API, see this issue: https://www.drupal.org/node/356074

If you check the db_next_id you'll see that the number passed as argument is just de minimum id that is considered valid.

See also this question and answer: What could cause uids to not be numbered consecutively?