Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cTz7d-0005SE-QA for pgsql-performance@arkaria.postgresql.org; Wed, 18 Jan 2017 22:54:29 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cTz7c-0004wB-Pi for pgsql-performance@arkaria.postgresql.org; Wed, 18 Jan 2017 22:54:28 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cTz7c-0004w2-4i for pgsql-performance@postgresql.org; Wed, 18 Jan 2017 22:54:28 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cTz7Z-00028b-KV for pgsql-performance@postgresql.org; Wed, 18 Jan 2017 22:54:26 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v0IMs9SF014965; Wed, 18 Jan 2017 17:54:09 -0500 From: Tom Lane To: Karl Czajkowski cc: pgsql-performance@postgresql.org Subject: Re: optimizing immutable vs. stable function calls? In-reply-to: <20170118223633.GC23081@moraine.isi.edu> References: <20170118223633.GC23081@moraine.isi.edu> Comments: In-reply-to Karl Czajkowski message dated "Wed, 18 Jan 2017 14:36:33 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <14963.1484780049.1@sss.pgh.pa.us> Date: Wed, 18 Jan 2017 17:54:09 -0500 Message-ID: <14964.1484780049@sss.pgh.pa.us> X-Pg-Spam-Score: -5.1 (-----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Karl Czajkowski writes: > The query planner does not seem to > recognize that it can eliminate redundant calls to a STABLE function. No, it doesn't. > In my case, the function call does not take any arguments and is thus > trivially independent of row data, and appears in a WHERE clause being > compared to constants. Why wouldn't the optimizer treat this case the > same as IMMUTABLE? "The same as IMMUTABLE" would be to reduce the function to a constant at plan time, which would be the wrong thing. It would be valid to execute it only once at query start, but there's no built-in mechanism for that. But you could force it by putting it in a sub-SELECT, that is if you don't like the performance of SELECT ... slow_stable_function() ... try this: SELECT ... (SELECT slow_stable_function()) ... That works because it's an uncorrelated sub-query, which gets evaluated just once per run. But the overhead associated with that mechanism is high enough that forcing it automatically for every stable function would be a loser. I'd recommend doing it only where it *really* matters. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance