From jkanter@virginia.edu Mon Jun 16 15:16:41 2003 X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 4C3DA30EE9A for ; Mon, 16 Jun 2003 15:16:41 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 40432-08 for ; Mon, 16 Jun 2003 12:16:30 -0300 (ADT) Received: from mail.virginia.edu (mail.Virginia.EDU [128.143.2.9]) by svr1.postgresql.org (Postfix) with SMTP id 539B630ED1B for ; Mon, 16 Jun 2003 12:16:30 -0300 (ADT) Received: from smtp.mail.virginia.edu by mail.virginia.edu id aa17220; 16 Jun 2003 11:16 EDT Received: from virginia.edu (d-128-19-222.bootp.Virginia.EDU [128.143.19.222]) by smtp.mail.Virginia.EDU (8.11.7/8.11.6) with ESMTP id h5GFGRl34398 for ; Mon, 16 Jun 2003 11:16:28 -0400 Message-ID: <3EEDE12C.30401@virginia.edu> Date: Mon, 16 Jun 2003 11:24:28 -0400 From: Jodi Kanter User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.4) Gecko/20011128 Netscape6/6.2.1 X-Accept-Language: en-us MIME-Version: 1.0 To: Postgres Admin List Subject: psql sequence question Content-Type: multipart/alternative; boundary="------------020404070500040702090507" X-Archive-Number: 200306/166 X-Sequence-Number: 9157 --------------020404070500040702090507 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit If I'm using transactions (not autocommit), are sequences atomic? In other words, after inserting a record to a table that uses sequence A, am I guaranteed that select last_value on sequence A is atomic, and cannot be interfered with by other transactions using that same sequence? Sequence A is used by several tables. Thanks, Jodi - _______________________________ Jodi L Kanter BioInformatics Database Administrator University of Virginia (434) 924-2846 jkanter@virginia.edu --------------020404070500040702090507 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit
If I'm using transactions (not autocommit), are sequences atomic?

In other words, after inserting a record to a table that 
uses sequence A, am I guaranteed that select last_value on 
sequence A is atomic, and cannot be interfered with by other 
transactions using that same sequence? Sequence A is used by
several tables.

Thanks,
Jodi
-
________________________________

_______________________________
Jodi L Kanter
BioInformatics Database Administrator
University of Virginia
(434) 924-2846
jkanter@virginia.edu


 

 

 

--------------020404070500040702090507-- From bruno@wolff.to Mon Jun 16 16:01:26 2003 X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 8E2CE30ED60 for ; Mon, 16 Jun 2003 16:01:26 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 45525-10 for ; Mon, 16 Jun 2003 13:01:16 -0300 (ADT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 95AC230ECC9 for ; Mon, 16 Jun 2003 13:01:15 -0300 (ADT) Received: (qmail 29457 invoked by uid 500); 16 Jun 2003 16:04:10 -0000 Date: Mon, 16 Jun 2003 11:04:10 -0500 From: Bruno Wolff III To: Jodi Kanter Cc: Postgres Admin List Subject: Re: psql sequence question Message-ID: <20030616160410.GA29057@wolff.to> Mail-Followup-To: Jodi Kanter , Postgres Admin List References: <3EEDE12C.30401@virginia.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3EEDE12C.30401@virginia.edu> User-Agent: Mutt/1.5.4i X-Archive-Number: 200306/170 X-Sequence-Number: 9161 On Mon, Jun 16, 2003 at 11:24:28 -0400, Jodi Kanter wrote: > If I'm using transactions (not autocommit), are sequences atomic? Yes. > In other words, after inserting a record to a table that > uses sequence A, am I guaranteed that select last_value on > sequence A is atomic, and cannot be interfered with by other > transactions using that same sequence? Sequence A is used by > several tables. Probably not in the way you mean. Every transaction is going to see a consistant view of the sequence table. However transactions proceeding in parallel may seem the same value for the last value. To make this work you would need to use serializable mode to do any updates based on the value of the last value or lock the table exclusively to prevent concurrent updates. This defeats the function of sequences providing unique values using light weight locking. The right way to use sequences is to use nextval to get new values and use currval to reuse the value you got from the latest call to nextval in the same session. From ernest@vogelsinger.at Mon Jun 16 18:26:55 2003 X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 6EF4130DF3E for ; Mon, 16 Jun 2003 18:26:55 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 37039-07 for ; Mon, 16 Jun 2003 15:26:45 -0300 (ADT) Received: from relay11.austria.eu.net (relay11.Austria.eu.net [193.154.160.115]) by svr1.postgresql.org (Postfix) with ESMTP id 157AE30DBE5 for ; Mon, 16 Jun 2003 15:26:44 -0300 (ADT) Received: from sisyphus.vogelsinger.at (vogelsinger.at [193.154.189.26]) by relay11.austria.eu.net (8.12.9/8.12.0.Beta10) with ESMTP id h5GIQfc8007775; Mon, 16 Jun 2003 20:26:42 +0200 Message-Id: <5.1.1.6.2.20030616202213.03ce7be0@mail.vogelsinger.at> X-Sender: homebird@mail.vogelsinger.at X-Mailer: QUALCOMM Windows Eudora Version 5.1.1 Date: Mon, 16 Jun 2003 20:26:37 +0200 To: Jodi Kanter From: Ernest E Vogelsinger Subject: Re: psql sequence question Cc: Postgres Admin List In-Reply-To: <3EEDE12C.30401@virginia.edu> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Archive-Number: 200306/177 X-Sequence-Number: 9168 At 17:24 16.06.2003, Jodi Kanter said: --------------------[snip]-------------------- >If I'm using transactions (not autocommit), are sequences atomic? Yes >In other words, after inserting a record to a table that >uses sequence A, am I guaranteed that select last_value on >sequence A is atomic, and cannot be interfered with by other >transactions using that same sequence? Sequence A is used by >several tables. Yes. But you should use currval('seq_name') since this guarantees to return the last value returned to the current connection. Check the recent posts - there was a discussion on just this topic. -- >O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/ From jkanter@virginia.edu Wed Jun 18 13:41:56 2003 X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 099D830DBD7 for ; Wed, 18 Jun 2003 13:41:56 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 48900-10 for ; Wed, 18 Jun 2003 10:41:45 -0300 (ADT) Received: from mail.virginia.edu (mail.Virginia.EDU [128.143.2.9]) by svr1.postgresql.org (Postfix) with SMTP id 3B6A930DBF6 for ; Wed, 18 Jun 2003 10:41:43 -0300 (ADT) Received: from smtp.mail.virginia.edu by mail.virginia.edu id aa26757; 18 Jun 2003 9:41 EDT Received: from virginia.edu (d-128-19-222.bootp.Virginia.EDU [128.143.19.222]) by smtp.mail.Virginia.EDU (8.11.7/8.11.6) with ESMTP id h5IDfgl36350; Wed, 18 Jun 2003 09:41:42 -0400 Message-ID: <3EF06E06.7010509@virginia.edu> Date: Wed, 18 Jun 2003 09:49:58 -0400 From: Jodi Kanter User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.4) Gecko/20011128 Netscape6/6.2.1 X-Accept-Language: en-us MIME-Version: 1.0 To: Bruno Wolff III Cc: Postgres Admin List Subject: Re: psql sequence question References: <3EEDE12C.30401@virginia.edu> <20030616160410.GA29057@wolff.to> Content-Type: multipart/alternative; boundary="------------010709070908030804080506" X-Archive-Number: 200306/219 X-Sequence-Number: 9210 --------------010709070908030804080506 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit so it sounds like I just need to tell my programmer friend to change his use of last value to currval instead? I just hate silly mistakes like this! Thanks for the help. Jodi Bruno Wolff III wrote: >On Mon, Jun 16, 2003 at 11:24:28 -0400, > Jodi Kanter wrote: > >>If I'm using transactions (not autocommit), are sequences atomic? >> > >Yes. > >>In other words, after inserting a record to a table that >>uses sequence A, am I guaranteed that select last_value on >>sequence A is atomic, and cannot be interfered with by other >>transactions using that same sequence? Sequence A is used by >>several tables. >> > >Probably not in the way you mean. Every transaction is going to see >a consistant view of the sequence table. However transactions proceeding >in parallel may seem the same value for the last value. To make this >work you would need to use serializable mode to do any updates based >on the value of the last value or lock the table exclusively to prevent >concurrent updates. This defeats the function of sequences providing >unique values using light weight locking. > >The right way to use sequences is to use nextval to get new values >and use currval to reuse the value you got from the latest call to >nextval in the same session. > >---------------------------(end of broadcast)--------------------------- >TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org > -- _______________________________ Jodi L Kanter BioInformatics Database Administrator University of Virginia (434) 924-2846 jkanter@virginia.edu --------------010709070908030804080506 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit so it sounds like I just need to tell my programmer friend to change his use of last value to currval instead?
I just hate silly mistakes like this! Thanks for the help.
Jodi


