Re: arbitrary number of values from a sequence

Eric G. Miller <egm2@jps.net>

From: "Eric G. Miller" <egm2@jps.net>
To: pgsql-general@postgresql.org
Date: 2001-05-05T13:13:06Z
Lists: pgsql-general
On Sat, May 05, 2001 at 02:33:22PM +0200, Gyozo Papp wrote:
> Ok. I'm looking for another solution.
> 
> The reason why I'm not dealing with sequence's increment is that 
> there is no way to set an appropiate limit, sometimes I need 5, sometimes 17.

CREATE TABLE myseq (
	val  integer NOT NULL;
);

psuedocode:

func (integer howmany)
    ...
    BEGIN TRANSACTION;
    oldval := SELECT val FROM myseq FOR UPDATE;
    newval := oldval + howmany;
    UPDATE myseq SET val = newval;
    COMMIT;

    for i := oldval; i < newval; incr i
    	do stuff

    ...

You might want to use LOCK instead of FOR UPDATE since its behavior
depends on the TRANSACTION ISOLATION LEVEL.

-- 
Eric G. Miller <egm2@jps.net>