Skip to content

Commit 7588aeb

Browse files
Ilia Evdokimovhackorum
authored andcommitted
Improve selectivity estimation for OR clauseswith equality conditions on the same column
1 parent 7340144 commit 7588aeb

2 files changed

Lines changed: 96 additions & 16 deletions

File tree

‎src/backend/optimizer/path/clausesel.c‎

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "optimizer/pathnode.h"
2121
#include "optimizer/plancat.h"
2222
#include "statistics/statistics.h"
23+
#include "statistics/extended_stats_internal.h"
2324
#include "utils/fmgroids.h"
2425
#include "utils/lsyscache.h"
2526
#include "utils/selfuncs.h"
@@ -341,6 +342,51 @@ clauselist_selectivity_ext(PlannerInfo *root,
341342
return s1;
342343
}
343344

345+
static bool
346+
is_simple_equality_clause(Node *clause, Var **var)
347+
{
348+
Node *clause_expr;
349+
350+
if (IsA(clause, RestrictInfo))
351+
{
352+
RestrictInfo *rinfo = (RestrictInfo *) clause;
353+
354+
/* Pseudoconstants are not interesting (they couldn't contain a Var) */
355+
if (rinfo->pseudoconstant)
356+
return false;
357+
358+
/* Clauses referencing multiple, or no, varnos are incompatible */
359+
if (bms_membership(rinfo->clause_relids) != BMS_SINGLETON)
360+
return false;
361+
362+
clause = (Node *) rinfo->clause;
363+
}
364+
365+
if (is_opclause(clause))
366+
{
367+
OpExpr *expr = (OpExpr *) clause;
368+
369+
/* Only expressions with two arguments are considered compatible. */
370+
if (list_length(expr->args) != 2)
371+
return false;
372+
373+
/* Check if the expression has the right shape */
374+
if (!examine_opclause_args(expr->args, &clause_expr, NULL, NULL))
375+
return false;
376+
377+
/* If it's not an "=" operator, just ignore the clause */
378+
if (get_oprrest(expr->opno) != F_EQSEL)
379+
return false;
380+
381+
if (IsA(clause_expr, Var))
382+
*var = (Var *) clause_expr;
383+
}
384+
else
385+
return false;
386+
387+
return true;
388+
}
389+
344390
/*
345391
* clauselist_selectivity_or -
346392
* Compute the selectivity of an implicitly-ORed list of boolean
@@ -353,7 +399,6 @@ clauselist_selectivity_ext(PlannerInfo *root,
353399
*
354400
* The basic approach is to apply extended statistics first, on as many
355401
* clauses as possible, in order to capture cross-column dependencies etc.
356-
* The remaining clauses are then estimated as if they were independent.
357402
*/
358403
static Selectivity
359404
clauselist_selectivity_or(PlannerInfo *root,
@@ -368,6 +413,8 @@ clauselist_selectivity_or(PlannerInfo *root,
368413
Bitmapset *estimatedclauses = NULL;
369414
ListCell *lc;
370415
int listidx;
416+
bool all_eq_clauses = true;
417+
Var *first_var = NULL;
371418

372419
/*
373420
* Determine if these clauses reference a single relation. If so, and if
@@ -387,14 +434,38 @@ clauselist_selectivity_or(PlannerInfo *root,
387434
&estimatedclauses, true);
388435
}
389436

390-
/*
391-
* Estimate the remaining clauses as if they were independent.
392-
*
393-
* Selectivities for an OR clause are computed as s1+s2 - s1*s2 to account
394-
* for the probable overlap of selected tuple sets.
395-
*
396-
* XXX is this too conservative?
397-
*/
437+
/* Check if all clauses use the same column and only equality conditions */
438+
listidx = -1;
439+
foreach (lc, clauses)
440+
{
441+
Node *clause = (Node *) lfirst(lc);
442+
Var *var = NULL;
443+
444+
listidx++;
445+
446+
/*
447+
* Skip this clause if it's already been estimated by some other
448+
* statistics above.
449+
*/
450+
if (bms_is_member(listidx, estimatedclauses))
451+
continue;
452+
453+
if (!is_simple_equality_clause(clause, &var))
454+
{
455+
all_eq_clauses = false;
456+
break;
457+
}
458+
459+
if (first_var == NULL)
460+
first_var = var;
461+
else if (!equal(first_var, var))
462+
{
463+
all_eq_clauses = false;
464+
break;
465+
}
466+
}
467+
468+
/* Estimate the remaining clauses. */
398469
listidx = -1;
399470
foreach(lc, clauses)
400471
{
@@ -412,7 +483,16 @@ clauselist_selectivity_or(PlannerInfo *root,
412483
s2 = clause_selectivity_ext(root, (Node *) lfirst(lc), varRelid,
413484
jointype, sjinfo, use_extended_stats);
414485

415-
s1 = s1 + s2 - s1 * s2;
486+
if (all_eq_clauses)
487+
{
488+
/* If clauses are like 'a = 1 OR a = 2 OR ... ' */
489+
s1 = s1 + s2;
490+
}
491+
else
492+
{
493+
/* If clauses are independent. */
494+
s1 = s1 + s2 - s1 * s2;
495+
}
416496
}
417497

418498
return s1;

‎src/test/regress/expected/stats_ext.out‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,19 +1417,19 @@ SELECT * FROM check_estimated_rows('SELECT * FROM functional_dependencies WHERE
14171417
SELECT * FROM check_estimated_rows('SELECT * FROM functional_dependencies WHERE (a = 1 OR a = 51) AND b = ''1''');
14181418
estimated | actual
14191419
-----------+--------
1420-
99 | 100
1420+
100 | 100
14211421
(1 row)
14221422

14231423
SELECT * FROM check_estimated_rows('SELECT * FROM functional_dependencies WHERE (a = 1 OR a = 51) AND (b = ''1'' OR b = ''2'')');
14241424
estimated | actual
14251425
-----------+--------
1426-
99 | 100
1426+
100 | 100
14271427
(1 row)
14281428

14291429
SELECT * FROM check_estimated_rows('SELECT * FROM functional_dependencies WHERE (a = 1 OR a = 2 OR a = 51 OR a = 52) AND (b = ''1'' OR b = ''2'')');
14301430
estimated | actual
14311431
-----------+--------
1432-
197 | 200
1432+
200 | 200
14331433
(1 row)
14341434

14351435
-- OR clauses referencing different attributes are incompatible
@@ -1763,19 +1763,19 @@ SELECT * FROM check_estimated_rows('SELECT * FROM functional_dependencies WHERE
17631763
SELECT * FROM check_estimated_rows('SELECT * FROM functional_dependencies WHERE ((a * 2) = 2 OR (a * 2) = 102) AND upper(b) = ''1''');
17641764
estimated | actual
17651765
-----------+--------
1766-
99 | 100
1766+
100 | 100
17671767
(1 row)
17681768

17691769
SELECT * FROM check_estimated_rows('SELECT * FROM functional_dependencies WHERE ((a * 2) = 2 OR (a * 2) = 102) AND (upper(b) = ''1'' OR upper(b) = ''2'')');
17701770
estimated | actual
17711771
-----------+--------
1772-
99 | 100
1772+
100 | 100
17731773
(1 row)
17741774

17751775
SELECT * FROM check_estimated_rows('SELECT * FROM functional_dependencies WHERE ((a * 2) = 2 OR (a * 2) = 4 OR (a * 2) = 102 OR (a * 2) = 104) AND (upper(b) = ''1'' OR upper(b) = ''2'')');
17761776
estimated | actual
17771777
-----------+--------
1778-
197 | 200
1778+
200 | 200
17791779
(1 row)
17801780

17811781
-- OR clauses referencing different attributes

0 commit comments

Comments
 (0)