Re: [SQL] Serial numbers - Mailing list pgsql-sql

From Vadim Mikheev
Subject Re: [SQL] Serial numbers
Date
Msg-id 36B02E52.8E61CD44@krs.ru
Whole thread Raw
In response to Serial numbers  (Marcus Better <marcusb@matematik.su.se>)
Responses Re: [SQL] Serial numbers
List pgsql-sql
Marcus Better wrote:
>
> Herouth Maoz writes:
>
>  > So, the inserts for the ccats table become:
>  >
>  > INSERT INTO ccats (cid, catid) VALUES (currval( 'name of seq' ), 5 );
>
> What if someone does another INSERT between the two INSERTs, then the
> number generator is stepped and the second table gets the wrong serial
> number. Should one use LOCK here?

No!
man create_sequence:

       The  function currval ('sequence_name') may be used to re-
       fetch the number returned by the last call to nextval  for
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       the specified sequence in the current session.  NOTE: cur-
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                     ^^^^^^^^^^^^^^^
       rval will return an error if nextval has never been called
       for  the  given  sequence  in the current backend session.
       Also beware that it does not give  the  last  number  ever
       allocated, only the last one allocated by this backend.

I.e. - CURRVAL is not affected by concurrent NEXVALs...

Vadim

pgsql-sql by date:

Previous
From: Marcus Better
Date:
Subject: Serial numbers
Next
From: The Hermit Hacker
Date:
Subject: Test ...