From: Tom Lane Date: Thu, 11 Oct 2007 21:28:20 +0000 (+0000) Subject: Ensure that the result of evaluating a function during constant-expression X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=c28c421850a6aba48132f45ecd79ac060bf1eabe;p=users%2Fbernd%2Fpostgres.git 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. --- diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index bca819ccaa..fe878e4837 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -2506,9 +2506,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);