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 54 of file planmain.c.

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

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(), 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(), elog, ERROR, extract_restriction_or_clauses(), find_lateral_references(), find_placeholders_in_jointree(), fix_placeholder_input_needed_levels(), generate_base_implied_equalities(), is_parallel_safe(), IsA, linitial, list_length(), make_one_rel(), match_foreign_keys_to_quals(), NIL, parse(), reconsider_outer_join_clauses(), reduce_unique_semijoins(), RelOptInfo::reltarget, remove_useless_groupby_columns(), remove_useless_joins(), remove_useless_self_joins(), root, RTE_RESULT, RangeTblEntry::rtekind, set_cheapest(), setup_eager_aggregation(), and setup_simple_rel_arrays().

Referenced by build_minmax_path(), and grouping_planner().