pathnode.c 15.2 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * pathnode.c--
4
 *	  Routines to manipulate pathlists and create path nodes
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
B
Bruce Momjian 已提交
10
 *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/pathnode.c,v 1.20 1999/02/08 04:29:21 momjian Exp $
11 12 13 14 15 16 17 18 19 20 21 22
 *
 *-------------------------------------------------------------------------
 */
#include <math.h>

#include "postgres.h"

#include "nodes/relation.h"
#include "utils/elog.h"

#include "optimizer/internal.h"
#include "optimizer/pathnode.h"
23
#include "optimizer/restrictinfo.h"
24 25 26 27 28 29
#include "optimizer/plancat.h"
#include "optimizer/cost.h"
#include "optimizer/keys.h"
#include "optimizer/xfunc.h"
#include "optimizer/ordering.h"

30
#include "parser/parsetree.h"	/* for getrelid() */
31

32
static Path *better_path(Path *new_path, List *unique_paths, bool *noOther);
33 34 35


/*****************************************************************************
36
 *		MISC. PATH UTILITIES
37 38
 *****************************************************************************/

39
/*
40
 * path-is-cheaper--
41 42
 *	  Returns t iff 'path1' is cheaper than 'path2'.
 *
43 44
 */
bool
45
path_is_cheaper(Path *path1, Path *path2)
46
{
47 48
	Cost		cost1 = path1->path_cost;
	Cost		cost2 = path2->path_cost;
49

50
	return (bool) (cost1 < cost2);
51 52
}

53
/*
54
 * set_cheapest--
55 56
 *	  Finds the minimum cost path from among a relation's paths.
 *
57 58
 * 'parent-rel' is the parent relation
 * 'pathlist' is a list of path nodes corresponding to 'parent-rel'
59 60
 *
 * Returns and sets the relation entry field with the pathnode that
61
 * is minimum.
62
 *
63
 */
64
Path *
65
set_cheapest(RelOptInfo *parent_rel, List *pathlist)
66
{
67 68
	List	   *p;
	Path	   *cheapest_so_far;
69

70
	Assert(pathlist != NIL);
B
Bruce Momjian 已提交
71
	Assert(IsA(parent_rel, RelOptInfo));
72

73
	cheapest_so_far = (Path *) lfirst(pathlist);
74

75 76
	foreach(p, lnext(pathlist))
	{
77
		Path	   *path = (Path *) lfirst(p);
78

79 80
		if (path_is_cheaper(path, cheapest_so_far))
			cheapest_so_far = path;
81 82
	}

83
	parent_rel->cheapestpath = cheapest_so_far;
84

85
	return cheapest_so_far;
86 87
}

88
/*
89
 * add_pathlist--
90 91 92 93 94 95
 *	  For each path in the list 'new-paths', add to the list 'unique-paths'
 *	  only those paths that are unique (i.e., unique ordering and ordering
 *	  keys).  Should a conflict arise, the more expensive path is thrown out,
 *	  thereby pruning the plan space.  But we don't prune if xfunc
 *	  told us not to.
 *
96
 * 'parent-rel' is the relation entry to which these paths correspond.
97
 *
98
 * Returns the list of unique pathnodes.
99
 *
100
 */
101
List *
102
add_pathlist(RelOptInfo *parent_rel, List *unique_paths, List *new_paths)
103
{
B
Bruce Momjian 已提交
104
	List	   *p1;
105

B
Bruce Momjian 已提交
106
	foreach(p1, new_paths)
107
	{
B
Bruce Momjian 已提交
108 109 110 111 112
		Path	   *new_path = (Path *) lfirst(p1);
 		Path	   *old_path;
		bool		noOther;

		/* Is this new path already in unique_paths? */
113 114
		if (member(new_path, unique_paths))
			continue;
B
Bruce Momjian 已提交
115 116

		/* Find best matching path */
117 118 119 120
		old_path = better_path(new_path, unique_paths, &noOther);

		if (noOther)
		{
B
Bruce Momjian 已提交
121
			/* This is a brand new path.  */
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
			new_path->parent = parent_rel;
			unique_paths = lcons(new_path, unique_paths);
		}
		else if (old_path == NULL)
		{
			;					/* do nothing if path is not cheaper */
		}
		else if (old_path != NULL)
		{						/* (IsA(old_path,Path)) { */
			new_path->parent = parent_rel;
			if (!parent_rel->pruneable)
				unique_paths = lcons(new_path, unique_paths);
			else
				unique_paths = lcons(new_path,
									 LispRemove(old_path, unique_paths));
		}
138
	}
139
	return unique_paths;
140 141
}

