RE: nextval(), and serial updates

From: "Culberson, Philip" <philip(dot)culberson(at)dat(dot)com>
To: "'Sampath, Krishna'" <KSampath(at)ekmail(dot)com>, pgsql-general <pgsql-general(at)postgreSQL(dot)org>
Subject: RE: nextval(), and serial updates
Date: 2000-04-04 15:59:04
Message-ID: A95EFC3B707BD311986C00A0C9E95B6A9DE602@datmail03.dat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Krishna,

In your example, by calling the "nextval()" function, you have just
incremented the sequence. Every call to "nextval('sequence_name')", by
definition, increments sequence_name by one so, again referring to your
example, the next call to "currval()" correctly returns 2.

Phil Culberson
DAT Services

-----Original Message-----
From: Sampath, Krishna [mailto:KSampath(at)ekmail(dot)com]
Sent: Tuesday, April 04, 2000 7:32 AM
To: pgsql-general
Subject: [GENERAL] nextval(), and serial updates

Normally, currval() points to the index sequence for the last inserted
record.

However, after I call
select nextval('i_seq'::text)
on the serial data tyep i_seq, the counter gets incremented by 1. The next
INSERT skips a number in the sequence. Is this by design? I was a bit
surprized because the currval() gets incremented also, and you no longer
point to the last inserted record.

puzzled,
krishna

concrete example:
create table t1 ( i serial, s varchar(20) ); -- create table
insert into t1 (s) values ('first line'); -- insert 1 record
select * from t1; -- gives (1
row)
select currval('t1_i_seq'); -- gives 1 as
currval .. ok
select nextval('t1_i_seq'); -- gives 2 as
nextval .. ok so far
select currval('t1_i_seq'); -- gives 2 as
currval ... should this be 1?
insert into t1 (s) values ('second line'); -- inserts second line,
serial id set to 3
select * from t1; -- (2 rows)
i is now 3, should be 2

Browse pgsql-general by date

  From Date Subject
Next Message Michael S. Kelly 2000-04-04 16:07:37 RE: The "nl" RPMs.
Previous Message davidb 2000-04-04 15:13:53 Re: Fetching record of the day