sanity-check.txt
text/plain
Filename: sanity-check.txt
Type: text/plain
Part: 0
diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/plan/analyzejoins.c
index 2da2a6c14e..334ea8d3bb 100644
--- a/src/backend/optimizer/plan/analyzejoins.c
+++ b/src/backend/optimizer/plan/analyzejoins.c
@@ -1751,6 +1751,48 @@ update_eclasses(PlannerInfo *root, EquivalenceClass *ec, int from, int to)
bms_free(ec->ec_source_indexes);
ec->ec_source_indexes = new_source_indexes;
ec->ec_relids = replace_relid(ec->ec_relids, from, to);
+
+#ifdef USE_ASSERT_CHECKING
+ /*
+ * Sanity check:
+ * Verify that there are no missing references between RestrictInfos and
+ * our indexes, namely eclass_source_indexes and eclass_derive_indexes.
+ */
+
+ foreach(lc, root->eq_sources)
+ {
+ RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
+ int index;
+ int k;
+
+ if (rinfo == NULL)
+ continue;
+
+ index = list_cell_number(root->eq_sources, lc);
+ k = -1;
+ while ((k = bms_next_member(rinfo->clause_relids, k)) >= 0)
+ {
+ Assert(bms_is_member(index, root->simple_rte_array[k]->eclass_source_indexes));
+ }
+ }
+
+ foreach(lc, root->eq_derives)
+ {
+ RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
+ int index;
+ int k;
+
+ if (rinfo == NULL)
+ continue;
+
+ index = list_cell_number(root->eq_derives, lc);
+ k = -1;
+ while ((k = bms_next_member(rinfo->clause_relids, k)) >= 0)
+ {
+ Assert(bms_is_member(index, root->simple_rte_array[k]->eclass_derive_indexes));
+ }
+ }
+#endif
}
/*