title: three applications picking the same unused value from a postgres database table
body: <p>I have 3 applications that get an unused value which is a bigint number from a table called tokens . The token table has 2 columns , token_id and status_id. If token is unused it has status_id=1 and when it is used it has status_id=3. The problem comes when 3 applications run at the same time and select the same unused value with status_id=1. When the token_id is selected, it is inserted into another table called tokenized. Besides making the token_id as foreign key in the tokenized table, is there another way of preventing the applications from picking the same unused token_id from tokens table?</p>
  <p>The query to select the unused token is below</p>
  <pre><code>select token_id from tokens where status_id=1 limit 1
  </code></pre>
  <p>The problem happens when 3 applications use the sql statement above at the same time and pick the same token_id</p>
  <p>How can i prevent this from happening</p>