From e74e92de47e4522dc6b2fccfbdbceb1ba30fb80f Mon Sep 17 00:00:00 2001
From: Richard Guo <guofenglinux@gmail.com>
Date: Fri, 4 Aug 2023 18:07:24 +0800
Subject: [PATCH v1] Check volatile functions in ppi_clauses for memoize node

---
 src/backend/optimizer/path/joinpath.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/backend/optimizer/path/joinpath.c b/src/backend/optimizer/path/joinpath.c
index 4b58936fa4..676a1eec84 100644
--- a/src/backend/optimizer/path/joinpath.c
+++ b/src/backend/optimizer/path/joinpath.c
@@ -582,6 +582,7 @@ get_memoize_path(PlannerInfo *root, RelOptInfo *innerrel,
 	List	   *hash_operators;
 	ListCell   *lc;
 	bool		binary_mode;
+	List	   *restrictlist;
 
 	/* Obviously not if it's disabled */
 	if (!enable_memoize)
@@ -655,11 +656,18 @@ get_memoize_path(PlannerInfo *root, RelOptInfo *innerrel,
 	 * We can't use a memoize node if there are volatile functions in the
 	 * inner rel's target list or restrict list.  A cache hit could reduce the
 	 * number of calls to these functions.
+	 *
+	 * Note that we need to also consider all the join clauses available from
+	 * the outer relation(s) as restriction clauses.
 	 */
 	if (contain_volatile_functions((Node *) innerrel->reltarget))
 		return NULL;
 
-	foreach(lc, innerrel->baserestrictinfo)
+	restrictlist = innerrel->baserestrictinfo;
+	if (inner_path->param_info)
+		restrictlist = list_concat_copy(restrictlist,
+										inner_path->param_info->ppi_clauses);
+	foreach(lc, restrictlist)
 	{
 		RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
 
-- 
2.31.0

