Thread: Reserve a value in a serial type field

Reserve a value in a serial type field

From
"Albert"
Date:
Hi all. I have this issue: I need to load a value for an integer field
(with auto increment) which I implemented with a serial type and to
avoid inserts with that value in this field. The task is for a
distributed program I'm creating and I wonder if there is an efficient
solution for it.

I thought about setting the current value of the sequence when loading
the needed value..is it a solution? Is there something more efficient?

Thanks very much! Any help is appreciated!


Re: Reserve a value in a serial type field

From
Shane Ambler
Date:
Albert wrote:
> Hi all. I have this issue: I need to load a value for an integer field
> (with auto increment) which I implemented with a serial type and to
> avoid inserts with that value in this field. The task is for a
> distributed program I'm creating and I wonder if there is an efficient
> solution for it.

Is this the initial loading of data or during normal usage?

> I thought about setting the current value of the sequence when loading
> the needed value..is it a solution? Is there something more efficient?

If you are loading the initial data to begin using the db then setting
sequence values will be fine.

If you are thinking about doing this constantly with normal usage then I
would say drop the serial type and just use a normal int column. A
unique index on the column will stop the duplicate value inserts and
allow your program to workout the values it wants to use.

If you are planning to keep changing the sequence next_value there is no
real point of using the serial and you will find you will get problems
with one client getting the current_value right before another program
increases the value and inserts the same value as the first client.


--

Shane Ambler
pgSQL@007Marketing.com

Get Sheeky @ http://Sheeky.Biz

Re: Reserve a value in a serial type field

From
"Albert"
Date:
Thanks Shane.

I solved taking trace of the sequence value directly in the
program...it is not a good solution but it works until I find a better
one!


Re: Reserve a value in a serial type field

From
"A. Kretschmer"
Date:
am  Mon, dem 15.01.2007, um 10:57:17 -0800 mailte Albert folgendes:
> Hi all. I have this issue: I need to load a value for an integer field
> (with auto increment) which I implemented with a serial type and to
> avoid inserts with that value in this field. The task is for a
> distributed program I'm creating and I wonder if there is an efficient
> solution for it.
>
> I thought about setting the current value of the sequence when loading
> the needed value..is it a solution? Is there something more efficient?

A squence with a gap?

See http://a-kretschmer.de/diverses.shtml -> "A Sequence with a gap".

I hope this can solve your problem.


Andreas
--
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net