Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 111A5632B07 for ; Thu, 14 Jan 2010 13:42:21 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 42278-07 for ; Thu, 14 Jan 2010 17:42:09 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id A1B4E634215 for ; Thu, 14 Jan 2010 13:42:08 -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 o0EHg6Bp025916; Thu, 14 Jan 2010 12:42:06 -0500 (EST) To: Andreas Kretschmer cc: pgsql-performance@postgresql.org Subject: Re: bad execution plan for subselects containing windowing-function In-reply-to: <20100114173025.GA8675@tux> References: <20100114170318.GA7634@tux> <25448.1263489336@sss.pgh.pa.us> <20100114173025.GA8675@tux> Comments: In-reply-to Andreas Kretschmer message dated "Thu, 14 Jan 2010 18:30:25 +0100" Date: Thu, 14 Jan 2010 12:42:05 -0500 Message-ID: <25915.1263490925@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.589 tagged_above=-10 required=5 tests=BAYES_00=-2.599, FH_DATE_PAST_20XX=3.188 X-Spam-Level: X-Archive-Number: 201001/290 X-Sequence-Number: 37170 Andreas Kretschmer writes: > Tom Lane wrote: >> I see no bug here. Your second query asks for a much more complicated >> computation, it's not surprising it takes longer. > But sorry, I disagree. It is the same query with the same result. I can't see > how the queries should return different results. In the first query select id, avg(value) over (partition by value) from values where id = 50 order by id; the avg() calculations are being done over only rows with id = 50. In the second query select * from (select id, avg(value) over (partition by value) from values order by id) foo where id = 50; they are being done over all rows. In this particular example you happen to get the same result, but that's just because "avg(foo) over partition by foo" is a dumb example --- it will necessarily just yield identically foo. In more realistic computations the results would be different. regards, tom lane