pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Alexis Palma Espinosa <apalma@uci.cu>
Cc: pgsql-sql@postgresql.org
Subject: Re: About sequences that works BAD !!!!
Date: Wed, 14 Jun 2006 19:28:38 +0100
Message-ID: <44905556.2060506@archonet.com> (raw)
In-Reply-To: <251EA6C55832BE4C80F868CC951BA8CE02764F94@ucixw2.uci.cu>
References: <251EA6C55832BE4C80F868CC951BA8CE02764F94@ucixw2.uci.cu>

Alexis Palma Espinosa wrote:
> Hello everyone:
> 
> 
> 
> We are working with serials fields and we found a problem with then:
> When we insert in a table that has e unique restrict, and this makes
> insert fails, the sequence increments anyway...¿What we can do about
> it?

Nothing. The whole point of sequences is that they don't lock. They *do* 
guarantee unique numbers, but they *do not* guarantee no gaps.

If you really want a series of ID numbers with no gaps you'll want to do 
something like:

1. Begin transaction
2. Lock table exclusively
3. Find highest existing ID (...ORDER BY id DESC LIMIT 1)
4. Add one to it
5. Store new row.
6. commit transaction, freeing the lock

-- 
   Richard Huxton
   Archonet Ltd




view thread (3+ messages)  latest in thread

Message-ID: <44905556.2060506@archonet.com>
Permalink:  ../44905556.2060506@archonet.com/
Also on:    postgresql.org/message-id/44905556.2060506@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, apalma@uci.cu
  Subject: Re: About sequences that works BAD !!!!
  In-Reply-To: <44905556.2060506@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox