I'm trying to figure out if the query below is safe to use for the following scenario:
I need to generate sequential numbers, without gaps. As I need to track many of them, I have a table holding sequence records, with a sequence integer column.
To get the next sequence, I'm firing off the SQL statement below.
WITH updated AS (
UPDATE sequences SET sequence = sequence + ?
WHERE sequence_id = ? RETURNING sequence
)
SELECT * FROM updated;
My question is: is the query below safe when multiple users fire this query at the database at the same time without explicitly starting a transaction?
Note: the first parameter is usually 1, but could also be 10 for example, to get a block of 10 new sequence numbers