Hi all,
one of my colleagues has found a little bit weird behavior of a query result cache. He has set result_cache_mode = 'FORCE' (but it can be reproduced with a result_cache hint too) and suddenly functions called from the query get executed twice (for the first time) .
An easy example:
alter session set result_cache_mode = 'FORCE';
create sequence test_seq;
create or replace function test_f(i number)
return number
is
begin
dbms_output.put_line('TEST_F executed');
--autonomous transaction or package variable can be used too
return test_seq.nextval;
end;
/
prompt First call
select test_f(1) from dual;
prompt Second call
select test_f(1) from dual;
drop sequence test_seq;
drop function test_f;
First call
TEST_F(1)
----------
2
TEST_F executed
TEST_F executed
Second call
TEST_F(1)
----------
1
As you can see - for the first time the function is executed twice and return the value from the second execution. When I execute the query again it returns the value from the first execution... but it doesn't matter, problem is in the double execution. Our developers used to send emails via select (it's easier for them):
select send_mail(...) from dual;
... and now the customers complains that they get emails twice
And now the question - is there any way, hot to get rid of this behavior (without changing the parameter back or rewriting code)? I thought that the result cache is automatically disabled for non-deterministic functions...or is this an expected behavior?
Thanks,
Ivan