1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* pathnode.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Routines to manipulate pathlists and create path nodes
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1999-08-16 04:17:58 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/pathnode.c,v 1.54 1999/08/16 02:17:58 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
|
1999-07-16 07:00:38 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "optimizer/cost.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "optimizer/pathnode.h"
|
1999-07-27 05:51:11 +02:00
|
|
|
#include "optimizer/paths.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "optimizer/plancat.h"
|
|
|
|
#include "optimizer/restrictinfo.h"
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "parser/parsetree.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
* MISC. PATH UTILITIES
|
1996-07-09 08:22:35 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* path_is_cheaper
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns t iff 'path1' is cheaper than 'path2'.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
bool
|
1997-09-08 23:56:23 +02:00
|
|
|
path_is_cheaper(Path *path1, Path *path2)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Cost cost1 = path1->path_cost;
|
|
|
|
Cost cost2 = path2->path_cost;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return (bool) (cost1 < cost2);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* set_cheapest
|
1997-09-07 07:04:48 +02:00
|
|
|
* Finds the minimum cost path from among a relation's paths.
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* 'parent_rel' is the parent relation
|
|
|
|
* 'pathlist' is a list of path nodes corresponding to 'parent_rel'
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
|
|
|
* Returns and sets the relation entry field with the pathnode that
|
1996-07-09 08:22:35 +02:00
|
|
|
* is minimum.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
Path *
|
1999-05-26 00:43:53 +02:00
|
|
|
set_cheapest(RelOptInfo *parent_rel, List *pathlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *p;
|
|
|
|
Path *cheapest_so_far;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(pathlist != NIL);
|
1998-07-18 06:22:52 +02:00
|
|
|
Assert(IsA(parent_rel, RelOptInfo));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
cheapest_so_far = (Path *) lfirst(pathlist);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
foreach(p, lnext(pathlist))
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Path *path = (Path *) lfirst(p);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (path_is_cheaper(path, cheapest_so_far))
|
|
|
|
cheapest_so_far = path;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
parent_rel->cheapestpath = cheapest_so_far;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return cheapest_so_far;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* add_pathlist
|
1999-08-16 04:17:58 +02:00
|
|
|
* Construct an output path list by adding to old_paths each path in
|
|
|
|
* new_paths that is worth considering --- that is, it has either a
|
|
|
|
* better sort order (better pathkeys) or cheaper cost than any of the
|
|
|
|
* existing old paths.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1999-08-16 04:17:58 +02:00
|
|
|
* Unless parent_rel->pruneable is false, we also remove from the output
|
|
|
|
* pathlist any old paths that are dominated by added path(s) --- that is,
|
|
|
|
* some new path is both cheaper and at least as well ordered.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1999-08-16 04:17:58 +02:00
|
|
|
* Note: the list old_paths is destructively modified, and in fact is
|
|
|
|
* turned into the output list.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1999-08-16 04:17:58 +02:00
|
|
|
* 'parent_rel' is the relation entry to which these paths correspond.
|
|
|
|
* 'old_paths' is the list of previously accepted paths for parent_rel.
|
|
|
|
* 'new_paths' is a list of potential new paths.
|
|
|
|
*
|
|
|
|
* Returns the updated list of interesting pathnodes.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
List *
|
1999-08-16 04:17:58 +02:00
|
|
|
add_pathlist(RelOptInfo *parent_rel, List *old_paths, List *new_paths)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-02-04 20:20:12 +01:00
|
|
|
List *p1;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-04 20:20:12 +01:00
|
|
|
foreach(p1, new_paths)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-02-04 20:20:12 +01:00
|
|
|
Path *new_path = (Path *) lfirst(p1);
|
1999-08-16 04:17:58 +02:00
|
|
|
bool accept_new = true; /* unless we find a superior old path */
|
|
|
|
List *p2_prev = NIL;
|
|
|
|
List *p2;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
/*
|
|
|
|
* Loop to check proposed new path against old paths. Note it is
|
|
|
|
* possible for more than one old path to be tossed out because
|
|
|
|
* new_path dominates it.
|
|
|
|
*/
|
|
|
|
foreach(p2, old_paths)
|
1999-02-09 04:51:42 +01:00
|
|
|
{
|
1999-08-16 04:17:58 +02:00
|
|
|
Path *old_path = (Path *) lfirst(p2);
|
|
|
|
bool remove_old = false; /* unless new proves superior */
|
1999-02-11 05:08:44 +01:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
switch (compare_pathkeys(new_path->pathkeys, old_path->pathkeys))
|
|
|
|
{
|
|
|
|
case PATHKEYS_EQUAL:
|
|
|
|
if (new_path->path_cost < old_path->path_cost)
|
|
|
|
remove_old = true; /* new dominates old */
|
|
|
|
else
|
|
|
|
accept_new = false; /* old equals or dominates new */
|
|
|
|
break;
|
|
|
|
case PATHKEYS_BETTER1:
|
|
|
|
if (new_path->path_cost <= old_path->path_cost)
|
|
|
|
remove_old = true; /* new dominates old */
|
|
|
|
break;
|
|
|
|
case PATHKEYS_BETTER2:
|
|
|
|
if (new_path->path_cost >= old_path->path_cost)
|
|
|
|
accept_new = false; /* old dominates new */
|
|
|
|
break;
|
|
|
|
case PATHKEYS_DIFFERENT:
|
|
|
|
/* keep both paths, since they have different ordering */
|
|
|
|
break;
|
|
|
|
}
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-02-11 22:05:28 +01:00
|
|
|
/*
|
1999-08-16 04:17:58 +02:00
|
|
|
* Remove current element from old_list if dominated by new,
|
|
|
|
* unless xfunc told us not to remove any paths.
|
1999-02-11 22:05:28 +01:00
|
|
|
*/
|
1999-08-16 04:17:58 +02:00
|
|
|
if (remove_old && parent_rel->pruneable)
|
1999-02-11 05:08:44 +01:00
|
|
|
{
|
1999-08-16 04:17:58 +02:00
|
|
|
if (p2_prev)
|
|
|
|
lnext(p2_prev) = lnext(p2);
|
|
|
|
else
|
|
|
|
old_paths = lnext(p2);
|
1999-02-11 22:05:28 +01:00
|
|
|
}
|
1999-08-16 04:17:58 +02:00
|
|
|
else
|
|
|
|
p2_prev = p2;
|
1999-02-11 22:05:28 +01:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
/*
|
|
|
|
* If we found an old path that dominates new_path, we can quit
|
|
|
|
* scanning old_paths; we will not add new_path, and we assume
|
|
|
|
* new_path cannot dominate any other elements of old_paths.
|
|
|
|
*/
|
|
|
|
if (! accept_new)
|
|
|
|
break;
|
|
|
|
}
|
1999-02-11 22:05:28 +01:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
if (accept_new)
|
|
|
|
{
|
|
|
|
/* Accept the path. Note that it will now be eligible to be
|
|
|
|
* compared against the additional elements of new_paths...
|
|
|
|
*/
|
|
|
|
new_path->parent = parent_rel; /* not redundant, see prune.c */
|
|
|
|
old_paths = lcons(new_path, old_paths);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
return old_paths;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
* PATH NODE CREATION ROUTINES
|
1996-07-09 08:22:35 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* create_seqscan_path
|
1997-09-07 07:04:48 +02:00
|
|
|
* Creates a path corresponding to a sequential scan, returning the
|
|
|
|
* pathnode.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
Path *
|
1999-05-26 00:43:53 +02:00
|
|
|
create_seqscan_path(RelOptInfo *rel)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Path *pathnode = makeNode(Path);
|
1999-08-16 04:17:58 +02:00
|
|
|
int relid = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
pathnode->pathtype = T_SeqScan;
|
|
|
|
pathnode->parent = rel;
|
|
|
|
pathnode->path_cost = 0.0;
|
1999-08-16 04:17:58 +02:00
|
|
|
pathnode->pathkeys = NIL; /* seqscan has unordered result */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
if (rel->relids != NIL) /* can this happen? */
|
1997-09-07 07:04:48 +02:00
|
|
|
relid = lfirsti(rel->relids);
|
|
|
|
|
|
|
|
pathnode->path_cost = cost_seqscan(relid,
|
|
|
|
rel->pages, rel->tuples);
|
1999-07-27 05:51:11 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return pathnode;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* create_index_path
|
1999-07-30 06:07:25 +02:00
|
|
|
* Creates a path node for an index scan.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'rel' is the parent rel
|
1999-07-30 06:07:25 +02:00
|
|
|
* 'index' is an index on 'rel'
|
|
|
|
* 'restriction_clauses' is a list of RestrictInfo nodes
|
|
|
|
* to be used as index qual conditions in the scan.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns the new path node.
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
IndexPath *
|
1997-09-08 23:56:23 +02:00
|
|
|
create_index_path(Query *root,
|
1999-05-26 00:43:53 +02:00
|
|
|
RelOptInfo *rel,
|
|
|
|
RelOptInfo *index,
|
1999-07-30 06:07:25 +02:00
|
|
|
List *restriction_clauses)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
IndexPath *pathnode = makeNode(IndexPath);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
pathnode->path.pathtype = T_IndexScan;
|
|
|
|
pathnode->path.parent = rel;
|
1999-08-16 04:17:58 +02:00
|
|
|
pathnode->path.pathkeys = build_index_pathkeys(root, rel, index);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
/*
|
|
|
|
* Note that we are making a pathnode for a single-scan indexscan;
|
1999-07-26 01:07:26 +02:00
|
|
|
* therefore, both indexid and indexqual should be single-element
|
1999-07-30 02:56:17 +02:00
|
|
|
* lists. We initialize indexqual to contain one empty sublist,
|
|
|
|
* representing a single index traversal with no index restriction
|
|
|
|
* conditions. If we do have restriction conditions to use, they
|
|
|
|
* will get inserted below.
|
1999-07-26 01:07:26 +02:00
|
|
|
*/
|
1999-07-30 02:56:17 +02:00
|
|
|
Assert(length(index->relids) == 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
pathnode->indexid = index->relids;
|
1999-07-30 02:56:17 +02:00
|
|
|
pathnode->indexqual = lcons(NIL, NIL);
|
1999-08-16 04:17:58 +02:00
|
|
|
pathnode->joinrelids = NIL; /* no join clauses here */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-07-30 06:07:25 +02:00
|
|
|
if (restriction_clauses == NIL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
/*
|
1999-07-30 06:07:25 +02:00
|
|
|
* We have no restriction clauses, so compute scan cost using
|
|
|
|
* selectivity of 1.0.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-02-03 22:18:02 +01:00
|
|
|
pathnode->path.path_cost = cost_index(lfirsti(index->relids),
|
1999-05-25 18:15:34 +02:00
|
|
|
index->pages,
|
|
|
|
1.0,
|
|
|
|
rel->pages,
|
|
|
|
rel->tuples,
|
|
|
|
index->pages,
|
|
|
|
index->tuples,
|
|
|
|
false);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
1999-07-30 02:56:17 +02:00
|
|
|
* Compute scan cost for the case when 'index' is used with
|
1999-08-16 04:17:58 +02:00
|
|
|
* restriction clause(s). Also, place indexqual in path node.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-07-26 01:07:26 +02:00
|
|
|
List *indexquals;
|
1997-09-08 04:41:22 +02:00
|
|
|
float npages;
|
|
|
|
float selec;
|
|
|
|
Cost clausesel;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-07-26 01:07:26 +02:00
|
|
|
indexquals = get_actual_clauses(restriction_clauses);
|
1999-07-27 05:51:11 +02:00
|
|
|
/* expand special operators to indexquals the executor can handle */
|
|
|
|
indexquals = expand_indexqual_conditions(indexquals);
|
1999-07-26 01:07:26 +02:00
|
|
|
|
1999-07-30 02:56:17 +02:00
|
|
|
/* Insert qual list into 1st sublist of pathnode->indexqual;
|
|
|
|
* we already made the cons cell above, no point in wasting it...
|
|
|
|
*/
|
|
|
|
lfirst(pathnode->indexqual) = indexquals;
|
|
|
|
|
1999-07-26 01:07:26 +02:00
|
|
|
index_selectivity(root,
|
|
|
|
lfirsti(rel->relids),
|
|
|
|
lfirsti(index->relids),
|
|
|
|
indexquals,
|
1997-09-07 07:04:48 +02:00
|
|
|
&npages,
|
|
|
|
&selec);
|
|
|
|
|
1998-09-21 17:41:28 +02:00
|
|
|
pathnode->path.path_cost = cost_index(lfirsti(index->relids),
|
1999-05-25 18:15:34 +02:00
|
|
|
(int) npages,
|
|
|
|
selec,
|
|
|
|
rel->pages,
|
|
|
|
rel->tuples,
|
|
|
|
index->pages,
|
|
|
|
index->tuples,
|
|
|
|
false);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
|
|
|
* Set selectivities of clauses used with index to the selectivity
|
|
|
|
* of this index, subdividing the selectivity equally over each of
|
1999-07-31 00:34:19 +02:00
|
|
|
* the clauses. To the extent that index_selectivity() can make a
|
|
|
|
* better estimate of the joint selectivity of these clauses than
|
|
|
|
* the product of individual estimates from compute_clause_selec()
|
|
|
|
* would be, this should give us a more accurate estimate of the
|
|
|
|
* total selectivity of all the clauses.
|
1999-07-27 05:51:11 +02:00
|
|
|
*
|
1999-07-31 00:34:19 +02:00
|
|
|
* XXX If there is more than one useful index for this rel, and the
|
|
|
|
* indexes can be used with different but overlapping groups of
|
|
|
|
* restriction clauses, we may end up with too optimistic an estimate,
|
|
|
|
* since set_clause_selectivities() will save the minimum of the
|
|
|
|
* per-clause selectivity estimated with each index. But that should
|
|
|
|
* be fairly unlikely for typical index usage.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-07-26 01:07:26 +02:00
|
|
|
clausesel = pow(selec, 1.0 / (double) length(restriction_clauses));
|
1997-09-07 07:04:48 +02:00
|
|
|
set_clause_selectivities(restriction_clauses, clausesel);
|
|
|
|
}
|
1999-07-26 01:07:26 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return pathnode;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* create_nestloop_path
|
1997-09-07 07:04:48 +02:00
|
|
|
* Creates a pathnode corresponding to a nestloop join between two
|
|
|
|
* relations.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'joinrel' is the join relation.
|
|
|
|
* 'outer_rel' is the outer join relation
|
1999-08-16 04:17:58 +02:00
|
|
|
* 'outer_path' is the outer path
|
|
|
|
* 'inner_path' is the inner path
|
|
|
|
* 'pathkeys' are the path keys of the new join path
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns the resulting path node.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1999-02-12 07:43:53 +01:00
|
|
|
NestPath *
|
1999-05-26 00:43:53 +02:00
|
|
|
create_nestloop_path(RelOptInfo *joinrel,
|
|
|
|
RelOptInfo *outer_rel,
|
1997-09-08 23:56:23 +02:00
|
|
|
Path *outer_path,
|
|
|
|
Path *inner_path,
|
1999-02-10 04:52:54 +01:00
|
|
|
List *pathkeys)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-02-12 07:43:53 +01:00
|
|
|
NestPath *pathnode = makeNode(NestPath);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
pathnode->path.pathtype = T_NestLoop;
|
|
|
|
pathnode->path.parent = joinrel;
|
|
|
|
pathnode->outerjoinpath = outer_path;
|
|
|
|
pathnode->innerjoinpath = inner_path;
|
1999-02-03 21:15:53 +01:00
|
|
|
pathnode->pathinfo = joinrel->restrictinfo;
|
1999-02-10 04:52:54 +01:00
|
|
|
pathnode->path.pathkeys = pathkeys;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-02-03 22:18:02 +01:00
|
|
|
pathnode->path.path_cost = cost_nestloop(outer_path->path_cost,
|
1999-05-25 18:15:34 +02:00
|
|
|
inner_path->path_cost,
|
|
|
|
outer_rel->size,
|
|
|
|
inner_path->parent->size,
|
|
|
|
page_size(outer_rel->size,
|
|
|
|
outer_rel->width),
|
|
|
|
IsA(inner_path, IndexPath));
|
1999-07-27 05:51:11 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return pathnode;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* create_mergejoin_path
|
1998-08-04 18:44:31 +02:00
|
|
|
* Creates a pathnode corresponding to a mergejoin join between
|
1997-09-07 07:04:48 +02:00
|
|
|
* two relations
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* '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
|
1999-08-16 04:17:58 +02:00
|
|
|
* 'pathkeys' are the path keys of the new join path
|
1996-07-09 08:22:35 +02:00
|
|
|
* '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
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
MergePath *
|
1999-05-26 00:43:53 +02:00
|
|
|
create_mergejoin_path(RelOptInfo *joinrel,
|
1997-09-07 07:04:48 +02:00
|
|
|
int outersize,
|
|
|
|
int innersize,
|
|
|
|
int outerwidth,
|
|
|
|
int innerwidth,
|
1997-09-08 23:56:23 +02:00
|
|
|
Path *outer_path,
|
|
|
|
Path *inner_path,
|
1999-02-10 04:52:54 +01:00
|
|
|
List *pathkeys,
|
1997-09-08 23:56:23 +02:00
|
|
|
List *mergeclauses,
|
|
|
|
List *outersortkeys,
|
|
|
|
List *innersortkeys)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
MergePath *pathnode = makeNode(MergePath);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-08-16 04:17:58 +02:00
|
|
|
/*
|
|
|
|
* If the given paths are already well enough ordered, we can skip
|
|
|
|
* doing an explicit sort.
|
|
|
|
*/
|
|
|
|
if (outersortkeys &&
|
|
|
|
pathkeys_contained_in(outersortkeys, outer_path->pathkeys))
|
|
|
|
outersortkeys = NIL;
|
|
|
|
if (innersortkeys &&
|
|
|
|
pathkeys_contained_in(innersortkeys, inner_path->pathkeys))
|
|
|
|
innersortkeys = NIL;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
pathnode->jpath.path.pathtype = T_MergeJoin;
|
|
|
|
pathnode->jpath.path.parent = joinrel;
|
|
|
|
pathnode->jpath.outerjoinpath = outer_path;
|
|
|
|
pathnode->jpath.innerjoinpath = inner_path;
|
1999-02-03 21:15:53 +01:00
|
|
|
pathnode->jpath.pathinfo = joinrel->restrictinfo;
|
1999-02-10 04:52:54 +01:00
|
|
|
pathnode->jpath.path.pathkeys = pathkeys;
|
1997-09-07 07:04:48 +02:00
|
|
|
pathnode->path_mergeclauses = mergeclauses;
|
|
|
|
pathnode->outersortkeys = outersortkeys;
|
|
|
|
pathnode->innersortkeys = innersortkeys;
|
1999-02-03 22:18:02 +01:00
|
|
|
pathnode->jpath.path.path_cost = cost_mergejoin(outer_path->path_cost,
|
1999-05-25 18:15:34 +02:00
|
|
|
inner_path->path_cost,
|
|
|
|
outersortkeys,
|
|
|
|
innersortkeys,
|
|
|
|
outersize,
|
|
|
|
innersize,
|
|
|
|
outerwidth,
|
|
|
|
innerwidth);
|
1999-07-27 05:51:11 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return pathnode;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-08-06 06:00:17 +02:00
|
|
|
* create_hashjoin_path
|
1997-09-07 07:04:48 +02:00
|
|
|
* Creates a pathnode corresponding to a hash join between two relations.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* '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
|
1999-08-06 06:00:17 +02:00
|
|
|
* 'outer_path' is the cheapest outer path
|
|
|
|
* 'inner_path' is the cheapest inner path
|
|
|
|
* 'hashclauses' is a list of the hash join clause (always a 1-element list)
|
|
|
|
* 'innerdisbursion' is an estimate of the disbursion of the inner hash key
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1999-08-16 04:17:58 +02:00
|
|
|
HashPath *
|
1999-05-26 00:43:53 +02:00
|
|
|
create_hashjoin_path(RelOptInfo *joinrel,
|
1997-09-07 07:04:48 +02:00
|
|
|
int outersize,
|
|
|
|
int innersize,
|
|
|
|
int outerwidth,
|
|
|
|
int innerwidth,
|
1997-09-08 23:56:23 +02:00
|
|
|
Path *outer_path,
|
|
|
|
Path *inner_path,
|
|
|
|
List *hashclauses,
|
1999-08-06 06:00:17 +02:00
|
|
|
Cost innerdisbursion)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
HashPath *pathnode = makeNode(HashPath);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
pathnode->jpath.path.pathtype = T_HashJoin;
|
|
|
|
pathnode->jpath.path.parent = joinrel;
|
|
|
|
pathnode->jpath.outerjoinpath = outer_path;
|
|
|
|
pathnode->jpath.innerjoinpath = inner_path;
|
1999-02-03 21:15:53 +01:00
|
|
|
pathnode->jpath.pathinfo = joinrel->restrictinfo;
|
1999-08-16 04:17:58 +02:00
|
|
|
/* A hashjoin never has pathkeys, since its ordering is unpredictable */
|
|
|
|
pathnode->jpath.path.pathkeys = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
pathnode->path_hashclauses = hashclauses;
|
1999-02-03 22:18:02 +01:00
|
|
|
pathnode->jpath.path.path_cost = cost_hashjoin(outer_path->path_cost,
|
1999-05-25 18:15:34 +02:00
|
|
|
inner_path->path_cost,
|
|
|
|
outersize, innersize,
|
1999-08-06 06:00:17 +02:00
|
|
|
outerwidth, innerwidth,
|
|
|
|
innerdisbursion);
|
1999-07-27 05:51:11 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return pathnode;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|