1996-07-09 06:22:35 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-13 23:22:53 +00:00
|
|
|
* allpaths.c
|
1997-09-07 05:04:48 +00:00
|
|
|
* Routines to find possible search paths for processing a query
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
2000-01-26 05:58:53 +00:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2000-02-07 04:41:04 +00:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.58 2000/02/07 04:40:59 tgl Exp $
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "optimizer/cost.h"
|
1997-02-19 12:59:07 +00:00
|
|
|
#include "optimizer/geqo.h"
|
1999-07-16 05:00:38 +00:00
|
|
|
#include "optimizer/internal.h"
|
|
|
|
#include "optimizer/pathnode.h"
|
|
|
|
#include "optimizer/paths.h"
|
1997-02-19 12:59:07 +00:00
|
|
|
|
2000-02-07 04:41:04 +00:00
|
|
|
|
1997-04-29 04:37:22 +00:00
|
|
|
#ifdef GEQO
|
2000-01-22 23:50:30 +00:00
|
|
|
bool enable_geqo = true;
|
1997-04-29 04:37:22 +00:00
|
|
|
#else
|
2000-01-22 23:50:30 +00:00
|
|
|
bool enable_geqo = false;
|
1997-04-29 04:37:22 +00:00
|
|
|
#endif
|
2000-01-22 23:50:30 +00:00
|
|
|
|
|
|
|
int geqo_rels = GEQO_RELS;
|
1997-04-29 04:37:22 +00:00
|
|
|
|
|
|
|
|
2000-02-07 04:41:04 +00:00
|
|
|
static void set_base_rel_pathlist(Query *root);
|
|
|
|
static RelOptInfo *make_one_rel_by_joins(Query *root, int levels_needed);
|
1998-09-01 04:40:42 +00:00
|
|
|
|
1998-08-10 02:26:40 +00:00
|
|
|
#ifdef OPTIMIZER_DEBUG
|
1999-05-25 22:43:53 +00:00
|
|
|
static void debug_print_rel(Query *root, RelOptInfo *rel);
|
1998-08-10 02:26:40 +00:00
|
|
|
#endif
|
1996-07-09 06:22:35 +00:00
|
|
|
|
2000-02-07 04:41:04 +00:00
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
/*
|
1999-02-15 03:22:37 +00:00
|
|
|
* make_one_rel
|
|
|
|
* Finds all possible access paths for executing a query, returning a
|
2000-02-07 04:41:04 +00:00
|
|
|
* single rel that represents the join of all base rels in the query.
|
1996-07-09 06:22:35 +00:00
|
|
|
*/
|
1999-02-15 03:22:37 +00:00
|
|
|
RelOptInfo *
|
2000-02-07 04:41:04 +00:00
|
|
|
make_one_rel(Query *root)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1997-12-21 05:18:48 +00:00
|
|
|
int levels_needed;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
1996-07-09 06:22:35 +00:00
|
|
|
/*
|
1997-09-07 05:04:48 +00:00
|
|
|
* Set the number of join (not nesting) levels yet to be processed.
|
1996-07-09 06:22:35 +00:00
|
|
|
*/
|
2000-02-07 04:41:04 +00:00
|
|
|
levels_needed = length(root->base_rel_list);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
1997-12-21 05:18:48 +00:00
|
|
|
if (levels_needed <= 0)
|
1999-02-15 03:22:37 +00:00
|
|
|
return NULL;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
1999-07-30 22:34:19 +00:00
|
|
|
/*
|
|
|
|
* Generate access paths for the base rels.
|
|
|
|
*/
|
2000-02-07 04:41:04 +00:00
|
|
|
set_base_rel_pathlist(root);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2000-02-07 04:41:04 +00:00
|
|
|
if (levels_needed == 1)
|
1997-09-07 05:04:48 +00:00
|
|
|
{
|
|
|
|
/*
|
2000-01-09 00:26:47 +00:00
|
|
|
* Single relation, no more processing is required.
|
1997-09-07 05:04:48 +00:00
|
|
|
*/
|
2000-02-07 04:41:04 +00:00
|
|
|
return (RelOptInfo *) lfirst(root->base_rel_list);
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2000-01-09 00:26:47 +00:00
|
|
|
* Generate join tree.
|
1997-09-07 05:04:48 +00:00
|
|
|
*/
|
2000-02-07 04:41:04 +00:00
|
|
|
return make_one_rel_by_joins(root, levels_needed);
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
/*
|
1999-02-15 05:21:12 +00:00
|
|
|
* set_base_rel_pathlist
|
2000-02-07 04:41:04 +00:00
|
|
|
* Finds all paths available for scanning each base-relation entry.
|
|
|
|
* Sequential scan and any available indices are considered.
|
|
|
|
* Each useful path is attached to its relation's 'pathlist' field.
|
1996-07-09 06:22:35 +00:00
|
|
|
*/
|
|
|
|
static void
|
2000-02-07 04:41:04 +00:00
|
|
|
set_base_rel_pathlist(Query *root)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2000-02-07 04:41:04 +00:00
|
|
|
List *rellist;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2000-02-07 04:41:04 +00:00
|
|
|
foreach(rellist, root->base_rel_list)
|
1997-09-07 05:04:48 +00:00
|
|
|
{
|
2000-02-07 04:41:04 +00:00
|
|
|
RelOptInfo *rel = (RelOptInfo *) lfirst(rellist);
|
1999-07-24 23:21:14 +00:00
|
|
|
List *indices = find_relation_indices(root, rel);
|
2000-02-07 04:41:04 +00:00
|
|
|
|
|
|
|
/* Mark rel with estimated output rows, width, etc */
|
|
|
|
set_baserel_size_estimates(root, rel);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate paths and add them to the rel's pathlist.
|
|
|
|
*
|
|
|
|
* add_path/add_pathlist will discard any paths that are dominated
|
|
|
|
* by another available path, keeping only those paths that are
|
|
|
|
* superior along at least one dimension of cost or sortedness.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Consider sequential scan */
|
|
|
|
add_path(rel, create_seqscan_path(rel));
|
|
|
|
|
|
|
|
/* Consider TID scans */
|
|
|
|
add_pathlist(rel, create_tidscan_paths(root, rel));
|
|
|
|
|
|
|
|
/* Consider index paths for both simple and OR index clauses */
|
|
|
|
add_pathlist(rel, create_index_paths(root,
|
|
|
|
rel,
|
|
|
|
indices,
|
|
|
|
rel->baserestrictinfo,
|
|
|
|
rel->joininfo));
|
1997-09-07 05:04:48 +00:00
|
|
|
|
1999-07-24 23:21:14 +00:00
|
|
|
/* Note: create_or_index_paths depends on create_index_paths
|
|
|
|
* to have marked OR restriction clauses with relevant indices;
|
|
|
|
* this is why it doesn't need to be given the full list of indices.
|
|
|
|
*/
|
2000-02-07 04:41:04 +00:00
|
|
|
add_pathlist(rel, create_or_index_paths(root, rel,
|
|
|
|
rel->baserestrictinfo));
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2000-02-07 04:41:04 +00:00
|
|
|
/* Now find the cheapest of the paths for this rel */
|
1999-02-12 05:57:08 +00:00
|
|
|
set_cheapest(rel, rel->pathlist);
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
/*
|
1999-02-15 03:22:37 +00:00
|
|
|
* make_one_rel_by_joins
|
1997-09-07 05:04:48 +00:00
|
|
|
* Find all possible joinpaths for a query by successively finding ways
|
2000-02-07 04:41:04 +00:00
|
|
|
* to join component relations into join relations.
|
1997-09-07 05:04:48 +00:00
|
|
|
*
|
2000-02-07 04:41:04 +00:00
|
|
|
* 'levels_needed' is the number of iterations needed, ie, the number of
|
|
|
|
* base relations present in the query
|
1997-09-07 05:04:48 +00:00
|
|
|
*
|
1996-07-09 06:22:35 +00:00
|
|
|
* Returns the final level of join relations, i.e., the relation that is
|
1997-12-21 05:18:48 +00:00
|
|
|
* the result of joining all the original relations together.
|
1996-07-09 06:22:35 +00:00
|
|
|
*/
|
1999-02-15 03:22:37 +00:00
|
|
|
static RelOptInfo *
|
2000-02-07 04:41:04 +00:00
|
|
|
make_one_rel_by_joins(Query *root, int levels_needed)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
2000-02-07 04:41:04 +00:00
|
|
|
int lev;
|
1998-08-07 05:02:32 +00:00
|
|
|
RelOptInfo *rel;
|
1996-07-09 06:22:35 +00:00
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
/*******************************************
|
|
|
|
* genetic query optimizer entry point *
|
|
|
|
* <utesch@aut.tu-freiberg.de> *
|
2000-02-07 04:41:04 +00:00
|
|
|
* rest will be skipped in case of GEQO *
|
1997-09-07 05:04:48 +00:00
|
|
|
*******************************************/
|
2000-02-07 04:41:04 +00:00
|
|
|
if (enable_geqo && levels_needed >= geqo_rels)
|
1999-02-15 03:22:37 +00:00
|
|
|
return geqo(root);
|
1999-05-25 16:15:34 +00:00
|
|
|
|
2000-02-07 04:41:04 +00:00
|
|
|
/*
|
|
|
|
* We employ a simple "dynamic programming" algorithm: we first
|
|
|
|
* find all ways to build joins of two base relations, then all ways
|
|
|
|
* to build joins of three base relations (from two-base-rel joins
|
|
|
|
* and other base rels), then four-base-rel joins, and so on until
|
|
|
|
* we have considered all ways to join all N relations into one rel.
|
|
|
|
*/
|
1996-07-09 06:22:35 +00:00
|
|
|
|
2000-02-07 04:41:04 +00:00
|
|
|
for (lev = 2; lev <= levels_needed; lev++)
|
1998-02-26 04:46:47 +00:00
|
|
|
{
|
2000-02-07 04:41:04 +00:00
|
|
|
List *first_old_rel = root->join_rel_list;
|
|
|
|
List *x;
|
1999-05-25 16:15:34 +00:00
|
|
|
|
1997-12-21 05:18:48 +00:00
|
|
|
/*
|
|
|
|
* Determine all possible pairs of relations to be joined at this
|
2000-02-07 04:41:04 +00:00
|
|
|
* level, and build paths for making each one from every available
|
|
|
|
* pair of lower-level relations. Results are prepended to
|
|
|
|
* root->join_rel_list.
|
1997-12-21 05:18:48 +00:00
|
|
|
*/
|
2000-02-07 04:41:04 +00:00
|
|
|
make_rels_by_joins(root, lev);
|
1998-02-26 04:46:47 +00:00
|
|
|
|
2000-02-07 04:41:04 +00:00
|
|
|
/*
|
|
|
|
* The relations created at the current level will appear at the
|
|
|
|
* front of root->join_rel_list.
|
|
|
|
*/
|
|
|
|
foreach(x, root->join_rel_list)
|
|
|
|
{
|
|
|
|
if (x == first_old_rel)
|
|
|
|
break; /* no more rels added at this level */
|
1998-02-26 04:46:47 +00:00
|
|
|
|
2000-02-07 04:41:04 +00:00
|
|
|
rel = (RelOptInfo *) lfirst(x);
|
1999-02-18 00:49:48 +00:00
|
|
|
|
1999-02-21 03:49:55 +00:00
|
|
|
#ifdef NOT_USED
|
2000-02-07 04:41:04 +00:00
|
|
|
/*
|
|
|
|
* * for each expensive predicate in each path in each distinct
|
|
|
|
* rel, * consider doing pullup -- JMH
|
|
|
|
*/
|
|
|
|
if (XfuncMode != XFUNC_NOPULL && XfuncMode != XFUNC_OFF)
|
|
|
|
xfunc_trypullup(rel);
|
1997-12-21 05:18:48 +00:00
|
|
|
#endif
|
1998-02-26 04:46:47 +00:00
|
|
|
|
2000-02-07 04:41:04 +00:00
|
|
|
/* Find and save the cheapest path for this rel */
|
|
|
|
set_cheapest(rel, rel->pathlist);
|
1999-02-14 04:57:02 +00:00
|
|
|
|
1996-07-09 06:22:35 +00:00
|
|
|
#ifdef OPTIMIZER_DEBUG
|
1997-12-21 05:18:48 +00:00
|
|
|
debug_print_rel(root, rel);
|
1997-09-07 05:04:48 +00:00
|
|
|
#endif
|
1997-12-21 05:18:48 +00:00
|
|
|
}
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
1997-12-21 05:18:48 +00:00
|
|
|
|
2000-02-07 04:41:04 +00:00
|
|
|
/*
|
|
|
|
* Now, the front of the join_rel_list should be the single rel
|
|
|
|
* representing the join of all the base rels.
|
|
|
|
*/
|
|
|
|
Assert(length(root->join_rel_list) > 0);
|
|
|
|
rel = (RelOptInfo *) lfirst(root->join_rel_list);
|
|
|
|
Assert(length(rel->relids) == levels_needed);
|
|
|
|
Assert(length(root->join_rel_list) == 1 ||
|
|
|
|
length(((RelOptInfo *) lsecond(root->join_rel_list))->relids) < levels_needed);
|
|
|
|
|
|
|
|
return rel;
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
1996-11-10 03:06:38 +00:00
|
|
|
#ifdef OPTIMIZER_DEBUG
|
2000-02-07 04:41:04 +00:00
|
|
|
|
1996-07-09 06:22:35 +00:00
|
|
|
static void
|
1997-09-08 21:56:23 +00:00
|
|
|
print_joinclauses(Query *root, List *clauses)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
List *l;
|
1997-09-08 21:56:23 +00:00
|
|
|
extern void print_expr(Node *expr, List *rtable); /* in print.c */
|
1996-07-09 06:22:35 +00:00
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
foreach(l, clauses)
|
|
|
|
{
|
1999-02-03 20:15:53 +00:00
|
|
|
RestrictInfo *c = lfirst(l);
|
1996-07-09 06:22:35 +00:00
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
print_expr((Node *) c->clause, root->rtable);
|
|
|
|
if (lnext(l))
|
|
|
|
printf(" ");
|
|
|
|
}
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
1996-11-10 03:06:38 +00:00
|
|
|
static void
|
1997-09-08 21:56:23 +00:00
|
|
|
print_path(Query *root, Path *path, int indent)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
char *ptype = NULL;
|
1999-02-12 17:25:05 +00:00
|
|
|
JoinPath *jp;
|
1997-09-08 02:41:22 +00:00
|
|
|
bool join = false;
|
|
|
|
int i;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
|
|
|
for (i = 0; i < indent; i++)
|
|
|
|
printf("\t");
|
|
|
|
|
|
|
|
switch (nodeTag(path))
|
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
case T_Path:
|
|
|
|
ptype = "SeqScan";
|
|
|
|
join = false;
|
|
|
|
break;
|
|
|
|
case T_IndexPath:
|
|
|
|
ptype = "IdxScan";
|
|
|
|
join = false;
|
|
|
|
break;
|
1999-02-12 06:43:53 +00:00
|
|
|
case T_NestPath:
|
1997-09-08 02:41:22 +00:00
|
|
|
ptype = "Nestloop";
|
|
|
|
join = true;
|
|
|
|
break;
|
|
|
|
case T_MergePath:
|
|
|
|
ptype = "MergeJoin";
|
|
|
|
join = true;
|
|
|
|
break;
|
|
|
|
case T_HashPath:
|
|
|
|
ptype = "HashJoin";
|
|
|
|
join = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
1997-09-07 05:04:48 +00:00
|
|
|
if (join)
|
|
|
|
{
|
1999-02-12 17:25:05 +00:00
|
|
|
jp = (JoinPath *) path;
|
2000-01-09 00:26:47 +00:00
|
|
|
printf("%s rows=%.0f cost=%f\n",
|
|
|
|
ptype, path->parent->rows, path->path_cost);
|
1997-09-07 05:04:48 +00:00
|
|
|
switch (nodeTag(path))
|
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
case T_MergePath:
|
|
|
|
case T_HashPath:
|
|
|
|
for (i = 0; i < indent + 1; i++)
|
|
|
|
printf("\t");
|
|
|
|
printf(" clauses=(");
|
2000-02-07 04:41:04 +00:00
|
|
|
print_joinclauses(root, jp->joinrestrictinfo);
|
1997-09-08 02:41:22 +00:00
|
|
|
printf(")\n");
|
|
|
|
|
|
|
|
if (nodeTag(path) == T_MergePath)
|
1997-09-07 05:04:48 +00:00
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
MergePath *mp = (MergePath *) path;
|
|
|
|
|
|
|
|
if (mp->outersortkeys || mp->innersortkeys)
|
|
|
|
{
|
|
|
|
for (i = 0; i < indent + 1; i++)
|
|
|
|
printf("\t");
|
|
|
|
printf(" sortouter=%d sortinner=%d\n",
|
|
|
|
((mp->outersortkeys) ? 1 : 0),
|
|
|
|
((mp->innersortkeys) ? 1 : 0));
|
|
|
|
}
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
1997-09-08 02:41:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
1997-09-07 05:04:48 +00:00
|
|
|
print_path(root, jp->outerjoinpath, indent + 1);
|
|
|
|
print_path(root, jp->innerjoinpath, indent + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
int relid = lfirsti(path->parent->relids);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2000-01-09 00:26:47 +00:00
|
|
|
printf("%s(%d) rows=%.0f cost=%f\n",
|
|
|
|
ptype, relid, path->parent->rows, path->path_cost);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
1999-08-16 02:17:58 +00:00
|
|
|
if (IsA(path, IndexPath))
|
1997-09-07 05:04:48 +00:00
|
|
|
{
|
1999-08-16 02:17:58 +00:00
|
|
|
printf(" pathkeys=");
|
|
|
|
print_pathkeys(path->pathkeys, root->rtable);
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
static void
|
1999-05-25 22:43:53 +00:00
|
|
|
debug_print_rel(Query *root, RelOptInfo *rel)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
List *l;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
|
|
|
printf("(");
|
|
|
|
foreach(l, rel->relids)
|
|
|
|
printf("%d ", lfirsti(l));
|
2000-01-09 00:26:47 +00:00
|
|
|
printf("): rows=%.0f width=%d\n", rel->rows, rel->width);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
|
|
|
printf("\tpath list:\n");
|
|
|
|
foreach(l, rel->pathlist)
|
|
|
|
print_path(root, lfirst(l), 1);
|
|
|
|
printf("\tcheapest path:\n");
|
|
|
|
print_path(root, rel->cheapestpath, 1);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
1997-09-07 05:04:48 +00:00
|
|
|
|
1998-09-01 04:40:42 +00:00
|
|
|
#endif /* OPTIMIZER_DEBUG */
|