X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 327AE9FB1D4 for ; Fri, 7 Jul 2006 15:49:55 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 64876-06 for ; Fri, 7 Jul 2006 15:49:45 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from exchange.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by postgresql.org (Postfix) with ESMTP id 677CF9FB1D0 for ; Fri, 7 Jul 2006 15:49:45 -0300 (ADT) Received: from 10.10.1.37 ([10.10.1.37]) by exchange.g2switchworks.com ([10.10.1.2]) with Microsoft Exchange Server HTTP-DAV ; Fri, 7 Jul 2006 18:49:41 +0000 Received: from state.g2switchworks.com by mail.g2switchworks.com; 07 Jul 2006 13:49:41 -0500 Subject: Re: Atomar SQL Statement From: Scott Marlowe To: Stephan Szabo Cc: Michael Glaesemann , "Weber, Johann (ISS Kassel)" , pgsql-sql@postgresql.org In-Reply-To: <20060707105847.J77811@megazone.bigpanda.com> References: <279E27B5-183F-4520-BC43-A726A50646EF@seespotcode.net> <20060707105847.J77811@megazone.bigpanda.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1152298181.22269.30.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Fri, 07 Jul 2006 13:49:41 -0500 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/111 X-Sequence-Number: 25685 On Fri, 2006-07-07 at 13:07, Stephan Szabo wrote: > On Fri, 7 Jul 2006, Michael Glaesemann wrote: > > > > > On Jul 7, 2006, at 7:55 , Weber, Johann (ISS Kassel) wrote: > > > > > My concern: in a multi threaded environment, can a second thread > > > interrupt this statement and eventually insert the same email > > > address in > > > the table with a different id? Or is this statement atomar? > > > > You're safe. Take a look at the FAQ entries on SERIAL: > > > > http://www.postgresql.org/docs/faqs.FAQ.html#item4.11.2 > > I don't think he is, because I don't think the issue is the SERIAL > behavior, but instead the NOT EXISTS behavior. Won't the NOT EXISTS in > read committed potentially be true for both concurrent sessions if the > second happens before the first commits, which then would mean that both > sessions will go on to attempt the insert (with their own respective ids > from the serial)? Without a unique constraint on email I think he can end > up with the same email address with two different ids. Yep, this is a possible race condition, if memory serves, and this is the reason for unique indexes. That way, should another transaction manage to sneak in between the two parts of this query, the unique index will still keep your data coherent.