Skip to content

Commit ac2b095

Browse files
1 parent 255efa2 commit ac2b095

File tree

8 files changed

+74
-60
lines changed

8 files changed

+74
-60
lines changed

contrib/file_fdw/file_fdw.c

-1
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,6 @@ estimate_size(PlannerInfo *root, RelOptInfo *baserel,
10131013
baserel->baserestrictinfo,
10141014
0,
10151015
JOIN_INNER,
1016-
NULL,
10171016
NULL);
10181017

10191018
nrows = clamp_row_est(nrows);

contrib/postgres_fdw/postgres_fdw.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,6 @@ postgresGetForeignRelSize(PlannerInfo *root,
591591
fpinfo->local_conds,
592592
baserel->relid,
593593
JOIN_INNER,
594-
NULL,
595594
NULL);
596595

597596
cost_qual_eval(&fpinfo->local_conds_cost, fpinfo->local_conds, root);
@@ -2573,7 +2572,6 @@ estimate_path_cost_size(PlannerInfo *root,
25732572
local_param_join_conds,
25742573
foreignrel->relid,
25752574
JOIN_INNER,
2576-
NULL,
25772575
NULL);
25782576
local_sel *= fpinfo->local_conds_sel;
25792577

@@ -4457,7 +4455,6 @@ postgresGetForeignJoinPaths(PlannerInfo *root,
44574455
fpinfo->local_conds,
44584456
0,
44594457
JOIN_INNER,
4460-
NULL,
44614458
NULL);
44624459
cost_qual_eval(&fpinfo->local_conds_cost, fpinfo->local_conds, root);
44634460

@@ -4468,7 +4465,7 @@ postgresGetForeignJoinPaths(PlannerInfo *root,
44684465
if (!fpinfo->use_remote_estimate)
44694466
fpinfo->joinclause_sel = clauselist_selectivity(root, fpinfo->joinclauses,
44704467
0, fpinfo->jointype,
4471-
extra->sjinfo, NULL);
4468+
extra->sjinfo);
44724469

44734470
/* Estimate costs for bare join relation */
44744471
estimate_path_cost_size(root, joinrel, NIL, NIL, &rows,

src/backend/optimizer/path/clausesel.c

+52-17
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ typedef struct RangeQueryClause
4141

4242
static void addRangeClause(RangeQueryClause **rqlist, Node *clause,
4343
bool varonleft, bool isLTsel, Selectivity s2);
44-
44+
static RelOptInfo *find_relation_from_clauses(PlannerInfo *root,
45+
List *clauses);
4546

4647
/****************************************************************************
4748
* ROUTINES TO COMPUTE SELECTIVITIES
@@ -101,14 +102,14 @@ clauselist_selectivity(PlannerInfo *root,
101102
List *clauses,
102103
int varRelid,
103104
JoinType jointype,
104-
SpecialJoinInfo *sjinfo,
105-
RelOptInfo *rel)
105+
SpecialJoinInfo *sjinfo)
106106
{
107107
Selectivity s1 = 1.0;
108108
RangeQueryClause *rqlist = NULL;
109109
ListCell *l;
110110
Bitmapset *estimatedclauses = NULL;
111111
int listidx;
112+
RelOptInfo *rel;
112113

113114
/*
114115
* If there's exactly one clause, then extended statistics is futile at
@@ -117,7 +118,14 @@ clauselist_selectivity(PlannerInfo *root,
117118
*/
118119
if (list_length(clauses) == 1)
119120
return clause_selectivity(root, (Node *) linitial(clauses),
120-
varRelid, jointype, sjinfo, rel);
121+
varRelid, jointype, sjinfo);
122+
123+
/*
124+
* Determine if these clauses reference a single relation. If so we might
125+
* like to try applying any extended statistics which exist on it.
126+
* Called many time during joins, so must return NULL quickly if not.
127+
*/
128+
rel = find_relation_from_clauses(root, clauses);
121129

