From: Tom Lane Date: Thu, 21 Feb 2019 01:53:08 +0000 (-0500) Subject: Speed up match_eclasses_to_foreign_key_col() when there are many ECs. X-Git-Tag: REL_12_BETA1~697 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fa86238f1eac5b1bfb3887d2deb138f53da82c9b;p=postgresql Speed up match_eclasses_to_foreign_key_col() when there are many ECs. Check ec_relids before bothering to iterate through the EC members. On a perhaps extreme, but still real-world, query in which match_eclasses_to_foreign_key_col() accounts for the bulk of the planner's runtime, this saves nearly 40% of the runtime. It's a bit of a stopgap fix, but it's simple enough to be back-patched to 9.6 where this code came in; so let's do that. David Rowley Discussion: https://postgr.es/m/6970.1545327857@sss.pgh.pa.us --- diff --git a/src/backend/optimizer/path/equivclass.c b/src/backend/optimizer/path/equivclass.c index 23792508b7..61b5b119b0 100644 --- a/src/backend/optimizer/path/equivclass.c +++ b/src/backend/optimizer/path/equivclass.c @@ -2052,6 +2052,14 @@ match_eclasses_to_foreign_key_col(PlannerInfo *root, continue; /* Note: it seems okay to match to "broken" eclasses here */ + /* + * If eclass visibly doesn't have members for both rels, there's no + * need to grovel through the members. + */ + if (!bms_is_member(var1varno, ec->ec_relids) || + !bms_is_member(var2varno, ec->ec_relids)) + continue; + foreach(lc2, ec->ec_members) { EquivalenceMember *em = (EquivalenceMember *) lfirst(lc2);