Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8ce22dd

Browse files
committed
Fix plpgsql's handling of "simple" expression evaluation.
In general, expression execution state trees aren't re-entrantly usable, since functions can store private state information in them. For efficiency reasons, plpgsql tries to cache and reuse state trees for "simple" expressions. It can get away with that most of the time, but it can fail if the state tree is dirty from a previous failed execution (as in an example from Alvaro) or is being used recursively (as noted by me). Fix by tracking whether a state tree is in use, and falling back to the "non-simple" code path if so. This results in a pretty considerable speed hit when the non-simple path is taken, but the available alternatives seem even more unpleasant because they add overhead in the simple path. Per idea from Heikki. Back-patch to all supported branches.
1 parent e9eb4f4 commit 8ce22dd

File tree

4 files changed

+121
-4
lines changed

4 files changed

+121
-4
lines changed

src/pl/plpgsql/src/pl_exec.c

+27-1
Original file line numberDiff line numberDiff line change
@@ -4491,7 +4491,18 @@ exec_for_query(PLpgSQL_execstate *estate, PLpgSQL_stmt_forq *stmt,
44914491
* a Datum by directly calling ExecEvalExpr().
44924492
*
44934493
* If successful, store results into *result, *isNull, *rettype and return
4494-
* TRUE. If the expression is not simple (any more), return FALSE.
4494+
* TRUE. If the expression cannot be handled by simple evaluation,
4495+
* return FALSE.
4496+
*
4497+
* Because we only store one execution tree for a simple expression, we
4498+
* can't handle recursion cases. So, if we see the tree is already busy
4499+
* with an evaluation in the current xact, we just return FALSE and let the
4500+
* caller run the expression the hard way. (Other alternatives such as
4501+
* creating a new tree for a recursive call either introduce memory leaks,
4502+
* or add enough bookkeeping to be doubtful wins anyway.) Another case that
4503+
* is covered by the expr_simple_in_use test is where a previous execution
4504+
* of the tree was aborted by an error: the tree may contain bogus state
4505+
* so we dare not re-use it.
44954506
*
44964507
* It is possible though unlikely for a simple expression to become non-simple
44974508
* (consider for example redefining a trivial view). We must handle that for
@@ -4527,6 +4538,12 @@ exec_eval_simple_expr(PLpgSQL_execstate *estate,
45274538
if (expr->expr_simple_expr == NULL)
45284539
return false;
45294540

4541+
/*
4542+
* If expression is in use in current xact, don't touch it.
4543+
*/
4544+
if (expr->expr_simple_in_use && expr->expr_simple_lxid == curlxid)
4545+
return false;
4546+
45304547
/*
45314548
* Revalidate cached plan, so that we will notice if it became stale. (We
45324549
* also need to hold a refcount while using the plan.) Note that even if
@@ -4562,6 +4579,7 @@ exec_eval_simple_expr(PLpgSQL_execstate *estate,
45624579
{
45634580
expr->expr_simple_state = ExecPrepareExpr(expr->expr_simple_expr,
45644581
simple_eval_estate);
4582+
expr->expr_simple_in_use = false;
45654583
expr->expr_simple_lxid = curlxid;
45664584
}
45674585

@@ -4596,6 +4614,11 @@ exec_eval_simple_expr(PLpgSQL_execstate *estate,
45964614
paramLI = setup_param_list(estate, expr);
45974615
econtext->ecxt_param_list_info = paramLI;
45984616

4617+
/*
4618+
* Mark expression as busy for the duration of the ExecEvalExpr call.
4619+
*/
4620+
expr->expr_simple_in_use = true;
4621+
45994622
/*
46004623
* Finally we can call the executor to evaluate the expression
46014624
*/
@@ -4605,6 +4628,8 @@ exec_eval_simple_expr(PLpgSQL_execstate *estate,
46054628
NULL);
46064629

46074630
/* Assorted cleanup */
4631+
expr->expr_simple_in_use = false;
4632+
46084633
estate->cur_expr = save_cur_expr;
46094634

46104635
if (!estate->readonly_func)
@@ -5341,6 +5366,7 @@ exec_simple_check_plan(PLpgSQL_expr *expr)
53415366
*/
53425367
expr->expr_simple_expr = tle->expr;
53435368
expr->expr_simple_state = NULL;
5369+
expr->expr_simple_in_use = false;
53445370
expr->expr_simple_lxid = InvalidLocalTransactionId;
53455371
/* Also stash away the expression result type */
53465372
expr->expr_simple_type = exprType((Node *) tle->expr);

src/pl/plpgsql/src/plpgsql.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,12 @@ typedef struct PLpgSQL_expr
214214

215215
/*
216216
* if expr is simple AND prepared in current transaction,
217-
* expr_simple_state is valid. Test validity by seeing if expr_simple_lxid
218-
* matches current LXID.
217+
* expr_simple_state and expr_simple_in_use are valid. Test validity by
218+
* seeing if expr_simple_lxid matches current LXID. (If not,
219+
* expr_simple_state probably points at garbage!)
219220
*/
220-
ExprState *expr_simple_state;
221+
ExprState *expr_simple_state; /* eval tree for expr_simple_expr */
222+
bool expr_simple_in_use; /* true if eval tree is active */
221223
LocalTransactionId expr_simple_lxid;
222224
} PLpgSQL_expr;
223225

src/test/regress/expected/plpgsql.out

+47
Original file line numberDiff line numberDiff line change
@@ -3988,6 +3988,53 @@ SELECT nonsimple_expr_test();
39883988
(1 row)
39893989

39903990
DROP FUNCTION nonsimple_expr_test();
3991+
--
3992+
-- Test cases involving recursion and error recovery in simple expressions
3993+
-- (bugs in all versions before October 2010). The problems are most
3994+
-- easily exposed by mutual recursion between plpgsql and sql functions.
3995+
--
3996+
create function recurse(float8) returns float8 as
3997+
$$
3998+
begin
3999+
if ($1 < 10) then
4000+
return sql_recurse($1 + 1);
4001+
else
4002+
return $1;
4003+
end if;
4004+
end;
4005+
$$ language plpgsql;
4006+
-- "limit" is to prevent this from being inlined
4007+
create function sql_recurse(float8) returns float8 as
4008+
$$ select recurse($1) limit 1; $$ language sql;
4009+
select recurse(0);
4010+
recurse
4011+
---------
4012+
10
4013+
(1 row)
4014+
4015+
create function error1(text) returns text language sql as
4016+
$$ SELECT relname::text FROM pg_class c WHERE c.oid = $1::regclass $$;
4017+
create function error2(p_name_table text) returns text language plpgsql as $$
4018+
begin
4019+
return error1(p_name_table);
4020+
end$$;
4021+
BEGIN;
4022+
create table public.stuffs (stuff text);
4023+
SAVEPOINT a;
4024+
select error2('nonexistent.stuffs');
4025+
ERROR: schema "nonexistent" does not exist
4026+
CONTEXT: SQL function "error1" statement 1
4027+
PL/pgSQL function "error2" line 3 at RETURN
4028+
ROLLBACK TO a;
4029+
select error2('public.stuffs');
4030+
error2
4031+
--------
4032+
stuffs
4033+
(1 row)
4034+
4035+
rollback;
4036+
drop function error2(p_name_table text);
4037+
drop function error1(text);
39914038
-- Test handling of string literals.
39924039
set standard_conforming_strings = off;
39934040
create or replace function strtest() returns text as $$

src/test/regress/sql/plpgsql.sql

+42
Original file line numberDiff line numberDiff line change
@@ -3190,6 +3190,48 @@ SELECT nonsimple_expr_test();
31903190

31913191
DROP FUNCTION nonsimple_expr_test();
31923192

3193+
--
3194+
-- Test cases involving recursion and error recovery in simple expressions
3195+
-- (bugs in all versions before October 2010). The problems are most
3196+
-- easily exposed by mutual recursion between plpgsql and sql functions.
3197+
--
3198+
3199+
create function recurse(float8) returns float8 as
3200+
$$
3201+
begin
3202+
if ($1 < 10) then
3203+
return sql_recurse($1 + 1);
3204+
else
3205+
return $1;
3206+
end if;
3207+
end;
3208+
$$ language plpgsql;
3209+
3210+
-- "limit" is to prevent this from being inlined
3211+
create function sql_recurse(float8) returns float8 as
3212+
$$ select recurse($1) limit 1; $$ language sql;
3213+
3214+
select recurse(0);
3215+
3216+
create function error1(text) returns text language sql as
3217+
$$ SELECT relname::text FROM pg_class c WHERE c.oid = $1::regclass $$;
3218+
3219+
create function error2(p_name_table text) returns text language plpgsql as $$
3220+
begin
3221+
return error1(p_name_table);
3222+
end$$;
3223+
3224+
BEGIN;
3225+
create table public.stuffs (stuff text);
3226+
SAVEPOINT a;
3227+
select error2('nonexistent.stuffs');
3228+
ROLLBACK TO a;
3229+
select error2('public.stuffs');
3230+
rollback;
3231+
3232+
drop function error2(p_name_table text);
3233+
drop function error1(text);
3234+
31933235
-- Test handling of string literals.
31943236

31953237
set standard_conforming_strings = off;

0 commit comments

Comments
 (0)