diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index 1add6c4..8e24901 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -2228,6 +2228,7 @@ _outPlannerInfo(StringInfo str, const PlannerInfo *node)
 	WRITE_NODE_FIELD(curOuterParams);
 	WRITE_BOOL_FIELD(partColsUpdated);
 	WRITE_BOOL_FIELD(contains_inherit_children);
+	WRITE_BOOL_FIELD(last_baserel_idx);
 }
 
 static void
diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c
index 31c8f16..00edb59 100644
--- a/src/backend/optimizer/path/allpaths.c
+++ b/src/backend/optimizer/path/allpaths.c
@@ -157,7 +157,7 @@ make_one_rel(PlannerInfo *root, List *joinlist)
 	 * Construct the all_baserels Relids set.
 	 */
 	root->all_baserels = NULL;
-	for (rti = 1; rti < root->simple_rel_array_size; rti++)
+	for (rti = 1; rti < root->last_baserel_idx; rti++)
 	{
 		RelOptInfo *brel = root->simple_rel_array[rti];
 
@@ -167,10 +167,6 @@ make_one_rel(PlannerInfo *root, List *joinlist)
 
 		Assert(brel->relid == rti); /* sanity check on array */
 
-		/* ignore RTEs that are "other rels" */
-		if (brel->reloptkind != RELOPT_BASEREL)
-			continue;
-
 		root->all_baserels = bms_add_member(root->all_baserels, brel->relid);
 	}
 
@@ -290,7 +286,7 @@ set_base_rel_sizes(PlannerInfo *root)
 {
 	Index		rti;
 
-	for (rti = 1; rti < root->simple_rel_array_size; rti++)
+	for (rti = 1; rti < root->last_baserel_idx; rti++)
 	{
 		RelOptInfo *rel = root->simple_rel_array[rti];
 		RangeTblEntry *rte;
@@ -333,7 +329,7 @@ set_base_rel_pathlists(PlannerInfo *root)
 {
 	Index		rti;
 
-	for (rti = 1; rti < root->simple_rel_array_size; rti++)
+	for (rti = 1; rti < root->last_baserel_idx; rti++)
 	{
 		RelOptInfo *rel = root->simple_rel_array[rti];
 
@@ -1955,7 +1951,7 @@ has_multiple_baserels(PlannerInfo *root)
 	int			num_base_rels = 0;
 	Index		rti;
 
-	for (rti = 1; rti < root->simple_rel_array_size; rti++)
+	for (rti = 1; rti < root->last_baserel_idx; rti++)
 	{
 		RelOptInfo *brel = root->simple_rel_array[rti];
 
diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index 077d320..1d86e6d 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -333,7 +333,7 @@ find_lateral_references(PlannerInfo *root)
 	/*
 	 * Examine all baserels (the rel array has been set up by now).
 	 */
-	for (rti = 1; rti < root->simple_rel_array_size; rti++)
+	for (rti = 1; rti < root->last_baserel_idx; rti++)
 	{
 		RelOptInfo *brel = root->simple_rel_array[rti];
 
@@ -482,7 +482,7 @@ create_lateral_join_info(PlannerInfo *root)
 	/*
 	 * Examine all baserels (the rel array has been set up by now).
 	 */
-	for (rti = 1; rti < root->simple_rel_array_size; rti++)
+	for (rti = 1; rti < root->last_baserel_idx; rti++)
 	{
 		RelOptInfo *brel = root->simple_rel_array[rti];
 		Relids		lateral_relids;
@@ -602,7 +602,7 @@ create_lateral_join_info(PlannerInfo *root)
 	 * The outer loop considers each baserel, and propagates its lateral
 	 * dependencies to those baserels that have a lateral dependency on it.
 	 */
-	for (rti = 1; rti < root->simple_rel_array_size; rti++)
+	for (rti = 1; rti < root->last_baserel_idx; rti++)
 	{
 		RelOptInfo *brel = root->simple_rel_array[rti];
 		Relids		outer_lateral_relids;
@@ -617,7 +617,7 @@ create_lateral_join_info(PlannerInfo *root)
 			continue;
 
 		/* else scan all baserels */
-		for (rti2 = 1; rti2 < root->simple_rel_array_size; rti2++)
+		for (rti2 = 1; rti2 < root->last_baserel_idx; rti2++)
 		{
 			RelOptInfo *brel2 = root->simple_rel_array[rti2];
 
@@ -636,7 +636,7 @@ create_lateral_join_info(PlannerInfo *root)
 	 * with the set of relids of rels that reference it laterally (possibly
 	 * indirectly) --- that is, the inverse mapping of lateral_relids.
 	 */
-	for (rti = 1; rti < root->simple_rel_array_size; rti++)
+	for (rti = 1; rti < root->last_baserel_idx; rti++)
 	{
 		RelOptInfo *brel = root->simple_rel_array[rti];
 		Relids		lateral_relids;
diff --git a/src/backend/optimizer/plan/planmain.c b/src/backend/optimizer/plan/planmain.c
index 42c2130..b5fe532 100644
--- a/src/backend/optimizer/plan/planmain.c
+++ b/src/backend/optimizer/plan/planmain.c
@@ -170,6 +170,12 @@ query_planner(PlannerInfo *root, List **tlist,
 	 * for example views.  We don't want to make RelOptInfos for them.
 	 */
 	add_base_rels_to_query(root, (Node *) parse->jointree);
+	
+	/*
+	 *  Save the last index of baserel for further optimization of baserel
+	 *  loop.
+	 */
+	root->last_baserel_idx = root->simple_rel_array_size;
 
 	/*
 	 * Examine the targetlist and join tree, adding entries to baserel
diff --git a/src/backend/optimizer/util/orclauses.c b/src/backend/optimizer/util/orclauses.c
index b671581..a24a9d1 100644
--- a/src/backend/optimizer/util/orclauses.c
+++ b/src/backend/optimizer/util/orclauses.c
@@ -78,7 +78,7 @@ extract_restriction_or_clauses(PlannerInfo *root)
 	Index		rti;
 
 	/* Examine each baserel for potential join OR clauses */
-	for (rti = 1; rti < root->simple_rel_array_size; rti++)
+	for (rti = 1; rti < root->last_baserel_idx; rti++)
 	{
 		RelOptInfo *rel = root->simple_rel_array[rti];
 		ListCell   *lc;
diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h
index 3298bd7..f78ba28 100644
--- a/src/include/nodes/pathnodes.h
+++ b/src/include/nodes/pathnodes.h
@@ -201,6 +201,7 @@ struct PlannerInfo
 	 */
 	struct RelOptInfo **simple_rel_array;	/* All 1-rel RelOptInfos */
 	int			simple_rel_array_size;	/* allocated size of array */
+	int			last_baserel_idx;	/* last index of baserel in the array*/
 
 	/*
 	 * simple_rte_array is the same length as simple_rel_array and holds
