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 1u2dah-003bsg-0X for pgsql-general@arkaria.postgresql.org; Wed, 09 Apr 2025 22:08:15 +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 1u2daf-007keB-As for pgsql-general@arkaria.postgresql.org; Wed, 09 Apr 2025 22:08:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u2dae-007kdv-Rv for pgsql-general@lists.postgresql.org; Wed, 09 Apr 2025 22:08:13 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u2dad-003wzu-17 for pgsql-general@lists.postgresql.org; Wed, 09 Apr 2025 22:08:12 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 539M88SK373228; Wed, 9 Apr 2025 18:08:08 -0400 From: Tom Lane To: "David G. Johnston" cc: Laurenz Albe , Olleg Samoylov , "pgsql-generallists.postgresql.org" Subject: Re: Interesting case of IMMUTABLE significantly hurting performance In-reply-to: References: <662101e2-5ca5-4bf5-9307-2519efde8690@ya.ru> <8687df7d49bc4b28b41c7cf4e9ad1e15d383b2fa.camel@cybertec.at> Comments: In-reply-to "David G. Johnston" message dated "Wed, 09 Apr 2025 14:06:12 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <373226.1744236488.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Wed, 09 Apr 2025 18:08:08 -0400 Message-ID: <373227.1744236488@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "David G. Johnston" writes: > On Wed, Apr 9, 2025 at 1:50 PM Laurenz Albe > 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