mirror of
https://github.com/postgres/postgres.git
synced 2025-07-11 10:01:57 +03:00
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-kAEYD0sYWKPdQ@mail.gmail.com
This commit is contained in:
@ -31,6 +31,7 @@
|
||||
#include "parser/analyze.h"
|
||||
#include "rewrite/rewriteManip.h"
|
||||
#include "utils/lsyscache.h"
|
||||
#include "utils/rel.h"
|
||||
#include "utils/typcache.h"
|
||||
|
||||
/* These parameters are set by GUC */
|
||||
@ -2629,32 +2630,51 @@ add_base_clause_to_rel(PlannerInfo *root, Index relid,
|
||||
RestrictInfo *restrictinfo)
|
||||
{
|
||||
RelOptInfo *rel = find_base_rel(root, relid);
|
||||
RangeTblEntry *rte = root->simple_rte_array[relid];
|
||||
|
||||
Assert(bms_membership(restrictinfo->required_relids) == BMS_SINGLETON);
|
||||
|
||||
/* Don't add the clause if it is always true */
|
||||
if (restriction_is_always_true(root, restrictinfo))
|
||||
return;
|
||||
|
||||
/*
|
||||
* Substitute the origin qual with constant-FALSE if it is provably always
|
||||
* false. Note that we keep the same rinfo_serial.
|
||||
* For inheritance parent tables, we must always record the RestrictInfo
|
||||
* in baserestrictinfo as is. If we were to transform or skip adding it,
|
||||
* then the original wouldn't be available in apply_child_basequals. Since
|
||||
* there are two RangeTblEntries for inheritance parents, one with
|
||||
* inh==true and the other with inh==false, we're still able to apply this
|
||||
* optimization to the inh==false one. The inh==true one is what
|
||||
* apply_child_basequals() sees, whereas the inh==false one is what's used
|
||||
* for the scan node in the final plan.
|
||||
*
|
||||
* We make an exception to this is for partitioned tables. For these, we
|
||||
* always apply the constant-TRUE and constant-FALSE transformations. A
|
||||
* qual which is either of these for a partitioned table must also be that
|
||||
* for all of its child partitions.
|
||||
*/
|
||||
if (restriction_is_always_false(root, restrictinfo))
|
||||
if (!rte->inh || rte->relkind == RELKIND_PARTITIONED_TABLE)
|
||||
{
|
||||
int save_rinfo_serial = restrictinfo->rinfo_serial;
|
||||
/* Don't add the clause if it is always true */
|
||||
if (restriction_is_always_true(root, restrictinfo))
|
||||
return;
|
||||
|
||||
restrictinfo = make_restrictinfo(root,
|
||||
(Expr *) makeBoolConst(false, false),
|
||||
restrictinfo->is_pushed_down,
|
||||
restrictinfo->has_clone,
|
||||
restrictinfo->is_clone,
|
||||
restrictinfo->pseudoconstant,
|
||||
0, /* security_level */
|
||||
restrictinfo->required_relids,
|
||||
restrictinfo->incompatible_relids,
|
||||
restrictinfo->outer_relids);
|
||||
restrictinfo->rinfo_serial = save_rinfo_serial;
|
||||
/*
|
||||
* Substitute the origin qual with constant-FALSE if it is provably
|
||||
* always false. Note that we keep the same rinfo_serial.
|
||||
*/
|
||||
if (restriction_is_always_false(root, restrictinfo))
|
||||
{
|
||||
int save_rinfo_serial = restrictinfo->rinfo_serial;
|
||||
|
||||
restrictinfo = make_restrictinfo(root,
|
||||
(Expr *) makeBoolConst(false, false),
|
||||
restrictinfo->is_pushed_down,
|
||||
restrictinfo->has_clone,
|
||||
restrictinfo->is_clone,
|
||||
restrictinfo->pseudoconstant,
|
||||
0, /* security_level */
|
||||
restrictinfo->required_relids,
|
||||
restrictinfo->incompatible_relids,
|
||||
restrictinfo->outer_relids);
|
||||
restrictinfo->rinfo_serial = save_rinfo_serial;
|
||||
}
|
||||
}
|
||||
|
||||
/* Add clause to rel's restriction list */
|
||||
|
Reference in New Issue
Block a user