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 1u2Y2R-002IMI-9y for pgsql-general@arkaria.postgresql.org; Wed, 09 Apr 2025 16:12:31 +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 1u2Y2O-004n2H-Ez for pgsql-general@arkaria.postgresql.org; Wed, 09 Apr 2025 16:12:28 +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 1u2Y2N-004mz8-Pg for pgsql-general@lists.postgresql.org; Wed, 09 Apr 2025 16:12:28 +0000 Received: from forward101d.mail.yandex.net ([2a02:6b8:c41:1300:1:45:d181:d101]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u2Y2K-003uDr-2H for pgsql-general@lists.postgresql.org; Wed, 09 Apr 2025 16:12:26 +0000 Received: from mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net [IPv6:2a02:6b8:c43:317e:0:640:ca69:0]) by forward101d.mail.yandex.net (Yandex) with ESMTPS id 2240760903 for ; Wed, 9 Apr 2025 19:12:20 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id JChOgY0LfGk0-haX3WzsX; Wed, 09 Apr 2025 19:12:19 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ya.ru; s=mail; t=1744215139; bh=WVj0WA/xE6j7yDhJu2S6XzTAr/NBGXpzc2NoP71damE=; h=Subject:From:To:Date:Message-ID; b=eJ96goUqJawoj7G2poX92ib9B85Rd3nkVKy1BnSzZlHBjnX4iyN1Ij+E0929a2/hg VKC1OtpddC6V1q69ymoA2W4CGUiHoRRbYAm0YaP4jDspYj857jEnhh8xYB3P5c8C4D /llSsu6WcgcpJdUtNBPIlj5GIPA5+n6L/Wmf+rys= Authentication-Results: mail-nwsmtp-smtp-production-main-99.klg.yp-c.yandex.net; dkim=pass header.i=@ya.ru Message-ID: <662101e2-5ca5-4bf5-9307-2519efde8690@ya.ru> Date: Wed, 9 Apr 2025 19:12:18 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: "pgsql-generallists.postgresql.org" Content-Language: en-US From: Olleg Samoylov Subject: Interesting case of IMMUTABLE significantly hurting performance Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PostgreSQL 17.4 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')); 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')); explain analyze select formatted_num_immutable(i) from generate_series(1,1000000) as i(i); QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------- Function Scan on generate_series i (cost=0.00..262500.00 rows=1000000 width=32) (actual time=56.892..1548.656 rows=1000000 loops=1) Planning Time: 0.039 ms JIT: Functions: 4 Options: Inlining false, Optimization false, Expressions true, Deforming true Timing: Generation 0.184 ms (Deform 0.040 ms), Inlining 0.000 ms, Optimization 0.115 ms, Emission 1.570 ms, Total 1.869 ms Execution Time: 1587.741 ms (7 rows) explain analyze select formatted_num_stable(i) from generate_series(1,1000000) as i(i); QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ Function Scan on generate_series i (cost=0.00..17500.00 rows=1000000 width=32) (actual time=54.993..573.333 rows=1000000 loops=1) Planning Time: 0.056 ms Execution Time: 598.190 ms (3 rows) First interesting thing is immutable variant has cost in 15 time more, then stable. That's why jit compilation is tuned on. Second, immutable function is working much longer (3 times). And jit is not the reason. => set jit=off; SET => explain analyze select formatted_num_immutable(i) from generate_series(1,1000000) as i(i); QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------- Function Scan on generate_series i (cost=0.00..262500.00 rows=1000000 width=32) (actual time=54.888..1537.602 rows=1000000 loops=1) Planning Time: 0.052 ms Execution Time: 1575.985 ms (3 rows) -- Olleg