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 1kqVHa-0000DW-5N for pgsql-general@arkaria.postgresql.org; Sat, 19 Dec 2020 05:59:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kqVHX-0003jf-GL for pgsql-general@arkaria.postgresql.org; Sat, 19 Dec 2020 05:59:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqVHW-0003jV-Jh for pgsql-general@lists.postgresql.org; Sat, 19 Dec 2020 05:59:55 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqVHN-0002nE-VJ for pgsql-general@postgresql.org; Sat, 19 Dec 2020 05:59:53 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 219B05C014F for ; Sat, 19 Dec 2020 00:59:44 -0500 (EST) Received: from imap2 ([10.202.2.52]) by compute5.internal (MEProxy); Sat, 19 Dec 2020 00:59:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= mime-version:message-id:date:from:to:subject:content-type; s= fm3; bh=NzLcodvNyDzeOTQ34YD/iMSHMpyIK/RRkSf5vHvk2y8=; b=vsd+NTvy r3zRCuY/+NVN1VAPr+DAFUKDFKP/EV3pwyvSrHo5ECaCihgfUfK981vCDN/SXEYD 3WyUN1wlZf4W9hfeDOAA4U1wXwuN+nS1dP6/8c4Er32IfqtUQ+kSHQe0kbnD5fi3 eKO99PvX5i5t22+4Yhvr1w9z0SsvU4d41lTZWJmzfTLJUcqyX0oWMhq5ZLtJWhXL Vmo2Igs0DF6I/n5kvYMGirxFW1l16wmJWZg15JBDgUC3s/kAT0oroNGAY70woFDN ijdb7vj0HCOH7G8we9I3dZAt3pAditcCx3U+/yrMpDlrxKXY6GBtkWcdehJAbJ/h yElxdofBwktQDQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=NzLcodvNyDzeOTQ34YD/iMSHMpyIK /RRkSf5vHvk2y8=; b=KwRqL6vpZAMZcyEJ/dFMXKhpoFvMjIWn3ICFKnomgUp2z VQ9EYfH5nV+wHlzVo7OExSE2mI3JO88ZwDEZ5kc8wNR9Ex8O/hkW4B5ph+wHPYtJ 7osa1d9D8dksFn2sKB692dlZB4reicJzzdXwRHSnvj7XHvAulcqe4WaxZ09HwF8K g/oWD/uFuep4LR4thTk7funh98Q9/xMWFXXnid0mnJZrV7Q0sAQXpXt8P5P4D6Ty KjsDbCycnruTno3O5SysCujWmBAv1sPYdjt8Dknk9FZ0eNtevLqkxRpIOtT/6RXW rEnCchV0u+aBnqs1Y2H0Mu842qUMTcsh7FtybLcqw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudeljedgkeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfffhffvufgtsegrtderre erredtnecuhfhrohhmpedflfhovghlucflrggtohgsshhonhdfuceojhhovghlsegtohhm phhilhgvrhdrohhrgheqnecuggftrfgrthhtvghrnheplefhkedvfeehueeiiefhvdeihf eghfffhfegiedufeeujeevieeltdfgheethedtnecuffhomhgrihhnpehgihhthhhusgdr tghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe hjohgvlhestghomhhpihhlvghrrdhorhhg X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 72780E04FF; Sat, 19 Dec 2020 00:59:41 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.3.1-61-gb52c239-fm-20201210.001-gb52c2396 Mime-Version: 1.0 Message-Id: Date: Sat, 19 Dec 2020 06:59:21 +0100 From: "Joel Jacobson" To: pgsql-general@postgresql.org Subject: Avoid excessive inlining? Content-Type: multipart/alternative; boundary=590af14c97b54504acc89b0c3d17b9aa List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --590af14c97b54504acc89b0c3d17b9aa Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Is there a way to avoid excessive inlining when writing pure SQL functio= ns, without having to use PL/pgSQL? The JOIN LATERAL and Nested Subqueries versions run much slower than the= PL/pgSQL version: Execution Times: JOIN LATERAL: 12198.010 ms Nested Subqueries: 12250.077 ms PL/pgSQL: 312.493 ms The three functions below are equivalent, they all compute the Eastern d= ate for a given year. CREATE OR REPLACE FUNCTION easter_lateral(year integer) RETURNS DATE LANGUAGE sql AS $$ SELECT make_date(year, easter_month, easter_day) FROM (VALUES (year % 19, year / 100)) AS Q1(g,c) JOIN LATERAL (VALUES ((c - c/4 - (8*c + 13)/25 + 19*g + 15) % 30)) AS Q2= (h) ON TRUE JOIN LATERAL (VALUES (h - (h/28)*(1 - (h/28)*(29/(h + 1))*((21 - g)/11))= )) AS Q3(i) ON TRUE JOIN LATERAL (VALUES ((year + year/4 + i + 2 - c + c/4) % 7)) AS Q4(j) O= N TRUE JOIN LATERAL (VALUES (i - j)) AS Q5(p) ON TRUE JOIN LATERAL (VALUES (3 + (p + 26)/30, 1 + (p + 27 + (p + 6)/40) % 31)) = AS Q6(easter_month, easter_day) ON TRUE $$; CREATE OR REPLACE FUNCTION easter_nested_subqueries(year integer) RETURNS DATE LANGUAGE sql AS $$ SELECT make_date(year, easter_month, easter_day) FROM ( SELECT *, 3 + (p + 26)/30 AS easter_month, 1 + (p + 27 + (p + 6)/40) % 31 AS easter_day FROM ( SELECT *, i - j AS p FROM ( SELECT *, (year + year/4 + i + 2 - c + c/4) % 7 AS j FROM ( SELECT *, h - (h/28)*(1 - (h/28)*(29/(h + 1))*((21 - g)/11)) AS i FROM ( SELECT *, (c - c/4 - (8*c + 13)/25 + 19*g + 15) % 30 AS h FROM ( SELECT year % 19 AS g, year / 100 AS c ) AS Q1 ) AS Q2 ) AS Q3 ) AS Q4 ) AS Q5 ) AS Q6 $$; CREATE OR REPLACE FUNCTION easter_plpgsql(year integer) RETURNS date LANGUAGE plpgsql AS $$ -- Based on: https://github.com/christopherthompson81/pgsql_holidays/blo= b/master/utils/easter.pgsql DECLARE g CONSTANT integer :=3D year % 19; c CONSTANT integer :=3D year / 100; h CONSTANT integer :=3D (c - c/4 - (8*c + 13)/25 + 19*g + 15) % 30; i CONSTANT integer :=3D h - (h/28)*(1 - (h/28)*(29/(h + 1))*((21 - g)/11= )); j CONSTANT integer :=3D (year + year/4 + i + 2 - c + c/4) % 7; p CONSTANT integer :=3D i - j; BEGIN RETURN make_date( year, 3 + (p + 26)/30, 1 + (p + 27 + (p + 6)/40) % 31 ); END; $$; joel=3D# EXPLAIN ANALYZE SELECT MAX(easter) FROM (SELECT easter_lateral(= year) AS easter FROM generate_series(1,100000) AS year) AS x; QUERY P= LAN ------------------------------------------------------------------------= -------------------------------------------------------------------- Aggregate (cost=3D27250.00..27250.01 rows=3D1 width=3D4) (actual time=3D= 12195.974..12195.974 rows=3D1 loops=3D1) -> Function Scan on generate_series year (cost=3D0.00..26000.00 row= s=3D100000 width=3D4) (actual time=3D15.840..12167.758 rows=3D100000 loo= ps=3D1) Planning Time: 0.262 ms Execution Time: 12198.010 ms (4 rows) joel=3D# EXPLAIN ANALYZE SELECT MAX(easter) FROM (SELECT easter_nested_s= ubqueries(year) AS easter FROM generate_series(1,100000) AS year) AS x; QUERY P= LAN ------------------------------------------------------------------------= -------------------------------------------------------------------- Aggregate (cost=3D27250.00..27250.01 rows=3D1 width=3D4) (actual time=3D= 12248.316..12248.317 rows=3D1 loops=3D1) -> Function Scan on generate_series year (cost=3D0.00..26000.00 row= s=3D100000 width=3D4) (actual time=3D17.707..12219.500 rows=3D100000 loo= ps=3D1) Planning Time: 0.277 ms Execution Time: 12250.077 ms (4 rows) joel=3D# EXPLAIN ANALYZE SELECT MAX(easter) FROM (SELECT easter_plpgsql(= year) AS easter FROM generate_series(1,100000) AS year) AS x; QUERY PL= AN ------------------------------------------------------------------------= ------------------------------------------------------------------ Aggregate (cost=3D27250.00..27250.01 rows=3D1 width=3D4) (actual time=3D= 311.107..311.108 rows=3D1 loops=3D1) -> Function Scan on generate_series year (cost=3D0.00..26000.00 row= s=3D100000 width=3D4) (actual time=3D12.369..296.221 rows=3D100000 loops= =3D1) Planning Time: 0.058 ms Execution Time: 312.493 ms (4 rows) If we look at the plan for the lateral and subqueries versions, we can s= ee how the inlining expands to huge expressions. Could this be the reason they run so much slower than the PL/pgSQL versi= on? PREPARE q_lateral AS SELECT make_date($1, easter_month, easter_day) FROM (VALUES ($1 % 19, $1 / 100)) AS Q1(g,c) JOIN LATERAL (VALUES ((c - c/4 - (8*c + 13)/25 + 19*g + 15) % 30)) AS Q2(h) ON TRUE JOIN LATERAL (VALUES (h - (h/28)*(1 - (h/28)*(29/(h + 1))*((21 - g)/11)))) AS Q3(i) ON TRUE JOIN LATERAL (VALUES (($1 + $1/4 + i + 2 - c + c/4) % 7)) AS Q4(j) ON TR= UE JOIN LATERAL (VALUES (i - j)) AS Q5(p) ON TRUE JOIN LATERAL (VALUES (3 + (p + 26)/30, 1 + (p + 27 + (p + 6)/40) % 31)) AS Q6(easter_month, easter_day) ON TRUE; SET plan_cache_mode =3D 'force_generic_plan'; EXPLAIN (ANALYZE, VERBOSE) EXECUTE q_lateral(2020); Result (cost=3D0.00..1.14 rows=3D1 width=3D4) (actual time=3D0.038..0.0= 38 rows=3D1 loops=3D1) Output: make_date($1, (3 + (((((((((($1 / 100) - (($1 / 100) / 4)) - (= ((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) - (((((((= ($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 *= ($1 % 19))) + 15) % 30) / 28) * (1 - ((((((((($1 / 100) - (($1 / 100) /= 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) /= 28) * (29 / ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) += 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19)) /= 11))))) - (((((($1 + ($1 / 4)) + ((((((($1 / 100) - (($1 / 100) / 4)) -= (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) - (((((= ((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19= * ($1 % 19))) + 15) % 30) / 28) * (1 - ((((((((($1 / 100) - (($1 / 100)= / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30)= / 28) * (29 / ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100))= + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19))= / 11)))))) + 2) - ($1 / 100)) + (($1 / 100) / 4)) % 7)) + 26) / 30)), (= 1 + ((((((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) = / 25)) + (19 * ($1 % 19))) + 15) % 30) - (((((((($1 / 100) - (($1 / 100)= / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30)= / 28) * (1 - ((((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)= ) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (29 / ((((((($1 /= 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 = % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19)) / 11))))) - (((((($1 + ($= 1 / 4)) + ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13= ) / 25)) + (19 * ($1 % 19))) + 15) % 30) - (((((((($1 / 100) - (($1 / 10= 0) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 3= 0) / 28) * (1 - ((((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 10= 0)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (29 / ((((((($1= / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($= 1 % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19)) / 11)))))) + 2) - ($1 /= 100)) + (($1 / 100) / 4)) % 7)) + 27) + (((((((((($1 / 100) - (($1 / 10= 0) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 3= 0) - (((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / = 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (1 - ((((((((($1 / 100) - (= ($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) += 15) % 30) / 28) * (29 / ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * (= $1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 - = ($1 % 19)) / 11))))) - (((((($1 + ($1 / 4)) + ((((((($1 / 100) - (($1 / = 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) %= 30) - (((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) = / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (1 - ((((((((($1 / 100) -= (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19)))= + 15) % 30) / 28) * (29 / ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 *= ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 = - ($1 % 19)) / 11)))))) + 2) - ($1 / 100)) + (($1 / 100) / 4)) % 7)) + 6= ) / 40)) % 31))) Planning Time: 4.144 ms Execution Time: 0.220 ms PREPARE q_subqueries AS SELECT make_date($1, easter_month, easter_day) FROM ( SELECT *, 3 + (p + 26)/30 AS easter_month, 1 + (p + 27 + (p + 6)/40) % 31 AS easter_day FROM ( SELECT *, i - j AS p FROM ( SELECT *, ($1 + $1/4 + i + 2 - c + c/4) % 7 AS j FROM ( SELECT *, h - (h/28)*(1 - (h/28)*(29/(h + 1))*((21 - g)/11)) AS i FROM ( SELECT *, (c - c/4 - (8*c + 13)/25 + 19*g + 15) % 30 AS h FROM ( SELECT $1 % 19 AS g, $1 / 100 AS c ) AS Q1 ) AS Q2 ) AS Q3 ) AS Q4 ) AS Q5 ) AS Q6 ; SET plan_cache_mode =3D 'force_generic_plan'; EXPLAIN (ANALYZE, VERBOSE) EXECUTE q_subqueries(2020); Result (cost=3D0.00..1.14 rows=3D1 width=3D4) (actual time=3D0.043..0.0= 44 rows=3D1 loops=3D1) Output: make_date($1, (3 + (((((((((($1 / 100) - (($1 / 100) / 4)) - (= ((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) - (((((((= ($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 *= ($1 % 19))) + 15) % 30) / 28) * (1 - ((((((((($1 / 100) - (($1 / 100) /= 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) /= 28) * (29 / ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) += 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19)) /= 11))))) - (((((($1 + ($1 / 4)) + ((((((($1 / 100) - (($1 / 100) / 4)) -= (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) - (((((= ((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19= * ($1 % 19))) + 15) % 30) / 28) * (1 - ((((((((($1 / 100) - (($1 / 100)= / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30)= / 28) * (29 / ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100))= + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19))= / 11)))))) + 2) - ($1 / 100)) + (($1 / 100) / 4)) % 7)) + 26) / 30)), (= 1 + ((((((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) = / 25)) + (19 * ($1 % 19))) + 15) % 30) - (((((((($1 / 100) - (($1 / 100)= / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30)= / 28) * (1 - ((((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)= ) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (29 / ((((((($1 /= 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 = % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19)) / 11))))) - (((((($1 + ($= 1 / 4)) + ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13= ) / 25)) + (19 * ($1 % 19))) + 15) % 30) - (((((((($1 / 100) - (($1 / 10= 0) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 3= 0) / 28) * (1 - ((((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 10= 0)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (29 / ((((((($1= / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($= 1 % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19)) / 11)))))) + 2) - ($1 /= 100)) + (($1 / 100) / 4)) % 7)) + 27) + (((((((((($1 / 100) - (($1 / 10= 0) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 3= 0) - (((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / = 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (1 - ((((((((($1 / 100) - (= ($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) += 15) % 30) / 28) * (29 / ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * (= $1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 - = ($1 % 19)) / 11))))) - (((((($1 + ($1 / 4)) + ((((((($1 / 100) - (($1 / = 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) %= 30) - (((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) = / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (1 - ((((((((($1 / 100) -= (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19)))= + 15) % 30) / 28) * (29 / ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 *= ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 = - ($1 % 19)) / 11)))))) + 2) - ($1 / 100)) + (($1 / 100) / 4)) % 7)) + 6= ) / 40)) % 31))) Planning Time: 2.443 ms Execution Time: 0.235 ms Thanks Andreas Karlsson for teaching me the PREPARE + SET plan_cache_mod= e =3D 'force_generic_plan'; + EXPLAIN trick, very useful. Best regards, Joel --590af14c97b54504acc89b0c3d17b9aa Content-Type: text/html Content-Transfer-Encoding: quoted-printable
Is there a way = to avoid excessive inlining when writing pure SQL functions, without hav= ing to use PL/pgSQL?

