Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cTzZv-0006zv-Bm for pgsql-performance@arkaria.postgresql.org; Wed, 18 Jan 2017 23:23:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cTzZu-00087v-VJ for pgsql-performance@arkaria.postgresql.org; Wed, 18 Jan 2017 23:23:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cTzZu-00087f-BD for pgsql-performance@postgresql.org; Wed, 18 Jan 2017 23:23:42 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cTzZq-0003YH-QB for pgsql-performance@postgresql.org; Wed, 18 Jan 2017 23:23:41 +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 v0INNQt4016159; Wed, 18 Jan 2017 18:23:26 -0500 From: Tom Lane To: "David G. Johnston" cc: Karl Czajkowski , "pgsql-performance@postgresql.org" Subject: Re: optimizing immutable vs. stable function calls? In-reply-to: References: <20170118223633.GC23081@moraine.isi.edu> <14964.1484780049@sss.pgh.pa.us> Comments: In-reply-to "David G. Johnston" message dated "Wed, 18 Jan 2017 16:06:54 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <16157.1484781806.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Wed, 18 Jan 2017 18:23:26 -0500 Message-ID: <16158.1484781806@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 "David G. Johnston" writes: > ​I'm feeling a bit dense here but even after having read a number of these > kinds of interchanges I still can't get it to stick. I think part of the > problem is this sentence from the docs: > https://www.postgresql.org/docs/current/static/xfunc-volatility.html > (Stable): "​This category allows the optimizer to optimize multiple calls > of the function to a single call" > I read that sentence (and the surrounding paragraph) and wonder why then > doesn't it do so in this case. It says "allows", it doesn't say "requires". The reason we have this category is that without it, it would be formally invalid to optimize an expression involving a non-immutable function into an index comparison value, because in that context the function is indeed only evaluated once (before the comparison value is fed into the index machinery). But there isn't a mechanism for that behavior outside of index scans. > If PostgreSQL cannot execute it only once at query start then all this talk > about optimization seems misleading. At worse there should be an sentence > explaining when the optimizations noted in that paragraph cannot occur - > and probably examples of both as well since its not clear when it can occur. If you want an exact definition of when things will happen or not happen, start reading the source code. I'm loath to document small optimizer details since they change all the time. 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