From 53250ce741acf3545474015702609e04b60e5bef Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Sat, 28 Nov 2026 12:29:55 -0500 Subject: [PATCH 1/2] Count NULL pairs in IS [NOT] DISTINCT FROM selectivity Rows with both inputs NULL were not counted. Add them using the null fractions, with a separate rule for semijoins. Co-authored-by: Rahul Yadav Discussion: https://postgr.es/m/17545-a0ca4de888953169@postgresql.org --- src/backend/optimizer/path/clausesel.c | 33 ++++++++++-- src/backend/utils/adt/selfuncs.c | 70 ++++++++++++++++++++++++++ src/include/utils/selfuncs.h | 2 + 3 files changed, 102 insertions(+), 3 deletions(-) diff --git a/src/backend/optimizer/path/clausesel.c b/src/backend/optimizer/path/clausesel.c index c22cbfc9ca0..599ed64e46e 100644 --- a/src/backend/optimizer/path/clausesel.c +++ b/src/backend/optimizer/path/clausesel.c @@ -803,13 +803,29 @@ clause_selectivity_ext(PlannerInfo *root, } else if (is_notclause(clause)) { + Node *arg = (Node *) get_notclausearg((Expr *) clause); + /* inverse of the selectivity of the underlying clause */ s1 = 1.0 - clause_selectivity_ext(root, - (Node *) get_notclausearg((Expr *) clause), + arg, varRelid, jointype, sjinfo, use_extended_stats); + + /* + * "x IS NOT DISTINCT FROM y" is NOT of a DistinctExpr. As a semijoin + * or antijoin clause, its estimate so far is that of "x = y", the + * fraction of outer rows having a non-null match. Add the outer rows + * whose input is null and that can match an inner null. + */ + if (IsA(arg, DistinctExpr) && + treat_as_join_clause(root, arg, rinfo, varRelid, sjinfo) && + (sjinfo->jointype == JOIN_SEMI || sjinfo->jointype == JOIN_ANTI)) + { + s1 += distinctnullsel(root, ((DistinctExpr *) arg)->args, sjinfo); + CLAMP_PROBABILITY(s1); + } } else if (is_andclause(clause)) { @@ -860,11 +876,22 @@ clause_selectivity_ext(PlannerInfo *root, /* * DistinctExpr has the same representation as OpExpr, but the * contained operator is "=" not "<>", so we must negate the result. - * This estimation method doesn't give the right behavior for nulls, - * but it's better than doing nothing. + * First add the fraction of rows having both inputs null, which "=" + * does not count. We don't do that for semijoins and antijoins, + * where the "=" estimate is not a fraction of row pairs; see the NOT + * case above for those. */ if (IsA(clause, DistinctExpr)) + { + if (sjinfo == NULL || + (sjinfo->jointype != JOIN_SEMI && + sjinfo->jointype != JOIN_ANTI)) + { + s1 += distinctnullsel(root, opclause->args, sjinfo); + CLAMP_PROBABILITY(s1); + } s1 = 1.0 - s1; + } } else if (is_funcclause(clause)) { diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index 5ee377595bd..917e2a86a1f 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -1856,6 +1856,76 @@ nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg, return (Selectivity) selec; } +/* + * distinctnullsel - Selectivity of null matches in IS NOT DISTINCT FROM + * + * Callers estimate a DistinctExpr using its "=" operator, which counts no + * rows having a null input. This returns the fraction to add for rows where + * both inputs are null. For a semijoin or antijoin clause, that is the + * fraction of outer rows having a null input and an inner null to match. + * Without statistics, we assume there are no nulls. + * + * The "=" estimator has usually just fetched the same statistics, but IS + * [NOT] DISTINCT FROM is not common enough to justify changing the join + * estimator API to share them. + */ +Selectivity +distinctnullsel(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo) +{ + VariableStatData vardata1; + VariableStatData vardata2; + double nullfrac1 = 0.0; + double nullfrac2 = 0.0; + bool join_is_reversed = false; + bool is_semi; + Selectivity selec; + + is_semi = sjinfo != NULL && + (sjinfo->jointype == JOIN_SEMI || sjinfo->jointype == JOIN_ANTI); + + if (is_semi) + get_join_variables(root, args, sjinfo, + &vardata1, &vardata2, &join_is_reversed); + else + { + examine_variable(root, linitial(args), 0, &vardata1); + examine_variable(root, lsecond(args), 0, &vardata2); + } + + if (HeapTupleIsValid(vardata1.statsTuple)) + nullfrac1 = + ((Form_pg_statistic) GETSTRUCT(vardata1.statsTuple))->stanullfrac; + if (HeapTupleIsValid(vardata2.statsTuple)) + nullfrac2 = + ((Form_pg_statistic) GETSTRUCT(vardata2.statsTuple))->stanullfrac; + + if (is_semi) + { + double outer_nullfrac = join_is_reversed ? nullfrac2 : nullfrac1; + double inner_nullfrac = join_is_reversed ? nullfrac1 : nullfrac2; + RelOptInfo *inner_rel = join_is_reversed ? vardata1.rel : vardata2.rel; + double inner_nulls = 0.0; + + /* + * The outer rows with a null input all have a match if there is at + * least one inner row with a null input. Assume that if we expect at + * least one, and scale it down if we expect fewer. + */ + if (inner_rel) + inner_nulls = inner_nullfrac * inner_rel->rows; + selec = outer_nullfrac * Min(inner_nulls, 1.0); + } + else + selec = nullfrac1 * nullfrac2; + + ReleaseVariableStats(vardata1); + ReleaseVariableStats(vardata2); + + CLAMP_PROBABILITY(selec); + + return selec; +} + /* * strip_array_coercion - strip binary-compatible relabeling from an array expr * diff --git a/src/include/utils/selfuncs.h b/src/include/utils/selfuncs.h index 8d9fff95a19..f37d7819946 100644 --- a/src/include/utils/selfuncs.h +++ b/src/include/utils/selfuncs.h @@ -209,6 +209,8 @@ extern Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype, extern Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo); +extern Selectivity distinctnullsel(PlannerInfo *root, List *args, + SpecialJoinInfo *sjinfo); extern Selectivity scalararraysel(PlannerInfo *root, ScalarArrayOpExpr *clause, bool is_join_clause, -- 2.37.1 (Apple Git-137.1)