Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Improve planner's understanding of strictness of type coercions.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 20 Feb 2019 19:39:11 +0000 (14:39 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 20 Feb 2019 19:39:11 +0000 (14:39 -0500)
PG type coercions are generally strict, ie a NULL input must produce
a NULL output (or, in domain cases, possibly an error).  The planner's
understanding of that was a bit incomplete though, so improve it:

* Teach contain_nonstrict_functions() that CoerceViaIO can always be
considered strict.  Previously it believed that only if the underlying
I/O functions were marked strict, which is often but not always true.

* Teach clause_is_strict_for() that CoerceViaIO, ArrayCoerceExpr,
ConvertRowtypeExpr, CoerceToDomain can all be considered strict.
Previously it knew nothing about any of them.

The main user-visible impact of this is that IS NOT NULL predicates
can be proven to hold from expressions involving casts in more cases
than before, allowing partial indexes with such predicates to be used
without extra pushups.  This reduces the surprise factor for users,
who may well be used to ordinary (function-call-based) casts being
known to be strict.

Per a gripe from Samuel Williams.  This doesn't rise to the level of
a bug, IMO, so no back-patch.

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

src/backend/optimizer/util/clauses.c
src/backend/optimizer/util/predtest.c

index 2d0bad7cde98a148631aa2e83179b523012c5970..501b0e9e2dc7a5644e9e58b1d2e1b8c99408e789 100644 (file)
@@ -1172,6 +1172,16 @@ contain_nonstrict_functions_walker(Node *node, void *context)
        return true;
    if (IsA(node, FieldStore))
        return true;
+   if (IsA(node, CoerceViaIO))
+   {
+       /*
+        * CoerceViaIO is strict regardless of whether the I/O functions are,
+        * so just go look at its argument; asking check_functions_in_node is
+        * useless expense and could deliver the wrong answer.
+        */
+       return contain_nonstrict_functions_walker((Node *) ((CoerceViaIO *) node)->arg,
+                                                 context);
+   }
    if (IsA(node, ArrayCoerceExpr))
    {
        /*
index 01f64eeab564a7870194ab57101d49795da4865d..3c9f245e4dafc6aa6e89f5968f437b8026b7ebcc 100644 (file)
@@ -1352,6 +1352,27 @@ clause_is_strict_for(Node *clause, Node *subexpr)
        return false;
    }
 
+   /*
+    * CoerceViaIO is strict (whether or not the I/O functions it calls are).
+    * Likewise, ArrayCoerceExpr is strict for its array argument (regardless
+    * of what the per-element expression is), ConvertRowtypeExpr is strict at
+    * the row level, and CoerceToDomain is strict too.  These are worth
+    * checking mainly because it saves us having to explain to users why some
+    * type coercions are known strict and others aren't.
+    */
+   if (IsA(clause, CoerceViaIO))
+       return clause_is_strict_for((Node *) ((CoerceViaIO *) clause)->arg,
+                                   subexpr);
+   if (IsA(clause, ArrayCoerceExpr))
+       return clause_is_strict_for((Node *) ((ArrayCoerceExpr *) clause)->arg,
+                                   subexpr);
+   if (IsA(clause, ConvertRowtypeExpr))
+       return clause_is_strict_for((Node *) ((ConvertRowtypeExpr *) clause)->arg,
+                                   subexpr);
+   if (IsA(clause, CoerceToDomain))
+       return clause_is_strict_for((Node *) ((CoerceToDomain *) clause)->arg,
+                                   subexpr);
+
    return false;
 }