Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1drXox-00044x-Vx for pgsql-bugs@arkaria.postgresql.org; Mon, 11 Sep 2017 23:08:52 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1drXox-0006tM-HA for pgsql-bugs@arkaria.postgresql.org; Mon, 11 Sep 2017 23:08:51 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1drXox-0006tD-3f for pgsql-bugs@postgresql.org; Mon, 11 Sep 2017 23:08:51 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1drXou-0008W6-Fl for pgsql-bugs@postgresql.org; Mon, 11 Sep 2017 23:08:49 +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 v8BN8lZM009312; Mon, 11 Sep 2017 19:08:47 -0400 From: Tom Lane To: serovov@gmail.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #14811: Nested IN SUBQERY that returns empty results executed multiple times. In-reply-to: <20170911212648.25634.89444@wrigleys.postgresql.org> References: <20170911212648.25634.89444@wrigleys.postgresql.org> Comments: In-reply-to serovov@gmail.com message dated "Mon, 11 Sep 2017 21:26:48 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <9310.1505171327.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 11 Sep 2017 19:08:47 -0400 Message-ID: <9311.1505171327@sss.pgh.pa.us> 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 serovov@gmail.com writes: > I have a query planner bug that executes the same subquery multiple times. This is not a bug, it's a RFE, and not really a planner RFE either. AFAICS, the issue is that the scan on betta2zetta returns zero rows. The plan chosen for ANY(ARRAY()) happens to exploit that case successfully: Limit (cost=3D4155.01..4183.81 rows=3D6 width=3D22) (actual time=3D0.122.= .0.122 rows=3D0 loops=3D1) InitPlan 1 (returns $1) -> Nested Loop (cost=3D134.14..4154.58 rows=3D6639 width=3D4) (actua= l time=3D0.078..0.078 rows=3D0 loops=3D1) -> Seq Scan on betta2zetta (cost=3D0.00..5.75 rows=3D1 width= =3D4) (actual time=3D0.077..0.077 rows=3D0 loops=3D1) Filter: (zetta_id =3D 3001) Rows Removed by Filter: 300 -> Bitmap Heap Scan on alpha2betta (cost=3D134.14..4079.52 row= s=3D6931 width=3D8) (never executed) Recheck Cond: (betta_id =3D betta2zetta.betta_id) -> Bitmap Index Scan on alpha2betta_betta_id_idx (cost= =3D0.00..132.41 rows=3D6931 width=3D0) (never executed) Index Cond: (betta_id =3D betta2zetta.betta_id) -> Index Scan using alpha_pkey on alpha (cost=3D0.42..48.43 rows=3D10 = width=3D22) (actual time=3D0.118..0.118 rows=3D0 loops=3D1) Index Cond: (id =3D ANY ($1)) Planning time: 0.944 ms Execution time: 0.294 ms Note that alpha2betta isn't read at all, and neither is alpha because the ANY doesn't get any values to scan for. But the plan used for IN doesn't optimize this case as well: Limit (cost=3D0.85..48.91 rows=3D6 width=3D22) (actual time=3D272.964..27= 2.964 rows=3D0 loops=3D1) -> Merge Semi Join (cost=3D0.85..53179.99 rows=3D6639 width=3D22) (act= ual time=3D272.962..272.962 rows=3D0 loops=3D1) Merge Cond: (alpha.id =3D alpha2betta.alpha_id) -> Index Scan using alpha_pkey on alpha (cost=3D0.42..22654.42 r= ows=3D700000 width=3D22) (actual time=3D0.017..0.017 rows=3D1 loops=3D1) -> Nested Loop (cost=3D0.42..28694.18 rows=3D6639 width=3D4) (ac= tual time=3D272.939..272.939 rows=3D0 loops=3D1) Join Filter: (alpha2betta.betta_id =3D betta2zetta.betta_id) -> Index Only Scan using alpha2betta_alpha_id_betta_id_key = on alpha2betta (cost=3D0.42..18188.42 rows=3D700000 width=3D8) (actual tim= e=3D0.100..117.954 rows=3D700000 loops=3D1) Heap Fetches: 0 -> Materialize (cost=3D0.00..5.75 rows=3D1 width=3D4) (act= ual time=3D0.000..0.000 rows=3D0 loops=3D700000) -> Seq Scan on betta2zetta (cost=3D0.00..5.75 rows= =3D1 width=3D4) (actual time=3D0.071..0.071 rows=3D0 loops=3D1) Filter: (zetta_id =3D 3001) Rows Removed by Filter: 300 Planning time: 1.423 ms Execution time: 273.148 ms We do stop short on the alpha scan, but alpha2betta gets read to the end, looking for a joinable row that it won't ever find. The planner doesn't ever optimize on the basis that a subquery will return zero rows (unless that's provably true), and I do not think it would be wise to do so. We have enough trouble with plans being optimized on the assumption of one row out and that proving to be an underestimate. So I would not like to make it prefer the form with an initplan to the form without, even if it were to believe that there are zero rows with zetta_id =3D 3001. If it's wrong that will be a horribly bad choice, as the estimated costs indicate. We could, however, imagine optimizing this case at runtime. If nodeNestloop.c were to note that it got no rows from the inner scan on the first iteration, and that it isn't passing any nestloop parameters into the inner side, then it could plausibly assume that all future executions of the inner plan will also give zero rows, and so it can't produce any join rows [if it's a standard inner join] and it can stop scanning the outer side. In that case we'd stop the alpha2betta indexscan after the first tuple and win. Merge and hash joins both have optimizations of this ilk for empty input relations, so it's reasonable for nestloop to do so also. Now a possible objection to this argument is that if the inner side contains volatile quals or set-returning functions, it might produce some rows in a future execution even though it didn't this time. But we've already decided that we aren't making any guarantees about such behavior. In this example, we broke any chance of behaving "correctly" for such an inner scan by sticking a Material node on top of it. More generally, if the inner scan doesn't have any parameter dependency on the outer, we're free to implement the join as a merge or hash join, which would read the inner scan only once anyway. So I think that objection can be rejected. Whether this is worth doing, given that it hasn't really come up before, I'm not sure of. It would add a little bit of overhead to nestloops: we'd need at least something like "node->nl_gotInnerTuple =3D true" added to each successful iteration. That's probably negligible, but maybe not. The equivalent optimizations for merge and hash add only one-time tests, not once-per-tuple overhead, so it's hard to argue that they cost much. regards, tom lane --=20 Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs