Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id DBA3465016D for ; Tue, 2 Sep 2008 06:33:52 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 69534-04 for ; Tue, 2 Sep 2008 06:33:41 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from outmail136012.authsmtp.net (outmail136012.authsmtp.net [62.13.136.12]) by postgresql.org (Postfix) with ESMTP id 31C8564FD1F for ; Tue, 2 Sep 2008 06:33:43 -0300 (ADT) Received: from punt7.authsmtp.com (punt7.authsmtp.com [62.13.128.13]) by punt3.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id m829Xe75019184 for ; Tue, 2 Sep 2008 10:33:40 +0100 (BST) Received: from mail-c189.authsmtp.com (mail-c189.authsmtp.com [62.13.128.71]) by punt7.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id m829XcfD037428; Tue, 2 Sep 2008 10:33:38 +0100 (BST) Received: from [192.168.0.2] (85-211-229-97.dyn.gotadsl.co.uk [85.211.229.97]) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id m829Xal9021856; Tue, 2 Sep 2008 10:33:36 +0100 (BST) Subject: Re: WIP Join Removal From: Simon Riggs To: Heikki Linnakangas Cc: List pgsql-patches In-Reply-To: <48BC4127.30706@enterprisedb.com> References: <1220176372.4371.118.camel@ebony.2ndQuadrant> <48BC4127.30706@enterprisedb.com> Content-Type: text/plain Date: Tue, 02 Sep 2008 10:41:27 +0100 Message-Id: <1220348487.4371.307.camel@ebony.2ndQuadrant> Mime-Version: 1.0 X-Mailer: Evolution 2.12.0 Content-Transfer-Encoding: 7bit X-Server-Quench: 37d73450-78d2-11dd-bc7a-001f29070be2 X-AuthRoute: OCdxZQATClZOTQEd DAteCiNZVAwpPBRK HVkIKg5MJUcNSQVJ NksadBtFaAdbflxB HGQLW1xEUFR7WmB/ aAwfZQBDYEtPQQdo WVZARU1QEQdtHhxP WxYcW3EjKwxGen94 YEAsXHddWwp5chJ6 QUpRFXABbTNidWEe BBRFJVBTIR5Kfh4T aFl/U3NfNGdJBC9q VzASB3gTOjRZYB5Y RwYTJFZ6 X-Authentic-SMTP: 61633235383639.kestrel.dmpriest.net.uk:1186/Kp X-Report-SPAM: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-Virus-Status: No virus detected - but ensure you scan with your own anti-virus system! X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200809/5 X-Sequence-Number: 7020 On Mon, 2008-09-01 at 22:23 +0300, Heikki Linnakangas wrote: > Simon Riggs wrote: > > Patch works, but there's a bit I haven't finished yet - checking unique > > indexes. > > Did plan invalidation make it safe to rely on the presence of a unique > index for planning decisions? My understanding was "Yes" and this case was the specific reason I originally wanted to pursue plan invalidation back in 2006. > Couldn't we also do join removal for inner joins, when there's a foreign > key reference that enforces that there's one and only one matching tuple > in the removed table: > > SELECT child.data FROM child, parent WHERE child.fkey = parent.pkey Hmm, I had thought this was the same case, but the inner join possibility wasn't something I'd seen. Guess that flaw shows this is all original thought - I'll go back and read that optimizer blog again... I agree it will work. We would need to replace the join condition with an alteration of the original quals on child so that we add "AND child.fkey is not null". Which would mean we would need to re-plan the access to that base relation so we picked up the new qual and potentially used an index for it as well. That would be possible only if the join condition exactly matches the FK constraint. Hmm, will think about that, its certainly not an easy addition, for me. I'll concentrate on getting this patch finished and committed first. > > + /* > > + * We can now remove join by pulling up child plan from the keeprel. > > + * This needs to be done considering costs, since its possible for > > + * a nested inner indexscan plan to be cheaper. So it isn't > > + * always desirable to remove the join. > > Can you elaborate that a bit? I can't imagine a case where we wouldn't > want to remove a join, when we know we can. Neither could I when I first looked at this. It turns out that a join like this select a.col2 from a left outer join b on a.col1 = b.col1 where b.col2 = 1; can be cheaper if we don't remove the join, when there is an index on a.col1 and b.col2, because the presence of b allows the values returned from b to be used for an index scan on a. -- Simon Riggs www.2ndQuadrant.com PostgreSQL Training, Services and Support