Hi
[1] pointed out that PostgreSQL's handling of IS [NOT] DISTINCT
FROM could be improved, noting that IS NOT DISTINCT FROM cannot
currently use indexes in the same way as ordinary equality
operators.
[2] implemented an optimization for IS DISTINCT FROM by
simplifying it to an equality operator when possible.
Following the approach in [2], this patch attempts to simplify
IS NOT DISTINCT FROM to an ordinary equality expression.
Specifically, if the input node is an expression of the form
"NOT (a IS DISTINCT FROM b)", and at least one of the operands
is known to be non-NULL, it can be safely converted to "a = b".
When both operands are non-NULL, the two expressions produce
the same result. When either operand is NULL, the original
expression returns FALSE, while the ordinary equality
comparison returns NULL. They therefore have the same filtering
behavior in a WHERE clause.
This enables the planner to generate better plans by utilizing
index scans, merge joins, hash joins, and equivalence-class-based
qual deduction.
I've attached a draft patch. Any feedback or suggestions on aspects
I may have overlooked would be very welcome.
References:
[1] https://www.postgresql.org/message-id/1385220.1657600906%40sss.pgh.pa.us
[2] https://www.postgresql.org/message-id/flat/CAMbWs4_kMG5qVGnhOS2C%3D3nYWZ9AT8ves834WbtbALzDmEf7%3Dg%40mail.gmail.com#c79474de5f920a4d4d2140a22b83fb99
regards,
--
ZizhuanLiu (X-MAN)
44973863@qq.com
Re: Optimize IS NOT DISTINCT FROM to equality when possible
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@qq.com