Reset API of clause_selectivity()

Discussion: https://postgr.es/m/CAKJS1f9yurJQW9pdnzL+rmOtsp2vOytkpXKGnMFJEO-qz5O5eA@mail.gmail.com
This commit is contained in:
Simon Riggs 2017-04-06 19:10:51 -04:00
parent 255efa241f
commit ac2b095088
8 changed files with 74 additions and 60 deletions

View File

@ -1013,7 +1013,6 @@ estimate_size(PlannerInfo *root, RelOptInfo *baserel,
baserel->baserestrictinfo, baserel->baserestrictinfo,
0, 0,
JOIN_INNER, JOIN_INNER,
NULL,
NULL); NULL);
nrows = clamp_row_est(nrows); nrows = clamp_row_est(nrows);

View File

@ -591,7 +591,6 @@ postgresGetForeignRelSize(PlannerInfo *root,
fpinfo->local_conds, fpinfo->local_conds,
baserel->relid, baserel->relid,
JOIN_INNER, JOIN_INNER,
NULL,
NULL); NULL);
cost_qual_eval(&fpinfo->local_conds_cost, fpinfo->local_conds, root); cost_qual_eval(&fpinfo->local_conds_cost, fpinfo->local_conds, root);
@ -2573,7 +2572,6 @@ estimate_path_cost_size(PlannerInfo *root,
local_param_join_conds, local_param_join_conds,
foreignrel->relid, foreignrel->relid,
JOIN_INNER, JOIN_INNER,
NULL,
NULL); NULL);
local_sel *= fpinfo->local_conds_sel; local_sel *= fpinfo->local_conds_sel;
@ -4457,7 +4455,6 @@ postgresGetForeignJoinPaths(PlannerInfo *root,
fpinfo->local_conds, fpinfo->local_conds,
0, 0,
JOIN_INNER, JOIN_INNER,
NULL,
NULL); NULL);
cost_qual_eval(&fpinfo->local_conds_cost, fpinfo->local_conds, root); cost_qual_eval(&fpinfo->local_conds_cost, fpinfo->local_conds, root);
@ -4468,7 +4465,7 @@ postgresGetForeignJoinPaths(PlannerInfo *root,
if (!fpinfo->use_remote_estimate) if (!fpinfo->use_remote_estimate)
fpinfo->joinclause_sel = clauselist_selectivity(root, fpinfo->joinclauses, fpinfo->joinclause_sel = clauselist_selectivity(root, fpinfo->joinclauses,
0, fpinfo->jointype, 0, fpinfo->jointype,
extra->sjinfo, NULL); extra->sjinfo);
/* Estimate costs for bare join relation */ /* Estimate costs for bare join relation */
estimate_path_cost_size(root, joinrel, NIL, NIL, &rows, estimate_path_cost_size(root, joinrel, NIL, NIL, &rows,

View File

@ -41,7 +41,8 @@ typedef struct RangeQueryClause
static void addRangeClause(RangeQueryClause **rqlist, Node *clause, static void addRangeClause(RangeQueryClause **rqlist, Node *clause,
bool varonleft, bool isLTsel, Selectivity s2); bool varonleft, bool isLTsel, Selectivity s2);
static RelOptInfo *find_relation_from_clauses(PlannerInfo *root,
List *clauses);
/**************************************************************************** /****************************************************************************
* ROUTINES TO COMPUTE SELECTIVITIES * ROUTINES TO COMPUTE SELECTIVITIES
@ -101,14 +102,14 @@ clauselist_selectivity(PlannerInfo *root,
List *clauses, List *clauses,
int varRelid, int varRelid,
JoinType jointype, JoinType jointype,
SpecialJoinInfo *sjinfo, SpecialJoinInfo *sjinfo)
RelOptInfo *rel)
{ {
Selectivity s1 = 1.0; Selectivity s1 = 1.0;
RangeQueryClause *rqlist = NULL; RangeQueryClause *rqlist = NULL;
ListCell *l; ListCell *l;
Bitmapset *estimatedclauses = NULL; Bitmapset *estimatedclauses = NULL;
int listidx; int listidx;
RelOptInfo *rel;
/* /*
* If there's exactly one clause, then extended statistics is futile at * If there's exactly one clause, then extended statistics is futile at
@ -117,7 +118,14 @@ clauselist_selectivity(PlannerInfo *root,
*/ */
if (list_length(clauses) == 1) if (list_length(clauses) == 1)
return clause_selectivity(root, (Node *) linitial(clauses), return clause_selectivity(root, (Node *) linitial(clauses),
varRelid, jointype, sjinfo, rel); varRelid, jointype, sjinfo);
/*
* Determine if these clauses reference a single relation. If so we might
* like to try applying any extended statistics which exist on it.
* Called many time during joins, so must return NULL quickly if not.
*/
rel = find_relation_from_clauses(root, clauses);
/* /*
* When a relation of RTE_RELATION is given as 'rel', we'll try to * When a relation of RTE_RELATION is given as 'rel', we'll try to
@ -164,7 +172,7 @@ clauselist_selectivity(PlannerInfo *root,
continue; continue;
/* Always compute the selectivity using clause_selectivity */ /* Always compute the selectivity using clause_selectivity */
s2 = clause_selectivity(root, clause, varRelid, jointype, sjinfo, rel); s2 = clause_selectivity(root, clause, varRelid, jointype, sjinfo);
/* /*
* Check for being passed a RestrictInfo. * Check for being passed a RestrictInfo.
@ -417,6 +425,39 @@ addRangeClause(RangeQueryClause **rqlist, Node *clause,
*rqlist = rqelem; *rqlist = rqelem;
} }
/*
* find_relation_from_clauses
* Process each clause in 'clauses' and determine if all clauses
* reference only a single relation. If so return that relation,
* otherwise return NULL.
*/
static RelOptInfo *
find_relation_from_clauses(PlannerInfo *root, List *clauses)
{
ListCell *l;
int relid;
int lastrelid = 0;
foreach(l, clauses)
{
RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
if (bms_get_singleton_member(rinfo->clause_relids, &relid))
{
if (lastrelid != 0 && relid != lastrelid)
return NULL; /* relation not the same as last one */
lastrelid = relid;
}
else
return NULL; /* multiple relations in clause */
}
if (lastrelid != 0)
return find_base_rel(root, lastrelid);
return NULL; /* no clauses */
}
/* /*
* bms_is_subset_singleton * bms_is_subset_singleton
* *
@ -529,8 +570,7 @@ clause_selectivity(PlannerInfo *root,
Node *clause, Node *clause,
int varRelid, int varRelid,
JoinType jointype, JoinType jointype,
SpecialJoinInfo *sjinfo, SpecialJoinInfo *sjinfo)
RelOptInfo *rel)
{ {
Selectivity s1 = 0.5; /* default for any unhandled clause type */ Selectivity s1 = 0.5; /* default for any unhandled clause type */
RestrictInfo *rinfo = NULL; RestrictInfo *rinfo = NULL;
@ -650,8 +690,7 @@ clause_selectivity(PlannerInfo *root,
(Node *) get_notclausearg((Expr *) clause), (Node *) get_notclausearg((Expr *) clause),
varRelid, varRelid,
jointype, jointype,
sjinfo, sjinfo);
rel);
} }
else if (and_clause(clause)) else if (and_clause(clause))
{ {
@ -660,8 +699,7 @@ clause_selectivity(PlannerInfo *root,
((BoolExpr *) clause)->args, ((BoolExpr *) clause)->args,
varRelid, varRelid,
jointype, jointype,
sjinfo, sjinfo);
rel);
} }
else if (or_clause(clause)) else if (or_clause(clause))
{ {
@ -680,8 +718,7 @@ clause_selectivity(PlannerInfo *root,
(Node *) lfirst(arg), (Node *) lfirst(arg),
varRelid, varRelid,
jointype, jointype,
sjinfo, sjinfo);
rel);
s1 = s1 + s2 - s1 * s2; s1 = s1 + s2 - s1 * s2;
} }
@ -774,8 +811,7 @@ clause_selectivity(PlannerInfo *root,
(Node *) ((RelabelType *) clause)->arg, (Node *) ((RelabelType *) clause)->arg,
varRelid, varRelid,
jointype, jointype,
sjinfo, sjinfo);
rel);
} }
else if (IsA(clause, CoerceToDomain)) else if (IsA(clause, CoerceToDomain))
{ {
@ -784,8 +820,7 @@ clause_selectivity(PlannerInfo *root,
(Node *) ((CoerceToDomain *) clause)->arg, (Node *) ((CoerceToDomain *) clause)->arg,
varRelid, varRelid,
jointype, jointype,
sjinfo, sjinfo);
rel);
} }
else else
{ {

View File

@ -3750,8 +3750,7 @@ compute_semi_anti_join_factors(PlannerInfo *root,
joinquals, joinquals,
0, 0,
jointype, jointype,
sjinfo, sjinfo);
NULL);
/* /*
* Also get the normal inner-join selectivity of the join clauses. * Also get the normal inner-join selectivity of the join clauses.
@ -3774,8 +3773,7 @@ compute_semi_anti_join_factors(PlannerInfo *root,
joinquals, joinquals,
0, 0,
JOIN_INNER, JOIN_INNER,
&norm_sjinfo, &norm_sjinfo);
NULL);
/* Avoid leaking a lot of ListCells */ /* Avoid leaking a lot of ListCells */
if (jointype == JOIN_ANTI) if (jointype == JOIN_ANTI)
@ -3942,7 +3940,7 @@ approx_tuple_count(PlannerInfo *root, JoinPath *path, List *quals)
Node *qual = (Node *) lfirst(l); Node *qual = (Node *) lfirst(l);
/* Note that clause_selectivity will be able to cache its result */ /* Note that clause_selectivity will be able to cache its result */
selec *= clause_selectivity(root, qual, 0, JOIN_INNER, &sjinfo, NULL); selec *= clause_selectivity(root, qual, 0, JOIN_INNER, &sjinfo);
} }
/* Apply it to the input relation sizes */ /* Apply it to the input relation sizes */
@ -3978,8 +3976,7 @@ set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
rel->baserestrictinfo, rel->baserestrictinfo,
0, 0,
JOIN_INNER, JOIN_INNER,
NULL, NULL);
rel);
rel->rows = clamp_row_est(nrows); rel->rows = clamp_row_est(nrows);
@ -4016,8 +4013,7 @@ get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel,
allclauses, allclauses,
rel->relid, /* do not use 0! */ rel->relid, /* do not use 0! */
JOIN_INNER, JOIN_INNER,
NULL, NULL);
rel);
nrows = clamp_row_est(nrows); nrows = clamp_row_est(nrows);
/* For safety, make sure result is not more than the base estimate */ /* For safety, make sure result is not more than the base estimate */
if (nrows > rel->rows) if (nrows > rel->rows)
@ -4183,14 +4179,12 @@ calc_joinrel_size_estimate(PlannerInfo *root,
joinquals, joinquals,
0, 0,
jointype, jointype,
sjinfo, sjinfo);
NULL);
pselec = clauselist_selectivity(root, pselec = clauselist_selectivity(root,
pushedquals, pushedquals,
0, 0,
jointype, jointype,
sjinfo, sjinfo);
NULL);
/* Avoid leaking a lot of ListCells */ /* Avoid leaking a lot of ListCells */
list_free(joinquals); list_free(joinquals);
@ -4202,8 +4196,7 @@ calc_joinrel_size_estimate(PlannerInfo *root,
restrictlist, restrictlist,
0, 0,
jointype, jointype,
sjinfo, sjinfo);
NULL);
pselec = 0.0; /* not used, keep compiler quiet */ pselec = 0.0; /* not used, keep compiler quiet */
} }
@ -4498,7 +4491,7 @@ get_foreign_key_join_selectivity(PlannerInfo *root,
Selectivity csel; Selectivity csel;
csel = clause_selectivity(root, (Node *) rinfo, csel = clause_selectivity(root, (Node *) rinfo,
0, jointype, sjinfo, NULL); 0, jointype, sjinfo);
thisfksel = Min(thisfksel, csel); thisfksel = Min(thisfksel, csel);
} }
fkselec *= thisfksel; fkselec *= thisfksel;

