PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
planmain.c File Reference
Include dependency graph for planmain.c:

Go to the source code of this file.

Functions

RelOptInfoquery_planner (PlannerInfo *root, List *tlist, query_pathkeys_callback qp_callback, void *qp_extra)
 

Function Documentation

RelOptInfo* query_planner ( PlannerInfo root,
List tlist,
query_pathkeys_callback  qp_callback,
void *  qp_extra 
)

Definition at line 54 of file planmain.c.

References add_base_rels_to_query(), add_path(), add_placeholders_to_base_rels(), Assert, build_base_rel_tlists(), build_empty_join_rel(), PlannerInfo::canon_pathkeys, RelOptInfo::cheapest_total_path, RelOptInfo::consider_parallel, create_lateral_join_info(), create_result_path(), deconstruct_jointree(), elog, ERROR, extract_restriction_or_clauses(), find_lateral_references(), find_placeholders_in_jointree(), fix_placeholder_input_needed_levels(), PlannerInfo::fkey_list, FromExpr::fromlist, PlannerInfo::full_join_clauses, generate_base_implied_equalities(), PlannerInfo::glob, PlannerInfo::initial_rels, is_parallel_safe(), PlannerInfo::join_cur_level, PlannerInfo::join_info_list, PlannerInfo::join_rel_hash, PlannerInfo::join_rel_level, PlannerInfo::join_rel_list, Query::jointree, PlannerInfo::left_join_clauses, make_one_rel(), match_foreign_keys_to_quals(), NIL, NULL, RelOptInfo::pages, PlannerGlobal::parallelModeOK, Path::param_info, parse(), PlannerInfo::parse, PlannerInfo::placeholder_list, FromExpr::quals, reconsider_outer_join_clauses(), RelOptInfo::relid, RELOPT_BASEREL, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, remove_useless_joins(), PlannerInfo::right_join_clauses, set_cheapest(), and setup_simple_rel_arrays().

Referenced by build_minmax_path(), and grouping_planner().

