From bd98fa9add02793efb71f60fc0aa10108d1cb09e Mon Sep 17 00:00:00 2001
From: Yuya Watari <watari.yuya@gmail.com>
Date: Fri, 17 Feb 2023 16:56:38 +0900
Subject: [PATCH v19 3/4] Move EquivalenceMember indexes from RelOptInfo to
 RangeTblEntry

---
 src/backend/nodes/outfuncs.c            |  3 ++
 src/backend/nodes/readfuncs.c           |  3 ++
 src/backend/optimizer/path/equivclass.c | 57 +++++++++++++------------
 src/backend/optimizer/path/indxpath.c   |  6 ++-
 src/backend/optimizer/prep/prepunion.c  | 12 +++---
 src/backend/optimizer/util/inherit.c    |  8 ++++
 src/backend/optimizer/util/relnode.c    |  9 ----
 src/include/nodes/parsenodes.h          |  9 ++++
 src/include/nodes/pathnodes.h           | 17 --------
 9 files changed, 62 insertions(+), 62 deletions(-)

diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index 6276fcb816..ff2d577366 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -570,6 +570,9 @@ _outRangeTblEntry(StringInfo str, const RangeTblEntry *node)
 	WRITE_BOOL_FIELD(inh);
 	WRITE_BOOL_FIELD(inFromCl);
 	WRITE_NODE_FIELD(securityQuals);
+	WRITE_BITMAPSET_FIELD(eclass_member_indexes);
+	WRITE_BITMAPSET_FIELD(eclass_source_indexes);
+	WRITE_BITMAPSET_FIELD(eclass_derive_indexes);
 }
 
 static void
diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c
index 97e43cbb49..030ed3bc83 100644
--- a/src/backend/nodes/readfuncs.c
+++ b/src/backend/nodes/readfuncs.c
@@ -567,6 +567,9 @@ _readRangeTblEntry(void)
 	READ_BOOL_FIELD(inh);
 	READ_BOOL_FIELD(inFromCl);
 	READ_NODE_FIELD(securityQuals);
+	READ_BITMAPSET_FIELD(eclass_member_indexes);
+	READ_BITMAPSET_FIELD(eclass_source_indexes);
+	READ_BITMAPSET_FIELD(eclass_derive_indexes);
 
 	READ_DONE();
 }
diff --git a/src/backend/optimizer/path/equivclass.c b/src/backend/optimizer/path/equivclass.c
index 530f1e9984..bda89ce833 100644
--- a/src/backend/optimizer/path/equivclass.c
+++ b/src/backend/optimizer/path/equivclass.c
@@ -549,9 +549,9 @@ add_eq_source(PlannerInfo *root, EquivalenceClass *ec, RestrictInfo *rinfo)
 	i = -1;
 	while ((i = bms_next_member(rinfo->clause_relids, i)) >= 0)
 	{
-		RelOptInfo *rel = root->simple_rel_array[i];
+		RangeTblEntry *rte = root->simple_rte_array[i];
 
-		rel->eclass_source_indexes = bms_add_member(rel->eclass_source_indexes,
+		rte->eclass_source_indexes = bms_add_member(rte->eclass_source_indexes,
 													source_idx);
 	}
 }
@@ -571,9 +571,9 @@ add_eq_derive(PlannerInfo *root, EquivalenceClass *ec, RestrictInfo *rinfo)
 	i = -1;
 	while ((i = bms_next_member(rinfo->clause_relids, i)) >= 0)
 	{
-		RelOptInfo *rel = root->simple_rel_array[i];
+		RangeTblEntry *rte = root->simple_rte_array[i];
 
-		rel->eclass_derive_indexes = bms_add_member(rel->eclass_derive_indexes,
+		rte->eclass_derive_indexes = bms_add_member(rte->eclass_derive_indexes,
 													derive_idx);
 	}
 }