Bruno Wolff III wrote:
On Mon, Jun 16, 2003 at 11:24:28 -0400,
Jodi Kanter <jkanter@virginia.edu> wrote:
If I'm using transactions (not autocommit), are sequences atomic?

Yes.

In other words, after inserting a record to a table that 
uses sequence A, am I guaranteed that select last_value on
sequence A is atomic, and cannot be interfered with by other
transactions using that same sequence? Sequence A is used by
several tables.

Probably not in the way you mean. Every transaction is going to see
a consistant view of the sequence table. However transactions proceeding
in parallel may seem the same value for the last value. To make this
work you would need to use serializable mode to do any updates based
on the value of the last value or lock the table exclusively to prevent
concurrent updates. This defeats the function of sequences providing
unique values using light weight locking.

The right way to use sequences is to use nextval to get new values
and use currval to reuse the value you got from the latest call to
nextval in the same session.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

--
________________________________

_______________________________
Jodi L Kanter
BioInformatics Database Administrator
University of Virginia
(434) 924-2846
jkanter@virginia.edu


 

 

 


--------------010709070908030804080506-- From bruno@wolff.to Wed Jun 18 14:28:23 2003 X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id A5CBF30D193 for ; Wed, 18 Jun 2003 14:28:23 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 63253-04 for ; Wed, 18 Jun 2003 11:28:13 -0300 (ADT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id D047230D185 for ; Wed, 18 Jun 2003 11:28:12 -0300 (ADT) Received: (qmail 20414 invoked by uid 500); 18 Jun 2003 14:31:14 -0000 Date: Wed, 18 Jun 2003 09:31:14 -0500 From: Bruno Wolff III To: Jodi Kanter Cc: Postgres Admin List Subject: Re: psql sequence question Message-ID: <20030618143114.GA20256@wolff.to> Mail-Followup-To: Jodi Kanter , Postgres Admin List References: <3EEDE12C.30401@virginia.edu> <20030616160410.GA29057@wolff.to> <3EF06E06.7010509@virginia.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3EF06E06.7010509@virginia.edu> User-Agent: Mutt/1.5.4i X-Archive-Number: 200306/224 X-Sequence-Number: 9215 On Wed, Jun 18, 2003 at 09:49:58 -0400, Jodi Kanter wrote: > so it sounds like I just need to tell my programmer friend to change his > use of last value to currval instead? This is most likely what you want to do.