The JOIN LATERAL and N= ested Subqueries versions run much slower than the PL/pgSQL version:
=

Execution Times:
JOIN LATERAL: 1= 2198.010 ms
Nested Subqueries: 12250.077 ms
= PL/pgSQL: 312.493 ms

The three functions be= low are equivalent, they all compute the Eastern date for a given year.<= br>

CREATE OR REPLACE FUNCTION easter_lateral(y= ear integer)
RETURNS DATE
LANGUAGE sql
AS $$
SELECT make_date(year, easter_month, easter= _day)
FROM (VALUES (year % 19, year / 100)) AS Q1(g,c)
=
JOIN LATERAL (VALUES ((c - c/4 - (8*c + 13)/25 + 19*g + 15) %= 30)) AS Q2(h) ON TRUE
JOIN LATERAL (VALUES (h - (h/28)*(1= - (h/28)*(29/(h + 1))*((21 - g)/11)))) AS Q3(i) ON TRUE
J= OIN LATERAL (VALUES ((year + year/4 + i + 2 - c + c/4) % 7)) AS Q4(j) ON= TRUE
JOIN LATERAL (VALUES (i - j)) AS Q5(p) ON TRUE
JOIN LATERAL (VALUES (3 + (p + 26)/30, 1 + (p + 27 + (p + 6)/40= ) % 31)) AS Q6(easter_month, easter_day) ON TRUE
$$;

