Discussion: https://postgr.es/m/CAKJS1f9yurJQW9pdnzL+rmOtsp2vOytkpXKGnMFJEO-qz5O5eA@mail.gmail.com
baserel->baserestrictinfo,
0,
JOIN_INNER,
- NULL,
NULL);
nrows = clamp_row_est(nrows);
fpinfo->local_conds,
baserel->relid,
JOIN_INNER,
- NULL,
NULL);
cost_qual_eval(&fpinfo->local_conds_cost, fpinfo->local_conds, root);
local_param_join_conds,
foreignrel->relid,
JOIN_INNER,
- NULL,
NULL);
local_sel *= fpinfo->local_conds_sel;
fpinfo->local_conds,
0,
JOIN_INNER,
- NULL,
NULL);
cost_qual_eval(&fpinfo->local_conds_cost, fpinfo->local_conds, root);
if (!fpinfo->use_remote_estimate)
fpinfo->joinclause_sel = clauselist_selectivity(root, fpinfo->joinclauses,
0, fpinfo->jointype,
- extra->sjinfo, NULL);
+ extra->sjinfo);
/* Estimate costs for bare join relation */
estimate_path_cost_size(root, joinrel, NIL, NIL, &rows,
static void addRangeClause(RangeQueryClause **rqlist, Node *clause,
bool varonleft, bool isLTsel, Selectivity s2);
-
+static RelOptInfo *find_relation_from_clauses(PlannerInfo *root,
+ List *clauses);
/****************************************************************************
* ROUTINES TO COMPUTE SELECTIVITIES
List *clauses,
int varRelid,
JoinType jointype,
- SpecialJoinInfo *sjinfo,
- RelOptInfo *rel)
+ SpecialJoinInfo *sjinfo)
{
Selectivity s1 = 1.0;
RangeQueryClause *rqlist = NULL;
ListCell *l;
Bitmapset *estimatedclauses = NULL;
int listidx;
+ RelOptInfo *rel;
/*
* If there's exactly one clause, then extended statistics is futile at
*/
if (list_length(clauses) == 1)
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
continue;
/* 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.
*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
*
Node *clause,
int varRelid,
JoinType jointype,
- SpecialJoinInfo *sjinfo,
- RelOptInfo *rel)
+ SpecialJoinInfo *sjinfo)
{
Selectivity s1 = 0.5; /* default for any unhandled clause type */
RestrictInfo *rinfo = NULL;
(Node *) get_notclausearg((Expr *) clause),
varRelid,
jointype,
- sjinfo,
- rel);
+ sjinfo);
}
else if (and_clause(clause))
{
((BoolExpr *) clause)->args,
varRelid,
jointype,
- sjinfo,
- rel);
+ sjinfo);
}
else if (or_clause(clause))
{
(Node *) lfirst(arg),
varRelid,
jointype,
- sjinfo,
- rel);
+ sjinfo);
s1 = s1 + s2 - s1 * s2;
}
(Node *) ((RelabelType *) clause)->arg,
varRelid,
jointype,
- sjinfo,
- rel);
+ sjinfo);
}
else if (IsA(clause, CoerceToDomain))
{
(Node *) ((CoerceToDomain *) clause)->arg,
varRelid,
jointype,
- sjinfo,
- rel);
+ sjinfo);
}
else
{
joinquals,
0,
jointype,
- sjinfo,
- NULL);
+ sjinfo);
/*
* Also get the normal inner-join selectivity of the join clauses.
joinquals,
0,
JOIN_INNER,
- &norm_sjinfo,
- NULL);
+ &norm_sjinfo);
/* Avoid leaking a lot of ListCells */
if (jointype == JOIN_ANTI)
Node *qual = (Node *) lfirst(l);
/* 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 */
rel->baserestrictinfo,
0,
JOIN_INNER,
- NULL,
- rel);
+ NULL);
rel->rows = clamp_row_est(nrows);
allclauses,
rel->relid, /* do not use 0! */
JOIN_INNER,
- NULL,
- rel);
+ NULL);
nrows = clamp_row_est(nrows);
/* For safety, make sure result is not more than the base estimate */
if (nrows > rel->rows)
joinquals,
0,
jointype,
- sjinfo,
- NULL);
+ sjinfo);
pselec = clauselist_selectivity(root,
pushedquals,
0,
jointype,
- sjinfo,
- NULL);
+ sjinfo);
/* Avoid leaking a lot of ListCells */
list_free(joinquals);
restrictlist,
0,
jointype,
- sjinfo,
- NULL);
+ sjinfo);
pselec = 0.0; /* not used, keep compiler quiet */
}
Selectivity csel;
csel = clause_selectivity(root, (Node *) rinfo,
- 0, jointype, sjinfo, NULL);
+ 0, jointype, sjinfo);
thisfksel = Min(thisfksel, csel);
}
fkselec *= thisfksel;
* saving work later.)
*/
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
/* Compute inner-join size */
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 */
join_or_rinfo->norm_selec = orig_selec / or_selec;
{
clause = (Node *) lfirst(l);
- s2 = clause_selectivity(root, clause, varRelid, jointype, sjinfo,
- NULL); /* don't try to use ext stats */
+ s2 = clause_selectivity(root, clause, varRelid, jointype,
+ sjinfo);
/* mark this one as done, so we don't touch it again. */
*estimatedclauses = bms_add_member(*estimatedclauses, listidx);
case IS_NOT_FALSE:
selec = (double) clause_selectivity(root, arg,
varRelid,
- jointype,
- sjinfo,
- NULL);
+ jointype, sjinfo);
break;
case IS_FALSE:
case IS_NOT_TRUE:
selec = 1.0 - (double) clause_selectivity(root, arg,
varRelid,
- jointype,
- sjinfo,
- NULL);
+ jointype, sjinfo);
break;
default:
elog(ERROR, "unrecognized booltesttype: %d",
indexSelectivity = clauselist_selectivity(root, selectivityQuals,
index->rel->relid,
JOIN_INNER,
- NULL,
- index->rel);
+ NULL);
/*
* If caller didn't give us an estimate, estimate the number of index
btreeSelectivity = clauselist_selectivity(root, selectivityQuals,
index->rel->relid,
JOIN_INNER,
- NULL,
- index->rel);
+ NULL);
numIndexTuples = btreeSelectivity * index->rel->tuples;
/*
*indexSelectivity = clauselist_selectivity(root, selectivityQuals,
index->rel->relid,
JOIN_INNER,
- NULL,
- index->rel);
+ NULL);
/* fetch estimated page cost for tablespace containing index */
get_tablespace_page_costs(index->reltablespace,
qualSelectivity = clauselist_selectivity(root, indexQuals,
baserel->relid,
- JOIN_INNER, NULL,
- baserel);
+ JOIN_INNER, NULL);
/* work out the actual number of ranges in the index */
indexRanges = Max(ceil((double) baserel->pages / statsData.pagesPerRange),
List *clauses,
int varRelid,
JoinType jointype,
- SpecialJoinInfo *sjinfo,
- RelOptInfo *rel);
+ SpecialJoinInfo *sjinfo);
extern Selectivity clause_selectivity(PlannerInfo *root,
Node *clause,
int varRelid,
JoinType jointype,
- SpecialJoinInfo *sjinfo,
- RelOptInfo *rel);
+ SpecialJoinInfo *sjinfo);
extern void cost_gather_merge(GatherMergePath *path, PlannerInfo *root,
RelOptInfo *rel, ParamPathInfo *param_info,
Cost input_startup_cost, Cost input_total_cost,