]> granicus.if.org Git - postgresql/commitdiff
Ensure that the result of evaluating a function during constant-expression
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 11 Oct 2007 21:28:12 +0000 (21:28 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 11 Oct 2007 21:28:12 +0000 (21:28 +0000)
simplification gets detoasted before it is incorporated into a Const node.
Otherwise, if an immutable function were to return a TOAST pointer (an
unlikely case, but it can be made to happen), we would end up with a plan
that depends on the continued existence of the out-of-line toast datum.

src/backend/optimizer/util/clauses.c

index 34f52937fe42786926c8ede9c020913ebf9fad70..c146f8049c1f24a9c77de9aa182681decc5b1421 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.201.2.4 2007/05/01 18:54:09 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.201.2.5 2007/10/11 21:28:12 tgl Exp $
  *
  * HISTORY
  *       AUTHOR                        DATE                    MAJOR EVENT
@@ -2662,9 +2662,20 @@ evaluate_expr(Expr *expr, Oid result_type)
        /* Get back to outer memory context */
        MemoryContextSwitchTo(oldcontext);
 
-       /* Must copy result out of sub-context used by expression eval */
+       /*
+        * Must copy result out of sub-context used by expression eval.
+        *
+        * Also, if it's varlena, forcibly detoast it.  This protects us against
+        * storing TOAST pointers into plans that might outlive the referenced
+        * data.
+        */
        if (!const_is_null)
-               const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
+       {
+               if (resultTypLen == -1)
+                       const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
+               else
+                       const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
+       }
 
        /* Release all the junk we just created */
        FreeExecutorState(estate);