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 1vGnzm-004NSJ-1e for pgsql-performance@arkaria.postgresql.org; Thu, 06 Nov 2025 00:36:57 +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 1vGj0r-003yQX-N3 for pgsql-performance@arkaria.postgresql.org; Wed, 05 Nov 2025 19:17:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vGj0r-003yQO-Cw for pgsql-performance@lists.postgresql.org; Wed, 05 Nov 2025 19:17:44 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vGj0o-006ART-1B for pgsql-performance@lists.postgresql.org; Wed, 05 Nov 2025 19:17: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 5A5JHeaF873497; Wed, 5 Nov 2025 14:17:40 -0500 From: Tom Lane To: "Dirschel, Steve" cc: "pgsql-performance@lists.postgresql.org" Subject: Re: [EXT] Re: Problem getting query to use index inside a function In-reply-to: References: <836752.1762359519@sss.pgh.pa.us> Comments: In-reply-to "Dirschel, Steve" message dated "Wed, 05 Nov 2025 16:55:38 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <873495.1762370260.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 05 Nov 2025 14:17:40 -0500 Message-ID: <873496.1762370260@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "Dirschel, Steve" writes: >> I think you would have better luck if the planner were "inlining" >> this function, which we can see it's not since you get a Function Scan = on steve1 rather than the contained query. >> I think the only thing stopping that from happening is that the functio= n is (by default) VOLATILE. Try marking it STABLE so that it can share th= e calling query's snapshot. > Thanks for the reply, but that did not seem to help. I tried to replicate this as follows: --- CUT --- create table request(objectid text, productid int, data jsonb); create index on request(objectid, productid); CREATE OR REPLACE FUNCTION public.steve1(param_requestid text[], = param_productid integer DEFAULT 1) RETURNS TABLE(objectid text, n = text, v text, vt integer) LANGUAGE sql AS $function$ SELECT objectid::text , i->>'n'::text , i->>'v'::text , (i->>'vt') :: INT as vt FROM request r , jsonb_array_elements(data -> 'i') i WHERE objectid =3D = ANY($1) AND productid=3D$2 $function$ stable ; explain SELECT objectid::text, n::text, v::text, vt::int FROM = steve1(ARRAY['5ab8e0ca-abb5-48c9-a95e-2bb2a375d903','adcbe251-6723-48a8-83= 85-55133fab704a'], 1); --- CUT --- and I got: QUERY= PLAN = --------------------------------------------------------------------------= -------------------------------------------------------------------------- Nested Loop (cost=3D0.15..11.42 rows=3D100 width=3D100) -> Index Scan using request_objectid_productid_idx on request r (cost= =3D0.15..8.17 rows=3D1 width=3D64) Index Cond: ((objectid =3D ANY ('{5ab8e0ca-abb5-48c9-a95e-2bb2a37= 5d903,adcbe251-6723-48a8-8385-55133fab704a}'::text[])) AND (productid =3D = 1)) -> Function Scan on jsonb_array_elements i (cost=3D0.01..1.00 rows=3D= 100 width=3D32) (4 rows) which is what I expected from successful inlining of the function. So there are some moving parts in your situation that you've not told us about. regards, tom lane