Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id CEF0C1337BF2 for ; Tue, 15 Mar 2011 05:04:48 -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 90623-02 for ; Tue, 15 Mar 2011 08:04:39 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.csc.lt (mail.csc.lt [193.138.191.5]) by mail.postgresql.org (Postfix) with ESMTP id 255341337BF0 for ; Tue, 15 Mar 2011 05:04:35 -0300 (ADT) Received: (qmail 21438 invoked by uid 210); 15 Mar 2011 08:04:34 -0000 Received: from mail.nsoft.lt by mail (envelope-from , uid 201) with qmail-scanner-2.05st (clamdscan: 0.97/12838. spamassassin: 3.2.1. perlscan: 2.05st. Clear:RC:1(77.240.249.53):. Processed in 0.01109 secs); 15 Mar 2011 08:04:34 -0000 Received: from mail.nsoft.lt (77.240.249.53) by mail.csc.lt with SMTP; 15 Mar 2011 08:04:34 -0000 Received: from [192.168.16.113] (unknown [77.240.249.53]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by mail.nsoft.lt (Postfix) with ESMTPS id 51FAC8E073 for ; Tue, 15 Mar 2011 10:04:32 +0200 (EET) Message-ID: <4D7F1D8F.2080005@nsoft.lt> Date: Tue, 15 Mar 2011 10:04:31 +0200 From: Julius Tuskenis Organization: UAB nSoft User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; lt-LT; rv:1.9.2.15) Gecko/20110303 Lightning/1.0b2 Thunderbird/3.1.9 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: unexpected stable function behavior References: <4D78FB98.7090609@nsoft.lt> <4D7DD5CF.4080502@nsoft.lt> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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 X-Spam-Level: X-Archive-Number: 201103/150 X-Sequence-Number: 42829 Hello, 2011.03.14 15:41, Merlin Moncure rašė: > WITH results as > ( > SELECT distinct price_id as price_id > FROM ticket_price > JOIN ticket_has_ticket_price ON (price_id = thtp_price_id) > WHERE price_event_id = 7820 and (current_timestamp>= price_date AND > current_timestamp<= price_date_till) > ) as qq > ) > SELECT web_select_extra_price(price_id, 7820, 1) from results; > Thank you Merlin for your help. I have updated my function to use CTE. Although there was no performance improvement (I had the select with function using distinct values joined earlyer) it's good to know the optimizer will not change the way I want the query to be executed. Thank you once again. > CTEs are great btw, I'd start learning them immediately. I am going to do that. > IMNSHO, this (uncontrolled number of function executions when run via > field select list) is a common gotcha w/postgres and a FAQ. Also the > documentation is not very helpful on this point... Yes, I totally agree with you. I think sentence like "Although function is marked as STABLE or IMMUTABLE the optimizer is not obliged to take advantage of these properties." (sorry for my English). > do you agree CTE is the right way to advise handling this problem...is it worth further > notation? Yes, the CTE worked fine for me. Reading some more on this topic I found some comments that the optimizer has no possibility to know how many times the function is to be called in such queries (without actually executing the query), so there is no way to determine the cost. That explains why not the optimal plan was chosen to my query. -- Julius Tuskenis Programavimo skyriaus vadovas UAB nSoft mob. +37068233050