CREATE OR REPLACE FUNCTION easter_nested_subquer= ies(year integer)
RETURNS DATE
LANGUAGE sql<= br>
AS $$
SELECT make_date(year, easter_month, e= aster_day)
FROM (
  SELECT *,
=
    3 + (p + 26)/30 AS easter_month,
&= nbsp;   1 + (p + 27 + (p + 6)/40) % 31 AS easter_day
=
  FROM (
    SELECT *,
<= div>      i - j AS p
  =   FROM (
      SELECT *,
=
      (year + year/4 + i + 2 - c + c= /4) % 7 AS j
      FROM (
        SELECT *,
          h - (h/28)*(1 -= (h/28)*(29/(h + 1))*((21 - g)/11)) AS i
   = ;     FROM (
    &= nbsp;     SELECT *,
   =          (c - c/4 - (8*c + 13)/2= 5 + 19*g + 15) % 30 AS h
     &nb= sp;    FROM (
     = ;       SELECT
  &= nbsp;           year %= 19 AS g,
        =       year / 100 AS c
  = ;        ) AS Q1
 =        ) AS Q2
  &= nbsp;   ) AS Q3
    ) AS Q4
  ) AS Q5
) AS Q6
$$;

CREATE OR REPLACE FUNCTION easter_plpgsql(year inte= ger)
RETURNS date
LANGUAGE plpgsql
=
AS $$
-- Based on: https://github.com/christopherthom= pson81/pgsql_holidays/blob/master/utils/easter.pgsql
DECLA= RE
g CONSTANT integer :=3D year % 19;
c CONS= TANT integer :=3D year / 100;
h CONSTANT integer :=3D (c -= c/4 - (8*c + 13)/25 + 19*g + 15) % 30;
i CONSTANT integer= :=3D h - (h/28)*(1 - (h/28)*(29/(h + 1))*((21 - g)/11));
= j CONSTANT integer :=3D (year + year/4 + i + 2 - c + c/4) % 7;
=
p CONSTANT integer :=3D i - j;
BEGIN
RE= TURN make_date(
  year,
  3 + (p += 26)/30,
  1 + (p + 27 + (p + 6)/40) % 31
);
END;
$$;

