1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

Fix expr_is_nonnullable() for IS DISTINCT FROM

Commit c8d2f68cc taught expr_is_nonnullable() to treat a DistinctExpr
as never returning NULL.  That is wrong: when neither input is NULL,
the result is NULL if the underlying equality operator returns NULL,
which nothing forbids.  This could lead to wrong query results.

To fix, remove the DistinctExpr case, along with its test case.

While at it, test the relkind before calling has_subclass() in
var_is_nonnullable(), to avoid a useless syscache lookup for
partitioned tables.

Reported-by: Robert Haas <robertmhaas@gmail.com>
Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CA+TgmoaLcsmc9weJr1yo1X1SwZJX1t4XXHj0SJzH8XztmN9Hdg@mail.gmail.com
Backpatch-through: 19
This commit is contained in:
Richard Guo
2026-10-10 10:52:52 +09:00
parent 91f75212bb
commit f5635c2786
3 changed files with 2 additions and 24 deletions

View File

@@ -4950,8 +4950,8 @@ var_is_nonnullable(PlannerInfo *root, Var *var, NotNullSource source)
* Note that we need to check if the relation actually has any
* children, as we might not have done that yet.
*/
if (rte->inh && has_subclass(rte->relid) &&
rte->relkind != RELKIND_PARTITIONED_TABLE)
if (rte->relkind != RELKIND_PARTITIONED_TABLE &&
rte->inh && has_subclass(rte->relid))
return false;
/* We need not lock the relation since it was already locked */
@@ -5100,14 +5100,6 @@ expr_is_nonnullable(PlannerInfo *root, Expr *expr, NotNullSource source)
*/
return true;
}
case T_DistinctExpr:
{
/*
* IS DISTINCT FROM never returns NULL, effectively acting as
* though NULL were a normal data value.
*/
return true;
}
case T_RelabelType:
{
/*

View File

@@ -611,16 +611,6 @@ SELECT * FROM pred_tab WHERE ((a > 1) IS TRUE) IS NULL;
One-Time Filter: false
(3 rows)
-- DistinctExpr
EXPLAIN (COSTS OFF)
SELECT * FROM pred_tab WHERE (a IS DISTINCT FROM b) IS NULL;
QUERY PLAN
------------------------------
Result
Replaces: Scan on pred_tab
One-Time Filter: false
(3 rows)
-- RelabelType
EXPLAIN (COSTS OFF)
SELECT * FROM pred_tab WHERE (a::oid) IS NULL;

View File

@@ -299,10 +299,6 @@ SELECT * FROM pred_tab WHERE (b IS NULL) IS NULL;
EXPLAIN (COSTS OFF)
SELECT * FROM pred_tab WHERE ((a > 1) IS TRUE) IS NULL;
-- DistinctExpr
EXPLAIN (COSTS OFF)
SELECT * FROM pred_tab WHERE (a IS DISTINCT FROM b) IS NULL;
-- RelabelType
EXPLAIN (COSTS OFF)
SELECT * FROM pred_tab WHERE (a::oid) IS NULL;