]> granicus.if.org Git - postgresql/commitdiff
Avoid crash in eval_const_expressions if a Param's type changes.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 26 Jul 2018 20:08:45 +0000 (16:08 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 26 Jul 2018 20:08:45 +0000 (16:08 -0400)
Since commit 6719b238e it's been possible for the values of plpgsql
record field variables to be exposed to the planner as Params.
(Before that, plpgsql never supplied values for such variables during
planning, so that the problematic code wasn't reached.)  Other places
that touch potentially-type-mutable Params either cope gracefully or
do runtime-test-and-ereport checks that the type is what they expect.
But eval_const_expressions() just had an Assert, meaning that it either
failed the assertion or risked crashes due to using an incompatible
value.

In this case, rather than throwing an ereport immediately, we can just
not perform a const-substitution in case of a mismatch.  This seems
important for the same reason that the Param fetch was speculative:
we might not actually reach this part of the expression at runtime.

Test case will follow in a separate commit.

Patch by me, pursuant to bug report from Andrew Gierth.
Back-patch to v11 where the previous commit appeared.

Discussion: https://postgr.es/m/87wotkfju1.fsf@news-spur.riddles.org.uk

src/backend/optimizer/util/clauses.c

index 505ae0af85db78e7d8390e6f62057a53dd43b6db..a04ad6e99e61db0a8e758f256192714782a36469 100644 (file)
@@ -2567,7 +2567,14 @@ eval_const_expressions_mutator(Node *node,
                                        else
                                                prm = &paramLI->params[param->paramid - 1];
 
-                                       if (OidIsValid(prm->ptype))
+                                       /*
+                                        * We don't just check OidIsValid, but insist that the
+                                        * fetched type match the Param, just in case the hook did
+                                        * something unexpected.  No need to throw an error here
+                                        * though; leave that for runtime.
+                                        */
+                                       if (OidIsValid(prm->ptype) &&
+                                               prm->ptype == param->paramtype)
                                        {
                                                /* OK to substitute parameter value? */
                                                if (context->estimate ||
@@ -2583,7 +2590,6 @@ eval_const_expressions_mutator(Node *node,
                                                        bool            typByVal;
                                                        Datum           pval;
 
-                                                       Assert(prm->ptype == param->paramtype);
                                                        get_typlenbyval(param->paramtype,
                                                                                        &typLen, &typByVal);
                                                        if (prm->isnull || typByVal)