142
/*
143
 * better_path--
144 145 146 147
 *	  Determines whether 'new-path' has the same ordering and keys as some
 *	  path in the list 'unique-paths'.	If there is a redundant path,
 *	  eliminate the more expensive path.
 *
148
 * Returns:
149 150 151 152 153
 *	  The old path - if 'new-path' matches some path in 'unique-paths' and is
 *				cheaper
 *	  nil - if 'new-path' matches but isn't cheaper
 *	  t - if there is no path in the list with the same ordering and keys
 *
154
 */
155
static Path *
156
better_path(Path *new_path, List *unique_paths, bool *noOther)
157
{
158 159 160 161
	Path	   *old_path = (Path *) NULL;
	Path	   *path = (Path *) NULL;
	List	   *temp = NIL;
	Path	   *retval = NULL;
162 163 164 165 166

	foreach(temp, unique_paths)
	{
		path = (Path *) lfirst(temp);

B
Bruce Momjian 已提交
167
		if (samekeys(path->keys, new_path->keys) &&
B
Bruce Momjian 已提交
168 169
			equal_path_ordering(&path->path_order,
								&new_path->path_order))
170 171 172 173
		{
			old_path = path;
			break;
		}
174
	}
175 176 177 178 179 180 181 182

	if (old_path == NULL)
		*noOther = true;
	else
	{
		*noOther = false;
		if (path_is_cheaper(new_path, old_path))
			retval = old_path;
183
	}
184

185
	return retval;
186 187 188 189 190
}



/*****************************************************************************
191
 *		PATH NODE CREATION ROUTINES
192 193
 *****************************************************************************/

194
/*
195
 * create_seqscan_path--
196 197 198
 *	  Creates a path corresponding to a sequential scan, returning the
 *	  pathnode.
 *
199
 */
200
Path *
201
create_seqscan_path(RelOptInfo * rel)
202
{
203
	int			relid = 0;
204

205
	Path	   *pathnode = makeNode(Path);
206 207 208 209

	pathnode->pathtype = T_SeqScan;
	pathnode->parent = rel;
	pathnode->path_cost = 0.0;
B
Bruce Momjian 已提交
210 211
	pathnode->path_order.ordtype = SORTOP_ORDER;
	pathnode->path_order.ord.sortop = NULL;
212 213 214
	pathnode->keys = NIL;

	/*
215
	 * copy restrictinfo list into path for expensive function processing --
216 217
	 * JMH, 7/7/92
	 */
218
	pathnode->loc_restrictinfo = (List *) copyObject((Node *) rel->restrictinfo);
219 220 221 222 223 224 225

	if (rel->relids != NULL)
		relid = lfirsti(rel->relids);

	pathnode->path_cost = cost_seqscan(relid,
									   rel->pages, rel->tuples);
	/* add in expensive functions cost!  -- JMH, 7/7/92 */
226
#if 0
227 228
	if (XfuncMode != XFUNC_OFF)
	{
229
		pathnode->path_cost += xfunc_get_path_cost(pathnode);
230
	}
231
#endif
232
	return pathnode;
233 234
}

235
/*
236
 * create_index_path--
237 238
 *	  Creates a single path node for an index scan.
 *
239 240 241 242
 * 'rel' is the parent rel
 * 'index' is the pathnode for the index on 'rel'
 * 'restriction-clauses' is a list of restriction clause nodes.
 * 'is-join-scan' is a flag indicating whether or not the index is being
243 244
 *		considered because of its sort order.
 *
245
 * Returns the new path node.
246
 *
247
 */
248
IndexPath  *
249
create_index_path(Query *root,
250 251
				  RelOptInfo * rel,
				  RelOptInfo * index,
252
				  List *restriction_clauses,
253
				  bool is_join_scan)
