Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id E811B63329A for ; Wed, 10 Mar 2010 18:37:33 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 82575-04 for ; Wed, 10 Mar 2010 22:37:18 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 2A63C6359C9 for ; Wed, 10 Mar 2010 18:37:23 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o2AMbKi6021593; Wed, 10 Mar 2010 17:37:20 -0500 (EST) To: Robert Haas cc: Yeb Havinga , sverhagen@wps-nl.com, pgsql-performance@postgresql.org Subject: Re: Strange workaround for slow query In-reply-to: <603c8f071003101355r155a3078p7962360d0167895d@mail.gmail.com> References: <4B976229.7030407@gmail.com> <4B977045.6040604@gmail.com> <4B977CD3.5030402@gmail.com> <603c8f071003101355r155a3078p7962360d0167895d@mail.gmail.com> Comments: In-reply-to Robert Haas message dated "Wed, 10 Mar 2010 16:55:32 -0500" Date: Wed, 10 Mar 2010 17:37:20 -0500 Message-ID: <21592.1268260640@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.455 tagged_above=-10 required=5 tests=AWL=0.144, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201003/118 X-Sequence-Number: 37943 Robert Haas writes: > It does seem like once the materialize step is done we could notice > that the tuplestore is empty and, given that uses no outer variables > or parameters and therefore will never be re-executed, we could skip > the rest of the index scan. Yeah, the same thing occurred to me while looking at this example. Right now, nodeNestloop is not really aware of whether the inner scan depends on any parameters from the outer scan, so it's a bit hard to determine whether the join can be abandoned. However, I have 9.1 plans to change that --- I'd like to get rid of the current pass-the-outer-tuple-to-ReScan hack entirely, in favor of having nodeNestloop explicitly set PARAM_EXEC parameters for the inner scan. Once that's in, it would be pretty easy to apply this optimization. (I've added a note to my private TODO file about it.) Another possible objection is that if the inner scan has any volatile functions in its quals, it might yield a different result upon rescan even without parameters. However, since we are already willing to stick a Materialize node on it at the whim of the planner, I don't see how such a short-circuit in the executor would make things any worse. regards, tom lane