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-01-26 05:58:53 +00:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.57 2000/01/26 05:56:34 momjian 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
|
|
|
|
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
|
|
|
|
|
|
|
|
1999-02-15 05:21:12 +00:00
|
|
|
static void set_base_rel_pathlist(Query *root, List *rels);
|
|
|
|
static RelOptInfo *make_one_rel_by_joins(Query *root, List *rels,
|
1999-05-25 16:15:34 +00:00
|
|
|
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-09-01 04:40:42 +00:00
|
|
|
|
1998-08-10 02:26:40 +00:00
|
|
|
#endif
|
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
|
|
|
|
* Finds all possible access paths for executing a query, returning a
|
|
|
|
* single rel.
|
1997-09-07 05:04:48 +00:00
|
|
|
*
|
1996-07-09 06:22:35 +00:00
|
|
|
* 'rels' is the list of single relation entries appearing in the query
|
|
|
|
*/
|
1999-02-15 03:22:37 +00:00
|
|
|
RelOptInfo *
|
|
|
|
make_one_rel(Query *root, List *rels)
|
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
|
|
|
*/
|
1997-12-21 05:18:48 +00:00
|
|
|
levels_needed = length(rels);
|
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.
|
|
|
|
*/
|
1999-02-15 05:21:12 +00:00
|
|
|
set_base_rel_pathlist(root, rels);
|
1997-09-07 05:04:48 +00:00
|
|
|
|
1997-12-21 05:18:48 +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
|
|
|
*/
|
1999-02-15 03:22:37 +00:00
|
|
|
return lfirst(rels);
|
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
|
|
|
*/
|
1999-02-15 03:22:37 +00:00
|
|
|
return make_one_rel_by_joins(root, rels, 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
|
1997-09-07 05:04:48 +00:00
|
|
|
* Finds all paths available for scanning each relation entry in
|
|
|
|
* 'rels'. Sequential scan and any available indices are considered
|
1999-07-24 23:21:14 +00:00
|
|
|
* if possible (indices are not considered for lower nesting levels).
|
|
|
|
* All useful paths are attached to the relation's 'pathlist' field.
|
1997-09-07 05:04:48 +00:00
|
|
|
*
|
|
|
|
* MODIFIES: rels
|
1996-07-09 06:22:35 +00:00
|
|
|
*/
|
|
|
|
static void
|
1999-02-15 05:21:12 +00:00
|
|
|
set_base_rel_pathlist(Query *root, List *rels)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
List *temp;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
|
|
|
foreach(temp, rels)
|
|
|
|
{
|
1999-07-24 23:21:14 +00:00
|
|
|
RelOptInfo *rel = (RelOptInfo *) lfirst(temp);
|
|
|
|
List *indices = find_relation_indices(root, rel);
|
1997-09-08 02:41:22 +00:00
|
|
|
List *sequential_scan_list;
|
|
|
|
List *rel_index_scan_list;
|
|
|
|
List *or_index_scan_list;
|
1999-11-23 20:07:06 +00:00
|
|
|
List *tidscan_pathlist;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
1998-08-10 02:26:40 +00:00
|
|
|
sequential_scan_list = lcons(create_seqscan_path(rel), NIL);
|
1999-11-23 20:07:06 +00:00
|
|
|
/* Tid Scan Pathlist add */
|
|
|
|
tidscan_pathlist = create_tidscan_paths(root, rel);
|
|
|
|
if (tidscan_pathlist)
|
|
|
|
sequential_scan_list = nconc(sequential_scan_list,
|
2000-01-09 00:26:47 +00:00
|
|
|
tidscan_pathlist);
|
1999-02-15 05:28:10 +00:00
|
|
|
rel_index_scan_list = create_index_paths(root,
|
1999-05-25 16:15:34 +00:00
|
|
|
rel,
|
1999-07-24 23:21:14 +00:00
|
|
|
indices,
|
1999-05-25 16:15:34 +00:00
|
|
|
rel->restrictinfo,
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
or_index_scan_list = create_or_index_paths(root, rel,
|
|
|
|
rel->restrictinfo);
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
1997-09-07 05:04:48 +00:00
|
|
|
rel->pathlist = add_pathlist(rel,
|
|
|
|
sequential_scan_list,
|
1999-02-22 05:26:58 +00:00
|
|
|
nconc(rel_index_scan_list,
|
1999-05-25 16:15:34 +00:00
|
|
|
or_index_scan_list));
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2000-01-09 00:26:47 +00:00
|
|
|
/* Now find the cheapest of the paths */
|
1999-02-12 05:57:08 +00:00
|
|
|
set_cheapest(rel, rel->pathlist);
|
2000-01-09 00:26:47 +00:00
|
|
|
/* Mark rel with estimated output rows and width */
|
|
|
|
set_rel_rows_width(root, rel);
|
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
|
|
|
|
* to join single relations into join relations.
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
1997-09-07 05:04:48 +00:00
|
|
|
* Find all possible joinpaths(bushy trees) for a query by systematically
|
|
|
|
* finding ways to join relations(both original and derived) together.
|
|
|
|
*
|
1999-02-15 05:21:12 +00:00
|
|
|
* 'rels' is the current list of relations for which join paths
|
1999-02-14 04:57:02 +00:00
|
|
|
* are to be found, i.e., the current list of relations that
|
1997-09-07 05:04:48 +00:00
|
|
|
* have already been derived.
|
1999-02-13 23:22:53 +00:00
|
|
|
* 'levels_needed' is the number of iterations needed
|
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 *
|
1999-02-15 05:21:12 +00:00
|
|
|
make_one_rel_by_joins(Query *root, List *rels, int levels_needed)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
List *x;
|
1999-02-14 05:27:12 +00:00
|
|
|
List *joined_rels = NIL;
|
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-01-22 23:50:30 +00:00
|
|
|
if (enable_geqo && length(root->base_rel_list) >= geqo_rels)
|
1999-02-15 03:22:37 +00:00
|
|
|
return geqo(root);
|
1999-05-25 16:15:34 +00:00
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
/*******************************************
|
2000-01-22 23:50:30 +00:00
|
|
|
* rest will be skipped in case of GEQO *
|
1997-09-07 05:04:48 +00:00
|
|
|
*******************************************/
|
1996-07-09 06:22:35 +00:00
|
|
|
|
1998-02-26 04:46:47 +00:00
|
|
|
while (--levels_needed)
|
|
|
|
{
|
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
|
1999-02-15 05:21:12 +00:00
|
|
|
* level. Determine paths for joining these relation pairs and
|
1999-02-14 05:27:12 +00:00
|
|
|
* modify 'joined_rels' accordingly, then eliminate redundant join
|
1998-02-26 04:46:47 +00:00
|
|
|
* relations.
|
1997-12-21 05:18:48 +00:00
|
|
|
*/
|
1999-02-15 05:21:12 +00:00
|
|
|
joined_rels = make_rels_by_joins(root, rels);
|
1998-02-26 04:46:47 +00:00
|
|
|
|
1999-02-14 05:27:12 +00:00
|
|
|
update_rels_pathlist_for_joins(root, joined_rels);
|
1998-02-26 04:46:47 +00:00
|
|
|
|
1999-02-14 05:27:12 +00:00
|
|
|
merge_rels_with_same_relids(joined_rels);
|
1998-02-26 04:46:47 +00:00
|
|
|
|
1999-02-18 00:49:48 +00:00
|
|
|
root->join_rel_list = rels = joined_rels;
|
|
|
|
|
1999-02-21 03:49:55 +00:00
|
|
|
#ifdef NOT_USED
|
1999-05-25 16:15:34 +00:00
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
/*
|
1998-02-26 04:46:47 +00:00
|
|
|
* * for each expensive predicate in each path in each distinct
|
|
|
|
* rel, * consider doing pullup -- JMH
|
1997-09-07 05:04:48 +00:00
|
|
|
*/
|
1997-12-21 05:18:48 +00:00
|
|
|
if (XfuncMode != XFUNC_NOPULL && XfuncMode != XFUNC_OFF)
|
1999-02-14 05:27:12 +00:00
|
|
|
foreach(x, joined_rels)
|
1998-07-18 04:22:52 +00:00
|
|
|
xfunc_trypullup((RelOptInfo *) lfirst(x));
|
1997-12-21 05:18:48 +00:00
|
|
|
#endif
|
1998-02-26 04:46:47 +00:00
|
|
|
|
2000-01-09 00:26:47 +00:00
|
|
|
rels_set_cheapest(root, joined_rels);
|
1998-02-26 04:46:47 +00:00
|
|
|
|
1999-02-14 05:27:12 +00:00
|
|
|
foreach(x, joined_rels)
|
1997-12-21 05:18:48 +00:00
|
|
|
{
|
1998-07-18 04:22:52 +00:00
|
|
|
rel = (RelOptInfo *) lfirst(x);
|
1999-02-14 04:57:02 +00:00
|
|
|
|
1996-07-09 06:22:35 +00:00
|
|
|
#ifdef OPTIMIZER_DEBUG
|
1998-08-07 05:02:32 +00:00
|
|
|
printf("levels left: %d\n", levels_needed);
|
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
|
|
|
}
|
1998-02-26 04:46:47 +00:00
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
}
|
1997-12-21 05:18:48 +00:00
|
|
|
|
1999-02-18 05:26:34 +00:00
|
|
|
return get_cheapest_complete_rel(rels);
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
1996-11-10 03:06:38 +00:00
|
|
|
#ifdef OPTIMIZER_DEBUG
|
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-01-09 00:26:47 +00:00
|
|
|
print_joinclauses(root, jp->path.parent->restrictinfo);
|
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 */
|