Thread: Sequences problem

Sequences problem

From
Judith
Date:
Hello everybody, I have a question related with sequences.............

When I init a sesion with my db, if a do the next sentence:
   SELECT   currval('pagos_id_pago_seq');


return this:  ERROR: the relation doesn't exist   (or somethimg like that, because the message is in spanish)

but if I do first,
   SELECT nextval('pagos_id_pago_seq');

and then   SELECT currval('pagos_id_pago_seq');   it success, returns the 
current sequence value,  it is possible to indicate that the sequence is 
already started and in the value 4567 but I need first to now the 
current value before the next, how can I do?
   Thanks in advanced



Re: Sequences problem

From
Andrew Sullivan
Date:
On Fri, Aug 17, 2007 at 04:33:04PM -0500, Judith wrote:
> When I init a sesion with my db, if a do the next sentence:
> 
>    SELECT   currval('pagos_id_pago_seq');
> 
> 
> return this:
>   ERROR: the relation doesn't exist
>    (or somethimg like that, because the message is in spanish)

You _must_ call nextval() before a currval().  This is documented
behaviour.

A

-- 
Andrew Sullivan  | ajs@crankycanuck.ca
The whole tendency of modern prose is away from concreteness.    --George Orwell


Re: Sequences problem

From
"Scott Marlowe"
Date:
On 8/17/07, Judith <jaltamirano@correolux.com.mx> wrote:
> Hello everybody, I have a question related with sequences.............
>
> When I init a sesion with my db, if a do the next sentence:
>
>     SELECT   currval('pagos_id_pago_seq');
>
>
> return this:
>    ERROR: the relation doesn't exist
>     (or somethimg like that, because the message is in spanish)
>
> but if I do first,
>
>     SELECT nextval('pagos_id_pago_seq');
>
> and then
>     SELECT currval('pagos_id_pago_seq');   it success, returns the
> current sequence value,  it is possible to indicate that the sequence is
> already started and in the value 4567 but I need first to now the
> current value before the next, how can I do?

Sorry, that's not how sequences work.

Have you read the manual on sequence manipulation?

http://www.postgresql.org/docs/8.2/static/functions-sequence.html

Sequences are designed to allow fast access to sequentially increasing
numbers to use as identifiers in the database.  While they guarantee
no repeats (unless you set them to roll over etc...) they don't
guarantee no gaps.

so, the current value only has context if you've actually pulled a
sequence from the generator, otherwise what it is right now isn't
really important yet.

There are some functions you can use to get the current number, but
doing that is not safe from race conditions, so you should only do
that stuff while access to the db is shut off to other users.