| From: | ZizhuanLiu X-MAN <44973863(at)qq(dot)com> |
|---|---|
| To: | pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: Optimize IS NOT DISTINCT FROM to equality when possible |
| Date: | 2026-11-23 07:42:19 |
| Message-ID: | tencent_4C17E0D9DB2469C32688BE292C83A8E31A09@qq.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
Attached is v2 of the patch that simplifies IS NOT DISTINCT FROM to an
ordinary equality expression when at least one input is known to be
non-null.
Changes since v1:
- The rewrite is now limited to top-level AND qual conjuncts and is done
in preprocess_expression()'s EXPRKIND_QUAL path, after
make_ands_implicit(), rather than inside eval_const_expressions().
v1 performed the rewrite in eval_const_expressions_mutator(), which is
context-free. That changed the value of "a IS NOT DISTINCT FROM b" in
a target list (NULL instead of false when one input is NULL), and it
could drop rows for "NOT (a IS NOT DISTINCT FROM b)" and
"(a IS NOT DISTINCT FROM b) IS FALSE", where NULL and false are not
interchangeable. Restricting the rewrite to top-level qual conjuncts
preserves the original semantics in all other contexts, while keeping
the useful cases: WHERE, ON and HAVING all go through this path.
- Reject non-strict equality operators. IS [NOT] DISTINCT FROM deals
with NULL inputs itself, whereas a plain OpExpr does so only if the
operator is strict. Without this check, a non-strict "=" could return
true for a NULL input and change the result.
- Add regression tests in predicate.sql. They cover index scans, hash
and merge joins, equivalence-class derivation, and the cases that must
not be rewritten (both inputs nullable, top-level NOT, target lists).
A local predicate_explain() helper prints estimated and actual row
counts with cost and timing masked, so the expected output is stable.
The core condition is unchanged from v1: when at least one input is
provably non-null (NOTNULL_SOURCE_HASHTABLE), the two forms select the
same rows in a top-level qual. If both inputs are non-null they return
the same value; if one is NULL, the original expression returns false
and the equality returns NULL, and both reject the row.
The implementation now lives in planner.c; clauses.c is no longer
modified. Since index scans, join clauses and equivalence classes are
all qual-level concepts, restricting the rewrite to qual does not lose
any useful optimization.
The predicate regression test passes.
Examples from predicate.sql (EXPLAIN (COSTS OFF)):
\d t_is_not_distinct
Table "public.t_is_not_distinct"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
id | integer | | |
a | integer | | |
b | integer | | not null |
Indexes:
"t_is_not_distinct_a_idx" btree (a)
"t_is_not_distinct_b_idx" btree (b)
WHERE a IS NOT DISTINCT FROM 1
before:
Seq Scan on t_is_not_distinct
Filter: (NOT (a IS DISTINCT FROM 1))
after:
Index Scan using t_is_not_distinct_a_idx on t_is_not_distinct
Index Cond: (a = 1)
x.a IS NOT DISTINCT FROM y.b (y.b is NOT NULL)
before:
Nested Loop
Join Filter: (NOT (x.a IS DISTINCT FROM y.b))
after:
Hash Join
Hash Cond: (x.a = y.b)
x.a IS NOT DISTINCT FROM y.b AND x.a = 1
before:
Nested Loop
-> Index Scan using t_is_not_distinct_a_idx on t_is_not_distinct x
Index Cond: (a = 1)
-> Seq Scan on t_is_not_distinct y
after:
Nested Loop
-> Index Scan using t_is_not_distinct_a_idx on t_is_not_distinct x
Index Cond: (a = 1)
-> Index Scan using t_is_not_distinct_b_idx on t_is_not_distinct y
Index Cond: (b = 1)
src/backend/optimizer/plan/planner.c | 111 ++++++++++++++++++
src/test/regress/sql/predicate.sql | 101 ++++++++++++++++
src/test/regress/expected/predicate.out | 197 ++++++++++++++++++++++++++++
3 files changed, 409 insertions(+)
regards,
--
ZizhuanLiu (X-MAN)
44973863(at)qq(dot)com
| Attachment | Content-Type | Size |
|---|---|---|
| v2-0001-Simplify-IS-NOT-DISTINCT-FROM-to-equality-when-po.patch | application/octet-stream | 21.2 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Andrey Borodin | 2026-11-23 07:45:48 | Re: [PATCH 0/2] Parallelizing the archiver |
| Previous Message | Adam Lee | 2026-11-23 07:37:43 | Re: [PATCH] Avoid orphaning buffers when a relation's file is missing |