Skip to content

Commit b7f1fe6

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 245dd2b commit b7f1fe6

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-
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.109.2.1 2005/04/14 21:44:46 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.109.2.2 2007/10/11 21:28:39 tgl Exp $
1212
*
1313
* HISTORY
1414
* AUTHOR DATE MAJOR EVENT
@@ -1702,9 +1702,20 @@ simplify_op_or_func(Expr *expr, List *args)
17021702
const_val = ExecEvalExprSwitchContext((Node *) newexpr, econtext,
17031703
&const_is_null, NULL);
17041704

1705-
/* Must copy result out of sub-context used by expression eval */
1705+
/*
1706+
* Must copy result out of sub-context used by expression eval.
1707+
*
1708+
* Also, if it's varlena, forcibly detoast it. This protects us against
1709+
* storing TOAST pointers into plans that might outlive the referenced
1710+
* data.
1711+
*/
17061712
if (!const_is_null)
1707-
const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
1713+
{
1714+
if (resultTypLen == -1)
1715+
const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
1716+
else
1717+
const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
1718+
}
17081719

17091720
FreeExprContext(econtext);
17101721
pfree(newexpr);

0 commit comments

Comments
 (0)