Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2b0c86b

Browse files
committed
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.
1 parent 05c609b commit 2b0c86b

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/backend/optimizer/util/clauses.c

+14-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.249 2007/09/06 17:31:58 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.250 2007/10/11 21:27:49 tgl Exp $
1212
*
1313
* HISTORY
1414
* AUTHOR DATE MAJOR EVENT
@@ -3265,9 +3265,20 @@ evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod)
32653265
/* Get back to outer memory context */
32663266
MemoryContextSwitchTo(oldcontext);
32673267

3268-
/* Must copy result out of sub-context used by expression eval */
3268+
/*
3269+
* Must copy result out of sub-context used by expression eval.
3270+
*
3271+
* Also, if it's varlena, forcibly detoast it. This protects us against
3272+
* storing TOAST pointers into plans that might outlive the referenced
3273+
* data.
3274+
*/
32693275
if (!const_is_null)
3270-
const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
3276+
{
3277+
if (resultTypLen == -1)
3278+
const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
3279+
else
3280+
const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
3281+
}
32713282

32723283
/* Release all the junk we just created */
32733284
FreeExecutorState(estate);

0 commit comments

Comments
 (0)