Skip to content

Reduce subtransactions in `Shard`

The subtransactions are coming from https://gitlab.com/gitlab-org/gitlab/blob/7116c4ec4831894a4b4dfc0bccf2bc79c71e7c02/app/models/shard.rb#L20-26:

  def self.by_name(name)
    transaction(requires_new: true) do
      find_or_create_by(name: name)
    end
  rescue ActiveRecord::RecordNotUnique
    retry
  end

We could easily update this to use safe_find_or_create_by since that includes an optimization that avoids the subtransaction when a record is found.

Edited by Heinrich Lee Yu