Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id A2E2F1337B9C for ; Fri, 22 Apr 2011 15:44:17 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 91578-08 for ; Fri, 22 Apr 2011 18:43:56 +0000 (UTC) X-Greylist: delayed 00:53:57.842433 by SQLgrey-1.7.6 Received: from mail-pv0-f174.google.com (mail-pv0-f174.google.com [74.125.83.174]) by mail.postgresql.org (Postfix) with ESMTP id B19691337B4E for ; Fri, 22 Apr 2011 15:43:55 -0300 (ADT) Received: by pvg12 with SMTP id 12so419980pvg.19 for ; Fri, 22 Apr 2011 11:43:54 -0700 (PDT) Received: by 10.68.5.38 with SMTP id p6mr1906962pbp.332.1303494596791; Fri, 22 Apr 2011 10:49:56 -0700 (PDT) Received: from Craig-Jamess-MacBook-Pro.local (wsip-70-167-122-86.sd.sd.cox.net [70.167.122.86]) by mx.google.com with ESMTPS id h2sm2122760pbi.50.2011.04.22.10.49.55 (version=SSLv3 cipher=OTHER); Fri, 22 Apr 2011 10:49:55 -0700 (PDT) Message-ID: <4DB1BFC2.7020806@emolecules.com> Date: Fri, 22 Apr 2011 10:49:54 -0700 From: Craig James User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 MIME-Version: 1.0 To: "pgsql-admin@postgresql.org" Subject: select for update Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201104/211 X-Sequence-Number: 37392 I thought I understood "select ... for update," but maybe not. We have a number of separate databases and a unique integer identifier that's supposed to be global across all databases. A single "archive" database is used to issue the next available ID when a process wants to create a new object. The sequence of operations goes like this (pseudo-code): /* start a transaction * begin; /* see if an objectid has been returned for re-use */ select objectid from archive where db_id is null limit 1 for update /* no ID available? issue a new one */ if (objectid is null) new_id = select nextval('object_id_sequence') insert into archive(objectid, db_id) values('new_id', 'new_id') /* ID available? claim it */ else update archive set db_id = this_db_id where objectid commit The problem is that very occasionally the same ID will be issued twice. I don't see how this can be. Doesn't the "for update" guarantee that no other process can claim that same row? Thanks, Craig