Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id BBCD020B4DA for ; Mon, 7 Nov 2011 17:41:29 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 41695-08 for ; Mon, 7 Nov 2011 21:41:20 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 3274620B4D9 for ; Mon, 7 Nov 2011 17:41:19 -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 pA7LfJ99003430; Mon, 7 Nov 2011 16:41:19 -0500 (EST) To: Jay Levitt cc: pgsql-performance@postgresql.org Subject: Re: Subquery in a JOIN not getting restricted? In-reply-to: <4EB84CB4.2010102@gmail.com> References: <4EB84CB4.2010102@gmail.com> Comments: In-reply-to Jay Levitt message dated "Mon, 07 Nov 2011 16:25:08 -0500" Date: Mon, 07 Nov 2011 16:41:18 -0500 Message-ID: <3429.1320702078@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-3.101 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-1.201 X-Spam-Level: X-Archive-Number: 201111/100 X-Sequence-Number: 45611 Jay Levitt writes: > When I run the following query: > select questions.id > from questions > join ( > select u.id as user_id > from users as u > left join scores as s > on s.user_id = u.id > ) as subquery > on subquery.user_id = questions.user_id; > the subquery is scanning my entire user table, even though it's restricted > by the outer query. (My real subquery is much more complicated, of course, > but this is the minimal fail case.) > Is this just not a thing the optimizer can do? Every release since 8.2 has been able to reorder joins in a query written that way. Probably it just thinks it's cheaper than the alternatives. (Unless you've reduced the collapse_limit variables for some reason?) regards, tom lane