@@ -653,9 +653,10 @@ add_eq_member(PlannerInfo *root, EquivalenceClass *ec, Expr *expr, Relids relids
 	i = -1;
 	while ((i = bms_next_member(expr_relids, i)) >= 0)
 	{
-		RelOptInfo *rel = root->simple_rel_array[i];
+		RangeTblEntry *rte = root->simple_rte_array[i];
 
-		rel->eclass_member_indexes = bms_add_member(rel->eclass_member_indexes, em_index);
+		rte->eclass_member_indexes =
+			bms_add_member(rte->eclass_member_indexes, em_index);
 	}
 
 	return em;
@@ -2566,10 +2567,10 @@ reconsider_full_join_clause(PlannerInfo *root, OuterJoinClauseInfo *ojcinfo)
 			i = -1;
 			while ((i = bms_next_member(coal_em->em_relids, i)) >= 0)
 			{
-				RelOptInfo *rel = root->simple_rel_array[i];
+				RangeTblEntry *rte = root->simple_rte_array[i];
 
-				rel->eclass_member_indexes = bms_del_member(rel->eclass_member_indexes,
-															coal_idx);
+				rte->eclass_member_indexes =
+					bms_del_member(rte->eclass_member_indexes, coal_idx);
 			}
 
 			return true;
@@ -3516,9 +3517,9 @@ get_ecmember_indexes(PlannerInfo *root, EquivalenceClass *ec, Relids relids,
 
 	while ((i = bms_next_member(relids, i)) >= 0)
 	{
-		RelOptInfo *rel = root->simple_rel_array[i];
+		RangeTblEntry *rte = root->simple_rte_array[i];
 
-		rel_ems = bms_add_members(rel_ems, rel->eclass_member_indexes);
+		rel_ems = bms_add_members(rel_ems, rte->eclass_member_indexes);
 	}
 
 #ifdef USE_ASSERT_CHECKING
@@ -3564,7 +3565,7 @@ get_ecmember_indexes_strict(PlannerInfo *root, EquivalenceClass *ec,
 
 	if (i >= 0)
 	{
-		RelOptInfo *rel = root->simple_rel_array[i];
+		RangeTblEntry *rte = root->simple_rte_array[i];
 
 		/*
 		 * bms_intersect to the first relation to try to keep the resulting
@@ -3573,17 +3574,17 @@ get_ecmember_indexes_strict(PlannerInfo *root, EquivalenceClass *ec,
 		 * more words than the other.
 		 */
 		if (!with_children)
-			matching_ems = bms_intersect(rel->eclass_member_indexes,
+			matching_ems = bms_intersect(rte->eclass_member_indexes,
 										 ec->ec_nonchild_indexes);
 		else
-			matching_ems = bms_intersect(rel->eclass_member_indexes,
+			matching_ems = bms_intersect(rte->eclass_member_indexes,
 										 ec->ec_member_indexes);
 
 		while ((i = bms_next_member(relids, i)) >= 0)
 		{
-			rel = root->simple_rel_array[i];
+			rte = root->simple_rte_array[i];
 			matching_ems = bms_int_members(matching_ems,
-										   rel->eclass_member_indexes);
+										   rte->eclass_member_indexes);
 		}
 	}
 
@@ -3928,9 +3929,9 @@ get_ec_source_indexes(PlannerInfo *root, EquivalenceClass *ec, Relids relids)
 
 	while ((i = bms_next_member(relids, i)) >= 0)
 	{
-		RelOptInfo *rel = root->simple_rel_array[i];
+		RangeTblEntry *rte = root->simple_rte_array[i];
 
-		rel_esis = bms_add_members(rel_esis, rel->eclass_source_indexes);
+		rel_esis = bms_add_members(rel_esis, rte->eclass_source_indexes);
 	}
 
 #ifdef USE_ASSERT_CHECKING
@@ -3966,7 +3967,7 @@ get_ec_source_indexes_strict(PlannerInfo *root, EquivalenceClass *ec,
 
 	if (i >= 0)
 	{
-		RelOptInfo *rel = root->simple_rel_array[i];
+		RangeTblEntry *rte = root->simple_rte_array[i];
 
 		/*
 		 * bms_intersect to the first relation to try to keep the resulting
@@ -3975,12 +3976,12 @@ get_ec_source_indexes_strict(PlannerInfo *root, EquivalenceClass *ec,
 		 * more words than the other.
 		 */
 		esis = bms_intersect(ec->ec_source_indexes,
-							 rel->eclass_source_indexes);
+							 rte->eclass_source_indexes);
 
 		while ((i = bms_next_member(relids, i)) >= 0)
 		{
-			rel = root->simple_rel_array[i];
-			esis = bms_int_members(esis, rel->eclass_source_indexes);
+			rte = root->simple_rte_array[i];
+			esis = bms_int_members(esis, rte->eclass_source_indexes);
 		}
 	}
 
@@ -4017,9 +4018,9 @@ get_ec_derive_indexes(PlannerInfo *root, EquivalenceClass *ec, Relids relids)
 
 	while ((i = bms_next_member(relids, i)) >= 0)
 	{
-		RelOptInfo *rel = root->simple_rel_array[i];
+		RangeTblEntry *rte = root->simple_rte_array[i];
 
-		rel_edis = bms_add_members(rel_edis, rel->eclass_derive_indexes);
+		rel_edis = bms_add_members(rel_edis, rte->eclass_derive_indexes);
 	}
 
 #ifdef USE_ASSERT_CHECKING
@@ -4055,7 +4056,7 @@ get_ec_derive_indexes_strict(PlannerInfo *root, EquivalenceClass *ec,
 
 	if (i >= 0)
 	{
-		RelOptInfo *rel = root->simple_rel_array[i];
+		RangeTblEntry *rte = root->simple_rte_array[i];
 
 		/*
 		 * bms_intersect to the first relation to try to keep the resulting
@@ -4064,12 +4065,12 @@ get_ec_derive_indexes_strict(PlannerInfo *root, EquivalenceClass *ec,
 		 * more words than the other.
 		 */
 		edis = bms_intersect(ec->ec_derive_indexes,
-							 rel->eclass_derive_indexes);
+							 rte->eclass_derive_indexes);
 
 		while ((i = bms_next_member(relids, i)) >= 0)
 		{
-			rel = root->simple_rel_array[i];
-			edis = bms_int_members(edis, rel->eclass_derive_indexes);
+			rte = root->simple_rte_array[i];
+			edis = bms_int_members(edis, rte->eclass_derive_indexes);
 		}
 	}
 
diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index 0b61bda7a7..f0564b4a03 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -3109,8 +3109,10 @@ match_pathkeys_to_index(PlannerInfo *root, IndexOptInfo *index,
 		 * be considered to match more than one pathkey list, which is OK
 		 * here.  See also get_eclass_for_sort_expr.)
 		 */
-		matching_ems = bms_intersect(pathkey->pk_eclass->ec_member_indexes,
-									 index->rel->eclass_member_indexes);
+		matching_ems =
+			bms_intersect(pathkey->pk_eclass->ec_member_indexes,
+						  root->simple_rte_array[index->rel->relid]
+							  ->eclass_member_indexes);
 
 		i = -1;
 		while ((i = bms_next_member(matching_ems, i)) >= 0)
diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index 4c7d66311f..8eb054f8ed 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -185,7 +185,7 @@ plan_set_operations(PlannerInfo *root)
 
 /*
  * setup_append_rel_entry
- *		Add entry into root's simple_rel_array at element 0.  This is required
+ *		Add entry into root's simple_rte_array at element 0.  This is required
  *		because generate_append_tlist() makes Vars with varno=0.  In
  *		add_eq_member() we need to index EquivalenceMembers belonging to this
  *		relation.
@@ -193,14 +193,14 @@ plan_set_operations(PlannerInfo *root)
 static void
 setup_append_rel_entry(PlannerInfo *root)
 {
-	RelOptInfo *rel = makeNode(RelOptInfo);
+	RangeTblEntry *rte = makeNode(RangeTblEntry);
 
-	memset(rel, 0, sizeof(RelOptInfo));
-	rel->eclass_member_indexes = NULL;
+	memset(rte, 0, sizeof(RangeTblEntry));
+	rte->eclass_member_indexes = NULL;
 
 	/* make sure nothing else has made use of this element */
-	Assert(root->simple_rel_array[0] == NULL);
-	root->simple_rel_array[0] = rel;
+	Assert(root->simple_rte_array[0] == NULL);
+	root->simple_rte_array[0] = rte;
 }
 
 /*
diff --git a/src/backend/optimizer/util/inherit.c b/src/backend/optimizer/util/inherit.c
index 94de855a22..d4d50f342d 100644
--- a/src/backend/optimizer/util/inherit.c
+++ b/src/backend/optimizer/util/inherit.c
@@ -481,6 +481,14 @@ expand_single_inheritance_child(PlannerInfo *root, RangeTblEntry *parentrte,
 	 */
 	childrte = makeNode(RangeTblEntry);
 	memcpy(childrte, parentrte, sizeof(RangeTblEntry));
+	/*
+	 * We do not want to inherit the EquivalenceMember indexes of the parent
+	 * to its child
+	 */
+	childrte->eclass_member_indexes = NULL;
+	childrte->eclass_source_indexes = NULL;
+	childrte->eclass_derive_indexes = NULL;
+
 	Assert(parentrte->rtekind == RTE_RELATION); /* else this is dubious */
 	childrte->relid = childOID;
 	childrte->relkind = childrel->rd_rel->relkind;
diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c
index c298b8ff7a..15e3910b79 100644
--- a/src/backend/optimizer/util/relnode.c
+++ b/src/backend/optimizer/util/relnode.c
@@ -228,9 +228,6 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
 	rel->tuples = 0;
 	rel->allvisfrac = 0;
 	rel->eclass_indexes = NULL;
-	rel->eclass_member_indexes = NULL;
-	rel->eclass_source_indexes = NULL;
-	rel->eclass_derive_indexes = NULL;
 	rel->subroot = NULL;
 	rel->subplan_params = NIL;
 	rel->rel_parallel_workers = -1; /* set up in get_relation_info */
@@ -719,9 +716,6 @@ build_join_rel(PlannerInfo *root,
 	joinrel->tuples = 0;
 	joinrel->allvisfrac = 0;
 	joinrel->eclass_indexes = NULL;
-	joinrel->eclass_member_indexes = NULL;
-	joinrel->eclass_source_indexes = NULL;
-	joinrel->eclass_derive_indexes = NULL;
 	joinrel->subroot = NULL;
 	joinrel->subplan_params = NIL;
 	joinrel->rel_parallel_workers = -1;
@@ -910,9 +904,6 @@ build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel,
 	joinrel->tuples = 0;
 	joinrel->allvisfrac = 0;
 	joinrel->eclass_indexes = NULL;
-	joinrel->eclass_member_indexes = NULL;
-	joinrel->eclass_source_indexes = NULL;
-	joinrel->eclass_derive_indexes = NULL;
 	joinrel->subroot = NULL;
 	joinrel->subplan_params = NIL;
 	joinrel->amflags = 0;
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index 88b03cc472..fb2baa45c3 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -1202,6 +1202,15 @@ typedef struct RangeTblEntry
 	bool		inh;			/* inheritance requested? */
 	bool		inFromCl;		/* present in FROM clause? */
 	List	   *securityQuals;	/* security barrier quals to apply, if any */
+	Bitmapset  *eclass_member_indexes;	/* Indexes in PlannerInfo's eq_members
+										 * list of EquivalenceMembers that
+										 * mention this relation */
+	Bitmapset  *eclass_source_indexes;	/* Indexes in PlannerInfo's eq_sources
+										 * list for RestrictInfos that mention
+										 * this relation */
+	Bitmapset  *eclass_derive_indexes;	/* Indexes in PlannerInfo's eq_derives
+										 * list for RestrictInfos that mention
+										 * this relation */
 } RangeTblEntry;
 
 /*
diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h
index 6e94acb1a7..3a45bcc3f4 100644
--- a/src/include/nodes/pathnodes.h
+++ b/src/include/nodes/pathnodes.h
@@ -939,23 +939,6 @@ typedef struct RelOptInfo
 	/* indexes in PlannerInfo's eq_classes list of ECs that mention this rel */
 	Bitmapset  *eclass_indexes;
 
-	/*
-	 * Indexes in PlannerInfo's eq_members list of EMs that mention this rel
-	 */
-	Bitmapset  *eclass_member_indexes;
-
-	/*
-	 * Indexes in PlannerInfo's eq_sources list for RestrictInfos that mention
-	 * this relation.
-	 */
-	Bitmapset  *eclass_source_indexes;
-
-	/*
-	 * Indexes in PlannerInfo's eq_derives list for RestrictInfos that mention
-	 * this relation.
-	 */
-	Bitmapset  *eclass_derive_indexes;
-
 	PlannerInfo *subroot;		/* if subquery */
 	List	   *subplan_params; /* if subquery */
 	/* wanted number of parallel workers */
-- 
2.41.0.windows.1

