📄 planner.c
字号:
/*------------------------------------------------------------------------- * * planner.c * The query optimizer external interface. * * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION * $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.194.2.1 2005/11/22 18:23:11 momjian Exp $ * *------------------------------------------------------------------------- */#include "postgres.h"#include <limits.h>#include "catalog/pg_operator.h"#include "catalog/pg_type.h"#include "executor/executor.h"#include "executor/nodeAgg.h"#include "miscadmin.h"#include "nodes/makefuncs.h"#ifdef OPTIMIZER_DEBUG#include "nodes/print.h"#endif#include "optimizer/clauses.h"#include "optimizer/cost.h"#include "optimizer/pathnode.h"#include "optimizer/paths.h"#include "optimizer/planmain.h"#include "optimizer/planner.h"#include "optimizer/prep.h"#include "optimizer/subselect.h"#include "optimizer/tlist.h"#include "optimizer/var.h"#include "parser/parsetree.h"#include "parser/parse_expr.h"#include "parser/parse_oper.h"#include "utils/selfuncs.h"#include "utils/syscache.h"ParamListInfo PlannerBoundParamList = NULL; /* current boundParams *//* Expression kind codes for preprocess_expression */#define EXPRKIND_QUAL 0#define EXPRKIND_TARGET 1#define EXPRKIND_RTFUNC 2#define EXPRKIND_LIMIT 3#define EXPRKIND_ININFO 4static Node *preprocess_expression(PlannerInfo *root, Node *expr, int kind);static void preprocess_qual_conditions(PlannerInfo *root, Node *jtnode);static Plan *inheritance_planner(PlannerInfo *root, List *inheritlist);static Plan *grouping_planner(PlannerInfo *root, double tuple_fraction);static double preprocess_limit(PlannerInfo *root, double tuple_fraction, int *offset_est, int *count_est);static bool choose_hashed_grouping(PlannerInfo *root, double tuple_fraction, Path *cheapest_path, Path *sorted_path, double dNumGroups, AggClauseCounts *agg_counts);static bool hash_safe_grouping(PlannerInfo *root);static List *make_subplanTargetList(PlannerInfo *root, List *tlist, AttrNumber **groupColIdx, bool *need_tlist_eval);static void locate_grouping_columns(PlannerInfo *root, List *tlist, List *sub_tlist, AttrNumber *groupColIdx);static List *postprocess_setop_tlist(List *new_tlist, List *orig_tlist);/***************************************************************************** * * Query optimizer entry point * *****************************************************************************/Plan *planner(Query *parse, bool isCursor, int cursorOptions, ParamListInfo boundParams){ double tuple_fraction; Plan *result_plan; Index save_PlannerQueryLevel; List *save_PlannerParamList; ParamListInfo save_PlannerBoundParamList; /* * The planner can be called recursively (an example is when * eval_const_expressions tries to pre-evaluate an SQL function). So, * these global state variables must be saved and restored. * * Query level and the param list cannot be moved into the per-query * PlannerInfo structure since their whole purpose is communication across * multiple sub-queries. Also, boundParams is explicitly info from outside * the query, and so is likewise better handled as a global variable. * * Note we do NOT save and restore PlannerPlanId: it exists to assign * unique IDs to SubPlan nodes, and we want those IDs to be unique for the * life of a backend. Also, PlannerInitPlan is saved/restored in * subquery_planner, not here. */ save_PlannerQueryLevel = PlannerQueryLevel; save_PlannerParamList = PlannerParamList; save_PlannerBoundParamList = PlannerBoundParamList; /* Initialize state for handling outer-level references and params */ PlannerQueryLevel = 0; /* will be 1 in top-level subquery_planner */ PlannerParamList = NIL; PlannerBoundParamList = boundParams; /* Determine what fraction of the plan is likely to be scanned */ if (isCursor) { /* * We have no real idea how many tuples the user will ultimately FETCH * from a cursor, but it seems a good bet that he doesn't want 'em * all. Optimize for 10% retrieval (you gotta better number? Should * this be a SETtable parameter?) */ tuple_fraction = 0.10; } else { /* Default assumption is we need all the tuples */ tuple_fraction = 0.0; } /* primary planning entry point (may recurse for subqueries) */ result_plan = subquery_planner(parse, tuple_fraction, NULL); /* check we popped out the right number of levels */ Assert(PlannerQueryLevel == 0); /* * If creating a plan for a scrollable cursor, make sure it can run * backwards on demand. Add a Material node at the top at need. */ if (isCursor && (cursorOptions & CURSOR_OPT_SCROLL)) { if (!ExecSupportsBackwardScan(result_plan)) result_plan = materialize_finished_plan(result_plan); } /* final cleanup of the plan */ result_plan = set_plan_references(result_plan, parse->rtable); /* executor wants to know total number of Params used overall */ result_plan->nParamExec = list_length(PlannerParamList); /* restore state for outer planner, if any */ PlannerQueryLevel = save_PlannerQueryLevel; PlannerParamList = save_PlannerParamList; PlannerBoundParamList = save_PlannerBoundParamList; return result_plan;}/*-------------------- * subquery_planner * Invokes the planner on a subquery. We recurse to here for each * sub-SELECT found in the query tree. * * parse is the querytree produced by the parser & rewriter. * tuple_fraction is the fraction of tuples we expect will be retrieved. * tuple_fraction is interpreted as explained for grouping_planner, below. * * If subquery_pathkeys isn't NULL, it receives a list of pathkeys indicating * the output sort ordering of the completed plan. * * Basically, this routine does the stuff that should only be done once * per Query object. It then calls grouping_planner. At one time, * grouping_planner could be invoked recursively on the same Query object; * that's not currently true, but we keep the separation between the two * routines anyway, in case we need it again someday. * * subquery_planner will be called recursively to handle sub-Query nodes * found within the query's expressions and rangetable. * * Returns a query plan. *-------------------- */Plan *subquery_planner(Query *parse, double tuple_fraction, List **subquery_pathkeys){ List *saved_initplan = PlannerInitPlan; int saved_planid = PlannerPlanId; PlannerInfo *root; Plan *plan; List *newHaving; List *lst; ListCell *l; /* Set up for a new level of subquery */ PlannerQueryLevel++; PlannerInitPlan = NIL; /* Create a PlannerInfo data structure for this subquery */ root = makeNode(PlannerInfo); root->parse = parse; /* * Look for IN clauses at the top level of WHERE, and transform them into * joins. Note that this step only handles IN clauses originally at top * level of WHERE; if we pull up any subqueries in the next step, their * INs are processed just before pulling them up. */ root->in_info_list = NIL; if (parse->hasSubLinks) parse->jointree->quals = pull_up_IN_clauses(root, parse->jointree->quals); /* * Check to see if any subqueries in the rangetable can be merged into * this query. */ parse->jointree = (FromExpr *) pull_up_subqueries(root, (Node *) parse->jointree, false); /* * Detect whether any rangetable entries are RTE_JOIN kind; if not, we can * avoid the expense of doing flatten_join_alias_vars(). Also check for * outer joins --- if none, we can skip reduce_outer_joins() and some * other processing. This must be done after we have done * pull_up_subqueries, of course. * * Note: if reduce_outer_joins manages to eliminate all outer joins, * root->hasOuterJoins is not reset currently. This is OK since its * purpose is merely to suppress unnecessary processing in simple cases. */ root->hasJoinRTEs = false; root->hasOuterJoins = false; foreach(l, parse->rtable) { RangeTblEntry *rte = (RangeTblEntry *) lfirst(l); if (rte->rtekind == RTE_JOIN) { root->hasJoinRTEs = true; if (IS_OUTER_JOIN(rte->jointype)) { root->hasOuterJoins = true; /* Can quit scanning once we find an outer join */ break; } } } /* * Set hasHavingQual to remember if HAVING clause is present. Needed * because preprocess_expression will reduce a constant-true condition to * an empty qual list ... but "HAVING TRUE" is not a semantic no-op. */ root->hasHavingQual = (parse->havingQual != NULL); /* * Do expression preprocessing on targetlist and quals. */ parse->targetList = (List *) preprocess_expression(root, (Node *) parse->targetList, EXPRKIND_TARGET); preprocess_qual_conditions(root, (Node *) parse->jointree); parse->havingQual = preprocess_expression(root, parse->havingQual, EXPRKIND_QUAL); parse->limitOffset = preprocess_expression(root, parse->limitOffset, EXPRKIND_LIMIT); parse->limitCount = preprocess_expression(root, parse->limitCount, EXPRKIND_LIMIT); root->in_info_list = (List *) preprocess_expression(root, (Node *) root->in_info_list, EXPRKIND_ININFO); /* Also need to preprocess expressions for function RTEs */ foreach(l, parse->rtable) { RangeTblEntry *rte = (RangeTblEntry *) lfirst(l); if (rte->rtekind == RTE_FUNCTION) rte->funcexpr = preprocess_expression(root, rte->funcexpr, EXPRKIND_RTFUNC); } /* * In some cases we may want to transfer a HAVING clause into WHERE. We * cannot do so if the HAVING clause contains aggregates (obviously) or * volatile functions (since a HAVING clause is supposed to be executed * only once per group). Also, it may be that the clause is so expensive * to execute that we're better off doing it only once per group, despite * the loss of selectivity. This is hard to estimate short of doing the * entire planning process twice, so we use a heuristic: clauses * containing subplans are left in HAVING. Otherwise, we move or copy the * HAVING clause into WHERE, in hopes of eliminating tuples before * aggregation instead of after. * * If the query has explicit grouping then we can simply move such a * clause into WHERE; any group that fails the clause will not be in the * output because none of its tuples will reach the grouping or * aggregation stage. Otherwise we must have a degenerate (variable-free) * HAVING clause, which we put in WHERE so that query_planner() can use it * in a gating Result node, but also keep in HAVING to ensure that we * don't emit a bogus aggregated row. (This could be done better, but it * seems not worth optimizing.) * * Note that both havingQual and parse->jointree->quals are in * implicitly-ANDed-list form at this point, even though they are declared * as Node *. */ newHaving = NIL; foreach(l, (List *) parse->havingQual) { Node *havingclause = (Node *) lfirst(l); if (contain_agg_clause(havingclause) || contain_volatile_functions(havingclause) || contain_subplans(havingclause)) { /* keep it in HAVING */ newHaving = lappend(newHaving, havingclause); } else if (parse->groupClause) { /* move it to WHERE */ parse->jointree->quals = (Node *) lappend((List *) parse->jointree->quals, havingclause); } else { /* put a copy in WHERE, keep it in HAVING */ parse->jointree->quals = (Node *) lappend((List *) parse->jointree->quals, copyObject(havingclause)); newHaving = lappend(newHaving, havingclause); } } parse->havingQual = (Node *) newHaving; /* * If we have any outer joins, try to reduce them to plain inner joins. * This step is most easily done after we've done expression * preprocessing. */ if (root->hasOuterJoins) reduce_outer_joins(root); /* * See if we can simplify the jointree; opportunities for this may come * from having pulled up subqueries, or from flattening explicit JOIN * syntax. We must do this after flattening JOIN alias variables, since * eliminating explicit JOIN nodes from the jointree will cause * get_relids_for_join() to fail. But it should happen after * reduce_outer_joins, anyway. */ parse->jointree = (FromExpr *) simplify_jointree(root, (Node *) parse->jointree); /* * Do the main planning. If we have an inherited target relation, that * needs special processing, else go straight to grouping_planner. */ if (parse->resultRelation && (lst = expand_inherited_rtentry(root, parse->resultRelation)) != NIL) plan = inheritance_planner(root, lst); else plan = grouping_planner(root, tuple_fraction); /* * If any subplans were generated, or if we're inside a subplan, build * initPlan list and extParam/allParam sets for plan nodes, and attach the * initPlans to the top plan node. */ if (PlannerPlanId != saved_planid || PlannerQueryLevel > 1) SS_finalize_plan(plan, parse->rtable); /* Return sort ordering info if caller wants it */ if (subquery_pathkeys) *subquery_pathkeys = root->query_pathkeys; /* Return to outer subquery context */ PlannerQueryLevel--; PlannerInitPlan = saved_initplan; /* we do NOT restore PlannerPlanId; that's not an oversight! */ return plan;}/* * preprocess_expression * Do subquery_planner's preprocessing work for an expression, * which can be a targetlist, a WHERE clause (including JOIN/ON * conditions), or a HAVING clause. */static Node *preprocess_expression(PlannerInfo *root, Node *expr, int kind){ /* * Fall out quickly if expression is empty. This occurs often enough to * be worth checking. Note that null->null is the correct conversion for * implicit-AND result format, too. */ if (expr == NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -