Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 71ACF1337BCB for ; Tue, 14 Sep 2010 04:10:50 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 99530-01 for ; Tue, 14 Sep 2010 07:10:44 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og111.obsmtp.com (exprod7og111.obsmtp.com [64.18.2.175]) by mail.postgresql.org (Postfix) with SMTP id 7B9601337BCF for ; Tue, 14 Sep 2010 04:10:42 -0300 (ADT) Received: from source ([209.85.215.172]) by exprod7ob111.postini.com ([64.18.6.12]) with SMTP ID DSNKTI8f8fjLyGtT8Dla7kJmvprm6Z8rVuVx@postini.com; Tue, 14 Sep 2010 00:10:43 PDT Received: by mail-ey0-f172.google.com with SMTP id 10so4056283eyd.17 for ; Tue, 14 Sep 2010 00:10:41 -0700 (PDT) Received: by 10.14.121.141 with SMTP id r13mr3409800eeh.47.1284448241234; Tue, 14 Sep 2010 00:10:41 -0700 (PDT) Received: from [192.168.1.117] (dsl-hkibrasgw2-ff7ac300-240.dhcp.inet.fi [88.195.122.240]) by mx.google.com with ESMTPS id v59sm10597023eeh.16.2010.09.14.00.10.39 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 14 Sep 2010 00:10:40 -0700 (PDT) Message-ID: <4C8F1FED.3050902@enterprisedb.com> Date: Tue, 14 Sep 2010 10:10:37 +0300 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; U; Linux i686; fi-FI; rv:1.9.1.10) Gecko/20100619 Iceowl/1.0b1 Icedove/3.0.5 MIME-Version: 1.0 To: Tom Lane CC: Gaetano Mendola , pgsql-performance@postgreSQL.org Subject: Re: Useless sort by References: <12103.1284389088@sss.pgh.pa.us> <4C8E4982.70004@gmail.com> <14518.1284396487@sss.pgh.pa.us> In-Reply-To: <14518.1284396487@sss.pgh.pa.us> 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=-4.2 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3 X-Spam-Level: X-Archive-Number: 201009/71 X-Sequence-Number: 40274 On 13/09/10 19:48, Tom Lane wrote: > Gaetano Mendola writes: >> Of course I'm not suggesting to take away the "sort by" and give the user >> an unsorted result, I'm asking why the the optimizer in cases like: > >> select unique(a) from v_table_with_order_by; > >> doesn't takes away the "order by" inside the view and puts it back "rewriting the >> query like this: > >> select unique(a) from v_table_without_order_by >> order by a; > > That changes the order in which the rows are fed to unique(a). The > principal real-world use for a non-top-level ORDER BY is exactly to > determine the order in which rows are fed to a function, so we will > have a revolt on our hands if we break that. You could check for volatile functions. I think this could be done safely. However, it doesn't seem worthwhile, it would be a fair amount of code, and it's not usually a good idea to put an ORDER BY in a view or subquery anyway unless you also have volatile functions in there, or you want to coerce the optimizer to choose a certain plan. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com