122130
/*
123131
* When a relation of RTE_RELATION is given as 'rel', we'll try to
@@ -164,7 +172,7 @@ clauselist_selectivity(PlannerInfo *root,
164172
continue;
165173

166174
/* Always compute the selectivity using clause_selectivity */
167-
s2 = clause_selectivity(root, clause, varRelid, jointype, sjinfo, rel);
175+
s2 = clause_selectivity(root, clause, varRelid, jointype, sjinfo);
168176

169177
/*
170178
* Check for being passed a RestrictInfo.
@@ -417,6 +425,39 @@ addRangeClause(RangeQueryClause **rqlist, Node *clause,
417425
*rqlist = rqelem;
418426
}
419427

428+
/*
429+
* find_relation_from_clauses
430+
* Process each clause in 'clauses' and determine if all clauses
431+
* reference only a single relation. If so return that relation,
432+
* otherwise return NULL.
433+
*/
434+
static RelOptInfo *
435+
find_relation_from_clauses(PlannerInfo *root, List *clauses)
436+
{
437+
ListCell *l;
438+
int relid;
439+
int lastrelid = 0;
440+
441+
foreach(l, clauses)
442+
{
443+
RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
444+
445+
if (bms_get_singleton_member(rinfo->clause_relids, &relid))
446+
{
447+
if (lastrelid != 0 && relid != lastrelid)
448+
return NULL; /* relation not the same as last one */
449+
lastrelid = relid;
450+
}
451+
else
452+
return NULL; /* multiple relations in clause */
453+
}
454+
455+
if (lastrelid != 0)
456+
return find_base_rel(root, lastrelid);
457+
458+
return NULL; /* no clauses */
459+
}
460+
420461
/*
421462
* bms_is_subset_singleton
422463
*
@@ -529,8 +570,7 @@ clause_selectivity(PlannerInfo *root,
529570
Node *clause,
530571
int varRelid,
531572
JoinType jointype,
532-
SpecialJoinInfo *sjinfo,
533-
RelOptInfo *rel)
573+
SpecialJoinInfo *sjinfo)
534574
{
535575
Selectivity s1 = 0.5; /* default for any unhandled clause type */
536576
RestrictInfo *rinfo = NULL;
@@ -650,8 +690,7 @@ clause_selectivity(PlannerInfo *root,
650690
(Node *) get_notclausearg((Expr *) clause),
651691
varRelid,
652692
jointype,
653-
sjinfo,
654-
rel);
693+
sjinfo);
655694
}
656695
else if (and_clause(clause))
657696
{
@@ -660,8 +699,7 @@ clause_selectivity(PlannerInfo *root,
660699
((BoolExpr *) clause)->args,
661700
varRelid,
662701
jointype,
663-
sjinfo,
664-
rel);
702+
sjinfo);
665703
}
666704
else if (or_clause(clause))
667705
{
@@ -680,8 +718,7 @@ clause_selectivity(PlannerInfo *root,
680718
(Node *) lfirst(arg),
681719
varRelid,
682720
jointype,
683-
sjinfo,
684-
rel);
721+
sjinfo);
685722

686723
s1 = s1 + s2 - s1 * s2;
687724
}
@@ -774,8 +811,7 @@ clause_selectivity(PlannerInfo *root,
774811
(Node *) ((RelabelType *) clause)->arg,
775812
varRelid,
776813
jointype,
777-
sjinfo,
778-
rel);
814+
sjinfo);
779815
}
780816
else if (IsA(clause, CoerceToDomain))
781817
{
@@ -784,8 +820,7 @@ clause_selectivity(PlannerInfo *root,
784820
(Node *) ((CoerceToDomain *) clause)->arg,
785821
varRelid,
786822
jointype,
787-
sjinfo,
788-
rel);
823+
sjinfo);
789824
}
790825
else
791826
{

src/backend/optimizer/path/costsize.c

+9-16
Original file line numberDiff line numberDiff line change
@@ -3750,8 +3750,7 @@ compute_semi_anti_join_factors(PlannerInfo *root,
37503750
joinquals,
37513751
0,
37523752
jointype,
3753-
sjinfo,
3754-
NULL);
3753+
sjinfo);
37553754

