Re: BUG #17545: Incorrect selectivity for IS NOT DISTINCT FROM and NULLs

From: ZizhuanLiu X-MAN <44973863(at)qq(dot)com>
To: shihao zhong <zhong950419(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Greg Burd <greg(at)burd(dot)me>, rahul <rahul(at)rhyadav(dot)dev>, pgsql-bugs <pgsql-bugs(at)postgresql(dot)org>, rpsalmi <rpsalmi(at)gmail(dot)com>, pgsql-bugs <pgsql-bugs(at)lists(dot)postgresql(dot)org>, guofenglinux <guofenglinux(at)gmail(dot)com>
Subject: Re: BUG #17545: Incorrect selectivity for IS NOT DISTINCT FROM and NULLs
Date: 2026-12-02 12:09:20
Message-ID: tencent_BDB96D86966B82E2BBDFB29ADAB3B1027E05@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

(It seems that the email was rejected. Let me try sending it again now. Sorry for the inconvenience.)

Hi,

Thanks for the v4 patch and for the feedback on preserving the estimator
interfaces. I had understood that v4 would cover the V3 fixes while
preserving the existing interfaces. After checking the patch, I see that
ordinary <> estimates for non-SEMI/ANTI joins, and SEMI/ANTI estimates
for ordinary <> and IS DISTINCT FROM, still need further work.

Following the earlier discussion, I revisited the fmgr call mechanism and
found that the existing fcinfo->context field could convey NULL-safe
comparison semantics without adding a sixth argument. This would allow
ordinary = and IS NOT DISTINCT FROM to share the equality estimator, and
ordinary <> and IS DISTINCT FROM to share the inequality estimator,
while distinguishing their NULL semantics for both pair selectivity and
SEMI/ANTI partner-existence estimates.

I would like to discuss using this mechanism for the V3 estimation
approach, while retaining the four-argument restriction estimator
signatures and the five-argument join estimator signatures.

My main concern is the division of responsibility for estimation. In
v2/v4, the equality estimator computes ordinary equality selectivity,
and clausesel.c then adds NULL matches and complements the result. This
splits the calculation across two layers. Extending this to IS DISTINCT
FROM SEMI/ANTI joins also requires determining whether particular values
have unequal partners; correcting or complementing equality selectivity
alone is insufficient. For example, with an outer value of 1 and inner
values {1,2}, both equality and inequality EXISTS predicates are true.
For these cases sharing statistics and matching algorithms, handling
NULL rules and partner-existence estimation within the estimators would
reduce coordination between the two layers and make further development
and maintenance easier.

I therefore suggest that clausesel.c identify the expression and dispatch
the call, while eqsel()/neqsel()/eqjoinsel()/neqjoinsel() and their helpers
handle statistics, matching and NULL rules together, returning the full
selectivity for the requested comparison semantics. General Boolean
expression composition would remain in clausesel.c.

Ordinary = and <> would retain their existing call paths. For the
recognized built-in estimators, dedicated NULL-safe entry points would
combine function lookup and call initialization, placing the current
DistinctExpr in context. For a join call, initialization would look like:

fmgr_info(oprjoin, &flinfo);
InitFunctionCallInfoData(*fcinfo, &flinfo, 5, inputcollid,
(Node *) distinctexpr, NULL);

These entry points would retain the existing argument setup and checks
for NULL return values and out-of-range selectivity. The estimators would
recognize NULL-safe semantics with:

bool null_safe_compare =
fcinfo->context != NULL &&
IsA(fcinfo->context, DistinctExpr);

The dispatch would be:

Ordinary = equality estimator, context = NULL
Ordinary <> inequality estimator, context = NULL
IS NOT DISTINCT FROM equality estimator, context = DistinctExpr
IS DISTINCT FROM inequality estimator, context = DistinctExpr

NOT(DistinctExpr) would go directly to the NULL-safe equality path,
avoiding an incorrect complement of the unequal-partner existence
estimate for SEMI/ANTI joins. When calling the inequality estimator, the
operator argument would be the corresponding negator; the original
DistinctExpr would remain unchanged.

The "Function Call Contexts" section of fmgr/README documents this
mechanism, which triggers, aggregates and CALL already use. This proposal
would leave the catalog signatures and FunctionCallInfo structure
unchanged, using the existing context mechanism to convey comparison
semantics to the estimators.

The calculations could reuse the V3 approach. Let E be ordinary equality
pair selectivity, n1/n2 the NULL fractions, and B = (1-n1)*(1-n2). For
non-SEMI/ANTI join clauses:

= E
<> B - E
IS NOT DISTINCT FROM E + n1*n2
IS DISTINCT FROM 1 - E - n1*n2

For SEMI/ANTI joins, estimate S, the fraction of outer rows with at least
one qualifying inner partner. Reuse the equality MCV match indices and
hash path, then account for NULLs and residual values under the ordinary
or NULL-safe rules. SEMI output uses S and ANTI output uses 1-S, without
modifying the statistics slots or original MCV frequencies.

I would prefer to address BUG #19926, ordinary <> SEMI/ANTI estimation,
and IS [NOT] DISTINCT FROM together in this design, sharing the statistics
and matching algorithms while explicitly distinguishing the NULL rules
through context. Restriction estimation would also be included in this
design.

This is currently a design proposal, pending implementation and testing.
Filtered distributions and insufficient statistics would still involve
approximation. I would appreciate feedback on using fcinfo->context to
convey comparison semantics and on this division of responsibility.

Related discussion:
https://www.postgresql.org/message-id/flat/17545-a0ca4de888953169%40postgresql.org

BUG #19926:
https://www.postgresql.org/message-id/flat/19926-21f3ff733b3fada7%40postgresql.org

Related CommitFest entry:
https://commitfest.postgresql.org/patch/7396/

regards,
--
ZizhuanLiu (X-MAN) 
44973863(at)qq(dot)com

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Konstantin Knizhnik 2026-12-02 12:23:00 Re: BUG #19519: REPACK can fail due to missing chunk for toast value
Previous Message Rui Zhao 2026-12-02 11:33:18 Re: BUG #19519: REPACK can fail due to missing chunk for toast value