Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ensd8-0001i7-DQ for pgsql-bugs@arkaria.postgresql.org; Mon, 19 Feb 2018 21:05:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ensd6-0007bY-Sb for pgsql-bugs@arkaria.postgresql.org; Mon, 19 Feb 2018 21:05:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ensd6-0007bO-N4 for pgsql-bugs@lists.postgresql.org; Mon, 19 Feb 2018 21:05:44 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ensd4-0000PU-85 for pgsql-bugs@postgresql.org; Mon, 19 Feb 2018 21:05:44 +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 w1JL5dnL002126; Mon, 19 Feb 2018 16:05:39 -0500 From: Tom Lane To: Marko Tiikkaja cc: Andrei Gorita , Pg Bugs Subject: Re: [BUGS] BUG #14870: wrong query results when using WITH with UPDATE In-reply-to: <7584.1519069396@sss.pgh.pa.us> References: <20171024155358.1471.82377@wrigleys.postgresql.org> <7584.1519069396@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Mon, 19 Feb 2018 14:43:16 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2124.1519074339.1@sss.pgh.pa.us> Date: Mon, 19 Feb 2018 16:05:39 -0500 Message-ID: <2125.1519074339@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I wrote: > The answer turns out to be that if EPQ is fired, then we instantiate a new > copy of the whole outer plan tree (though apparently not the InitPlan), > resulting in an additional ExecInitCteScan call that allocates an > additional read pointer on the same tuplestore, and it's that one being > the active one that causes the observed misbehavior. Oh, on closer inspection I had that backwards: the EPQ plan tree would normally contain only the child node(s) of the ModifyTable node, but EvalPlanQualStart also duplicates all InitPlan or SubPlan node trees, because it can't tell which of them might get used within the EPQ subtree. So we get one extra ExecInitCteScan call, even though no actual access to the tuplestore could happen. So we can now characterize the problem case as "CTE is read within an InitPlan or SubPlan, and the query as a whole encounters an EPQ recheck". regards, tom lane