Fix improper uses of canonicalize_qual().
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 11 Mar 2018 22:10:43 +0000 (18:10 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 11 Mar 2018 22:10:43 +0000 (18:10 -0400)
One of the things canonicalize_qual() does is to remove constant-NULL
subexpressions of top-level AND/OR clauses.  It does that on the assumption
that what it's given is a top-level WHERE clause, so that NULL can be
treated like FALSE.  Although this is documented down inside a subroutine
of canonicalize_qual(), it wasn't mentioned in the documentation of that
function itself, and some callers hadn't gotten that memo.

Notably, commit d007a9505 caused get_relation_constraints() to apply
canonicalize_qual() to CHECK constraints.  That allowed constraint
exclusion to misoptimize situations in which a CHECK constraint had a
provably-NULL subclause, as seen in the regression test case added here,
in which a child table that should be scanned is not.  (Although this
thinko is ancient, the test case doesn't fail before 9.2, for reasons
I've not bothered to track down in detail.  There may be related cases
that do fail before that.)

More recently, commit f0e44751d added an independent bug by applying
canonicalize_qual() to index expressions, which is even sillier since
those might not even be boolean.  If they are, though, I think this
could lead to making incorrect index entries for affected index
expressions in v10.  I haven't attempted to prove that though.

To fix, add an "is_check" parameter to canonicalize_qual() to specify
whether it should assume WHERE or CHECK semantics, and make it perform
NULL-elimination accordingly.  Adjust the callers to apply the right
semantics, or remove the call entirely in cases where it's not known
that the expression has one or the other semantics.  I also removed
the call in some cases involving partition expressions, where it should
be a no-op because such expressions should be canonical already ...
and was a no-op, independently of whether it could in principle have
done something, because it was being handed the qual in implicit-AND
format which isn't what it expects.  In HEAD, add an Assert to catch
that type of mistake in future.

This represents an API break for external callers of canonicalize_qual().
While that's intentional in HEAD to make such callers think about which
case applies to them, it seems like something we probably wouldn't be
thanked for in released branches.  Hence, in released branches, the
extra parameter is added to a new function canonicalize_qual_ext(),
and canonicalize_qual() is a wrapper that retains its old behavior.

Patch by me with suggestions from Dean Rasheed.  Back-patch to all
supported branches.

Discussion: https://postgr.es/m/24475.1520635069@sss.pgh.pa.us

src/backend/optimizer/plan/planner.c
src/backend/optimizer/plan/subselect.c
src/backend/optimizer/prep/prepqual.c
src/backend/optimizer/util/plancat.c
src/backend/utils/cache/relcache.c
src/include/optimizer/prep.h
src/test/regress/expected/inherit.out
src/test/regress/sql/inherit.sql

index 300fbcec1635bbdb2b47a055eef943e0c89c73c1..98c301f1a40c4c7fc6eebe52638b7f8f86558252 100644 (file)
@@ -657,7 +657,7 @@ preprocess_expression(PlannerInfo *root, Node *expr, int kind)
     */
    if (kind == EXPRKIND_QUAL)
    {
-       expr = (Node *) canonicalize_qual((Expr *) expr);
+       expr = (Node *) canonicalize_qual_ext((Expr *) expr, false);
 
 #ifdef OPTIMIZER_DEBUG
        printf("After canonicalize_qual()\n");
index 18f75eb4dcd5f9a671cb272c216072f893c1bd7b..1168dd7c5f2a686abb79b009bfd8305bb6d88b25 100644 (file)
@@ -1572,7 +1572,7 @@ convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
     * subroot.
     */
    whereClause = eval_const_expressions(root, whereClause);
-   whereClause = (Node *) canonicalize_qual((Expr *) whereClause);
+   whereClause = (Node *) canonicalize_qual_ext((Expr *) whereClause, false);
    whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
 
    /*
index 684b1a5e4c5faf4d87c1bd81980e64ae7e3d54c8..8aa3a3a5bad29ccc3faff718a2ad805e20652314 100644 (file)
@@ -40,7 +40,7 @@
 
 static List *pull_ands(List *andlist);
 static List *pull_ors(List *orlist);
-static Expr *find_duplicate_ors(Expr *qual);
+static Expr *find_duplicate_ors(Expr *qual, bool is_check);
 static Expr *process_duplicate_ors(List *orlist);
 
 
@@ -268,6 +268,24 @@ negate_clause(Node *node)
  * canonicalize_qual
  *   Convert a qualification expression to the most useful form.
  *
+ * Backwards-compatibility wrapper for use by external code that hasn't
+ * been updated.
+ */
+Expr *
+canonicalize_qual(Expr *qual)
+{
+   return canonicalize_qual_ext(qual, false);
+}
+
+/*
+ * canonicalize_qual_ext
+ *   Convert a qualification expression to the most useful form.
+ *
+ * This is primarily intended to be used on top-level WHERE (or JOIN/ON)
+ * clauses.  It can also be used on top-level CHECK constraints, for which
+ * pass is_check = true.  DO NOT call it on any expression that is not known
+ * to be one or the other, as it might apply inappropriate simplifications.
+ *
  * The name of this routine is a holdover from a time when it would try to
  * force the expression into canonical AND-of-ORs or OR-of-ANDs form.
  * Eventually, we recognized that that had more theoretical purity than
@@ -282,7 +300,7 @@ negate_clause(Node *node)
  * Returns the modified qualification.
  */
 Expr *
-canonicalize_qual(Expr *qual)
+canonicalize_qual_ext(Expr *qual, bool is_check)
 {
    Expr       *newqual;
 
@@ -295,7 +313,7 @@ canonicalize_qual(Expr *qual)
     * within the top-level AND/OR structure; there's no point in looking
     * deeper.  Also remove any NULL constants in the top-level structure.
     */
-   newqual = find_duplicate_ors(qual);
+   newqual = find_duplicate_ors(qual, is_check);
 
    return newqual;
 }
@@ -394,16 +412,17 @@ pull_ors(List *orlist)
  *   Only the top-level AND/OR structure is searched.
  *
  * While at it, we remove any NULL constants within the top-level AND/OR
- * structure, eg "x OR NULL::boolean" is reduced to "x".  In general that
- * would change the result, so eval_const_expressions can't do it; but at
- * top level of WHERE, we don't need to distinguish between FALSE and NULL
- * results, so it's valid to treat NULL::boolean the same as FALSE and then
- * simplify AND/OR accordingly.
+ * structure, eg in a WHERE clause, "x OR NULL::boolean" is reduced to "x".
+ * In general that would change the result, so eval_const_expressions can't
+ * do it; but at top level of WHERE, we don't need to distinguish between
+ * FALSE and NULL results, so it's valid to treat NULL::boolean the same
+ * as FALSE and then simplify AND/OR accordingly.  Conversely, in a top-level
+ * CHECK constraint, we may treat a NULL the same as TRUE.
  *
  * Returns the modified qualification.  AND/OR flatness is preserved.
  */
 static Expr *
-find_duplicate_ors(Expr *qual)
+find_duplicate_ors(Expr *qual, bool is_check)
 {
    if (or_clause((Node *) qual))
    {
@@ -415,18 +434,29 @@ find_duplicate_ors(Expr *qual)
        {
            Expr       *arg = (Expr *) lfirst(temp);
 
-           arg = find_duplicate_ors(arg);
+           arg = find_duplicate_ors(arg, is_check);
 
            /* Get rid of any constant inputs */
            if (arg && IsA(arg, Const))
            {
                Const      *carg = (Const *) arg;
 
-               /* Drop constant FALSE or NULL */
-               if (carg->constisnull || !DatumGetBool(carg->constvalue))
-                   continue;
-               /* constant TRUE, so OR reduces to TRUE */
-               return arg;
+               if (is_check)
+               {
+                   /* Within OR in CHECK, drop constant FALSE */
+                   if (!carg->constisnull && !DatumGetBool(carg->constvalue))
+                       continue;
+                   /* Constant TRUE or NULL, so OR reduces to TRUE */
+                   return (Expr *) makeBoolConst(true, false);
+               }
+               else
+               {
+                   /* Within OR in WHERE, drop constant FALSE or NULL */
+                   if (carg->constisnull || !DatumGetBool(carg->constvalue))
+                       continue;
+                   /* Constant TRUE, so OR reduces to TRUE */
+                   return arg;
+               }
            }
 
            orlist = lappend(orlist, arg);
@@ -448,18 +478,29 @@ find_duplicate_ors(Expr *qual)
        {
            Expr       *arg = (Expr *) lfirst(temp);
 
-           arg = find_duplicate_ors(arg);
+           arg = find_duplicate_ors(arg, is_check);
 
            /* Get rid of any constant inputs */
            if (arg && IsA(arg, Const))
            {
                Const      *carg = (Const *) arg;
 
-               /* Drop constant TRUE */
-               if (!carg->constisnull && DatumGetBool(carg->constvalue))
-                   continue;
-               /* constant FALSE or NULL, so AND reduces to FALSE */
-               return (Expr *) makeBoolConst(false, false);
+               if (is_check)
+               {
+                   /* Within AND in CHECK, drop constant TRUE or NULL */
+                   if (carg->constisnull || DatumGetBool(carg->constvalue))
+                       continue;
+                   /* Constant FALSE, so AND reduces to FALSE */
+                   return arg;
+               }
+               else
+               {
+                   /* Within AND in WHERE, drop constant TRUE */
+                   if (!carg->constisnull && DatumGetBool(carg->constvalue))
+                       continue;
+                   /* Constant FALSE or NULL, so AND reduces to FALSE */
+                   return (Expr *) makeBoolConst(false, false);
+               }
            }
 
            andlist = lappend(andlist, arg);
index a9e51b4f32912e0d77caef5de64d9b8bf280a513..f5eab59f96fd17c089c02c50a2eff783b68f796d 100644 (file)
@@ -685,7 +685,7 @@ get_relation_constraints(PlannerInfo *root,
             */
            cexpr = eval_const_expressions(root, cexpr);
 
-           cexpr = (Node *) canonicalize_qual((Expr *) cexpr);
+           cexpr = (Node *) canonicalize_qual_ext((Expr *) cexpr, true);
 
            /* Fix Vars to have the desired varno */
            if (varno != 1)
index 799e9aebe9218a7432da1afe4e74a44840d33390..47f0913f7088cb2f624ac5879a1baac6896b141a 100644 (file)
@@ -3712,7 +3712,8 @@ RelationGetIndexExpressions(Relation relation)
     * Run the expressions through eval_const_expressions. This is not just an
     * optimization, but is necessary, because the planner will be comparing
     * them to similarly-processed qual clauses, and may fail to detect valid
-    * matches without this.  We don't bother with canonicalize_qual, however.
+    * matches without this.  We must not use canonicalize_qual, however,
+    * since these aren't qual expressions.
     */
    result = (List *) eval_const_expressions(NULL, (Node *) result);
 
@@ -3780,7 +3781,7 @@ RelationGetIndexPredicate(Relation relation)
     */
    result = (List *) eval_const_expressions(NULL, (Node *) result);
 
-   result = (List *) canonicalize_qual((Expr *) result);
+   result = (List *) canonicalize_qual_ext((Expr *) result, false);
 
    /* Also convert to implicit-AND format */
    result = make_ands_implicit((Expr *) result);
index 13453d80c6ef60b16a191afbad16adb2ab4474b6..c7866fa68772ab21c2b91185b4f08182b2a0437d 100644 (file)
@@ -34,6 +34,7 @@ extern Relids get_relids_for_join(PlannerInfo *root, int joinrelid);
  */
 extern Node *negate_clause(Node *node);
 extern Expr *canonicalize_qual(Expr *qual);
+extern Expr *canonicalize_qual_ext(Expr *qual, bool is_check);
 
 /*
  * prototypes for preptlist.c
index 0782242a62a7b89f82f165cffda21bf79ae5e09d..c873aab353e4a039a98f6fbe1300175a0bfbcad9 100644 (file)
@@ -1604,3 +1604,27 @@ FROM generate_series(1, 3) g(i);
 reset enable_seqscan;
 reset enable_indexscan;
 reset enable_bitmapscan;
+--
+-- Check handling of a constant-null CHECK constraint
+--
+create table cnullparent (f1 int);
+create table cnullchild (check (f1 = 1 or f1 = null)) inherits(cnullparent);
+insert into cnullchild values(1);
+insert into cnullchild values(2);
+insert into cnullchild values(null);
+select * from cnullparent;
+ f1 
+----
+  1
+  2
+   
+(3 rows)
+
+select * from cnullparent where f1 = 2;
+ f1 
+----
+  2
+(1 row)
+
+drop table cnullparent cascade;
+NOTICE:  drop cascades to table cnullchild
index 0e390cda3e4d31899bb4c30fe146da81d7e0b569..385359b76112581b59f0fcb18047ccd11b2a3370 100644 (file)
@@ -562,3 +562,15 @@ FROM generate_series(1, 3) g(i);
 reset enable_seqscan;
 reset enable_indexscan;
 reset enable_bitmapscan;
+
+--
+-- Check handling of a constant-null CHECK constraint
+--
+create table cnullparent (f1 int);
+create table cnullchild (check (f1 = 1 or f1 = null)) inherits(cnullparent);
+insert into cnullchild values(1);
+insert into cnullchild values(2);
+insert into cnullchild values(null);
+select * from cnullparent;
+select * from cnullparent where f1 = 2;
+drop table cnullparent cascade;