= joel=3D# EXPLAIN ANALYZE SELECT MAX(easter) FROM (SELECT easter_lateral(= year) AS easter FROM generate_series(1,100000) AS year) AS x;
<= div>           &n= bsp;           &n= bsp;           &n= bsp;           &n= bsp;           &n= bsp;     QUERY PLAN
------------------= ------------------------------------------------------------------------= --------------------------------------------------
Aggrega= te  (cost=3D27250.00..27250.01 rows=3D1 width=3D4) (actual time=3D1= 2195.974..12195.974 rows=3D1 loops=3D1)
   ->=   Function Scan on generate_series year  (cost=3D0.00..26000.0= 0 rows=3D100000 width=3D4) (actual time=3D15.840..12167.758 rows=3D10000= 0 loops=3D1)
Planning Time: 0.262 ms
Executi= on Time: 12198.010 ms
(4 rows)

joel=3D# EXPLAIN ANALYZE SELECT MAX(easter) FROM (SELECT easter_nested= _subqueries(year) AS easter FROM generate_series(1,100000) AS year) AS x= ;
         &n= bsp;           &n= bsp;           &n= bsp;           &n= bsp;           &n= bsp;       QUERY PLAN
------= ------------------------------------------------------------------------= --------------------------------------------------------------
=
Aggregate  (cost=3D27250.00..27250.01 rows=3D1 width=3D4) (act= ual time=3D12248.316..12248.317 rows=3D1 loops=3D1)
 =   ->  Function Scan on generate_series year  (cost=3D0= .00..26000.00 rows=3D100000 width=3D4) (actual time=3D17.707..12219.500 = rows=3D100000 loops=3D1)