254
{
255
	IndexPath  *pathnode = makeNode(IndexPath);
256 257 258

	pathnode->path.pathtype = T_IndexScan;
	pathnode->path.parent = rel;
B
Bruce Momjian 已提交
259 260
	pathnode->path.path_order.ordtype = SORTOP_ORDER;
	pathnode->path.path_order.ord.sortop = index->ordering;
261 262 263 264 265

	pathnode->indexid = index->relids;
	pathnode->indexkeys = index->indexkeys;
	pathnode->indexqual = NIL;

266
	/*
267
	 * copy restrictinfo list into path for expensive function processing --
268
	 * JMH, 7/7/92
269
	 */
270
	pathnode->path.loc_restrictinfo = set_difference((List *) copyObject((Node *) rel->restrictinfo),
271
					   (List *) restriction_clauses);
272 273

	/*
274 275
	 * The index must have an ordering for the path to have (ordering)
	 * keys, and vice versa.
276
	 */
B
Bruce Momjian 已提交
277
	if (pathnode->path.path_order.ord.sortop)
278 279 280 281 282 283 284 285 286 287 288
	{
		pathnode->path.keys = collect_index_pathkeys(index->indexkeys,
													 rel->targetlist);

		/*
		 * Check that the keys haven't 'disappeared', since they may no
		 * longer be in the target list (i.e., index keys that are not
		 * relevant to the scan are not applied to the scan path node, so
		 * if no index keys were found, we can't order the path).
		 */
		if (pathnode->path.keys == NULL)
B
Bruce Momjian 已提交
289
			pathnode->path.path_order.ord.sortop = NULL;
290 291 292 293 294 295 296 297 298 299 300 301 302
	}
	else
		pathnode->path.keys = NULL;

	if (is_join_scan || restriction_clauses == NULL)
	{

		/*
		 * Indices used for joins or sorting result nodes don't restrict
		 * the result at all, they simply order it, so compute the scan
		 * cost accordingly -- use a selectivity of 1.0.
		 */
/* is the statement above really true?	what about IndexScan as the
303
   inner of a join? */
304
		pathnode->path.path_cost = cost_index(lfirsti(index->relids),
305 306 307 308 309 310 311 312
					   index->pages,
					   1.0,
					   rel->pages,
					   rel->tuples,
					   index->pages,
					   index->tuples,
					   false);
		/* add in expensive functions cost!  -- JMH, 7/7/92 */
313
#if 0
314 315
		if (XfuncMode != XFUNC_OFF)
		{
316
			pathnode->path_cost = (pathnode->path_cost +
317 318
				 xfunc_get_path_cost((Path *) pathnode));
		}
319
#endif
320 321 322 323 324 325 326 327
	}
	else
	{

		/*
		 * Compute scan cost for the case when 'index' is used with a
		 * restriction clause.
		 */
328 329 330 331 332 333
		List	   *attnos;
		List	   *values;
		List	   *flags;
		float		npages;
		float		selec;
		Cost		clausesel;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

		get_relattvals(restriction_clauses,
					   &attnos,
					   &values,
					   &flags);
		index_selectivity(lfirsti(index->relids),
						  index->classlist,
						  get_opnos(restriction_clauses),
						  getrelid(lfirsti(rel->relids),
								   root->rtable),
						  attnos,
						  values,
						  flags,
						  length(restriction_clauses),
						  &npages,
						  &selec);
		/* each clause gets an equal selectivity */
B
Bruce Momjian 已提交
351
		clausesel =	pow(selec, 1.0 / (double) length(restriction_clauses));
352 353

		pathnode->indexqual = restriction_clauses;
B
Bruce Momjian 已提交
354 355 356 357 358 359 360 361
		pathnode->path.path_cost = cost_index(lfirsti(index->relids),
											   (int) npages,
											   selec,
											   rel->pages,
											   rel->tuples,
											   index->pages,
											   index->tuples,
											   false);
362 363

#if 0
364 365 366
		/* add in expensive functions cost!  -- JMH, 7/7/92 */
		if (XfuncMode != XFUNC_OFF)
		{
367
			pathnode->path_cost += xfunc_get_path_cost((Path *) pathnode);
368
		}
369 370
#endif

371 372 373 374 375 376 377 378 379
		/*
		 * Set selectivities of clauses used with index to the selectivity
		 * of this index, subdividing the selectivity equally over each of
		 * the clauses.
		 */

		/* XXX Can this divide the selectivities in a better way? */
		set_clause_selectivities(restriction_clauses, clausesel);
	}
380
	return pathnode;
381 382
}

