Re: arbitrary number of values from a sequence

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Gyozo Papp" <pgerzson(at)freestart(dot)hu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: arbitrary number of values from a sequence
Date: 2001-05-04 16:54:36
Message-ID: 25770.988995276@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Gyozo Papp" <pgerzson(at)freestart(dot)hu> writes:
> and I need 5 five subsequent values (from 1234 to 1238), - in other
> words - a range with a length of 5 from the current value. I thought
> this simple trick could do the job :

> =#select setval('seq', (select nextval('seq')) + 5);

No good, because there's no interlock being held between the nextval()
and the setval(). So, while process A is busy adding 5 to the nextval()
result it got, process B could sneak in and do a nextval(). It will
then be allocated one of the values that A thinks it's reserved. After
the setval() occurs, there's not even any way to see anything's wrong.

I do not think you can avoid calling nextval() 5 times, in the current
implementation; nor can you assume you will get 5 consecutive values.
However, you might be able to improve efficiency by setting the 'cache'
value of the sequence to be more than one. Read the caution about
'cache' on the CREATE SEQUENCE manual page first...

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2001-05-04 17:31:09 Re: Re: mysql to Pgsql
Previous Message Joel Burton 2001-05-04 16:48:24 Re: a primer on trigger?