Fix IS [NOT] NULL qual optimization for inheritance tables

b262ad440 added code to have the planner remove redundant IS NOT NULL
quals and eliminate needless scans for IS NULL quals on tables where the
qual's column has a NOT NULL constraint.

That commit failed to consider that an inheritance parent table could
have differing NOT NULL constraints between the parent and the child.
This caused issues as if we eliminated a qual on the parent, when
applying the quals to child tables in apply_child_basequals(), the qual
might not have been added to the parent's baserestrictinfo.

Here we fix this by not applying the optimization to remove redundant
quals to RelOptInfos belonging to inheritance parents and applying the
optimization again in apply_child_basequals().  Effectively, this means
that the parent and child are considered independently as the parent has
both an inh=true and inh=false RTE and we still apply the optimization
to the RelOptInfo corresponding to the inh=false RTE.

We're able to still apply the optimization in add_base_clause_to_rel()
for partitioned tables as the NULLability of partitions must match that
of their parent.  And, if we ever expand restriction_is_always_false()
and restriction_is_always_true() to handle partition constraints then we
can apply the same logic as, even in multi-level partitioned tables,
there's no way to route values to a partition when the qual does not
match the partition qual of the partitioned table's parent partition.
The same is true for CHECK constraints as those must also match between
arent partitioned tables and their partitions.

Author: Richard Guo, David Rowley
Discussion: 
https://postgr.es/m/cambws4930gqszmjr7aanzeapdy61gcg6z8dt-kaeyd0sywk...@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/3af7040985b6df504a72cd307aad5d69ac5f5384

Modified Files
--------------
src/backend/optimizer/plan/initsplan.c  | 60 ++++++++++++++++++++++-----------
src/backend/optimizer/util/inherit.c    | 33 ++++++++++++------
src/backend/optimizer/util/plancat.c    | 37 +++++++++++++-------
src/backend/optimizer/util/relnode.c    | 25 +++++++++-----
src/include/nodes/pathnodes.h           |  6 +++-
src/test/regress/expected/predicate.out | 48 ++++++++++++++++++++++++++
src/test/regress/sql/predicate.sql      | 27 +++++++++++++++
7 files changed, 182 insertions(+), 54 deletions(-)

Reply via email to