1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* costsize.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Routines to compute (and set) relation sizes and path costs
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2006-06-05 04:49:58 +02:00
|
|
|
* Path costs are measured in arbitrary units established by these basic
|
|
|
|
* parameters:
|
2000-02-15 21:49:31 +01:00
|
|
|
*
|
2006-06-05 04:49:58 +02:00
|
|
|
* seq_page_cost Cost of a sequential page fetch
|
2000-02-15 21:49:31 +01:00
|
|
|
* random_page_cost Cost of a non-sequential page fetch
|
|
|
|
* cpu_tuple_cost Cost of typical CPU time to process a tuple
|
|
|
|
* cpu_index_tuple_cost Cost of typical CPU time to process an index tuple
|
2006-06-05 04:49:58 +02:00
|
|
|
* cpu_operator_cost Cost of CPU time to execute an operator or function
|
|
|
|
*
|
|
|
|
* We expect that the kernel will typically do some amount of read-ahead
|
|
|
|
* optimization; this in conjunction with seek costs means that seq_page_cost
|
|
|
|
* is normally considerably less than random_page_cost. (However, if the
|
|
|
|
* database is fully cached in RAM, it is reasonable to set them equal.)
|
2000-02-15 21:49:31 +01:00
|
|
|
*
|
|
|
|
* We also use a rough estimate "effective_cache_size" of the number of
|
|
|
|
* disk pages in Postgres + OS-level disk cache. (We can't simply use
|
|
|
|
* NBuffers for this purpose because that would ignore the effects of
|
|
|
|
* the kernel's disk cache.)
|
|
|
|
*
|
|
|
|
* Obviously, taking constants for these values is an oversimplification,
|
|
|
|
* but it's tough enough to get any useful estimates even at this level of
|
2000-04-12 19:17:23 +02:00
|
|
|
* detail. Note that all of these parameters are user-settable, in case
|
2000-02-15 21:49:31 +01:00
|
|
|
* the default values are drastically off for a particular platform.
|
|
|
|
*
|
2010-01-05 22:54:00 +01:00
|
|
|
* seq_page_cost and random_page_cost can also be overridden for an individual
|
|
|
|
* tablespace, in case some data is on a fast disk and other data is on a slow
|
|
|
|
* disk. Per-tablespace overrides never apply to temporary work files such as
|
|
|
|
* an external sort or a materialize node that overflows work_mem.
|
|
|
|
*
|
2000-02-15 21:49:31 +01:00
|
|
|
* We compute two separate costs for each path:
|
|
|
|
* total_cost: total estimated cost to fetch all tuples
|
|
|
|
* startup_cost: cost that is expended before first tuple is fetched
|
|
|
|
* In some scenarios, such as when there is a LIMIT or we are implementing
|
|
|
|
* an EXISTS(...) sub-select, it is not necessary to fetch all tuples of the
|
|
|
|
* path's result. A caller can estimate the cost of fetching a partial
|
|
|
|
* result by interpolating between startup_cost and total_cost. In detail:
|
|
|
|
* actual_cost = startup_cost +
|
|
|
|
* (total_cost - startup_cost) * tuples_to_fetch / path->parent->rows;
|
2001-05-10 01:13:37 +02:00
|
|
|
* Note that a base relation's rows count (and, by extension, plan_rows for
|
|
|
|
* plan nodes below the LIMIT node) are set without regard to any LIMIT, so
|
|
|
|
* that this equation works properly. (Also, these routines guarantee not to
|
|
|
|
* set the rows count to zero, so there will be no zero divide.) The LIMIT is
|
|
|
|
* applied as a top-level plan node.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2003-01-27 21:51:54 +01:00
|
|
|
* For largely historical reasons, most of the routines in this module use
|
|
|
|
* the passed result Path only to store their startup_cost and total_cost
|
|
|
|
* results into. All the input data they need is passed as separate
|
|
|
|
* parameters, even though much of it could be extracted from the Path.
|
|
|
|
* An exception is made for the cost_XXXjoin() routines, which expect all
|
|
|
|
* the non-cost fields of the passed XXXPath to be filled in.
|
|
|
|
*
|
2000-04-12 19:17:23 +02:00
|
|
|
*
|
2010-01-02 17:58:17 +01:00
|
|
|
* Portions Copyright (c) 1996-2010, 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
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/optimizer/path/costsize.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1997-01-08 11:33:46 +01:00
|
|
|
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "postgres.h"
|
1999-08-06 06:00:17 +02:00
|
|
|
|
2000-01-09 01:26:47 +01:00
|
|
|
#include <math.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2009-09-13 00:12:09 +02:00
|
|
|
#include "executor/executor.h"
|
2000-04-18 07:43:02 +02:00
|
|
|
#include "executor/nodeHash.h"
|
1999-08-06 06:00:17 +02:00
|
|
|
#include "miscadmin.h"
|
2008-08-26 00:42:34 +02:00
|
|
|
#include "nodes/nodeFuncs.h"
|
2000-02-15 21:49:31 +01:00
|
|
|
#include "optimizer/clauses.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "optimizer/cost.h"
|
2001-05-07 02:43:27 +02:00
|
|
|
#include "optimizer/pathnode.h"
|
2008-10-21 22:42:53 +02:00
|
|
|
#include "optimizer/placeholder.h"
|
2010-11-19 23:31:50 +01:00
|
|
|
#include "optimizer/plancat.h"
|
2007-01-22 02:35:23 +01:00
|
|
|
#include "optimizer/planmain.h"
|
2009-05-10 00:51:41 +02:00
|
|
|
#include "optimizer/restrictinfo.h"
|
2001-05-07 02:43:27 +02:00
|
|
|
#include "parser/parsetree.h"
|
1998-01-13 05:05:12 +01:00
|
|
|
#include "utils/lsyscache.h"
|
2006-07-11 18:35:33 +02:00
|
|
|
#include "utils/selfuncs.h"
|
2010-01-05 22:54:00 +01:00
|
|
|
#include "utils/spccache.h"
|
2006-02-19 06:54:06 +01:00
|
|
|
#include "utils/tuplesort.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-10-31 06:58:01 +01:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
#define LOG2(x) (log(x) / 0.693147180559945)
|
|
|
|
|
2003-01-27 21:51:54 +01:00
|
|
|
/*
|
|
|
|
* Some Paths return less than the nominal number of rows of their parent
|
|
|
|
* relations; join nodes need to do this to get the correct input count:
|
|
|
|
*/
|
|
|
|
#define PATH_ROWS(path) \
|
|
|
|
(IsA(path, UniquePath) ? \
|
|
|
|
((UniquePath *) (path))->rows : \
|
|
|
|
(path)->parent->rows)
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
|
2006-06-05 04:49:58 +02:00
|
|
|
double seq_page_cost = DEFAULT_SEQ_PAGE_COST;
|
2001-03-22 05:01:46 +01:00
|
|
|
double random_page_cost = DEFAULT_RANDOM_PAGE_COST;
|
|
|
|
double cpu_tuple_cost = DEFAULT_CPU_TUPLE_COST;
|
|
|
|
double cpu_index_tuple_cost = DEFAULT_CPU_INDEX_TUPLE_COST;
|
|
|
|
double cpu_operator_cost = DEFAULT_CPU_OPERATOR_COST;
|
2000-01-23 00:50:30 +01:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
int effective_cache_size = DEFAULT_EFFECTIVE_CACHE_SIZE;
|
2006-06-05 04:49:58 +02:00
|
|
|
|
2009-04-17 17:33:33 +02:00
|
|
|
Cost disable_cost = 1.0e10;
|
2000-01-23 00:50:30 +01:00
|
|
|
|
|
|
|
bool enable_seqscan = true;
|
|
|
|
bool enable_indexscan = true;
|
2005-04-21 21:18:13 +02:00
|
|
|
bool enable_bitmapscan = true;
|
2000-01-23 00:50:30 +01:00
|
|
|
bool enable_tidscan = true;
|
|
|
|
bool enable_sort = true;
|
2002-11-21 01:42:20 +01:00
|
|
|
bool enable_hashagg = true;
|
2000-01-23 00:50:30 +01:00
|
|
|
bool enable_nestloop = true;
|
2010-04-19 02:55:26 +02:00
|
|
|
bool enable_material = true;
|
2000-01-23 00:50:30 +01:00
|
|
|
bool enable_mergejoin = true;
|
|
|
|
bool enable_hashjoin = true;
|
|
|
|
|
2007-02-22 23:00:26 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
PlannerInfo *root;
|
|
|
|
QualCost total;
|
2007-11-15 23:25:18 +01:00
|
|
|
} cost_qual_eval_context;
|
2000-01-23 00:50:30 +01:00
|
|
|
|
2007-01-22 21:00:40 +01:00
|
|
|
static MergeScanSelCache *cached_scansel(PlannerInfo *root,
|
2007-11-15 22:14:46 +01:00
|
|
|
RestrictInfo *rinfo,
|
2007-11-15 23:25:18 +01:00
|
|
|
PathKey *pathkey);
|
2009-09-13 00:12:09 +02:00
|
|
|
static void cost_rescan(PlannerInfo *root, Path *path,
|
|
|
|
Cost *rescan_startup_cost, Cost *rescan_total_cost);
|
2007-11-15 23:25:18 +01:00
|
|
|
static bool cost_qual_eval_walker(Node *node, cost_qual_eval_context *context);
|
2009-05-10 00:51:41 +02:00
|
|
|
static bool adjust_semi_join(PlannerInfo *root, JoinPath *path,
|
|
|
|
SpecialJoinInfo *sjinfo,
|
|
|
|
Selectivity *outer_match_frac,
|
|
|
|
Selectivity *match_count,
|
|
|
|
bool *indexed_join_quals);
|
2008-08-16 02:01:38 +02:00
|
|
|
static double approx_tuple_count(PlannerInfo *root, JoinPath *path,
|
2009-06-11 16:49:15 +02:00
|
|
|
List *quals);
|
2005-06-06 00:32:58 +02:00
|
|
|
static void set_rel_width(PlannerInfo *root, RelOptInfo *rel);
|
2000-01-09 01:26:47 +01:00
|
|
|
static double relation_byte_size(double tuples, int width);
|
|
|
|
static double page_size(double tuples, int width);
|
1999-08-06 06:00:17 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-01-05 06:07:36 +01:00
|
|
|
/*
|
|
|
|
* clamp_row_est
|
|
|
|
* Force a row-count estimate to a sane value.
|
|
|
|
*/
|
|
|
|
double
|
|
|
|
clamp_row_est(double nrows)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Force estimate to be at least one row, to make explain output look
|
2005-10-15 04:49:52 +02:00
|
|
|
* better and to avoid possible divide-by-zero when interpolating costs.
|
|
|
|
* Make it an integer, too.
|
2004-01-05 06:07:36 +01:00
|
|
|
*/
|
2006-06-06 19:59:58 +02:00
|
|
|
if (nrows <= 1.0)
|
2004-01-05 06:07:36 +01:00
|
|
|
nrows = 1.0;
|
|
|
|
else
|
2005-04-21 04:28:02 +02:00
|
|
|
nrows = rint(nrows);
|
2004-01-05 06:07:36 +01:00
|
|
|
|
|
|
|
return nrows;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* cost_seqscan
|
1997-09-07 07:04:48 +02:00
|
|
|
* Determines and returns the cost of scanning a relation sequentially.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
cost_seqscan(Path *path, PlannerInfo *root,
|
2001-06-05 07:26:05 +02:00
|
|
|
RelOptInfo *baserel)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2010-01-05 22:54:00 +01:00
|
|
|
double spc_seq_page_cost;
|
2000-02-15 21:49:31 +01:00
|
|
|
Cost startup_cost = 0;
|
|
|
|
Cost run_cost = 0;
|
|
|
|
Cost cpu_per_tuple;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-01-09 01:26:47 +01:00
|
|
|
/* Should only be applied to base relations */
|
2003-02-08 21:20:55 +01:00
|
|
|
Assert(baserel->relid > 0);
|
2002-05-12 22:10:05 +02:00
|
|
|
Assert(baserel->rtekind == RTE_RELATION);
|
2000-01-09 01:26:47 +01:00
|
|
|
|
2000-01-23 00:50:30 +01:00
|
|
|
if (!enable_seqscan)
|
2000-02-15 21:49:31 +01:00
|
|
|
startup_cost += disable_cost;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2010-01-05 22:54:00 +01:00
|
|
|
/* fetch estimated page cost for tablespace containing table */
|
|
|
|
get_tablespace_page_costs(baserel->reltablespace,
|
|
|
|
NULL,
|
|
|
|
&spc_seq_page_cost);
|
|
|
|
|
2000-06-19 00:44:35 +02:00
|
|
|
/*
|
|
|
|
* disk costs
|
|
|
|
*/
|
2010-01-05 22:54:00 +01:00
|
|
|
run_cost += spc_seq_page_cost * baserel->pages;
|
2000-01-09 01:26:47 +01:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/* CPU costs */
|
2003-01-12 23:35:29 +01:00
|
|
|
startup_cost += baserel->baserestrictcost.startup;
|
|
|
|
cpu_per_tuple = cpu_tuple_cost + baserel->baserestrictcost.per_tuple;
|
2000-02-15 21:49:31 +01:00
|
|
|
run_cost += cpu_per_tuple * baserel->tuples;
|
|
|
|
|
|
|
|
path->startup_cost = startup_cost;
|
|
|
|
path->total_cost = startup_cost + run_cost;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* cost_index
|
1997-09-07 07:04:48 +02:00
|
|
|
* Determines and returns the cost of scanning a relation using an index.
|
|
|
|
*
|
2000-01-09 01:26:47 +01:00
|
|
|
* 'index' is the index to be used
|
2000-01-23 00:50:30 +01:00
|
|
|
* 'indexQuals' is the list of applicable qual clauses (implicit AND semantics)
|
2006-06-06 19:59:58 +02:00
|
|
|
* 'outer_rel' is the outer relation when we are considering using the index
|
|
|
|
* scan as the inside of a nestloop join (hence, some of the indexQuals
|
|
|
|
* are join clauses, and we should expect repeated scans of the index);
|
|
|
|
* NULL for a plain index scan
|
2000-01-09 01:26:47 +01:00
|
|
|
*
|
2005-04-21 04:28:02 +02:00
|
|
|
* cost_index() takes an IndexPath not just a Path, because it sets a few
|
|
|
|
* additional fields of the IndexPath besides startup_cost and total_cost.
|
|
|
|
* These fields are needed if the IndexPath is used in a BitmapIndexScan.
|
|
|
|
*
|
2000-01-23 00:50:30 +01:00
|
|
|
* NOTE: 'indexQuals' must contain only clauses usable as index restrictions.
|
|
|
|
* Any additional quals evaluated as qpquals may reduce the number of returned
|
|
|
|
* tuples, but they won't reduce the number of tuples we have to fetch from
|
|
|
|
* the table, so they don't reduce the scan cost.
|
2004-01-06 00:39:54 +01:00
|
|
|
*
|
2004-08-04 23:34:35 +02:00
|
|
|
* NOTE: as of 8.0, indexQuals is a list of RestrictInfo nodes, where formerly
|
2004-01-06 00:39:54 +01:00
|
|
|
* it was a list of bare clause expressions.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
cost_index(IndexPath *path, PlannerInfo *root,
|
2000-01-09 01:26:47 +01:00
|
|
|
IndexOptInfo *index,
|
2000-01-23 00:50:30 +01:00
|
|
|
List *indexQuals,
|
2006-06-06 19:59:58 +02:00
|
|
|
RelOptInfo *outer_rel)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-03-27 08:29:49 +02:00
|
|
|
RelOptInfo *baserel = index->rel;
|
2000-02-15 21:49:31 +01:00
|
|
|
Cost startup_cost = 0;
|
|
|
|
Cost run_cost = 0;
|
|
|
|
Cost indexStartupCost;
|
|
|
|
Cost indexTotalCost;
|
2000-04-12 19:17:23 +02:00
|
|
|
Selectivity indexSelectivity;
|
2001-05-10 01:13:37 +02:00
|
|
|
double indexCorrelation,
|
|
|
|
csquared;
|
2010-01-05 22:54:00 +01:00
|
|
|
double spc_seq_page_cost,
|
|
|
|
spc_random_page_cost;
|
2001-05-10 01:13:37 +02:00
|
|
|
Cost min_IO_cost,
|
|
|
|
max_IO_cost;
|
|
|
|
Cost cpu_per_tuple;
|
2000-02-15 21:49:31 +01:00
|
|
|
double tuples_fetched;
|
|
|
|
double pages_fetched;
|
2000-01-09 01:26:47 +01:00
|
|
|
|
|
|
|
/* Should only be applied to base relations */
|
2002-05-12 22:10:05 +02:00
|
|
|
Assert(IsA(baserel, RelOptInfo) &&
|
|
|
|
IsA(index, IndexOptInfo));
|
2003-02-08 21:20:55 +01:00
|
|
|
Assert(baserel->relid > 0);
|
2002-05-12 22:10:05 +02:00
|
|
|
Assert(baserel->rtekind == RTE_RELATION);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2002-11-30 06:21:03 +01:00
|
|
|
if (!enable_indexscan)
|
2000-02-15 21:49:31 +01:00
|
|
|
startup_cost += disable_cost;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Call index-access-method-specific code to estimate the processing cost
|
|
|
|
* for scanning the index, as well as the selectivity of the index (ie,
|
|
|
|
* the fraction of main-table tuples we will have to retrieve) and its
|
|
|
|
* correlation to the main-table tuple order.
|
1999-04-30 06:01:44 +02:00
|
|
|
*/
|
2006-06-06 19:59:58 +02:00
|
|
|
OidFunctionCall8(index->amcostestimate,
|
2000-05-30 06:25:00 +02:00
|
|
|
PointerGetDatum(root),
|
|
|
|
PointerGetDatum(index),
|
|
|
|
PointerGetDatum(indexQuals),
|
2006-06-06 19:59:58 +02:00
|
|
|
PointerGetDatum(outer_rel),
|
2000-05-30 06:25:00 +02:00
|
|
|
PointerGetDatum(&indexStartupCost),
|
|
|
|
PointerGetDatum(&indexTotalCost),
|
2001-05-10 01:13:37 +02:00
|
|
|
PointerGetDatum(&indexSelectivity),
|
|
|
|
PointerGetDatum(&indexCorrelation));
|
1999-04-30 06:01:44 +02:00
|
|
|
|
2005-04-21 04:28:02 +02:00
|
|
|
/*
|
2005-04-21 21:18:13 +02:00
|
|
|
* Save amcostestimate's results for possible use in bitmap scan planning.
|
2005-10-15 04:49:52 +02:00
|
|
|
* We don't bother to save indexStartupCost or indexCorrelation, because a
|
|
|
|
* bitmap scan doesn't care about either.
|
2005-04-21 04:28:02 +02:00
|
|
|
*/
|
|
|
|
path->indextotalcost = indexTotalCost;
|
|
|
|
path->indexselectivity = indexSelectivity;
|
|
|
|
|
2000-01-23 00:50:30 +01:00
|
|
|
/* all costs for touching index itself included here */
|
2000-02-15 21:49:31 +01:00
|
|
|
startup_cost += indexStartupCost;
|
|
|
|
run_cost += indexTotalCost - indexStartupCost;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2006-06-06 19:59:58 +02:00
|
|
|
/* estimate number of main-table tuples fetched */
|
|
|
|
tuples_fetched = clamp_row_est(indexSelectivity * baserel->tuples);
|
|
|
|
|
2010-01-05 22:54:00 +01:00
|
|
|
/* fetch estimated page costs for tablespace containing table */
|
|
|
|
get_tablespace_page_costs(baserel->reltablespace,
|
|
|
|
&spc_random_page_cost,
|
|
|
|
&spc_seq_page_cost);
|
|
|
|
|
2001-05-10 01:13:37 +02:00
|
|
|
/*----------
|
2006-06-06 19:59:58 +02:00
|
|
|
* Estimate number of main-table pages fetched, and compute I/O cost.
|
2000-01-09 01:26:47 +01:00
|
|
|
*
|
2001-05-10 01:13:37 +02:00
|
|
|
* When the index ordering is uncorrelated with the table ordering,
|
2006-06-06 19:59:58 +02:00
|
|
|
* we use an approximation proposed by Mackert and Lohman (see
|
|
|
|
* index_pages_fetched() for details) to compute the number of pages
|
2010-01-05 22:54:00 +01:00
|
|
|
* fetched, and then charge spc_random_page_cost per page fetched.
|
2001-05-10 01:13:37 +02:00
|
|
|
*
|
|
|
|
* When the index ordering is exactly correlated with the table ordering
|
|
|
|
* (just after a CLUSTER, for example), the number of pages fetched should
|
2006-06-06 19:59:58 +02:00
|
|
|
* be exactly selectivity * table_size. What's more, all but the first
|
|
|
|
* will be sequential fetches, not the random fetches that occur in the
|
|
|
|
* uncorrelated case. So if the number of pages is more than 1, we
|
|
|
|
* ought to charge
|
2010-01-05 22:54:00 +01:00
|
|
|
* spc_random_page_cost + (pages_fetched - 1) * spc_seq_page_cost
|
2006-06-06 19:59:58 +02:00
|
|
|
* For partially-correlated indexes, we ought to charge somewhere between
|
|
|
|
* these two estimates. We currently interpolate linearly between the
|
|
|
|
* estimates based on the correlation squared (XXX is that appropriate?).
|
2001-05-10 01:13:37 +02:00
|
|
|
*----------
|
1999-04-30 06:01:44 +02:00
|
|
|
*/
|
2006-06-06 19:59:58 +02:00
|
|
|
if (outer_rel != NULL && outer_rel->rows > 1)
|
2001-05-10 01:13:37 +02:00
|
|
|
{
|
2006-06-06 19:59:58 +02:00
|
|
|
/*
|
2006-12-15 19:42:26 +01:00
|
|
|
* For repeated indexscans, the appropriate estimate for the
|
|
|
|
* uncorrelated case is to scale up the number of tuples fetched in
|
2006-10-04 02:30:14 +02:00
|
|
|
* the Mackert and Lohman formula by the number of scans, so that we
|
2006-12-15 19:42:26 +01:00
|
|
|
* estimate the number of pages fetched by all the scans; then
|
2006-10-04 02:30:14 +02:00
|
|
|
* pro-rate the costs for one scan. In this case we assume all the
|
2006-12-15 19:42:26 +01:00
|
|
|
* fetches are random accesses.
|
2006-06-06 19:59:58 +02:00
|
|
|
*/
|
|
|
|
double num_scans = outer_rel->rows;
|
|
|
|
|
|
|
|
pages_fetched = index_pages_fetched(tuples_fetched * num_scans,
|
|
|
|
baserel->pages,
|
2006-09-20 00:49:53 +02:00
|
|
|
(double) index->pages,
|
|
|
|
root);
|
2006-06-06 19:59:58 +02:00
|
|
|
|
2010-01-05 22:54:00 +01:00
|
|
|
max_IO_cost = (pages_fetched * spc_random_page_cost) / num_scans;
|
2006-12-15 19:42:26 +01:00
|
|
|
|
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* In the perfectly correlated case, the number of pages touched by
|
|
|
|
* each scan is selectivity * table_size, and we can use the Mackert
|
|
|
|
* and Lohman formula at the page level to estimate how much work is
|
|
|
|
* saved by caching across scans. We still assume all the fetches are
|
|
|
|
* random, though, which is an overestimate that's hard to correct for
|
|
|
|
* without double-counting the cache effects. (But in most cases
|
|
|
|
* where such a plan is actually interesting, only one page would get
|
|
|
|
* fetched per scan anyway, so it shouldn't matter much.)
|
2006-12-15 19:42:26 +01:00
|
|
|
*/
|
|
|
|
pages_fetched = ceil(indexSelectivity * (double) baserel->pages);
|
|
|
|
|
|
|
|
pages_fetched = index_pages_fetched(pages_fetched * num_scans,
|
|
|
|
baserel->pages,
|
|
|
|
(double) index->pages,
|
|
|
|
root);
|
|
|
|
|
2010-01-05 22:54:00 +01:00
|
|
|
min_IO_cost = (pages_fetched * spc_random_page_cost) / num_scans;
|
2001-05-10 01:13:37 +02:00
|
|
|
}
|
2000-02-15 21:49:31 +01:00
|
|
|
else
|
2001-05-10 01:13:37 +02:00
|
|
|
{
|
2006-06-06 19:59:58 +02:00
|
|
|
/*
|
|
|
|
* Normal case: apply the Mackert and Lohman formula, and then
|
|
|
|
* interpolate between that and the correlation-derived result.
|
|
|
|
*/
|
|
|
|
pages_fetched = index_pages_fetched(tuples_fetched,
|
|
|
|
baserel->pages,
|
2006-09-20 00:49:53 +02:00
|
|
|
(double) index->pages,
|
|
|
|
root);
|
2006-06-06 19:59:58 +02:00
|
|
|
|
|
|
|
/* max_IO_cost is for the perfectly uncorrelated case (csquared=0) */
|
2010-01-05 22:54:00 +01:00
|
|
|
max_IO_cost = pages_fetched * spc_random_page_cost;
|
2006-06-06 19:59:58 +02:00
|
|
|
|
|
|
|
/* min_IO_cost is for the perfectly correlated case (csquared=1) */
|
|
|
|
pages_fetched = ceil(indexSelectivity * (double) baserel->pages);
|
2010-01-05 22:54:00 +01:00
|
|
|
min_IO_cost = spc_random_page_cost;
|
2006-06-06 19:59:58 +02:00
|
|
|
if (pages_fetched > 1)
|
2010-01-05 22:54:00 +01:00
|
|
|
min_IO_cost += (pages_fetched - 1) * spc_seq_page_cost;
|
2006-12-15 19:42:26 +01:00
|
|
|
}
|
2006-06-06 19:59:58 +02:00
|
|
|
|
2006-12-15 19:42:26 +01:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* Now interpolate based on estimated index order correlation to get total
|
|
|
|
* disk I/O cost for main table accesses.
|
2006-12-15 19:42:26 +01:00
|
|
|
*/
|
|
|
|
csquared = indexCorrelation * indexCorrelation;
|
2006-06-06 19:59:58 +02:00
|
|
|
|
2006-12-15 19:42:26 +01:00
|
|
|
run_cost += max_IO_cost + csquared * (min_IO_cost - max_IO_cost);
|
2000-02-15 21:49:31 +01:00
|
|
|
|
|
|
|
/*
|
2001-05-10 01:13:37 +02:00
|
|
|
* Estimate CPU costs per tuple.
|
|
|
|
*
|
2001-10-25 07:50:21 +02:00
|
|
|
* Normally the indexquals will be removed from the list of restriction
|
|
|
|
* clauses that we have to evaluate as qpquals, so we should subtract
|
2003-01-12 23:35:29 +01:00
|
|
|
* their costs from baserestrictcost. But if we are doing a join then
|
2005-10-15 04:49:52 +02:00
|
|
|
* some of the indexquals are join clauses and shouldn't be subtracted.
|
|
|
|
* Rather than work out exactly how much to subtract, we don't subtract
|
|
|
|
* anything.
|
2000-02-15 21:49:31 +01:00
|
|
|
*/
|
2003-01-12 23:35:29 +01:00
|
|
|
startup_cost += baserel->baserestrictcost.startup;
|
|
|
|
cpu_per_tuple = cpu_tuple_cost + baserel->baserestrictcost.per_tuple;
|
2001-05-10 01:13:37 +02:00
|
|
|
|
2006-06-06 19:59:58 +02:00
|
|
|
if (outer_rel == NULL)
|
2003-01-12 23:35:29 +01:00
|
|
|
{
|
|
|
|
QualCost index_qual_cost;
|
|
|
|
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval(&index_qual_cost, indexQuals, root);
|
2004-01-06 05:31:01 +01:00
|
|
|
/* any startup cost still has to be paid ... */
|
2003-01-12 23:35:29 +01:00
|
|
|
cpu_per_tuple -= index_qual_cost.per_tuple;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
run_cost += cpu_per_tuple * tuples_fetched;
|
|
|
|
|
2005-04-21 04:28:02 +02:00
|
|
|
path->path.startup_cost = startup_cost;
|
|
|
|
path->path.total_cost = startup_cost + run_cost;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2006-06-06 19:59:58 +02:00
|
|
|
/*
|
|
|
|
* index_pages_fetched
|
|
|
|
* Estimate the number of pages actually fetched after accounting for
|
|
|
|
* cache effects.
|
|
|
|
*
|
|
|
|
* We use an approximation proposed by Mackert and Lohman, "Index Scans
|
|
|
|
* Using a Finite LRU Buffer: A Validated I/O Model", ACM Transactions
|
|
|
|
* on Database Systems, Vol. 14, No. 3, September 1989, Pages 401-424.
|
|
|
|
* The Mackert and Lohman approximation is that the number of pages
|
|
|
|
* fetched is
|
|
|
|
* PF =
|
|
|
|
* min(2TNs/(2T+Ns), T) when T <= b
|
|
|
|
* 2TNs/(2T+Ns) when T > b and Ns <= 2Tb/(2T-b)
|
|
|
|
* b + (Ns - 2Tb/(2T-b))*(T-b)/T when T > b and Ns > 2Tb/(2T-b)
|
|
|
|
* where
|
|
|
|
* T = # pages in table
|
|
|
|
* N = # tuples in table
|
|
|
|
* s = selectivity = fraction of table to be scanned
|
|
|
|
* b = # buffer pages available (we include kernel space here)
|
|
|
|
*
|
|
|
|
* We assume that effective_cache_size is the total number of buffer pages
|
2006-09-20 00:49:53 +02:00
|
|
|
* available for the whole query, and pro-rate that space across all the
|
|
|
|
* tables in the query and the index currently under consideration. (This
|
|
|
|
* ignores space needed for other indexes used by the query, but since we
|
|
|
|
* don't know which indexes will get used, we can't estimate that very well;
|
|
|
|
* and in any case counting all the tables may well be an overestimate, since
|
|
|
|
* depending on the join plan not all the tables may be scanned concurrently.)
|
2006-06-06 19:59:58 +02:00
|
|
|
*
|
|
|
|
* The product Ns is the number of tuples fetched; we pass in that
|
2006-09-20 00:49:53 +02:00
|
|
|
* product rather than calculating it here. "pages" is the number of pages
|
|
|
|
* in the object under consideration (either an index or a table).
|
|
|
|
* "index_pages" is the amount to add to the total table space, which was
|
|
|
|
* computed for us by query_planner.
|
2006-06-06 19:59:58 +02:00
|
|
|
*
|
|
|
|
* Caller is expected to have ensured that tuples_fetched is greater than zero
|
2006-10-04 02:30:14 +02:00
|
|
|
* and rounded to integer (see clamp_row_est). The result will likewise be
|
2006-06-06 19:59:58 +02:00
|
|
|
* greater than zero and integral.
|
|
|
|
*/
|
|
|
|
double
|
|
|
|
index_pages_fetched(double tuples_fetched, BlockNumber pages,
|
2006-09-20 00:49:53 +02:00
|
|
|
double index_pages, PlannerInfo *root)
|
2006-06-06 19:59:58 +02:00
|
|
|
{
|
|
|
|
double pages_fetched;
|
2006-09-20 00:49:53 +02:00
|
|
|
double total_pages;
|
2006-06-06 19:59:58 +02:00
|
|
|
double T,
|
|
|
|
b;
|
|
|
|
|
|
|
|
/* T is # pages in table, but don't allow it to be zero */
|
|
|
|
T = (pages > 1) ? (double) pages : 1.0;
|
|
|
|
|
2006-09-20 00:49:53 +02:00
|
|
|
/* Compute number of pages assumed to be competing for cache space */
|
|
|
|
total_pages = root->total_table_pages + index_pages;
|
|
|
|
total_pages = Max(total_pages, 1.0);
|
|
|
|
Assert(T <= total_pages);
|
|
|
|
|
2006-06-06 19:59:58 +02:00
|
|
|
/* b is pro-rated share of effective_cache_size */
|
2006-10-04 02:30:14 +02:00
|
|
|
b = (double) effective_cache_size *T / total_pages;
|
|
|
|
|
2006-06-06 19:59:58 +02:00
|
|
|
/* force it positive and integral */
|
|
|
|
if (b <= 1.0)
|
|
|
|
b = 1.0;
|
|
|
|
else
|
|
|
|
b = ceil(b);
|
|
|
|
|
|
|
|
/* This part is the Mackert and Lohman formula */
|
|
|
|
if (T <= b)
|
|
|
|
{
|
|
|
|
pages_fetched =
|
|
|
|
(2.0 * T * tuples_fetched) / (2.0 * T + tuples_fetched);
|
|
|
|
if (pages_fetched >= T)
|
|
|
|
pages_fetched = T;
|
|
|
|
else
|
|
|
|
pages_fetched = ceil(pages_fetched);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
double lim;
|
|
|
|
|
|
|
|
lim = (2.0 * T * b) / (2.0 * T - b);
|
|
|
|
if (tuples_fetched <= lim)
|
|
|
|
{
|
|
|
|
pages_fetched =
|
|
|
|
(2.0 * T * tuples_fetched) / (2.0 * T + tuples_fetched);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pages_fetched =
|
|
|
|
b + (tuples_fetched - lim) * (T - b) / T;
|
|
|
|
}
|
|
|
|
pages_fetched = ceil(pages_fetched);
|
|
|
|
}
|
|
|
|
return pages_fetched;
|
|
|
|
}
|
|
|
|
|
2006-09-20 00:49:53 +02:00
|
|
|
/*
|
|
|
|
* get_indexpath_pages
|
|
|
|
* Determine the total size of the indexes used in a bitmap index path.
|
|
|
|
*
|
|
|
|
* Note: if the same index is used more than once in a bitmap tree, we will
|
|
|
|
* count it multiple times, which perhaps is the wrong thing ... but it's
|
|
|
|
* not completely clear, and detecting duplicates is difficult, so ignore it
|
|
|
|
* for now.
|
|
|
|
*/
|
|
|
|
static double
|
|
|
|
get_indexpath_pages(Path *bitmapqual)
|
|
|
|
{
|
|
|
|
double result = 0;
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
if (IsA(bitmapqual, BitmapAndPath))
|
|
|
|
{
|
|
|
|
BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
|
|
|
|
|
|
|
|
foreach(l, apath->bitmapquals)
|
|
|
|
{
|
|
|
|
result += get_indexpath_pages((Path *) lfirst(l));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (IsA(bitmapqual, BitmapOrPath))
|
|
|
|
{
|
|
|
|
BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
|
|
|
|
|
|
|
|
foreach(l, opath->bitmapquals)
|
|
|
|
{
|
|
|
|
result += get_indexpath_pages((Path *) lfirst(l));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (IsA(bitmapqual, IndexPath))
|
|
|
|
{
|
|
|
|
IndexPath *ipath = (IndexPath *) bitmapqual;
|
|
|
|
|
|
|
|
result = (double) ipath->indexinfo->pages;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-04-20 00:35:18 +02:00
|
|
|
/*
|
2005-04-21 21:18:13 +02:00
|
|
|
* cost_bitmap_heap_scan
|
2005-04-20 00:35:18 +02:00
|
|
|
* Determines and returns the cost of scanning a relation using a bitmap
|
|
|
|
* index-then-heap plan.
|
|
|
|
*
|
|
|
|
* 'baserel' is the relation to be scanned
|
2005-04-21 21:18:13 +02:00
|
|
|
* 'bitmapqual' is a tree of IndexPaths, BitmapAndPaths, and BitmapOrPaths
|
2006-07-22 17:41:56 +02:00
|
|
|
* 'outer_rel' is the outer relation when we are considering using the bitmap
|
|
|
|
* scan as the inside of a nestloop join (hence, some of the indexQuals
|
|
|
|
* are join clauses, and we should expect repeated scans of the table);
|
|
|
|
* NULL for a plain bitmap scan
|
2006-06-06 19:59:58 +02:00
|
|
|
*
|
2006-07-22 17:41:56 +02:00
|
|
|
* Note: if this is a join inner path, the component IndexPaths in bitmapqual
|
|
|
|
* should have been costed accordingly.
|
2005-04-20 00:35:18 +02:00
|
|
|
*/
|
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel,
|
2006-07-22 17:41:56 +02:00
|
|
|
Path *bitmapqual, RelOptInfo *outer_rel)
|
2005-04-20 00:35:18 +02:00
|
|
|
{
|
|
|
|
Cost startup_cost = 0;
|
|
|
|
Cost run_cost = 0;
|
2005-04-21 04:28:02 +02:00
|
|
|
Cost indexTotalCost;
|
|
|
|
Selectivity indexSelectivity;
|
|
|
|
Cost cpu_per_tuple;
|
|
|
|
Cost cost_per_page;
|
|
|
|
double tuples_fetched;
|
|
|
|
double pages_fetched;
|
2010-01-05 22:54:00 +01:00
|
|
|
double spc_seq_page_cost,
|
|
|
|
spc_random_page_cost;
|
2005-04-21 04:28:02 +02:00
|
|
|
double T;
|
2005-04-20 00:35:18 +02:00
|
|
|
|
|
|
|
/* Should only be applied to base relations */
|
|
|
|
Assert(IsA(baserel, RelOptInfo));
|
|
|
|
Assert(baserel->relid > 0);
|
|
|
|
Assert(baserel->rtekind == RTE_RELATION);
|
|
|
|
|
2005-04-21 21:18:13 +02:00
|
|
|
if (!enable_bitmapscan)
|
2005-04-21 04:28:02 +02:00
|
|
|
startup_cost += disable_cost;
|
|
|
|
|
|
|
|
/*
|
2005-04-21 21:18:13 +02:00
|
|
|
* Fetch total cost of obtaining the bitmap, as well as its total
|
2005-04-21 04:28:02 +02:00
|
|
|
* selectivity.
|
|
|
|
*/
|
2005-04-21 21:18:13 +02:00
|
|
|
cost_bitmap_tree_node(bitmapqual, &indexTotalCost, &indexSelectivity);
|
2005-04-21 04:28:02 +02:00
|
|
|
|
|
|
|
startup_cost += indexTotalCost;
|
|
|
|
|
2010-01-05 22:54:00 +01:00
|
|
|
/* Fetch estimated page costs for tablespace containing table. */
|
|
|
|
get_tablespace_page_costs(baserel->reltablespace,
|
|
|
|
&spc_random_page_cost,
|
|
|
|
&spc_seq_page_cost);
|
|
|
|
|
2005-04-21 04:28:02 +02:00
|
|
|
/*
|
2006-07-22 17:41:56 +02:00
|
|
|
* Estimate number of main-table pages fetched.
|
2005-04-21 04:28:02 +02:00
|
|
|
*/
|
|
|
|
tuples_fetched = clamp_row_est(indexSelectivity * baserel->tuples);
|
|
|
|
|
|
|
|
T = (baserel->pages > 1) ? (double) baserel->pages : 1.0;
|
2006-07-22 17:41:56 +02:00
|
|
|
|
|
|
|
if (outer_rel != NULL && outer_rel->rows > 1)
|
|
|
|
{
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* For repeated bitmap scans, scale up the number of tuples fetched in
|
|
|
|
* the Mackert and Lohman formula by the number of scans, so that we
|
|
|
|
* estimate the number of pages fetched by all the scans. Then
|
|
|
|
* pro-rate for one scan.
|
2006-07-22 17:41:56 +02:00
|
|
|
*/
|
|
|
|
double num_scans = outer_rel->rows;
|
|
|
|
|
|
|
|
pages_fetched = index_pages_fetched(tuples_fetched * num_scans,
|
|
|
|
baserel->pages,
|
2006-09-20 00:49:53 +02:00
|
|
|
get_indexpath_pages(bitmapqual),
|
|
|
|
root);
|
2006-07-22 17:41:56 +02:00
|
|
|
pages_fetched /= num_scans;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* For a single scan, the number of heap pages that need to be fetched
|
|
|
|
* is the same as the Mackert and Lohman formula for the case T <= b
|
|
|
|
* (ie, no re-reads needed).
|
|
|
|
*/
|
|
|
|
pages_fetched = (2.0 * T * tuples_fetched) / (2.0 * T + tuples_fetched);
|
|
|
|
}
|
2006-06-05 04:49:58 +02:00
|
|
|
if (pages_fetched >= T)
|
2005-04-21 04:28:02 +02:00
|
|
|
pages_fetched = T;
|
2006-06-05 04:49:58 +02:00
|
|
|
else
|
|
|
|
pages_fetched = ceil(pages_fetched);
|
2005-04-21 04:28:02 +02:00
|
|
|
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* For small numbers of pages we should charge spc_random_page_cost
|
|
|
|
* apiece, while if nearly all the table's pages are being read, it's more
|
|
|
|
* appropriate to charge spc_seq_page_cost apiece. The effect is
|
|
|
|
* nonlinear, too. For lack of a better idea, interpolate like this to
|
|
|
|
* determine the cost per page.
|
2005-04-21 04:28:02 +02:00
|
|
|
*/
|
2005-04-22 23:58:32 +02:00
|
|
|
if (pages_fetched >= 2.0)
|
2010-01-05 22:54:00 +01:00
|
|
|
cost_per_page = spc_random_page_cost -
|
|
|
|
(spc_random_page_cost - spc_seq_page_cost)
|
|
|
|
* sqrt(pages_fetched / T);
|
2005-04-22 23:58:32 +02:00
|
|
|
else
|
2010-01-05 22:54:00 +01:00
|
|
|
cost_per_page = spc_random_page_cost;
|
2005-04-21 04:28:02 +02:00
|
|
|
|
|
|
|
run_cost += pages_fetched * cost_per_page;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Estimate CPU costs per tuple.
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Often the indexquals don't need to be rechecked at each tuple ... but
|
|
|
|
* not always, especially not if there are enough tuples involved that the
|
2005-10-15 04:49:52 +02:00
|
|
|
* bitmaps become lossy. For the moment, just assume they will be
|
|
|
|
* rechecked always.
|
2005-04-21 04:28:02 +02:00
|
|
|
*/
|
|
|
|
startup_cost += baserel->baserestrictcost.startup;
|
|
|
|
cpu_per_tuple = cpu_tuple_cost + baserel->baserestrictcost.per_tuple;
|
|
|
|
|
|
|
|
run_cost += cpu_per_tuple * tuples_fetched;
|
2005-04-20 00:35:18 +02:00
|
|
|
|
|
|
|
path->startup_cost = startup_cost;
|
|
|
|
path->total_cost = startup_cost + run_cost;
|
|
|
|
}
|
|
|
|
|
2005-04-21 04:28:02 +02:00
|
|
|
/*
|
2005-04-21 21:18:13 +02:00
|
|
|
* cost_bitmap_tree_node
|
|
|
|
* Extract cost and selectivity from a bitmap tree node (index/and/or)
|
2005-04-21 04:28:02 +02:00
|
|
|
*/
|
2005-04-22 23:58:32 +02:00
|
|
|
void
|
2005-04-21 21:18:13 +02:00
|
|
|
cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec)
|
2005-04-21 04:28:02 +02:00
|
|
|
{
|
2005-04-21 21:18:13 +02:00
|
|
|
if (IsA(path, IndexPath))
|
2005-04-21 04:28:02 +02:00
|
|
|
{
|
2005-04-21 21:18:13 +02:00
|
|
|
*cost = ((IndexPath *) path)->indextotalcost;
|
|
|
|
*selec = ((IndexPath *) path)->indexselectivity;
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2006-12-15 19:42:26 +01:00
|
|
|
/*
|
|
|
|
* Charge a small amount per retrieved tuple to reflect the costs of
|
|
|
|
* manipulating the bitmap. This is mostly to make sure that a bitmap
|
2007-11-15 22:14:46 +01:00
|
|
|
* scan doesn't look to be the same cost as an indexscan to retrieve a
|
|
|
|
* single tuple.
|
2006-12-15 19:42:26 +01:00
|
|
|
*/
|
|
|
|
*cost += 0.1 * cpu_operator_cost * ((IndexPath *) path)->rows;
|
2005-04-21 04:28:02 +02:00
|
|
|
}
|
2005-04-21 21:18:13 +02:00
|
|
|
else if (IsA(path, BitmapAndPath))
|
2005-04-21 04:28:02 +02:00
|
|
|
{
|
2005-04-21 21:18:13 +02:00
|
|
|
*cost = path->total_cost;
|
|
|
|
*selec = ((BitmapAndPath *) path)->bitmapselectivity;
|
2005-04-21 04:28:02 +02:00
|
|
|
}
|
2005-04-21 21:18:13 +02:00
|
|
|
else if (IsA(path, BitmapOrPath))
|
2005-04-21 04:28:02 +02:00
|
|
|
{
|
2005-04-21 21:18:13 +02:00
|
|
|
*cost = path->total_cost;
|
|
|
|
*selec = ((BitmapOrPath *) path)->bitmapselectivity;
|
2005-04-21 04:28:02 +02:00
|
|
|
}
|
|
|
|
else
|
2006-11-11 02:14:19 +01:00
|
|
|
{
|
2005-04-21 21:18:13 +02:00
|
|
|
elog(ERROR, "unrecognized node type: %d", nodeTag(path));
|
2006-11-11 02:14:19 +01:00
|
|
|
*cost = *selec = 0; /* keep compiler quiet */
|
|
|
|
}
|
2005-04-21 21:18:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cost_bitmap_and_node
|
|
|
|
* Estimate the cost of a BitmapAnd node
|
|
|
|
*
|
|
|
|
* Note that this considers only the costs of index scanning and bitmap
|
2005-10-15 04:49:52 +02:00
|
|
|
* creation, not the eventual heap access. In that sense the object isn't
|
2005-04-21 21:18:13 +02:00
|
|
|
* truly a Path, but it has enough path-like properties (costs in particular)
|
|
|
|
* to warrant treating it as one.
|
|
|
|
*/
|
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
cost_bitmap_and_node(BitmapAndPath *path, PlannerInfo *root)
|
2005-04-21 21:18:13 +02:00
|
|
|
{
|
|
|
|
Cost totalCost;
|
2005-10-15 04:49:52 +02:00
|
|
|
Selectivity selec;
|
2005-04-21 21:18:13 +02:00
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We estimate AND selectivity on the assumption that the inputs are
|
|
|
|
* independent. This is probably often wrong, but we don't have the info
|
|
|
|
* to do better.
|
2005-04-21 21:18:13 +02:00
|
|
|
*
|
|
|
|
* The runtime cost of the BitmapAnd itself is estimated at 100x
|
2005-10-15 04:49:52 +02:00
|
|
|
* cpu_operator_cost for each tbm_intersect needed. Probably too small,
|
|
|
|
* definitely too simplistic?
|
2005-04-21 21:18:13 +02:00
|
|
|
*/
|
|
|
|
totalCost = 0.0;
|
|
|
|
selec = 1.0;
|
|
|
|
foreach(l, path->bitmapquals)
|
2005-04-21 04:28:02 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
Path *subpath = (Path *) lfirst(l);
|
|
|
|
Cost subCost;
|
2005-04-21 21:18:13 +02:00
|
|
|
Selectivity subselec;
|
|
|
|
|
|
|
|
cost_bitmap_tree_node(subpath, &subCost, &subselec);
|
|
|
|
|
|
|
|
selec *= subselec;
|
|
|
|
|
|
|
|
totalCost += subCost;
|
|
|
|
if (l != list_head(path->bitmapquals))
|
|
|
|
totalCost += 100.0 * cpu_operator_cost;
|
2005-04-21 04:28:02 +02:00
|
|
|
}
|
2005-04-21 21:18:13 +02:00
|
|
|
path->bitmapselectivity = selec;
|
|
|
|
path->path.startup_cost = totalCost;
|
|
|
|
path->path.total_cost = totalCost;
|
|
|
|
}
|
2005-04-21 04:28:02 +02:00
|
|
|
|
2005-04-21 21:18:13 +02:00
|
|
|
/*
|
|
|
|
* cost_bitmap_or_node
|
|
|
|
* Estimate the cost of a BitmapOr node
|
|
|
|
*
|
|
|
|
* See comments for cost_bitmap_and_node.
|
|
|
|
*/
|
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
cost_bitmap_or_node(BitmapOrPath *path, PlannerInfo *root)
|
2005-04-21 21:18:13 +02:00
|
|
|
{
|
|
|
|
Cost totalCost;
|
2005-10-15 04:49:52 +02:00
|
|
|
Selectivity selec;
|
2005-04-21 21:18:13 +02:00
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We estimate OR selectivity on the assumption that the inputs are
|
|
|
|
* non-overlapping, since that's often the case in "x IN (list)" type
|
|
|
|
* situations. Of course, we clamp to 1.0 at the end.
|
2005-04-21 21:18:13 +02:00
|
|
|
*
|
|
|
|
* The runtime cost of the BitmapOr itself is estimated at 100x
|
2005-10-15 04:49:52 +02:00
|
|
|
* cpu_operator_cost for each tbm_union needed. Probably too small,
|
|
|
|
* definitely too simplistic? We are aware that the tbm_unions are
|
|
|
|
* optimized out when the inputs are BitmapIndexScans.
|
2005-04-21 21:18:13 +02:00
|
|
|
*/
|
|
|
|
totalCost = 0.0;
|
|
|
|
selec = 0.0;
|
|
|
|
foreach(l, path->bitmapquals)
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
Path *subpath = (Path *) lfirst(l);
|
|
|
|
Cost subCost;
|
2005-04-21 21:18:13 +02:00
|
|
|
Selectivity subselec;
|
|
|
|
|
|
|
|
cost_bitmap_tree_node(subpath, &subCost, &subselec);
|
|
|
|
|
|
|
|
selec += subselec;
|
|
|
|
|
|
|
|
totalCost += subCost;
|
|
|
|
if (l != list_head(path->bitmapquals) &&
|
|
|
|
!IsA(subpath, IndexPath))
|
|
|
|
totalCost += 100.0 * cpu_operator_cost;
|
|
|
|
}
|
|
|
|
path->bitmapselectivity = Min(selec, 1.0);
|
|
|
|
path->path.startup_cost = totalCost;
|
|
|
|
path->path.total_cost = totalCost;
|
2005-04-21 04:28:02 +02:00
|
|
|
}
|
|
|
|
|
1999-11-23 21:07:06 +01:00
|
|
|
/*
|
|
|
|
* cost_tidscan
|
2001-06-05 07:26:05 +02:00
|
|
|
* Determines and returns the cost of scanning a relation using TIDs.
|
1999-11-23 21:07:06 +01:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
cost_tidscan(Path *path, PlannerInfo *root,
|
2005-11-26 23:14:57 +01:00
|
|
|
RelOptInfo *baserel, List *tidquals)
|
1999-11-23 21:07:06 +01:00
|
|
|
{
|
2000-02-15 21:49:31 +01:00
|
|
|
Cost startup_cost = 0;
|
|
|
|
Cost run_cost = 0;
|
2007-10-24 20:37:09 +02:00
|
|
|
bool isCurrentOf = false;
|
2000-02-15 21:49:31 +01:00
|
|
|
Cost cpu_per_tuple;
|
2007-06-11 03:16:30 +02:00
|
|
|
QualCost tid_qual_cost;
|
2005-11-26 23:14:57 +01:00
|
|
|
int ntuples;
|
|
|
|
ListCell *l;
|
2010-01-05 22:54:00 +01:00
|
|
|
double spc_random_page_cost;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2002-05-12 22:10:05 +02:00
|
|
|
/* Should only be applied to base relations */
|
2003-02-08 21:20:55 +01:00
|
|
|
Assert(baserel->relid > 0);
|
2002-05-12 22:10:05 +02:00
|
|
|
Assert(baserel->rtekind == RTE_RELATION);
|
|
|
|
|
2005-11-26 23:14:57 +01:00
|
|
|
/* Count how many tuples we expect to retrieve */
|
|
|
|
ntuples = 0;
|
|
|
|
foreach(l, tidquals)
|
|
|
|
{
|
|
|
|
if (IsA(lfirst(l), ScalarArrayOpExpr))
|
|
|
|
{
|
|
|
|
/* Each element of the array yields 1 tuple */
|
|
|
|
ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) lfirst(l);
|
2006-10-04 02:30:14 +02:00
|
|
|
Node *arraynode = (Node *) lsecond(saop->args);
|
2005-11-26 23:14:57 +01:00
|
|
|
|
|
|
|
ntuples += estimate_array_length(arraynode);
|
|
|
|
}
|
2007-10-24 20:37:09 +02:00
|
|
|
else if (IsA(lfirst(l), CurrentOfExpr))
|
|
|
|
{
|
|
|
|
/* CURRENT OF yields 1 tuple */
|
|
|
|
isCurrentOf = true;
|
|
|
|
ntuples++;
|
|
|
|
}
|
2005-11-26 23:14:57 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* It's just CTID = something, count 1 tuple */
|
|
|
|
ntuples++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-24 20:37:09 +02:00
|
|
|
/*
|
|
|
|
* We must force TID scan for WHERE CURRENT OF, because only nodeTidscan.c
|
2007-11-15 22:14:46 +01:00
|
|
|
* understands how to do it correctly. Therefore, honor enable_tidscan
|
2007-10-24 20:37:09 +02:00
|
|
|
* only when CURRENT OF isn't present. Also note that cost_qual_eval
|
|
|
|
* counts a CurrentOfExpr as having startup cost disable_cost, which we
|
|
|
|
* subtract off here; that's to prevent other plan types such as seqscan
|
|
|
|
* from winning.
|
|
|
|
*/
|
|
|
|
if (isCurrentOf)
|
|
|
|
{
|
|
|
|
Assert(baserel->baserestrictcost.startup >= disable_cost);
|
|
|
|
startup_cost -= disable_cost;
|
|
|
|
}
|
|
|
|
else if (!enable_tidscan)
|
|
|
|
startup_cost += disable_cost;
|
|
|
|
|
2007-06-11 03:16:30 +02:00
|
|
|
/*
|
|
|
|
* The TID qual expressions will be computed once, any other baserestrict
|
|
|
|
* quals once per retrived tuple.
|
|
|
|
*/
|
|
|
|
cost_qual_eval(&tid_qual_cost, tidquals, root);
|
|
|
|
|
2010-01-05 22:54:00 +01:00
|
|
|
/* fetch estimated page cost for tablespace containing table */
|
|
|
|
get_tablespace_page_costs(baserel->reltablespace,
|
|
|
|
&spc_random_page_cost,
|
|
|
|
NULL);
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/* disk costs --- assume each tuple on a different page */
|
2010-01-05 22:54:00 +01:00
|
|
|
run_cost += spc_random_page_cost * ntuples;
|
1999-11-23 21:07:06 +01:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/* CPU costs */
|
2007-06-11 03:16:30 +02:00
|
|
|
startup_cost += baserel->baserestrictcost.startup +
|
|
|
|
tid_qual_cost.per_tuple;
|
|
|
|
cpu_per_tuple = cpu_tuple_cost + baserel->baserestrictcost.per_tuple -
|
|
|
|
tid_qual_cost.per_tuple;
|
2000-02-15 21:49:31 +01:00
|
|
|
run_cost += cpu_per_tuple * ntuples;
|
|
|
|
|
|
|
|
path->startup_cost = startup_cost;
|
|
|
|
path->total_cost = startup_cost + run_cost;
|
1999-11-23 21:07:06 +01:00
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2003-07-15 00:35:54 +02:00
|
|
|
/*
|
|
|
|
* cost_subqueryscan
|
|
|
|
* Determines and returns the cost of scanning a subquery RTE.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cost_subqueryscan(Path *path, RelOptInfo *baserel)
|
|
|
|
{
|
|
|
|
Cost startup_cost;
|
|
|
|
Cost run_cost;
|
|
|
|
Cost cpu_per_tuple;
|
|
|
|
|
|
|
|
/* Should only be applied to base relations that are subqueries */
|
|
|
|
Assert(baserel->relid > 0);
|
|
|
|
Assert(baserel->rtekind == RTE_SUBQUERY);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Cost of path is cost of evaluating the subplan, plus cost of evaluating
|
|
|
|
* any restriction clauses that will be attached to the SubqueryScan node,
|
|
|
|
* plus cpu_tuple_cost to account for selection and projection overhead.
|
2003-07-15 00:35:54 +02:00
|
|
|
*/
|
|
|
|
path->startup_cost = baserel->subplan->startup_cost;
|
|
|
|
path->total_cost = baserel->subplan->total_cost;
|
|
|
|
|
|
|
|
startup_cost = baserel->baserestrictcost.startup;
|
|
|
|
cpu_per_tuple = cpu_tuple_cost + baserel->baserestrictcost.per_tuple;
|
|
|
|
run_cost = cpu_per_tuple * baserel->tuples;
|
|
|
|
|
|
|
|
path->startup_cost += startup_cost;
|
|
|
|
path->total_cost += startup_cost + run_cost;
|
|
|
|
}
|
|
|
|
|
2002-05-12 22:10:05 +02:00
|
|
|
/*
|
|
|
|
* cost_functionscan
|
|
|
|
* Determines and returns the cost of scanning a function RTE.
|
|
|
|
*/
|
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
cost_functionscan(Path *path, PlannerInfo *root, RelOptInfo *baserel)
|
2002-05-12 22:10:05 +02:00
|
|
|
{
|
|
|
|
Cost startup_cost = 0;
|
|
|
|
Cost run_cost = 0;
|
|
|
|
Cost cpu_per_tuple;
|
2007-01-22 02:35:23 +01:00
|
|
|
RangeTblEntry *rte;
|
|
|
|
QualCost exprcost;
|
2002-05-12 22:10:05 +02:00
|
|
|
|
|
|
|
/* Should only be applied to base relations that are functions */
|
2003-02-08 21:20:55 +01:00
|
|
|
Assert(baserel->relid > 0);
|
2007-04-21 23:01:45 +02:00
|
|
|
rte = planner_rt_fetch(baserel->relid, root);
|
2007-01-22 02:35:23 +01:00
|
|
|
Assert(rte->rtekind == RTE_FUNCTION);
|
2002-05-12 22:10:05 +02:00
|
|
|
|
2009-09-13 00:12:09 +02:00
|
|
|
/*
|
|
|
|
* Estimate costs of executing the function expression.
|
|
|
|
*
|
|
|
|
* Currently, nodeFunctionscan.c always executes the function to
|
|
|
|
* completion before returning any rows, and caches the results in a
|
2010-02-26 03:01:40 +01:00
|
|
|
* tuplestore. So the function eval cost is all startup cost, and per-row
|
|
|
|
* costs are minimal.
|
2009-09-13 00:12:09 +02:00
|
|
|
*
|
|
|
|
* XXX in principle we ought to charge tuplestore spill costs if the
|
|
|
|
* number of rows is large. However, given how phony our rowcount
|
2010-02-26 03:01:40 +01:00
|
|
|
* estimates for functions tend to be, there's not a lot of point in that
|
|
|
|
* refinement right now.
|
2009-09-13 00:12:09 +02:00
|
|
|
*/
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval_node(&exprcost, rte->funcexpr, root);
|
2007-01-22 02:35:23 +01:00
|
|
|
|
2009-09-13 00:12:09 +02:00
|
|
|
startup_cost += exprcost.startup + exprcost.per_tuple;
|
2002-05-12 22:10:05 +02:00
|
|
|
|
|
|
|
/* Add scanning CPU costs */
|
2003-01-12 23:35:29 +01:00
|
|
|
startup_cost += baserel->baserestrictcost.startup;
|
2009-09-13 00:12:09 +02:00
|
|
|
cpu_per_tuple = cpu_tuple_cost + baserel->baserestrictcost.per_tuple;
|
2002-05-12 22:10:05 +02:00
|
|
|
run_cost += cpu_per_tuple * baserel->tuples;
|
|
|
|
|
|
|
|
path->startup_cost = startup_cost;
|
|
|
|
path->total_cost = startup_cost + run_cost;
|
|
|
|
}
|
|
|
|
|
2006-08-02 03:59:48 +02:00
|
|
|
/*
|
|
|
|
* cost_valuesscan
|
|
|
|
* Determines and returns the cost of scanning a VALUES RTE.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cost_valuesscan(Path *path, PlannerInfo *root, RelOptInfo *baserel)
|
|
|
|
{
|
|
|
|
Cost startup_cost = 0;
|
|
|
|
Cost run_cost = 0;
|
|
|
|
Cost cpu_per_tuple;
|
|
|
|
|
|
|
|
/* Should only be applied to base relations that are values lists */
|
|
|
|
Assert(baserel->relid > 0);
|
|
|
|
Assert(baserel->rtekind == RTE_VALUES);
|
|
|
|
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* For now, estimate list evaluation cost at one operator eval per list
|
|
|
|
* (probably pretty bogus, but is it worth being smarter?)
|
2006-08-02 03:59:48 +02:00
|
|
|
*/
|
|
|
|
cpu_per_tuple = cpu_operator_cost;
|
|
|
|
|
|
|
|
/* Add scanning CPU costs */
|
|
|
|
startup_cost += baserel->baserestrictcost.startup;
|
|
|
|
cpu_per_tuple += cpu_tuple_cost + baserel->baserestrictcost.per_tuple;
|
|
|
|
run_cost += cpu_per_tuple * baserel->tuples;
|
|
|
|
|
|
|
|
path->startup_cost = startup_cost;
|
|
|
|
path->total_cost = startup_cost + run_cost;
|
|
|
|
}
|
|
|
|
|
2008-10-04 23:56:55 +02:00
|
|
|
/*
|
|
|
|
* cost_ctescan
|
|
|
|
* Determines and returns the cost of scanning a CTE RTE.
|
|
|
|
*
|
|
|
|
* Note: this is used for both self-reference and regular CTEs; the
|
|
|
|
* possible cost differences are below the threshold of what we could
|
2009-06-11 16:49:15 +02:00
|
|
|
* estimate accurately anyway. Note that the costs of evaluating the
|
2008-10-04 23:56:55 +02:00
|
|
|
* referenced CTE query are added into the final plan as initplan costs,
|
|
|
|
* and should NOT be counted here.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cost_ctescan(Path *path, PlannerInfo *root, RelOptInfo *baserel)
|
|
|
|
{
|
|
|
|
Cost startup_cost = 0;
|
|
|
|
Cost run_cost = 0;
|
|
|
|
Cost cpu_per_tuple;
|
|
|
|
|
|
|
|
/* Should only be applied to base relations that are CTEs */
|
|
|
|
Assert(baserel->relid > 0);
|
|
|
|
Assert(baserel->rtekind == RTE_CTE);
|
|
|
|
|
|
|
|
/* Charge one CPU tuple cost per row for tuplestore manipulation */
|
|
|
|
cpu_per_tuple = cpu_tuple_cost;
|
|
|
|
|
|
|
|
/* Add scanning CPU costs */
|
|
|
|
startup_cost += baserel->baserestrictcost.startup;
|
|
|
|
cpu_per_tuple += cpu_tuple_cost + baserel->baserestrictcost.per_tuple;
|
|
|
|
run_cost += cpu_per_tuple * baserel->tuples;
|
|
|
|
|
|
|
|
path->startup_cost = startup_cost;
|
|
|
|
path->total_cost = startup_cost + run_cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cost_recursive_union
|
|
|
|
* Determines and returns the cost of performing a recursive union,
|
|
|
|
* and also the estimated output size.
|
|
|
|
*
|
|
|
|
* We are given Plans for the nonrecursive and recursive terms.
|
|
|
|
*
|
|
|
|
* Note that the arguments and output are Plans, not Paths as in most of
|
|
|
|
* the rest of this module. That's because we don't bother setting up a
|
|
|
|
* Path representation for recursive union --- we have only one way to do it.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cost_recursive_union(Plan *runion, Plan *nrterm, Plan *rterm)
|
|
|
|
{
|
|
|
|
Cost startup_cost;
|
|
|
|
Cost total_cost;
|
|
|
|
double total_rows;
|
|
|
|
|
|
|
|
/* We probably have decent estimates for the non-recursive term */
|
|
|
|
startup_cost = nrterm->startup_cost;
|
|
|
|
total_cost = nrterm->total_cost;
|
|
|
|
total_rows = nrterm->plan_rows;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We arbitrarily assume that about 10 recursive iterations will be
|
2009-06-11 16:49:15 +02:00
|
|
|
* needed, and that we've managed to get a good fix on the cost and output
|
|
|
|
* size of each one of them. These are mighty shaky assumptions but it's
|
|
|
|
* hard to see how to do better.
|
2008-10-04 23:56:55 +02:00
|
|
|
*/
|
|
|
|
total_cost += 10 * rterm->total_cost;
|
|
|
|
total_rows += 10 * rterm->plan_rows;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Also charge cpu_tuple_cost per row to account for the costs of
|
|
|
|
* manipulating the tuplestores. (We don't worry about possible
|
|
|
|
* spill-to-disk costs.)
|
|
|
|
*/
|
|
|
|
total_cost += cpu_tuple_cost * total_rows;
|
|
|
|
|
|
|
|
runion->startup_cost = startup_cost;
|
|
|
|
runion->total_cost = total_cost;
|
|
|
|
runion->plan_rows = total_rows;
|
|
|
|
runion->plan_width = Max(nrterm->plan_width, rterm->plan_width);
|
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* cost_sort
|
2002-11-21 01:42:20 +01:00
|
|
|
* Determines and returns the cost of sorting a relation, including
|
|
|
|
* the cost of reading the input data.
|
2000-02-15 21:49:31 +01:00
|
|
|
*
|
2010-10-08 02:00:28 +02:00
|
|
|
* If the total volume of data to sort is less than sort_mem, we will do
|
2000-01-09 01:26:47 +01:00
|
|
|
* an in-memory sort, which requires no I/O and about t*log2(t) tuple
|
2000-02-15 21:49:31 +01:00
|
|
|
* comparisons for t tuples.
|
2000-01-09 01:26:47 +01:00
|
|
|
*
|
2010-10-08 02:00:28 +02:00
|
|
|
* If the total volume exceeds sort_mem, we switch to a tape-style merge
|
2000-01-09 01:26:47 +01:00
|
|
|
* algorithm. There will still be about t*log2(t) tuple comparisons in
|
|
|
|
* total, but we will also need to write and read each tuple once per
|
2006-10-04 02:30:14 +02:00
|
|
|
* merge pass. We expect about ceil(logM(r)) merge passes where r is the
|
2006-02-19 06:54:06 +01:00
|
|
|
* number of initial runs formed and M is the merge order used by tuplesort.c.
|
2010-10-08 02:00:28 +02:00
|
|
|
* Since the average initial run should be about twice sort_mem, we have
|
|
|
|
* disk traffic = 2 * relsize * ceil(logM(p / (2*sort_mem)))
|
2000-02-15 21:49:31 +01:00
|
|
|
* cpu = comparison_cost * t * log2(t)
|
|
|
|
*
|
2007-05-04 03:13:45 +02:00
|
|
|
* If the sort is bounded (i.e., only the first k result tuples are needed)
|
2010-10-08 02:00:28 +02:00
|
|
|
* and k tuples can fit into sort_mem, we use a heap method that keeps only
|
2007-05-04 03:13:45 +02:00
|
|
|
* k tuples in the heap; this will require about t*log2(k) tuple comparisons.
|
|
|
|
*
|
2006-06-05 22:56:33 +02:00
|
|
|
* The disk traffic is assumed to be 3/4ths sequential and 1/4th random
|
2000-02-15 21:49:31 +01:00
|
|
|
* accesses (XXX can't we refine that guess?)
|
|
|
|
*
|
2010-10-08 02:00:28 +02:00
|
|
|
* By default, we charge two operator evals per tuple comparison, which should
|
|
|
|
* be in the right ballpark in most cases. The caller can tweak this by
|
|
|
|
* specifying nonzero comparison_cost; typically that's used for any extra
|
|
|
|
* work that has to be done to prepare the inputs to the comparison operators.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1999-02-10 04:52:54 +01:00
|
|
|
* 'pathkeys' is a list of sort keys
|
2002-11-21 01:42:20 +01:00
|
|
|
* 'input_cost' is the total cost for reading the input data
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'tuples' is the number of tuples in the relation
|
|
|
|
* 'width' is the average tuple width in bytes
|
2010-10-08 02:00:28 +02:00
|
|
|
* 'comparison_cost' is the extra cost per comparison, if any
|
|
|
|
* 'sort_mem' is the number of kilobytes of work memory allowed for the sort
|
2007-05-04 03:13:45 +02:00
|
|
|
* 'limit_tuples' is the bound on the number of output tuples; -1 if no bound
|
1999-04-30 06:01:44 +02:00
|
|
|
*
|
2000-01-09 01:26:47 +01:00
|
|
|
* NOTE: some callers currently pass NIL for pathkeys because they
|
1999-05-01 21:47:42 +02:00
|
|
|
* can't conveniently supply the sort keys. Since this routine doesn't
|
1999-04-30 06:01:44 +02:00
|
|
|
* currently do anything with pathkeys anyway, that doesn't matter...
|
1999-05-01 21:47:42 +02:00
|
|
|
* but if it ever does, it should react gracefully to lack of key data.
|
2002-11-21 01:42:20 +01:00
|
|
|
* (Actually, the thing we'd most likely be interested in is just the number
|
|
|
|
* of sort keys, which all callers *could* supply.)
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
cost_sort(Path *path, PlannerInfo *root,
|
2007-05-04 03:13:45 +02:00
|
|
|
List *pathkeys, Cost input_cost, double tuples, int width,
|
2010-10-08 02:00:28 +02:00
|
|
|
Cost comparison_cost, int sort_mem,
|
2007-05-04 03:13:45 +02:00
|
|
|
double limit_tuples)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-11-21 01:42:20 +01:00
|
|
|
Cost startup_cost = input_cost;
|
2000-02-15 21:49:31 +01:00
|
|
|
Cost run_cost = 0;
|
2007-05-04 03:13:45 +02:00
|
|
|
double input_bytes = relation_byte_size(tuples, width);
|
|
|
|
double output_bytes;
|
|
|
|
double output_tuples;
|
2010-10-08 02:00:28 +02:00
|
|
|
long sort_mem_bytes = sort_mem * 1024L;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-01-23 00:50:30 +01:00
|
|
|
if (!enable_sort)
|
2000-02-15 21:49:31 +01:00
|
|
|
startup_cost += disable_cost;
|
1999-04-30 06:01:44 +02:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We want to be sure the cost of a sort is never estimated as zero, even
|
|
|
|
* if passed-in tuple count is zero. Besides, mustn't do log(0)...
|
1999-04-30 06:01:44 +02:00
|
|
|
*/
|
2000-01-09 01:26:47 +01:00
|
|
|
if (tuples < 2.0)
|
|
|
|
tuples = 2.0;
|
1999-04-30 06:01:44 +02:00
|
|
|
|
2010-10-08 02:00:28 +02:00
|
|
|
/* Include the default cost-per-comparison */
|
|
|
|
comparison_cost += 2.0 * cpu_operator_cost;
|
|
|
|
|
2007-05-04 03:13:45 +02:00
|
|
|
/* Do we have a useful LIMIT? */
|
|
|
|
if (limit_tuples > 0 && limit_tuples < tuples)
|
|
|
|
{
|
|
|
|
output_tuples = limit_tuples;
|
|
|
|
output_bytes = relation_byte_size(output_tuples, width);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
output_tuples = tuples;
|
|
|
|
output_bytes = input_bytes;
|
|
|
|
}
|
1999-04-30 06:01:44 +02:00
|
|
|
|
2010-10-08 02:00:28 +02:00
|
|
|
if (output_bytes > sort_mem_bytes)
|
2000-01-09 01:26:47 +01:00
|
|
|
{
|
2007-05-04 03:13:45 +02:00
|
|
|
/*
|
|
|
|
* We'll have to use a disk-based sort of all the tuples
|
|
|
|
*/
|
|
|
|
double npages = ceil(input_bytes / BLCKSZ);
|
2010-10-08 02:00:28 +02:00
|
|
|
double nruns = (input_bytes / sort_mem_bytes) * 0.5;
|
|
|
|
double mergeorder = tuplesort_merge_order(sort_mem_bytes);
|
2006-02-19 06:54:06 +01:00
|
|
|
double log_runs;
|
2000-02-15 21:49:31 +01:00
|
|
|
double npageaccesses;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2007-05-04 03:13:45 +02:00
|
|
|
/*
|
|
|
|
* CPU costs
|
|
|
|
*
|
2010-10-08 02:00:28 +02:00
|
|
|
* Assume about N log2 N comparisons
|
2007-05-04 03:13:45 +02:00
|
|
|
*/
|
2010-10-08 02:00:28 +02:00
|
|
|
startup_cost += comparison_cost * tuples * LOG2(tuples);
|
2007-05-04 03:13:45 +02:00
|
|
|
|
|
|
|
/* Disk costs */
|
|
|
|
|
2006-02-19 06:54:06 +01:00
|
|
|
/* Compute logM(r) as log(r) / log(M) */
|
|
|
|
if (nruns > mergeorder)
|
|
|
|
log_runs = ceil(log(nruns) / log(mergeorder));
|
|
|
|
else
|
2000-01-09 01:26:47 +01:00
|
|
|
log_runs = 1.0;
|
2000-02-15 21:49:31 +01:00
|
|
|
npageaccesses = 2.0 * npages * log_runs;
|
2006-06-05 22:56:33 +02:00
|
|
|
/* Assume 3/4ths of accesses are sequential, 1/4th are not */
|
2000-02-15 21:49:31 +01:00
|
|
|
startup_cost += npageaccesses *
|
2006-06-05 22:56:33 +02:00
|
|
|
(seq_page_cost * 0.75 + random_page_cost * 0.25);
|
2000-01-09 01:26:47 +01:00
|
|
|
}
|
2010-10-08 02:00:28 +02:00
|
|
|
else if (tuples > 2 * output_tuples || input_bytes > sort_mem_bytes)
|
2007-05-04 03:13:45 +02:00
|
|
|
{
|
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* We'll use a bounded heap-sort keeping just K tuples in memory, for
|
|
|
|
* a total number of tuple comparisons of N log2 K; but the constant
|
|
|
|
* factor is a bit higher than for quicksort. Tweak it so that the
|
|
|
|
* cost curve is continuous at the crossover point.
|
2007-05-04 03:13:45 +02:00
|
|
|
*/
|
2010-10-08 02:00:28 +02:00
|
|
|
startup_cost += comparison_cost * tuples * LOG2(2.0 * output_tuples);
|
2007-05-04 03:13:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We'll use plain quicksort on all the input tuples */
|
2010-10-08 02:00:28 +02:00
|
|
|
startup_cost += comparison_cost * tuples * LOG2(tuples);
|
2007-05-04 03:13:45 +02:00
|
|
|
}
|
1999-04-30 06:01:44 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Also charge a small amount (arbitrarily set equal to operator cost) per
|
2010-02-19 22:49:10 +01:00
|
|
|
* extracted tuple. We don't charge cpu_tuple_cost because a Sort node
|
|
|
|
* doesn't do qual-checking or projection, so it has less overhead than
|
|
|
|
* most plan nodes. Note it's correct to use tuples not output_tuples
|
2007-05-04 03:13:45 +02:00
|
|
|
* here --- the upper LIMIT will pro-rate the run cost so we'd be double
|
|
|
|
* counting the LIMIT otherwise.
|
2000-02-15 21:49:31 +01:00
|
|
|
*/
|
2002-03-01 21:50:20 +01:00
|
|
|
run_cost += cpu_operator_cost * tuples;
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
path->startup_cost = startup_cost;
|
|
|
|
path->total_cost = startup_cost + run_cost;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2010-10-14 22:56:39 +02:00
|
|
|
/*
|
|
|
|
* cost_merge_append
|
|
|
|
* Determines and returns the cost of a MergeAppend node.
|
|
|
|
*
|
|
|
|
* MergeAppend merges several pre-sorted input streams, using a heap that
|
|
|
|
* at any given instant holds the next tuple from each stream. If there
|
|
|
|
* are N streams, we need about N*log2(N) tuple comparisons to construct
|
|
|
|
* the heap at startup, and then for each output tuple, about log2(N)
|
|
|
|
* comparisons to delete the top heap entry and another log2(N) comparisons
|
|
|
|
* to insert its successor from the same stream.
|
|
|
|
*
|
|
|
|
* (The effective value of N will drop once some of the input streams are
|
|
|
|
* exhausted, but it seems unlikely to be worth trying to account for that.)
|
|
|
|
*
|
|
|
|
* The heap is never spilled to disk, since we assume N is not very large.
|
|
|
|
* So this is much simpler than cost_sort.
|
|
|
|
*
|
|
|
|
* As in cost_sort, we charge two operator evals per tuple comparison.
|
|
|
|
*
|
|
|
|
* 'pathkeys' is a list of sort keys
|
|
|
|
* 'n_streams' is the number of input streams
|
|
|
|
* 'input_startup_cost' is the sum of the input streams' startup costs
|
|
|
|
* 'input_total_cost' is the sum of the input streams' total costs
|
|
|
|
* 'tuples' is the number of tuples in all the streams
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cost_merge_append(Path *path, PlannerInfo *root,
|
|
|
|
List *pathkeys, int n_streams,
|
|
|
|
Cost input_startup_cost, Cost input_total_cost,
|
|
|
|
double tuples)
|
|
|
|
{
|
|
|
|
Cost startup_cost = 0;
|
|
|
|
Cost run_cost = 0;
|
|
|
|
Cost comparison_cost;
|
|
|
|
double N;
|
|
|
|
double logN;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid log(0)...
|
|
|
|
*/
|
|
|
|
N = (n_streams < 2) ? 2.0 : (double) n_streams;
|
|
|
|
logN = LOG2(N);
|
|
|
|
|
|
|
|
/* Assumed cost per tuple comparison */
|
|
|
|
comparison_cost = 2.0 * cpu_operator_cost;
|
|
|
|
|
|
|
|
/* Heap creation cost */
|
|
|
|
startup_cost += comparison_cost * N * logN;
|
|
|
|
|
|
|
|
/* Per-tuple heap maintenance cost */
|
|
|
|
run_cost += tuples * comparison_cost * 2.0 * logN;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Also charge a small amount (arbitrarily set equal to operator cost) per
|
|
|
|
* extracted tuple. We don't charge cpu_tuple_cost because a MergeAppend
|
|
|
|
* node doesn't do qual-checking or projection, so it has less overhead
|
|
|
|
* than most plan nodes.
|
|
|
|
*/
|
|
|
|
run_cost += cpu_operator_cost * tuples;
|
|
|
|
|
|
|
|
path->startup_cost = startup_cost + input_startup_cost;
|
|
|
|
path->total_cost = startup_cost + run_cost + input_total_cost;
|
|
|
|
}
|
|
|
|
|
2002-11-30 06:21:03 +01:00
|
|
|
/*
|
|
|
|
* cost_material
|
|
|
|
* Determines and returns the cost of materializing a relation, including
|
|
|
|
* the cost of reading the input data.
|
|
|
|
*
|
2004-02-03 18:34:04 +01:00
|
|
|
* If the total volume of data to materialize exceeds work_mem, we will need
|
2002-11-30 06:21:03 +01:00
|
|
|
* to write it to disk, so the cost is much higher in that case.
|
2009-09-13 00:12:09 +02:00
|
|
|
*
|
|
|
|
* Note that here we are estimating the costs for the first scan of the
|
|
|
|
* relation, so the materialization is all overhead --- any savings will
|
|
|
|
* occur only on rescan, which is estimated in cost_rescan.
|
2002-11-30 06:21:03 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
cost_material(Path *path,
|
2009-09-13 00:12:09 +02:00
|
|
|
Cost input_startup_cost, Cost input_total_cost,
|
|
|
|
double tuples, int width)
|
2002-11-30 06:21:03 +01:00
|
|
|
{
|
2009-09-13 00:12:09 +02:00
|
|
|
Cost startup_cost = input_startup_cost;
|
|
|
|
Cost run_cost = input_total_cost - input_startup_cost;
|
2002-11-30 06:21:03 +01:00
|
|
|
double nbytes = relation_byte_size(tuples, width);
|
2004-02-03 18:34:04 +01:00
|
|
|
long work_mem_bytes = work_mem * 1024L;
|
2002-11-30 06:21:03 +01:00
|
|
|
|
2009-09-13 00:12:09 +02:00
|
|
|
/*
|
2010-02-19 22:49:10 +01:00
|
|
|
* Whether spilling or not, charge 2x cpu_operator_cost per tuple to
|
|
|
|
* reflect bookkeeping overhead. (This rate must be more than what
|
|
|
|
* cost_rescan charges for materialize, ie, cpu_operator_cost per tuple;
|
2009-09-13 00:12:09 +02:00
|
|
|
* if it is exactly the same then there will be a cost tie between
|
|
|
|
* nestloop with A outer, materialized B inner and nestloop with B outer,
|
|
|
|
* materialized A inner. The extra cost ensures we'll prefer
|
2010-02-26 03:01:40 +01:00
|
|
|
* materializing the smaller rel.) Note that this is normally a good deal
|
2010-02-19 22:49:10 +01:00
|
|
|
* less than cpu_tuple_cost; which is OK because a Material plan node
|
|
|
|
* doesn't do qual-checking or projection, so it's got less overhead than
|
|
|
|
* most plan nodes.
|
2009-09-13 00:12:09 +02:00
|
|
|
*/
|
2010-02-19 22:49:10 +01:00
|
|
|
run_cost += 2 * cpu_operator_cost * tuples;
|
2009-09-13 00:12:09 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we will spill to disk, charge at the rate of seq_page_cost per page.
|
|
|
|
* This cost is assumed to be evenly spread through the plan run phase,
|
|
|
|
* which isn't exactly accurate but our cost model doesn't allow for
|
|
|
|
* nonuniform costs within the run phase.
|
|
|
|
*/
|
2004-02-03 18:34:04 +01:00
|
|
|
if (nbytes > work_mem_bytes)
|
2002-11-30 06:21:03 +01:00
|
|
|
{
|
|
|
|
double npages = ceil(nbytes / BLCKSZ);
|
|
|
|
|
2006-06-05 04:49:58 +02:00
|
|
|
run_cost += seq_page_cost * npages;
|
2002-11-30 06:21:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
path->startup_cost = startup_cost;
|
|
|
|
path->total_cost = startup_cost + run_cost;
|
|
|
|
}
|
|
|
|
|
2002-11-21 01:42:20 +01:00
|
|
|
/*
|
|
|
|
* cost_agg
|
|
|
|
* Determines and returns the cost of performing an Agg plan node,
|
|
|
|
* including the cost of its input.
|
|
|
|
*
|
|
|
|
* Note: when aggstrategy == AGG_SORTED, caller must ensure that input costs
|
|
|
|
* are for appropriately-sorted input.
|
|
|
|
*/
|
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
cost_agg(Path *path, PlannerInfo *root,
|
2002-11-21 01:42:20 +01:00
|
|
|
AggStrategy aggstrategy, int numAggs,
|
|
|
|
int numGroupCols, double numGroups,
|
|
|
|
Cost input_startup_cost, Cost input_total_cost,
|
|
|
|
double input_tuples)
|
|
|
|
{
|
|
|
|
Cost startup_cost;
|
|
|
|
Cost total_cost;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We charge one cpu_operator_cost per aggregate function per input tuple,
|
|
|
|
* and another one per output tuple (corresponding to transfn and finalfn
|
|
|
|
* calls respectively). If we are grouping, we charge an additional
|
|
|
|
* cpu_operator_cost per grouping column per input tuple for grouping
|
|
|
|
* comparisons.
|
2002-11-21 01:42:20 +01:00
|
|
|
*
|
2003-08-04 02:43:34 +02:00
|
|
|
* We will produce a single output tuple if not grouping, and a tuple per
|
2005-08-28 00:37:00 +02:00
|
|
|
* group otherwise. We charge cpu_tuple_cost for each output tuple.
|
2003-02-15 22:39:58 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Note: in this cost model, AGG_SORTED and AGG_HASHED have exactly the
|
|
|
|
* same total CPU cost, but AGG_SORTED has lower startup cost. If the
|
|
|
|
* input path is already sorted appropriately, AGG_SORTED should be
|
|
|
|
* preferred (since it has no risk of memory overflow). This will happen
|
|
|
|
* as long as the computed total costs are indeed exactly equal --- but if
|
|
|
|
* there's roundoff error we might do the wrong thing. So be sure that
|
|
|
|
* the computations below form the same intermediate values in the same
|
|
|
|
* order.
|
2007-01-22 02:35:23 +01:00
|
|
|
*
|
|
|
|
* Note: ideally we should use the pg_proc.procost costs of each
|
|
|
|
* aggregate's component functions, but for now that seems like an
|
|
|
|
* excessive amount of work.
|
2002-11-21 01:42:20 +01:00
|
|
|
*/
|
|
|
|
if (aggstrategy == AGG_PLAIN)
|
|
|
|
{
|
|
|
|
startup_cost = input_total_cost;
|
|
|
|
startup_cost += cpu_operator_cost * (input_tuples + 1) * numAggs;
|
|
|
|
/* we aren't grouping */
|
2005-08-28 00:37:00 +02:00
|
|
|
total_cost = startup_cost + cpu_tuple_cost;
|
2002-11-21 01:42:20 +01:00
|
|
|
}
|
|
|
|
else if (aggstrategy == AGG_SORTED)
|
|
|
|
{
|
|
|
|
/* Here we are able to deliver output on-the-fly */
|
|
|
|
startup_cost = input_startup_cost;
|
|
|
|
total_cost = input_total_cost;
|
2003-02-15 22:39:58 +01:00
|
|
|
/* calcs phrased this way to match HASHED case, see note above */
|
2002-11-21 01:42:20 +01:00
|
|
|
total_cost += cpu_operator_cost * input_tuples * numGroupCols;
|
2003-02-15 22:39:58 +01:00
|
|
|
total_cost += cpu_operator_cost * input_tuples * numAggs;
|
|
|
|
total_cost += cpu_operator_cost * numGroups * numAggs;
|
2005-08-28 00:37:00 +02:00
|
|
|
total_cost += cpu_tuple_cost * numGroups;
|
2002-11-21 01:42:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* must be AGG_HASHED */
|
|
|
|
startup_cost = input_total_cost;
|
|
|
|
startup_cost += cpu_operator_cost * input_tuples * numGroupCols;
|
2003-02-15 22:39:58 +01:00
|
|
|
startup_cost += cpu_operator_cost * input_tuples * numAggs;
|
2002-11-21 01:42:20 +01:00
|
|
|
total_cost = startup_cost;
|
|
|
|
total_cost += cpu_operator_cost * numGroups * numAggs;
|
2005-08-28 00:37:00 +02:00
|
|
|
total_cost += cpu_tuple_cost * numGroups;
|
2002-11-21 01:42:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
path->startup_cost = startup_cost;
|
|
|
|
path->total_cost = total_cost;
|
|
|
|
}
|
|
|
|
|
2008-12-28 19:54:01 +01:00
|
|
|
/*
|
|
|
|
* cost_windowagg
|
|
|
|
* Determines and returns the cost of performing a WindowAgg plan node,
|
|
|
|
* including the cost of its input.
|
|
|
|
*
|
|
|
|
* Input is assumed already properly sorted.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cost_windowagg(Path *path, PlannerInfo *root,
|
|
|
|
int numWindowFuncs, int numPartCols, int numOrderCols,
|
|
|
|
Cost input_startup_cost, Cost input_total_cost,
|
|
|
|
double input_tuples)
|
|
|
|
{
|
|
|
|
Cost startup_cost;
|
|
|
|
Cost total_cost;
|
|
|
|
|
|
|
|
startup_cost = input_startup_cost;
|
|
|
|
total_cost = input_total_cost;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We charge one cpu_operator_cost per window function per tuple (often a
|
|
|
|
* drastic underestimate, but without a way to gauge how many tuples the
|
|
|
|
* window function will fetch, it's hard to do better). We also charge
|
|
|
|
* cpu_operator_cost per grouping column per tuple for grouping
|
|
|
|
* comparisons, plus cpu_tuple_cost per tuple for general overhead.
|
|
|
|
*/
|
|
|
|
total_cost += cpu_operator_cost * input_tuples * numWindowFuncs;
|
|
|
|
total_cost += cpu_operator_cost * input_tuples * (numPartCols + numOrderCols);
|
|
|
|
total_cost += cpu_tuple_cost * input_tuples;
|
|
|
|
|
|
|
|
path->startup_cost = startup_cost;
|
|
|
|
path->total_cost = total_cost;
|
|
|
|
}
|
|
|
|
|
2002-11-21 01:42:20 +01:00
|
|
|
/*
|
|
|
|
* cost_group
|
|
|
|
* Determines and returns the cost of performing a Group plan node,
|
|
|
|
* including the cost of its input.
|
|
|
|
*
|
|
|
|
* Note: caller must ensure that input costs are for appropriately-sorted
|
|
|
|
* input.
|
|
|
|
*/
|
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
cost_group(Path *path, PlannerInfo *root,
|
2002-11-21 01:42:20 +01:00
|
|
|
int numGroupCols, double numGroups,
|
|
|
|
Cost input_startup_cost, Cost input_total_cost,
|
|
|
|
double input_tuples)
|
|
|
|
{
|
|
|
|
Cost startup_cost;
|
|
|
|
Cost total_cost;
|
|
|
|
|
|
|
|
startup_cost = input_startup_cost;
|
|
|
|
total_cost = input_total_cost;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Charge one cpu_operator_cost per comparison per input tuple. We assume
|
|
|
|
* all columns get compared at most of the tuples.
|
2002-11-21 01:42:20 +01:00
|
|
|
*/
|
|
|
|
total_cost += cpu_operator_cost * input_tuples * numGroupCols;
|
|
|
|
|
|
|
|
path->startup_cost = startup_cost;
|
|
|
|
path->total_cost = total_cost;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2006-02-05 03:59:17 +01:00
|
|
|
/*
|
|
|
|
* If a nestloop's inner path is an indexscan, be sure to use its estimated
|
|
|
|
* output row count, which may be lower than the restriction-clause-only row
|
|
|
|
* count of its parent. (We don't include this case in the PATH_ROWS macro
|
|
|
|
* because it applies *only* to a nestloop's inner relation.) We have to
|
2010-10-14 22:56:39 +02:00
|
|
|
* be prepared to recurse through Append or MergeAppend nodes in case of an
|
|
|
|
* appendrel. (It's not clear MergeAppend can be seen here, but we may as
|
|
|
|
* well handle it if so.)
|
2006-02-05 03:59:17 +01:00
|
|
|
*/
|
|
|
|
static double
|
|
|
|
nestloop_inner_path_rows(Path *path)
|
|
|
|
{
|
|
|
|
double result;
|
|
|
|
|
|
|
|
if (IsA(path, IndexPath))
|
|
|
|
result = ((IndexPath *) path)->rows;
|
|
|
|
else if (IsA(path, BitmapHeapPath))
|
|
|
|
result = ((BitmapHeapPath *) path)->rows;
|
|
|
|
else if (IsA(path, AppendPath))
|
|
|
|
{
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
result = 0;
|
|
|
|
foreach(l, ((AppendPath *) path)->subpaths)
|
|
|
|
{
|
|
|
|
result += nestloop_inner_path_rows((Path *) lfirst(l));
|
|
|
|
}
|
|
|
|
}
|
2010-10-14 22:56:39 +02:00
|
|
|
else if (IsA(path, MergeAppendPath))
|
|
|
|
{
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
result = 0;
|
|
|
|
foreach(l, ((MergeAppendPath *) path)->subpaths)
|
|
|
|
{
|
|
|
|
result += nestloop_inner_path_rows((Path *) lfirst(l));
|
|
|
|
}
|
|
|
|
}
|
2006-02-05 03:59:17 +01:00
|
|
|
else
|
|
|
|
result = PATH_ROWS(path);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* cost_nestloop
|
1997-09-07 07:04:48 +02:00
|
|
|
* Determines and returns the cost of joining two relations using the
|
|
|
|
* nested loop algorithm.
|
|
|
|
*
|
2003-01-27 21:51:54 +01:00
|
|
|
* 'path' is already filled in except for the cost fields
|
2008-08-14 20:48:00 +02:00
|
|
|
* 'sjinfo' is extra info about the join for selectivity estimation
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
void
|
2008-08-14 20:48:00 +02:00
|
|
|
cost_nestloop(NestPath *path, PlannerInfo *root, SpecialJoinInfo *sjinfo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-01-27 21:51:54 +01:00
|
|
|
Path *outer_path = path->outerjoinpath;
|
|
|
|
Path *inner_path = path->innerjoinpath;
|
2000-02-15 21:49:31 +01:00
|
|
|
Cost startup_cost = 0;
|
|
|
|
Cost run_cost = 0;
|
2009-09-13 00:12:09 +02:00
|
|
|
Cost inner_rescan_start_cost;
|
|
|
|
Cost inner_rescan_total_cost;
|
2009-05-10 00:51:41 +02:00
|
|
|
Cost inner_run_cost;
|
2009-09-13 00:12:09 +02:00
|
|
|
Cost inner_rescan_run_cost;
|
2000-02-15 21:49:31 +01:00
|
|
|
Cost cpu_per_tuple;
|
2003-01-12 23:35:29 +01:00
|
|
|
QualCost restrict_qual_cost;
|
2003-01-27 21:51:54 +01:00
|
|
|
double outer_path_rows = PATH_ROWS(outer_path);
|
2006-02-05 03:59:17 +01:00
|
|
|
double inner_path_rows = nestloop_inner_path_rows(inner_path);
|
2000-02-15 21:49:31 +01:00
|
|
|
double ntuples;
|
2009-06-11 16:49:15 +02:00
|
|
|
Selectivity outer_match_frac;
|
|
|
|
Selectivity match_count;
|
2009-05-10 00:51:41 +02:00
|
|
|
bool indexed_join_quals;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-01-23 00:50:30 +01:00
|
|
|
if (!enable_nestloop)
|
2000-02-15 21:49:31 +01:00
|
|
|
startup_cost += disable_cost;
|
|
|
|
|
2009-09-13 00:12:09 +02:00
|
|
|
/* estimate costs to rescan the inner relation */
|
|
|
|
cost_rescan(root, inner_path,
|
|
|
|
&inner_rescan_start_cost,
|
|
|
|
&inner_rescan_total_cost);
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/* cost of source data */
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/*
|
2001-04-26 00:04:37 +02:00
|
|
|
* NOTE: clearly, we must pay both outer and inner paths' startup_cost
|
2001-10-25 07:50:21 +02:00
|
|
|
* before we can start returning tuples, so the join's startup cost is
|
2009-09-13 00:12:09 +02:00
|
|
|
* their sum. We'll also pay the inner path's rescan startup cost
|
|
|
|
* multiple times.
|
2000-02-15 21:49:31 +01:00
|
|
|
*/
|
|
|
|
startup_cost += outer_path->startup_cost + inner_path->startup_cost;
|
|
|
|
run_cost += outer_path->total_cost - outer_path->startup_cost;
|
2009-09-13 00:12:09 +02:00
|
|
|
if (outer_path_rows > 1)
|
|
|
|
run_cost += (outer_path_rows - 1) * inner_rescan_start_cost;
|
|
|
|
|
2009-05-10 00:51:41 +02:00
|
|
|
inner_run_cost = inner_path->total_cost - inner_path->startup_cost;
|
2009-09-13 00:12:09 +02:00
|
|
|
inner_rescan_run_cost = inner_rescan_total_cost - inner_rescan_start_cost;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2009-05-10 00:51:41 +02:00
|
|
|
if (adjust_semi_join(root, path, sjinfo,
|
|
|
|
&outer_match_frac,
|
|
|
|
&match_count,
|
|
|
|
&indexed_join_quals))
|
|
|
|
{
|
|
|
|
double outer_matched_rows;
|
2009-06-11 16:49:15 +02:00
|
|
|
Selectivity inner_scan_frac;
|
2009-05-10 00:51:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SEMI or ANTI join: executor will stop after first match.
|
|
|
|
*
|
|
|
|
* For an outer-rel row that has at least one match, we can expect the
|
|
|
|
* inner scan to stop after a fraction 1/(match_count+1) of the inner
|
|
|
|
* rows, if the matches are evenly distributed. Since they probably
|
|
|
|
* aren't quite evenly distributed, we apply a fuzz factor of 2.0 to
|
|
|
|
* that fraction. (If we used a larger fuzz factor, we'd have to
|
|
|
|
* clamp inner_scan_frac to at most 1.0; but since match_count is at
|
|
|
|
* least 1, no such clamp is needed now.)
|
2009-09-13 00:12:09 +02:00
|
|
|
*
|
|
|
|
* A complicating factor is that rescans may be cheaper than first
|
|
|
|
* scans. If we never scan all the way to the end of the inner rel,
|
|
|
|
* it might be (depending on the plan type) that we'd never pay the
|
|
|
|
* whole inner first-scan run cost. However it is difficult to
|
|
|
|
* estimate whether that will happen, so be conservative and always
|
|
|
|
* charge the whole first-scan cost once.
|
2009-05-10 00:51:41 +02:00
|
|
|
*/
|
2009-09-13 00:12:09 +02:00
|
|
|
run_cost += inner_run_cost;
|
|
|
|
|
2009-05-10 00:51:41 +02:00
|
|
|
outer_matched_rows = rint(outer_path_rows * outer_match_frac);
|
|
|
|
inner_scan_frac = 2.0 / (match_count + 1.0);
|
|
|
|
|
2009-09-13 00:12:09 +02:00
|
|
|
/* Add inner run cost for additional outer tuples having matches */
|
|
|
|
if (outer_matched_rows > 1)
|
|
|
|
run_cost += (outer_matched_rows - 1) * inner_rescan_run_cost * inner_scan_frac;
|
2009-05-10 00:51:41 +02:00
|
|
|
|
|
|
|
/* Compute number of tuples processed (not number emitted!) */
|
|
|
|
ntuples = outer_matched_rows * inner_path_rows * inner_scan_frac;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For unmatched outer-rel rows, there are two cases. If the inner
|
2009-06-11 16:49:15 +02:00
|
|
|
* path is an indexscan using all the joinquals as indexquals, then an
|
|
|
|
* unmatched row results in an indexscan returning no rows, which is
|
|
|
|
* probably quite cheap. We estimate this case as the same cost to
|
|
|
|
* return the first tuple of a nonempty scan. Otherwise, the executor
|
|
|
|
* will have to scan the whole inner rel; not so cheap.
|
2009-05-10 00:51:41 +02:00
|
|
|
*/
|
|
|
|
if (indexed_join_quals)
|
|
|
|
{
|
|
|
|
run_cost += (outer_path_rows - outer_matched_rows) *
|
2009-09-13 00:12:09 +02:00
|
|
|
inner_rescan_run_cost / inner_path_rows;
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-09-13 00:12:09 +02:00
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* We won't be evaluating any quals at all for these rows, so
|
|
|
|
* don't add them to ntuples.
|
2009-09-13 00:12:09 +02:00
|
|
|
*/
|
2009-05-10 00:51:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
run_cost += (outer_path_rows - outer_matched_rows) *
|
2009-09-13 00:12:09 +02:00
|
|
|
inner_rescan_run_cost;
|
2009-05-10 00:51:41 +02:00
|
|
|
ntuples += (outer_path_rows - outer_matched_rows) *
|
|
|
|
inner_path_rows;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Normal case; we'll scan whole input rel for each outer row */
|
2009-09-13 00:12:09 +02:00
|
|
|
run_cost += inner_run_cost;
|
|
|
|
if (outer_path_rows > 1)
|
|
|
|
run_cost += (outer_path_rows - 1) * inner_rescan_run_cost;
|
2009-05-10 00:51:41 +02:00
|
|
|
|
|
|
|
/* Compute number of tuples processed (not number emitted!) */
|
|
|
|
ntuples = outer_path_rows * inner_path_rows;
|
|
|
|
}
|
2000-01-09 01:26:47 +01:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/* CPU costs */
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval(&restrict_qual_cost, path->joinrestrictinfo, root);
|
2003-01-12 23:35:29 +01:00
|
|
|
startup_cost += restrict_qual_cost.startup;
|
|
|
|
cpu_per_tuple = cpu_tuple_cost + restrict_qual_cost.per_tuple;
|
2000-02-15 21:49:31 +01:00
|
|
|
run_cost += cpu_per_tuple * ntuples;
|
|
|
|
|
2003-01-27 21:51:54 +01:00
|
|
|
path->path.startup_cost = startup_cost;
|
|
|
|
path->path.total_cost = startup_cost + run_cost;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* cost_mergejoin
|
2000-01-09 01:26:47 +01:00
|
|
|
* Determines and returns the cost of joining two relations using the
|
|
|
|
* merge join algorithm.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2009-11-15 03:45:35 +01:00
|
|
|
* Unlike other costsize functions, this routine makes one actual decision:
|
|
|
|
* whether we should materialize the inner path. We do that either because
|
|
|
|
* the inner path can't support mark/restore, or because it's cheaper to
|
2010-02-26 03:01:40 +01:00
|
|
|
* use an interposed Material node to handle mark/restore. When the decision
|
2009-11-15 03:45:35 +01:00
|
|
|
* is cost-based it would be logically cleaner to build and cost two separate
|
|
|
|
* paths with and without that flag set; but that would require repeating most
|
2010-02-26 03:01:40 +01:00
|
|
|
* of the calculations here, which are not all that cheap. Since the choice
|
2009-11-15 03:45:35 +01:00
|
|
|
* will not affect output pathkeys or startup cost, only total cost, there is
|
|
|
|
* no possibility of wanting to keep both paths. So it seems best to make
|
|
|
|
* the decision here and record it in the path's materialize_inner field.
|
|
|
|
*
|
|
|
|
* 'path' is already filled in except for the cost fields and materialize_inner
|
2008-08-14 20:48:00 +02:00
|
|
|
* 'sjinfo' is extra info about the join for selectivity estimation
|
2003-01-27 21:51:54 +01:00
|
|
|
*
|
|
|
|
* Notes: path's mergeclauses should be a subset of the joinrestrictinfo list;
|
|
|
|
* outersortkeys and innersortkeys are lists of the keys to be used
|
|
|
|
* to sort the outer and inner relations, or NIL if no explicit
|
|
|
|
* sort is needed because the source path is already ordered.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
void
|
2008-08-14 20:48:00 +02:00
|
|
|
cost_mergejoin(MergePath *path, PlannerInfo *root, SpecialJoinInfo *sjinfo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-01-27 21:51:54 +01:00
|
|
|
Path *outer_path = path->jpath.outerjoinpath;
|
|
|
|
Path *inner_path = path->jpath.innerjoinpath;
|
|
|
|
List *mergeclauses = path->path_mergeclauses;
|
|
|
|
List *outersortkeys = path->outersortkeys;
|
|
|
|
List *innersortkeys = path->innersortkeys;
|
2000-02-15 21:49:31 +01:00
|
|
|
Cost startup_cost = 0;
|
|
|
|
Cost run_cost = 0;
|
2009-11-15 03:45:35 +01:00
|
|
|
Cost cpu_per_tuple,
|
|
|
|
inner_run_cost,
|
|
|
|
bare_inner_cost,
|
|
|
|
mat_inner_cost;
|
2003-01-27 21:51:54 +01:00
|
|
|
QualCost merge_qual_cost;
|
|
|
|
QualCost qp_qual_cost;
|
|
|
|
double outer_path_rows = PATH_ROWS(outer_path);
|
|
|
|
double inner_path_rows = PATH_ROWS(inner_path);
|
2002-03-01 05:09:28 +01:00
|
|
|
double outer_rows,
|
2007-12-08 22:05:11 +01:00
|
|
|
inner_rows,
|
|
|
|
outer_skip_rows,
|
|
|
|
inner_skip_rows;
|
2003-01-27 21:51:54 +01:00
|
|
|
double mergejointuples,
|
|
|
|
rescannedtuples;
|
|
|
|
double rescanratio;
|
2007-12-08 22:05:11 +01:00
|
|
|
Selectivity outerstartsel,
|
|
|
|
outerendsel,
|
|
|
|
innerstartsel,
|
|
|
|
innerendsel;
|
2000-02-15 21:49:31 +01:00
|
|
|
Path sort_path; /* dummy for result of cost_sort */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2008-03-24 22:53:04 +01:00
|
|
|
/* Protect some assumptions below that rowcounts aren't zero */
|
|
|
|
if (outer_path_rows <= 0)
|
|
|
|
outer_path_rows = 1;
|
|
|
|
if (inner_path_rows <= 0)
|
|
|
|
inner_path_rows = 1;
|
|
|
|
|
2000-01-23 00:50:30 +01:00
|
|
|
if (!enable_mergejoin)
|
2000-02-15 21:49:31 +01:00
|
|
|
startup_cost += disable_cost;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-01-27 21:51:54 +01:00
|
|
|
/*
|
2008-08-16 02:01:38 +02:00
|
|
|
* Compute cost of the mergequals and qpquals (other restriction clauses)
|
|
|
|
* separately.
|
2003-01-27 21:51:54 +01:00
|
|
|
*/
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval(&merge_qual_cost, mergeclauses, root);
|
|
|
|
cost_qual_eval(&qp_qual_cost, path->jpath.joinrestrictinfo, root);
|
2004-01-05 06:07:36 +01:00
|
|
|
qp_qual_cost.startup -= merge_qual_cost.startup;
|
|
|
|
qp_qual_cost.per_tuple -= merge_qual_cost.per_tuple;
|
2003-01-27 21:51:54 +01:00
|
|
|
|
2008-08-16 02:01:38 +02:00
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Get approx # tuples passing the mergequals. We use approx_tuple_count
|
2009-02-07 00:43:24 +01:00
|
|
|
* here because we need an estimate done with JOIN_INNER semantics.
|
2008-08-16 02:01:38 +02:00
|
|
|
*/
|
2009-02-07 00:43:24 +01:00
|
|
|
mergejointuples = approx_tuple_count(root, &path->jpath, mergeclauses);
|
2003-01-27 21:51:54 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* When there are equal merge keys in the outer relation, the mergejoin
|
|
|
|
* must rescan any matching tuples in the inner relation. This means
|
2009-11-15 03:45:35 +01:00
|
|
|
* re-fetching inner tuples; we have to estimate how often that happens.
|
2003-01-27 21:51:54 +01:00
|
|
|
*
|
2008-08-16 02:01:38 +02:00
|
|
|
* For regular inner and outer joins, the number of re-fetches can be
|
|
|
|
* estimated approximately as size of merge join output minus size of
|
|
|
|
* inner relation. Assume that the distinct key values are 1, 2, ..., and
|
|
|
|
* denote the number of values of each key in the outer relation as m1,
|
2009-06-11 16:49:15 +02:00
|
|
|
* m2, ...; in the inner relation, n1, n2, ... Then we have
|
2003-01-27 21:51:54 +01:00
|
|
|
*
|
2003-08-04 02:43:34 +02:00
|
|
|
* size of join = m1 * n1 + m2 * n2 + ...
|
2003-01-27 21:51:54 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* number of rescanned tuples = (m1 - 1) * n1 + (m2 - 1) * n2 + ... = m1 *
|
|
|
|
* n1 + m2 * n2 + ... - (n1 + n2 + ...) = size of join - size of inner
|
2003-08-04 02:43:34 +02:00
|
|
|
* relation
|
2003-01-27 21:51:54 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* This equation works correctly for outer tuples having no inner match
|
|
|
|
* (nk = 0), but not for inner tuples having no outer match (mk = 0); we
|
|
|
|
* are effectively subtracting those from the number of rescanned tuples,
|
|
|
|
* when we should not. Can we do better without expensive selectivity
|
2005-10-15 04:49:52 +02:00
|
|
|
* computations?
|
2008-08-16 02:01:38 +02:00
|
|
|
*
|
2009-02-07 00:43:24 +01:00
|
|
|
* The whole issue is moot if we are working from a unique-ified outer
|
|
|
|
* input.
|
2003-01-27 21:51:54 +01:00
|
|
|
*/
|
2009-02-07 00:43:24 +01:00
|
|
|
if (IsA(outer_path, UniquePath))
|
2003-01-27 21:51:54 +01:00
|
|
|
rescannedtuples = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rescannedtuples = mergejointuples - inner_path_rows;
|
|
|
|
/* Must clamp because of possible underestimate */
|
|
|
|
if (rescannedtuples < 0)
|
|
|
|
rescannedtuples = 0;
|
|
|
|
}
|
2009-11-15 03:45:35 +01:00
|
|
|
/* We'll inflate various costs this much to account for rescanning */
|
2003-01-27 21:51:54 +01:00
|
|
|
rescanratio = 1.0 + (rescannedtuples / inner_path_rows);
|
|
|
|
|
2002-03-01 05:09:28 +01:00
|
|
|
/*
|
2005-04-04 03:43:12 +02:00
|
|
|
* A merge join will stop as soon as it exhausts either input stream
|
|
|
|
* (unless it's an outer join, in which case the outer side has to be
|
|
|
|
* scanned all the way anyway). Estimate fraction of the left and right
|
2007-12-08 22:05:11 +01:00
|
|
|
* inputs that will actually need to be scanned. Likewise, we can
|
2009-06-11 16:49:15 +02:00
|
|
|
* estimate the number of rows that will be skipped before the first join
|
|
|
|
* pair is found, which should be factored into startup cost. We use only
|
|
|
|
* the first (most significant) merge clause for this purpose. Since
|
|
|
|
* mergejoinscansel() is a fairly expensive computation, we cache the
|
|
|
|
* results in the merge clause RestrictInfo.
|
2002-03-01 05:09:28 +01:00
|
|
|
*/
|
2005-04-04 03:43:12 +02:00
|
|
|
if (mergeclauses && path->jpath.jointype != JOIN_FULL)
|
2002-03-01 21:50:20 +01:00
|
|
|
{
|
2007-01-20 21:45:41 +01:00
|
|
|
RestrictInfo *firstclause = (RestrictInfo *) linitial(mergeclauses);
|
|
|
|
List *opathkeys;
|
|
|
|
List *ipathkeys;
|
2007-11-15 22:14:46 +01:00
|
|
|
PathKey *opathkey;
|
|
|
|
PathKey *ipathkey;
|
2007-01-22 21:00:40 +01:00
|
|
|
MergeScanSelCache *cache;
|
2007-01-20 21:45:41 +01:00
|
|
|
|
|
|
|
/* Get the input pathkeys to determine the sort-order details */
|
|
|
|
opathkeys = outersortkeys ? outersortkeys : outer_path->pathkeys;
|
|
|
|
ipathkeys = innersortkeys ? innersortkeys : inner_path->pathkeys;
|
|
|
|
Assert(opathkeys);
|
|
|
|
Assert(ipathkeys);
|
|
|
|
opathkey = (PathKey *) linitial(opathkeys);
|
|
|
|
ipathkey = (PathKey *) linitial(ipathkeys);
|
|
|
|
/* debugging check */
|
|
|
|
if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
|
|
|
|
opathkey->pk_strategy != ipathkey->pk_strategy ||
|
|
|
|
opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
|
|
|
|
elog(ERROR, "left and right pathkeys do not match in mergejoin");
|
|
|
|
|
2007-01-22 21:00:40 +01:00
|
|
|
/* Get the selectivity with caching */
|
|
|
|
cache = cached_scansel(root, firstclause, opathkey);
|
2007-01-20 21:45:41 +01:00
|
|
|
|
|
|
|
if (bms_is_subset(firstclause->left_relids,
|
|
|
|
outer_path->parent->relids))
|
2004-04-06 20:46:03 +02:00
|
|
|
{
|
|
|
|
/* left side of clause is outer */
|
2007-12-08 22:05:11 +01:00
|
|
|
outerstartsel = cache->leftstartsel;
|
|
|
|
outerendsel = cache->leftendsel;
|
|
|
|
innerstartsel = cache->rightstartsel;
|
|
|
|
innerendsel = cache->rightendsel;
|
2004-04-06 20:46:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* left side of clause is inner */
|
2007-12-08 22:05:11 +01:00
|
|
|
outerstartsel = cache->rightstartsel;
|
|
|
|
outerendsel = cache->rightendsel;
|
|
|
|
innerstartsel = cache->leftstartsel;
|
|
|
|
innerendsel = cache->leftendsel;
|
2004-04-06 20:46:03 +02:00
|
|
|
}
|
2008-08-16 02:01:38 +02:00
|
|
|
if (path->jpath.jointype == JOIN_LEFT ||
|
|
|
|
path->jpath.jointype == JOIN_ANTI)
|
2007-12-08 22:05:11 +01:00
|
|
|
{
|
|
|
|
outerstartsel = 0.0;
|
|
|
|
outerendsel = 1.0;
|
|
|
|
}
|
2005-04-04 03:43:12 +02:00
|
|
|
else if (path->jpath.jointype == JOIN_RIGHT)
|
2007-12-08 22:05:11 +01:00
|
|
|
{
|
|
|
|
innerstartsel = 0.0;
|
|
|
|
innerendsel = 1.0;
|
|
|
|
}
|
2002-03-01 21:50:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-04-04 03:43:12 +02:00
|
|
|
/* cope with clauseless or full mergejoin */
|
2007-12-08 22:05:11 +01:00
|
|
|
outerstartsel = innerstartsel = 0.0;
|
|
|
|
outerendsel = innerendsel = 1.0;
|
2002-03-01 21:50:20 +01:00
|
|
|
}
|
|
|
|
|
2007-12-08 22:05:11 +01:00
|
|
|
/*
|
|
|
|
* Convert selectivities to row counts. We force outer_rows and
|
|
|
|
* inner_rows to be at least 1, but the skip_rows estimates can be zero.
|
|
|
|
*/
|
|
|
|
outer_skip_rows = rint(outer_path_rows * outerstartsel);
|
|
|
|
inner_skip_rows = rint(inner_path_rows * innerstartsel);
|
|
|
|
outer_rows = clamp_row_est(outer_path_rows * outerendsel);
|
|
|
|
inner_rows = clamp_row_est(inner_path_rows * innerendsel);
|
|
|
|
|
|
|
|
Assert(outer_skip_rows <= outer_rows);
|
|
|
|
Assert(inner_skip_rows <= inner_rows);
|
2003-01-22 21:16:42 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Readjust scan selectivities to account for above rounding. This is
|
2005-10-15 04:49:52 +02:00
|
|
|
* normally an insignificant effect, but when there are only a few rows in
|
|
|
|
* the inputs, failing to do this makes for a large percentage error.
|
2003-01-22 21:16:42 +01:00
|
|
|
*/
|
2007-12-08 22:05:11 +01:00
|
|
|
outerstartsel = outer_skip_rows / outer_path_rows;
|
|
|
|
innerstartsel = inner_skip_rows / inner_path_rows;
|
|
|
|
outerendsel = outer_rows / outer_path_rows;
|
|
|
|
innerendsel = inner_rows / inner_path_rows;
|
|
|
|
|
|
|
|
Assert(outerstartsel <= outerendsel);
|
|
|
|
Assert(innerstartsel <= innerendsel);
|
2002-03-01 05:09:28 +01:00
|
|
|
|
2000-01-09 01:26:47 +01:00
|
|
|
/* cost of source data */
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
if (outersortkeys) /* do we need to sort outer? */
|
|
|
|
{
|
|
|
|
cost_sort(&sort_path,
|
2001-06-05 07:26:05 +02:00
|
|
|
root,
|
2000-02-15 21:49:31 +01:00
|
|
|
outersortkeys,
|
2002-11-21 01:42:20 +01:00
|
|
|
outer_path->total_cost,
|
2003-01-27 21:51:54 +01:00
|
|
|
outer_path_rows,
|
2007-05-04 03:13:45 +02:00
|
|
|
outer_path->parent->width,
|
2010-10-08 02:00:28 +02:00
|
|
|
0.0,
|
|
|
|
work_mem,
|
2007-05-04 03:13:45 +02:00
|
|
|
-1.0);
|
2000-02-15 21:49:31 +01:00
|
|
|
startup_cost += sort_path.startup_cost;
|
2007-12-08 22:05:11 +01:00
|
|
|
startup_cost += (sort_path.total_cost - sort_path.startup_cost)
|
|
|
|
* outerstartsel;
|
2002-03-01 05:09:28 +01:00
|
|
|
run_cost += (sort_path.total_cost - sort_path.startup_cost)
|
2007-12-08 22:05:11 +01:00
|
|
|
* (outerendsel - outerstartsel);
|
2000-02-15 21:49:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
startup_cost += outer_path->startup_cost;
|
2007-12-08 22:05:11 +01:00
|
|
|
startup_cost += (outer_path->total_cost - outer_path->startup_cost)
|
|
|
|
* outerstartsel;
|
2002-03-01 05:09:28 +01:00
|
|
|
run_cost += (outer_path->total_cost - outer_path->startup_cost)
|
2007-12-08 22:05:11 +01:00
|
|
|
* (outerendsel - outerstartsel);
|
2000-02-15 21:49:31 +01:00
|
|
|
}
|
2000-01-09 01:26:47 +01:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
if (innersortkeys) /* do we need to sort inner? */
|
|
|
|
{
|
|
|
|
cost_sort(&sort_path,
|
2001-06-05 07:26:05 +02:00
|
|
|
root,
|
2000-02-15 21:49:31 +01:00
|
|
|
innersortkeys,
|
2002-11-21 01:42:20 +01:00
|
|
|
inner_path->total_cost,
|
2003-01-27 21:51:54 +01:00
|
|
|
inner_path_rows,
|
2007-05-04 03:13:45 +02:00
|
|
|
inner_path->parent->width,
|
2010-10-08 02:00:28 +02:00
|
|
|
0.0,
|
|
|
|
work_mem,
|
2007-05-04 03:13:45 +02:00
|
|
|
-1.0);
|
2000-02-15 21:49:31 +01:00
|
|
|
startup_cost += sort_path.startup_cost;
|
2007-12-08 22:05:11 +01:00
|
|
|
startup_cost += (sort_path.total_cost - sort_path.startup_cost)
|
2009-11-15 03:45:35 +01:00
|
|
|
* innerstartsel;
|
|
|
|
inner_run_cost = (sort_path.total_cost - sort_path.startup_cost)
|
|
|
|
* (innerendsel - innerstartsel);
|
2000-02-15 21:49:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
startup_cost += inner_path->startup_cost;
|
2007-12-08 22:05:11 +01:00
|
|
|
startup_cost += (inner_path->total_cost - inner_path->startup_cost)
|
2009-11-15 03:45:35 +01:00
|
|
|
* innerstartsel;
|
|
|
|
inner_run_cost = (inner_path->total_cost - inner_path->startup_cost)
|
|
|
|
* (innerendsel - innerstartsel);
|
2000-02-15 21:49:31 +01:00
|
|
|
}
|
1999-04-30 06:01:44 +02:00
|
|
|
|
2009-11-15 03:45:35 +01:00
|
|
|
/*
|
|
|
|
* Decide whether we want to materialize the inner input to shield it from
|
2010-02-26 03:01:40 +01:00
|
|
|
* mark/restore and performing re-fetches. Our cost model for regular
|
2009-11-15 03:45:35 +01:00
|
|
|
* re-fetches is that a re-fetch costs the same as an original fetch,
|
|
|
|
* which is probably an overestimate; but on the other hand we ignore the
|
|
|
|
* bookkeeping costs of mark/restore. Not clear if it's worth developing
|
2010-02-26 03:01:40 +01:00
|
|
|
* a more refined model. So we just need to inflate the inner run cost by
|
|
|
|
* rescanratio.
|
2009-11-15 03:45:35 +01:00
|
|
|
*/
|
|
|
|
bare_inner_cost = inner_run_cost * rescanratio;
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-11-15 03:45:35 +01:00
|
|
|
/*
|
|
|
|
* When we interpose a Material node the re-fetch cost is assumed to be
|
2010-02-19 22:49:10 +01:00
|
|
|
* just cpu_operator_cost per tuple, independently of the underlying
|
|
|
|
* plan's cost; and we charge an extra cpu_operator_cost per original
|
|
|
|
* fetch as well. Note that we're assuming the materialize node will
|
|
|
|
* never spill to disk, since it only has to remember tuples back to the
|
|
|
|
* last mark. (If there are a huge number of duplicates, our other cost
|
2009-11-15 03:45:35 +01:00
|
|
|
* factors will make the path so expensive that it probably won't get
|
2010-02-26 03:01:40 +01:00
|
|
|
* chosen anyway.) So we don't use cost_rescan here.
|
2009-11-15 03:45:35 +01:00
|
|
|
*
|
|
|
|
* Note: keep this estimate in sync with create_mergejoin_plan's labeling
|
|
|
|
* of the generated Material node.
|
|
|
|
*/
|
|
|
|
mat_inner_cost = inner_run_cost +
|
2010-02-19 22:49:10 +01:00
|
|
|
cpu_operator_cost * inner_path_rows * rescanratio;
|
2009-11-15 03:45:35 +01:00
|
|
|
|
2010-04-19 02:55:26 +02:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Prefer materializing if it looks cheaper, unless the user has asked to
|
|
|
|
* suppress materialization.
|
2010-04-19 02:55:26 +02:00
|
|
|
*/
|
|
|
|
if (enable_material && mat_inner_cost < bare_inner_cost)
|
2009-11-15 03:45:35 +01:00
|
|
|
path->materialize_inner = true;
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-11-15 03:45:35 +01:00
|
|
|
/*
|
|
|
|
* Even if materializing doesn't look cheaper, we *must* do it if the
|
|
|
|
* inner path is to be used directly (without sorting) and it doesn't
|
|
|
|
* support mark/restore.
|
|
|
|
*
|
|
|
|
* Since the inner side must be ordered, and only Sorts and IndexScans can
|
|
|
|
* create order to begin with, and they both support mark/restore, you
|
|
|
|
* might think there's no problem --- but you'd be wrong. Nestloop and
|
|
|
|
* merge joins can *preserve* the order of their inputs, so they can be
|
|
|
|
* selected as the input of a mergejoin, and they don't support
|
|
|
|
* mark/restore at present.
|
2010-04-19 02:55:26 +02:00
|
|
|
*
|
2010-07-06 21:19:02 +02:00
|
|
|
* We don't test the value of enable_material here, because
|
|
|
|
* materialization is required for correctness in this case, and turning
|
|
|
|
* it off does not entitle us to deliver an invalid plan.
|
2009-11-15 03:45:35 +01:00
|
|
|
*/
|
|
|
|
else if (innersortkeys == NIL &&
|
|
|
|
!ExecSupportsMarkRestore(inner_path->pathtype))
|
|
|
|
path->materialize_inner = true;
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-11-15 03:45:35 +01:00
|
|
|
/*
|
|
|
|
* Also, force materializing if the inner path is to be sorted and the
|
|
|
|
* sort is expected to spill to disk. This is because the final merge
|
|
|
|
* pass can be done on-the-fly if it doesn't have to support mark/restore.
|
|
|
|
* We don't try to adjust the cost estimates for this consideration,
|
|
|
|
* though.
|
2010-04-19 02:55:26 +02:00
|
|
|
*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Since materialization is a performance optimization in this case,
|
|
|
|
* rather than necessary for correctness, we skip it if enable_material is
|
|
|
|
* off.
|
2009-11-15 03:45:35 +01:00
|
|
|
*/
|
2010-04-19 02:55:26 +02:00
|
|
|
else if (enable_material && innersortkeys != NIL &&
|
2009-11-15 03:45:35 +01:00
|
|
|
relation_byte_size(inner_path_rows, inner_path->parent->width) >
|
|
|
|
(work_mem * 1024L))
|
|
|
|
path->materialize_inner = true;
|
|
|
|
else
|
|
|
|
path->materialize_inner = false;
|
|
|
|
|
|
|
|
/* Charge the right incremental cost for the chosen case */
|
|
|
|
if (path->materialize_inner)
|
|
|
|
run_cost += mat_inner_cost;
|
|
|
|
else
|
|
|
|
run_cost += bare_inner_cost;
|
|
|
|
|
2003-01-27 21:51:54 +01:00
|
|
|
/* CPU costs */
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The number of tuple comparisons needed is approximately number of outer
|
|
|
|
* rows plus number of inner rows plus number of rescanned tuples (can we
|
|
|
|
* refine this?). At each one, we need to evaluate the mergejoin quals.
|
2003-01-27 21:51:54 +01:00
|
|
|
*/
|
|
|
|
startup_cost += merge_qual_cost.startup;
|
2007-12-08 22:05:11 +01:00
|
|
|
startup_cost += merge_qual_cost.per_tuple *
|
|
|
|
(outer_skip_rows + inner_skip_rows * rescanratio);
|
2003-01-27 21:51:54 +01:00
|
|
|
run_cost += merge_qual_cost.per_tuple *
|
2007-12-08 22:05:11 +01:00
|
|
|
((outer_rows - outer_skip_rows) +
|
|
|
|
(inner_rows - inner_skip_rows) * rescanratio);
|
2001-06-05 07:26:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For each tuple that gets through the mergejoin proper, we charge
|
|
|
|
* cpu_tuple_cost plus the cost of evaluating additional restriction
|
2005-10-15 04:49:52 +02:00
|
|
|
* clauses that are to be applied at the join. (This is pessimistic since
|
2008-08-16 02:01:38 +02:00
|
|
|
* not all of the quals may get evaluated at each tuple.)
|
2009-05-10 00:51:41 +02:00
|
|
|
*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Note: we could adjust for SEMI/ANTI joins skipping some qual
|
|
|
|
* evaluations here, but it's probably not worth the trouble.
|
2001-06-05 07:26:05 +02:00
|
|
|
*/
|
2003-01-27 21:51:54 +01:00
|
|
|
startup_cost += qp_qual_cost.startup;
|
|
|
|
cpu_per_tuple = cpu_tuple_cost + qp_qual_cost.per_tuple;
|
2008-08-16 02:01:38 +02:00
|
|
|
run_cost += cpu_per_tuple * mergejointuples;
|
2000-02-15 21:49:31 +01:00
|
|
|
|
2003-01-27 21:51:54 +01:00
|
|
|
path->jpath.path.startup_cost = startup_cost;
|
|
|
|
path->jpath.path.total_cost = startup_cost + run_cost;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2007-01-22 21:00:40 +01:00
|
|
|
/*
|
|
|
|
* run mergejoinscansel() with caching
|
|
|
|
*/
|
|
|
|
static MergeScanSelCache *
|
2007-11-15 23:25:18 +01:00
|
|
|
cached_scansel(PlannerInfo *root, RestrictInfo *rinfo, PathKey *pathkey)
|
2007-01-22 21:00:40 +01:00
|
|
|
{
|
|
|
|
MergeScanSelCache *cache;
|
|
|
|
ListCell *lc;
|
2007-12-08 22:05:11 +01:00
|
|
|
Selectivity leftstartsel,
|
|
|
|
leftendsel,
|
|
|
|
rightstartsel,
|
|
|
|
rightendsel;
|
2007-01-22 21:00:40 +01:00
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
/* Do we have this result already? */
|
|
|
|
foreach(lc, rinfo->scansel_cache)
|
|
|
|
{
|
|
|
|
cache = (MergeScanSelCache *) lfirst(lc);
|
|
|
|
if (cache->opfamily == pathkey->pk_opfamily &&
|
|
|
|
cache->strategy == pathkey->pk_strategy &&
|
|
|
|
cache->nulls_first == pathkey->pk_nulls_first)
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nope, do the computation */
|
|
|
|
mergejoinscansel(root,
|
|
|
|
(Node *) rinfo->clause,
|
|
|
|
pathkey->pk_opfamily,
|
|
|
|
pathkey->pk_strategy,
|
|
|
|
pathkey->pk_nulls_first,
|
2007-12-08 22:05:11 +01:00
|
|
|
&leftstartsel,
|
|
|
|
&leftendsel,
|
|
|
|
&rightstartsel,
|
|
|
|
&rightendsel);
|
2007-01-22 21:00:40 +01:00
|
|
|
|
|
|
|
/* Cache the result in suitably long-lived workspace */
|
|
|
|
oldcontext = MemoryContextSwitchTo(root->planner_cxt);
|
|
|
|
|
|
|
|
cache = (MergeScanSelCache *) palloc(sizeof(MergeScanSelCache));
|
|
|
|
cache->opfamily = pathkey->pk_opfamily;
|
|
|
|
cache->strategy = pathkey->pk_strategy;
|
|
|
|
cache->nulls_first = pathkey->pk_nulls_first;
|
2007-12-08 22:05:11 +01:00
|
|
|
cache->leftstartsel = leftstartsel;
|
|
|
|
cache->leftendsel = leftendsel;
|
|
|
|
cache->rightstartsel = rightstartsel;
|
|
|
|
cache->rightendsel = rightendsel;
|
2007-01-22 21:00:40 +01:00
|
|
|
|
|
|
|
rinfo->scansel_cache = lappend(rinfo->scansel_cache, cache);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-08-06 06:00:17 +02:00
|
|
|
* cost_hashjoin
|
2000-01-09 01:26:47 +01:00
|
|
|
* Determines and returns the cost of joining two relations using the
|
|
|
|
* hash join algorithm.
|
1999-08-06 06:00:17 +02:00
|
|
|
*
|
2003-01-27 21:51:54 +01:00
|
|
|
* 'path' is already filled in except for the cost fields
|
2008-08-14 20:48:00 +02:00
|
|
|
* 'sjinfo' is extra info about the join for selectivity estimation
|
2003-01-27 21:51:54 +01:00
|
|
|
*
|
|
|
|
* Note: path's hashclauses should be a subset of the joinrestrictinfo list
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-02-15 21:49:31 +01:00
|
|
|
void
|
2008-08-14 20:48:00 +02:00
|
|
|
cost_hashjoin(HashPath *path, PlannerInfo *root, SpecialJoinInfo *sjinfo)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-01-27 21:51:54 +01:00
|
|
|
Path *outer_path = path->jpath.outerjoinpath;
|
|
|
|
Path *inner_path = path->jpath.innerjoinpath;
|
|
|
|
List *hashclauses = path->path_hashclauses;
|
2000-02-15 21:49:31 +01:00
|
|
|
Cost startup_cost = 0;
|
|
|
|
Cost run_cost = 0;
|
|
|
|
Cost cpu_per_tuple;
|
2003-01-27 21:51:54 +01:00
|
|
|
QualCost hash_qual_cost;
|
|
|
|
QualCost qp_qual_cost;
|
|
|
|
double hashjointuples;
|
|
|
|
double outer_path_rows = PATH_ROWS(outer_path);
|
|
|
|
double inner_path_rows = PATH_ROWS(inner_path);
|
2004-05-31 01:40:41 +02:00
|
|
|
int num_hashclauses = list_length(hashclauses);
|
2005-03-06 23:15:05 +01:00
|
|
|
int numbuckets;
|
2002-12-30 16:21:23 +01:00
|
|
|
int numbatches;
|
2009-03-21 01:04:40 +01:00
|
|
|
int num_skew_mcvs;
|
2005-03-06 23:15:05 +01:00
|
|
|
double virtualbuckets;
|
2001-06-05 07:26:05 +02:00
|
|
|
Selectivity innerbucketsize;
|
2009-06-11 16:49:15 +02:00
|
|
|
Selectivity outer_match_frac;
|
|
|
|
Selectivity match_count;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *hcl;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-01-23 00:50:30 +01:00
|
|
|
if (!enable_hashjoin)
|
2000-02-15 21:49:31 +01:00
|
|
|
startup_cost += disable_cost;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-01-27 21:51:54 +01:00
|
|
|
/*
|
2008-08-16 02:01:38 +02:00
|
|
|
* Compute cost of the hashquals and qpquals (other restriction clauses)
|
|
|
|
* separately.
|
2003-01-27 21:51:54 +01:00
|
|
|
*/
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval(&hash_qual_cost, hashclauses, root);
|
|
|
|
cost_qual_eval(&qp_qual_cost, path->jpath.joinrestrictinfo, root);
|
2004-01-05 06:07:36 +01:00
|
|
|
qp_qual_cost.startup -= hash_qual_cost.startup;
|
|
|
|
qp_qual_cost.per_tuple -= hash_qual_cost.per_tuple;
|
2003-01-27 21:51:54 +01:00
|
|
|
|
1999-04-05 04:07:07 +02:00
|
|
|
/* cost of source data */
|
2000-02-15 21:49:31 +01:00
|
|
|
startup_cost += outer_path->startup_cost;
|
|
|
|
run_cost += outer_path->total_cost - outer_path->startup_cost;
|
|
|
|
startup_cost += inner_path->total_cost;
|
1999-04-05 04:07:07 +02:00
|
|
|
|
2003-01-27 21:51:54 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Cost of computing hash function: must do it once per input tuple. We
|
2007-01-08 17:09:22 +01:00
|
|
|
* charge one cpu_operator_cost for each column's hash function. Also,
|
|
|
|
* tack on one cpu_tuple_cost per inner row, to model the costs of
|
|
|
|
* inserting the row into the hashtable.
|
2003-01-27 21:51:54 +01:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* XXX when a hashclause is more complex than a single operator, we really
|
|
|
|
* should charge the extra eval costs of the left or right side, as
|
|
|
|
* appropriate, here. This seems more work than it's worth at the moment.
|
2003-01-27 21:51:54 +01:00
|
|
|
*/
|
2007-01-08 17:09:22 +01:00
|
|
|
startup_cost += (cpu_operator_cost * num_hashclauses + cpu_tuple_cost)
|
|
|
|
* inner_path_rows;
|
2003-01-27 21:51:54 +01:00
|
|
|
run_cost += cpu_operator_cost * num_hashclauses * outer_path_rows;
|
1999-04-05 04:07:07 +02:00
|
|
|
|
2009-03-21 01:04:40 +01:00
|
|
|
/*
|
|
|
|
* Get hash table size that executor would use for inner relation.
|
|
|
|
*
|
|
|
|
* XXX for the moment, always assume that skew optimization will be
|
|
|
|
* performed. As long as SKEW_WORK_MEM_PERCENT is small, it's not worth
|
|
|
|
* trying to determine that for sure.
|
|
|
|
*
|
|
|
|
* XXX at some point it might be interesting to try to account for skew
|
|
|
|
* optimization in the cost estimate, but for now, we don't.
|
|
|
|
*/
|
2003-01-27 21:51:54 +01:00
|
|
|
ExecChooseHashTableSize(inner_path_rows,
|
2002-12-30 16:21:23 +01:00
|
|
|
inner_path->parent->width,
|
2009-06-11 16:49:15 +02:00
|
|
|
true, /* useskew */
|
2005-03-06 23:15:05 +01:00
|
|
|
&numbuckets,
|
2009-03-21 01:04:40 +01:00
|
|
|
&numbatches,
|
|
|
|
&num_skew_mcvs);
|
2005-10-15 04:49:52 +02:00
|
|
|
virtualbuckets = (double) numbuckets *(double) numbatches;
|
2009-06-11 16:49:15 +02:00
|
|
|
|
2009-03-26 18:15:35 +01:00
|
|
|
/* mark the path with estimated # of batches */
|
|
|
|
path->num_batches = numbatches;
|
2002-12-30 16:21:23 +01:00
|
|
|
|
2001-06-05 07:26:05 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Determine bucketsize fraction for inner relation. We use the smallest
|
|
|
|
* bucketsize estimated for any individual hashclause; this is undoubtedly
|
|
|
|
* conservative.
|
2003-01-28 23:13:41 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* BUT: if inner relation has been unique-ified, we can assume it's good
|
|
|
|
* for hashing. This is important both because it's the right answer, and
|
2005-10-15 04:49:52 +02:00
|
|
|
* because we avoid contaminating the cache with a value that's wrong for
|
|
|
|
* non-unique-ified paths.
|
2001-06-05 07:26:05 +02:00
|
|
|
*/
|
2003-01-28 23:13:41 +01:00
|
|
|
if (IsA(inner_path, UniquePath))
|
|
|
|
innerbucketsize = 1.0 / virtualbuckets;
|
|
|
|
else
|
2001-06-05 07:26:05 +02:00
|
|
|
{
|
2003-01-28 23:13:41 +01:00
|
|
|
innerbucketsize = 1.0;
|
|
|
|
foreach(hcl, hashclauses)
|
|
|
|
{
|
|
|
|
RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(hcl);
|
|
|
|
Selectivity thisbucketsize;
|
2002-11-30 01:08:22 +01:00
|
|
|
|
2003-01-28 23:13:41 +01:00
|
|
|
Assert(IsA(restrictinfo, RestrictInfo));
|
2002-11-30 01:08:22 +01:00
|
|
|
|
2003-01-28 23:13:41 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* First we have to figure out which side of the hashjoin clause
|
|
|
|
* is the inner side.
|
2003-01-28 23:13:41 +01:00
|
|
|
*
|
|
|
|
* Since we tend to visit the same clauses over and over when
|
2005-10-15 04:49:52 +02:00
|
|
|
* planning a large query, we cache the bucketsize estimate in the
|
|
|
|
* RestrictInfo node to avoid repeated lookups of statistics.
|
2003-01-28 23:13:41 +01:00
|
|
|
*/
|
2003-02-08 21:20:55 +01:00
|
|
|
if (bms_is_subset(restrictinfo->right_relids,
|
|
|
|
inner_path->parent->relids))
|
2002-11-30 01:08:22 +01:00
|
|
|
{
|
2003-01-28 23:13:41 +01:00
|
|
|
/* righthand side is inner */
|
|
|
|
thisbucketsize = restrictinfo->right_bucketsize;
|
|
|
|
if (thisbucketsize < 0)
|
|
|
|
{
|
|
|
|
/* not cached yet */
|
|
|
|
thisbucketsize =
|
|
|
|
estimate_hash_bucketsize(root,
|
2005-10-15 04:49:52 +02:00
|
|
|
get_rightop(restrictinfo->clause),
|
2003-01-28 23:13:41 +01:00
|
|
|
virtualbuckets);
|
|
|
|
restrictinfo->right_bucketsize = thisbucketsize;
|
|
|
|
}
|
2002-11-30 01:08:22 +01:00
|
|
|
}
|
2003-01-28 23:13:41 +01:00
|
|
|
else
|
2002-11-30 01:08:22 +01:00
|
|
|
{
|
2003-02-08 21:20:55 +01:00
|
|
|
Assert(bms_is_subset(restrictinfo->left_relids,
|
|
|
|
inner_path->parent->relids));
|
2003-01-28 23:13:41 +01:00
|
|
|
/* lefthand side is inner */
|
|
|
|
thisbucketsize = restrictinfo->left_bucketsize;
|
|
|
|
if (thisbucketsize < 0)
|
|
|
|
{
|
|
|
|
/* not cached yet */
|
|
|
|
thisbucketsize =
|
|
|
|
estimate_hash_bucketsize(root,
|
2005-10-15 04:49:52 +02:00
|
|
|
get_leftop(restrictinfo->clause),
|
2003-01-28 23:13:41 +01:00
|
|
|
virtualbuckets);
|
|
|
|
restrictinfo->left_bucketsize = thisbucketsize;
|
|
|
|
}
|
2002-11-30 01:08:22 +01:00
|
|
|
}
|
|
|
|
|
2003-01-28 23:13:41 +01:00
|
|
|
if (innerbucketsize > thisbucketsize)
|
|
|
|
innerbucketsize = thisbucketsize;
|
|
|
|
}
|
2001-06-05 07:26:05 +02:00
|
|
|
}
|
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
2005-03-06 23:15:05 +01:00
|
|
|
* If inner relation is too big then we will need to "batch" the join,
|
2005-10-15 04:49:52 +02:00
|
|
|
* which implies writing and reading most of the tuples to disk an extra
|
2007-01-08 17:09:22 +01:00
|
|
|
* time. Charge seq_page_cost per page, since the I/O should be nice and
|
2007-11-15 22:14:46 +01:00
|
|
|
* sequential. Writing the inner rel counts as startup cost, all the rest
|
|
|
|
* as run cost.
|
1999-08-06 06:00:17 +02:00
|
|
|
*/
|
2005-03-06 23:15:05 +01:00
|
|
|
if (numbatches > 1)
|
2000-02-15 21:49:31 +01:00
|
|
|
{
|
2003-01-27 21:51:54 +01:00
|
|
|
double outerpages = page_size(outer_path_rows,
|
2000-04-12 19:17:23 +02:00
|
|
|
outer_path->parent->width);
|
2003-01-27 21:51:54 +01:00
|
|
|
double innerpages = page_size(inner_path_rows,
|
2000-04-12 19:17:23 +02:00
|
|
|
inner_path->parent->width);
|
2000-02-15 21:49:31 +01:00
|
|
|
|
2007-01-08 17:09:22 +01:00
|
|
|
startup_cost += seq_page_cost * innerpages;
|
|
|
|
run_cost += seq_page_cost * (innerpages + 2 * outerpages);
|
2000-02-15 21:49:31 +01:00
|
|
|
}
|
1999-08-06 06:00:17 +02:00
|
|
|
|
2003-01-27 21:51:54 +01:00
|
|
|
/* CPU costs */
|
|
|
|
|
2009-05-10 00:51:41 +02:00
|
|
|
if (adjust_semi_join(root, &path->jpath, sjinfo,
|
|
|
|
&outer_match_frac,
|
|
|
|
&match_count,
|
|
|
|
NULL))
|
|
|
|
{
|
|
|
|
double outer_matched_rows;
|
2009-06-11 16:49:15 +02:00
|
|
|
Selectivity inner_scan_frac;
|
2009-05-10 00:51:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SEMI or ANTI join: executor will stop after first match.
|
|
|
|
*
|
|
|
|
* For an outer-rel row that has at least one match, we can expect the
|
|
|
|
* bucket scan to stop after a fraction 1/(match_count+1) of the
|
|
|
|
* bucket's rows, if the matches are evenly distributed. Since they
|
|
|
|
* probably aren't quite evenly distributed, we apply a fuzz factor of
|
|
|
|
* 2.0 to that fraction. (If we used a larger fuzz factor, we'd have
|
|
|
|
* to clamp inner_scan_frac to at most 1.0; but since match_count is
|
|
|
|
* at least 1, no such clamp is needed now.)
|
|
|
|
*/
|
|
|
|
outer_matched_rows = rint(outer_path_rows * outer_match_frac);
|
|
|
|
inner_scan_frac = 2.0 / (match_count + 1.0);
|
|
|
|
|
|
|
|
startup_cost += hash_qual_cost.startup;
|
|
|
|
run_cost += hash_qual_cost.per_tuple * outer_matched_rows *
|
|
|
|
clamp_row_est(inner_path_rows * innerbucketsize * inner_scan_frac) * 0.5;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For unmatched outer-rel rows, the picture is quite a lot different.
|
|
|
|
* In the first place, there is no reason to assume that these rows
|
|
|
|
* preferentially hit heavily-populated buckets; instead assume they
|
|
|
|
* are uncorrelated with the inner distribution and so they see an
|
|
|
|
* average bucket size of inner_path_rows / virtualbuckets. In the
|
2009-06-11 16:49:15 +02:00
|
|
|
* second place, it seems likely that they will have few if any exact
|
|
|
|
* hash-code matches and so very few of the tuples in the bucket will
|
|
|
|
* actually require eval of the hash quals. We don't have any good
|
|
|
|
* way to estimate how many will, but for the moment assume that the
|
|
|
|
* effective cost per bucket entry is one-tenth what it is for
|
|
|
|
* matchable tuples.
|
2009-05-10 00:51:41 +02:00
|
|
|
*/
|
|
|
|
run_cost += hash_qual_cost.per_tuple *
|
|
|
|
(outer_path_rows - outer_matched_rows) *
|
|
|
|
clamp_row_est(inner_path_rows / virtualbuckets) * 0.05;
|
|
|
|
|
|
|
|
/* Get # of tuples that will pass the basic join */
|
|
|
|
if (path->jpath.jointype == JOIN_SEMI)
|
|
|
|
hashjointuples = outer_matched_rows;
|
|
|
|
else
|
|
|
|
hashjointuples = outer_path_rows - outer_matched_rows;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The number of tuple comparisons needed is the number of outer
|
|
|
|
* tuples times the typical number of tuples in a hash bucket, which
|
|
|
|
* is the inner relation size times its bucketsize fraction. At each
|
|
|
|
* one, we need to evaluate the hashjoin quals. But actually,
|
|
|
|
* charging the full qual eval cost at each tuple is pessimistic,
|
|
|
|
* since we don't evaluate the quals unless the hash values match
|
|
|
|
* exactly. For lack of a better idea, halve the cost estimate to
|
|
|
|
* allow for that.
|
|
|
|
*/
|
|
|
|
startup_cost += hash_qual_cost.startup;
|
|
|
|
run_cost += hash_qual_cost.per_tuple * outer_path_rows *
|
|
|
|
clamp_row_est(inner_path_rows * innerbucketsize) * 0.5;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get approx # tuples passing the hashquals. We use
|
|
|
|
* approx_tuple_count here because we need an estimate done with
|
|
|
|
* JOIN_INNER semantics.
|
|
|
|
*/
|
|
|
|
hashjointuples = approx_tuple_count(root, &path->jpath, hashclauses);
|
|
|
|
}
|
2003-01-27 21:51:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For each tuple that gets through the hashjoin proper, we charge
|
|
|
|
* cpu_tuple_cost plus the cost of evaluating additional restriction
|
2005-10-15 04:49:52 +02:00
|
|
|
* clauses that are to be applied at the join. (This is pessimistic since
|
|
|
|
* not all of the quals may get evaluated at each tuple.)
|
2003-01-27 21:51:54 +01:00
|
|
|
*/
|
|
|
|
startup_cost += qp_qual_cost.startup;
|
|
|
|
cpu_per_tuple = cpu_tuple_cost + qp_qual_cost.per_tuple;
|
2008-08-16 02:01:38 +02:00
|
|
|
run_cost += cpu_per_tuple * hashjointuples;
|
2003-01-27 21:51:54 +01:00
|
|
|
|
|
|
|
path->jpath.path.startup_cost = startup_cost;
|
|
|
|
path->jpath.path.total_cost = startup_cost + run_cost;
|
2000-02-15 21:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 02:16:04 +02:00
|
|
|
/*
|
|
|
|
* cost_subplan
|
|
|
|
* Figure the costs for a SubPlan (or initplan).
|
|
|
|
*
|
|
|
|
* Note: we could dig the subplan's Plan out of the root list, but in practice
|
|
|
|
* all callers have it handy already, so we make them pass it.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan)
|
|
|
|
{
|
|
|
|
QualCost sp_cost;
|
|
|
|
|
|
|
|
/* Figure any cost for evaluating the testexpr */
|
|
|
|
cost_qual_eval(&sp_cost,
|
|
|
|
make_ands_implicit((Expr *) subplan->testexpr),
|
|
|
|
root);
|
|
|
|
|
|
|
|
if (subplan->useHashTable)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we are using a hash table for the subquery outputs, then the
|
|
|
|
* cost of evaluating the query is a one-time cost. We charge one
|
|
|
|
* cpu_operator_cost per tuple for the work of loading the hashtable,
|
|
|
|
* too.
|
|
|
|
*/
|
|
|
|
sp_cost.startup += plan->total_cost +
|
|
|
|
cpu_operator_cost * plan->plan_rows;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The per-tuple costs include the cost of evaluating the lefthand
|
|
|
|
* expressions, plus the cost of probing the hashtable. We already
|
2009-06-11 16:49:15 +02:00
|
|
|
* accounted for the lefthand expressions as part of the testexpr, and
|
|
|
|
* will also have counted one cpu_operator_cost for each comparison
|
|
|
|
* operator. That is probably too low for the probing cost, but it's
|
|
|
|
* hard to make a better estimate, so live with it for now.
|
2008-08-22 02:16:04 +02:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Otherwise we will be rescanning the subplan output on each
|
2009-06-11 16:49:15 +02:00
|
|
|
* evaluation. We need to estimate how much of the output we will
|
2008-08-22 02:16:04 +02:00
|
|
|
* actually need to scan. NOTE: this logic should agree with the
|
|
|
|
* tuple_fraction estimates used by make_subplan() in
|
|
|
|
* plan/subselect.c.
|
|
|
|
*/
|
|
|
|
Cost plan_run_cost = plan->total_cost - plan->startup_cost;
|
|
|
|
|
|
|
|
if (subplan->subLinkType == EXISTS_SUBLINK)
|
|
|
|
{
|
|
|
|
/* we only need to fetch 1 tuple */
|
|
|
|
sp_cost.per_tuple += plan_run_cost / plan->plan_rows;
|
|
|
|
}
|
|
|
|
else if (subplan->subLinkType == ALL_SUBLINK ||
|
|
|
|
subplan->subLinkType == ANY_SUBLINK)
|
|
|
|
{
|
|
|
|
/* assume we need 50% of the tuples */
|
|
|
|
sp_cost.per_tuple += 0.50 * plan_run_cost;
|
|
|
|
/* also charge a cpu_operator_cost per row examined */
|
|
|
|
sp_cost.per_tuple += 0.50 * plan->plan_rows * cpu_operator_cost;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* assume we need all tuples */
|
|
|
|
sp_cost.per_tuple += plan_run_cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Also account for subplan's startup cost. If the subplan is
|
2009-09-13 00:12:09 +02:00
|
|
|
* uncorrelated or undirect correlated, AND its topmost node is one
|
|
|
|
* that materializes its output, assume that we'll only need to pay
|
|
|
|
* its startup cost once; otherwise assume we pay the startup cost
|
|
|
|
* every time.
|
2008-08-22 02:16:04 +02:00
|
|
|
*/
|
|
|
|
if (subplan->parParam == NIL &&
|
2009-09-13 00:12:09 +02:00
|
|
|
ExecMaterializesOutput(nodeTag(plan)))
|
2008-08-22 02:16:04 +02:00
|
|
|
sp_cost.startup += plan->startup_cost;
|
|
|
|
else
|
|
|
|
sp_cost.per_tuple += plan->startup_cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
subplan->startup_cost = sp_cost.startup;
|
|
|
|
subplan->per_call_cost = sp_cost.per_tuple;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-13 00:12:09 +02:00
|
|
|
/*
|
|
|
|
* cost_rescan
|
|
|
|
* Given a finished Path, estimate the costs of rescanning it after
|
2010-02-26 03:01:40 +01:00
|
|
|
* having done so the first time. For some Path types a rescan is
|
2009-09-13 00:12:09 +02:00
|
|
|
* cheaper than an original scan (if no parameters change), and this
|
|
|
|
* function embodies knowledge about that. The default is to return
|
2010-02-26 03:01:40 +01:00
|
|
|
* the same costs stored in the Path. (Note that the cost estimates
|
2009-09-13 00:12:09 +02:00
|
|
|
* actually stored in Paths are always for first scans.)
|
|
|
|
*
|
|
|
|
* This function is not currently intended to model effects such as rescans
|
|
|
|
* being cheaper due to disk block caching; what we are concerned with is
|
|
|
|
* plan types wherein the executor caches results explicitly, or doesn't
|
|
|
|
* redo startup calculations, etc.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
cost_rescan(PlannerInfo *root, Path *path,
|
2010-02-26 03:01:40 +01:00
|
|
|
Cost *rescan_startup_cost, /* output parameters */
|
2009-09-13 00:12:09 +02:00
|
|
|
Cost *rescan_total_cost)
|
|
|
|
{
|
|
|
|
switch (path->pathtype)
|
|
|
|
{
|
|
|
|
case T_FunctionScan:
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-09-13 00:12:09 +02:00
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* Currently, nodeFunctionscan.c always executes the function to
|
|
|
|
* completion before returning any rows, and caches the results in
|
|
|
|
* a tuplestore. So the function eval cost is all startup cost
|
|
|
|
* and isn't paid over again on rescans. However, all run costs
|
|
|
|
* will be paid over again.
|
2009-09-13 00:12:09 +02:00
|
|
|
*/
|
|
|
|
*rescan_startup_cost = 0;
|
|
|
|
*rescan_total_cost = path->total_cost - path->startup_cost;
|
|
|
|
break;
|
|
|
|
case T_HashJoin:
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-09-13 00:12:09 +02:00
|
|
|
/*
|
|
|
|
* Assume that all of the startup cost represents hash table
|
|
|
|
* building, which we won't have to do over.
|
|
|
|
*/
|
|
|
|
*rescan_startup_cost = 0;
|
|
|
|
*rescan_total_cost = path->total_cost - path->startup_cost;
|
|
|
|
break;
|
|
|
|
case T_CteScan:
|
|
|
|
case T_WorkTableScan:
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* These plan types materialize their final result in a
|
2010-02-26 03:01:40 +01:00
|
|
|
* tuplestore or tuplesort object. So the rescan cost is only
|
2009-09-13 00:12:09 +02:00
|
|
|
* cpu_tuple_cost per tuple, unless the result is large enough
|
|
|
|
* to spill to disk.
|
|
|
|
*/
|
2010-02-26 03:01:40 +01:00
|
|
|
Cost run_cost = cpu_tuple_cost * path->parent->rows;
|
|
|
|
double nbytes = relation_byte_size(path->parent->rows,
|
|
|
|
path->parent->width);
|
|
|
|
long work_mem_bytes = work_mem * 1024L;
|
2009-09-13 00:12:09 +02:00
|
|
|
|
|
|
|
if (nbytes > work_mem_bytes)
|
|
|
|
{
|
|
|
|
/* It will spill, so account for re-read cost */
|
|
|
|
double npages = ceil(nbytes / BLCKSZ);
|
|
|
|
|
|
|
|
run_cost += seq_page_cost * npages;
|
|
|
|
}
|
|
|
|
*rescan_startup_cost = 0;
|
|
|
|
*rescan_total_cost = run_cost;
|
|
|
|
}
|
|
|
|
break;
|
2010-02-19 22:49:10 +01:00
|
|
|
case T_Material:
|
|
|
|
case T_Sort:
|
|
|
|
{
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* These plan types not only materialize their results, but do
|
|
|
|
* not implement qual filtering or projection. So they are
|
|
|
|
* even cheaper to rescan than the ones above. We charge only
|
|
|
|
* cpu_operator_cost per tuple. (Note: keep that in sync with
|
|
|
|
* the run_cost charge in cost_sort, and also see comments in
|
|
|
|
* cost_material before you change it.)
|
2010-02-19 22:49:10 +01:00
|
|
|
*/
|
2010-02-26 03:01:40 +01:00
|
|
|
Cost run_cost = cpu_operator_cost * path->parent->rows;
|
|
|
|
double nbytes = relation_byte_size(path->parent->rows,
|
|
|
|
path->parent->width);
|
|
|
|
long work_mem_bytes = work_mem * 1024L;
|
2010-02-19 22:49:10 +01:00
|
|
|
|
|
|
|
if (nbytes > work_mem_bytes)
|
|
|
|
{
|
|
|
|
/* It will spill, so account for re-read cost */
|
|
|
|
double npages = ceil(nbytes / BLCKSZ);
|
|
|
|
|
|
|
|
run_cost += seq_page_cost * npages;
|
|
|
|
}
|
|
|
|
*rescan_startup_cost = 0;
|
|
|
|
*rescan_total_cost = run_cost;
|
|
|
|
}
|
|
|
|
break;
|
2009-09-13 00:12:09 +02:00
|
|
|
default:
|
|
|
|
*rescan_startup_cost = path->startup_cost;
|
|
|
|
*rescan_total_cost = path->total_cost;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/*
|
|
|
|
* cost_qual_eval
|
2003-01-12 23:35:29 +01:00
|
|
|
* Estimate the CPU costs of evaluating a WHERE clause.
|
2000-02-15 21:49:31 +01:00
|
|
|
* The input can be either an implicitly-ANDed list of boolean
|
2007-01-22 02:35:23 +01:00
|
|
|
* expressions, or a list of RestrictInfo nodes. (The latter is
|
|
|
|
* preferred since it allows caching of the results.)
|
2003-01-12 23:35:29 +01:00
|
|
|
* The result includes both a one-time (startup) component,
|
|
|
|
* and a per-evaluation component.
|
2000-02-15 21:49:31 +01:00
|
|
|
*/
|
2003-01-12 23:35:29 +01:00
|
|
|
void
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
|
2000-02-15 21:49:31 +01:00
|
|
|
{
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval_context context;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *l;
|
2000-02-15 21:49:31 +01:00
|
|
|
|
2007-02-22 23:00:26 +01:00
|
|
|
context.root = root;
|
|
|
|
context.total.startup = 0;
|
|
|
|
context.total.per_tuple = 0;
|
2003-01-12 23:35:29 +01:00
|
|
|
|
2000-12-13 00:33:34 +01:00
|
|
|
/* We don't charge any cost for the implicit ANDing at top level ... */
|
|
|
|
|
|
|
|
foreach(l, quals)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Node *qual = (Node *) lfirst(l);
|
2000-12-13 00:33:34 +01:00
|
|
|
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval_walker(qual, &context);
|
2000-12-13 00:33:34 +01:00
|
|
|
}
|
2007-02-22 23:00:26 +01:00
|
|
|
|
|
|
|
*cost = context.total;
|
2000-02-15 21:49:31 +01:00
|
|
|
}
|
|
|
|
|
2007-01-22 02:35:23 +01:00
|
|
|
/*
|
|
|
|
* cost_qual_eval_node
|
|
|
|
* As above, for a single RestrictInfo or expression.
|
|
|
|
*/
|
|
|
|
void
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
|
2007-01-22 02:35:23 +01:00
|
|
|
{
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval_context context;
|
|
|
|
|
|
|
|
context.root = root;
|
|
|
|
context.total.startup = 0;
|
|
|
|
context.total.per_tuple = 0;
|
|
|
|
|
|
|
|
cost_qual_eval_walker(qual, &context);
|
|
|
|
|
|
|
|
*cost = context.total;
|
2007-01-22 02:35:23 +01:00
|
|
|
}
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
static bool
|
2007-11-15 23:25:18 +01:00
|
|
|
cost_qual_eval_walker(Node *node, cost_qual_eval_context *context)
|
2000-02-15 21:49:31 +01:00
|
|
|
{
|
|
|
|
if (node == NULL)
|
|
|
|
return false;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
/*
|
2007-01-22 02:35:23 +01:00
|
|
|
* RestrictInfo nodes contain an eval_cost field reserved for this
|
2007-11-15 22:14:46 +01:00
|
|
|
* routine's use, so that it's not necessary to evaluate the qual clause's
|
|
|
|
* cost more than once. If the clause's cost hasn't been computed yet,
|
|
|
|
* the field's startup value will contain -1.
|
2007-01-22 02:35:23 +01:00
|
|
|
*/
|
|
|
|
if (IsA(node, RestrictInfo))
|
|
|
|
{
|
|
|
|
RestrictInfo *rinfo = (RestrictInfo *) node;
|
|
|
|
|
|
|
|
if (rinfo->eval_cost.startup < 0)
|
|
|
|
{
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval_context locContext;
|
|
|
|
|
|
|
|
locContext.root = context->root;
|
|
|
|
locContext.total.startup = 0;
|
|
|
|
locContext.total.per_tuple = 0;
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2007-01-22 02:35:23 +01:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* For an OR clause, recurse into the marked-up tree so that we
|
|
|
|
* set the eval_cost for contained RestrictInfos too.
|
2007-01-22 02:35:23 +01:00
|
|
|
*/
|
|
|
|
if (rinfo->orclause)
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval_walker((Node *) rinfo->orclause, &locContext);
|
2007-01-22 02:35:23 +01:00
|
|
|
else
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval_walker((Node *) rinfo->clause, &locContext);
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2007-01-22 02:35:23 +01:00
|
|
|
/*
|
|
|
|
* If the RestrictInfo is marked pseudoconstant, it will be tested
|
|
|
|
* only once, so treat its cost as all startup cost.
|
|
|
|
*/
|
|
|
|
if (rinfo->pseudoconstant)
|
|
|
|
{
|
|
|
|
/* count one execution during startup */
|
2007-02-22 23:00:26 +01:00
|
|
|
locContext.total.startup += locContext.total.per_tuple;
|
|
|
|
locContext.total.per_tuple = 0;
|
2007-01-22 02:35:23 +01:00
|
|
|
}
|
2007-02-22 23:00:26 +01:00
|
|
|
rinfo->eval_cost = locContext.total;
|
2007-01-22 02:35:23 +01:00
|
|
|
}
|
2007-02-22 23:00:26 +01:00
|
|
|
context->total.startup += rinfo->eval_cost.startup;
|
|
|
|
context->total.per_tuple += rinfo->eval_cost.per_tuple;
|
2007-01-22 02:35:23 +01:00
|
|
|
/* do NOT recurse into children */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For each operator or function node in the given tree, we charge the
|
2007-11-15 22:14:46 +01:00
|
|
|
* estimated execution cost given by pg_proc.procost (remember to multiply
|
|
|
|
* this by cpu_operator_cost).
|
2007-01-22 02:35:23 +01:00
|
|
|
*
|
|
|
|
* Vars and Consts are charged zero, and so are boolean operators (AND,
|
|
|
|
* OR, NOT). Simplistic, but a lot better than no model at all.
|
2000-02-15 21:49:31 +01:00
|
|
|
*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Note that Aggref and WindowFunc nodes are (and should be) treated like
|
|
|
|
* Vars --- whatever execution cost they have is absorbed into
|
|
|
|
* plan-node-specific costing. As far as expression evaluation is
|
2008-12-28 19:54:01 +01:00
|
|
|
* concerned they're just like Vars.
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Should we try to account for the possibility of short-circuit
|
2007-01-22 02:35:23 +01:00
|
|
|
* evaluation of AND/OR? Probably *not*, because that would make the
|
|
|
|
* results depend on the clause ordering, and we are not in any position
|
|
|
|
* to expect that the current ordering of the clauses is the one that's
|
2007-11-15 22:14:46 +01:00
|
|
|
* going to end up being used. (Is it worth applying order_qual_clauses
|
2007-01-22 02:35:23 +01:00
|
|
|
* much earlier in the planning process to fix this?)
|
1999-04-05 04:07:07 +02:00
|
|
|
*/
|
2007-01-22 02:35:23 +01:00
|
|
|
if (IsA(node, FuncExpr))
|
|
|
|
{
|
2007-02-22 23:00:26 +01:00
|
|
|
context->total.per_tuple +=
|
|
|
|
get_func_cost(((FuncExpr *) node)->funcid) * cpu_operator_cost;
|
2007-01-22 02:35:23 +01:00
|
|
|
}
|
|
|
|
else if (IsA(node, OpExpr) ||
|
|
|
|
IsA(node, DistinctExpr) ||
|
|
|
|
IsA(node, NullIfExpr))
|
|
|
|
{
|
|
|
|
/* rely on struct equivalence to treat these all alike */
|
|
|
|
set_opfuncid((OpExpr *) node);
|
2007-02-22 23:00:26 +01:00
|
|
|
context->total.per_tuple +=
|
|
|
|
get_func_cost(((OpExpr *) node)->opfuncid) * cpu_operator_cost;
|
2007-01-22 02:35:23 +01:00
|
|
|
}
|
2003-06-29 02:33:44 +02:00
|
|
|
else if (IsA(node, ScalarArrayOpExpr))
|
|
|
|
{
|
2005-11-26 23:14:57 +01:00
|
|
|
/*
|
|
|
|
* Estimate that the operator will be applied to about half of the
|
|
|
|
* array elements before the answer is determined.
|
|
|
|
*/
|
|
|
|
ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) node;
|
2006-10-04 02:30:14 +02:00
|
|
|
Node *arraynode = (Node *) lsecond(saop->args);
|
2005-11-26 23:14:57 +01:00
|
|
|
|
2007-01-22 02:35:23 +01:00
|
|
|
set_sa_opfuncid(saop);
|
2007-02-22 23:00:26 +01:00
|
|
|
context->total.per_tuple += get_func_cost(saop->opfuncid) *
|
2005-11-26 23:14:57 +01:00
|
|
|
cpu_operator_cost * estimate_array_length(arraynode) * 0.5;
|
2003-06-29 02:33:44 +02:00
|
|
|
}
|
2007-06-05 23:31:09 +02:00
|
|
|
else if (IsA(node, CoerceViaIO))
|
|
|
|
{
|
|
|
|
CoerceViaIO *iocoerce = (CoerceViaIO *) node;
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid iofunc;
|
|
|
|
Oid typioparam;
|
|
|
|
bool typisvarlena;
|
2007-06-05 23:31:09 +02:00
|
|
|
|
|
|
|
/* check the result type's input function */
|
|
|
|
getTypeInputInfo(iocoerce->resulttype,
|
|
|
|
&iofunc, &typioparam);
|
|
|
|
context->total.per_tuple += get_func_cost(iofunc) * cpu_operator_cost;
|
|
|
|
/* check the input type's output function */
|
|
|
|
getTypeOutputInfo(exprType((Node *) iocoerce->arg),
|
|
|
|
&iofunc, &typisvarlena);
|
|
|
|
context->total.per_tuple += get_func_cost(iofunc) * cpu_operator_cost;
|
|
|
|
}
|
2007-03-28 01:21:12 +02:00
|
|
|
else if (IsA(node, ArrayCoerceExpr))
|
|
|
|
{
|
|
|
|
ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
|
|
|
|
Node *arraynode = (Node *) acoerce->arg;
|
|
|
|
|
|
|
|
if (OidIsValid(acoerce->elemfuncid))
|
|
|
|
context->total.per_tuple += get_func_cost(acoerce->elemfuncid) *
|
|
|
|
cpu_operator_cost * estimate_array_length(arraynode);
|
|
|
|
}
|
2005-12-28 02:30:02 +01:00
|
|
|
else if (IsA(node, RowCompareExpr))
|
|
|
|
{
|
|
|
|
/* Conservatively assume we will check all the columns */
|
|
|
|
RowCompareExpr *rcexpr = (RowCompareExpr *) node;
|
2007-01-22 02:35:23 +01:00
|
|
|
ListCell *lc;
|
2005-12-28 02:30:02 +01:00
|
|
|
|
2007-01-22 02:35:23 +01:00
|
|
|
foreach(lc, rcexpr->opnos)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid opid = lfirst_oid(lc);
|
2007-01-22 02:35:23 +01:00
|
|
|
|
2007-02-22 23:00:26 +01:00
|
|
|
context->total.per_tuple += get_func_cost(get_opcode(opid)) *
|
2007-01-22 02:35:23 +01:00
|
|
|
cpu_operator_cost;
|
|
|
|
}
|
2005-12-28 02:30:02 +01:00
|
|
|
}
|
2007-06-11 03:16:30 +02:00
|
|
|
else if (IsA(node, CurrentOfExpr))
|
|
|
|
{
|
2007-10-24 20:37:09 +02:00
|
|
|
/* Report high cost to prevent selection of anything but TID scan */
|
|
|
|
context->total.startup += disable_cost;
|
2007-06-11 03:16:30 +02:00
|
|
|
}
|
2003-01-12 23:35:29 +01:00
|
|
|
else if (IsA(node, SubLink))
|
|
|
|
{
|
|
|
|
/* This routine should not be applied to un-planned expressions */
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "cannot handle unplanned sub-select");
|
2003-01-12 23:35:29 +01:00
|
|
|
}
|
2002-12-14 01:17:59 +01:00
|
|
|
else if (IsA(node, SubPlan))
|
2000-02-15 21:49:31 +01:00
|
|
|
{
|
2002-12-12 16:49:42 +01:00
|
|
|
/*
|
2003-01-12 23:35:29 +01:00
|
|
|
* A subplan node in an expression typically indicates that the
|
2005-10-15 04:49:52 +02:00
|
|
|
* subplan will be executed on each evaluation, so charge accordingly.
|
|
|
|
* (Sub-selects that can be executed as InitPlans have already been
|
|
|
|
* removed from the expression.)
|
2002-12-12 16:49:42 +01:00
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
SubPlan *subplan = (SubPlan *) node;
|
2000-02-15 21:49:31 +01:00
|
|
|
|
2008-08-22 02:16:04 +02:00
|
|
|
context->total.startup += subplan->startup_cost;
|
|
|
|
context->total.per_tuple += subplan->per_call_cost;
|
2003-01-12 23:35:29 +01:00
|
|
|
|
2008-08-22 02:16:04 +02:00
|
|
|
/*
|
|
|
|
* We don't want to recurse into the testexpr, because it was already
|
|
|
|
* counted in the SubPlan node's costs. So we're done.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (IsA(node, AlternativeSubPlan))
|
|
|
|
{
|
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Arbitrarily use the first alternative plan for costing. (We should
|
2008-08-22 02:16:04 +02:00
|
|
|
* certainly only include one alternative, and we don't yet have
|
2009-06-11 16:49:15 +02:00
|
|
|
* enough information to know which one the executor is most likely to
|
|
|
|
* use.)
|
2008-08-22 02:16:04 +02:00
|
|
|
*/
|
|
|
|
AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2008-08-22 02:16:04 +02:00
|
|
|
return cost_qual_eval_walker((Node *) linitial(asplan->subplans),
|
|
|
|
context);
|
2000-02-15 21:49:31 +01:00
|
|
|
}
|
2002-12-12 16:49:42 +01:00
|
|
|
|
2007-01-22 02:35:23 +01:00
|
|
|
/* recurse into children */
|
2000-02-15 21:49:31 +01:00
|
|
|
return expression_tree_walker(node, cost_qual_eval_walker,
|
2007-02-22 23:00:26 +01:00
|
|
|
(void *) context);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-02-15 21:49:31 +01:00
|
|
|
|
2009-05-10 00:51:41 +02:00
|
|
|
/*
|
|
|
|
* adjust_semi_join
|
|
|
|
* Estimate how much of the inner input a SEMI or ANTI join
|
|
|
|
* can be expected to scan.
|
|
|
|
*
|
|
|
|
* In a hash or nestloop SEMI/ANTI join, the executor will stop scanning
|
|
|
|
* inner rows as soon as it finds a match to the current outer row.
|
|
|
|
* We should therefore adjust some of the cost components for this effect.
|
|
|
|
* This function computes some estimates needed for these adjustments.
|
|
|
|
*
|
|
|
|
* 'path' is already filled in except for the cost fields
|
|
|
|
* 'sjinfo' is extra info about the join for selectivity estimation
|
|
|
|
*
|
|
|
|
* Returns TRUE if this is a SEMI or ANTI join, FALSE if not.
|
|
|
|
*
|
|
|
|
* Output parameters (set only in TRUE-result case):
|
|
|
|
* *outer_match_frac is set to the fraction of the outer tuples that are
|
|
|
|
* expected to have at least one match.
|
|
|
|
* *match_count is set to the average number of matches expected for
|
|
|
|
* outer tuples that have at least one match.
|
|
|
|
* *indexed_join_quals is set to TRUE if all the joinquals are used as
|
|
|
|
* inner index quals, FALSE if not.
|
|
|
|
*
|
|
|
|
* indexed_join_quals can be passed as NULL if that information is not
|
|
|
|
* relevant (it is only useful for the nestloop case).
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
adjust_semi_join(PlannerInfo *root, JoinPath *path, SpecialJoinInfo *sjinfo,
|
|
|
|
Selectivity *outer_match_frac,
|
|
|
|
Selectivity *match_count,
|
|
|
|
bool *indexed_join_quals)
|
|
|
|
{
|
|
|
|
JoinType jointype = path->jointype;
|
|
|
|
Selectivity jselec;
|
|
|
|
Selectivity nselec;
|
|
|
|
Selectivity avgmatch;
|
|
|
|
SpecialJoinInfo norm_sjinfo;
|
|
|
|
List *joinquals;
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
/* Fall out if it's not JOIN_SEMI or JOIN_ANTI */
|
|
|
|
if (jointype != JOIN_SEMI && jointype != JOIN_ANTI)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: it's annoying to repeat this selectivity estimation on each call,
|
|
|
|
* when the joinclause list will be the same for all path pairs
|
|
|
|
* implementing a given join. clausesel.c will save us from the worst
|
|
|
|
* effects of this by caching at the RestrictInfo level; but perhaps it'd
|
|
|
|
* be worth finding a way to cache the results at a higher level.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* In an ANTI join, we must ignore clauses that are "pushed down", since
|
|
|
|
* those won't affect the match logic. In a SEMI join, we do not
|
2009-05-10 00:51:41 +02:00
|
|
|
* distinguish joinquals from "pushed down" quals, so just use the whole
|
|
|
|
* restrictinfo list.
|
|
|
|
*/
|
|
|
|
if (jointype == JOIN_ANTI)
|
|
|
|
{
|
|
|
|
joinquals = NIL;
|
|
|
|
foreach(l, path->joinrestrictinfo)
|
|
|
|
{
|
|
|
|
RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
|
|
|
|
|
|
|
|
Assert(IsA(rinfo, RestrictInfo));
|
|
|
|
if (!rinfo->is_pushed_down)
|
|
|
|
joinquals = lappend(joinquals, rinfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
joinquals = path->joinrestrictinfo;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the JOIN_SEMI or JOIN_ANTI selectivity of the join clauses.
|
|
|
|
*/
|
|
|
|
jselec = clauselist_selectivity(root,
|
|
|
|
joinquals,
|
|
|
|
0,
|
|
|
|
jointype,
|
|
|
|
sjinfo);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Also get the normal inner-join selectivity of the join clauses.
|
|
|
|
*/
|
|
|
|
norm_sjinfo.type = T_SpecialJoinInfo;
|
|
|
|
norm_sjinfo.min_lefthand = path->outerjoinpath->parent->relids;
|
|
|
|
norm_sjinfo.min_righthand = path->innerjoinpath->parent->relids;
|
|
|
|
norm_sjinfo.syn_lefthand = path->outerjoinpath->parent->relids;
|
|
|
|
norm_sjinfo.syn_righthand = path->innerjoinpath->parent->relids;
|
|
|
|
norm_sjinfo.jointype = JOIN_INNER;
|
|
|
|
/* we don't bother trying to make the remaining fields valid */
|
|
|
|
norm_sjinfo.lhs_strict = false;
|
|
|
|
norm_sjinfo.delay_upper_joins = false;
|
|
|
|
norm_sjinfo.join_quals = NIL;
|
|
|
|
|
|
|
|
nselec = clauselist_selectivity(root,
|
|
|
|
joinquals,
|
|
|
|
0,
|
|
|
|
JOIN_INNER,
|
|
|
|
&norm_sjinfo);
|
|
|
|
|
|
|
|
/* Avoid leaking a lot of ListCells */
|
|
|
|
if (jointype == JOIN_ANTI)
|
|
|
|
list_free(joinquals);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* jselec can be interpreted as the fraction of outer-rel rows that have
|
2009-06-11 16:49:15 +02:00
|
|
|
* any matches (this is true for both SEMI and ANTI cases). And nselec is
|
|
|
|
* the fraction of the Cartesian product that matches. So, the average
|
|
|
|
* number of matches for each outer-rel row that has at least one match is
|
|
|
|
* nselec * inner_rows / jselec.
|
2009-05-10 00:51:41 +02:00
|
|
|
*
|
|
|
|
* Note: it is correct to use the inner rel's "rows" count here, not
|
|
|
|
* PATH_ROWS(), even if the inner path under consideration is an inner
|
2009-06-11 16:49:15 +02:00
|
|
|
* indexscan. This is because we have included all the join clauses in
|
|
|
|
* the selectivity estimate, even ones used in an inner indexscan.
|
2009-05-10 00:51:41 +02:00
|
|
|
*/
|
|
|
|
if (jselec > 0) /* protect against zero divide */
|
|
|
|
{
|
|
|
|
avgmatch = nselec * path->innerjoinpath->parent->rows / jselec;
|
|
|
|
/* Clamp to sane range */
|
|
|
|
avgmatch = Max(1.0, avgmatch);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
avgmatch = 1.0;
|
|
|
|
|
|
|
|
*outer_match_frac = jselec;
|
|
|
|
*match_count = avgmatch;
|
|
|
|
|
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* If requested, check whether the inner path uses all the joinquals as
|
|
|
|
* indexquals. (If that's true, we can assume that an unmatched outer
|
|
|
|
* tuple is cheap to process, whereas otherwise it's probably expensive.)
|
2009-05-10 00:51:41 +02:00
|
|
|
*/
|
|
|
|
if (indexed_join_quals)
|
|
|
|
{
|
2010-11-02 23:45:36 +01:00
|
|
|
if (path->joinrestrictinfo != NIL)
|
|
|
|
{
|
|
|
|
List *nrclauses;
|
2009-05-10 00:51:41 +02:00
|
|
|
|
2010-11-02 23:45:36 +01:00
|
|
|
nrclauses = select_nonredundant_join_clauses(root,
|
|
|
|
path->joinrestrictinfo,
|
|
|
|
path->innerjoinpath);
|
|
|
|
*indexed_join_quals = (nrclauses == NIL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* a clauseless join does NOT qualify */
|
|
|
|
*indexed_join_quals = false;
|
|
|
|
}
|
2009-05-10 00:51:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-05 07:26:05 +02:00
|
|
|
/*
|
2008-08-16 02:01:38 +02:00
|
|
|
* approx_tuple_count
|
|
|
|
* Quick-and-dirty estimation of the number of join rows passing
|
|
|
|
* a set of qual conditions.
|
2001-06-05 07:26:05 +02:00
|
|
|
*
|
2008-08-16 02:01:38 +02:00
|
|
|
* The quals can be either an implicitly-ANDed list of boolean expressions,
|
|
|
|
* or a list of RestrictInfo nodes (typically the latter).
|
2008-08-14 20:48:00 +02:00
|
|
|
*
|
2009-02-07 00:43:24 +01:00
|
|
|
* We intentionally compute the selectivity under JOIN_INNER rules, even
|
|
|
|
* if it's some type of outer join. This is appropriate because we are
|
|
|
|
* trying to figure out how many tuples pass the initial merge or hash
|
|
|
|
* join step.
|
|
|
|
*
|
2004-01-04 04:51:52 +01:00
|
|
|
* This is quick-and-dirty because we bypass clauselist_selectivity, and
|
|
|
|
* simply multiply the independent clause selectivities together. Now
|
2002-03-01 07:01:20 +01:00
|
|
|
* clauselist_selectivity often can't do any better than that anyhow, but
|
2004-01-04 04:51:52 +01:00
|
|
|
* for some situations (such as range constraints) it is smarter. However,
|
|
|
|
* we can't effectively cache the results of clauselist_selectivity, whereas
|
|
|
|
* the individual clause selectivities can be and are cached.
|
2001-06-05 07:26:05 +02:00
|
|
|
*
|
|
|
|
* Since we are only using the results to estimate how many potential
|
|
|
|
* output tuples are generated and passed through qpqual checking, it
|
|
|
|
* seems OK to live with the approximation.
|
|
|
|
*/
|
2008-08-16 02:01:38 +02:00
|
|
|
static double
|
2009-02-07 00:43:24 +01:00
|
|
|
approx_tuple_count(PlannerInfo *root, JoinPath *path, List *quals)
|
2001-06-05 07:26:05 +02:00
|
|
|
{
|
2008-08-16 02:01:38 +02:00
|
|
|
double tuples;
|
|
|
|
double outer_tuples = path->outerjoinpath->parent->rows;
|
|
|
|
double inner_tuples = path->innerjoinpath->parent->rows;
|
2009-02-07 00:43:24 +01:00
|
|
|
SpecialJoinInfo sjinfo;
|
2008-08-16 02:01:38 +02:00
|
|
|
Selectivity selec = 1.0;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *l;
|
2001-06-05 07:26:05 +02:00
|
|
|
|
2009-02-07 00:43:24 +01:00
|
|
|
/*
|
|
|
|
* Make up a SpecialJoinInfo for JOIN_INNER semantics.
|
|
|
|
*/
|
|
|
|
sjinfo.type = T_SpecialJoinInfo;
|
|
|
|
sjinfo.min_lefthand = path->outerjoinpath->parent->relids;
|
|
|
|
sjinfo.min_righthand = path->innerjoinpath->parent->relids;
|
|
|
|
sjinfo.syn_lefthand = path->outerjoinpath->parent->relids;
|
|
|
|
sjinfo.syn_righthand = path->innerjoinpath->parent->relids;
|
|
|
|
sjinfo.jointype = JOIN_INNER;
|
|
|
|
/* we don't bother trying to make the remaining fields valid */
|
|
|
|
sjinfo.lhs_strict = false;
|
|
|
|
sjinfo.delay_upper_joins = false;
|
|
|
|
sjinfo.join_quals = NIL;
|
|
|
|
|
2008-08-16 02:01:38 +02:00
|
|
|
/* Get the approximate selectivity */
|
2001-06-05 07:26:05 +02:00
|
|
|
foreach(l, quals)
|
|
|
|
{
|
|
|
|
Node *qual = (Node *) lfirst(l);
|
|
|
|
|
2004-01-04 04:51:52 +01:00
|
|
|
/* Note that clause_selectivity will be able to cache its result */
|
2009-02-07 00:43:24 +01:00
|
|
|
selec *= clause_selectivity(root, qual, 0, JOIN_INNER, &sjinfo);
|
2001-06-05 07:26:05 +02:00
|
|
|
}
|
2008-08-16 02:01:38 +02:00
|
|
|
|
2009-02-07 00:43:24 +01:00
|
|
|
/* Apply it to the input relation sizes */
|
|
|
|
tuples = selec * outer_tuples * inner_tuples;
|
2008-08-16 02:01:38 +02:00
|
|
|
|
|
|
|
return clamp_row_est(tuples);
|
2001-06-05 07:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2000-02-07 05:41:04 +01:00
|
|
|
* set_baserel_size_estimates
|
|
|
|
* Set the size estimates for the given base relation.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2000-02-07 05:41:04 +01:00
|
|
|
* The rel's targetlist and restrictinfo list must have been constructed
|
2010-11-19 23:31:50 +01:00
|
|
|
* already, and rel->tuples must be set.
|
2000-02-07 05:41:04 +01:00
|
|
|
*
|
|
|
|
* We set the following fields of the rel node:
|
|
|
|
* rows: the estimated number of output tuples (after applying
|
2000-04-12 19:17:23 +02:00
|
|
|
* restriction clauses).
|
2000-02-07 05:41:04 +01:00
|
|
|
* width: the estimated average output tuple width in bytes.
|
2000-02-15 21:49:31 +01:00
|
|
|
* baserestrictcost: estimated cost of evaluating baserestrictinfo clauses.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-01-09 01:26:47 +01:00
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-01-05 06:07:36 +01:00
|
|
|
double nrows;
|
2002-12-13 18:29:25 +01:00
|
|
|
|
2000-01-09 01:26:47 +01:00
|
|
|
/* Should only be applied to base relations */
|
2003-02-08 21:20:55 +01:00
|
|
|
Assert(rel->relid > 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-01-05 06:07:36 +01:00
|
|
|
nrows = rel->tuples *
|
2004-01-04 04:51:52 +01:00
|
|
|
clauselist_selectivity(root,
|
|
|
|
rel->baserestrictinfo,
|
|
|
|
0,
|
2008-08-14 20:48:00 +02:00
|
|
|
JOIN_INNER,
|
|
|
|
NULL);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2004-01-05 06:07:36 +01:00
|
|
|
rel->rows = clamp_row_est(nrows);
|
2000-02-15 21:49:31 +01:00
|
|
|
|
2007-02-22 23:00:26 +01:00
|
|
|
cost_qual_eval(&rel->baserestrictcost, rel->baserestrictinfo, root);
|
2000-01-09 01:26:47 +01:00
|
|
|
|
|
|
|
set_rel_width(root, rel);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2000-02-07 05:41:04 +01:00
|
|
|
* set_joinrel_size_estimates
|
|
|
|
* Set the size estimates for the given join relation.
|
|
|
|
*
|
|
|
|
* The rel's targetlist must have been constructed already, and a
|
|
|
|
* restriction clause list that matches the given component rels must
|
|
|
|
* be provided.
|
|
|
|
*
|
|
|
|
* Since there is more than one way to make a joinrel for more than two
|
|
|
|
* base relations, the results we get here could depend on which component
|
|
|
|
* rel pair is provided. In theory we should get the same answers no matter
|
|
|
|
* which pair is provided; in practice, since the selectivity estimation
|
|
|
|
* routines don't handle all cases equally well, we might not. But there's
|
|
|
|
* not much to be done about it. (Would it make sense to repeat the
|
|
|
|
* calculations for each pair of input rels that's encountered, and somehow
|
|
|
|
* average the results? Probably way more trouble than it's worth.)
|
|
|
|
*
|
2004-01-05 06:07:36 +01:00
|
|
|
* We set only the rows field here. The width field was already set by
|
|
|
|
* build_joinrel_tlist, and baserestrictcost is not used for join rels.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-01-09 01:26:47 +01:00
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
|
2000-02-07 05:41:04 +01:00
|
|
|
RelOptInfo *outer_rel,
|
|
|
|
RelOptInfo *inner_rel,
|
2008-08-14 20:48:00 +02:00
|
|
|
SpecialJoinInfo *sjinfo,
|
2000-02-07 05:41:04 +01:00
|
|
|
List *restrictlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2008-08-14 20:48:00 +02:00
|
|
|
JoinType jointype = sjinfo->jointype;
|
2006-11-10 02:21:41 +01:00
|
|
|
Selectivity jselec;
|
|
|
|
Selectivity pselec;
|
2004-01-05 06:07:36 +01:00
|
|
|
double nrows;
|
2000-01-09 01:26:47 +01:00
|
|
|
|
2000-02-07 05:41:04 +01:00
|
|
|
/*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Compute joinclause selectivity. Note that we are only considering
|
2005-10-15 04:49:52 +02:00
|
|
|
* clauses that become restriction clauses at this join level; we are not
|
|
|
|
* double-counting them because they were not considered in estimating the
|
|
|
|
* sizes of the component rels.
|
2006-11-10 02:21:41 +01:00
|
|
|
*
|
2007-11-15 22:14:46 +01:00
|
|
|
* For an outer join, we have to distinguish the selectivity of the join's
|
|
|
|
* own clauses (JOIN/ON conditions) from any clauses that were "pushed
|
|
|
|
* down". For inner joins we just count them all as joinclauses.
|
2000-02-07 05:41:04 +01:00
|
|
|
*/
|
2006-11-10 02:21:41 +01:00
|
|
|
if (IS_OUTER_JOIN(jointype))
|
|
|
|
{
|
|
|
|
List *joinquals = NIL;
|
|
|
|
List *pushedquals = NIL;
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
/* Grovel through the clauses to separate into two lists */
|
|
|
|
foreach(l, restrictlist)
|
|
|
|
{
|
|
|
|
RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
|
|
|
|
|
|
|
|
Assert(IsA(rinfo, RestrictInfo));
|
|
|
|
if (rinfo->is_pushed_down)
|
|
|
|
pushedquals = lappend(pushedquals, rinfo);
|
|
|
|
else
|
|
|
|
joinquals = lappend(joinquals, rinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the separate selectivities */
|
|
|
|
jselec = clauselist_selectivity(root,
|
|
|
|
joinquals,
|
|
|
|
0,
|
2008-08-14 20:48:00 +02:00
|
|
|
jointype,
|
|
|
|
sjinfo);
|
2006-11-10 02:21:41 +01:00
|
|
|
pselec = clauselist_selectivity(root,
|
|
|
|
pushedquals,
|
|
|
|
0,
|
2008-08-14 20:48:00 +02:00
|
|
|
jointype,
|
|
|
|
sjinfo);
|
2006-11-10 02:21:41 +01:00
|
|
|
|
|
|
|
/* Avoid leaking a lot of ListCells */
|
|
|
|
list_free(joinquals);
|
|
|
|
list_free(pushedquals);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
jselec = clauselist_selectivity(root,
|
|
|
|
restrictlist,
|
|
|
|
0,
|
2008-08-14 20:48:00 +02:00
|
|
|
jointype,
|
|
|
|
sjinfo);
|
2006-11-10 02:21:41 +01:00
|
|
|
pselec = 0.0; /* not used, keep compiler quiet */
|
|
|
|
}
|
2000-01-09 01:26:47 +01:00
|
|
|
|
2001-02-16 01:03:08 +01:00
|
|
|
/*
|
2003-01-27 21:51:54 +01:00
|
|
|
* Basically, we multiply size of Cartesian product by selectivity.
|
2003-01-20 19:55:07 +01:00
|
|
|
*
|
2006-11-10 02:21:41 +01:00
|
|
|
* If we are doing an outer join, take that into account: the joinqual
|
|
|
|
* selectivity has to be clamped using the knowledge that the output must
|
2007-11-15 22:14:46 +01:00
|
|
|
* be at least as large as the non-nullable input. However, any
|
2006-11-10 02:21:41 +01:00
|
|
|
* pushed-down quals are applied after the outer join, so their
|
|
|
|
* selectivity applies fully.
|
2003-01-27 21:51:54 +01:00
|
|
|
*
|
2008-08-16 02:01:38 +02:00
|
|
|
* For JOIN_SEMI and JOIN_ANTI, the selectivity is defined as the fraction
|
|
|
|
* of LHS rows that have matches, and we apply that straightforwardly.
|
2001-02-16 01:03:08 +01:00
|
|
|
*/
|
|
|
|
switch (jointype)
|
|
|
|
{
|
|
|
|
case JOIN_INNER:
|
2006-11-10 02:21:41 +01:00
|
|
|
nrows = outer_rel->rows * inner_rel->rows * jselec;
|
2001-02-16 01:03:08 +01:00
|
|
|
break;
|
|
|
|
case JOIN_LEFT:
|
2006-11-10 02:21:41 +01:00
|
|
|
nrows = outer_rel->rows * inner_rel->rows * jselec;
|
2004-01-05 06:07:36 +01:00
|
|
|
if (nrows < outer_rel->rows)
|
|
|
|
nrows = outer_rel->rows;
|
2006-11-10 02:21:41 +01:00
|
|
|
nrows *= pselec;
|
2001-02-16 01:03:08 +01:00
|
|
|
break;
|
|
|
|
case JOIN_FULL:
|
2006-11-10 02:21:41 +01:00
|
|
|
nrows = outer_rel->rows * inner_rel->rows * jselec;
|
2004-01-05 06:07:36 +01:00
|
|
|
if (nrows < outer_rel->rows)
|
|
|
|
nrows = outer_rel->rows;
|
|
|
|
if (nrows < inner_rel->rows)
|
|
|
|
nrows = inner_rel->rows;
|
2006-11-10 02:21:41 +01:00
|
|
|
nrows *= pselec;
|
2001-02-16 01:03:08 +01:00
|
|
|
break;
|
2008-08-14 20:48:00 +02:00
|
|
|
case JOIN_SEMI:
|
2008-08-16 02:01:38 +02:00
|
|
|
nrows = outer_rel->rows * jselec;
|
2008-11-22 23:47:06 +01:00
|
|
|
/* pselec not used */
|
2003-01-20 19:55:07 +01:00
|
|
|
break;
|
2008-08-14 20:48:00 +02:00
|
|
|
case JOIN_ANTI:
|
2008-08-16 02:01:38 +02:00
|
|
|
nrows = outer_rel->rows * (1.0 - jselec);
|
2008-08-14 20:48:00 +02:00
|
|
|
nrows *= pselec;
|
2003-01-20 19:55:07 +01:00
|
|
|
break;
|
2001-02-16 01:03:08 +01:00
|
|
|
default:
|
2008-08-14 20:48:00 +02:00
|
|
|
/* other values not expected here */
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "unrecognized join type: %d", (int) jointype);
|
2004-01-05 06:07:36 +01:00
|
|
|
nrows = 0; /* keep compiler quiet */
|
2001-02-16 01:03:08 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-01-05 06:07:36 +01:00
|
|
|
rel->rows = clamp_row_est(nrows);
|
|
|
|
}
|
|
|
|
|
2010-11-19 23:31:50 +01:00
|
|
|
/*
|
|
|
|
* set_subquery_size_estimates
|
|
|
|
* Set the size estimates for a base relation that is a subquery.
|
|
|
|
*
|
|
|
|
* The rel's targetlist and restrictinfo list must have been constructed
|
|
|
|
* already, and the plan for the subquery must have been completed.
|
|
|
|
* We look at the subquery's plan and PlannerInfo to extract data.
|
|
|
|
*
|
|
|
|
* We set the same fields as set_baserel_size_estimates.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
set_subquery_size_estimates(PlannerInfo *root, RelOptInfo *rel,
|
|
|
|
PlannerInfo *subroot)
|
|
|
|
{
|
|
|
|
RangeTblEntry *rte;
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
/* Should only be applied to base relations that are subqueries */
|
|
|
|
Assert(rel->relid > 0);
|
|
|
|
rte = planner_rt_fetch(rel->relid, root);
|
|
|
|
Assert(rte->rtekind == RTE_SUBQUERY);
|
|
|
|
|
|
|
|
/* Copy raw number of output rows from subplan */
|
|
|
|
rel->tuples = rel->subplan->plan_rows;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute per-output-column width estimates by examining the subquery's
|
|
|
|
* targetlist. For any output that is a plain Var, get the width estimate
|
|
|
|
* that was made while planning the subquery. Otherwise, fall back on a
|
|
|
|
* datatype-based estimate.
|
|
|
|
*/
|
|
|
|
foreach(lc, subroot->parse->targetList)
|
|
|
|
{
|
|
|
|
TargetEntry *te = (TargetEntry *) lfirst(lc);
|
|
|
|
Node *texpr = (Node *) te->expr;
|
|
|
|
int32 item_width;
|
|
|
|
|
|
|
|
Assert(IsA(te, TargetEntry));
|
|
|
|
/* junk columns aren't visible to upper query */
|
|
|
|
if (te->resjunk)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX This currently doesn't work for subqueries containing set
|
|
|
|
* operations, because the Vars in their tlists are bogus references
|
|
|
|
* to the first leaf subquery, which wouldn't give the right answer
|
|
|
|
* even if we could still get to its PlannerInfo. So fall back on
|
|
|
|
* datatype in that case.
|
|
|
|
*/
|
|
|
|
if (IsA(texpr, Var) &&
|
|
|
|
subroot->parse->setOperations == NULL)
|
|
|
|
{
|
|
|
|
Var *var = (Var *) texpr;
|
|
|
|
RelOptInfo *subrel = find_base_rel(subroot, var->varno);
|
|
|
|
|
|
|
|
item_width = subrel->attr_widths[var->varattno - subrel->min_attr];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item_width = get_typavgwidth(exprType(texpr), exprTypmod(texpr));
|
|
|
|
}
|
|
|
|
Assert(item_width > 0);
|
|
|
|
Assert(te->resno >= rel->min_attr && te->resno <= rel->max_attr);
|
|
|
|
rel->attr_widths[te->resno - rel->min_attr] = item_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now estimate number of output rows, etc */
|
|
|
|
set_baserel_size_estimates(root, rel);
|
|
|
|
}
|
|
|
|
|
2002-05-12 22:10:05 +02:00
|
|
|
/*
|
|
|
|
* set_function_size_estimates
|
|
|
|
* Set the size estimates for a base relation that is a function call.
|
|
|
|
*
|
|
|
|
* The rel's targetlist and restrictinfo list must have been constructed
|
|
|
|
* already.
|
|
|
|
*
|
2004-01-05 06:07:36 +01:00
|
|
|
* We set the same fields as set_baserel_size_estimates.
|
2002-05-12 22:10:05 +02:00
|
|
|
*/
|
|
|
|
void
|
2005-06-06 00:32:58 +02:00
|
|
|
set_function_size_estimates(PlannerInfo *root, RelOptInfo *rel)
|
2002-05-12 22:10:05 +02:00
|
|
|
{
|
2005-10-05 19:19:19 +02:00
|
|
|
RangeTblEntry *rte;
|
|
|
|
|
2002-05-12 22:10:05 +02:00
|
|
|
/* Should only be applied to base relations that are functions */
|
2003-02-08 21:20:55 +01:00
|
|
|
Assert(rel->relid > 0);
|
2007-04-21 23:01:45 +02:00
|
|
|
rte = planner_rt_fetch(rel->relid, root);
|
2005-10-05 19:19:19 +02:00
|
|
|
Assert(rte->rtekind == RTE_FUNCTION);
|
2002-05-12 22:10:05 +02:00
|
|
|
|
2007-01-22 02:35:23 +01:00
|
|
|
/* Estimate number of rows the function itself will return */
|
|
|
|
rel->tuples = clamp_row_est(expression_returns_set_rows(rte->funcexpr));
|
2002-05-12 22:10:05 +02:00
|
|
|
|
2004-01-05 06:07:36 +01:00
|
|
|
/* Now estimate number of output rows, etc */
|
|
|
|
set_baserel_size_estimates(root, rel);
|
2002-05-12 22:10:05 +02:00
|
|
|
}
|
|
|
|
|
2006-08-02 03:59:48 +02:00
|
|
|
/*
|
|
|
|
* set_values_size_estimates
|
|
|
|
* Set the size estimates for a base relation that is a values list.
|
|
|
|
*
|
|
|
|
* The rel's targetlist and restrictinfo list must have been constructed
|
|
|
|
* already.
|
|
|
|
*
|
|
|
|
* We set the same fields as set_baserel_size_estimates.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
set_values_size_estimates(PlannerInfo *root, RelOptInfo *rel)
|
|
|
|
{
|
|
|
|
RangeTblEntry *rte;
|
|
|
|
|
|
|
|
/* Should only be applied to base relations that are values lists */
|
|
|
|
Assert(rel->relid > 0);
|
2007-04-21 23:01:45 +02:00
|
|
|
rte = planner_rt_fetch(rel->relid, root);
|
2006-08-02 03:59:48 +02:00
|
|
|
Assert(rte->rtekind == RTE_VALUES);
|
|
|
|
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Estimate number of rows the values list will return. We know this
|
|
|
|
* precisely based on the list length (well, barring set-returning
|
|
|
|
* functions in list items, but that's a refinement not catered for
|
|
|
|
* anywhere else either).
|
2006-08-02 03:59:48 +02:00
|
|
|
*/
|
|
|
|
rel->tuples = list_length(rte->values_lists);
|
|
|
|
|
|
|
|
/* Now estimate number of output rows, etc */
|
|
|
|
set_baserel_size_estimates(root, rel);
|
|
|
|
}
|
|
|
|
|
2008-10-04 23:56:55 +02:00
|
|
|
/*
|
|
|
|
* set_cte_size_estimates
|
|
|
|
* Set the size estimates for a base relation that is a CTE reference.
|
|
|
|
*
|
|
|
|
* The rel's targetlist and restrictinfo list must have been constructed
|
|
|
|
* already, and we need the completed plan for the CTE (if a regular CTE)
|
|
|
|
* or the non-recursive term (if a self-reference).
|
|
|
|
*
|
|
|
|
* We set the same fields as set_baserel_size_estimates.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
set_cte_size_estimates(PlannerInfo *root, RelOptInfo *rel, Plan *cteplan)
|
|
|
|
{
|
|
|
|
RangeTblEntry *rte;
|
|
|
|
|
|
|
|
/* Should only be applied to base relations that are CTE references */
|
|
|
|
Assert(rel->relid > 0);
|
|
|
|
rte = planner_rt_fetch(rel->relid, root);
|
|
|
|
Assert(rte->rtekind == RTE_CTE);
|
|
|
|
|
|
|
|
if (rte->self_reference)
|
|
|
|
{
|
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* In a self-reference, arbitrarily assume the average worktable size
|
|
|
|
* is about 10 times the nonrecursive term's size.
|
2008-10-04 23:56:55 +02:00
|
|
|
*/
|
|
|
|
rel->tuples = 10 * cteplan->plan_rows;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise just believe the CTE plan's output estimate */
|
|
|
|
rel->tuples = cteplan->plan_rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now estimate number of output rows, etc */
|
|
|
|
set_baserel_size_estimates(root, rel);
|
|
|
|
}
|
|
|
|
|
2002-05-12 22:10:05 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2000-01-09 01:26:47 +01:00
|
|
|
* set_rel_width
|
2003-06-30 01:05:05 +02:00
|
|
|
* Set the estimated output width of a base relation.
|
2001-05-09 02:35:09 +02:00
|
|
|
*
|
2010-11-19 23:31:50 +01:00
|
|
|
* The estimated output width is the sum of the per-attribute width estimates
|
|
|
|
* for the actually-referenced columns, plus any PHVs or other expressions
|
|
|
|
* that have to be calculated at this relation. This is the amount of data
|
|
|
|
* we'd need to pass upwards in case of a sort, hash, etc.
|
|
|
|
*
|
2003-06-30 01:05:05 +02:00
|
|
|
* NB: this works best on plain relations because it prefers to look at
|
2010-11-19 23:31:50 +01:00
|
|
|
* real Vars. For subqueries, set_subquery_size_estimates will already have
|
|
|
|
* copied up whatever per-column estimates were made within the subquery,
|
|
|
|
* and for other types of rels there isn't much we can do anyway. We fall
|
|
|
|
* back on (fairly stupid) datatype-based width estimates if we can't get
|
|
|
|
* any better number.
|
2003-06-30 01:05:05 +02:00
|
|
|
*
|
|
|
|
* The per-attribute width estimates are cached for possible re-use while
|
|
|
|
* building join relations.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-01-09 01:26:47 +01:00
|
|
|
static void
|
2005-06-06 00:32:58 +02:00
|
|
|
set_rel_width(PlannerInfo *root, RelOptInfo *rel)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2008-10-17 22:27:24 +02:00
|
|
|
Oid reloid = planner_rt_fetch(rel->relid, root)->relid;
|
2001-05-09 02:35:09 +02:00
|
|
|
int32 tuple_width = 0;
|
2010-11-19 23:31:50 +01:00
|
|
|
bool have_wholerow_var = false;
|
2008-10-21 22:42:53 +02:00
|
|
|
ListCell *lc;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2008-10-21 22:42:53 +02:00
|
|
|
foreach(lc, rel->reltargetlist)
|
2001-05-09 02:35:09 +02:00
|
|
|
{
|
2008-10-21 22:42:53 +02:00
|
|
|
Node *node = (Node *) lfirst(lc);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2008-10-21 22:42:53 +02:00
|
|
|
if (IsA(node, Var))
|
2004-06-05 03:55:05 +02:00
|
|
|
{
|
2008-10-21 22:42:53 +02:00
|
|
|
Var *var = (Var *) node;
|
|
|
|
int ndx;
|
|
|
|
int32 item_width;
|
2008-10-17 22:27:24 +02:00
|
|
|
|
2008-10-21 22:42:53 +02:00
|
|
|
Assert(var->varno == rel->relid);
|
|
|
|
Assert(var->varattno >= rel->min_attr);
|
|
|
|
Assert(var->varattno <= rel->max_attr);
|
2003-06-30 01:05:05 +02:00
|
|
|
|
2008-10-21 22:42:53 +02:00
|
|
|
ndx = var->varattno - rel->min_attr;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2008-10-21 22:42:53 +02:00
|
|
|
/*
|
2010-11-19 23:31:50 +01:00
|
|
|
* If it's a whole-row Var, we'll deal with it below after we
|
|
|
|
* have already cached as many attr widths as possible.
|
|
|
|
*/
|
|
|
|
if (var->varattno == 0)
|
|
|
|
{
|
|
|
|
have_wholerow_var = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The width may have been cached already (especially if it's
|
|
|
|
* a subquery), so don't duplicate effort.
|
2008-10-21 22:42:53 +02:00
|
|
|
*/
|
|
|
|
if (rel->attr_widths[ndx] > 0)
|
2001-05-09 02:35:09 +02:00
|
|
|
{
|
2008-10-21 22:42:53 +02:00
|
|
|
tuple_width += rel->attr_widths[ndx];
|
2003-06-30 01:05:05 +02:00
|
|
|
continue;
|
2001-05-09 02:35:09 +02:00
|
|
|
}
|
2008-10-21 22:42:53 +02:00
|
|
|
|
|
|
|
/* Try to get column width from statistics */
|
2010-11-19 23:31:50 +01:00
|
|
|
if (reloid != InvalidOid && var->varattno > 0)
|
2008-10-21 22:42:53 +02:00
|
|
|
{
|
|
|
|
item_width = get_attavgwidth(reloid, var->varattno);
|
|
|
|
if (item_width > 0)
|
|
|
|
{
|
|
|
|
rel->attr_widths[ndx] = item_width;
|
|
|
|
tuple_width += item_width;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Not a plain relation, or can't find statistics for it. Estimate
|
|
|
|
* using just the type info.
|
|
|
|
*/
|
|
|
|
item_width = get_typavgwidth(var->vartype, var->vartypmod);
|
|
|
|
Assert(item_width > 0);
|
|
|
|
rel->attr_widths[ndx] = item_width;
|
|
|
|
tuple_width += item_width;
|
2001-05-09 02:35:09 +02:00
|
|
|
}
|
2008-10-21 22:42:53 +02:00
|
|
|
else if (IsA(node, PlaceHolderVar))
|
|
|
|
{
|
|
|
|
PlaceHolderVar *phv = (PlaceHolderVar *) node;
|
|
|
|
PlaceHolderInfo *phinfo = find_placeholder_info(root, phv);
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2008-10-21 22:42:53 +02:00
|
|
|
tuple_width += phinfo->ph_width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-11 06:09:33 +02:00
|
|
|
/*
|
|
|
|
* We could be looking at an expression pulled up from a subquery,
|
2010-02-26 03:01:40 +01:00
|
|
|
* or a ROW() representing a whole-row child Var, etc. Do what we
|
|
|
|
* can using the expression type information.
|
2009-07-11 06:09:33 +02:00
|
|
|
*/
|
|
|
|
int32 item_width;
|
|
|
|
|
|
|
|
item_width = get_typavgwidth(exprType(node), exprTypmod(node));
|
|
|
|
Assert(item_width > 0);
|
|
|
|
tuple_width += item_width;
|
2008-10-21 22:42:53 +02:00
|
|
|
}
|
2001-05-09 02:35:09 +02:00
|
|
|
}
|
2010-11-19 23:31:50 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have a whole-row reference, estimate its width as the sum of
|
|
|
|
* per-column widths plus sizeof(HeapTupleHeaderData).
|
|
|
|
*/
|
|
|
|
if (have_wholerow_var)
|
|
|
|
{
|
|
|
|
int32 wholerow_width = sizeof(HeapTupleHeaderData);
|
|
|
|
|
|
|
|
if (reloid != InvalidOid)
|
|
|
|
{
|
|
|
|
/* Real relation, so estimate true tuple width */
|
|
|
|
wholerow_width += get_relation_data_width(reloid,
|
|
|
|
rel->attr_widths - rel->min_attr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Do what we can with info for a phony rel */
|
|
|
|
AttrNumber i;
|
|
|
|
|
|
|
|
for (i = 1; i <= rel->max_attr; i++)
|
|
|
|
wholerow_width += rel->attr_widths[i - rel->min_attr];
|
|
|
|
}
|
|
|
|
|
|
|
|
rel->attr_widths[0 - rel->min_attr] = wholerow_width;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Include the whole-row Var as part of the output tuple. Yes,
|
|
|
|
* that really is what happens at runtime.
|
|
|
|
*/
|
|
|
|
tuple_width += wholerow_width;
|
|
|
|
}
|
|
|
|
|
2001-05-09 02:35:09 +02:00
|
|
|
Assert(tuple_width >= 0);
|
|
|
|
rel->width = tuple_width;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-04-05 04:07:07 +02:00
|
|
|
/*
|
|
|
|
* relation_byte_size
|
1999-05-25 18:15:34 +02:00
|
|
|
* Estimate the storage space in bytes for a given number of tuples
|
|
|
|
* of a given width (size in bytes).
|
1999-04-05 04:07:07 +02:00
|
|
|
*/
|
|
|
|
static double
|
2000-01-09 01:26:47 +01:00
|
|
|
relation_byte_size(double tuples, int width)
|
1999-04-05 04:07:07 +02:00
|
|
|
{
|
2003-12-18 04:46:45 +01:00
|
|
|
return tuples * (MAXALIGN(width) + MAXALIGN(sizeof(HeapTupleHeaderData)));
|
1999-04-05 04:07:07 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1999-02-14 00:22:53 +01:00
|
|
|
* page_size
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns an estimate of the number of pages covered by a given
|
|
|
|
* number of tuples of a given width (size in bytes).
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-01-09 01:26:47 +01:00
|
|
|
static double
|
|
|
|
page_size(double tuples, int width)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-01-09 01:26:47 +01:00
|
|
|
return ceil(relation_byte_size(tuples, width) / BLCKSZ);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|