37563755
/*
37573756
* Also get the normal inner-join selectivity of the join clauses.
@@ -3774,8 +3773,7 @@ compute_semi_anti_join_factors(PlannerInfo *root,
37743773
joinquals,
37753774
0,
37763775
JOIN_INNER,
3777-
&norm_sjinfo,
3778-
NULL);
3776+
&norm_sjinfo);
37793777

37803778
/* Avoid leaking a lot of ListCells */
37813779
if (jointype == JOIN_ANTI)
@@ -3942,7 +3940,7 @@ approx_tuple_count(PlannerInfo *root, JoinPath *path, List *quals)
39423940
Node *qual = (Node *) lfirst(l);
39433941

39443942
/* Note that clause_selectivity will be able to cache its result */
3945-
selec *= clause_selectivity(root, qual, 0, JOIN_INNER, &sjinfo, NULL);
3943+
selec *= clause_selectivity(root, qual, 0, JOIN_INNER, &sjinfo);
39463944
}
39473945

39483946
/* Apply it to the input relation sizes */
@@ -3978,8 +3976,7 @@ set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
39783976
rel->baserestrictinfo,
39793977
0,
39803978
JOIN_INNER,
3981-
NULL,
3982-
rel);
3979+
NULL);
39833980

39843981
rel->rows = clamp_row_est(nrows);
39853982

@@ -4016,8 +4013,7 @@ get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel,
40164013
allclauses,
40174014
rel->relid, /* do not use 0! */
40184015
JOIN_INNER,
4019-
NULL,
4020-
rel);
4016+
NULL);
40214017
nrows = clamp_row_est(nrows);
40224018
/* For safety, make sure result is not more than the base estimate */
40234019
if (nrows > rel->rows)
@@ -4183,14 +4179,12 @@ calc_joinrel_size_estimate(PlannerInfo *root,
41834179
joinquals,
41844180
0,
41854181
jointype,
4186-
sjinfo,
4187-
NULL);
4182+
sjinfo);
41884183
pselec = clauselist_selectivity(root,
41894184
pushedquals,
41904185
0,
41914186
jointype,
4192-
sjinfo,
4193-
NULL);
4187+
sjinfo);
41944188

41954189
/* Avoid leaking a lot of ListCells */
41964190
list_free(joinquals);
@@ -4202,8 +4196,7 @@ calc_joinrel_size_estimate(PlannerInfo *root,
42024196
restrictlist,
42034197
0,
42044198
jointype,
4205-
sjinfo,
4206-
NULL);
4199+
sjinfo);
42074200
pselec = 0.0; /* not used, keep compiler quiet */
42084201
}
42094202

@@ -4498,7 +4491,7 @@ get_foreign_key_join_selectivity(PlannerInfo *root,
44984491
Selectivity csel;
44994492

45004493
csel = clause_selectivity(root, (Node *) rinfo,
4501-
0, jointype, sjinfo, NULL);
4494+
0, jointype, sjinfo);
45024495
thisfksel = Min(thisfksel, csel);
45034496
}
45044497
fkselec *= thisfksel;

src/backend/optimizer/util/orclauses.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ consider_new_or_clause(PlannerInfo *root, RelOptInfo *rel,
280280
* saving work later.)
281281
*/
282282
or_selec = clause_selectivity(root, (Node *) or_rinfo,
283-
0, JOIN_INNER, NULL, rel);
283+
0, JOIN_INNER, NULL);
284284

285285
/*
286286
* 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,
344344

345345
/* Compute inner-join size */
346346
orig_selec = clause_selectivity(root, (Node *) join_or_rinfo,
347-
0, JOIN_INNER, &sjinfo, NULL);
347+
0, JOIN_INNER, &sjinfo);
348348

