Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eJuDQ-0007IN-Lq for pgsql-performance@arkaria.postgresql.org; Wed, 29 Nov 2017 04:43:20 +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 1eJuDQ-0000Lz-2L for pgsql-performance@arkaria.postgresql.org; Wed, 29 Nov 2017 04:43:20 +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 1eJuDP-0000Lq-Rn for pgsql-performance@lists.postgresql.org; Wed, 29 Nov 2017 04:43:19 +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 1eJuDN-0004aw-8Z for pgsql-performance@lists.postgresql.org; Wed, 29 Nov 2017 04:43:19 +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 vAT4hEEo004238; Tue, 28 Nov 2017 23:43:14 -0500 From: Tom Lane To: Alex Reece cc: pgsql-performance@lists.postgresql.org Subject: Re: Bad plan chosen for union all In-reply-to: References: Comments: In-reply-to Alex Reece message dated "Wed, 29 Nov 2017 03:39:22 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4236.1511930594.1@sss.pgh.pa.us> Date: Tue, 28 Nov 2017 23:43:14 -0500 Message-ID: <4237.1511930594@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alex Reece writes: > I managed to reduce my test case: the following query does not take > advantage of the index on contribution metrics. Yeah. What you're wishing is that the planner would push a join condition down into a subquery, but it won't do that at present. Doing so would require generating "parameterized paths" for subqueries. While I do not think there's any fundamental technical reason anymore that we couldn't do so, there's considerable risk of wasting a lot of planner cycles chasing unprofitable plan alternatives. Anyway it was totally impractical before 9.6's upper-planner-pathification changes, and not all of the dust has settled from that rewrite. > But I expected it to be equivalent to the plan from this query: The difference here is that, from the perspective of the outer query, the WHERE condition is a restriction clause on the "cim" relation, not a join clause. So it will get pushed down into the subquery without creating any join order constraints on the outer query. regards, tom lane