Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 73E92633707 for ; Thu, 14 Jan 2010 13:03:33 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 20999-09 for ; Thu, 14 Jan 2010 17:03:22 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from vs137086.vserver.de (static-ip-62-75-137-86.inaddr.intergenia.de [62.75.137.86]) by mail.postgresql.org (Postfix) with ESMTP id 7770D633DA1 for ; Thu, 14 Jan 2010 13:03:22 -0400 (AST) Received: (qmail 7409 invoked from network); 14 Jan 2010 18:03:20 +0100 Received: from p54b3a04d.dip0.t-ipconnect.de (HELO a-kretschmer.de) (84.179.160.77) by static-ip-62-75-137-86.inaddr.intergenia.de with (AES256-SHA encrypted) SMTP; 14 Jan 2010 18:03:19 +0100 Received: from kretschmer by a-kretschmer.de with local (Exim 4.69) (envelope-from ) id 1NVT6M-00025s-En for pgsql-performance@postgresql.org; Thu, 14 Jan 2010 18:03:18 +0100 Date: Thu, 14 Jan 2010 18:03:18 +0100 From: Andreas Kretschmer To: pgsql-performance@postgresql.org Subject: bad execution plan for subselects containing windowing-function Message-ID: <20100114170318.GA7634@tux> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-GPG-Fingerprint: EE16 3C01 7B9C 10F7 2C8B 3B86 4DB3 D9EE 7F45 84DA X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! X-Lugdd: Gerd Kube X-Info: My name is root. Just root. And I am licensed to kill -9 User-Agent: Mutt/1.5.18 (2008-05-17) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.962 tagged_above=-10 required=5 tests=AWL=0.637, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201001/287 X-Sequence-Number: 37167 Hi, version: 8.4.2 I have a table called values: test=*# \d values Table "public.values" Column | Type | Modifiers --------+---------+----------- id | integer | value | real | Indexes: "idx_id" btree (id) The table contains 100000 random rows and is analysed. And i have 2 queries, both returns the same result: test=*# explain analyse select id, avg(value) over (partition by value) from values where id = 50 order by id; QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------- WindowAgg (cost=531.12..549.02 rows=1023 width=8) (actual time=2.032..4.165 rows=942 loops=1) -> Sort (cost=531.12..533.68 rows=1023 width=8) (actual time=2.021..2.270 rows=942 loops=1) Sort Key: value Sort Method: quicksort Memory: 53kB -> Bitmap Heap Scan on "values" (cost=24.19..479.98 rows=1023 width=8) (actual time=0.269..1.167 rows=942 loops=1) Recheck Cond: (id = 50) -> Bitmap Index Scan on idx_id (cost=0.00..23.93 rows=1023 width=0) (actual time=0.202..0.202 rows=942 loops=1) Index Cond: (id = 50) Total runtime: 4.454 ms (9 rows) Time: 4.859 ms test=*# explain analyse select * from (select id, avg(value) over (partition by value) from values order by id) foo where id = 50; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------- Subquery Scan foo (cost=22539.64..24039.64 rows=500 width=12) (actual time=677.196..722.975 rows=942 loops=1) Filter: (foo.id = 50) -> Sort (cost=22539.64..22789.64 rows=100000 width=8) (actual time=631.991..690.411 rows=100000 loops=1) Sort Key: "values".id Sort Method: external merge Disk: 2528kB -> WindowAgg (cost=11116.32..12866.32 rows=100000 width=8) (actual time=207.462..479.330 rows=100000 loops=1) -> Sort (cost=11116.32..11366.32 rows=100000 width=8) (actual time=207.442..281.546 rows=100000 loops=1) Sort Key: "values".value Sort Method: external merge Disk: 1752kB -> Seq Scan on "values" (cost=0.00..1443.00 rows=100000 width=8) (actual time=0.010..29.742 rows=100000 loops=1) Total runtime: 725.362 ms (11 rows) No question, this is a silly query, but the problem is the 2nd query: it is obviously not possible for the planner to put the where-condition into the subquery. That's bad if i want to create a view: test=*# create view view_values as select id, avg(value) over (partition by value) from values order by id; CREATE VIEW Time: 41.280 ms test=*# commit; COMMIT Time: 0.514 ms test=# explain analyse select * from view_values where id=50; It is the same bad plan with the Seq Scan on "values". Is this a bug or PEBKAC or something else? Andreas -- Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect. (Linus Torvalds) "If I was god, I would recompile penguin with --enable-fly." (unknown) Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°