Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8ab72a3

Browse files
committed
optimizer cleanup
1 parent 61f40ac commit 8ab72a3

File tree

5 files changed

+76
-77
lines changed

5 files changed

+76
-77
lines changed

src/backend/optimizer/README

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ The optimizer generates optimial query plans by doing several steps:
55

66
1) Take each relation in a query, and make a RelOptInfo structure for
77
it. Find each way of accessing the relation, called a Path, including
8-
sequential and index scans, and add it to RelOptInfo.pathlist.
8+
sequential and index scans, and add it to RelOptInfo.pathlist. Also
9+
create RelOptInfo.joininfo that lists all the joins that involve this
10+
relation.
911

10-
2) Join each RelOptInfo to each other RelOptInfo as specified in the
11-
WHERE clause. At this point each RelOptInfo is a single relation, so
12-
you are joining every relation to every relation as joined in the WHERE
13-
clause.
12+
2) Join each RelOptInfo to other RelOptInfo as specified in
13+
RelOptInfo.joininfo. At this point each RelOptInfo is a single
14+
relation, so you are joining every relation to the other relations as
15+
joined in the WHERE clause.
1416

1517
Joins occur using two RelOptInfos. One is outer, the other inner.
1618
Outers drive lookups of values in the inner. In a nested loop, lookups
@@ -137,8 +139,8 @@ Optimizer Structures
137139

138140
RelOptInfo - a relation or joined relations
139141

140-
RestrictInfo - restriction clauses
141-
JoinInfo - join clauses
142+
RestrictInfo - restriction clauses, like "x = 3"
143+
JoinInfo - join clauses, including the relids needed for the join
142144

143145
Path - every way to generate a RelOptInfo(sequential,index,joins)
144146
IndexPath - index scans

src/backend/optimizer/path/pathkey.c

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/Attic/pathkey.c,v 1.1 1999/02/18 19:58:52 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/Attic/pathkey.c,v 1.2 1999/02/19 02:05:15 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -27,9 +27,9 @@
2727

2828

