summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2007-10-11 21:28:39 +0000
committerTom Lane2007-10-11 21:28:39 +0000
commitb7f1fe6c46c971a89e6a6cfcc629b1ccffeef58f (patch)
tree80daad52de479805ed418e86d9be135e230739a3
parent245dd2bca78762b5784fb9ec532c01d6e08334ce (diff)
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.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 9b8bbf87b74..8d2a71c1fde 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.109.2.1 2005/04/14 21:44:46 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.109.2.2 2007/10/11 21:28:39 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
@@ -1702,9 +1702,20 @@ simplify_op_or_func(Expr *expr, List *args)
const_val = ExecEvalExprSwitchContext((Node *) newexpr, econtext,
&const_is_null, NULL);
- /* 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);
+ }
FreeExprContext(econtext);
pfree(newexpr);