1 /*-------------------------------------------------------------------------
4 * Relation-node lookup/construction routines
6 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/optimizer/util/relnode.c,v 1.66 2005/05/23 03:01:14 tgl Exp $
13 *-------------------------------------------------------------------------
17 #include "optimizer/cost.h"
18 #include "optimizer/joininfo.h"
19 #include "optimizer/pathnode.h"
20 #include "optimizer/plancat.h"
21 #include "optimizer/restrictinfo.h"
22 #include "optimizer/tlist.h"
23 #include "parser/parsetree.h"
26 static RelOptInfo *make_reloptinfo(Query *root, int relid,
27 RelOptKind reloptkind);
28 static void build_joinrel_tlist(Query *root, RelOptInfo *joinrel);
29 static List *build_joinrel_restrictlist(Query *root,
31 RelOptInfo *outer_rel,
32 RelOptInfo *inner_rel,
34 static void build_joinrel_joinlist(RelOptInfo *joinrel,
35 RelOptInfo *outer_rel,
36 RelOptInfo *inner_rel);
37 static List *subbuild_joinrel_restrictlist(RelOptInfo *joinrel,
39 static void subbuild_joinrel_joinlist(RelOptInfo *joinrel,
45 * Construct a new base relation RelOptInfo, and put it in the query's
49 build_base_rel(Query *root, int relid)
54 /* Rel should not exist already */
55 foreach(l, root->base_rel_list)
57 rel = (RelOptInfo *) lfirst(l);
58 if (relid == rel->relid)
59 elog(ERROR, "rel already exists");
62 /* It should not exist as an "other" rel, either */
63 foreach(l, root->other_rel_list)
65 rel = (RelOptInfo *) lfirst(l);
66 if (relid == rel->relid)
67 elog(ERROR, "rel already exists as \"other\" rel");
70 /* No existing RelOptInfo for this base rel, so make a new one */
71 rel = make_reloptinfo(root, relid, RELOPT_BASEREL);
73 /* and add it to the list */
74 root->base_rel_list = lcons(rel, root->base_rel_list);
79 * Returns relation entry corresponding to 'relid', creating a new one
80 * if necessary. This is for 'other' relations, which are much like
81 * base relations except that they live in a different list.
84 build_other_rel(Query *root, int relid)
90 foreach(l, root->other_rel_list)
92 rel = (RelOptInfo *) lfirst(l);
93 if (relid == rel->relid)
97 /* It should not exist as a base rel */
98 foreach(l, root->base_rel_list)
100 rel = (RelOptInfo *) lfirst(l);
101 if (relid == rel->relid)
102 elog(ERROR, "rel already exists as base rel");
105 /* No existing RelOptInfo for this other rel, so make a new one */
106 /* presently, must be an inheritance child rel */
107 rel = make_reloptinfo(root, relid, RELOPT_OTHER_CHILD_REL);
109 /* and add it to the list */
110 root->other_rel_list = lcons(rel, root->other_rel_list);
117 * Construct a RelOptInfo for the specified rangetable index.
119 * Common code for build_base_rel and build_other_rel.
122 make_reloptinfo(Query *root, int relid, RelOptKind reloptkind)
124 RelOptInfo *rel = makeNode(RelOptInfo);
125 RangeTblEntry *rte = rt_fetch(relid, root->rtable);
127 rel->reloptkind = reloptkind;
128 rel->relids = bms_make_singleton(relid);
131 rel->reltargetlist = NIL;
133 rel->cheapest_startup_path = NULL;
134 rel->cheapest_total_path = NULL;
135 rel->cheapest_unique_path = NULL;
137 rel->rtekind = rte->rtekind;
138 /* min_attr, max_attr, attr_needed, attr_widths are set below */
139 rel->indexlist = NIL;
143 rel->baserestrictinfo = NIL;
144 rel->baserestrictcost.startup = 0;
145 rel->baserestrictcost.per_tuple = 0;
146 rel->outerjoinset = NULL;
148 rel->index_outer_relids = NULL;
149 rel->index_inner_paths = NIL;
151 /* Check type of rtable entry */
152 switch (rte->rtekind)
155 /* Table --- retrieve statistics from the system catalogs */
156 get_relation_info(rte->relid, rel);
160 /* Subquery or function --- set up attr range and arrays */
161 /* Note: 0 is included in range to support whole-row Vars */
163 rel->max_attr = list_length(rte->eref->colnames);
164 rel->attr_needed = (Relids *)
165 palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
166 rel->attr_widths = (int32 *)
167 palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
170 elog(ERROR, "unrecognized RTE kind: %d",
180 * Find a base or other relation entry, which must already exist
181 * (since we'd have no idea which list to add it to).
184 find_base_rel(Query *root, int relid)
189 foreach(l, root->base_rel_list)
191 rel = (RelOptInfo *) lfirst(l);
192 if (relid == rel->relid)
196 foreach(l, root->other_rel_list)
198 rel = (RelOptInfo *) lfirst(l);
199 if (relid == rel->relid)
203 elog(ERROR, "no relation entry for relid %d", relid);
205 return NULL; /* keep compiler quiet */
210 * Returns relation entry corresponding to 'relids' (a set of RT indexes),
211 * or NULL if none exists. This is for join relations.
214 find_join_rel(Query *root, Relids relids)
218 foreach(l, root->join_rel_list)
220 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
222 if (bms_equal(rel->relids, relids))
231 * Returns relation entry corresponding to the union of two given rels,
232 * creating a new relation entry if none already exists.
234 * 'joinrelids' is the Relids set that uniquely identifies the join
235 * 'outer_rel' and 'inner_rel' are relation nodes for the relations to be
237 * 'jointype': type of join (inner/outer)
238 * 'restrictlist_ptr': result variable. If not NULL, *restrictlist_ptr
239 * receives the list of RestrictInfo nodes that apply to this
240 * particular pair of joinable relations.
242 * restrictlist_ptr makes the routine's API a little grotty, but it saves
243 * duplicated calculation of the restrictlist...
246 build_join_rel(Query *root,
248 RelOptInfo *outer_rel,
249 RelOptInfo *inner_rel,
251 List **restrictlist_ptr)
257 * See if we already have a joinrel for this set of base rels.
259 joinrel = find_join_rel(root, joinrelids);
264 * Yes, so we only need to figure the restrictlist for this
265 * particular pair of component relations.
267 if (restrictlist_ptr)
268 *restrictlist_ptr = build_joinrel_restrictlist(root,
279 joinrel = makeNode(RelOptInfo);
280 joinrel->reloptkind = RELOPT_JOINREL;
281 joinrel->relids = bms_copy(joinrelids);
284 joinrel->reltargetlist = NIL;
285 joinrel->pathlist = NIL;
286 joinrel->cheapest_startup_path = NULL;
287 joinrel->cheapest_total_path = NULL;
288 joinrel->cheapest_unique_path = NULL;
289 joinrel->relid = 0; /* indicates not a baserel */
290 joinrel->rtekind = RTE_JOIN;
291 joinrel->min_attr = 0;
292 joinrel->max_attr = 0;
293 joinrel->attr_needed = NULL;
294 joinrel->attr_widths = NULL;
295 joinrel->indexlist = NIL;
298 joinrel->subplan = NULL;
299 joinrel->baserestrictinfo = NIL;
300 joinrel->baserestrictcost.startup = 0;
301 joinrel->baserestrictcost.per_tuple = 0;
302 joinrel->outerjoinset = NULL;
303 joinrel->joininfo = NIL;
304 joinrel->index_outer_relids = NULL;
305 joinrel->index_inner_paths = NIL;
308 * Create a new tlist containing just the vars that need to be output
309 * from this join (ie, are needed for higher joinclauses or final
312 build_joinrel_tlist(root, joinrel);
315 * Construct restrict and join clause lists for the new joinrel. (The
316 * caller might or might not need the restrictlist, but I need it
317 * anyway for set_joinrel_size_estimates().)
319 restrictlist = build_joinrel_restrictlist(root,
324 if (restrictlist_ptr)
325 *restrictlist_ptr = restrictlist;
326 build_joinrel_joinlist(joinrel, outer_rel, inner_rel);
329 * Set estimates of the joinrel's size.
331 set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
332 jointype, restrictlist);
335 * Add the joinrel to the query's joinrel list.
337 root->join_rel_list = lcons(joinrel, root->join_rel_list);
343 * build_joinrel_tlist
344 * Builds a join relation's target list.
346 * The join's targetlist includes all Vars of its member relations that
347 * will still be needed above the join.
349 * In a former lifetime, this just merged the tlists of the two member
350 * relations first presented. While we could still do that, working from
351 * lists of Vars would mean doing a find_base_rel lookup for each Var.
352 * It seems more efficient to scan the list of base rels and collect the
353 * needed vars directly from there.
355 * We also compute the expected width of the join's output, making use
356 * of data that was cached at the baserel level by set_rel_width().
359 build_joinrel_tlist(Query *root, RelOptInfo *joinrel)
361 Relids relids = joinrel->relids;
364 joinrel->reltargetlist = NIL;
367 foreach(rels, root->base_rel_list)
369 RelOptInfo *baserel = (RelOptInfo *) lfirst(rels);
372 if (!bms_is_member(baserel->relid, relids))
375 foreach(vars, baserel->reltargetlist)
377 Var *var = (Var *) lfirst(vars);
378 int ndx = var->varattno - baserel->min_attr;
380 /* We can't run into any child RowExprs here */
381 Assert(IsA(var, Var));
383 if (bms_nonempty_difference(baserel->attr_needed[ndx], relids))
385 joinrel->reltargetlist = lappend(joinrel->reltargetlist, var);
386 Assert(baserel->attr_widths[ndx] > 0);
387 joinrel->width += baserel->attr_widths[ndx];
394 * build_joinrel_restrictlist
395 * build_joinrel_joinlist
396 * These routines build lists of restriction and join clauses for a
397 * join relation from the joininfo lists of the relations it joins.
399 * These routines are separate because the restriction list must be
400 * built afresh for each pair of input sub-relations we consider, whereas
401 * the join lists need only be computed once for any join RelOptInfo.
402 * The join lists are fully determined by the set of rels making up the
403 * joinrel, so we should get the same results (up to ordering) from any
404 * candidate pair of sub-relations. But the restriction list is whatever
405 * is not handled in the sub-relations, so it depends on which
406 * sub-relations are considered.
408 * If a join clause from an input relation refers to base rels still not
409 * present in the joinrel, then it is still a join clause for the joinrel;
410 * we put it into an appropriate JoinInfo list for the joinrel. Otherwise,
411 * the clause is now a restrict clause for the joined relation, and we
412 * return it to the caller of build_joinrel_restrictlist() to be stored in
413 * join paths made from this pair of sub-relations. (It will not need to
414 * be considered further up the join tree.)
416 * When building a restriction list, we eliminate redundant clauses.
417 * We don't try to do that for join clause lists, since the join clauses
418 * aren't really doing anything, just waiting to become part of higher
419 * levels' restriction lists.
421 * 'joinrel' is a join relation node
422 * 'outer_rel' and 'inner_rel' are a pair of relations that can be joined
424 * 'jointype' is the type of join used.
426 * build_joinrel_restrictlist() returns a list of relevant restrictinfos,
427 * whereas build_joinrel_joinlist() stores its results in the joinrel's
428 * joininfo lists. One or the other must accept each given clause!
430 * NB: Formerly, we made deep(!) copies of each input RestrictInfo to pass
431 * up to the join relation. I believe this is no longer necessary, because
432 * RestrictInfo nodes are no longer context-dependent. Instead, just include
433 * the original nodes in the lists made for the join relation.
436 build_joinrel_restrictlist(Query *root,
438 RelOptInfo *outer_rel,
439 RelOptInfo *inner_rel,
446 * Collect all the clauses that syntactically belong at this level.
448 rlist = list_concat(subbuild_joinrel_restrictlist(joinrel,
449 outer_rel->joininfo),
450 subbuild_joinrel_restrictlist(joinrel,
451 inner_rel->joininfo));
454 * Eliminate duplicate and redundant clauses.
456 * We must eliminate duplicates, since we will see many of the same
457 * clauses arriving from both input relations. Also, if a clause is a
458 * mergejoinable clause, it's possible that it is redundant with
459 * previous clauses (see optimizer/README for discussion). We detect
460 * that case and omit the redundant clause from the result list.
462 result = remove_redundant_join_clauses(root, rlist,
463 IS_OUTER_JOIN(jointype));
471 build_joinrel_joinlist(RelOptInfo *joinrel,
472 RelOptInfo *outer_rel,
473 RelOptInfo *inner_rel)
475 subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo);
476 subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo);
480 subbuild_joinrel_restrictlist(RelOptInfo *joinrel,
483 List *restrictlist = NIL;
486 foreach(xjoininfo, joininfo_list)
488 JoinInfo *joininfo = (JoinInfo *) lfirst(xjoininfo);
490 if (bms_is_subset(joininfo->unjoined_relids, joinrel->relids))
493 * Clauses in this JoinInfo list become restriction clauses
494 * for the joinrel, since they refer to no outside rels.
496 * We must copy the list to avoid disturbing the input relation,
497 * but we can use a shallow copy.
499 restrictlist = list_concat(restrictlist,
500 list_copy(joininfo->jinfo_restrictinfo));
505 * These clauses are still join clauses at this level, so we
506 * ignore them in this routine.
515 subbuild_joinrel_joinlist(RelOptInfo *joinrel,
520 foreach(xjoininfo, joininfo_list)
522 JoinInfo *joininfo = (JoinInfo *) lfirst(xjoininfo);
523 Relids new_unjoined_relids;
525 new_unjoined_relids = bms_difference(joininfo->unjoined_relids,
527 if (bms_is_empty(new_unjoined_relids))
530 * Clauses in this JoinInfo list become restriction clauses
531 * for the joinrel, since they refer to no outside rels. So we
532 * can ignore them in this routine.
534 bms_free(new_unjoined_relids);
539 * These clauses are still join clauses at this level, so find
540 * or make the appropriate JoinInfo item for the joinrel, and
541 * add the clauses to it, eliminating duplicates. (Since
542 * RestrictInfo nodes are normally multiply-linked rather than
543 * copied, pointer equality should be a sufficient test. If
544 * two equal() nodes should happen to sneak in, no great harm
545 * is done --- they'll be detected by redundant-clause testing
546 * when they reach a restriction list.)
548 JoinInfo *new_joininfo;
550 new_joininfo = make_joininfo_node(joinrel, new_unjoined_relids);
551 new_joininfo->jinfo_restrictinfo =
552 list_union_ptr(new_joininfo->jinfo_restrictinfo,
553 joininfo->jinfo_restrictinfo);