How to do get_or_insert_by in Ecto without race condition?

I think we’re mostly in agreement here.

As I said, at 0 scale basically anything works. Heck, even just “hope the race condition doesn’t happen” might be just fine.

If you’re not doing JSONB specifically I don’t super duper understand what would be even vaguely hard about a unique index. create unique index on my_table (my_unique_column) and just call it a day. You knock out two birds with one stone:

  1. Your get_by will stay fast
  2. You are guaranteed to not get > 1 rows back.

This will take you all the way from “Hope the race condition doesn’t happen” up through tens of millions of rows without even really thinking about it and while yes, there is always a bottleneck, 8 orders of magnitude worth of scale is pretty dang good as a default approach.