diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2007-10-11 21:28:30 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2007-10-11 21:28:30 +0000 |
commit | ad8e3b37a7782f529a21deae1dafdbbc7b51bc17 (patch) | |
tree | 5b8546967b09d6d114aff945f59d461935100cd5 | |
parent | d9e961d52737823b30549d5c130790d62a79731f (diff) | |
download | postgresql-ad8e3b37a7782f529a21deae1dafdbbc7b51bc17.tar.gz postgresql-ad8e3b37a7782f529a21deae1dafdbbc7b51bc17.zip |
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.
-rw-r--r-- | src/backend/optimizer/util/clauses.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index 6683e417ca5..db7dcd313c7 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.154.2.7 2007/05/01 18:54:24 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.154.2.8 2007/10/11 21:28:30 tgl Exp $ * * HISTORY * AUTHOR DATE MAJOR EVENT @@ -2118,9 +2118,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); |