Fix const-simplification for index expressions and predicate
authorRichard Guo <[email protected]>
Tue, 9 Dec 2025 07:56:26 +0000 (16:56 +0900)
committerRichard Guo <[email protected]>
Tue, 9 Dec 2025 07:56:26 +0000 (16:56 +0900)
commitc925ad30b047ec1388247539e86729d584c34b8d
tree038f5d6fbc32cba9065c62de7fe85b6160457c10
parent04396eacd3faeaa4fa3d084a6749e4e384bdf0db
Fix const-simplification for index expressions and predicate

Similar to the issue with constraint and statistics expressions fixed
in 317c117d6, index expressions and predicate can also suffer from
incorrect reduction of NullTest clauses during const-simplification,
due to unfixed varnos and the use of a NULL root.  It has been
reported that this issue can cause the planner to fail to pick up a
partial index that it previously matched successfully.

Because we need to cache the const-simplified index expressions and
predicate in the relcache entry, we cannot fix the Vars before
applying eval_const_expressions.  To ensure proper reduction of
NullTest clauses, this patch runs eval_const_expressions a second time
-- after the Vars have been fixed and with a valid root.

It could be argued that the additional call to eval_const_expressions
might increase planning time, but I don't think that's a concern.  It
only runs when index expressions and predicate are present; it is
relatively cheap when run on small expression trees (which is
typically the case for index expressions and predicate), and it runs
on expressions that have already been const-simplified once, making
the second pass even cheaper.  In return, in cases like the one
reported, it allows the planner to match and use partial indexes,
which can lead to significant execution-time improvements.

Bug: #19007
Reported-by: Bryan Fox <[email protected]>
Author: Richard Guo <[email protected]>
Discussion: https://postgr.es/m/19007-4cc6e252ed8aa54a@postgresql.org
src/backend/optimizer/util/plancat.c
src/test/regress/expected/join.out
src/test/regress/expected/predicate.out
src/test/regress/sql/join.sql
src/test/regress/sql/predicate.sql