Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vn5sl-0007Bl-4i for pgsql-bugs@arkaria.postgresql.org; Sun, 01 Dec 2013 12:12:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Vn5sk-00033t-Jc for pgsql-bugs@arkaria.postgresql.org; Sun, 01 Dec 2013 12:12:14 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vn5sj-00032K-3l for pgsql-bugs@postgresql.org; Sun, 01 Dec 2013 12:12:13 +0000 Received: from mail.anarazel.de ([217.115.131.40]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vn5sg-0000Oq-O1 for pgsql-bugs@postgresql.org; Sun, 01 Dec 2013 12:12:12 +0000 Received: from intern.anarazel.de (p54AF4AA8.dip0.t-ipconnect.de [84.175.74.168]) (Authenticated sender: andres@anarazel.de) by mail.anarazel.de (Postfix) with ESMTPSA id BCD334E8003; Sun, 1 Dec 2013 13:12:09 +0100 (CET) Date: Sun, 1 Dec 2013 13:12:09 +0100 From: Andres Freund To: Oliver Seemann Cc: pgsql-bugs@postgresql.org Subject: Re: Update with subselect sometimes returns wrong result Message-ID: <20131201121209.GH18793@alap2.anarazel.de> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Hi, On 2013-11-30 00:08:14 +0100, Oliver Seemann wrote: > Then the following UPDATE should return exactly one row: > > UPDATE t1 SET id = t1.id > FROM (SELECT id FROM t1 LIMIT 1 FOR UPDATE) AS subset > WHERE t1.id = subset.id > RETURNING t1.id It turns out, this currently (as Tom points out) is a question of how the query is planned. UPDATEs with a FROM essentially are a join between the involved tables. Roughly, this query can either be planned as a) Scan all rows in subset, check whether it matches a row in t1. or b) Scan all rows in t1, check for each whether it matches a row in subset. a) is perfectly fine for what you want, it will only return one row. But b) is problematic since it will execute the subselect multiple times, once for each row in t1. "FOR locklevel" currently has the property of ignoring rows that the current command has modified, so you'll always get a different row back... To get rid of that ambiguity, I suggest rewriting the query to look like: WITH locked_row AS ( SELECT id FROM t1 LIMIT 1 FOR UPDATE ) UPDATE t1 SET id = t1.id FROM (SELECT * FROM locked_row) locked WHERE t1.id = locked.id RETURNING t1.id; that should always be safe and indeed, I cannot reproduce the problem that way. Greetings, Andres Freund -- Andres Freund http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs