Re: arbitrary number of values from a sequence - Mailing list pgsql-general

From Eric G. Miller
Subject Re: arbitrary number of values from a sequence
Date
Msg-id 20010505061306.A25928@calico.local
Whole thread Raw
In response to Re: arbitrary number of values from a sequence  ("Gyozo Papp" <pgerzson@freestart.hu>)
List 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>

pgsql-general by date:

Previous
From: "Gyozo Papp"
Date:
Subject: Re: arbitrary number of values from a sequence
Next
From: "Christian Marschalek"
Date:
Subject: RE: [ADMIN] Primary Keys