1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* orindxpath.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Routines to find index paths that match a set of 'or' clauses
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2003-08-04 04:40:20 +02:00
|
|
|
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2003-11-29 20:52:15 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/optimizer/path/orindxpath.c,v 1.54 2003/11/29 19:51:50 pgsql Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2000-02-05 19:26:09 +01:00
|
|
|
#include "postgres.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "optimizer/cost.h"
|
2000-02-15 21:49:31 +01:00
|
|
|
#include "optimizer/pathnode.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "optimizer/paths.h"
|
|
|
|
#include "optimizer/restrictinfo.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
1999-07-25 01:21:14 +02:00
|
|
|
static void best_or_subclause_indices(Query *root, RelOptInfo *rel,
|
2000-04-12 19:17:23 +02:00
|
|
|
List *subclauses, List *indices,
|
|
|
|
IndexPath *pathnode);
|
1999-07-25 01:21:14 +02:00
|
|
|
static void best_or_subclause_index(Query *root, RelOptInfo *rel,
|
2000-04-12 19:17:23 +02:00
|
|
|
Expr *subclause, List *indices,
|
2001-05-20 22:28:20 +02:00
|
|
|
IndexOptInfo **retIndexInfo,
|
2000-04-12 19:17:23 +02:00
|
|
|
List **retIndexQual,
|
|
|
|
Cost *retStartupCost,
|
|
|
|
Cost *retTotalCost);
|
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_or_index_paths
|
1997-09-07 07:04:48 +02:00
|
|
|
* Creates index paths for indices that match 'or' clauses.
|
1999-07-25 01:21:14 +02:00
|
|
|
* create_index_paths() must already have been called.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2000-02-05 19:26:09 +01:00
|
|
|
* 'rel' is the relation entry for which the paths are to be created
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* Returns nothing, but adds paths to rel->pathlist via add_path().
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
void
|
2001-06-05 19:13:52 +02:00
|
|
|
create_or_index_paths(Query *root, RelOptInfo *rel)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-06-05 19:13:52 +02:00
|
|
|
List *rlist;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-05 19:13:52 +02:00
|
|
|
foreach(rlist, rel->baserestrictinfo)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-06-05 19:13:52 +02:00
|
|
|
RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(rlist);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if this clause is an 'or' clause, and, if so,
|
|
|
|
* whether or not each of the subclauses within the 'or' clause
|
2000-04-12 19:17:23 +02:00
|
|
|
* has been matched by an index. The information used was saved
|
|
|
|
* by create_index_paths().
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-06-05 19:13:52 +02:00
|
|
|
if (restriction_is_or_clause(restrictinfo) &&
|
|
|
|
restrictinfo->subclauseindices)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-07-25 01:21:14 +02:00
|
|
|
bool all_indexable = true;
|
|
|
|
List *temp;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-05 19:13:52 +02:00
|
|
|
foreach(temp, restrictinfo->subclauseindices)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-07-25 01:21:14 +02:00
|
|
|
if (lfirst(temp) == NIL)
|
1998-08-02 00:12:13 +02:00
|
|
|
{
|
1999-07-25 01:21:14 +02:00
|
|
|
all_indexable = false;
|
1998-08-02 00:12:13 +02:00
|
|
|
break;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1999-07-25 01:21:14 +02:00
|
|
|
if (all_indexable)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* OK, build an IndexPath for this OR clause, using the
|
|
|
|
* best available index for each subclause.
|
|
|
|
*/
|
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;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* This is an IndexScan, but the overall result will
|
|
|
|
* consist of tuples extracted in multiple passes (one for
|
|
|
|
* each subclause of the OR), so the result cannot be
|
|
|
|
* claimed to have any particular ordering.
|
1998-09-21 17:41:28 +02:00
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
pathnode->path.pathkeys = NIL;
|
2003-06-16 00:51:45 +02:00
|
|
|
|
|
|
|
/* It's not an innerjoin path. */
|
|
|
|
pathnode->indexjoinclauses = NIL;
|
1998-09-21 17:41:28 +02:00
|
|
|
|
2001-06-05 19:13:52 +02:00
|
|
|
/* We don't actually care what order the index scans in. */
|
2000-02-15 21:49:31 +01:00
|
|
|
pathnode->indexscandir = NoMovementScanDirection;
|
|
|
|
|
2000-03-22 23:08:35 +01:00
|
|
|
pathnode->rows = rel->rows;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
best_or_subclause_indices(root,
|
|
|
|
rel,
|
2003-08-04 02:43:34 +02:00
|
|
|
((BoolExpr *) restrictinfo->clause)->args,
|
2001-06-05 19:13:52 +02:00
|
|
|
restrictinfo->subclauseindices,
|
2000-02-15 21:49:31 +01:00
|
|
|
pathnode);
|
|
|
|
|
|
|
|
add_path(rel, (Path *) pathnode);
|
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-02-14 00:22:53 +01:00
|
|
|
* best_or_subclause_indices
|
1997-09-07 07:04:48 +02:00
|
|
|
* Determines the best index to be used in conjunction with each subclause
|
|
|
|
* of an 'or' clause and the cost of scanning a relation using these
|
1999-07-25 01:21:14 +02:00
|
|
|
* indices. The cost is the sum of the individual index costs, since
|
|
|
|
* the executor will perform a scan for each subclause of the 'or'.
|
2001-05-20 22:28:20 +02:00
|
|
|
* Returns a list of IndexOptInfo nodes, one per scan.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2001-05-20 22:28:20 +02:00
|
|
|
* This routine also creates the indexqual list that will be needed by
|
|
|
|
* the executor. The indexqual list has one entry for each scan of the base
|
|
|
|
* rel, which is a sublist of indexqual conditions to apply in that scan.
|
|
|
|
* The implicit semantics are AND across each sublist of quals, and OR across
|
|
|
|
* the toplevel list (note that the executor takes care not to return any
|
|
|
|
* single tuple more than once).
|
1999-07-27 05:51:11 +02:00
|
|
|
*
|
1999-07-25 01:21:14 +02:00
|
|
|
* 'rel' is the node of the relation on which the indexes are defined
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'subclauses' are the subclauses of the 'or' clause
|
2000-01-09 01:26:47 +01:00
|
|
|
* 'indices' is a list of sublists of the IndexOptInfo nodes that matched
|
|
|
|
* each subclause of the 'or' clause
|
2000-02-15 21:49:31 +01:00
|
|
|
* 'pathnode' is the IndexPath node being built.
|
|
|
|
*
|
|
|
|
* Results are returned by setting these fields of the passed pathnode:
|
2001-05-20 22:28:20 +02:00
|
|
|
* 'indexinfo' gets a list of the index IndexOptInfo nodes, one per scan
|
2000-02-15 21:49:31 +01:00
|
|
|
* 'indexqual' gets the constructed indexquals for the path (a list
|
1999-07-27 05:51:11 +02:00
|
|
|
* of sublists of clauses, one sublist per scan of the base rel)
|
2000-02-15 21:49:31 +01:00
|
|
|
* 'startup_cost' and 'total_cost' get the complete path costs.
|
|
|
|
*
|
|
|
|
* 'startup_cost' is the startup cost for the first index scan only;
|
|
|
|
* startup costs for later scans will be paid later on, so they just
|
|
|
|
* get reflected in total_cost.
|
|
|
|
*
|
|
|
|
* NOTE: we choose each scan on the basis of its total cost, ignoring startup
|
|
|
|
* cost. This is reasonable as long as all index types have zero or small
|
|
|
|
* startup cost, but we might have to work harder if any index types with
|
|
|
|
* nontrivial startup cost are ever invented.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
static void
|
1997-09-08 23:56:23 +02:00
|
|
|
best_or_subclause_indices(Query *root,
|
1999-05-26 00:43:53 +02:00
|
|
|
RelOptInfo *rel,
|
1997-09-08 23:56:23 +02:00
|
|
|
List *subclauses,
|
|
|
|
List *indices,
|
2000-02-15 21:49:31 +01:00
|
|
|
IndexPath *pathnode)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-05-29 00:32:50 +02:00
|
|
|
FastList infos;
|
|
|
|
FastList quals;
|
1999-05-25 18:15:34 +02:00
|
|
|
List *slist;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
2003-05-29 00:32:50 +02:00
|
|
|
FastListInit(&infos);
|
|
|
|
FastListInit(&quals);
|
2000-02-15 21:49:31 +01:00
|
|
|
pathnode->path.startup_cost = 0;
|
|
|
|
pathnode->path.total_cost = 0;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
foreach(slist, subclauses)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-07-27 05:51:11 +02:00
|
|
|
Expr *subclause = lfirst(slist);
|
2001-05-20 22:28:20 +02:00
|
|
|
IndexOptInfo *best_indexinfo;
|
2000-02-05 19:26:09 +01:00
|
|
|
List *best_indexqual;
|
2000-02-15 21:49:31 +01:00
|
|
|
Cost best_startup_cost;
|
|
|
|
Cost best_total_cost;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-02-05 19:26:09 +01:00
|
|
|
best_or_subclause_index(root, rel, subclause, lfirst(indices),
|
2001-05-20 22:28:20 +02:00
|
|
|
&best_indexinfo, &best_indexqual,
|
2000-02-15 21:49:31 +01:00
|
|
|
&best_startup_cost, &best_total_cost);
|
2000-01-09 01:26:47 +01:00
|
|
|
|
2001-05-20 22:28:20 +02:00
|
|
|
Assert(best_indexinfo != NULL);
|
1998-09-01 06:40:42 +02:00
|
|
|
|
2003-05-29 00:32:50 +02:00
|
|
|
FastAppend(&infos, best_indexinfo);
|
|
|
|
FastAppend(&quals, best_indexqual);
|
2001-10-28 07:26:15 +01:00
|
|
|
if (slist == subclauses) /* first scan? */
|
2000-02-15 21:49:31 +01:00
|
|
|
pathnode->path.startup_cost = best_startup_cost;
|
|
|
|
pathnode->path.total_cost += best_total_cost;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-08-02 00:12:13 +02:00
|
|
|
indices = lnext(indices);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2003-05-29 00:32:50 +02:00
|
|
|
|
|
|
|
pathnode->indexinfo = FastListValue(&infos);
|
|
|
|
pathnode->indexqual = FastListValue(&quals);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* best_or_subclause_index
|
2000-02-15 21:49:31 +01:00
|
|
|
* Determines which is the best index to be used with a subclause of an
|
|
|
|
* 'or' clause by estimating the cost of using each index and selecting
|
|
|
|
* the least expensive (considering total cost only, for now).
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'rel' is the node of the relation on which the index is defined
|
2000-02-05 19:26:09 +01:00
|
|
|
* 'subclause' is the OR subclause being considered
|
2000-01-09 01:26:47 +01:00
|
|
|
* 'indices' is a list of IndexOptInfo nodes that match the subclause
|
2001-05-20 22:28:20 +02:00
|
|
|
* '*retIndexInfo' gets the IndexOptInfo of the best index
|
2000-02-05 19:26:09 +01:00
|
|
|
* '*retIndexQual' gets a list of the indexqual conditions for the best index
|
2000-02-15 21:49:31 +01:00
|
|
|
* '*retStartupCost' gets the startup cost of a scan with that index
|
|
|
|
* '*retTotalCost' gets the total cost of a scan with that index
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
static void
|
1997-09-08 23:56:23 +02:00
|
|
|
best_or_subclause_index(Query *root,
|
1999-05-26 00:43:53 +02:00
|
|
|
RelOptInfo *rel,
|
2000-02-05 19:26:09 +01:00
|
|
|
Expr *subclause,
|
1997-09-08 23:56:23 +02:00
|
|
|
List *indices,
|
2001-10-25 07:50:21 +02:00
|
|
|
IndexOptInfo **retIndexInfo, /* return value */
|
2000-02-05 19:26:09 +01:00
|
|
|
List **retIndexQual, /* return value */
|
2000-02-15 21:49:31 +01:00
|
|
|
Cost *retStartupCost, /* return value */
|
|
|
|
Cost *retTotalCost) /* return value */
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-02-05 19:26:09 +01:00
|
|
|
bool first_time = true;
|
1999-07-25 01:21:14 +02:00
|
|
|
List *ilist;
|
1998-08-31 09:19:56 +02:00
|
|
|
|
|
|
|
/* if we don't match anything, return zeros */
|
2001-05-20 22:28:20 +02:00
|
|
|
*retIndexInfo = NULL;
|
2000-02-05 19:26:09 +01:00
|
|
|
*retIndexQual = NIL;
|
2000-02-15 21:49:31 +01:00
|
|
|
*retStartupCost = 0;
|
|
|
|
*retTotalCost = 0;
|
1999-07-25 01:21:14 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
foreach(ilist, indices)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-01-09 01:26:47 +01:00
|
|
|
IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
|
2000-02-05 19:26:09 +01:00
|
|
|
List *indexqual;
|
2000-02-15 21:49:31 +01:00
|
|
|
Path subclause_path;
|
2000-01-09 01:26:47 +01:00
|
|
|
|
|
|
|
Assert(IsA(index, IndexOptInfo));
|
1999-07-26 01:07:26 +02:00
|
|
|
|
2000-02-05 19:26:09 +01:00
|
|
|
/* Convert this 'or' subclause to an indexqual list */
|
|
|
|
indexqual = extract_or_indexqual_conditions(rel, index, subclause);
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
cost_index(&subclause_path, root, rel, index, indexqual, false);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
if (first_time || subclause_path.total_cost < *retTotalCost)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-05-20 22:28:20 +02:00
|
|
|
*retIndexInfo = index;
|
2000-02-05 19:26:09 +01:00
|
|
|
*retIndexQual = indexqual;
|
2000-02-15 21:49:31 +01:00
|
|
|
*retStartupCost = subclause_path.startup_cost;
|
|
|
|
*retTotalCost = subclause_path.total_cost;
|
2000-02-05 19:26:09 +01:00
|
|
|
first_time = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-08-02 00:12:13 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|