]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/util/relnode.c
ba1e7e69320d3e839fc89d957b0f3bb9ba2f6982
[postgresql] / src / backend / optimizer / util / relnode.c
1 /*-------------------------------------------------------------------------
2  *
3  * relnode.c
4  *        Relation-node lookup/construction routines
5  *
6  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/optimizer/util/relnode.c,v 1.58 2004/05/30 23:40:31 neilc Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
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"
24
25
26 static RelOptInfo *make_base_rel(Query *root, int relid);
27 static void build_joinrel_tlist(Query *root, RelOptInfo *joinrel);
28 static List *build_joinrel_restrictlist(Query *root,
29                                                    RelOptInfo *joinrel,
30                                                    RelOptInfo *outer_rel,
31                                                    RelOptInfo *inner_rel,
32                                                    JoinType jointype);
33 static void build_joinrel_joinlist(RelOptInfo *joinrel,
34                                            RelOptInfo *outer_rel,
35                                            RelOptInfo *inner_rel);
36 static List *subbuild_joinrel_restrictlist(RelOptInfo *joinrel,
37                                                           List *joininfo_list);
38 static void subbuild_joinrel_joinlist(RelOptInfo *joinrel,
39                                                   List *joininfo_list);
40
41
42 /*
43  * build_base_rel
44  *        Construct a new base relation RelOptInfo, and put it in the query's
45  *        base_rel_list.
46  */
47 void
48 build_base_rel(Query *root, int relid)
49 {
50         ListCell   *l;
51         RelOptInfo *rel;
52
53         /* Rel should not exist already */
54         foreach(l, root->base_rel_list)
55         {
56                 rel = (RelOptInfo *) lfirst(l);
57                 if (relid == rel->relid)
58                         elog(ERROR, "rel already exists");
59         }
60
61         /* It should not exist as an "other" rel, either */
62         foreach(l, root->other_rel_list)
63         {
64                 rel = (RelOptInfo *) lfirst(l);
65                 if (relid == rel->relid)
66                         elog(ERROR, "rel already exists as \"other\" rel");
67         }
68
69         /* No existing RelOptInfo for this base rel, so make a new one */
70         rel = make_base_rel(root, relid);
71
72         /* and add it to the list */
73         root->base_rel_list = lcons(rel, root->base_rel_list);
74 }
75
76 /*
77  * build_other_rel
78  *        Returns relation entry corresponding to 'relid', creating a new one
79  *        if necessary.  This is for 'other' relations, which are much like
80  *        base relations except that they live in a different list.
81  */
82 RelOptInfo *
83 build_other_rel(Query *root, int relid)
84 {
85         ListCell   *l;
86         RelOptInfo *rel;
87
88         /* Already made? */
89         foreach(l, root->other_rel_list)
90         {
91                 rel = (RelOptInfo *) lfirst(l);
92                 if (relid == rel->relid)
93                         return rel;
94         }
95
96         /* It should not exist as a base rel */
97         foreach(l, root->base_rel_list)
98         {
99                 rel = (RelOptInfo *) lfirst(l);
100                 if (relid == rel->relid)
101                         elog(ERROR, "rel already exists as base rel");
102         }
103
104         /* No existing RelOptInfo for this other rel, so make a new one */
105         rel = make_base_rel(root, relid);
106
107         /* presently, must be an inheritance child rel */
108         Assert(rel->reloptkind == RELOPT_BASEREL);
109         rel->reloptkind = RELOPT_OTHER_CHILD_REL;
110
111         /* and add it to the list */
112         root->other_rel_list = lcons(rel, root->other_rel_list);
113
114         return rel;
115 }
116
117 /*
118  * make_base_rel
119  *        Construct a base-relation RelOptInfo for the specified rangetable index.
120  *
121  * Common code for build_base_rel and build_other_rel.
122  */
123 static RelOptInfo *
124 make_base_rel(Query *root, int relid)
125 {
126         RelOptInfo *rel = makeNode(RelOptInfo);
127         RangeTblEntry *rte = rt_fetch(relid, root->rtable);
128
129         rel->reloptkind = RELOPT_BASEREL;
130         rel->relids = bms_make_singleton(relid);
131         rel->rows = 0;
132         rel->width = 0;
133         FastListInit(&rel->reltargetlist);
134         rel->pathlist = NIL;
135         rel->cheapest_startup_path = NULL;
136         rel->cheapest_total_path = NULL;
137         rel->cheapest_unique_path = NULL;
138         rel->relid = relid;
139         rel->rtekind = rte->rtekind;
140         /* min_attr, max_attr, attr_needed, attr_widths are set below */
141         rel->indexlist = NIL;
142         rel->pages = 0;
143         rel->tuples = 0;
144         rel->subplan = NULL;
145         rel->baserestrictinfo = NIL;
146         rel->baserestrictcost.startup = 0;
147         rel->baserestrictcost.per_tuple = 0;
148         rel->outerjoinset = NULL;
149         rel->joininfo = NIL;
150         rel->index_outer_relids = NULL;
151         rel->index_inner_paths = NIL;
152
153         /* Check type of rtable entry */
154         switch (rte->rtekind)
155         {
156                 case RTE_RELATION:
157                         /* Table --- retrieve statistics from the system catalogs */
158                         get_relation_info(rte->relid, rel);
159                         break;
160                 case RTE_SUBQUERY:
161                 case RTE_FUNCTION:
162                         /* Subquery or function --- need only set up attr range */
163                         /* Note: 0 is included in range to support whole-row Vars */
164                         rel->min_attr = 0;
165                         rel->max_attr = list_length(rte->eref->colnames);
166                         break;
167                 default:
168                         elog(ERROR, "unrecognized RTE kind: %d",
169                                  (int) rte->rtekind);
170                         break;
171         }
172
173         Assert(rel->max_attr >= rel->min_attr);
174         rel->attr_needed = (Relids *)
175                 palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
176         rel->attr_widths = (int32 *)
177                 palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
178
179         return rel;
180 }
181
182 /*
183  * find_base_rel
184  *        Find a base or other relation entry, which must already exist
185  *        (since we'd have no idea which list to add it to).
186  */
187 RelOptInfo *
188 find_base_rel(Query *root, int relid)
189 {
190         ListCell   *l;
191         RelOptInfo *rel;
192
193         foreach(l, root->base_rel_list)
194         {
195                 rel = (RelOptInfo *) lfirst(l);
196                 if (relid == rel->relid)
197                         return rel;
198         }
199
200         foreach(l, root->other_rel_list)
201         {
202                 rel = (RelOptInfo *) lfirst(l);
203                 if (relid == rel->relid)
204                         return rel;
205         }
206
207         elog(ERROR, "no relation entry for relid %d", relid);
208
209         return NULL;                            /* keep compiler quiet */
210 }
211
212 /*
213  * find_join_rel
214  *        Returns relation entry corresponding to 'relids' (a set of RT indexes),
215  *        or NULL if none exists.  This is for join relations.
216  */
217 RelOptInfo *
218 find_join_rel(Query *root, Relids relids)
219 {
220         ListCell   *l;
221
222         foreach(l, root->join_rel_list)
223         {
224                 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
225
226                 if (bms_equal(rel->relids, relids))
227                         return rel;
228         }
229
230         return NULL;
231 }
232
233 /*
234  * build_join_rel
235  *        Returns relation entry corresponding to the union of two given rels,
236  *        creating a new relation entry if none already exists.
237  *
238  * 'joinrelids' is the Relids set that uniquely identifies the join
239  * 'outer_rel' and 'inner_rel' are relation nodes for the relations to be
240  *              joined
241  * 'jointype': type of join (inner/outer)
242  * 'restrictlist_ptr': result variable.  If not NULL, *restrictlist_ptr
243  *              receives the list of RestrictInfo nodes that apply to this
244  *              particular pair of joinable relations.
245  *
246  * restrictlist_ptr makes the routine's API a little grotty, but it saves
247  * duplicated calculation of the restrictlist...
248  */
249 RelOptInfo *
250 build_join_rel(Query *root,
251                            Relids joinrelids,
252                            RelOptInfo *outer_rel,
253                            RelOptInfo *inner_rel,
254                            JoinType jointype,
255                            List **restrictlist_ptr)
256 {
257         RelOptInfo *joinrel;
258         List       *restrictlist;
259
260         /*
261          * See if we already have a joinrel for this set of base rels.
262          */
263         joinrel = find_join_rel(root, joinrelids);
264
265         if (joinrel)
266         {
267                 /*
268                  * Yes, so we only need to figure the restrictlist for this
269                  * particular pair of component relations.
270                  */
271                 if (restrictlist_ptr)
272                         *restrictlist_ptr = build_joinrel_restrictlist(root,
273                                                                                                                    joinrel,
274                                                                                                                    outer_rel,
275                                                                                                                    inner_rel,
276                                                                                                                    jointype);
277                 return joinrel;
278         }
279
280         /*
281          * Nope, so make one.
282          */
283         joinrel = makeNode(RelOptInfo);
284         joinrel->reloptkind = RELOPT_JOINREL;
285         joinrel->relids = bms_copy(joinrelids);
286         joinrel->rows = 0;
287         joinrel->width = 0;
288         FastListInit(&joinrel->reltargetlist);
289         joinrel->pathlist = NIL;
290         joinrel->cheapest_startup_path = NULL;
291         joinrel->cheapest_total_path = NULL;
292         joinrel->cheapest_unique_path = NULL;
293         joinrel->relid = 0;                     /* indicates not a baserel */
294         joinrel->rtekind = RTE_JOIN;
295         joinrel->min_attr = 0;
296         joinrel->max_attr = 0;
297         joinrel->attr_needed = NULL;
298         joinrel->attr_widths = NULL;
299         joinrel->indexlist = NIL;
300         joinrel->pages = 0;
301         joinrel->tuples = 0;
302         joinrel->subplan = NULL;
303         joinrel->baserestrictinfo = NIL;
304         joinrel->baserestrictcost.startup = 0;
305         joinrel->baserestrictcost.per_tuple = 0;
306         joinrel->outerjoinset = NULL;
307         joinrel->joininfo = NIL;
308         joinrel->index_outer_relids = NULL;
309         joinrel->index_inner_paths = NIL;
310
311         /*
312          * Create a new tlist containing just the vars that need to be output
313          * from this join (ie, are needed for higher joinclauses or final
314          * output).
315          */
316         build_joinrel_tlist(root, joinrel);
317
318         /*
319          * Construct restrict and join clause lists for the new joinrel. (The
320          * caller might or might not need the restrictlist, but I need it
321          * anyway for set_joinrel_size_estimates().)
322          */
323         restrictlist = build_joinrel_restrictlist(root,
324                                                                                           joinrel,
325                                                                                           outer_rel,
326                                                                                           inner_rel,
327                                                                                           jointype);
328         if (restrictlist_ptr)
329                 *restrictlist_ptr = restrictlist;
330         build_joinrel_joinlist(joinrel, outer_rel, inner_rel);
331
332         /*
333          * Set estimates of the joinrel's size.
334          */
335         set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
336                                                            jointype, restrictlist);
337
338         /*
339          * Add the joinrel to the query's joinrel list.
340          */
341         root->join_rel_list = lcons(joinrel, root->join_rel_list);
342
343         return joinrel;
344 }
345
346 /*
347  * build_joinrel_tlist
348  *        Builds a join relation's target list.
349  *
350  * The join's targetlist includes all Vars of its member relations that
351  * will still be needed above the join.
352  *
353  * In a former lifetime, this just merged the tlists of the two member
354  * relations first presented.  While we could still do that, working from
355  * lists of Vars would mean doing a find_base_rel lookup for each Var.
356  * It seems more efficient to scan the list of base rels and collect the
357  * needed vars directly from there.
358  *
359  * We also compute the expected width of the join's output, making use
360  * of data that was cached at the baserel level by set_rel_width().
361  */
362 static void
363 build_joinrel_tlist(Query *root, RelOptInfo *joinrel)
364 {
365         Relids          relids = joinrel->relids;
366         ListCell   *rels;
367
368         FastListInit(&joinrel->reltargetlist);
369         joinrel->width = 0;
370
371         foreach(rels, root->base_rel_list)
372         {
373                 RelOptInfo *baserel = (RelOptInfo *) lfirst(rels);
374                 ListCell   *vars;
375
376                 if (!bms_is_member(baserel->relid, relids))
377                         continue;
378
379                 foreach(vars, FastListValue(&baserel->reltargetlist))
380                 {
381                         Var                *var = (Var *) lfirst(vars);
382                         int                     ndx = var->varattno - baserel->min_attr;
383
384                         if (bms_nonempty_difference(baserel->attr_needed[ndx], relids))
385                         {
386                                 FastAppend(&joinrel->reltargetlist, var);
387                                 Assert(baserel->attr_widths[ndx] > 0);
388                                 joinrel->width += baserel->attr_widths[ndx];
389                         }
390                 }
391         }
392 }
393
394 /*
395  * build_joinrel_restrictlist
396  * build_joinrel_joinlist
397  *        These routines build lists of restriction and join clauses for a
398  *        join relation from the joininfo lists of the relations it joins.
399  *
400  *        These routines are separate because the restriction list must be
401  *        built afresh for each pair of input sub-relations we consider, whereas
402  *        the join lists need only be computed once for any join RelOptInfo.
403  *        The join lists are fully determined by the set of rels making up the
404  *        joinrel, so we should get the same results (up to ordering) from any
405  *        candidate pair of sub-relations.      But the restriction list is whatever
406  *        is not handled in the sub-relations, so it depends on which
407  *        sub-relations are considered.
408  *
409  *        If a join clause from an input relation refers to base rels still not
410  *        present in the joinrel, then it is still a join clause for the joinrel;
411  *        we put it into an appropriate JoinInfo list for the joinrel.  Otherwise,
412  *        the clause is now a restrict clause for the joined relation, and we
413  *        return it to the caller of build_joinrel_restrictlist() to be stored in
414  *        join paths made from this pair of sub-relations.      (It will not need to
415  *        be considered further up the join tree.)
416  *
417  *        When building a restriction list, we eliminate redundant clauses.
418  *        We don't try to do that for join clause lists, since the join clauses
419  *        aren't really doing anything, just waiting to become part of higher
420  *        levels' restriction lists.
421  *
422  * 'joinrel' is a join relation node
423  * 'outer_rel' and 'inner_rel' are a pair of relations that can be joined
424  *              to form joinrel.
425  * 'jointype' is the type of join used.
426  *
427  * build_joinrel_restrictlist() returns a list of relevant restrictinfos,
428  * whereas build_joinrel_joinlist() stores its results in the joinrel's
429  * joininfo lists.      One or the other must accept each given clause!
430  *
431  * NB: Formerly, we made deep(!) copies of each input RestrictInfo to pass
432  * up to the join relation.  I believe this is no longer necessary, because
433  * RestrictInfo nodes are no longer context-dependent.  Instead, just include
434  * the original nodes in the lists made for the join relation.
435  */
436 static List *
437 build_joinrel_restrictlist(Query *root,
438                                                    RelOptInfo *joinrel,
439                                                    RelOptInfo *outer_rel,
440                                                    RelOptInfo *inner_rel,
441                                                    JoinType jointype)
442 {
443         List       *result;
444         List       *rlist;
445
446         /*
447          * Collect all the clauses that syntactically belong at this level.
448          */
449         rlist = list_concat(subbuild_joinrel_restrictlist(joinrel,
450                                                                                                           outer_rel->joininfo),
451                                                 subbuild_joinrel_restrictlist(joinrel,
452                                                                                                           inner_rel->joininfo));
453
454         /*
455          * Eliminate duplicate and redundant clauses.
456          *
457          * We must eliminate duplicates, since we will see many of the same
458          * clauses arriving from both input relations.  Also, if a clause is a
459          * mergejoinable clause, it's possible that it is redundant with
460          * previous clauses (see optimizer/README for discussion).      We detect
461          * that case and omit the redundant clause from the result list.
462          */
463         result = remove_redundant_join_clauses(root, rlist, jointype);
464
465         list_free(rlist);
466
467         return result;
468 }
469
470 static void
471 build_joinrel_joinlist(RelOptInfo *joinrel,
472                                            RelOptInfo *outer_rel,
473                                            RelOptInfo *inner_rel)
474 {
475         subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo);
476         subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo);
477 }
478
479 static List *
480 subbuild_joinrel_restrictlist(RelOptInfo *joinrel,
481                                                           List *joininfo_list)
482 {
483         List       *restrictlist = NIL;
484         ListCell   *xjoininfo;
485
486         foreach(xjoininfo, joininfo_list)
487         {
488                 JoinInfo   *joininfo = (JoinInfo *) lfirst(xjoininfo);
489
490                 if (bms_is_subset(joininfo->unjoined_relids, joinrel->relids))
491                 {
492                         /*
493                          * Clauses in this JoinInfo list become restriction clauses
494                          * for the joinrel, since they refer to no outside rels.
495                          *
496                          * We must copy the list to avoid disturbing the input relation,
497                          * but we can use a shallow copy.
498                          */
499                         restrictlist = list_concat(restrictlist,
500                                                                            list_copy(joininfo->jinfo_restrictinfo));
501                 }
502                 else
503                 {
504                         /*
505                          * These clauses are still join clauses at this level, so we
506                          * ignore them in this routine.
507                          */
508                 }
509         }
510
511         return restrictlist;
512 }
513
514 static void
515 subbuild_joinrel_joinlist(RelOptInfo *joinrel,
516                                                   List *joininfo_list)
517 {
518         ListCell   *xjoininfo;
519
520         foreach(xjoininfo, joininfo_list)
521         {
522                 JoinInfo   *joininfo = (JoinInfo *) lfirst(xjoininfo);
523                 Relids          new_unjoined_relids;
524
525                 new_unjoined_relids = bms_difference(joininfo->unjoined_relids,
526                                                                                          joinrel->relids);
527                 if (bms_is_empty(new_unjoined_relids))
528                 {
529                         /*
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.
533                          */
534                         bms_free(new_unjoined_relids);
535                 }
536                 else
537                 {
538                         /*
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.)
547                          */
548                         JoinInfo   *new_joininfo;
549
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);
554                 }
555         }
556 }