Referencing serial col's sequence for insert - Mailing list pgsql-general

From Anil Menon
Subject Referencing serial col's sequence for insert
Date
Msg-id CAHzbRKeftnspi8dPJPP9A0ktPMuKHkb3GPA=3hHbtJ8n45WPqw@mail.gmail.com
Whole thread Raw
Responses Re: Referencing serial col's sequence for insert  (rob stone <floriparob@gmail.com>)
List pgsql-general
Hi,

I have a question on the right/correct practice on using the serial col's sequence for insert.

Best way of explanation is by an example:

create table id01 (col1 serial, col2 varchar(10));

insert into id01(col2) values ( 'data'||currval('id01_col1_seq')::varchar);

while I do get what I want:

select  * from id01;
 col1 | col2
------+-------
    1 |
data1

Is this guaranteed to work : I am assuming that an insert triggers the id01_col1_seq's nextval first hence using
id01_col1_seq's currval subsequently will have the "correct" / expected value (and not the previous value before the insert).

Is my assumption correct?

Thanks in advance,
AK


pgsql-general by date:

Previous
From: Jakub Jindra
Date:
Subject: Re: giving a user permission to kill their processes only
Next
From: Sergiy Zuban
Date:
Subject: postgres_fdw - IS TRUE/FALSE conditions are not pushed down