Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id B36CE20B4D9 for ; Mon, 7 Nov 2011 17:25:19 -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 37973-03 for ; Mon, 7 Nov 2011 21:25:13 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail-vx0-f174.google.com (mail-vx0-f174.google.com [209.85.220.174]) by mail.postgresql.org (Postfix) with ESMTP id 162F020B4DA for ; Mon, 7 Nov 2011 17:25:12 -0400 (AST) Received: by vcbf1 with SMTP id f1so2524346vcb.19 for ; Mon, 07 Nov 2011 13:25:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=Ghajf85QFqcNQLhCADZRJR5hEKfdXObeqYA6CY5D1/w=; b=LT5Bs5aBxV6c7SM8TlICHrOd+RVVq67sRvCLpWurMRLIwH1ed7PJDmMw9wbF82+cBM Nsw4OzkHXA2i/mhqGuUBScHfe2nSRNo5k4VnuDpSL9LSXUh0Zt2z8yL/o8N2f62IXYCk 4e23YgY+jQDQEnwj9eYr8j8KlEtKl7n7lsGRw= Received: by 10.220.175.134 with SMTP id ba6mr2127225vcb.61.1320701112283; Mon, 07 Nov 2011 13:25:12 -0800 (PST) Received: from jay.local (c-24-63-250-125.hsd1.ma.comcast.net. [24.63.250.125]) by mx.google.com with ESMTPS id ey9sm31946900vdc.19.2011.11.07.13.25.11 (version=SSLv3 cipher=OTHER); Mon, 07 Nov 2011 13:25:11 -0800 (PST) Message-ID: <4EB84CB4.2010102@gmail.com> Date: Mon, 07 Nov 2011 16:25:08 -0500 From: Jay Levitt User-Agent: Postbox 3.0.1 (Macintosh/20111102) MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Subquery in a JOIN not getting restricted? Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001 X-Spam-Level: X-Archive-Number: 201111/99 X-Sequence-Number: 45610 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? Are there ways to rewrite this, still as a subquery, that will be smart enough to only produce the one row of subquery that matches questions.user_id? Jay Levitt