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.