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 1nZGWN-0008Qs-0B for pgsql-general@arkaria.postgresql.org; Tue, 29 Mar 2022 18:24:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nZGWL-00052V-O3 for pgsql-general@arkaria.postgresql.org; Tue, 29 Mar 2022 18:24:45 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nZGWL-00052M-Dm for pgsql-general@lists.postgresql.org; Tue, 29 Mar 2022 18:24:45 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nZGWJ-0007j1-9F for pgsql-general@lists.postgresql.org; Tue, 29 Mar 2022 18:24:44 +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 22TIOfgc051175; Tue, 29 Mar 2022 14:24:41 -0400 From: Tom Lane To: Philip Semanchuk cc: PostgreSQL General Subject: Re: Why is my function inlined only when STABLE? In-reply-to: <867DEE4A-D480-4009-9DE9-5082B09B357C@americanefficient.com> References: <867DEE4A-D480-4009-9DE9-5082B09B357C@americanefficient.com> Comments: In-reply-to Philip Semanchuk message dated "Tue, 29 Mar 2022 14:05:44 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <51173.1648578281.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 29 Mar 2022 14:24:41 -0400 Message-ID: <51174.1648578281@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Philip Semanchuk writes: > I have a function that isn't being inlined, and I would appreciate help = to understand why that's the case. = The example you show *is* inline-able, as you can easily prove with EXPLAI= N. regression=3D# CREATE OR REPLACE FUNCTION f(foo text) RETURNS text AS $$ SELECT substring(foo FROM 1 FOR 2) $$ LANGUAGE sql IMMUTABLE PARALLEL SAFE; CREATE FUNCTION regression=3D# explain verbose select f(f1) from text_tbl; QUERY PLAN = ---------------------------------------------------------------- Seq Scan on public.text_tbl (cost=3D0.00..1.02 rows=3D2 width=3D32) Output: "substring"(f1, 1, 2) (2 rows) No f() anywhere there. I think the test methodology you used is faulty, because it does not distinguish between "inline-able" and "foldable to a constant". Given an immutable function applied to constant(s), the planner prefers to fold to a constant by just executing the function. The inline-ing transformation is considered only when that case doesn't apply. regards, tom lane