Re: Fetching generated keys

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Heikki Linnakangas <heikki(at)enterprisedb(dot)com>
Cc: Mike Clements <mclement(at)progress(dot)com>, PostgreSQL JDBC List <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Fetching generated keys
Date: 2007-03-05 22:48:01
Message-ID: 23790.1173134881@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Heikki Linnakangas <heikki(at)enterprisedb(dot)com> writes:
> 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 meant "currval". In any case the point is that these are
session-local and so Mike's concern is unfounded.

regards, tom lane

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Mike Clements 2007-03-05 23:09:32 Re: Fetching generated keys
Previous Message Mark Lewis 2007-03-05 22:28:36 Re: Fetching generated keys