* pathkeys_useful_for_merging * Count the number of pathkeys that may be useful for mergejoins * above the given relation. * * We consider a pathkey potentially useful if it corresponds to the merge * ordering of either side of any joinclause for the rel. This might be * overoptimistic, since joinclauses that require different other relations * might never be usable at the same time, but
| 2346 | * right_merge_direction() implements this heuristic. |
| 2347 | */ |
| 2348 | static int |
| 2349 | pathkeys_useful_for_merging(PlannerInfo *root, RelOptInfo *rel, List *pathkeys) |
| 2350 | { |
| 2351 | int useful = 0; |
| 2352 | ListCell *i; |
| 2353 | |
| 2354 | foreach(i, pathkeys) |
| 2355 | { |
| 2356 | PathKey *pathkey = (PathKey *) lfirst(i); |
| 2357 | bool matched = false; |
| 2358 | ListCell *j; |
| 2359 | |
| 2360 | /* If "wrong" direction, not useful for merging */ |
| 2361 | if (!right_merge_direction(root, pathkey)) |
| 2362 | break; |
| 2363 | |
| 2364 | /* |
| 2365 | * First look into the EquivalenceClass of the pathkey, to see if |
| 2366 | * there are any members not yet joined to the rel. If so, it's |
| 2367 | * surely possible to generate a mergejoin clause using them. |
| 2368 | */ |
| 2369 | if (rel->has_eclass_joins && |
| 2370 | eclass_useful_for_merging(root, pathkey->pk_eclass, rel)) |
| 2371 | matched = true; |
| 2372 | else |
| 2373 | { |
| 2374 | /* |
| 2375 | * Otherwise search the rel's joininfo list, which contains |
| 2376 | * non-EquivalenceClass-derivable join clauses that might |
| 2377 | * nonetheless be mergejoinable. |
| 2378 | */ |
| 2379 | foreach(j, rel->joininfo) |
| 2380 | { |
| 2381 | RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(j); |
| 2382 | |
| 2383 | if (restrictinfo->mergeopfamilies == NIL) |
| 2384 | continue; |
| 2385 | update_mergeclause_eclasses(root, restrictinfo); |
| 2386 | |
| 2387 | if (pathkey->pk_eclass == restrictinfo->left_ec || |
| 2388 | pathkey->pk_eclass == restrictinfo->right_ec) |
| 2389 | { |
| 2390 | matched = true; |
| 2391 | break; |
| 2392 | } |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | /* |
| 2397 | * If we didn't find a mergeclause, we're done --- any additional |
| 2398 | * sort-key positions in the pathkeys are useless. (But we can still |
| 2399 | * mergejoin if we found at least one mergeclause.) |
| 2400 | */ |
| 2401 | if (matched) |
| 2402 | useful++; |
| 2403 | else |
| 2404 | break; |
| 2405 | } |
no test coverage detected