PostgreSQL Source Code  git master
planmain.c File Reference
Include dependency graph for planmain.c:

Go to the source code of this file.

Functions

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

Function Documentation

◆ query_planner()

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

Definition at line 55 of file planmain.c.

57 {
58  Query *parse = root->parse;
59  List *joinlist;
60  RelOptInfo *final_rel;
61 
62  /*
63  * Init planner lists to empty.
64  *
65  * NOTE: append_rel_list was set up by subquery_planner, so do not touch
66  * here.
67  */
68  root->join_rel_list = NIL;
69  root->join_rel_hash = NULL;
70  root->join_rel_level = NULL;
71  root->join_cur_level = 0;
72  root->canon_pathkeys = NIL;
73  root->left_join_clauses = NIL;
74  root->right_join_clauses = NIL;
75  root->full_join_clauses = NIL;
76  root->join_info_list = NIL;
77  root->placeholder_list = NIL;
78  root->placeholder_array = NULL;
79  root->placeholder_array_size = 0;
80  root->fkey_list = NIL;
81  root->initial_rels = NIL;
82 
83  /*
84  * Set up arrays for accessing base relations and AppendRelInfos.
85  */
87 
88  /*
89  * In the trivial case where the jointree is a single RTE_RESULT relation,
90  * bypass all the rest of this function and just make a RelOptInfo and its
91  * one access path. This is worth optimizing because it applies for
92  * common cases like "SELECT expression" and "INSERT ... VALUES()".
93  */
94  Assert(parse->jointree->fromlist != NIL);
95  if (list_length(parse->jointree->fromlist) == 1)
96  {
97  Node *jtnode = (Node *) linitial(parse->jointree->fromlist);
98 
99  if (IsA(jtnode, RangeTblRef))
100  {
101  int varno = ((RangeTblRef *) jtnode)->rtindex;
102  RangeTblEntry *rte = root->simple_rte_array[varno];
103 
104  Assert(rte != NULL);
105  if (rte->rtekind == RTE_RESULT)
106  {
107  /* Make the RelOptInfo for it directly */
108  final_rel = build_simple_rel(root, varno, NULL);
109 
110  /*
111  * If query allows parallelism in general, check whether the
112  * quals are parallel-restricted. (We need not check
113  * final_rel->reltarget because it's empty at this point.
114  * Anything parallel-restricted in the query tlist will be
115  * dealt with later.) We should always do this in a subquery,
116  * since it might be useful to use the subquery in parallel
117  * paths in the parent level. At top level this is normally
118  * not worth the cycles, because a Result-only plan would
119  * never be interesting to parallelize. However, if
120  * debug_parallel_query is on, then we want to execute the
121  * Result in a parallel worker if possible, so we must check.
122  */
123  if (root->glob->parallelModeOK &&
124  (root->query_level > 1 ||
126  final_rel->consider_parallel =
127  is_parallel_safe(root, parse->jointree->quals);
128 
129  /*
130  * The only path for it is a trivial Result path. We cheat a
131  * bit here by using a GroupResultPath, because that way we
132  * can just jam the quals into it without preprocessing them.
133  * (But, if you hold your head at the right angle, a FROM-less
134  * SELECT is a kind of degenerate-grouping case, so it's not
135  * that much of a cheat.)
136  */
137  add_path(final_rel, (Path *)
138  create_group_result_path(root, final_rel,
139  final_rel->reltarget,
140  (List *) parse->jointree->quals));
141 
142  /* Select cheapest path (pretty easy in this case...) */
143  set_cheapest(final_rel);
144 
145  /*
146  * We don't need to run generate_base_implied_equalities, but
147  * we do need to pretend that EC merging is complete.
148  */
149  root->ec_merging_done = true;
150 
151  /*
152  * We still are required to call qp_callback, in case it's
153  * something like "SELECT 2+2 ORDER BY 1".
154  */
155  (*qp_callback) (root, qp_extra);
156 
157  return final_rel;
158  }
159  }
160  }
161 
162  /*
163  * Construct RelOptInfo nodes for all base relations used in the query.
164  * Appendrel member relations ("other rels") will be added later.
165  *
166  * Note: the reason we find the baserels by searching the jointree, rather
167  * than scanning the rangetable, is that the rangetable may contain RTEs
168  * for rels not actively part of the query, for example views. We don't
169  * want to make RelOptInfos for them.
170  */
171  add_base_rels_to_query(root, (Node *) parse->jointree);
172 
173  /*
174  * Examine the targetlist and join tree, adding entries to baserel
175  * targetlists for all referenced Vars, and generating PlaceHolderInfo
176  * entries for all referenced PlaceHolderVars. Restrict and join clauses
177  * are added to appropriate lists belonging to the mentioned relations. We
178  * also build EquivalenceClasses for provably equivalent expressions. The
179  * SpecialJoinInfo list is also built to hold information about join order
180  * restrictions. Finally, we form a target joinlist for make_one_rel() to
181  * work from.
182  */
184 
186 
188 
189  joinlist = deconstruct_jointree(root);
190 
191  /*
192  * Reconsider any postponed outer-join quals now that we have built up
193  * equivalence classes. (This could result in further additions or
194  * mergings of classes.)
195  */
197 
198  /*
199  * If we formed any equivalence classes, generate additional restriction
200  * clauses as appropriate. (Implied join clauses are formed on-the-fly
201  * later.)
202  */
204 
205  /*
206  * We have completed merging equivalence sets, so it's now possible to
207  * generate pathkeys in canonical form; so compute query_pathkeys and
208  * other pathkeys fields in PlannerInfo.
209  */
210  (*qp_callback) (root, qp_extra);
211 
212  /*
213  * Examine any "placeholder" expressions generated during subquery pullup.
214  * Make sure that the Vars they need are marked as needed at the relevant
215  * join level. This must be done before join removal because it might
216  * cause Vars or placeholders to be needed above a join when they weren't
217  * so marked before.
218  */
220 
221  /*
222  * Remove any useless outer joins. Ideally this would be done during
223  * jointree preprocessing, but the necessary information isn't available
224  * until we've built baserel data structures and classified qual clauses.
225  */
226  joinlist = remove_useless_joins(root, joinlist);
227 
228  /*
229  * Also, reduce any semijoins with unique inner rels to plain inner joins.
230  * Likewise, this can't be done until now for lack of needed info.
231  */
233 
234  /*
235  * Remove self joins on a unique column.
236  */
237  joinlist = remove_useless_self_joins(root, joinlist);
238 
239  /*
240  * Now distribute "placeholders" to base rels as needed. This has to be
241  * done after join removal because removal could change whether a
242  * placeholder is evaluable at a base rel.
243  */
245 
246  /*
247  * Construct the lateral reference sets now that we have finalized
248  * PlaceHolderVar eval levels.
249  */
251 
252  /*
253  * Match foreign keys to equivalence classes and join quals. This must be
254  * done after finalizing equivalence classes, and it's useful to wait till
255  * after join removal so that we can skip processing foreign keys
256  * involving removed relations.
257  */
259 
260  /*
261  * Look for join OR clauses that we can extract single-relation
262  * restriction OR clauses from.
263  */
265 
266  /*
267  * Now expand appendrels by adding "otherrels" for their children. We
268  * delay this to the end so that we have as much information as possible
269  * available for each baserel, including all restriction clauses. That
270  * let us prune away partitions that don't satisfy a restriction clause.
271  * Also note that some information such as lateral_relids is propagated
272  * from baserels to otherrels here, so we must have computed it already.
273  */
275 
276  /*
277  * Distribute any UPDATE/DELETE/MERGE row identity variables to the target
278  * relations. This can't be done till we've finished expansion of
279  * appendrels.
280  */
282 
283  /*
284  * Ready to do the primary planning.
285  */
286  final_rel = make_one_rel(root, joinlist);
287 
288  /* Check that we got at least one usable path */
289  if (!final_rel || !final_rel->cheapest_total_path ||
290  final_rel->cheapest_total_path->param_info != NULL)
291  elog(ERROR, "failed to construct the join relation");
292 
293  return final_rel;
294 }
RelOptInfo * make_one_rel(PlannerInfo *root, List *joinlist)
Definition: allpaths.c:174
List * remove_useless_joins(PlannerInfo *root, List *joinlist)
Definition: analyzejoins.c:94
List * remove_useless_self_joins(PlannerInfo *root, List *joinlist)
void reduce_unique_semijoins(PlannerInfo *root)
Definition: analyzejoins.c:774
void distribute_row_identity_vars(PlannerInfo *root)
Definition: appendinfo.c:966
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:736
#define ERROR
Definition: elog.h:39
void generate_base_implied_equalities(PlannerInfo *root)
Definition: equivclass.c:1033
void reconsider_outer_join_clauses(PlannerInfo *root)
Definition: equivclass.c:1982
List * deconstruct_jointree(PlannerInfo *root)
Definition: initsplan.c:741
void match_foreign_keys_to_quals(PlannerInfo *root)
Definition: initsplan.c:2972
void find_lateral_references(PlannerInfo *root)
Definition: initsplan.c:359
void add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
Definition: initsplan.c:158
void build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
Definition: initsplan.c:235
void add_other_rels_to_query(PlannerInfo *root)
Definition: initsplan.c:196
void create_lateral_join_info(PlannerInfo *root)
Definition: initsplan.c:502
Assert(fmt[strlen(fmt) - 1] !='\n')
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
@ DEBUG_PARALLEL_OFF
Definition: optimizer.h:105
void extract_restriction_or_clauses(PlannerInfo *root)
Definition: orclauses.c:76
@ RTE_RESULT
Definition: parsenodes.h:1014
GroupResultPath * create_group_result_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, List *havingqual)
Definition: pathnode.c:1516
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define linitial(l)
Definition: pg_list.h:178
void add_placeholders_to_base_rels(PlannerInfo *root)
Definition: placeholder.c:329
void fix_placeholder_input_needed_levels(PlannerInfo *root)
Definition: placeholder.c:300
void find_placeholders_in_jointree(PlannerInfo *root)
Definition: placeholder.c:185
int debug_parallel_query
Definition: planner.c:73
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:715
void setup_simple_rel_arrays(PlannerInfo *root)
Definition: relnode.c:93
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition: relnode.c:191
Definition: pg_list.h:54
Definition: nodes.h:129
bool parallelModeOK
Definition: pathnodes.h:153
List * canon_pathkeys
Definition: pathnodes.h:317
List * processed_tlist
Definition: pathnodes.h:453
List * join_rel_list
Definition: pathnodes.h:277
bool ec_merging_done
Definition: pathnodes.h:314
List * left_join_clauses
Definition: pathnodes.h:323
List * full_join_clauses
Definition: pathnodes.h:334
Index query_level
Definition: pathnodes.h:205
List * placeholder_list
Definition: pathnodes.h:371
PlannerGlobal * glob
Definition: pathnodes.h:202
List * right_join_clauses
Definition: pathnodes.h:329
List * fkey_list
Definition: pathnodes.h:379
Query * parse
Definition: pathnodes.h:199
List * join_info_list
Definition: pathnodes.h:337
int join_cur_level
Definition: pathnodes.h:293
RTEKind rtekind
Definition: parsenodes.h:1025
struct PathTarget * reltarget
Definition: pathnodes.h:878
bool consider_parallel
Definition: pathnodes.h:872
struct Path * cheapest_total_path
Definition: pathnodes.h:887

References add_base_rels_to_query(), add_other_rels_to_query(), add_path(), add_placeholders_to_base_rels(), Assert(), build_base_rel_tlists(), build_simple_rel(), PlannerInfo::canon_pathkeys, RelOptInfo::cheapest_total_path, RelOptInfo::consider_parallel, create_group_result_path(), create_lateral_join_info(), DEBUG_PARALLEL_OFF, debug_parallel_query, deconstruct_jointree(), distribute_row_identity_vars(), PlannerInfo::ec_merging_done, elog(), ERROR, extract_restriction_or_clauses(), find_lateral_references(), find_placeholders_in_jointree(), fix_placeholder_input_needed_levels(), PlannerInfo::fkey_list, PlannerInfo::full_join_clauses, generate_base_implied_equalities(), PlannerInfo::glob, is_parallel_safe(), IsA, PlannerInfo::join_cur_level, PlannerInfo::join_info_list, PlannerInfo::join_rel_list, PlannerInfo::left_join_clauses, linitial, list_length(), make_one_rel(), match_foreign_keys_to_quals(), NIL, PlannerGlobal::parallelModeOK, parse(), PlannerInfo::parse, PlannerInfo::placeholder_list, PlannerInfo::processed_tlist, PlannerInfo::query_level, reconsider_outer_join_clauses(), reduce_unique_semijoins(), RelOptInfo::reltarget, remove_useless_joins(), remove_useless_self_joins(), PlannerInfo::right_join_clauses, RTE_RESULT, RangeTblEntry::rtekind, set_cheapest(), and setup_simple_rel_arrays().

Referenced by build_minmax_path(), and grouping_planner().