How can I guarantee that I can search if a username exists in my database, then insert that username into the database as a new row without any intercept between the SELECT
and INSERT
statements?
Almost as if I am locking on a row that doesn't exist. I want to lock on the non-existent row with the username "Foo", so that I can now check if it exists in the database AND insert it into the database if it doesn't already exist without any interruption.
I know that using LOCK IN SHARE MODE
and FOR UPDATE
exist but as far as I know, that only works on rows that already exist. I am not sure what to do in this situation.