aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer/util/relnode.c
blob: 5346a1c72a5933798c0bf8c8ea3c244cb28ac5f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*-------------------------------------------------------------------------
 *
 * relnode.c
 *	  Relation manipulation routines
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/relnode.c,v 1.16 1999/05/25 16:10:01 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "nodes/relation.h"

#include "optimizer/internal.h"
#include "optimizer/pathnode.h" /* where the decls go */
#include "optimizer/plancat.h"



/*
 * get_base_rel
 *	  Returns relation entry corresponding to 'relid', creating a new one if
 *	  necessary. This is for base relations.
 *
 */
RelOptInfo *
get_base_rel(Query *root, int relid)
{
	Relids		relids;
	RelOptInfo *rel;

	relids = lconsi(relid, NIL);
	rel = rel_member(relids, root->base_rel_list);
	if (rel == NULL)
	{
		rel = makeNode(RelOptInfo);
		rel->relids = relids;
		rel->indexed = false;
		rel->pages = 0;
		rel->tuples = 0;
		rel->width = 0;
		rel->targetlist = NIL;
		rel->pathlist = NIL;
		rel->cheapestpath = (Path *) NULL;
		rel->pruneable = true;
		rel->classlist = NULL;
		rel->ordering = NULL;
		rel->relam = InvalidOid;
		rel->restrictinfo = NIL;
		rel->joininfo = NIL;
		rel->innerjoin = NIL;

		root->base_rel_list = lcons(rel, root->base_rel_list);

		/*
		 * ??? the old lispy C code (get_rel) do a listp(relid) here but
		 * that can never happen since we already established relid is not
		 * a list.								-ay 10/94
		 */
		if (relid < 0)
		{

			/*
			 * If the relation is a materialized relation, assume
			 * constants for sizes.
			 */
			rel->pages = _NONAME_RELATION_PAGES_;
			rel->tuples = _NONAME_RELATION_TUPLES_;
		}
		else
		{
			bool		hasindex;
			int			pages,
						tuples;

			/*
			 * Otherwise, retrieve relation characteristics from the
			 * system catalogs.
			 */
			relation_info(root, relid, &hasindex, &pages, &tuples);
			rel->indexed = hasindex;
			rel->pages = pages;
			rel->tuples = tuples;
		}
	}
	return rel;
}

/*
 * get_join_rel
 *	  Returns relation entry corresponding to 'relid' (a list of relids),
 *	  or NULL.
 */
RelOptInfo *
get_join_rel(Query *root, Relids relid)
{
	return rel_member(relid, root->join_rel_list);
}

/*
 * rel_member
 *	  Determines whether a relation of id 'relid' is contained within a list
 *	  'rels'.
 *
 * Returns the corresponding entry in 'rels' if it is there.
 *
 */
RelOptInfo *
rel_member(Relids relids, List *rels)
{
	List	   *temp = NIL;
	List	   *temprelid = NIL;

	if (relids != NIL && rels != NIL)
	{
		foreach(temp, rels)
		{
			temprelid = ((RelOptInfo *) lfirst(temp))->relids;
			if (same(temprelid, relids))
				return (RelOptInfo *) (lfirst(temp));
		}
	}
	return NULL;
}