@jeremy So now I pre-update all foreign keys in separate `UPDATE` statements so I can bail out if it has already been updated.

Note that it only prevent race conditions where the same `UPDATE` is performed multiple times concurrently.

If the updates are different then the the counter will be desynchronized, the only way to prevent that is to lock and reload the record.

So it's definitely more work for the DB as discussed here: https://github.com/rails/rails/pull/14735#issuecomment-40392602

Are you ok with that solution?