2929
static int match_pathkey_joinkeys(List *pathkey, List *joinkeys,
30-
int which_subkey);
30+
int outer_or_inner);
3131
static bool every_func(List *joinkeys, List *pathkey,
32-
int which_subkey);
32+
int outer_or_inner);
3333
static List *new_join_pathkey(List *subkeys, List *considered_subkeys,
3434
List *join_rel_tlist, List *joinclauses);
3535
static List *new_matching_subkeys(Var *subkey, List *considered_subkeys,
@@ -54,7 +54,7 @@ static List *new_matching_subkeys(Var *subkey, List *considered_subkeys,
5454
* ( (outer inner) (outer inner) ... )
5555
* 'joinclauses' is a list of clauses corresponding to the join keys in
5656
* 'joinkeys'
57-
* 'which_subkey' is a flag that selects the desired subkey of a join key
57+
* 'outer_or_inner' is a flag that selects the desired subkey of a join key
5858
* in 'joinkeys'
5959
*
6060
* Returns the join keys and corresponding join clauses in a list if all
@@ -72,7 +72,7 @@ List *
7272
match_pathkeys_joinkeys(List *pathkeys,
7373
List *joinkeys,
7474
List *joinclauses,
75-
int which_subkey,
75+
int outer_or_inner,
7676
List **matchedJoinClausesPtr)
7777
{
7878
List *matched_joinkeys = NIL;
@@ -84,19 +84,17 @@ match_pathkeys_joinkeys(List *pathkeys,
8484
foreach(i, pathkeys)
8585
{
8686
pathkey = lfirst(i);
87-
matched_joinkey_index = match_pathkey_joinkeys(pathkey, joinkeys, which_subkey);
87+
matched_joinkey_index = match_pathkey_joinkeys(pathkey, joinkeys,
88+
outer_or_inner);
8889

8990
if (matched_joinkey_index != -1)
9091
{
9192
List *xjoinkey = nth(matched_joinkey_index, joinkeys);
9293
List *joinclause = nth(matched_joinkey_index, joinclauses);
9394

94-
/* XXX was "push" function */
95-
matched_joinkeys = lappend(matched_joinkeys, xjoinkey);
96-
matched_joinkeys = nreverse(matched_joinkeys);
95+
matched_joinkeys = lcons(xjoinkey, matched_joinkeys);
96+
matched_joinclauses = lcons(joinclause, matched_joinclauses);
9797

98-
matched_joinclauses = lappend(matched_joinclauses, joinclause);
99-
matched_joinclauses = nreverse(matched_joinclauses);
10098
joinkeys = LispRemove(xjoinkey, joinkeys);
10199
}
102100
else
@@ -111,6 +109,7 @@ match_pathkeys_joinkeys(List *pathkeys,
111109
return nreverse(matched_joinkeys);
112110
}
113111

112+
114113
/*
115114
* match_pathkey_joinkeys
116115
* Returns the 0-based index into 'joinkeys' of the first joinkey whose
@@ -119,7 +118,7 @@ match_pathkeys_joinkeys(List *pathkeys,
119118
static int
120119
match_pathkey_joinkeys(List *pathkey,
121120
List *joinkeys,
122-
int which_subkey)
121+
int outer_or_inner)
123122
{
124123
Var *path_subkey;
125124
int pos;
@@ -135,14 +134,15 @@ match_pathkey_joinkeys(List *pathkey,
135134
{
136135
jk = (JoinKey *) lfirst(x);
137136
if (var_equal(path_subkey,
138-
extract_join_subkey(jk, which_subkey)))
137+
extract_join_key(jk, outer_or_inner)))
139138
return pos;
140139
pos++;
141140
}
142141
}
143142
return -1; /* no index found */
144143
}
145144

145+
146146
/*
147147
* match_paths_joinkeys
148148
* Attempts to find a path in 'paths' whose keys match a set of join
@@ -159,53 +159,16 @@ match_pathkey_joinkeys(List *pathkey,
159159
* must correspond
160160
* 'paths' is a list of(inner) paths which are to be matched against
161161
* each join key in 'joinkeys'
162-
* 'which_subkey' is a flag that selects the desired subkey of a join key
162+
* 'outer_or_inner' is a flag that selects the desired subkey of a join key
163163
* in 'joinkeys'
164164
*
165-
* Returns the matching path node if one exists, nil otherwise.
166-
*/
167-
static bool
168-
every_func(List *joinkeys, List *pathkey, int which_subkey)
169-
{
170-
JoinKey *xjoinkey;
171-
Var *temp;
172-
Var *tempkey = NULL;
173-
bool found = false;
174-
List *i = NIL;
175-
List *j = NIL;
176-
177-
foreach(i, joinkeys)
178-
{
179-
xjoinkey = (JoinKey *) lfirst(i);
180-
found = false;
181-
foreach(j, pathkey)
182-
{
183-
temp = (Var *) lfirst((List *) lfirst(j));
184-
if (temp == NULL)
185-
continue;
186-
tempkey = extract_join_subkey(xjoinkey, which_subkey);
187-
if (var_equal(tempkey, temp))
188-
{
189-
found = true;
190-
break;
191-
}
192-
}
193-
if (found == false)
194-
return false;
195-
}
196-
return found;
197-
}
198-
199-
200-
/*
201-
* match_paths_joinkeys -
202-
* find the cheapest path that matches the join keys
165+
* Find the cheapest path that matches the join keys
203166
*/
204167
Path *
205168
match_paths_joinkeys(List *joinkeys,
206169
PathOrder *ordering,
207170
List *paths,
208-
int which_subkey)
171+
int outer_or_inner)
209172
{
210173
Path *matched_path = NULL;
211174
bool key_match = false;
@@ -216,13 +179,12 @@ match_paths_joinkeys(List *joinkeys,
216179
Path *path = (Path *) lfirst(i);
217180
int better_sort;
218181

219-
key_match = every_func(joinkeys, path->pathkeys, which_subkey);
182+
key_match = every_func(joinkeys, path->pathkeys, outer_or_inner);
220183

221184
if (pathorder_match(ordering, path->pathorder, &better_sort) &&
222185
better_sort == 0 &&
223186
length(joinkeys) == length(path->pathkeys) && key_match)
224187
{
225-
226188
if (matched_path)
227189
{
228190
if (path->path_cost < matched_path->path_cost)
@@ -236,7 +198,6 @@ match_paths_joinkeys(List *joinkeys,
236198
}
237199

238200

239-
240201
/*
241202
* extract_path_keys
242203
* Builds a subkey list for a path by pulling one of the subkeys from
@@ -245,7 +206,7 @@ match_paths_joinkeys(List *joinkeys,
245206
*
246207
* 'joinkeys' is a list of join key pairs
247208
* 'tlist' is a relation target list
248-
* 'which_subkey' is a flag that selects the desired subkey of a join key
209+
* 'outer_or_inner' is a flag that selects the desired subkey of a join key
249210
* in 'joinkeys'
250211
*
251212
* Returns a list of pathkeys: ((tlvar1)(tlvar2)...(tlvarN)).
@@ -254,7 +215,7 @@ match_paths_joinkeys(List *joinkeys,
254215
List *
255216
extract_path_keys(List *joinkeys,
256217
List *tlist,
257-
int which_subkey)
218+
int outer_or_inner)
258219
{
259220
List *pathkeys = NIL;
260221
List *jk;
@@ -269,7 +230,7 @@ extract_path_keys(List *joinkeys,
269230
/*
270231
* find the right Var in the target list for this key
271232
*/
272-
var = (Var *) extract_join_subkey(jkey, which_subkey);
233+
var = (Var *) extract_join_key(jkey, outer_or_inner);
273234
key = (Var *) matching_tlist_var(var, tlist);
274235

275236
/*
@@ -291,6 +252,42 @@ extract_path_keys(List *joinkeys,
291252
}
292253

293254

255+
/*
256+
* every_func
257+
*/
258+
static bool
259+
every_func(List *joinkeys, List *pathkey, int outer_or_inner)
260+
{
261+
JoinKey *xjoinkey;
262+
Var *temp;
263+
Var *tempkey = NULL;
264+
bool found = false;
265+
List *i = NIL;
266+
List *j = NIL;
267+
268+
foreach(i, joinkeys)
269+
{
270+
xjoinkey = (JoinKey *) lfirst(i);
271+
found = false;
272+
foreach(j, pathkey)
273+
{
274+
temp = (Var *) lfirst((List *) lfirst(j));
275+
if (temp == NULL)
276+
continue;
277+
tempkey = extract_join_key(xjoinkey, outer_or_inner);
278+
if (var_equal(tempkey, temp))
279+
{
280+
found = true;
281+
break;
282+
}
283+
}
284+
if (found == false)
285+
return false;
286+
}
287+
return found;
288+
}
289+
290+
294291
/****************************************************************************
295292
* NEW PATHKEY FORMATION
296293
****************************************************************************/

src/backend/optimizer/util/keys.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/Attic/keys.c,v 1.17 1999/02/13 23:16:45 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/Attic/keys.c,v 1.18 1999/02/19 02:05:16 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -80,17 +80,17 @@ equal_indexkey_var(int index_key, Var *var)
8080
}
8181

8282
/*
83-
* extract_join_subkey
83+
* extract_join_key
8484
* Returns the subkey in a join key corresponding to the outer or inner
8585
* relation.
8686
*
8787
*/
8888
Var *
89-
extract_join_subkey(JoinKey *jk, int which_subkey)
89+
extract_join_key(JoinKey *jk, int outer_or_inner)
9090
{
9191
Var *retval;
9292

93-
switch (which_subkey)
93+
switch (outer_or_inner)
9494
{
9595
case OUTER:
9696
retval = jk->outer;
@@ -99,7 +99,7 @@ extract_join_subkey(JoinKey *jk, int which_subkey)
9999
retval = jk->inner;
100100
break;
101101
default: /* do nothing */
102-
elog(DEBUG, "extract_join_subkey with neither INNER or OUTER");
102+
elog(DEBUG, "extract_join_key with neither INNER or OUTER");
103103
retval = NULL;
104104
}
105105
return retval;

src/include/optimizer/keys.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: keys.h,v 1.12 1999/02/13 23:21:49 momjian Exp $
9+
* $Id: keys.h,v 1.13 1999/02/19 02:05:18 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -17,7 +17,7 @@
1717
#include "nodes/relation.h"
1818

1919
extern bool match_indexkey_operand(int indexkey, Var *operand, RelOptInfo *rel);
20-
extern Var *extract_join_subkey(JoinKey *jk, int which_subkey);
20+
extern Var *extract_join_key(JoinKey *jk, int outer_or_inner);
2121
extern bool pathkeys_match(List *keys1, List *keys2, int *better_key);
2222
extern List *collect_index_pathkeys(int *index_keys, List *tlist);
2323

src/include/optimizer/paths.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: paths.h,v 1.22 1999/02/18 04:45:36 momjian Exp $
10+
* $Id: paths.h,v 1.23 1999/02/19 02:05:20 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -55,12 +55,12 @@ extern List *group_clauses_by_hashop(List *restrictinfo_list,
5555
* generic join method key/clause routines
5656
*/
5757
extern List *match_pathkeys_joinkeys(List *pathkeys,
58-
List *joinkeys, List *joinclauses, int which_subkey,
58+
List *joinkeys, List *joinclauses, int outer_or_inner,
5959
List **matchedJoinClausesPtr);
6060
extern List *extract_path_keys(List *joinkeys, List *tlist,
61-
int which_subkey);
61+
int outer_or_inner);
6262
extern Path *match_paths_joinkeys(List *joinkeys, PathOrder *ordering,
63-
List *paths, int which_subkey);
63+
List *paths, int outer_or_inner);
6464
extern List *new_join_pathkeys(List *outer_pathkeys,
6565
List *join_rel_tlist, List *joinclauses);
6666

0 commit comments

Comments
 (0)