Skip site navigation (1) Skip section navigation (2)

Re: Last Id?

From: <me(at)alternize(dot)com>
To: "Jan Danielsson" <jan(dot)danielsson(at)gmail(dot)com>,"Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Last Id?
Date: 2005-09-06 09:32:33
Message-ID: 045101c5b2c5$e910f030$8d02a8c0@iwing (view raw or flat)
Thread:
Lists: pgsql-novice
unfortunately as i had to find out some days ago, "session-local" is a bit 
dependend on how (and when) you actually open up new db connections to the 
db, especially when you maybe have connection pooling enabled. i am now 
using the other concept as described in [1]:

-------------------
One approach is to retrieve the next SERIAL value from the sequence object 
with the nextval() function before inserting and then insert it explicitly. 
Using the example table in 4.11.1, an example in a pseudo-language would 
look like this:

    new_id = execute("SELECT nextval('person_id_seq')");
    execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise 
Pascal')");

You would then also have the new value stored in new_id for use in other 
queries (e.g., as a foreign key to the person table).
-------------------

this works fine.

cheers,
thomas

[1] http://www.postgresql.org/docs/faqs.FAQ.html#4.11.1

----- Original Message ----- 
From: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Jan Danielsson" <jan(dot)danielsson(at)gmail(dot)com>
Cc: <pgsql-novice(at)postgresql(dot)org>
Sent: Tuesday, September 06, 2005 5:51 AM
Subject: Re: [NOVICE] Last Id?


> Jan Danielsson <jan(dot)danielsson(at)gmail(dot)com> writes:
>>    I have seen a few "currval", "last_value", tricks and such, but they
>> aren't good enough, AFAICT, since they only work when I can guarantee
>> that no one else will insert a new record between the time that I insert
>> one, and query for the last_value.
>
> You evidently do not understand how currval works.  It is session-local,
> so the race condition you are imagining does not exist.
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
>               http://archives.postgresql.org
> 



In response to

Responses

pgsql-novice by date

Next:From: Pradeepkumar, Pyatalo (IE10)Date: 2005-09-06 09:39:48
Subject: FW: Libpq - multibyte character support
Previous:From: Pradeepkumar, Pyatalo (IE10)Date: 2005-09-06 06:35:08
Subject: Libpq - multibyte character support

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group