383
/*
384
 * create_nestloop_path--
385 386 387
 *	  Creates a pathnode corresponding to a nestloop join between two
 *	  relations.
 *
388 389 390 391 392
 * 'joinrel' is the join relation.
 * 'outer_rel' is the outer join relation
 * 'outer_path' is the outer join path.
 * 'inner_path' is the inner join path.
 * 'keys' are the keys of the path
393
 *
394
 * Returns the resulting path node.
395
 *
396
 */
397
JoinPath   *
398 399
create_nestloop_path(RelOptInfo * joinrel,
					 RelOptInfo * outer_rel,
400 401 402
					 Path *outer_path,
					 Path *inner_path,
					 List *keys)
403
{
404
	JoinPath   *pathnode = makeNode(JoinPath);
405 406 407 408 409

	pathnode->path.pathtype = T_NestLoop;
	pathnode->path.parent = joinrel;
	pathnode->outerjoinpath = outer_path;
	pathnode->innerjoinpath = inner_path;
410
	pathnode->pathinfo = joinrel->restrictinfo;
411 412 413
	pathnode->path.keys = keys;
	pathnode->path.joinid = NIL;
	pathnode->path.outerjoincost = (Cost) 0.0;
414
	pathnode->path.loc_restrictinfo = NIL;
415 416 417

	if (keys)
	{
B
Bruce Momjian 已提交
418 419
		pathnode->path.path_order.ordtype = outer_path->path_order.ordtype;
		if (outer_path->path_order.ordtype == SORTOP_ORDER)
420
		{
B
Bruce Momjian 已提交
421
			pathnode->path.path_order.ord.sortop = outer_path->path_order.ord.sortop;
422 423 424
		}
		else
		{
B
Bruce Momjian 已提交
425
			pathnode->path.path_order.ord.merge = outer_path->path_order.ord.merge;
426
		}
427
	}
428 429
	else
	{
B
Bruce Momjian 已提交
430 431
		pathnode->path.path_order.ordtype = SORTOP_ORDER;
		pathnode->path.path_order.ord.sortop = NULL;
432 433
	}

434
	pathnode->path.path_cost = cost_nestloop(outer_path->path_cost,
435 436 437 438 439 440 441
					  inner_path->path_cost,
					  outer_rel->size,
					  inner_path->parent->size,
					  page_size(outer_rel->size,
								outer_rel->width),
					  IsA(inner_path, IndexPath));
	/* add in expensive function costs -- JMH 7/7/92 */
442
#if 0
443 444
	if (XfuncMode != XFUNC_OFF)
		pathnode->path_cost += xfunc_get_path_cost((Path *) pathnode);
445
#endif
446
	return pathnode;
447 448
}

449
/*
450 451
 * create_mergejoin_path--
 *	  Creates a pathnode corresponding to a mergejoin join between
452 453
 *	  two relations
 *
454 455 456 457 458 459 460 461 462 463 464 465
 * 'joinrel' is the join relation
 * 'outersize' is the number of tuples in the outer relation
 * 'innersize' is the number of tuples in the inner relation
 * 'outerwidth' is the number of bytes per tuple in the outer relation
 * 'innerwidth' is the number of bytes per tuple in the inner relation
 * 'outer_path' is the outer path
 * 'inner_path' is the inner path
 * 'keys' are the new keys of the join relation
 * 'order' is the sort order required for the merge
 * 'mergeclauses' are the applicable join/restriction clauses
 * 'outersortkeys' are the sort varkeys for the outer relation
 * 'innersortkeys' are the sort varkeys for the inner relation
466
 *
467
 */
468
MergePath  *
469
create_mergejoin_path(RelOptInfo * joinrel,
470 471 472 473
					  int outersize,
					  int innersize,
					  int outerwidth,
					  int innerwidth,
474 475 476 477 478 479 480
					  Path *outer_path,
					  Path *inner_path,
					  List *keys,
					  MergeOrder *order,
					  List *mergeclauses,
					  List *outersortkeys,
					  List *innersortkeys)
