1

I have a table (id, serialnumber, status) of numbers and their statuses, need to take one with status 'unused' and change it's state to something like 'in work'. Can do it this way:

UPDATE serials
SET status = 'in work'
WHERE serialnumber IN (
  SELECT "serialnumber"
  FROM serials
  WHERE status = 'unused'
  ORDER BY id ASC 
  LIMIT 1
)
RETURNING serialnumber;

Is that the right way of selecting and updating a row in concurrent environment? It's unacceptable to take a serial when someone already took it.

1
  • 1
    See the linked question and answer, which has many methods and details. If you think that your problem is different, please edit your question and clarify and we can reopen it. – ypercubeᵀᴹ Dec 20 '18 at 14:19