/* * get_combiner_join_rel * * Gets the input rel for a combine plan, which only ever needs to read from a TuplestoreScan * because the workers have already done most of the work */ static RelOptInfo * get_combiner_join_rel(PlannerInfo *root, int levels_needed, List *initial_rels) { RelOptInfo *rel; Path *path; rel = standard_join_search(root, levels_needed, initial_rels); rel->pathlist = NIL; path = create_tuplestore_scan_path(rel); add_path(rel, path); set_cheapest(rel); return rel; }
/* * query_planner * Generate a path (that is, a simplified plan) for a basic query, * which may involve joins but not any fancier features. * * Since query_planner does not handle the toplevel processing (grouping, * sorting, etc) it cannot select the best path by itself. Instead, it * returns the RelOptInfo for the top level of joining, and the caller * (grouping_planner) can choose one of the surviving paths for the rel. * Normally it would choose either the rel's cheapest path, or the cheapest * path for the desired sort order. * * root describes the query to plan * tlist is the target list the query should produce * (this is NOT necessarily root->parse->targetList!) * qp_callback is a function to compute query_pathkeys once it's safe to do so * qp_extra is optional extra data to pass to qp_callback * * Note: the PlannerInfo node also includes a query_pathkeys field, which * tells query_planner the sort order that is desired in the final output * plan. This value is *not* available at call time, but is computed by * qp_callback once we have completed merging the query's equivalence classes. * (We cannot construct canonical pathkeys until that's done.) */ RelOptInfo * query_planner(PlannerInfo *root, List *tlist, query_pathkeys_callback qp_callback, void *qp_extra) { Query *parse = root->parse; List *joinlist; RelOptInfo *final_rel; Index rti; double total_pages; /* * If the query has an empty join tree, then it's something easy like * "SELECT 2+2;" or "INSERT ... VALUES()". Fall through quickly. */ if (parse->jointree->fromlist == NIL) { /* We need a dummy joinrel to describe the empty set of baserels */ final_rel = build_empty_join_rel(root); /* The only path for it is a trivial Result path */ add_path(final_rel, (Path *) create_result_path((List *) parse->jointree->quals)); /* Select cheapest path (pretty easy in this case...) */ set_cheapest(final_rel); /* * We still are required to call qp_callback, in case it's something * like "SELECT 2+2 ORDER BY 1". */ root->canon_pathkeys = NIL; (*qp_callback) (root, qp_extra); return final_rel; } /* * Init planner lists to empty. * * NOTE: append_rel_list was set up by subquery_planner, so do not touch * here; eq_classes and minmax_aggs may contain data already, too. */ root->join_rel_list = NIL; root->join_rel_hash = NULL; root->join_rel_level = NULL; root->join_cur_level = 0; root->canon_pathkeys = NIL; root->left_join_clauses = NIL; root->right_join_clauses = NIL; root->full_join_clauses = NIL; root->join_info_list = NIL; root->lateral_info_list = NIL; root->placeholder_list = NIL; root->initial_rels = NIL; /* * Make a flattened version of the rangetable for faster access (this is * OK because the rangetable won't change any more), and set up an empty * array for indexing base relations. */ setup_simple_rel_arrays(root); /* * Construct RelOptInfo nodes for all base relations in query, and * indirectly for all appendrel member relations ("other rels"). This * will give us a RelOptInfo for every "simple" (non-join) rel involved in * the query. * * Note: the reason we find the rels by searching the jointree and * appendrel list, rather than just scanning the rangetable, is that the * rangetable may contain RTEs for rels not actively part of the query, * for example views. We don't want to make RelOptInfos for them. */ add_base_rels_to_query(root, (Node *) parse->jointree); /* * Examine the targetlist and join tree, adding entries to baserel * targetlists for all referenced Vars, and generating PlaceHolderInfo * entries for all referenced PlaceHolderVars. Restrict and join clauses * are added to appropriate lists belonging to the mentioned relations. We * also build EquivalenceClasses for provably equivalent expressions. The * SpecialJoinInfo list is also built to hold information about join order * restrictions. Finally, we form a target joinlist for make_one_rel() to * work from. */ build_base_rel_tlists(root, tlist); find_placeholders_in_jointree(root); find_lateral_references(root); joinlist = deconstruct_jointree(root); /* * Reconsider any postponed outer-join quals now that we have built up * equivalence classes. (This could result in further additions or * mergings of classes.) */ reconsider_outer_join_clauses(root); /* * If we formed any equivalence classes, generate additional restriction * clauses as appropriate. (Implied join clauses are formed on-the-fly * later.) */ generate_base_implied_equalities(root); /* * We have completed merging equivalence sets, so it's now possible to * generate pathkeys in canonical form; so compute query_pathkeys and * other pathkeys fields in PlannerInfo. */ (*qp_callback) (root, qp_extra); /* * Examine any "placeholder" expressions generated during subquery pullup. * Make sure that the Vars they need are marked as needed at the relevant * join level. This must be done before join removal because it might * cause Vars or placeholders to be needed above a join when they weren't * so marked before. */ fix_placeholder_input_needed_levels(root); /* * Remove any useless outer joins. Ideally this would be done during * jointree preprocessing, but the necessary information isn't available * until we've built baserel data structures and classified qual clauses. */ joinlist = remove_useless_joins(root, joinlist); /* * Now distribute "placeholders" to base rels as needed. This has to be * done after join removal because removal could change whether a * placeholder is evaluatable at a base rel. */ add_placeholders_to_base_rels(root); /* * Create the LateralJoinInfo list now that we have finalized * PlaceHolderVar eval levels and made any necessary additions to the * lateral_vars lists for lateral references within PlaceHolderVars. */ create_lateral_join_info(root); /* * Look for join OR clauses that we can extract single-relation * restriction OR clauses from. */ extract_restriction_or_clauses(root); /* * We should now have size estimates for every actual table involved in * the query, and we also know which if any have been deleted from the * query by join removal; so we can compute total_table_pages. * * Note that appendrels are not double-counted here, even though we don't * bother to distinguish RelOptInfos for appendrel parents, because the * parents will still have size zero. * * XXX if a table is self-joined, we will count it once per appearance, * which perhaps is the wrong thing ... but that's not completely clear, * and detecting self-joins here is difficult, so ignore it for now. */ total_pages = 0; for (rti = 1; rti < root->simple_rel_array_size; rti++) { RelOptInfo *brel = root->simple_rel_array[rti]; if (brel == NULL) continue; Assert(brel->relid == rti); /* sanity check on array */ if (brel->reloptkind == RELOPT_BASEREL || brel->reloptkind == RELOPT_OTHER_MEMBER_REL) total_pages += (double) brel->pages; } root->total_table_pages = total_pages; /* * Ready to do the primary planning. */ final_rel = make_one_rel(root, joinlist); /* Check that we got at least one usable path */ if (!final_rel || !final_rel->cheapest_total_path || final_rel->cheapest_total_path->param_info != NULL) elog(ERROR, "failed to construct the join relation"); return final_rel; }
/* * gimme_tree * Form planner estimates for a join tree constructed in the specified * order. * * 'tour' is the proposed join order, of length 'num_gene' * 'evaldata' contains the context we need * * Returns a new join relation whose cheapest path is the best plan for * this join order. NB: will return NULL if join order is invalid. * * The original implementation of this routine always joined in the specified * order, and so could only build left-sided plans (and right-sided and * mixtures, as a byproduct of the fact that make_join_rel() is symmetric). * It could never produce a "bushy" plan. This had a couple of big problems, * of which the worst was that as of 7.4, there are situations involving IN * subqueries where the only valid plans are bushy. * * The present implementation takes the given tour as a guideline, but * postpones joins that seem unsuitable according to some heuristic rules. * This allows correct bushy plans to be generated at need, and as a nice * side-effect it seems to materially improve the quality of the generated * plans. */ RelOptInfo * gimme_tree(Gene *tour, int num_gene, GeqoEvalData *evaldata) { RelOptInfo **stack; int stack_depth; RelOptInfo *joinrel; int rel_count; /* * Create a stack to hold not-yet-joined relations. */ stack = (RelOptInfo **) palloc(num_gene * sizeof(RelOptInfo *)); stack_depth = 0; /* * Push each relation onto the stack in the specified order. After * pushing each relation, see whether the top two stack entries are * joinable according to the desirable_join() heuristics. If so, join * them into one stack entry, and try again to combine with the next stack * entry down (if any). When the stack top is no longer joinable, * continue to the next input relation. After we have pushed the last * input relation, the heuristics are disabled and we force joining all * the remaining stack entries. * * If desirable_join() always returns true, this produces a straight * left-to-right join just like the old code. Otherwise we may produce a * bushy plan or a left/right-sided plan that really corresponds to some * tour other than the one given. To the extent that the heuristics are * helpful, however, this will be a better plan than the raw tour. * * Also, when a join attempt fails (because of OJ or IN constraints), we * may be able to recover and produce a workable plan, where the old code * just had to give up. This case acts the same as a false result from * desirable_join(). */ for (rel_count = 0; rel_count < num_gene; rel_count++) { int cur_rel_index; /* Get the next input relation and push it */ cur_rel_index = (int) tour[rel_count]; stack[stack_depth] = (RelOptInfo *) list_nth(evaldata->initial_rels, cur_rel_index - 1); stack_depth++; /* * While it's feasible, pop the top two stack entries and replace with * their join. */ while (stack_depth >= 2) { RelOptInfo *outer_rel = stack[stack_depth - 2]; RelOptInfo *inner_rel = stack[stack_depth - 1]; /* * Don't pop if heuristics say not to join now. However, once we * have exhausted the input, the heuristics can't prevent popping. */ if (rel_count < num_gene - 1 && !desirable_join(evaldata->root, outer_rel, inner_rel)) break; /* * Construct a RelOptInfo representing the join of these two input * relations. Note that we expect the joinrel not to exist in * root->join_rel_list yet, and so the paths constructed for it * will only include the ones we want. */ joinrel = make_join_rel(evaldata->root, outer_rel, inner_rel); /* Can't pop stack here if join order is not valid */ if (!joinrel) break; /* Find and save the cheapest paths for this rel */ set_cheapest(joinrel); /* Pop the stack and replace the inputs with their join */ stack_depth--; stack[stack_depth - 1] = joinrel; } } /* Did we succeed in forming a single join relation? */ if (stack_depth == 1) joinrel = stack[0]; else joinrel = NULL; pfree(stack); return joinrel; }