Planning Time: 0.277 ms
=
Execution Time: 12250.077 ms
(4 rows)
<= br>
joel=3D# EXPLAIN ANALYZE SELECT MAX(easter) FROM (SELECT e= aster_plpgsql(year) AS easter FROM generate_series(1,100000) AS year) AS= x;
         =             =             =             =             =        QUERY PLAN
----------= ------------------------------------------------------------------------= --------------------------------------------------------
A= ggregate  (cost=3D27250.00..27250.01 rows=3D1 width=3D4) (actual ti= me=3D311.107..311.108 rows=3D1 loops=3D1)
   -&g= t;  Function Scan on generate_series year  (cost=3D0.00..26000= .00 rows=3D100000 width=3D4) (actual time=3D12.369..296.221 rows=3D10000= 0 loops=3D1)
Planning Time: 0.058 ms
Executi= on Time: 312.493 ms
(4 rows)

= If we look at the plan for the lateral and subqueries versions, we can s= ee how the inlining expands to huge expressions.

Could this be the reason they run so much slower than the PL/pgSQL= version?

PREPARE q_lateral AS SELECT make_= date($1, easter_month, easter_day)
FROM (VALUES ($1 % 19, = $1 / 100)) AS Q1(g,c)
JOIN LATERAL (VALUES ((c - c/4 - (8*= c + 13)/25 + 19*g + 15) % 30)) AS
Q2(h) ON TRUE
<= div>JOIN LATERAL (VALUES (h - (h/28)*(1 - (h/28)*(29/(h + 1))*((21 -
=
g)/11)))) AS Q3(i) ON TRUE
JOIN LATERAL (VALUES= (($1 + $1/4 + i + 2 - c + c/4) % 7)) AS Q4(j) ON TRUE
JOI= N LATERAL (VALUES (i - j)) AS Q5(p) ON TRUE
JOIN LATERAL (= VALUES (3 + (p + 26)/30, 1 + (p + 27 + (p + 6)/40) % 31))
= AS Q6(easter_month, easter_day) ON TRUE;