481
{
482
	MergePath  *pathnode = makeNode(MergePath);
483 484 485 486 487

	pathnode->jpath.path.pathtype = T_MergeJoin;
	pathnode->jpath.path.parent = joinrel;
	pathnode->jpath.outerjoinpath = outer_path;
	pathnode->jpath.innerjoinpath = inner_path;
488
	pathnode->jpath.pathinfo = joinrel->restrictinfo;
489
	pathnode->jpath.path.keys = keys;
B
Bruce Momjian 已提交
490 491
	pathnode->jpath.path.path_order.ordtype = MERGE_ORDER;
	pathnode->jpath.path.path_order.ord.merge = order;
492
	pathnode->path_mergeclauses = mergeclauses;
493
	pathnode->jpath.path.loc_restrictinfo = NIL;
494 495
	pathnode->outersortkeys = outersortkeys;
	pathnode->innersortkeys = innersortkeys;
496
	pathnode->jpath.path.path_cost = cost_mergejoin(outer_path->path_cost,
497 498 499 500 501 502 503 504
					   inner_path->path_cost,
					   outersortkeys,
					   innersortkeys,
					   outersize,
					   innersize,
					   outerwidth,
					   innerwidth);
	/* add in expensive function costs -- JMH 7/7/92 */
505
#if 0
506 507
	if (XfuncMode != XFUNC_OFF)
	{
508
		pathnode->path_cost += xfunc_get_path_cost((Path *) pathnode);
509
	}
510
#endif
511
	return pathnode;
512 513
}

514 515 516 517
/*
 * create_hashjoin_path--						XXX HASH
 *	  Creates a pathnode corresponding to a hash join between two relations.
 *
518 519 520 521 522 523 524 525 526 527 528 529
 * 'joinrel' is the join relation
 * 'outersize' is the number of tuples in the outer relation
 * 'innersize' is the number of tuples in the inner relation
 * 'outerwidth' is the number of bytes per tuple in the outer relation
 * 'innerwidth' is the number of bytes per tuple in the inner relation
 * 'outer_path' is the outer path
 * 'inner_path' is the inner path
 * 'keys' are the new keys of the join relation
 * 'operator' is the hashjoin operator
 * 'hashclauses' are the applicable join/restriction clauses
 * 'outerkeys' are the sort varkeys for the outer relation
 * 'innerkeys' are the sort varkeys for the inner relation
530
 *
531
 */
532
HashPath   *
533
create_hashjoin_path(RelOptInfo * joinrel,
534 535 536 537
					 int outersize,
					 int innersize,
					 int outerwidth,
					 int innerwidth,
538 539 540
					 Path *outer_path,
					 Path *inner_path,
					 List *keys,
541
					 Oid operator,
542 543 544
					 List *hashclauses,
					 List *outerkeys,
					 List *innerkeys)
545
{
546
	HashPath   *pathnode = makeNode(HashPath);
547 548 549 550 551

	pathnode->jpath.path.pathtype = T_HashJoin;
	pathnode->jpath.path.parent = joinrel;
	pathnode->jpath.outerjoinpath = outer_path;
	pathnode->jpath.innerjoinpath = inner_path;
552 553
	pathnode->jpath.pathinfo = joinrel->restrictinfo;
	pathnode->jpath.path.loc_restrictinfo = NIL;
554
	pathnode->jpath.path.keys = keys;
B
Bruce Momjian 已提交
555 556
	pathnode->jpath.path.path_order.ordtype = SORTOP_ORDER;
	pathnode->jpath.path.path_order.ord.sortop = NULL;
557 558 559 560 561 562
	pathnode->jpath.path.outerjoincost = (Cost) 0.0;
	pathnode->jpath.path.joinid = (Relid) NULL;
	/* pathnode->hashjoinoperator = operator;  */
	pathnode->path_hashclauses = hashclauses;
	pathnode->outerhashkeys = outerkeys;
	pathnode->innerhashkeys = innerkeys;
563
	pathnode->jpath.path.path_cost = cost_hashjoin(outer_path->path_cost,
564 565 566 567 568 569
					  inner_path->path_cost,
					  outerkeys,
					  innerkeys,
					  outersize, innersize,
					  outerwidth, innerwidth);
	/* add in expensive function costs -- JMH 7/7/92 */
570
#if 0
571 572
	if (XfuncMode != XFUNC_OFF)
	{
573
		pathnode->path_cost += xfunc_get_path_cost((Path *) pathnode);
574
	}
575
#endif
576
	return pathnode;
577
}