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/[email protected]

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/c925ad30b047ec1388247539e86729d584c34b8d

Modified Files
--------------
src/backend/optimizer/util/plancat.c    | 48 +++++++++++++++++++++++++++------
src/test/regress/expected/join.out      |  2 +-
src/test/regress/expected/predicate.out | 41 ++++++++++++++++++++++++++++
src/test/regress/sql/join.sql           |  2 +-
src/test/regress/sql/predicate.sql      | 19 +++++++++++++
5 files changed, 102 insertions(+), 10 deletions(-)

Reply via email to