SE= T plan_cache_mode =3D 'force_generic_plan';

EXPLAIN (ANALYZE, VERBOSE) EXECUTE q_lateral(2020);

<= /div>
Result  (cost=3D0.00..1.14 rows=3D1 width=3D4) (actual ti= me=3D0.038..0.038 rows=3D1 loops=3D1)
  Output: make_= date($1, (3 + (((((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100= )) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) - (((((((($1 / 100) - ((= $1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + = 15) % 30) / 28) * (1 - ((((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * (= $1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (29 / ((= ((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (= 19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19)) / 11))))) - ((((= (($1 + ($1 / 4)) + ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 1= 00)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) - (((((((($1 / 100) - = (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) = + 15) % 30) / 28) * (1 - ((((((((($1 / 100) - (($1 / 100) / 4)) - (((8 *= ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (29 / = ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) += (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19)) / 11)))))) + 2= ) - ($1 / 100)) + (($1 / 100) / 4)) % 7)) + 26) / 30)), (1 + (((((((((((= $1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * = ($1 % 19))) + 15) % 30) - (((((((($1 / 100) - (($1 / 100) / 4)) - (((8 *= ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (1 - (= (((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) = + (19 * ($1 % 19))) + 15) % 30) / 28) * (29 / ((((((($1 / 100) - (($1 / = 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) %= 30) + 1))) * ((21 - ($1 % 19)) / 11))))) - (((((($1 + ($1 / 4)) + (((((= (($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 = * ($1 % 19))) + 15) % 30) - (((((((($1 / 100) - (($1 / 100) / 4)) - (((8= * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (1 -= ((((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)= ) + (19 * ($1 % 19))) + 15) % 30) / 28) * (29 / ((((((($1 / 100) - (($1 = / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15)= % 30) + 1))) * ((21 - ($1 % 19)) / 11)))))) + 2) - ($1 / 100)) + (($1 /= 100) / 4)) % 7)) + 27) + (((((((((($1 / 100) - (($1 / 100) / 4)) - (((8= * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) - (((((((($1= / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($= 1 % 19))) + 15) % 30) / 28) * (1 - ((((((((($1 / 100) - (($1 / 100) / 4)= ) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28= ) * (29 / ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13= ) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19)) / 11= ))))) - (((((($1 + ($1 / 4)) + ((((((($1 / 100) - (($1 / 100) / 4)) - ((= (8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) - ((((((((= $1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * = ($1 % 19))) + 15) % 30) / 28) * (1 - ((((((((($1 / 100) - (($1 / 100) / = 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / = 28) * (29 / ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + = 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19)) / = 11)))))) + 2) - ($1 / 100)) + (($1 / 100) / 4)) % 7)) + 6) / 40)) % 31))= )
Planning Time: 4.144 ms
Execution Time: 0.= 220 ms

PREPARE q_subqueries AS SELECT make_= date($1, easter_month, easter_day)
FROM (
&n= bsp; SELECT *,
    3 + (p + 26)/30 AS easte= r_month,
    1 + (p + 27 + (p + 6)/40) % 31= AS easter_day
  FROM (
  &nb= sp; SELECT *,
      i - j AS p
    FROM (
   &n= bsp;  SELECT *,
      ($1 + = $1/4 + i + 2 - c + c/4) % 7 AS j
    &= nbsp; FROM (
        SE= LECT *,
        &n= bsp; h - (h/28)*(1 - (h/28)*(29/(h + 1))*((21 - g)/11)) AS i
        FROM (
 = ;         SELECT *,
            (c = - c/4 - (8*c + 13)/25 + 19*g + 15) % 30 AS h
  &= nbsp;       FROM (
 &nb= sp;          SELECT
           = ;   $1 % 19 AS g,
     =          $1 / 100 AS c
=
          ) AS Q1
<= /div>
        ) AS Q2
      ) AS Q3
  &nbs= p; ) AS Q4
  ) AS Q5
) AS Q6
<= div>;

SET plan_cache_mode =3D 'force_generi= c_plan';

EXPLAIN (ANALYZE, VERBOSE) EXECUTE= q_subqueries(2020);

Result  (cost=3D0= .00..1.14 rows=3D1 width=3D4) (actual time=3D0.043..0.044 rows=3D1 loops= =3D1)
  Output: make_date($1, (3 + (((((((((($1 / 100= ) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19= ))) + 15) % 30) - (((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 1= 00)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (1 - (((((((((= $1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * = ($1 % 19))) + 15) % 30) / 28) * (29 / ((((((($1 / 100) - (($1 / 100) / 4= )) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1= ))) * ((21 - ($1 % 19)) / 11))))) - (((((($1 + ($1 / 4)) + ((((((($1 / 1= 00) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % = 19))) + 15) % 30) - (((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 /= 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (1 - (((((((= (($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 = * ($1 % 19))) + 15) % 30) / 28) * (29 / ((((((($1 / 100) - (($1 / 100) /= 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) += 1))) * ((21 - ($1 % 19)) / 11)))))) + 2) - ($1 / 100)) + (($1 / 100) / = 4)) % 7)) + 26) / 30)), (1 + ((((((((((($1 / 100) - (($1 / 100) / 4)) - = (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) - ((((((= (($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 = * ($1 % 19))) + 15) % 30) / 28) * (1 - ((((((((($1 / 100) - (($1 / 100) = / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) = / 28) * (29 / ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) = + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19)) = / 11))))) - (((((($1 + ($1 / 4)) + ((((((($1 / 100) - (($1 / 100) / 4)) = - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) - ((((= (((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (1= 9 * ($1 % 19))) + 15) % 30) / 28) * (1 - ((((((((($1 / 100) - (($1 / 100= ) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30= ) / 28) * (29 / ((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)= ) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) + 1))) * ((21 - ($1 % 19)= ) / 11)))))) + 2) - ($1 / 100)) + (($1 / 100) / 4)) % 7)) + 27) + ((((((= (((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (1= 9 * ($1 % 19))) + 15) % 30) - (((((((($1 / 100) - (($1 / 100) / 4)) - ((= (8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (1= - ((((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 2= 5)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (29 / ((((((($1 / 100) - (($= 1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 1= 5) % 30) + 1))) * ((21 - ($1 % 19)) / 11))))) - (((((($1 + ($1 / 4)) + (= (((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + = (19 * ($1 % 19))) + 15) % 30) - (((((((($1 / 100) - (($1 / 100) / 4)) - = (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * = (1 - ((((((((($1 / 100) - (($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) /= 25)) + (19 * ($1 % 19))) + 15) % 30) / 28) * (29 / ((((((($1 / 100) - (= ($1 / 100) / 4)) - (((8 * ($1 / 100)) + 13) / 25)) + (19 * ($1 % 19))) += 15) % 30) + 1))) * ((21 - ($1 % 19)) / 11)))))) + 2) - ($1 / 100)) + ((= $1 / 100) / 4)) % 7)) + 6) / 40)) % 31)))
Planning Time: 2= .443 ms
Execution Time: 0.235 ms

<= div>Thanks Andreas Karlsson for teaching me the PREPARE + SET plan_cache= _mode =3D 'force_generic_plan'; + EXPLAIN trick, very useful.
<= div>
Best regards,

Joel
--590af14c97b54504acc89b0c3d17b9aa--