Referencing serial col's sequence for insert

From: Anil Menon <gakmenon(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Referencing serial col's sequence for insert
Date: 2014-07-21 02:00:12
Message-ID: CAHzbRKeftnspi8dPJPP9A0ktPMuKHkb3GPA=3hHbtJ8n45WPqw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Torsten Förtsch 2014-07-21 04:08:33 Re: check database integrity
Previous Message Adrian Klaver 2014-07-20 18:54:16 Re: Copying a database.