pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dave Cramer <pg@fastcrypt.com>
To: Heikki Linnakangas <heikki@enterprisedb.com>
Cc: Mike Clements <mclement@progress.com>
Cc: PostgreSQL JDBC List <pgsql-jdbc@postgresql.org>
Subject: Re: Fetching generated keys
Date: Mon, 5 Mar 2007 16:50:47 -0500
Message-ID: <6BAEEF62-D85B-4049-8AD8-2BC660B60629@fastcrypt.com> (raw)
In-Reply-To: <45EC86E2.8020506@enterprisedb.com>
References: <626C0646ACE5D544BC9675C1FB81846B3388B1@MAIL03.bedford.progress.com>
	<45EC86E2.8020506@enterprisedb.com>


On 5-Mar-07, at 4:08 PM, Heikki Linnakangas wrote:

> Mike Clements wrote:
>> Up to now, we were selecting the current value of the sequence
>> immediately after the insert. I thought this was safe because
>> transactions should be isolated. But now I realize this could
>> potentially fail because the default transaction isolation is "read
>> committed". Thus if another connection inserts into the same table,
>> causing the sequence to increment, if it commits before we read the
>> sequence value, we might read the wrong value (the value as  
>> incremented
>> by the other transaction, not the value as it was for our own  
>> insert).
>
> You should use the lastval-function. It retrieves the last value  
> returned in your connection. "SELECT lastval('sequence')"
>
I think you should be using currval( 'sequence_name') in the same  
connection. It will not be the wrong one! It does the "right thing".   
I believe lastval does not allow you to specify the sequence.

Dave
> -- 
>   Heikki Linnakangas
>   EnterpriseDB   http://www.enterprisedb.com
>
> ---------------------------(end of  
> broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
>




view thread (19+ messages)  latest in thread

Message-ID: <6BAEEF62-D85B-4049-8AD8-2BC660B60629@fastcrypt.com>
Permalink:  ../6BAEEF62-D85B-4049-8AD8-2BC660B60629@fastcrypt.com/
Also on:    postgresql.org/message-id/6BAEEF62-D85B-4049-8AD8-2BC660B60629@fastcrypt.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: pg@fastcrypt.com, heikki@enterprisedb.com, mclement@progress.com
  Subject: Re: Fetching generated keys
  In-Reply-To: <6BAEEF62-D85B-4049-8AD8-2BC660B60629@fastcrypt.com>
  References: <626C0646ACE5D544BC9675C1FB81846B3388B1@MAIL03.bedford.progress.com> <45EC86E2.8020506@enterprisedb.com> <6BAEEF62-D85B-4049-8AD8-2BC660B60629@fastcrypt.com>

* 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