Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u2tz0-007XN3-B7 for pgsql-general@arkaria.postgresql.org; Thu, 10 Apr 2025 15:38:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1u2tyy-00DLbZ-3K for pgsql-general@arkaria.postgresql.org; Thu, 10 Apr 2025 15:38:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u2tyx-00DLa4-NT for pgsql-general@lists.postgresql.org; Thu, 10 Apr 2025 15:38:24 +0000 Received: from fhigh-a2-smtp.messagingengine.com ([103.168.172.153]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u2tyv-004XuX-2d for pgsql-general@lists.postgresql.org; Thu, 10 Apr 2025 15:38:23 +0000 Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfhigh.phl.internal (Postfix) with ESMTP id 787C8114015B; Thu, 10 Apr 2025 11:38:20 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Thu, 10 Apr 2025 11:38:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1744299500; x=1744385900; bh=4OOk/f3kDSSbRI00iUOCnIqLinZGV9a39TqflcDMfaM=; b= c/nyZQblkwoJtitiIQ1HYw6bwTRiHKudKEbsyuZuowLZ+htbf5ms0vlvjok5FvPg Lk04JvAI/Uhf0b87oCTXSAXNUs6WMmk499zjlzV3EjmxmxyOxp7SZWs5vnFTvfpP LUCIWK80Kjm2oPBXVqFPcUDDYPNRbb2476w7zfQigUkM8etP9EVJ6wzZdzNYZQ8V sqJGFCd6pJFYcO+To4kzLlFNc3NEMLJAKpvYyGdNNah5PL9J4CfCmY3pbSncH6Sx ucDSDBEs31l9DMCiSpnWkwJb45loepk2kcdk/Mj6QSYfh0y+fjFzKf8pOrRY7xk6 9x0/ByWajxHvdPOmHExE/w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1744299500; x= 1744385900; bh=4OOk/f3kDSSbRI00iUOCnIqLinZGV9a39TqflcDMfaM=; b=u fmEKf5NqY0+7ka+Y+YSW/a6LpD4uTXlUvMTdaRDkG3l0UnZk1hQ9tkn4lqS0TgCk /37b96lijciqZ0daOK8bWpjRXg4OThkmInw0K76uaGTbcqHXtY2t9/LVV5hGvqkf tpa11Cirz4fbZXv9fmEz1yS7BIYmsAh9DICNJG1VX+XWpgsGUvJsVeHaI2A6MU3S 1OzaAT35wJeA/Fztc1Iu7kd2jYCQWgP0jno94yHgbC4pOlIAFdDTnF0nNFLInaDi 7NBT6/vnqeyIGvUIi1QYWQ6ANzu78YIE279qClUM+iocg7KOPaZzY9ZmfDUXsfXA 4tsq/PiG/d31cPhvOBUfw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdelfedtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih gvnhhtshculddquddttddmnecujfgurhepkfffgggfuffvvehfhfgjtgfgsehtkeertddt vdejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvh gvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepfeegfeeiuedtgffg teeggfehkeejheetieeliefgteeikeejvdeiveeigfehvedtnecuvehluhhsthgvrhfuih iivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhes rghklhgrvhgvrhdrtghomhdpnhgspghrtghpthhtohepfedpmhhouggvpehsmhhtphhouh htpdhrtghpthhtohepshhplhgrrhhvseihrgdrrhhupdhrtghpthhtohepthhglhesshhs shdrphhghhdrphgrrdhushdprhgtphhtthhopehpghhsqhhlqdhgvghnvghrrghlsehlih hsthhsrdhpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 10 Apr 2025 11:38:19 -0400 (EDT) Message-ID: <2efb8ad0-7f1f-43b3-80e1-fdd0a7f182ca@aklaver.com> Date: Thu, 10 Apr 2025 08:38:18 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Interesting case of IMMUTABLE significantly hurting performance To: Olleg Samoylov , Tom Lane Cc: "pgsql-generallists.postgresql.org" References: <662101e2-5ca5-4bf5-9307-2519efde8690@ya.ru> <8687df7d49bc4b28b41c7cf4e9ad1e15d383b2fa.camel@cybertec.at> <373227.1744236488@sss.pgh.pa.us> <379a1e13-c775-4fb5-a444-b41308996cc4@ya.ru> Content-Language: en-US From: Adrian Klaver In-Reply-To: <379a1e13-c775-4fb5-a444-b41308996cc4@ya.ru> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 4/9/25 22:33, Olleg Samoylov 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. I doubt the code actually looks at the template, it just does the lookup on pg_proc and sees: proname | provolatile | prosrc ---------+-------------+--------------------- to_char | s | int8_to_char > > So this is looked "strange", immutable function marked as immutable > function can not be inlined, but exactly the same function marked as > volatile do. -- Adrian Klaver adrian.klaver@aklaver.com