Commit c058fc2a authored by Andres Freund's avatar Andres Freund

Rationalize expression context reset in ExecModifyTable().

The current pattern of reseting expressions both in
ExecProcessReturning() and ExecOnConflictUpdate() makes it harder than
necessary to reason about memory lifetimes.  It also requires
materializing slots unnecessarily, although this patch doesn't take
advantage of the fact that that's not necessary anymore.

Instead reset the expression context once for each input tuple.

Author: Ashutosh Bapat
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
parent 6a744413
...@@ -163,12 +163,6 @@ ExecProcessReturning(ResultRelInfo *resultRelInfo, ...@@ -163,12 +163,6 @@ ExecProcessReturning(ResultRelInfo *resultRelInfo,
ProjectionInfo *projectReturning = resultRelInfo->ri_projectReturning; ProjectionInfo *projectReturning = resultRelInfo->ri_projectReturning;
ExprContext *econtext = projectReturning->pi_exprContext; ExprContext *econtext = projectReturning->pi_exprContext;
/*
* Reset per-tuple memory context to free any expression evaluation
* storage allocated in the previous cycle.
*/
ResetExprContext(econtext);
/* Make tuple and any needed join variables available to ExecProject */ /* Make tuple and any needed join variables available to ExecProject */
if (tupleSlot) if (tupleSlot)
econtext->ecxt_scantuple = tupleSlot; econtext->ecxt_scantuple = tupleSlot;
...@@ -1453,13 +1447,7 @@ ExecOnConflictUpdate(ModifyTableState *mtstate, ...@@ -1453,13 +1447,7 @@ ExecOnConflictUpdate(ModifyTableState *mtstate,
elog(ERROR, "unrecognized heap_lock_tuple status: %u", test); elog(ERROR, "unrecognized heap_lock_tuple status: %u", test);
} }
/* /* Success, the tuple is locked. */
* Success, the tuple is locked.
*
* Reset per-tuple memory context to free any expression evaluation
* storage allocated in the previous cycle.
*/
ResetExprContext(econtext);
/* /*
* Verify that the tuple is visible to our MVCC snapshot if the current * Verify that the tuple is visible to our MVCC snapshot if the current
...@@ -2028,6 +2016,14 @@ ExecModifyTable(PlanState *pstate) ...@@ -2028,6 +2016,14 @@ ExecModifyTable(PlanState *pstate)
*/ */
ResetPerTupleExprContext(estate); ResetPerTupleExprContext(estate);
/*
* Reset per-tuple memory context used for processing on conflict and
* returning clauses, to free any expression evaluation storage
* allocated in the previous cycle.
*/
if (pstate->ps_ExprContext)
ResetExprContext(pstate->ps_ExprContext);
planSlot = ExecProcNode(subplanstate); planSlot = ExecProcNode(subplanstate);
if (TupIsNull(planSlot)) if (TupIsNull(planSlot))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment