How to do get_or_insert_by in Ecto without race condition?

A table lock will work if you aren’t allowed to use a unique index. Without insight into why a client might “forbid” such an index though it’s hard to comment further, that is such a strange choice. As I noted, whether you use a table lock or not you still probably want an index to avoid a table scan. At that point adding unique is literally free. If you are OK with table scans and don’t mind locking the whole table for inserts while you do the table scan then probably we’re back to 0 scale.

EDIT: Notably, a table lock is ONLY going to work if EVERY code path which inserts takes this lock and verifies. It provides no guarantees of the data “at rest” and so it is quite susceptible to bugs, or even just gaps in reasoning where you forget that in addition to the normal code path there is also some other path which forgets to make this check.