postgresql/src/backend/optimizer/util
David Rowley b262ad440e Add better handling of redundant IS [NOT] NULL quals
Until now PostgreSQL has not been very smart about optimizing away IS
NOT NULL base quals on columns defined as NOT NULL.  The evaluation of
these needless quals adds overhead.  Ordinarily, anyone who came
complaining about that would likely just have been told to not include
the qual in their query if it's not required.  However, a recent bug
report indicates this might not always be possible.

Bug 17540 highlighted that when we optimize Min/Max aggregates the IS NOT
NULL qual that the planner adds to make the rewritten plan ignore NULLs
can cause issues with poor index choice.  That particular case
demonstrated that other quals, especially ones where no statistics are
available to allow the planner a chance at estimating an approximate
selectivity for can result in poor index choice due to cheap startup paths
being prefered with LIMIT 1.

Here we take generic approach to fixing this by having the planner check
for NOT NULL columns and just have the planner remove these quals (when
they're not needed) for all queries, not just when optimizing Min/Max
aggregates.

Additionally, here we also detect IS NULL quals on a NOT NULL column and
transform that into a gating qual so that we don't have to perform the
scan at all.  This also works for join relations when the Var is not
nullable by any outer join.

This also helps with the self-join removal work as it must replace
strict join quals with IS NOT NULL quals to ensure equivalence with the
original query.

Author: David Rowley, Richard Guo, Andy Fan
Reviewed-by: Richard Guo, David Rowley
Discussion: https://postgr.es/m/CAApHDvqg6XZDhYRPz0zgOcevSMo0d3vxA9DvHrZtKfqO30WTnw@mail.gmail.com
Discussion: https://postgr.es/m/17540-7aa1855ad5ec18b4%40postgresql.org
2024-01-23 18:09:18 +13:00
..
Makefile Split all OBJS style lines in makefiles into one-line-per-entry style. 2019-11-05 14:41:07 -08:00
appendinfo.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
clauses.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
inherit.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
joininfo.c Add better handling of redundant IS [NOT] NULL quals 2024-01-23 18:09:18 +13:00
meson.build Update copyright for 2024 2024-01-03 20:49:05 -05:00
orclauses.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
paramassign.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pathnode.c Fix Asserts in calc_non_nestloop_required_outer(). 2024-01-10 13:51:36 -05:00
placeholder.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
plancat.c Add better handling of redundant IS [NOT] NULL quals 2024-01-23 18:09:18 +13:00
predtest.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
relnode.c Add better handling of redundant IS [NOT] NULL quals 2024-01-23 18:09:18 +13:00
restrictinfo.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
tlist.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
var.c Update copyright for 2024 2024-01-03 20:49:05 -05:00