However, in an authid current_user procedure (or package), it is.
This is demonstrated in the following research.
Creating two users/schemas
We need two schemas, one for the packages and one that executes the stored procuders.
create user pkg_owner identified by pw default tablespace data quota unlimited on data;
create user pkg_user identified by pw default tablespace data quota unlimited on data;
grant create session, create procedure to pkg_owner;
grant create session to pkg_user;
The two packages. One is defined with authid definer the other with authid current_user. Both have exactly one procedure: do_something.
In both cases, do_something uses execute immediate to
print the «actual» current schema,
change the current schema and
print the «actual» current schema again
After executing execute immediate, the procedure prints the «actual» current schema yet again.
create package PKG_DEFINER AUTHID DEFINER
as
procedure do_something;
end;
/
create package PKG_CURRENT_USER AUTHID CURRENT_USER
as
procedure do_something;
end;
/
create package body PKG_DEFINER
as
procedure do_something as
begin
dbms_output.put_line('pkg_definer');
execute immediate q'{
begin
dbms_output.put_line(' exec immediate: current schema before setting current_schema: ' || sys_context('userenv', 'current_schema'));
execute immediate 'alter session set current_schema = pkg_user';
dbms_output.put_line(' exec immediate: current schema after setting current_schema: ' || sys_context('userenv', 'current_schema'));
end;
}';
--
-- Turns out, the alter session set current_schema in the
-- previous execute immediate affects only the statement being
-- executed in the execute statement. It does not change
-- the enclosing scope.
--
dbms_output.put_line(' current schema after execute immediate: ' || sys_context('userenv', 'current_schema'));
end do_something;
end pkg_definer;
/
create package body PKG_CURRENT_USER
as
procedure do_something as
begin
dbms_output.put_line('pkg_current_user');
execute immediate q'{
begin
dbms_output.put_line(' exec immediate: current schema before setting current_schema: ' || sys_context('userenv', 'current_schema'));
execute immediate 'alter session set current_schema = pkg_owner';
dbms_output.put_line(' exec immediate: current schema after setting current_schema: ' || sys_context('userenv', 'current_schema'));
end;
}';
--
-- Turns out, the alter session set current_schema in the
-- previous execute immediate affects affects also the scope
-- of the enclosing scope
--
dbms_output.put_line(' current schema after execute immediate: ' || sys_context('userenv', 'current_schema'));
end do_something;
end pkg_current_user;
/
grant execute on pkg_definer to pkg_user;
grant execute on pkg_current_user to pkg_user;
kg_definer
exec immediate: current schema before setting current_schema: PKG_OWNER
exec immediate: current schema after setting current_schema: PKG_USER
current schema after execute immediate: PKG_OWNER
pkg_current_user
exec immediate: current schema before setting current_schema: PKG_USER
exec immediate: current schema after setting current_schema: PKG_OWNER
current schema after execute immediate: PKG_OWNER