| From: | ZizhuanLiu X-MAN <44973863(at)qq(dot)com> |
|---|---|
| To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
| Cc: | 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-05 15:33:05 |
| Message-ID: | tencent_65DF1A96B6D42C8D05F07C6D25B448132408@qq.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
Hi Tom,
Thanks for your feedback.
To avoid the question of whether a clausesel-side helper would bypass the
estimator selected by oprjoin, v5 passes the comparison semantics through
fcinfo->context instead:
- clausesel.c identifies the comparison expression and selects the
estimation entry point;
- nullsafe_join_selectivity() builds the fcinfo manually, places the
DistinctExpr in context, and calls the built-in eqjoinsel()/neqjoinsel();
- FunctionCall*Coll()/DirectFunctionCall*Coll() still initialize context to
NULL, so other callers are unaffected, and estimators that do not
recognize context keep their existing behavior;
- other estimators continue to use the ordinary join_selectivity() path and
the five-argument calling convention.
The patch covers the ordinary <> and IS DISTINCT FROM estimates for
non-SEMI/ANTI and SEMI/ANTI joins. The IS NOT DISTINCT FROM part gives the
same estimates as v4 [1] and is kept only for reference and testing; it will
be removed when rebasing if v4 is committed first.
One point I would like your opinion on: the context currently carries a
DistinctExpr node. As you noted, the API should not be defined so that it
can only be used for DistinctExpr. Would you prefer a dedicated context
structure (operator OID plus a comparison-semantics enum) from the start, or
is it acceptable to begin with DistinctExpr and generalize later? Related
to that, should extension estimators also be allowed to receive context --
reading it if they understand it and ignoring it otherwise -- rather than
only the built-in eqjoinsel/neqjoinsel?
On testing, the new planner_est cases cover NULLs, values inside and outside
the MCV lists, absent MCV lists, single-value and multi-value inner inputs,
cross-type matching, and custom estimator procedures; the full regression
suite passes. The v5 patch is attached; reviews are welcome.
files: clausesel.c | plancat.c | selfuncs.c | fmgr.c
[v4] NULL correction in the node-type branches of clausesel
clausesel.c: clause_selectivity_ext()
|
+-- IS NOT DISTINCT FROM [NOT(DistinctExpr)]
| +-- 1 - clause_selectivity_ext(DistinctExpr) [clausesel.c]
| | \-- join/restriction_selectivity("=") [plancat.c]
| | \-- eqjoinsel() [selfuncs.c]
| | extension estimator still used via fmgr
| \-- distinctnullsel() [selfuncs.c]
| upper layer adds NULL/NULL match
| SEMI/ANTI: outer NULL rows with an inner NULL partner
|
+-- IS DISTINCT FROM [DistinctExpr]
| +-- join/restriction_selectivity("=") [plancat.c]
| | \-- eqjoinsel() [selfuncs.c]
| \-- negate + distinctnullsel() [clausesel.c/selfuncs.c]
|
+-- = -> join_selectivity("=") -> eqjoinsel() [plancat.c -> selfuncs.c]
\-- <> -> join_selectivity("<>") -> neqjoinsel() [plancat.c -> selfuncs.c]
files: clausesel.c | plancat.c | selfuncs.c | fmgr.c
[v5] NULL correction in the join-type branches of the estimator
clausesel.c: clause_selectivity_ext()
|
+-- IS NOT DISTINCT FROM [NOT(DistinctExpr)]
| \-- nullsafe_join_selectivity(opno="=", context=DistinctExpr) [plancat.c]
| +-- oprjoin("=") is built-in?
| | +-- yes -> fmgr_info(F_EQJOINSEL) + manual fcinfo [fmgr.c]
| | | \-- eqjoinsel(): null_safe=true [selfuncs.c]
| | | +-- SEMI/ANTI -> eqjoinsel_semi() -> S [selfuncs.c]
| | | \-- otherwise -> E + n1*n2
| | \-- no -> join_selectivity("=") [plancat.c]
| | extension estimator, context=NULL
|
+-- IS DISTINCT FROM [DistinctExpr]
| \-- nullsafe_join_selectivity(opno="<>", context=DistinctExpr) [plancat.c]
| +-- oprjoin("<>") is built-in?
| | +-- yes -> fmgr_info(F_NEQJOINSEL) + manual fcinfo [fmgr.c]
| | | \-- neqjoinsel(): null_safe=true [selfuncs.c]
| | | +-- SEMI/ANTI -> distinctjoinsel_semi() -> S [selfuncs.c]
| | | \-- otherwise -> 1 - E - n1*n2
| | \-- no -> join_selectivity("<>") [plancat.c]
| | extension estimator, context=NULL
|
+-- = -> join_selectivity("=") -> eqjoinsel() [plancat.c -> selfuncs.c]
\-- <> -> join_selectivity("<>") -> neqjoinsel() [plancat.c -> selfuncs.c]
Key difference:
- v4: clausesel calls the generic estimator first (extension estimators still run),
then applies the NULL adjustment in the node-type branches. distinctnullsel()
does not bypass oprjoin.
- v5: clausesel only identifies the comparison semantics and passes context;
the NULL rules move into the join-type branches of eqjoinsel()/neqjoinsel().
Built-in estimators take the context path; extension estimators get no context
and keep their previous behavior.
regards,
--
ZizhuanLiu (X-MAN)
44973863(at)qq(dot)com
| Attachment | Content-Type | Size |
|---|---|---|
| v5-0001-Fix-join-selectivity-for-NULL-safe-comparisons-and-ordinary-inequality.patch | application/octet-stream | 60.2 KB |
| test_result_v5.xlsx | application/octet-stream | 69.7 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Kacper Kuras | 2026-12-05 16:04:43 | Re: BUG #18754: Logical replication cannot restart if client sent a copyDone |
| Previous Message | Richard Guo | 2026-12-05 06:52:28 | Re: BUG #19976: Internal planner error: ERROR: XX000: too late to create a new PlaceHolderInfo |