Re: BUG #19926: Incorrect join selectivity for <> with NULLs (related to BUG #17545)

From: ZizhuanLiu X-MAN <44973863(at)qq(dot)com>
To: pgsql-bugs <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #19926: Incorrect join selectivity for <> with NULLs (related to BUG #17545)
Date: 2026-12-04 16:04:49
Message-ID: tencent_06A4A50BB56D63167C729B142DD484D8B706@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

I would like to summarize the scope of the proposed fix for BUG #19926
and outline my plan for splitting it into a separate patch.

1. Scope
The proposed patch would address two sets of estimation issues:
- Ordinary <> joins, for both non-SEMI/ANTI and SEMI/ANTI cases.
- IS DISTINCT FROM joins, for the SEMI/ANTI cases.

Both involve NULL handling, but their comparison semantics differ:
ordinary <> returns UNKNOWN if either operand is NULL, whereas
IS DISTINCT FROM returns TRUE if exactly one operand is NULL
and FALSE if both are NULL.

2. Plan
Following the discussion in CF7396 [1], I plan to extract the logic addressing
these issues from v3 [2] into a separate patch. This would allow it to be reviewed
and developed independently, without blocking the work on v4 / BUG #17545.

3. Ordinary <> joins: non-SEMI/ANTI cases
For these cases, I plan to follow the layering approach in v4: obtain the existing
equality selectivity estimate E in clause_selectivity_ext(), then combine it with the
NULL fractions on both sides:

B = (1 - fL) * (1 - fR)
selectivity(<>) = B - E

Here, fL and fR are the NULL fractions on the two sides, B estimates the fraction of
row pairs in which both values are non-NULL, and E is the join selectivity of the
corresponding ordinary equality condition being TRUE.

This would not require changing the existing calling interface for operator join-selectivity
estimators.

4. SEMI/ANTI cases
For SEMI/ANTI joins, we need to estimate whether an outer row has at least one inner
row satisfying the comparison condition. For ordinary <>, this matching probability
cannot be derived solely from the final equality SEMI/ANTI selectivity estimate.
IS DISTINCT FROM also requires handling NULLs according to its own semantics.

I plan to add a helper in selfuncs.c, tentatively named neqjoinsel_semi(), based on
distinctjoinsel_semi() in v3. It would reuse the statistics and matching logic while
explicitly distinguishing the NULL handling for ordinary <> and IS DISTINCT FROM.
The helper would be called from clause_selectivity_ext(), again without changing the
existing calling interface for operator join-selectivity estimators.

This approach is intended to follow the layering discussed in v4 and the BUG #17545 thread [3].
I would welcome feedback on the proposed scope, as well as the responsibilities of the SEMI/ANTI
helper and where it should be called.

References:

[1] CF7396:
https://commitfest.postgresql.org/patch/7396/

[2] v3 discussion:
https://www.postgresql.org/message-id/flat/tencent_F85023353EC117B719D1BE4E147D7B593A0A%40qq.com

[3] BUG #17545 thread:
https://www.postgresql.org/message-id/flat/17545-a0ca4de888953169(at)postgresql(dot)org

v4 / architecture discussion:
https://www.postgresql.org/message-id/flat/at9R47rsFaDzBVFp(at)floki

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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Jan Nidzwetzki 2026-12-04 16:37:32 Re: dbase_redo: XLOG_DBASE_CREATE_FILE_COPY replay empties live database
Previous Message ZizhuanLiu X-MAN 2026-12-04 13:49:07 Re: BUG #17545: Incorrect selectivity for IS NOT DISTINCT FROM and NULLs