View File

@ -280,7 +280,7 @@ consider_new_or_clause(PlannerInfo *root, RelOptInfo *rel,
* saving work later.) * saving work later.)
*/ */
or_selec = clause_selectivity(root, (Node *) or_rinfo, or_selec = clause_selectivity(root, (Node *) or_rinfo,
0, JOIN_INNER, NULL, rel); 0, JOIN_INNER, NULL);
/* /*
* The clause is only worth adding to the query if it rejects a useful * The clause is only worth adding to the query if it rejects a useful
@ -344,7 +344,7 @@ consider_new_or_clause(PlannerInfo *root, RelOptInfo *rel,
/* Compute inner-join size */ /* Compute inner-join size */
orig_selec = clause_selectivity(root, (Node *) join_or_rinfo, orig_selec = clause_selectivity(root, (Node *) join_or_rinfo,
0, JOIN_INNER, &sjinfo, NULL); 0, JOIN_INNER, &sjinfo);
/* And hack cached selectivity so join size remains the same */ /* And hack cached selectivity so join size remains the same */
join_or_rinfo->norm_selec = orig_selec / or_selec; join_or_rinfo->norm_selec = orig_selec / or_selec;

View File

@ -1050,8 +1050,8 @@ dependencies_clauselist_selectivity(PlannerInfo *root,
{ {
clause = (Node *) lfirst(l); clause = (Node *) lfirst(l);
s2 = clause_selectivity(root, clause, varRelid, jointype, sjinfo, s2 = clause_selectivity(root, clause, varRelid, jointype,
NULL); /* don't try to use ext stats */ sjinfo);
/* mark this one as done, so we don't touch it again. */ /* mark this one as done, so we don't touch it again. */
*estimatedclauses = bms_add_member(*estimatedclauses, listidx); *estimatedclauses = bms_add_member(*estimatedclauses, listidx);

View File

@ -1634,17 +1634,13 @@ booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
case IS_NOT_FALSE: case IS_NOT_FALSE:
selec = (double) clause_selectivity(root, arg, selec = (double) clause_selectivity(root, arg,
varRelid, varRelid,
jointype, jointype, sjinfo);
sjinfo,
NULL);
break; break;
case IS_FALSE: case IS_FALSE:
case IS_NOT_TRUE: case IS_NOT_TRUE:
selec = 1.0 - (double) clause_selectivity(root, arg, selec = 1.0 - (double) clause_selectivity(root, arg,
varRelid, varRelid,
jointype, jointype, sjinfo);
sjinfo,
NULL);
break; break;
default: default:
elog(ERROR, "unrecognized booltesttype: %d", elog(ERROR, "unrecognized booltesttype: %d",
@ -6441,8 +6437,7 @@ genericcostestimate(PlannerInfo *root,
indexSelectivity = clauselist_selectivity(root, selectivityQuals, indexSelectivity = clauselist_selectivity(root, selectivityQuals,
index->rel->relid, index->rel->relid,
JOIN_INNER, JOIN_INNER,
NULL, NULL);
index->rel);
/* /*
* If caller didn't give us an estimate, estimate the number of index * If caller didn't give us an estimate, estimate the number of index
@ -6763,8 +6758,7 @@ btcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
btreeSelectivity = clauselist_selectivity(root, selectivityQuals, btreeSelectivity = clauselist_selectivity(root, selectivityQuals,
index->rel->relid, index->rel->relid,
JOIN_INNER, JOIN_INNER,
NULL, NULL);
index->rel);
numIndexTuples = btreeSelectivity * index->rel->tuples; numIndexTuples = btreeSelectivity * index->rel->tuples;
/* /*
@ -7523,8 +7517,7 @@ gincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
*indexSelectivity = clauselist_selectivity(root, selectivityQuals, *indexSelectivity = clauselist_selectivity(root, selectivityQuals,
index->rel->relid, index->rel->relid,
JOIN_INNER, JOIN_INNER,
NULL, NULL);
index->rel);
/* fetch estimated page cost for tablespace containing index */ /* fetch estimated page cost for tablespace containing index */
get_tablespace_page_costs(index->reltablespace, get_tablespace_page_costs(index->reltablespace,
@ -7854,8 +7847,7 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
qualSelectivity = clauselist_selectivity(root, indexQuals, qualSelectivity = clauselist_selectivity(root, indexQuals,
baserel->relid, baserel->relid,
JOIN_INNER, NULL, JOIN_INNER, NULL);
baserel);
/* work out the actual number of ranges in the index */ /* work out the actual number of ranges in the index */
indexRanges = Max(ceil((double) baserel->pages / statsData.pagesPerRange), indexRanges = Max(ceil((double) baserel->pages / statsData.pagesPerRange),

View File

@ -203,14 +203,12 @@ extern Selectivity clauselist_selectivity(PlannerInfo *root,
List *clauses, List *clauses,
int varRelid, int varRelid,
JoinType jointype, JoinType jointype,
SpecialJoinInfo *sjinfo, SpecialJoinInfo *sjinfo);
RelOptInfo *rel);
extern Selectivity clause_selectivity(PlannerInfo *root, extern Selectivity clause_selectivity(PlannerInfo *root,
Node *clause, Node *clause,
int varRelid, int varRelid,
JoinType jointype, JoinType jointype,
SpecialJoinInfo *sjinfo, SpecialJoinInfo *sjinfo);
RelOptInfo *rel);
extern void cost_gather_merge(GatherMergePath *path, PlannerInfo *root, extern void cost_gather_merge(GatherMergePath *path, PlannerInfo *root,
RelOptInfo *rel, ParamPathInfo *param_info, RelOptInfo *rel, ParamPathInfo *param_info,
Cost input_startup_cost, Cost input_total_cost, Cost input_startup_cost, Cost input_total_cost,