56 {
57  Query *parse = root->parse;
58  List *joinlist;
59  RelOptInfo *final_rel;
60  Index rti;
61  double total_pages;
62 
63  /*
64  * If the query has an empty join tree, then it's something easy like
65  * "SELECT 2+2;" or "INSERT ... VALUES()". Fall through quickly.
66  */
67  if (parse->jointree->fromlist == NIL)
68  {
69  /* We need a dummy joinrel to describe the empty set of baserels */
70  final_rel = build_empty_join_rel(root);
71 
72  /*
73  * If query allows parallelism in general, check whether the quals are
74  * parallel-restricted. (We need not check final_rel->reltarget
75  * because it's empty at this point. Anything parallel-restricted in
76  * the query tlist will be dealt with later.)
77  */
78  if (root->glob->parallelModeOK)
79  final_rel->consider_parallel =
80  is_parallel_safe(root, parse->jointree->quals);
81 
82  /* The only path for it is a trivial Result path */
83  add_path(final_rel, (Path *)
84  create_result_path(root, final_rel,
85  final_rel->reltarget,
86  (List *) parse->jointree->quals));
87 
88  /* Select cheapest path (pretty easy in this case...) */
89  set_cheapest(final_rel);
90 
91  /*
92  * We still are required to call qp_callback, in case it's something
93  * like "SELECT 2+2 ORDER BY 1".
94  */
95  root->canon_pathkeys = NIL;
96  (*qp_callback) (root, qp_extra);
97 
98  return final_rel;
99  }
100 
101  /*
102  * Init planner lists to empty.
103  *
104  * NOTE: append_rel_list was set up by subquery_planner, so do not touch
105  * here.
106  */
107  root->join_rel_list = NIL;
108  root->join_rel_hash = NULL;
109  root->join_rel_level = NULL;
110  root->join_cur_level = 0;
111  root->canon_pathkeys = NIL;
112  root->left_join_clauses = NIL;
113  root->right_join_clauses = NIL;
114  root->full_join_clauses = NIL;
115  root->join_info_list = NIL;
116  root->placeholder_list = NIL;
117  root->fkey_list = NIL;
118  root->initial_rels = NIL;
119 
120  /*
121  * Make a flattened version of the rangetable for faster access (this is
122  * OK because the rangetable won't change any more), and set up an empty
123  * array for indexing base relations.
124  */
126 
127  /*
128  * Construct RelOptInfo nodes for all base relations in query, and
129  * indirectly for all appendrel member relations ("other rels"). This
130  * will give us a RelOptInfo for every "simple" (non-join) rel involved in
131  * the query.
132  *
133  * Note: the reason we find the rels by searching the jointree and
134  * appendrel list, rather than just scanning the rangetable, is that the
135  * rangetable may contain RTEs for rels not actively part of the query,
136  * for example views. We don't want to make RelOptInfos for them.
137  */
138  add_base_rels_to_query(root, (Node *) parse->jointree);
139 
140  /*
141  * Examine the targetlist and join tree, adding entries to baserel
142  * targetlists for all referenced Vars, and generating PlaceHolderInfo
143  * entries for all referenced PlaceHolderVars. Restrict and join clauses
144  * are added to appropriate lists belonging to the mentioned relations. We
145  * also build EquivalenceClasses for provably equivalent expressions. The
146  * SpecialJoinInfo list is also built to hold information about join order
147  * restrictions. Finally, we form a target joinlist for make_one_rel() to
148  * work from.
149  */
150  build_base_rel_tlists(root, tlist);
151 
153 
155 
156  joinlist = deconstruct_jointree(root);
157 
158  /*
159  * Reconsider any postponed outer-join quals now that we have built up
160  * equivalence classes. (This could result in further additions or
161  * mergings of classes.)
162  */
164 
165  /*
166  * If we formed any equivalence classes, generate additional restriction
167  * clauses as appropriate. (Implied join clauses are formed on-the-fly
168  * later.)
169  */
171 
172  /*
173  * We have completed merging equivalence sets, so it's now possible to
174  * generate pathkeys in canonical form; so compute query_pathkeys and
175  * other pathkeys fields in PlannerInfo.
176  */
177  (*qp_callback) (root, qp_extra);
178 
179  /*
180  * Examine any "placeholder" expressions generated during subquery pullup.
181  * Make sure that the Vars they need are marked as needed at the relevant
182  * join level. This must be done before join removal because it might
183  * cause Vars or placeholders to be needed above a join when they weren't
184  * so marked before.
185  */
187 
188  /*
189  * Remove any useless outer joins. Ideally this would be done during
190  * jointree preprocessing, but the necessary information isn't available
191  * until we've built baserel data structures and classified qual clauses.
192  */
193  joinlist = remove_useless_joins(root, joinlist);
194 
195  /*
196  * Now distribute "placeholders" to base rels as needed. This has to be
197  * done after join removal because removal could change whether a
198  * placeholder is evaluable at a base rel.
199  */
201 
202  /*
203  * Construct the lateral reference sets now that we have finalized
204  * PlaceHolderVar eval levels.
205  */
207 
208  /*
209  * Match foreign keys to equivalence classes and join quals. This must be
210  * done after finalizing equivalence classes, and it's useful to wait till
211  * after join removal so that we can skip processing foreign keys
212  * involving removed relations.
213  */
215 
216  /*
217  * Look for join OR clauses that we can extract single-relation
218  * restriction OR clauses from.
219  */
221 
222  /*
223  * We should now have size estimates for every actual table involved in
224  * the query, and we also know which if any have been deleted from the
225  * query by join removal; so we can compute total_table_pages.
226  *
227  * Note that appendrels are not double-counted here, even though we don't
228  * bother to distinguish RelOptInfos for appendrel parents, because the
229  * parents will still have size zero.
230  *
231  * XXX if a table is self-joined, we will count it once per appearance,
232  * which perhaps is the wrong thing ... but that's not completely clear,
233  * and detecting self-joins here is difficult, so ignore it for now.
234  */
235  total_pages = 0;
236  for (rti = 1; rti < root->simple_rel_array_size; rti++)
237  {
238  RelOptInfo *brel = root->simple_rel_array[rti];
239 
240  if (brel == NULL)
241  continue;
242 
243  Assert(brel->relid == rti); /* sanity check on array */
244 
245  if (brel->reloptkind == RELOPT_BASEREL ||
247  total_pages += (double) brel->pages;
248  }
249  root->total_table_pages = total_pages;
250 
251  /*
252  * Ready to do the primary planning.
253  */
254  final_rel = make_one_rel(root, joinlist);
255 
256  /* Check that we got at least one usable path */
257  if (!final_rel || !final_rel->cheapest_total_path ||
258  final_rel->cheapest_total_path->param_info != NULL)
259  elog(ERROR, "failed to construct the join relation");
260 
261  return final_rel;
262 }
#define NIL
Definition: pg_list.h:69
int join_cur_level
Definition: relation.h:223
RelOptInfo * make_one_rel(PlannerInfo *root, List *joinlist)
Definition: allpaths.c:138
Query * parse
Definition: relation.h:152
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
RelOptKind reloptkind
Definition: relation.h:487
void add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
Definition: initsplan.c:104
List * join_info_list
Definition: relation.h:247
FromExpr * jointree
Definition: parsenodes.h:129
void extract_restriction_or_clauses(PlannerInfo *root)
Definition: orclauses.c:73
List * deconstruct_jointree(PlannerInfo *root)
Definition: initsplan.c:690
ParamPathInfo * param_info
Definition: relation.h:897
Definition: nodes.h:508
List * join_rel_list
Definition: relation.h:212
List * fromlist
Definition: primnodes.h:1433
List * fkey_list
Definition: relation.h:255
void add_placeholders_to_base_rels(PlannerInfo *root)
Definition: placeholder.c:379
Node * quals
Definition: primnodes.h:1434
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:1071
#define ERROR
Definition: elog.h:43
bool parallelModeOK
Definition: relation.h:126
struct Path * cheapest_total_path
Definition: relation.h:508
PlannerGlobal * glob
Definition: relation.h:154
List * left_join_clauses
Definition: relation.h:236
List * full_join_clauses
Definition: relation.h:244
List * canon_pathkeys
Definition: relation.h:234
Index relid
Definition: relation.h:518
void fix_placeholder_input_needed_levels(PlannerInfo *root)
Definition: placeholder.c:350
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:234
void find_placeholders_in_jointree(PlannerInfo *root)
Definition: placeholder.c:144
void generate_base_implied_equalities(PlannerInfo *root)
Definition: equivclass.c:762
unsigned int Index
Definition: c.h:362
void reconsider_outer_join_clauses(PlannerInfo *root)
Definition: equivclass.c:1532
BlockNumber pages
Definition: relation.h:528
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
RelOptInfo * build_empty_join_rel(PlannerInfo *root)
Definition: relnode.c:836
List ** join_rel_level
Definition: relation.h:222
void setup_simple_rel_arrays(PlannerInfo *root)
Definition: relnode.c:59
List * remove_useless_joins(PlannerInfo *root, List *joinlist)
Definition: analyzejoins.c:55
struct HTAB * join_rel_hash
Definition: relation.h:213
void build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
Definition: initsplan.c:150
bool consider_parallel
Definition: relation.h:498
void match_foreign_keys_to_quals(PlannerInfo *root)
Definition: initsplan.c:2410
List * placeholder_list
Definition: relation.h:253
ResultPath * create_result_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, List *resconstantqual)
Definition: pathnode.c:1346
List * initial_rels
Definition: relation.h:264
List * right_join_clauses
Definition: relation.h:240
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
struct PathTarget * reltarget
Definition: relation.h:501
void create_lateral_join_info(PlannerInfo *root)
Definition: initsplan.c:415
Definition: relation.h:888
void find_lateral_references(PlannerInfo *root)
Definition: initsplan.c:271
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:651