From a599c0fe02dabe04ab527342fd007488493167c8 Mon Sep 17 00:00:00 2001
From: Richard Guo <guofenglinux@gmail.com>
Date: Tue, 10 Oct 2023 15:45:15 +0800
Subject: [PATCH v1] Retire has_multiple_baserels()

The function has_multiple_baserels() is used in set_subquery_pathlist()
to check and see if there are more than 1 base rel, by looping through
simple_rel_array[].  One simpler way to do that is to check
root->all_baserels to see if it has multiple members.  This is a safe
replacement because all_baserels is computed before we generate access
paths for subquery RTEs and it contains all base rels (but not "other"
rels).
---
 src/backend/optimizer/path/allpaths.c | 24 +-----------------------
 1 file changed, 1 insertion(+), 23 deletions(-)

diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c
index eea49cca7b..3cda88e333 100644
--- a/src/backend/optimizer/path/allpaths.c
+++ b/src/backend/optimizer/path/allpaths.c
@@ -2190,28 +2190,6 @@ set_dummy_rel_pathlist(RelOptInfo *rel)
 	set_cheapest(rel);
 }
 
-/* quick-and-dirty test to see if any joining is needed */
-static bool
-has_multiple_baserels(PlannerInfo *root)
-{
-	int			num_base_rels = 0;
-	Index		rti;
-
-	for (rti = 1; rti < root->simple_rel_array_size; rti++)
-	{
-		RelOptInfo *brel = root->simple_rel_array[rti];
-
-		if (brel == NULL)
-			continue;
-
-		/* ignore RTEs that are "other rels" */
-		if (brel->reloptkind == RELOPT_BASEREL)
-			if (++num_base_rels > 1)
-				return true;
-	}
-	return false;
-}
-
 /*
  * find_window_run_conditions
  *		Determine if 'wfunc' is really a WindowFunc and call its prosupport
@@ -2661,7 +2639,7 @@ set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
 		root->hasHavingQual ||
 		parse->distinctClause ||
 		parse->sortClause ||
-		has_multiple_baserels(root))
+		bms_membership(root->all_baserels) == BMS_MULTIPLE)
 		tuple_fraction = 0.0;	/* default case */
 	else
 		tuple_fraction = root->tuple_fraction;
-- 
2.31.0

