33 * procedural language
44 *
55 * IDENTIFICATION
6- * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.145 2005/06/2020:44:44 tgl Exp $
6+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.146 2005/06/2022:51:29 tgl Exp $
77 *
88 * This software is copyrighted by Jan Wieck - Hamburg.
99 *
@@ -2012,11 +2012,30 @@ plpgsql_estate_setup(PLpgSQL_execstate *estate,
20122012estate -> eval_tuptable = NULL ;
20132013estate -> eval_processed = 0 ;
20142014estate -> eval_lastoid = InvalidOid ;
2015- estate -> eval_econtext = NULL ;
20162015
20172016estate -> err_func = func ;
20182017estate -> err_stmt = NULL ;
20192018estate -> err_text = NULL ;
2019+
2020+ /*
2021+ * Create an EState for evaluation of simple expressions, if there's
2022+ * not one already in the current transaction.The EState is made a
2023+ * child of TopTransactionContext so it will have the right lifespan.
2024+ */
2025+ if (simple_eval_estate == NULL )
2026+ {
2027+ MemoryContext oldcontext ;
2028+
2029+ oldcontext = MemoryContextSwitchTo (TopTransactionContext );
2030+ simple_eval_estate = CreateExecutorState ();
2031+ MemoryContextSwitchTo (oldcontext );
2032+ }
2033+
2034+ /*
2035+ * Create an expression context for simple expressions.
2036+ * This must be a child of simple_eval_estate.
2037+ */
2038+ estate -> eval_econtext = CreateExprContext (simple_eval_estate );
20202039}
20212040
20222041/* ----------
@@ -3264,6 +3283,8 @@ exec_eval_datum(PLpgSQL_execstate *estate,
32643283Datum * value ,
32653284bool * isnull )
32663285{
3286+ MemoryContext oldcontext ;
3287+
32673288switch (datum -> dtype )
32683289{
32693290case PLPGSQL_DTYPE_VAR :
@@ -3290,9 +3311,11 @@ exec_eval_datum(PLpgSQL_execstate *estate,
32903311elog (ERROR ,"row variable has no tupdesc" );
32913312/* Make sure we have a valid type/typmod setting */
32923313BlessTupleDesc (row -> rowtupdesc );
3314+ oldcontext = MemoryContextSwitchTo (estate -> eval_econtext -> ecxt_per_tuple_memory );
32933315tup = make_tuple_from_row (estate ,row ,row -> rowtupdesc );
32943316if (tup == NULL )/* should not happen */
32953317elog (ERROR ,"row not compatible with its own tupdesc" );
3318+ MemoryContextSwitchTo (oldcontext );
32963319* typeid = row -> rowtupdesc -> tdtypeid ;
32973320* value = HeapTupleGetDatum (tup );
32983321* isnull = false;
@@ -3325,10 +3348,12 @@ exec_eval_datum(PLpgSQL_execstate *estate,
33253348 * fields. Copy the tuple body and insert the right
33263349 * values.
33273350 */
3351+ oldcontext = MemoryContextSwitchTo (estate -> eval_econtext -> ecxt_per_tuple_memory );
33283352heap_copytuple_with_tuple (rec -> tup ,& worktup );
33293353HeapTupleHeaderSetDatumLength (worktup .t_data ,worktup .t_len );
33303354HeapTupleHeaderSetTypeId (worktup .t_data ,rec -> tupdesc -> tdtypeid );
33313355HeapTupleHeaderSetTypMod (worktup .t_data ,rec -> tupdesc -> tdtypmod );
3356+ MemoryContextSwitchTo (oldcontext );
33323357* typeid = rec -> tupdesc -> tdtypeid ;
33333358* value = HeapTupleGetDatum (& worktup );
33343359* isnull = false;
@@ -3605,7 +3630,7 @@ exec_eval_simple_expr(PLpgSQL_execstate *estate,
36053630Oid * rettype )
36063631{
36073632Datum retval ;
3608- ExprContext * volatile econtext ;
3633+ ExprContext * econtext = estate -> eval_econtext ;
36093634ParamListInfo paramLI ;
36103635int i ;
36113636Snapshot saveActiveSnapshot ;
@@ -3615,20 +3640,6 @@ exec_eval_simple_expr(PLpgSQL_execstate *estate,
36153640 */
36163641* rettype = expr -> expr_simple_type ;
36173642
3618- /*
3619- * Create an EState for evaluation of simple expressions, if there's
3620- * not one already in the current transaction.The EState is made a
3621- * child of TopTransactionContext so it will have the right lifespan.
3622- */
3623- if (simple_eval_estate == NULL )
3624- {
3625- MemoryContext oldcontext ;
3626-
3627- oldcontext = MemoryContextSwitchTo (TopTransactionContext );
3628- simple_eval_estate = CreateExecutorState ();
3629- MemoryContextSwitchTo (oldcontext );
3630- }
3631-
36323643/*
36333644 * Prepare the expression for execution, if it's not been done already
36343645 * in the current transaction.
@@ -3642,18 +3653,6 @@ exec_eval_simple_expr(PLpgSQL_execstate *estate,
36423653active_simple_exprs = expr ;
36433654}
36443655
3645- /*
3646- * Create an expression context for simple expressions, if there's not
3647- * one already in the current function call. This must be a child of
3648- * simple_eval_estate.
3649- */
3650- econtext = estate -> eval_econtext ;
3651- if (econtext == NULL )
3652- {
3653- econtext = CreateExprContext (simple_eval_estate );
3654- estate -> eval_econtext = econtext ;
3655- }
3656-
36573656/*
36583657 * Param list can live in econtext's temporary memory context.
36593658 *