pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michalis Kabrianis <mk@interzone.gr>
To: Andrei Bintintan <klodoma@ar-sd.net>
Cc: mixo <mixo@coza.net.za>
Cc: pgsql-sql@postgresql.org
Subject: Re: Last insert id
Date: Wed, 16 Jun 2004 09:47:17 +0300
Message-ID: <40CFECF5.6060203@interzone.gr> (raw)
In-Reply-To: <20040615070408.80EA4175A8@ar-sd.net>
References: <20040615070408.80EA4175A8@ar-sd.net>



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@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
> On Behalf Of mixo
> Sent: Wednesday, June 09, 2004 9:24 AM
> To: pgsql-sql@postgresql.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



view thread (6+ messages)

Message-ID: <40CFECF5.6060203@interzone.gr>
Permalink:  ../40CFECF5.6060203@interzone.gr/
Also on:    postgresql.org/message-id/40CFECF5.6060203@interzone.gr

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mk@interzone.gr, klodoma@ar-sd.net, mixo@coza.net.za
  Subject: Re: Last insert id
  In-Reply-To: <40CFECF5.6060203@interzone.gr>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox