pg_hint_plan
pg_hint_plan copied to clipboard
Invalid plpgsql_recurse_level after catch any exception
Hi. We have an issues related to the invalid value of plpgsql_recurse_level counter in the PG13 and current master branch (Postgres 14).
The issue is that when we catch and handle any error inside any function/procedure then plpgsql_recurse_level counter becomes invalid because pg_hint_plan_plpgsql_stmt_end is not called. Therefore after catch any error until the end of the transaction we have the following issues:
PG13: pg_hint_plan does not process new hints for new requests PG14 (master): pg_hint_plan applies old hints from old queries to the new queries.
Cases to reproduce
For PG13 branch + Postgresql 13
-- PG13
begin;
create or replace function pg_temp.func_with_exception(p_var varchar) returns numeric language plpgsql as
$$
begin
return p_var::numeric;
exception when others then null;
return -1;
end;
$$;
select pg_temp.func_with_exception('abc');
explain (COSTS false) /*+MergeJoin(d1 d2) */ with dual as (select 'x' as dummy) select * from dual d1, dual d2 where d1.dummy = d2.dummy;
-- Hint will be ignored
drop function pg_temp.func_with_exception(varchar);
rollback;
For master branch + Postgresql 14
-- PG14 case
begin;
select set_config('compute_query_id','off', true);
create or replace function pg_temp.test_bug_hint(p_var varchar) returns numeric language plpgsql as
$$
declare
begin
return p_var::numeric;
exception when others then return 1;
end;
$$;
select pg_temp.test_bug_hint('x');
-- Counter of nested level was changed to 1 due to invalid tracking during exception
explain with test /*+ MergeJoin(t1 t2) */ as (select 'x' val) select t1.val from test t1, test t2 where t1.val = t2.val;
explain with test2 as (select 'y' val) select t1.val from test t1, test t2 where t1.val <> t2.val;
-- MergeJoin will be applied to the both queries because current_hint_str wasn't cleanup due to the current nested level logic
rollback;
Dear @horiguti can you review the issue please Questions are welcome
Related pull request: https://github.com/ossc-db/pg_hint_plan/pull/100
This has been fixed as of a9863af.