Re: currval, lastval, nextvar? - Mailing list pgsql-novice

From Tom Lane
Subject Re: currval, lastval, nextvar?
Date
Msg-id 28852.1240502828@sss.pgh.pa.us
Whole thread Raw
In response to Re: currval, lastval, nextvar?  ("A. Kretschmer" <andreas.kretschmer@schollglas.com>)
Responses Re: currval, lastval, nextvar?  (Andreas Kretschmer <akretschmer@spamfence.net>)
List pgsql-novice
"A. Kretschmer" <andreas.kretschmer@schollglas.com> writes:
> Yes. You don't need call nextval.

Well, you do, but the point is that the default expression for a
serial column includes the nextval() call.  Look at the table with
\d, eg

postgres=# create table t1(id serial primary key);
NOTICE:  CREATE TABLE will create implicit sequence "t1_id_seq" for serial column "t1.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1"
CREATE TABLE
postgres=# \d t1
                         Table "public.t1"
 Column |  Type   |                    Modifiers
--------+---------+-------------------------------------------------
 id     | integer | not null default nextval('t1_id_seq'::regclass)
Indexes:
    "t1_pkey" PRIMARY KEY, btree (id)

postgres=#

So if you do an insert that doesn't provide a value for that column,
the nextval is done implicitly.

            regards, tom lane

pgsql-novice by date:

Previous
From: "A. Kretschmer"
Date:
Subject: Re: currval, lastval, nextvar?
Next
From: Andreas Kretschmer
Date:
Subject: Re: currval, lastval, nextvar?