Re: Interesting case of IMMUTABLE significantly hurting performance - Mailing list pgsql-general

From Tom Lane
Subject Re: Interesting case of IMMUTABLE significantly hurting performance
Date
Msg-id 373227.1744236488@sss.pgh.pa.us
Whole thread Raw
In response to Re: Interesting case of IMMUTABLE significantly hurting performance  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: Interesting case of IMMUTABLE significantly hurting performance
Re: Interesting case of IMMUTABLE significantly hurting performance
List pgsql-general
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Wed, Apr 9, 2025 at 1:50 PM Laurenz Albe <laurenz.albe@cybertec.at>
> wrote:
>> The IMMUTABLE function cannot be inlined because to_char() is not
>> IMMUTABLE.

> So, the punishment for lying about the volatility of one's function is to
> prohibit it from being inlined even in a case where had you been truthful
> about the volatility it would have been inlined.

Yeah.  The assumption is that you had a reason for marking the
function IMMUTABLE and you want the planner to treat it that way
even if it isn't really.  (There are valid use-cases for that, for
instance if you want calls to the function to be constant-folded.)

So we don't inline the function --- if we did, the merely-stable
contained expression would be exposed and then treated as STABLE.
But that comes at a pretty substantial cost, since the
SQL-language-function executor isn't exactly free.

If you err in the other direction, you don't get slapped on the
wrist that way.  We're willing to inline VOLATILE functions,
for instance, whether or not the contained expression is volatile.
Similarly for STRICT, and I think parallel safety as well.
So my own habit when writing a SQL function that I wish to be
inlined is to leave off all those markings.  They won't matter
if the function is successfully inlined, and they might get in
the way of that happening.

            regards, tom lane



pgsql-general by date:

Previous
From: "Abraham, Danny"
Date:
Subject: RE: [EXTERNAL] Re: Cannot connect local with ttrust (pg_upgrade assumes it)
Next
From: Amitabh Kant
Date:
Subject: Re: timescaledb vs NULL vs pg_timeseries vs partman + pgcron + pg_ivm