aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer/util/inherit.c
diff options
context:
space:
mode:
authorDavid Rowley <drowley@postgresql.org>2024-04-12 20:07:53 +1200
committerDavid Rowley <drowley@postgresql.org>2024-04-12 20:07:53 +1200
commit3af7040985b6df504a72cd307aad5d69ac5f5384 (patch)
treefece479e802969e5377b670dc6c33efc05780070 /src/backend/optimizer/util/inherit.c
parent6d4f062714953ed0ec865ff96d5af122b8c3e77f (diff)
downloadpostgresql-3af7040985b6df504a72cd307aad5d69ac5f5384.tar.gz
postgresql-3af7040985b6df504a72cd307aad5d69ac5f5384.zip
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
Diffstat (limited to 'src/backend/optimizer/util/inherit.c')
-rw-r--r--src/backend/optimizer/util/inherit.c33
1 files changed, 22 insertions, 11 deletions
diff --git a/src/backend/optimizer/util/inherit.c b/src/backend/optimizer/util/inherit.c
index 5c7acf8a901..4797312ae53 100644
--- a/src/backend/optimizer/util/inherit.c
+++ b/src/backend/optimizer/util/inherit.c
@@ -822,11 +822,13 @@ expand_appendrel_subquery(PlannerInfo *root, RelOptInfo *rel,
/*
* apply_child_basequals
* Populate childrel's base restriction quals from parent rel's quals,
- * translating them using appinfo.
+ * translating Vars using appinfo and re-checking for quals which are
+ * constant-TRUE or constant-FALSE when applied to this child relation.
*
* If any of the resulting clauses evaluate to constant false or NULL, we
* return false and don't apply any quals. Caller should mark the relation as
- * a dummy rel in this case, since it doesn't need to be scanned.
+ * a dummy rel in this case, since it doesn't need to be scanned. Constant
+ * true quals are ignored.
*/
bool
apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel,
@@ -875,6 +877,7 @@ apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel,
{
Node *onecq = (Node *) lfirst(lc2);
bool pseudoconstant;
+ RestrictInfo *childrinfo;
/* check for pseudoconstant (no Vars or volatile functions) */
pseudoconstant =
@@ -886,15 +889,23 @@ apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel,
root->hasPseudoConstantQuals = true;
}
/* reconstitute RestrictInfo with appropriate properties */
- childquals = lappend(childquals,
- make_restrictinfo(root,
- (Expr *) onecq,
- rinfo->is_pushed_down,
- rinfo->has_clone,
- rinfo->is_clone,
- pseudoconstant,
- rinfo->security_level,
- NULL, NULL, NULL));
+ childrinfo = make_restrictinfo(root,
+ (Expr *) onecq,
+ rinfo->is_pushed_down,
+ rinfo->has_clone,
+ rinfo->is_clone,
+ pseudoconstant,
+ rinfo->security_level,
+ NULL, NULL, NULL);
+
+ /* Restriction is proven always false */
+ if (restriction_is_always_false(root, childrinfo))
+ return false;
+ /* Restriction is proven always true, so drop it */
+ if (restriction_is_always_true(root, childrinfo))
+ continue;
+
+ childquals = lappend(childquals, childrinfo);
/* track minimum security level among child quals */
cq_min_security = Min(cq_min_security, rinfo->security_level);
}