Thread: Serial numbers

Serial numbers

From
Marcus Better
Date:
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?

Marcus

Re: [SQL] Serial numbers

From
Vadim Mikheev
Date:
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

Re: [SQL] Serial numbers

From
Marcus Better
Date:
 >        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-

Ah. Missed that part :) Thanks.

Marcus