diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c
index 67921a08262..e2fb8ce0499 100644
--- a/src/backend/optimizer/path/allpaths.c
+++ b/src/backend/optimizer/path/allpaths.c
@@ -205,11 +205,9 @@ make_one_rel(PlannerInfo *root, List *joinlist)
 		RelOptInfo *brel = root->simple_rel_array[rti];
 
 		/* there may be empty slots corresponding to non-baserel RTEs */
-		if (brel == NULL)
+		if (brel == NULL || brel->relid != rti)
 			continue;
 
-		Assert(brel->relid == rti); /* sanity check on array */
-
 		if (IS_DUMMY_REL(brel))
 			continue;
 
@@ -300,11 +298,9 @@ set_base_rel_sizes(PlannerInfo *root)
 		RangeTblEntry *rte;
 
 		/* there may be empty slots corresponding to non-baserel RTEs */
-		if (rel == NULL)
+		if (rel == NULL || rel->relid != rti)
 			continue;
 
-		Assert(rel->relid == rti);	/* sanity check on array */
-
 		/* ignore RTEs that are "other rels" */
 		if (rel->reloptkind != RELOPT_BASEREL)
 			continue;
@@ -342,11 +338,9 @@ set_base_rel_pathlists(PlannerInfo *root)
 		RelOptInfo *rel = root->simple_rel_array[rti];
 
 		/* there may be empty slots corresponding to non-baserel RTEs */
-		if (rel == NULL)
+		if (rel == NULL || rel->relid != rti)
 			continue;
 
-		Assert(rel->relid == rti);	/* sanity check on array */
-
 		/* ignore RTEs that are "other rels" */
 		if (rel->reloptkind != RELOPT_BASEREL)
 			continue;
diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index 03a5fbdc6dc..76784c7ccb9 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -1900,9 +1900,8 @@ get_loop_count(PlannerInfo *root, Index cur_relid, Relids outer_relids)
 		if (outer_relid >= root->simple_rel_array_size)
 			continue;
 		outer_rel = root->simple_rel_array[outer_relid];
-		if (outer_rel == NULL)
+		if (outer_rel == NULL || outer_rel->relid != outer_relid)
 			continue;
-		Assert(outer_rel->relid == outer_relid);	/* sanity check on array */
 
 		/* Other relation could be proven empty, if so ignore */
 		if (IS_DUMMY_REL(outer_rel))
@@ -1991,9 +1990,8 @@ approximate_joinrel_size(PlannerInfo *root, Relids relids)
 		if (relid >= root->simple_rel_array_size)
 			continue;
 		rel = root->simple_rel_array[relid];
-		if (rel == NULL)
+		if (rel == NULL || rel->relid != relid)
 			continue;
-		Assert(rel->relid == relid);	/* sanity check on array */
 
 		/* Relation could be proven empty, if so ignore */
 		if (IS_DUMMY_REL(rel))
diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/plan/analyzejoins.c
index ccf07b2077c..73b5140945f 100644
--- a/src/backend/optimizer/plan/analyzejoins.c
+++ b/src/backend/optimizer/plan/analyzejoins.c
@@ -375,11 +375,9 @@ remove_rel_from_query(PlannerInfo *root, RelOptInfo *rel,
 		int			attroff;
 
 		/* there may be empty slots corresponding to non-baserel RTEs */
-		if (otherrel == NULL)
+		if (otherrel == NULL || otherrel->relid != rti)
 			continue;
 
-		Assert(otherrel->relid == rti); /* sanity check on array */
-
 		/* no point in processing target rel itself */
 		if (otherrel == rel)
 			continue;
@@ -1890,7 +1888,7 @@ remove_self_join_rel(PlannerInfo *root, PlanRowMark *kmark, PlanRowMark *rmark,
 	 * referenced again.  (We can't do this earlier because
 	 * remove_join_clause_from_rels will touch it.)
 	 */
-	root->simple_rel_array[toRemove->relid] = NULL;
+	root->simple_rel_array[toRemove->relid] = root->simple_rel_array[toKeep->relid];
 
 	/* And nuke the RelOptInfo, just in case there's another access path */
 	pfree(toRemove);
diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index b31d8921211..d652e39a640 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -372,11 +372,9 @@ find_lateral_references(PlannerInfo *root)
 		RelOptInfo *brel = root->simple_rel_array[rti];
 
 		/* there may be empty slots corresponding to non-baserel RTEs */
-		if (brel == NULL)
+		if (brel == NULL || brel->relid != rti)
 			continue;
 
-		Assert(brel->relid == rti); /* sanity check on array */
-
 		/*
 		 * This bit is less obvious than it might look.  We ignore appendrel
 		 * otherrels and consider only their parent baserels.  In a case where
@@ -521,11 +519,9 @@ create_lateral_join_info(PlannerInfo *root)
 		Relids		lateral_relids;
 
 		/* there may be empty slots corresponding to non-baserel RTEs */
-		if (brel == NULL)
+		if (brel == NULL || brel->relid != rti)
 			continue;
 
-		Assert(brel->relid == rti); /* sanity check on array */
-
 		/* ignore RTEs that are "other rels" */
 		if (brel->reloptkind != RELOPT_BASEREL)
 			continue;
diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c
index fc3709510d2..deb8a97684f 100644
--- a/src/backend/optimizer/plan/setrefs.c
+++ b/src/backend/optimizer/plan/setrefs.c
@@ -438,10 +438,8 @@ add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
 		{
 			RelOptInfo *rel = root->simple_rel_array[rti];
 
-			if (rel != NULL)
+			if (rel != NULL && rel->relid == rti)
 			{
-				Assert(rel->relid == rti);	/* sanity check on array */
-
 				/*
 				 * The subquery might never have been planned at all, if it
 				 * was excluded on the basis of self-contradictory constraints
diff --git a/src/backend/optimizer/util/orclauses.c b/src/backend/optimizer/util/orclauses.c
index 6ef9d14b902..4f87d3d412e 100644
--- a/src/backend/optimizer/util/orclauses.c
+++ b/src/backend/optimizer/util/orclauses.c
@@ -84,11 +84,9 @@ extract_restriction_or_clauses(PlannerInfo *root)
 		ListCell   *lc;
 
 		/* there may be empty slots corresponding to non-baserel RTEs */
-		if (rel == NULL)
+		if (rel == NULL || rel->relid != rti)
 			continue;
 
-		Assert(rel->relid == rti);	/* sanity check on array */
-
 		/* ignore RTEs that are "other rels" */
 		if (rel->reloptkind != RELOPT_BASEREL)
 			continue;
