postgresql/src/backend
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
..
access Fix some typos 2024-01-22 13:55:25 +09:00
archive Update copyright for 2024 2024-01-03 20:49:05 -05:00
backup Add new function pg_get_wal_summarizer_state(). 2024-01-11 12:41:18 -05:00
bootstrap Make attstattarget nullable 2024-01-13 18:14:53 +01:00
catalog Add optimized C string hashing 2024-01-19 12:56:15 +07:00
commands Fix some typos 2024-01-22 13:55:25 +09:00
executor Fix indentation in ExecParallelHashIncreaseNumBatches() 2024-01-08 20:00:20 +02:00
foreign Update copyright for 2024 2024-01-03 20:49:05 -05:00
jit Refactor code checking for file existence 2024-01-12 12:04:51 +09:00
lib Fix broken Bitmapset optimization in DiscreteKnapsack() 2024-01-19 10:44:36 +13:00
libpq Update copyright for 2024 2024-01-03 20:49:05 -05:00
main Update copyright for 2024 2024-01-03 20:49:05 -05:00
nodes Fix broken Bitmapset optimization in DiscreteKnapsack() 2024-01-19 10:44:36 +13:00
optimizer Add better handling of redundant IS [NOT] NULL quals 2024-01-23 18:09:18 +13:00
parser Add hint about not qualifying UPDATE...SET target with relation name. 2024-01-20 17:54:14 -05:00
partitioning Update copyright for 2024 2024-01-03 20:49:05 -05:00
po Update copyright for 2024 2024-01-03 20:49:05 -05:00
port Close socket in case of errors in setting non-blocking 2024-01-17 11:24:11 +01:00
postmaster Reindent after commit d9ef650fca. 2024-01-11 13:24:35 -05:00
regex Update copyright for 2024 2024-01-03 20:49:05 -05:00
replication Fix some typos 2024-01-22 13:55:25 +09:00
rewrite Support identity columns in partitioned tables 2024-01-16 17:24:52 +01:00
snowball Add copyright notices to a few perl scripts that don't have them 2024-01-05 13:15:50 +00:00
statistics Update copyright for 2024 2024-01-03 20:49:05 -05:00
storage Fix possible NULL pointer dereference in GetNamedDSMSegment(). 2024-01-22 20:44:38 -06:00
tcop Error message capitalisation 2024-01-18 09:35:12 +01:00
tsearch Update copyright for 2024 2024-01-03 20:49:05 -05:00
utils Fix ERROR message in injection_point.c 2024-01-23 10:45:00 +09:00
.gitignore
Makefile Cross-check lists of predefined LWLocks. 2024-01-09 11:05:19 -06:00
common.mk Blind attempt to fix LLVM dependency in the backend 2022-09-15 10:53:48 +07:00
meson.build Update copyright for 2024 2024-01-03 20:49:05 -05:00
nls.mk Remove distprep 2023-11-06 15:18:04 +01:00