Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 1884A1337B4E for ; Sat, 23 Apr 2011 21:55:07 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 08101-03 for ; Sun, 24 Apr 2011 00:55:01 +0000 (UTC) X-Greylist: delayed 00:05:45.181437 by SQLgrey-1.7.6 Received: from mail-pw0-f46.google.com (mail-pw0-f46.google.com [209.85.160.46]) by mail.postgresql.org (Postfix) with ESMTP id 1E3821337A67 for ; Sat, 23 Apr 2011 21:54:58 -0300 (ADT) Received: by pwi15 with SMTP id 15so772906pwi.19 for ; Sat, 23 Apr 2011 17:54:58 -0700 (PDT) Received: by 10.68.47.6 with SMTP id z6mr3543744pbm.310.1303606153177; Sat, 23 Apr 2011 17:49:13 -0700 (PDT) Received: from Craig-Jamess-MacBook-Pro.local (mta-76-176-59-179.san.rr.com [76.176.59.179]) by mx.google.com with ESMTPS id m6sm733993pbl.85.2011.04.23.17.49.10 (version=SSLv3 cipher=OTHER); Sat, 23 Apr 2011 17:49:11 -0700 (PDT) Message-ID: <4DB37385.3090209@emolecules.com> Date: Sat, 23 Apr 2011 17:49:09 -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: Tom Lane CC: "pgsql-admin@postgresql.org" Subject: Re: select for update References: <4DB1BFC2.7020806@emolecules.com> <10313.1303505924@sss.pgh.pa.us> <4DB20AEC.90400@emolecules.com> <1161.1303528668@sss.pgh.pa.us> In-Reply-To: <1161.1303528668@sss.pgh.pa.us> 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/220 X-Sequence-Number: 37401 On 4/22/11 8:17 PM, Tom Lane wrote: > Craig James writes: >> On 4/22/11 1:58 PM, Tom Lane wrote: >>> Craig James writes: >>>> select objectid from archive where db_id is null limit 1 for update >>> The interaction between LIMIT and FOR UPDATE changed in 9.0 ... what >>> PG version are you using? >> 8.4.4 > Well, note what it says in the 8.4 SELECT reference page: > > Caution > > It is possible for a SELECT command using both LIMIT and FOR > UPDATE/SHARE clauses to return fewer rows than specified by > LIMIT. This is because LIMIT is applied first. The command > selects the specified number of rows, but might then block > trying to obtain a lock on one or more of them. Once the SELECT > unblocks, the row might have been deleted or updated so that it > does not meet the query WHERE condition anymore, in which case > it will not be returned. > > I think what's probably happening to you is you're getting a NULL not > because there isn't a matching row, but because somebody is updating the > first matching row underneath you and then the LIMIT prevents finding > any other matches. However, that pseudo-code is too pseudo to tell > whether this theory is correct. Thanks, it sounds like this is exactly what's happening. It happens very rarely (a few times per month), so this makes sense. I think I just need a two-step approach: $object_id = $dbh->selectrow_array("select min(objectid) from archive where db_id is null"); if ($object_id) { $db_id = $dbh->selectrow_array("select db_id from archive where objectid = $object_id for update"); ... double check that db_id is still NULL, repeat if someone else grabbed it... } > (9.0 handles these situations in a less unintuitive fashion, btw.) We'll be migrating soon, thanks. Craig