Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 16E29D953FB for ; Thu, 10 Nov 2011 09:52:43 -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 63453-07 for ; Thu, 10 Nov 2011 13:52:26 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail-vw0-f46.google.com (mail-vw0-f46.google.com [209.85.212.46]) by mail.postgresql.org (Postfix) with ESMTP id 1B803D953FA for ; Thu, 10 Nov 2011 09:52:25 -0400 (AST) Received: by vws1 with SMTP id 1so2361569vws.19 for ; Thu, 10 Nov 2011 05:52:26 -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:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=OqdX7fMQPWUD52WB+4pNJOR22ozLOv8sJcrOJFVxCyQ=; b=s8k36zQVA5Hl+FZrpvEvTg9NWwEF/0B/uXEKWb+hojytpISuFsLNQwFYpzNNp5aqMi Q1Lw5dj+Jj7pPIGdwmJE4J8ILrhR/0RnXUhC+Cz22HAHBW1IetB1AeT1T1oJPz2nWxnc ay2/tSXyQhLnn4HAn0juTY9Bj5td04WfBTfAk= Received: by 10.52.34.177 with SMTP id a17mr12798488vdj.103.1320933145899; Thu, 10 Nov 2011 05:52:25 -0800 (PST) Received: from speedbook-air.home.jay.fm (home.jay.fm. [173.166.48.118]) by mx.google.com with ESMTPS id c7sm12247328vdh.12.2011.11.10.05.52.24 (version=SSLv3 cipher=OTHER); Thu, 10 Nov 2011 05:52:25 -0800 (PST) Message-ID: <4EBBD717.80507@gmail.com> Date: Thu, 10 Nov 2011 08:52:23 -0500 From: Jay Levitt User-Agent: Postbox 3.0.1 (Macintosh/20111102) MIME-Version: 1.0 To: Kevin Grittner CC: Merlin Moncure , pgsql-performance@postgresql.org Subject: Re: Subquery in a JOIN not getting restricted? References: <4EB84CB4.2010102@gmail.com> <4EB7FEFD0200002500042B1C@gw.wicourts.gov> <4EB80CAA0200002500042B3B@gw.wicourts.gov> <4EB86784.7080209@gmail.com> <4EB88C5A.2020704@gmail.com> <4EBA44B70200002500042BC8@gw.wicourts.gov> <4EBA8C100200002500042C23@gw.wicourts.gov> In-Reply-To: <4EBA8C100200002500042C23@gw.wicourts.gov> 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/130 X-Sequence-Number: 45641 Kevin Grittner wrote: > Merlin Moncure wrote: > >> Well, this may not fit the OP's 'real' query > > Right, if I recall correctly, the OP said it was simplified down as > far as it could be and still have the issue show. > >> but the inner subquery is probably better written as a semi-join >> (WHERE EXISTS). Kevin's right. The real query involves several SQL and PL/pgsql functions (all now inlineable), custom aggregates, a union or two and a small coyote. I could post it, but that feels like "Please write my code for me". Still, if you really want to... Meanwhile, it's good for me to learn how the planner sees my queries and how I can best state them. I assume this is me not understanding something about restrictions across group-by nodes. If the query was more like select questions.id from questions join ( select sum(u.id) from users as u group by u.id ) as s on s.id = questions.user_id where questions.id = 1; would you no longer be surprised that it scanned all user rows? I.E. is the "group by" a red herring, which usually wouldn't be present without an aggregate, and the real problem is that the planner can't restrict aggregates? This comment in planagg.c may be relevant; I'm not doing min/max, but is it still true that GROUP BY always looks at all the rows, period? void preprocess_minmax_aggregates(PlannerInfo *root, List *tlist) ... /* We don't handle GROUP BY or windowing, because our current * implementations of grouping require looking at all the rows anyway, */