title: postgresql generate sequence with no gap
body: <p>I must / have to create unique ID for invoices. I have a table id and another column for this unique number. I use serialization isolation level. Using</p>
  
  <pre><code>  var seq = @"SELECT invoice_serial + 1 FROM  invoice WHERE ""type""=@type ORDER BY invoice_serial DESC LIMIT 1";
  </code></pre>
  
  <p>Doesn't help because even using FOR UPDATE it wont read correct value as in serialization level.</p>
  
  <p>Only solution seems to put some retry code. </p>