349349
/* And hack cached selectivity so join size remains the same */
350350
join_or_rinfo->norm_selec = orig_selec / or_selec;

src/backend/statistics/dependencies.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,8 @@ dependencies_clauselist_selectivity(PlannerInfo *root,
10501050
{
10511051
clause = (Node *) lfirst(l);
10521052

1053-
s2 = clause_selectivity(root, clause, varRelid, jointype, sjinfo,
1054-
NULL); /* don't try to use ext stats */
1053+
s2 = clause_selectivity(root, clause, varRelid, jointype,
1054+
sjinfo);
10551055

10561056
/* mark this one as done, so we don't touch it again. */
10571057
*estimatedclauses = bms_add_member(*estimatedclauses, listidx);

src/backend/utils/adt/selfuncs.c

+6-14
Original file line numberDiff line numberDiff line change
@@ -1634,17 +1634,13 @@ booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg,
16341634
case IS_NOT_FALSE:
16351635
selec = (double) clause_selectivity(root, arg,
16361636
varRelid,
1637-
jointype,
1638-
sjinfo,
1639-
NULL);
1637+
jointype, sjinfo);
16401638
break;
16411639
case IS_FALSE:
16421640
case IS_NOT_TRUE:
16431641
selec = 1.0 - (double) clause_selectivity(root, arg,
16441642
varRelid,
1645-
jointype,
1646-
sjinfo,
1647-
NULL);
1643+
jointype, sjinfo);
16481644
break;
16491645
default:
16501646
elog(ERROR, "unrecognized booltesttype: %d",
@@ -6441,8 +6437,7 @@ genericcostestimate(PlannerInfo *root,
64416437
indexSelectivity = clauselist_selectivity(root, selectivityQuals,
64426438
index->rel->relid,
64436439
JOIN_INNER,
6444-
NULL,
6445-
index->rel);
6440+
NULL);
64466441

64476442
/*
64486443
* 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,
67636758
btreeSelectivity = clauselist_selectivity(root, selectivityQuals,
67646759
index->rel->relid,
67656760
JOIN_INNER,
6766-
NULL,
6767-
index->rel);
6761+
NULL);
67686762
numIndexTuples = btreeSelectivity * index->rel->tuples;
67696763

67706764
/*
@@ -7523,8 +7517,7 @@ gincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
75237517
*indexSelectivity = clauselist_selectivity(root, selectivityQuals,
75247518
index->rel->relid,
75257519
JOIN_INNER,
7526-
NULL,
7527-
index->rel);
7520+
NULL);
75287521

75297522
/* fetch estimated page cost for tablespace containing index */
75307523
get_tablespace_page_costs(index->reltablespace,
@@ -7854,8 +7847,7 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
78547847

78557848
qualSelectivity = clauselist_selectivity(root, indexQuals,
78567849
baserel->relid,
7857-
JOIN_INNER, NULL,
7858-
baserel);
7850+
JOIN_INNER, NULL);
78597851

78607852
/* work out the actual number of ranges in the index */
78617853
indexRanges = Max(ceil((double) baserel->pages / statsData.pagesPerRange),

src/include/optimizer/cost.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,12 @@ extern Selectivity clauselist_selectivity(PlannerInfo *root,
203203
List *clauses,
204204
int varRelid,
205205
JoinType jointype,
206-
SpecialJoinInfo *sjinfo,
207-
RelOptInfo *rel);
206+
SpecialJoinInfo *sjinfo);
208207
extern Selectivity clause_selectivity(PlannerInfo *root,
209208
Node *clause,
210209
int varRelid,
211210
JoinType jointype,
212-
SpecialJoinInfo *sjinfo,
213-
RelOptInfo *rel);
211+
SpecialJoinInfo *sjinfo);
214212
extern void cost_gather_merge(GatherMergePath *path, PlannerInfo *root,
215213
RelOptInfo *rel, ParamPathInfo *param_info,
216214
Cost input_startup_cost, Cost input_total_cost,

0 commit comments

Comments
 (0)