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 1u35Qy-00AHk1-Qw for pgsql-general@arkaria.postgresql.org; Fri, 11 Apr 2025 03:52:04 +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 1u35Qw-003uuG-4J for pgsql-general@arkaria.postgresql.org; Fri, 11 Apr 2025 03:52:02 +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 1u35Qv-003uu7-OR for pgsql-general@lists.postgresql.org; Fri, 11 Apr 2025 03:52:02 +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 1u35Qu-004AeH-0Z for pgsql-general@lists.postgresql.org; Fri, 11 Apr 2025 03:52:01 +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 53B3prjE752813; Thu, 10 Apr 2025 23:51:53 -0400 From: Tom Lane To: Merlin Moncure cc: "David G. Johnston" , Nico Williams , Adrian Klaver , 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 Merlin Moncure message dated "Thu, 10 Apr 2025 22:18:42 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <752811.1744343513.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 10 Apr 2025 23:51:53 -0400 Message-ID: <752812.1744343513@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Merlin Moncure writes: > I guess the real problems here are lack of feedback on a number of front= s: > *) the server knows the function is not immutable but lets you create it > anyway, even though it can have negative downstream consequences That's debatable I think. If you know what you're doing, you're going to be annoyed by warnings telling you that you don't. > *) there is no way to discern inline vs non-inlined execution in explain That's simply false. Using the examples in this thread: regression=3D# explain (verbose,analyze) select formatted_num_immutable(i) from generate_series(1,1000000) i; QUERY= PLAN = --------------------------------------------------------------------------= -------------------------------------------------------------------------- Function Scan on pg_catalog.generate_series i (cost=3D0.00..262500.00 ro= ws=3D1000000 width=3D32) (actual time=3D65.535..2444.956 rows=3D1000000.00= loops=3D1) Output: formatted_num_immutable((i)::bigint) Function Call: generate_series(1, 1000000) Buffers: temp read=3D1709 written=3D1709 Planning Time: 0.086 ms Execution Time: 2481.218 ms (6 rows) regression=3D# explain (verbose,analyze) select formatted_num_stable(i) from generate_series(1,1000000) i; QUERY = PLAN = --------------------------------------------------------------------------= ------------------------------------------------------------------------ Function Scan on pg_catalog.generate_series i (cost=3D0.00..17500.00 row= s=3D1000000 width=3D32) (actual time=3D65.615..478.780 rows=3D1000000.00 l= oops=3D1) Output: ltrim(to_char((i)::bigint, '999 999 999 999 999 999 999 999'::t= ext)) Function Call: generate_series(1, 1000000) Buffers: temp read=3D1709 written=3D1709 Planning Time: 0.091 ms Execution Time: 501.412 ms (6 rows) You can easily see that the second case was inlined, because you don't see the SQL function anymore, rather its body. > *) the planner is clearly not modelling function scan overhead give the > relative costing discrepancies That's also false; note the 15x difference in estimated cost above, which is actually more than the real difference in runtime. (I hasten to add that I don't have a lot of faith in our function cost estimates. But the planner is quite well aware that a non-inlined SQL function is likely to be expensive.) regards, tom lane