Re: [GENERAL] The value returned by autoinc ?

From: Herouth Maoz <herouth(at)oumail(dot)openu(dot)ac(dot)il>
To: Silvio Emanuel Barbosa de Macedo <smacedo(at)inescn(dot)pt>, pgsql general mailing list <pgsql-general(at)postgreSQL(dot)org>
Subject: Re: [GENERAL] The value returned by autoinc ?
Date: 1999-03-15 09:51:39
Message-ID: l03110702b31288079b29@[147.233.159.109]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

At 2:57 +0200 on 15/3/99, Silvio Emanuel Barbosa de Macedo wrote:

> When I insert data into a table with a sequence associated to a column
> (and the required trigger), how can I know the value the sequence has
> just generated ? (think in parallel accesses)
>
> Would this approach be the answer ?
> begin work
> insert...
> select max...
> commit

No, this approach is a waste of precious time... The correct approach is:

INSERT...
SELECT currval( 'seq_name' );

currval gives you the last value the sequence has given to the current
session. That is, it won't work if you use it before the insertion (because
the sequence didn't give you a number yet). It will also give you the
correct number even if between the INSERT and the SELECT, another process
or another connection also made an insert.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Silvio Emanuel Barbosa de Macedo 1999-03-15 09:59:28 Re: [GENERAL] The value returned by autoinc ?
Previous Message Bernhard Krick 1999-03-15 09:20:47 how to start postmaster at system boot on linux?