Commit 2b0c86b6 authored by Tom Lane's avatar Tom Lane

Ensure that the result of evaluating a function during constant-expression

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.
parent 05c609b3
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.249 2007/09/06 17:31:58 tgl Exp $ * $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.250 2007/10/11 21:27:49 tgl Exp $
* *
* HISTORY * HISTORY
* AUTHOR DATE MAJOR EVENT * AUTHOR DATE MAJOR EVENT
...@@ -3265,9 +3265,20 @@ evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod) ...@@ -3265,9 +3265,20 @@ evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod)
/* Get back to outer memory context */ /* Get back to outer memory context */
MemoryContextSwitchTo(oldcontext); 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) if (!const_is_null)
{
if (resultTypLen == -1)
const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
else
const_val = datumCopy(const_val, resultTypByVal, resultTypLen); const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
}
/* Release all the junk we just created */ /* Release all the junk we just created */
FreeExecutorState(estate); FreeExecutorState(estate);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment