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 1oEEbp-0000do-DW for pgsql-general@arkaria.postgresql.org; Wed, 20 Jul 2022 18:39:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oEEbn-0003ON-Ns for pgsql-general@arkaria.postgresql.org; Wed, 20 Jul 2022 18:39:43 +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 1oEEbn-0003FJ-D5 for pgsql-general@lists.postgresql.org; Wed, 20 Jul 2022 18:39:43 +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 1oEEbl-0000Hw-1u for pgsql-general@lists.postgresql.org; Wed, 20 Jul 2022 18:39:42 +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 26KIddl01881313; Wed, 20 Jul 2022 14:39:39 -0400 From: Tom Lane To: Thierry Henrio cc: pgsql-general@lists.postgresql.org Subject: Re: plan for function returning table combined with condition In-reply-to: References: Comments: In-reply-to Thierry Henrio message dated "Wed, 20 Jul 2022 19:51:55 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1881311.1658342379.1@sss.pgh.pa.us> Date: Wed, 20 Jul 2022 14:39:39 -0400 Message-ID: <1881312.1658342379@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thierry Henrio writes: > I made a function out of this sql: > create or replace function expand_shop_opening_times() returns table(id > int, name text, day int, startt time, endt time) > as $$ > select id, name, day::int, (value -> 0 ->> 0)::time as startt, (value -> 0 > ->> 1)::time as endt from ( > select s.id, s.name, j.* from shops s cross join > jsonb_each(s.opening_times) as j(day, value) > ) t > $$ language sql > So I can use it like so (A): > select id, name from expand_shop_opening_times() where id=1307; > The plan for statement (A) is: > Function Scan on expand_shop_opening_times (cost=0.25..12.75 rows=5 > width=36) (actual time=15.950..16.418 rows=7 loops=1) > Filter: (id = 1307) > Rows Removed by Filter: 10540 > Planning Time: 0.082 ms > Execution Time: 16.584 ms You want this SQL function to be inlined, but it isn't being. I think the reason is that (by default) it's VOLATILE, and inline_set_returning_function doesn't like that: * Forget it if the function is not SQL-language or has other showstopper * properties. In particular it mustn't be declared STRICT, since we * couldn't enforce that. It also mustn't be VOLATILE, because that is * supposed to cause it to be executed with its own snapshot, rather than * sharing the snapshot of the calling query. We also disallow returning * SETOF VOID, because inlining would result in exposing the actual result * of the function's last SELECT, which should not happen in that case. So try adding STABLE to the function definition. (This could be better documented, perhaps.) regards, tom lane