aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer/util/clauses.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2007-10-11 21:27:49 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2007-10-11 21:27:49 +0000
commit2b0c86b66563cf2fd430ba47e6637846e512ee53 (patch)
tree80c9c923f1836a8415636f3477b130fe152bf5d6 /src/backend/optimizer/util/clauses.c
parent05c609b3d45cc173c5d2a247c07e4e03b30fd697 (diff)
downloadpostgresql-2b0c86b66563cf2fd430ba47e6637846e512ee53.tar.gz
postgresql-2b0c86b66563cf2fd430ba47e6637846e512ee53.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.
Diffstat (limited to 'src/backend/optimizer/util/clauses.c')
-rw-r--r--src/backend/optimizer/util/clauses.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index 24d3b09cf13..c541713f3f9 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -8,7 +8,7 @@
*
*
* 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
* AUTHOR DATE MAJOR EVENT
@@ -3265,9 +3265,20 @@ evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod)
/* 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);