]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/util/relnode.c
Add:
[postgresql] / src / backend / optimizer / util / relnode.c
1 /*-------------------------------------------------------------------------
2  *
3  * relnode.c
4  *        Relation manipulation routines
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/optimizer/util/relnode.c,v 1.21 2000/01/26 05:56:40 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17
18 #include "optimizer/internal.h"
19 #include "optimizer/pathnode.h"
20 #include "optimizer/plancat.h"
21
22
23
24 /*
25  * get_base_rel
26  *        Returns relation entry corresponding to 'relid', creating a new one if
27  *        necessary. This is for base relations.
28  *
29  */
30 RelOptInfo *
31 get_base_rel(Query *root, int relid)
32 {
33         Relids          relids = lconsi(relid, NIL);
34         RelOptInfo *rel;
35
36         rel = rel_member(relids, root->base_rel_list);
37         if (rel == NULL)
38         {
39                 rel = makeNode(RelOptInfo);
40                 rel->relids = relids;
41                 rel->rows = 0;
42                 rel->width = 0;
43                 rel->targetlist = NIL;
44                 rel->pathlist = NIL;
45                 rel->cheapestpath = (Path *) NULL;
46                 rel->pruneable = true;
47                 rel->indexed = false;
48                 rel->pages = 0;
49                 rel->tuples = 0;
50                 rel->restrictinfo = NIL;
51                 rel->joininfo = NIL;
52                 rel->innerjoin = NIL;
53
54                 root->base_rel_list = lcons(rel, root->base_rel_list);
55
56                 if (relid < 0)
57                 {
58                         /*
59                          * If the relation is a materialized relation, assume
60                          * constants for sizes.
61                          */
62                         rel->pages = _NONAME_RELATION_PAGES_;
63                         rel->tuples = _NONAME_RELATION_TUPLES_;
64                 }
65                 else
66                 {
67                         /*
68                          * Otherwise, retrieve relation statistics from the
69                          * system catalogs.
70                          */
71                         relation_info(root, relid,
72                                                   &rel->indexed, &rel->pages, &rel->tuples);
73                 }
74         }
75         return rel;
76 }
77
78 /*
79  * get_join_rel
80  *        Returns relation entry corresponding to 'relid' (a list of relids),
81  *        or NULL.
82  */
83 RelOptInfo *
84 get_join_rel(Query *root, Relids relid)
85 {
86         return rel_member(relid, root->join_rel_list);
87 }
88
89 /*
90  * rel_member
91  *        Determines whether a relation of id 'relid' is contained within a list
92  *        'rels'.
93  *
94  * Returns the corresponding entry in 'rels' if it is there.
95  *
96  */
97 RelOptInfo *
98 rel_member(Relids relids, List *rels)
99 {
100         if (relids != NIL && rels != NIL)
101         {
102                 List       *temp;
103
104                 foreach(temp, rels)
105                 {
106                         RelOptInfo *rel = (RelOptInfo *) lfirst(temp);
107
108                         if (same(rel->relids, relids))
109                                 return rel;
110                 }
111         }
112         return NULL;
113 }