From dcd58bbe27e715688d49b3d0244524c817c5c6c3 Mon Sep 17 00:00:00 2001
From: "Paul A. Jungwirth" <pj@illuminatedcomputing.com>
Date: Thu, 27 Jun 2024 10:17:35 -0700
Subject: [PATCH v1] Inline non-SQL SRFs using SupportRequestSimplify

If the support request returns a Query node, then
inline_set_returning_function will use it in place of the FuncExpr.
---
 src/backend/optimizer/util/clauses.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index b4e085e9d4b..d412033dba6 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -5085,11 +5085,17 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
 	if (rte->funcordinality)
 		return NULL;
 
-	/* Fail if RTE isn't a single, simple FuncExpr */
 	if (list_length(rte->functions) != 1)
 		return NULL;
 	rtfunc = (RangeTblFunction *) linitial(rte->functions);
 
+	/*
+	 * If the SRF was inlined using SupportRequestInline, then we expect a
+	 * Query, otherwise a FuncExpr. Either way, only one item.
+	 */
+	if (IsA(rtfunc->funcexpr, Query))
+		return (Query *)rtfunc->funcexpr;
+
 	if (!IsA(rtfunc->funcexpr, FuncExpr))
 		return NULL;
 	fexpr = (FuncExpr *) rtfunc->funcexpr;
-- 
2.42.0

