Re: About sequences that works BAD !!!!

From: Richard Huxton <dev(at)archonet(dot)com>
To: Alexis Palma Espinosa <apalma(at)uci(dot)cu>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: About sequences that works BAD !!!!
Date: 2006-06-14 18:28:38
Message-ID: 44905556.2060506@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Terry Lee Tucker 2006-06-14 18:39:33 Re: About sequences that works BAD !!!!
Previous Message Alexis Palma Espinosa 2006-06-14 18:02:55 About sequences that works BAD !!!!