Re: going crazy with serial type - Mailing list pgsql-general

From Fran Fabrizio
Subject Re: going crazy with serial type
Date
Msg-id 3C59B37B.1050800@mmrd.com
Whole thread Raw
In response to Re: going crazy with serial type  (Cindy <ctmoore@uci.edu>)
List pgsql-general
Cindy wrote:

>OK, next question.  I'm trying to use nextval/currval and I'm getting
>this:
>
>search_info=# select currval('state_vectors_id_seq');
>ERROR:  state_vectors_id_seq.currval is not yet defined in this session
>search_info=# select id from state_vectors;
> id
>----
>  1
>(1 row)
>
>
>shouldn't the first select have returned 1?  The state_vectors_id_seq
>
Nope.  currval() is per session, so if you haven't called nextval()
during the current connection, you'll see exactly what you saw.

test=# create sequence testseq;
CREATE
test=# select currval('testseq');
ERROR:  testseq.currval is not yet defined in this session
test=# select nextval('testseq');
 nextval
---------
       1
(1 row)

test=# select currval('testseq');
 currval
---------
       1
(1 row)

test=# \q

$ psql test

Welcome to psql, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help on internal slash commands
       \g or terminate with semicolon to execute query
       \q to quit

test=# select currval('testseq');
ERROR:  testseq.currval is not yet defined in this session
test=#

-Fran


pgsql-general by date:

Previous
From: Ellen Cyran
Date:
Subject: Re: Function to Pivot data
Next
From: Tom Lane
Date:
Subject: Re: going crazy with serial type