Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 590281337BAE for ; Thu, 11 Nov 2010 19:56:43 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 52677-03 for ; Thu, 11 Nov 2010 23:56:36 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pz0-f46.google.com (mail-pz0-f46.google.com [209.85.210.46]) by mail.postgresql.org (Postfix) with ESMTP id EA1761337B94 for ; Thu, 11 Nov 2010 19:56:35 -0400 (AST) Received: by pzk28 with SMTP id 28so512104pzk.19 for ; Thu, 11 Nov 2010 15:56:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:subject:mime-version :content-type:from:in-reply-to:date:cc:content-transfer-encoding :message-id:references:to:x-mailer; bh=ZHxdP4MALq3qRKPWl9Rv7LII6nwXMxjvc/BraM7fVjE=; b=nly6QgYrPxBMesUn0qArWNrdTy5YeFtnVCXd3yJwo/V079gmFJ/1XPoPEbkSpByLeU BzEi3o1n47vShfa3E84/S5WgoLvkIqJAAHMIKHlI5cQHqi8mdFwefCqXobyPVkqxDpuO scxOpp9rmzZ68hOgk89eN/MOq7aa0x8PBDUJo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; b=rw6AqOUGVFW31Kfs/wURpiM2Cjc8Rp3kDZuyIwGQOb/ddkUIkHoXqq5dlgp81oe3az NNvsF6d4dEnsolBuDFagD+htl5BCyYQIRvmGzRpQ8Nw32/4WcSGGsMFR/W8cuJIJ/6Oy /yT0dUK51JhfW8E/3pt6es1AcMCgvB3lUOk7Y= Received: by 10.142.165.10 with SMTP id n10mr1300150wfe.70.1289519795478; Thu, 11 Nov 2010 15:56:35 -0800 (PST) Received: from [192.168.1.102] (adsl-71-138-129-224.dsl.pltn13.pacbell.net [71.138.129.224]) by mx.google.com with ESMTPS id e14sm3015409wfg.20.2010.11.11.15.56.33 (version=TLSv1/SSLv3 cipher=RC4-MD5); Thu, 11 Nov 2010 15:56:34 -0800 (PST) Subject: Re: equivalent queries lead to different query plans for self-joins with group by? Mime-Version: 1.0 (Apple Message framework v1081) Content-Type: text/plain; charset=us-ascii From: Ben In-Reply-To: <23147.1289515069@sss.pgh.pa.us> Date: Thu, 11 Nov 2010 15:56:31 -0800 Cc: pgsql-performance@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <5E705CB3-DF3D-4804-8F87-984D2041C9AC@gmail.com> References: <5D061DAC-31E2-4C47-AE3B-7799118C57D3@gmail.com> <23147.1289515069@sss.pgh.pa.us> To: Tom Lane X-Mailer: Apple Mail (2.1081) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201011/177 X-Sequence-Number: 41201 appreciate the instant response. > Well, arguably it's not doing the right thing either way --- you'd = sort > of like the inequalities to get pushed down into both of the join > inputs, not just one of them. PG doesn't make that deduction though; > it can make such inferences for equalities, but inequalities are not > optimized as much. in my work i have replaced the query with a sql function + window : create or replace function bar(timestamp, timestamp) returns setof foo language 'sql' as $$ select ts, id, val - (avg(val) over (partition by ts)) as val from foo where ts > $1 and ts < $2 $$; i was forced to use a sql function as opposed to a view because the = query planner was unable to push down restrictions on ts inside the view = subquery, which i've manually done in the function. indeed, explain select ts, id, val - (avg(val) over (partition by ts)) as val = from foo where ts > '2009-10-20' and ts < '2009-10-21'; and explain select * from (select ts, id, val - (avg(val) over (partition by = ts)) as val from foo) as f where ts > '2009-10-20' and ts < = '2009-10-21'; give different answers, despite being equivalent, but i understand it is = hard to push things into subqueries in general. in this case it is only = legal because we partition by ts. thanks again for the explanations! best, ben