title: Atomic UPDATE to increment integer in Postgresql
body: <p>I'm trying to figure out if the query below is safe to use for the following scenario:</p>
  
  <p>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.</p>
  
  <p>To get the next sequence, I'm firing off the SQL statement below.</p>
  
  <pre><code>WITH updated AS (
    UPDATE sequences SET sequence = sequence + ? 
    WHERE sequence_id = ? RETURNING sequence
  )
  SELECT * FROM updated;
  </code></pre>
  
  <p>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?</p>
  
  <p>Note: the first parameter is usually 1, but could also be 10 for example, to get a block of 10 new sequence numbers</p>