Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id CAD959FB80A for ; Sat, 4 Aug 2007 16:33:42 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 39035-10 for ; Sat, 4 Aug 2007 16:33:38 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from floppy.pyrenet.fr (news.pyrenet.fr [194.116.145.2]) by postgresql.org (Postfix) with ESMTP id 0E29D9FB7A6 for ; Sat, 4 Aug 2007 16:33:42 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id 1417431440; Sat, 4 Aug 2007 21:33:41 +0200 (MET DST) Date: Sat, 04 Aug 2007 15:33:39 -0400 From: Lew User-Agent: Thunderbird 2.0.0.5 (X11/20070719) MIME-Version: 1.0 X-Newsgroups: pgsql.sql Subject: Re: Race condition in resetting a sequence References: <20070803165058.020509FBF2F@postgresql.org> <46b381f2.0b86460a.3a8c.5460SMTPIN_ADDED@mx.google.com> <20070803221508.2EF819FB37F@postgresql.org> <5298.1186185715@sss.pgh.pa.us> In-Reply-To: <5298.1186185715@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 39 X-Usenet-Provider: http://www.giganews.com X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.35 To: pgsql-sql@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/64 X-Sequence-Number: 29002 Steve Midgley writes: >> The code I provided to reset a primary key sequence is actually part of >> Ruby on Rails core library - actually they use something very similar >> to what I originally sent: ... >> SELECT setval('#{sequence}', (SELECT COALESCE(MAX(#{pk})+(SELECT >> increment_by FROM #{sequence}), (SELECT min_value FROM #{sequence})) >> FROM #{table}), false) Tom Lane wrote: > Ugh. That's completely unsafe/broken, unless they also use locking that > you didn't show. ... > It doesn't have a race condition "all by itself": it will do what it's > told. The problem with commands such as the above is that there's a > time window between calculating the max() and executing the setval(), > and that window is more than large enough to allow someone else to > insert a row that invalidates your max() computation. (Because of MVCC > snapshotting, the risk window is in fact as long as the entire > calculation of the max --- it's not just a few instructions as some > might naively think.) > > Now it is possible to make this brute-force approach safe: you can lock > the table against all other modifications until you've applied your own > changes. But you pay a high price in loss of concurrency if you do > that. All this trouble over semantically-significant ID columns seems to support the camp that excoriates use of artificial ID columns and autoincrementation altogether. The usual argument in their favor is that they speed up performance, but this epicyclic dance to accomodate FK references to autoincremented keys makes the case that there is also a performance penalty, and in the more critical performance area of code development and correctness than in the less critical search speed area. -- Lew