Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vn0iQ-0003H2-5j for pgsql-bugs@arkaria.postgresql.org; Sun, 01 Dec 2013 06:41:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Vn0iP-0006DA-Gf for pgsql-bugs@arkaria.postgresql.org; Sun, 01 Dec 2013 06:41:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vn0iN-0006D1-JM for pgsql-bugs@postgresql.org; Sun, 01 Dec 2013 06:41:11 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vn0iK-00050h-VR for pgsql-bugs@postgresql.org; Sun, 01 Dec 2013 06:41:11 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id rB16f2wK022221; Sun, 1 Dec 2013 01:41:02 -0500 From: Tom Lane To: Andres Freund cc: Oliver Seemann , pgsql-bugs@postgresql.org Subject: Re: Update with subselect sometimes returns wrong result In-reply-to: <20131130214018.GJ31100@awork2.anarazel.de> References: <25601.1385834264@sss.pgh.pa.us> <20131130214018.GJ31100@awork2.anarazel.de> Comments: In-reply-to Andres Freund message dated "Sat, 30 Nov 2013 22:40:18 +0100" Date: Sun, 01 Dec 2013 01:41:02 -0500 Message-ID: <22220.1385880062@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) 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 Andres Freund writes: > Slightly hacked up (probably python 2 only) version of the test script > attached. Ah, deleting and reinserting the rows each time like that makes it a lot more reproducible. I don't have the full story but it's got something to do with screwed-up tuple info flags. The plan that's generated looks like Update on t1 -> Nested Loop Join Filter: (t1.id = subset.id) -> Seq Scan on t1 -> Subquery Scan on subset -> Limit -> LockRows -> Seq Scan on t1 t1_1 so the subquery scan will be executed twice, once for each row in t1. The first time through, heap_lock_tuple looks at the first tuple and locks it. It doesn't get called on the second tuple because the LIMIT node stops evaluation. But in the second scan, heap_lock_tuple returns HeapTupleSelfUpdated for the first tuple, so ExecLockRows ignores it, moves on to the second tuple, and returns that. The LIMIT is happy cause it just got one row back; it doesn't know it's not the same row as before. And this row of course passes the join qual with the second row from the outer scan of t1, so we perform a second update. Now, the thing about this is that the tuple heap_lock_tuple is rejecting in the second pass is the one that we just updated, up at the ModifyTable plan node. So I can't find it exactly surprising that it says HeapTupleSelfUpdated. But tracing through tqual.c shows that the tuple has got the HEAP_XMAX_IS_MULTI bit set, which might be thought a bit peculiar. There's not multiple transactions involved. Anyway, at this point I'm not so much wondering why it fails as why it (seems to) work *any* of the time. And how is it that VACUUM sometimes manages to flip it from working state to not-working state? (Once you're in the state where the UPDATE will say it updated two rows, it's 100% reproducible.) Anyway, it seems pretty clear that the explanation is down somewhere in the tuple visibility and multixact logic that you and Alvaro have been hacking on with such vim lately. I'm out of steam for tonight, over to you ... regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs