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 1u2cN5-003Jks-6q for pgsql-general@arkaria.postgresql.org; Wed, 09 Apr 2025 20:50:07 +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 1u2cN3-007Afj-Kp for pgsql-general@arkaria.postgresql.org; Wed, 09 Apr 2025 20:50:05 +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 1u2cN3-007AfR-80 for pgsql-general@lists.postgresql.org; Wed, 09 Apr 2025 20:50:05 +0000 Received: from mail-ed1-x533.google.com ([2a00:1450:4864:20::533]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1u2cN1-003wIv-2J for pgsql-general@lists.postgresql.org; Wed, 09 Apr 2025 20:50:04 +0000 Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-5e5bc066283so223520a12.0 for ; Wed, 09 Apr 2025 13:50:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1744231801; x=1744836601; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=cRNQw55GyNkwpD0KviFEUJiaCKAzVcOroHnti2YCPi0=; b=Jting8EsaKOLZ6ZYClVXCdT2InsE7Bakp77bcRVrHDnaF7xm8IH3dVyYPCqpLCODlh ytJJu7gEhCCR3PSyjm5cmHIekjR/Dh40sToq5VICU5qmQU7jfq2JdU3xtmaslVa4cNII IBLI7XPCb2TRWouY8RQ9BieNOjEXuxGS6P4mMmX4qfaEG7k6+IopVjhVEmZcqOCxQKBa 4kBntJKXucB6s+GSp5vQCTH3m7XkCh0cd9Teg3Kl0GgdsIDC8sZx2qCS+XZA0wCzguu/ 5cOf7eGqfO04SbRH1UMCav07N/r5VdJ74vUR3g3g9bqwfakXSrTjTz6Nl8IIqsz33ceK MSWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744231801; x=1744836601; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=cRNQw55GyNkwpD0KviFEUJiaCKAzVcOroHnti2YCPi0=; b=Dks/d82NX9loI4xy9ksOlJcSzzKW99PV4Zez1n+vtCsbdMBVXzPrOo3yDOeJ3Nd6mA kfUi8dGEVE+jntJyktzeFQ1YN7byGWZoAfcbz003gxBDPiYDtQC1rd+1gd1gM/jfh15S DB7fuqPZbYJX4Qv5kNTF5sA+E1r3d14nVryqsDGeZlewoMR8vI8FUjyCAFpgqO0TIrx6 Y3PAXhrp14mO29ewE1vsjVbWmpYUoXN3AzHcUfFstEGxQdUQ7nkCUREsXkBoFiQfIggU hCnWSBXSpwFyjmeEJUzB2Vt8fapKyLML+xpXUByokLCGzR38M9n6+5tr71T7yJ1PWctX jroA== X-Forwarded-Encrypted: i=1; AJvYcCUtojbUbRbW+dqe6IzxCljLzzelfXdgDi5v3C/xoYwpoxnUcfqkQphRTzX1RyUMpUF1yxlYoL3xSmG6tWo8@lists.postgresql.org X-Gm-Message-State: AOJu0Yy4F/k+wJ80Zt/5TT9DjxLtJOa/KnwVdinpAE9/u1qgz7ZVTcud Y8Yp4hn4KlR2QONPNfTeFwz3ljNF+S03qEq+lsQ+day2zJTIOWV/Y3q3uUIQJkcOTx4kRhBAh1P rfdI= X-Gm-Gg: ASbGncvsjV04n/jb45+PMkIpQvJIa2ZwN9qd82C0x1DdhV85grgpbjHypN69kpnmmud JDT8RCWYStfG52biOwNjCITi1m1J9kBnUrNTWXW6D5jF/6SMcXAsI7Jj+GmteOngAWy32FEjGk0 I/dwQOij4XwD5vkKedOz8PTqgTz+Jl6QAZHye75Nw1SX2v2cTbNVTiLXW8Vw6bh3mQhHNtC2IGg CVFQd2NknMdXkLRDIQ65taabKie73mD5xsAATPxsqXXFsqHjvitxXNAwaW/3xHdf0UeCb2l+JKb pgJstPBaqI92q7y3Ltd7qg7cqOjFWaGlHeV52vdS6HeO25yLnAv95S9ObeTU X-Google-Smtp-Source: AGHT+IE1+dtrJpBOGwK+K8pR7o7DWkw0WOSXGMP0yIxHYIkanM8j3aYVlJ95a0WI516jqV58Xp68IA== X-Received: by 2002:a17:907:94d0:b0:ac3:45c6:a1ec with SMTP id a640c23a62f3a-acabd20587dmr10956666b.25.1744231801119; Wed, 09 Apr 2025 13:50:01 -0700 (PDT) Received: from localhost.localdomain ([2001:871:255:52d3:3ac8:61ab:b634:342]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-acaa1ccd1c4sm150035166b.148.2025.04.09.13.50.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Apr 2025 13:50:00 -0700 (PDT) Message-ID: <8687df7d49bc4b28b41c7cf4e9ad1e15d383b2fa.camel@cybertec.at> Subject: Re: Interesting case of IMMUTABLE significantly hurting performance From: Laurenz Albe To: Olleg Samoylov , "pgsql-generallists.postgresql.org" Date: Wed, 09 Apr 2025 22:50:00 +0200 In-Reply-To: <662101e2-5ca5-4bf5-9307-2519efde8690@ya.ru> References: <662101e2-5ca5-4bf5-9307-2519efde8690@ya.ru> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.54.3 (3.54.3-1.fc41) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2025-04-09 at 19:12 +0300, Olleg Samoylov wrote: > PostgreSQL 17.4 >=20 > 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')); >=20 > CREATE OR REPLACE FUNCTION formatted_num_stable(p_summa bigint) > RETURNS text > LANGUAGE sql > STABLE STRICT > RETURN ltrim(to_char(p_summa, '999 999 999 999 999 999 999 999')); >=20 > explain analyze select formatted_num_immutable(i) from=20 > generate_series(1,1000000) as i(i); > QUERY PLAN > -------------------------------------------------------------------------= ------------------------------------------------------------- > Function Scan on generate_series i (cost=3D0.00..262500.00 rows=3D1000= 000=20 > width=3D32) (actual time=3D56.892..1548.656 rows=3D1000000 loops=3D1) > Planning Time: 0.039 ms > JIT: > Functions: 4 > Options: Inlining false, Optimization false, Expressions true,=20 > Deforming true > Timing: Generation 0.184 ms (Deform 0.040 ms), Inlining 0.000 ms,=20 > Optimization 0.115 ms, Emission 1.570 ms, Total 1.869 ms > Execution Time: 1587.741 ms > (7 rows) >=20 > explain analyze select formatted_num_stable(i) from=20 > generate_series(1,1000000) as i(i); > QUERY PLAN > -------------------------------------------------------------------------= ----------------------------------------------------------- > Function Scan on generate_series i (cost=3D0.00..17500.00 rows=3D10000= 00=20 > width=3D32) (actual time=3D54.993..573.333 rows=3D1000000 loops=3D1) > Planning Time: 0.056 ms > Execution Time: 598.190 ms > (3 rows) >=20 > First interesting thing is immutable variant has cost in 15 time more,= =20 > then stable. That's why jit compilation is tuned on. Second, immutable= =20 > function is working much longer (3 times). And jit is not the reason. >=20 > =3D> set jit=3Doff; > SET > =3D> explain analyze select formatted_num_immutable(i) from=20 > generate_series(1,1000000) as i(i); > QUERY PLAN > -------------------------------------------------------------------------= ------------------------------------------------------------- > Function Scan on generate_series i (cost=3D0.00..262500.00 rows=3D1000= 000=20 > width=3D32) (actual time=3D54.888..1537.602 rows=3D1000000 loops=3D1) > Planning Time: 0.052 ms > Execution Time: 1575.985 ms > (3 rows) If you use EXPLAIN (VERBOSE), you will see that the function gets inlined i= n the fast case. That saves the overhead of a function call. The IMMUTABLE function cannot be inlined because to_char() is not IMMUTABLE= . Yours, Laurenz Albe