Обсуждение: About sequences that works BAD !!!!

Поиск
Список
Период
Сортировка

About sequences that works BAD !!!!

От
"Alexis Palma Espinosa"
Дата:

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?

 

We hope you can help us…..

 

Alexis Palma Espinosa.

Ingeniero en Informática.

"If you are not part of the solution...you are part of the problem"

 

Re: About sequences that works BAD !!!!

От
Richard Huxton
Дата:
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



Re: About sequences that works BAD !!!!

От
Terry Lee Tucker
Дата:
On Wednesday 14 June 2006 02:02 pm, "Alexis Palma Espinosa" <apalma@uci.cu>
thus communicated:
--> Hello everyone:
-->
-->
-->
--> We are working with serials fields and we found a problem with then: Whenwe insert in a table that has e unique
restrict,and this makes insertfails, the sequence increments anyway...¿What we can do about it? --> 
-->
-->
--> We hope you can help us.....
-->
-->
-->

This is doing exactly what it is supposed to do. See the docs:
http://www.postgresql.org/docs/7.4/static/functions-sequence.html