public inbox for [email protected]
help / color / mirror / Atom feedRe: Interesting case of IMMUTABLE significantly hurting performance
2+ messages / 2 participants
[nested] [flat]
* Re: Interesting case of IMMUTABLE significantly hurting performance
@ 2025-04-10 05:33 Olleg Samoylov <[email protected]>
0 siblings, 1 reply; 2+ messages in thread
From: Olleg Samoylov @ 2025-04-10 05:33 UTC (permalink / raw)
To: Tom Lane <[email protected]>; +Cc: pgsql-generallists.postgresql.org <[email protected]>
On 10.04.2025 01:08, Tom Lane wrote:
> 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.)
> regards, tom lane
Well, to_char(bigint, text) indeed not immutable, because in some
pattern it uses get information from locale. For instance,'SLDG'
patterns. But in case of
CREATE OR REPLACE FUNCTION formatted_num_immutable(p_summa bigint)
RETURNS text
LANGUAGE sql
IMMUTABLE STRICT
RETURN ltrim(to_char(p_summa, '999 999 999 999 999 999 999 999'));
to_char do not use locale information in this pattern. So it is correct
conclude that to_char is immutable with this pattern and
formatted_num_immutable too. I did not lie to the planner.
So this is looked "strange", immutable function marked as immutable
function can not be inlined, but exactly the same function marked as
volatile do.
--
Olleg
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Interesting case of IMMUTABLE significantly hurting performance
@ 2025-04-10 14:01 David G. Johnston <[email protected]>
parent: Olleg Samoylov <[email protected]>
0 siblings, 0 replies; 2+ messages in thread
From: David G. Johnston @ 2025-04-10 14:01 UTC (permalink / raw)
To: Olleg Samoylov <[email protected]>; +Cc: Tom Lane <[email protected]>; pgsql-generallists.postgresql.org <[email protected]>
On Wednesday, April 9, 2025, Olleg Samoylov <[email protected]> wrote:
>
> On 10.04.2025 01:08, Tom Lane wrote:
>
>> 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.)
>> regards, tom lane
>>
>
> Well, to_char(bigint, text) indeed not immutable, because in some pattern
> it uses get information from locale. For instance,'SLDG' patterns. But in
> case of
>
> CREATE OR REPLACE FUNCTION formatted_num_immutable(p_summa bigint)
> RETURNS text
> LANGUAGE sql
> IMMUTABLE STRICT
> RETURN ltrim(to_char(p_summa, '999 999 999 999 999 999 999 999'));
>
> to_char do not use locale information in this pattern. So it is correct
> conclude that to_char is immutable with this pattern and
> formatted_num_immutable too. I did not lie to the planner.
>
> So this is looked "strange", immutable function marked as immutable
> function can not be inlined, but exactly the same function marked as
> volatile do.
>
>
Yeah, the inlining is an optimization, and while it seems like it could
perform more tests or maybe make slightly different/more adjustments, it
really isn’t worth the development effort or runtime cost to do so. Make
your function volatility match the most volatile function you internally
call - constant input arguments don’t change this.
There is no reason to perform number formatting immutably - function call
results involving table data are not memoized.
David J.
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2025-04-10 14:01 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-04-10 05:33 Re: Interesting case of IMMUTABLE significantly hurting performance Olleg Samoylov <[email protected]>
2025-04-10 14:01 ` David G. Johnston <[email protected]>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox