Sub topic 11
Granting EXECUTE and Choosing AUTHID
This chapter covers who can run a stored subprogram and whose privileges apply while it runs. It assumes you can create procedures and functions, call them from blocks, and read basic dictionary views. You also need a schema with CREATE PROCEDURE and a second test user for the grant examples.
The EXECUTE privilege
A stored subprogram belongs to the schema that created it. Other users cannot call it until the owner, or a user with the right administrative privilege, grants them EXECUTE on it.
GRANT EXECUTE ON raise_salary TO app_user;After the grant, app_user calls the procedure with its schema prefix, unless a synonym exists:
BEGIN
hr.raise_salary(p_employee_id => 100, p_percent => 5);
END;
/You can grant EXECUTE to a user, to a role, or to PUBLIC. Granting to a role is easier to maintain, because you add and remove people from the role instead of changing grants on every object.
CREATE ROLE payroll_runner;
GRANT EXECUTE ON raise_salary TO payroll_runner;
GRANT payroll_runner TO app_user;Add WITH GRANT OPTION only if the grantee must pass the privilege on. Use it rarely, because it spreads control beyond the owner.
Revoke the privilege with REVOKE EXECUTE ON raise_salary FROM app_user;.
Granting on a package
For a package, you grant EXECUTE on the whole package, not on individual members. Everything declared in the package specification becomes callable by the grantee. Anything that exists only in the package body stays private.
This makes packages a good way to define a public interface. Expose a few entry points in the specification, keep helpers in the body, and grant EXECUTE on the package only.
A synonym for convenience
A grant does not change how the caller names the object. A synonym removes the schema prefix:
CREATE PUBLIC SYNONYM raise_salary FOR hr.raise_salary;A synonym gives no access. The caller still needs EXECUTE.
Two questions, not one
Two separate questions decide whether a call works:
- Can the caller run the subprogram? That is the EXECUTE privilege.
- Once it runs, whose privileges apply to the tables and other objects the code touches? That is the AUTHID clause.
The first answer controls entry. The second controls what happens inside.
Definer's rights
By default, a subprogram uses definer's rights. The code runs with the privileges of the schema that owns it, not those of the caller. This is equivalent to writing AUTHID DEFINER.
Definer's rights has these consequences:
- Unqualified names such as
employeesresolve in the owner's schema. - Table privileges come from the owner. Privileges granted to the owner through a role are ignored in named PL/SQL. They must be granted directly.
- The caller needs only EXECUTE. They need no access to the underlying tables.
The last point is the main use of definer's rights. You can let app_user raise salaries through raise_salary while denying direct UPDATE on employees. The procedure is the only controlled path to the data.
-- Run as the owner (HR)
REVOKE UPDATE ON employees FROM app_user;
GRANT EXECUTE ON raise_salary TO app_user;Now app_user can run hr.raise_salary, but an UPDATE hr.employees ... statement from that user fails with ORA-01031 or ORA-00942.
The role gotcha
Suppose the owner has UPDATE on a table only because of a role. The procedure compiles with errors such as PL/SQL: ORA-00942: table or view does not exist, even though the same UPDATE works in SQL*Plus. Roles are disabled inside definer's-rights code. Fix it with a direct grant from the table's owner:
GRANT UPDATE ON hr.employees TO report_owner;Invoker's rights
With AUTHID CURRENT_USER, the subprogram runs with the privileges of the user who calls it. Unqualified object names resolve in the caller's schema, and the caller's enabled roles apply.
The tax_rate_for function from the topic examples uses this clause:
CREATE OR REPLACE FUNCTION tax_rate_for (
p_country_code IN VARCHAR2
) RETURN NUMBER
AUTHID CURRENT_USER
DETERMINISTIC
IS
BEGIN
RETURN CASE p_country_code
WHEN 'DE' THEN 0.19
WHEN 'FR' THEN 0.20
ELSE 0
END;
END tax_rate_for;
/
GRANT EXECUTE ON tax_rate_for TO app_user;This function reads no tables, so AUTHID has no visible effect here. It becomes important when the body queries tables that exist in several schemas.
Consider a utility that counts rows in a table named orders:
CREATE OR REPLACE FUNCTION count_orders RETURN NUMBER
AUTHID CURRENT_USER
IS
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count FROM orders;
RETURN v_count;
END count_orders;
/If sales_east and sales_west each own an orders table and both users have EXECUTE on this function, each user counts the rows in their own table. One piece of code serves many schemas.
What invoker's rights still checks
At compile time, the owner needs enough access to compile the code. The privileges that matter at run time are those of the caller. If a caller lacks access to the table, the call fails with ORA-00942 or ORA-01031, even though they hold EXECUTE.
Comparing the two
| Aspect | Definer's rights (`AUTHID DEFINER`) | Invoker's rights (`AUTHID CURRENT_USER`) |
|---|---|---|
| Default | Yes | No |
| Privileges used | Owner's direct grants | Caller's grants and enabled roles |
| Name resolution | Owner's schema | Caller's schema |
| Caller needs table access | No | Yes |
| Typical use | Controlled access to data, application APIs | Shared utilities that work on the caller's own data |
Choosing between them
Start from the default. Use definer's rights when you want to hide tables behind a narrow interface. Use invoker's rights when the same logic must run against data owned by whoever calls it, such as generic maintenance or reporting utilities.
Invoker's rights has a cost. Because the caller's privileges and schema decide what the code touches, the behavior changes with the caller. You must test it as each kind of user, and a missing grant shows up as a run-time failure, not a compile-time one.
Definer's rights has its own risk. Anything the owner can do, the procedure can do for any caller with EXECUTE. Keep such code small, validate its input, and avoid dynamic SQL built from caller-supplied text.
Checking grants and settings
The dictionary shows who holds EXECUTE:
SELECT grantee, privilege, grantable
FROM user_tab_privs_made
WHERE table_name = 'RAISE_SALARY';The AUTHID setting is stored in USER_PROCEDURES:
SELECT object_name, authid
FROM user_procedures
WHERE object_name IN ('RAISE_SALARY', 'TAX_RATE_FOR', 'COUNT_ORDERS');The authid column shows DEFINER or CURRENT_USER.
Common errors
- ORA-00942 on a call that worked in SQL*Plus. The owner's access comes from a role and the code uses definer's rights. Grant the privilege directly.
- ORA-06550 with PLS-00201. The caller used a name that is not visible, or lacks EXECUTE. Check the grant and the schema prefix or synonym.
- The wrong table is read. An invoker's-rights routine resolved an unqualified name in the caller's schema. Qualify the name if you always want one table.
- A grant is lost after a drop.
DROPremoves the object and its grants.CREATE OR REPLACEkeeps them, which is one reason to prefer it for updates.
Exercise
The procedure hr.raise_salary uses the default AUTHID. The user app_user has EXECUTE on it but has no privileges on hr.employees. Does the call succeed? Now suppose the procedure is changed to AUTHID CURRENT_USER. What happens, and what grant would fix it?
Show answer
With definer's rights, the call succeeds, because the UPDATE runs with the owner's privileges and appuser needs only EXECUTE. With AUTHID CURRENTUSER, the UPDATE runs with appuser's privileges, so the call fails with ORA-00942 or ORA-01031. Granting UPDATE (and SELECT if the code reads) on hr.employees to appuser, directly or through an enabled role, fixes it.
Exercise
A definer's-rights procedure in schema REPORTS fails to compile with ORA-00942 on a table in schema SALES. A DBA says REPORTS can query that table in SQL*Plus. What is the likely cause, and how do you fix it?
Show answer
REPORTS has SELECT on the table through a role. Roles are not active inside definer's-rights code, so the compiler cannot see the table. Grant SELECT on the table directly to REPORTS, then recompile.