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

Re: Last insert id

From: Michalis Kabrianis <mk(at)interzone(dot)gr>
To: Andrei Bintintan <klodoma(at)ar-sd(dot)net>
Cc: 'mixo' <mixo(at)coza(dot)net(dot)za>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Last insert id
Date: 2004-06-16 06:47:17
Message-ID: 40CFECF5.6060203@interzone.gr (view raw or flat)
Thread:
Lists: pgsql-sql

Andrei Bintintan wrote:
> "Is it safe to use "select max(table1_id) from table1" after the insert?"
> 
> Yes it is safe, but ONLY if you use it inside a transaction.(BEGIN/COMMIT).
> 
> BR.
> 

Hi,
I think it would be safe to use :
select currval('tablename_idname_seq');
inside a session to receive the current value of the sequence that feeds 
the serial column (i.e. the last inserted value on that session). Every 
other session is irrelevant (as the currval returns the session-specific 
value).
If I understand correctly, it doesn't even need the transaction to be 
open (in which case all consecutive inserts on the same table would block).

M.K.
> 
> -----Original Message-----
> From: pgsql-sql-owner(at)postgresql(dot)org [mailto:pgsql-sql-owner(at)postgresql(dot)org]
> On Behalf Of mixo
> Sent: Wednesday, June 09, 2004 9:24 AM
> To: pgsql-sql(at)postgresql(dot)org
> Subject: [SQL] Last insert id
> 
> I have three tables which are related a serial field, table1_id, in on of
> the tables. Updating the tables is done through a transaction. My problem
> is, once I have insert  a row in the first tables with table1_id, I need for
> the other two tables. How can I get this? Is it safe to use "select
> max(table1_id) from table1" after the insert?
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
> 
>                http://www.postgresql.org/docs/faqs/FAQ.html
> 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
> 
>                http://www.postgresql.org/docs/faqs/FAQ.html

In response to

pgsql-sql by date

Next:From: Stijn VanroyeDate: 2004-06-16 07:27:24
Subject: Re: Your question in postgresql.org forum (Diff. between two times as a numeric value in a stored proc)
Previous:From: Jean-Luc LachanceDate: 2004-06-15 20:38:21
Subject: Re: Is there a faster way to do this?

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