Fix estimate_expression_value to constant-fold SQLValueFunction nodes.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 29 Nov 2016 00:08:38 +0000 (19:08 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 29 Nov 2016 00:08:45 +0000 (19:08 -0500)
Oversight in my commit 0bb51aa96.  Noted while poking at a recent
bug report --- HEAD's estimates for a query using CURRENT_DATE
were unexpectedly much worse than 9.6's.

src/backend/optimizer/util/clauses.c

index 9598f28bab31f5a57b83f9180db9cbb9a7194a90..9af29dd5b12468fbd9e72737698fa74fe1d5c18a 100644 (file)
@@ -3346,6 +3346,23 @@ eval_const_expressions_mutator(Node *node,
                newcoalesce->location = coalesceexpr->location;
                return (Node *) newcoalesce;
            }
+       case T_SQLValueFunction:
+           {
+               /*
+                * All variants of SQLValueFunction are stable, so if we are
+                * estimating the expression's value, we should evaluate the
+                * current function value.  Otherwise just copy.
+                */
+               SQLValueFunction *svf = (SQLValueFunction *) node;
+
+               if (context->estimate)
+                   return (Node *) evaluate_expr((Expr *) svf,
+                                                 svf->type,
+                                                 svf->typmod,
+                                                 InvalidOid);
+               else
+                   return copyObject((Node *) svf);
+           }
        case T_FieldSelect:
            {
                /*