Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1njLuU-0005j0-Q7 for pgsql-bugs@arkaria.postgresql.org; Tue, 26 Apr 2022 14:11:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1njLuT-0007ez-3d for pgsql-bugs@arkaria.postgresql.org; Tue, 26 Apr 2022 14:11:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1njLuS-0007eq-RZ for pgsql-bugs@lists.postgresql.org; Tue, 26 Apr 2022 14:11:20 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1njLuO-0003q4-9i for pgsql-bugs@lists.postgresql.org; Tue, 26 Apr 2022 14:11:20 +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 23QEBAXq218919; Tue, 26 Apr 2022 10:11:11 -0400 From: Tom Lane To: Federico Travaglini cc: Merlin Moncure , pgsql-bugs Subject: Re: R: 14.1 immutable function, bad performance if check number = 'NaN' In-reply-to: <43e497193ed4de4bfc503e5221b843d3@mail.gmail.com> References: <43e497193ed4de4bfc503e5221b843d3@mail.gmail.com> Comments: In-reply-to Federico Travaglini message dated "Tue, 26 Apr 2022 09:45:40 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <218917.1650982270.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Tue, 26 Apr 2022 10:11:10 -0400 Message-ID: <218918.1650982270@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Federico Travaglini writes: > Here it is what I tested. I’s a code fragment from a bigger procedure. The > strings in green are passed as parameters, as well as the thresholds > 1,2,3,4,5. To test just this fragment of code I replaced them with fixed > values Is that different from what you do normally? In this example, the function clearly is getting inlined, which means that the parameter values are potentially evaluated multiple times: > antsgeo_get_severity_thr((e.measure_list #> ('{' || > 'cluster_comuni_italiani' || ',o}')::*text*[])::*numeric*, 1, 2, 3, 4, 5) > *AS* severity_1, expands to > CASE WHEN > ((((e.measure_list #> > ('{cluster_comuni_italiani,o}'::cstring)::text[]))::numeric)::double > precision >= '4'::double precision) THEN '1 Clear'::text WHEN > ((((e.measure_list #> > ('{cluster_comuni_italiani,o}'::cstring)::text[]))::numeric)::double > precision >= '3'::double precision) THEN '2 Warning'::text WHEN > ((((e.measure_list #> > ('{cluster_comuni_italiani,o}'::cstring)::text[]))::numeric)::double > precision >= '2'::double precision) THEN '3 Minor'::text WHEN > ((((e.measure_list #> > ('{cluster_comuni_italiani,o}'::cstring)::text[]))::numeric)::double > precision >= '1'::double precision) THEN '4 Major'::text WHEN > ((((e.measure_list #> > ('{cluster_comuni_italiani,o}'::cstring)::text[]))::numeric)::double > precision < '1'::double precision) THEN '5 Critical'::text ELSE '6 > Unk'::text END, That seems pretty inefficient, becase #> isn't the fastest thing in the world. Maybe the speed differential you're seeing is just from adding one more evaluation of the #> for the NaN test. So my advice is to fix things so that #> isn't evaluated multiple times. There are ways to prevent the inlining from happening but they're all underdocumented hacks. A more reliable fix would be to convert the function to plpgsql language. regards, tom lane