Understanding AUTHID CURRENT_USER in Oracle PL/SQL – Step by Step with Examples
1. What is AUTHID in PL/SQL?
In Oracle PL/SQL, a package, procedure, or function can run under two modes:
- AUTHID DEFINER (default): Code runs with the privileges of the schema that owns the object.
- AUTHID CURRENT_USER: Code runs with the privileges of the user who is calling the object.
If you don’t explicitly mention AUTHID
, Oracle assumes AUTHID DEFINER
.
2. Why AUTHID CURRENT_USER Matters
- Useful in multi-schema applications where you want to allow different users to call the same code without creating synonyms everywhere.
- Forces Oracle to check privileges of the invoker, not just the owner.
- Can cause ORA-01031: insufficient privileges if the calling user does not have required grants.
3. Example 1 – Default Behavior (AUTHID DEFINER)
- emp_pkg runs as HR (the owner).
- If another user APPUSER executes emp_pkg.get_emp, they do not need direct SELECT on employees table.
- Because privileges of HR are used.
4. Example 2 – AUTHID CURRENT_USER
- Now, when APPUSER calls emp_pkg.get_emp, the query runs with APPUSER’s privileges.
- If APPUSER does not have SELECT on HR.employees, they get ORA-01031 insufficient privileges.
- To fix, grant the privilege:
5. Example 3 – With Synonym
But with AUTHID CURRENT_USER
, the privileges still must exist for APPUSER
. The synonym only simplifies object reference; it does not provide privileges.
6. Common Issues
- Why does it work in one DB and not another?
In another DB, grants are missing.
- Why synonyms don’t solve it?
- Why does Oracle sometimes add AUTHID CURRENT_USER?
7. Key Takeaways
- AUTHID DEFINER (default) = runs as owner.
- AUTHID CURRENT_USER = runs as caller (invoker).
- Synonyms help with object names, not privileges.
- Always check which user needs which grant when AUTHID CURRENT_USER is used.
Post a Comment
Post a Comment