Re: About sequences that works BAD !!!! - Mailing list pgsql-sql

From Richard Huxton
Subject Re: About sequences that works BAD !!!!
Date
Msg-id 44905556.2060506@archonet.com
Whole thread Raw
In response to About sequences that works BAD !!!!  ("Alexis Palma Espinosa" <apalma@uci.cu>)
List pgsql-sql
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



pgsql-sql by date:

Previous
From: "Alexis Palma Espinosa"
Date:
Subject: About sequences that works BAD !!!!
Next
From: Terry Lee Tucker
Date:
Subject: Re: About sequences that works BAD !!!!