It is now possible to get the last inserted ID from db_merge().

The accepted resolution was to add a getResult() method to the MergeQuery class.

Example implementation:

  $merge = db_merge('block_custom')
      'bid' => $delta,
      'body' => $edit['body']['value'],
      'info' => $edit['info'],
      'title' => $edit['title'],
      'format' => $edit['body']['format'],
  $result = $merge->execute();
  if ($result === $merge::STATUS_INSERT) {
    $delta = $merge->getResult();

The return value from $merge->execute() would stay the same, but after execution we could get the result from the $merge object.

Multiple previous reports stated that it was not possible, other than selecting the highest ID from the table, which either requires a table lock or causes a race condition.

Similar reports:

Introduced in branch: 
Introduced in version: 
Module developers