omission in file 'includes/':

function db_next_id($name) {

** Note that REPLACE query below correctly creates a new sequence
** when needed

$name = db_prefix_tables($name);
// added:
$seqtbl = db_prefix_tables("{sequences}");
//db_query("LOCK TABLES {sequences} WRITE");
// now is:
db_query("LOCK TABLES {$seqtbl} WRITE");
// and so on...
$id = db_result(db_query("SELECT id FROM {$seqtbl} WHERE name = '%s'", $name)) + 1;
db_query("REPLACE INTO {$seqtbl} VALUES ('%s', %d)", $name, $id);
db_query("UNLOCK TABLES");

return $id;

Further caveat:
- IFF db_prefix is enabled AFTER drupal has already run, the 'name' column in table 'sequences' is NOT updated, so all counters are reset.

I think it is safe to not prefix the $name arg passed to db_next_id().
I did not have time to look at the context where func db_next_id() is used, and various kinds of args it might be passed to it (I was rushing to fix this drupal site that was moved, and just updated the sequences table as a quick fix).

But the collective you has more experience with the innards and perhaps can come with the best solution.

thanks and keep up the great work!