Sub topic 10
Dependencies: Why the Body Can Change Without Breaking Callers
Assume you know basic SQL, PL/SQL variables and control flow, standalone procedures and functions, and basic exception handling. This chapter focuses on how Oracle tracks dependencies between a package and the objects that use it.
The specification is the dependency boundary
A package has two parts:
- The specification declares the public interface.
- The body implements that interface and can contain private code.
A caller depends on the declarations in the specification. The caller does not depend directly on the statements inside the body. This separation lets you change an implementation without changing every caller.
For example, create a package with one public function:
CREATE OR REPLACE PACKAGE tax_api AS
FUNCTION calculate_tax(p_amount IN NUMBER)
RETURN NUMBER;
END tax_api;
/
CREATE OR REPLACE PACKAGE BODY tax_api AS
FUNCTION calculate_tax(p_amount IN NUMBER)
RETURN NUMBER IS
BEGIN
RETURN p_amount * 0.10;
END calculate_tax;
END tax_api;
/
CREATE OR REPLACE PROCEDURE show_tax(p_amount IN NUMBER) IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Tax: ' || tax_api.calculate_tax(p_amount));
END show_tax;
/show_tax depends on the public function signature. It does not need to know whether the function uses a literal rate, a table lookup, or a private helper.
Changing the body
You can change the tax calculation while keeping the specification unchanged:
CREATE OR REPLACE PACKAGE BODY tax_api AS
FUNCTION calculate_tax(p_amount IN NUMBER)
RETURN NUMBER IS
l_rate NUMBER := 0.12;
BEGIN
RETURN p_amount * l_rate;
END calculate_tax;
END tax_api;
/The function now uses a local variable and a different rate. The public name, parameter mode, parameter type, and return type are unchanged. Existing callers can continue to call tax_api.calculate_tax.
Oracle recompiles the package body. A caller such as show_tax normally remains valid because its dependency is the specification. This is the main benefit of the package boundary.
Changing the specification
A specification change can affect callers. For example, changing the function's return type or parameter list changes the interface that callers compiled against. Oracle may mark dependent objects invalid.
You can inspect object status with the data dictionary. A data dictionary view is an Oracle view that describes database objects and their metadata.
SELECT object_name, object_type, status
FROM user_objects
WHERE object_name IN ('TAX_API', 'SHOW_TAX');An object with status INVALID must be compiled again before it can run successfully. Recompile a procedure with:
ALTER PROCEDURE show_tax COMPILE;If compilation fails, inspect the errors:
SELECT line, position, text
FROM user_errors
WHERE name = 'SHOW_TAX'
ORDER BY sequence;A specification change can also fail immediately if the body no longer matches the new declarations. For example, adding a public procedure to the specification requires the body to implement that procedure.
Dependency flow
The package body itself can depend on tables, views, types, and other program units. If one of those dependencies changes, Oracle may invalidate the body even when the package specification is unchanged. The body must then be recompiled successfully.
Practical design rules
- Put only stable, necessary declarations in the specification.
- Keep helper functions and internal variables in the body.
- Change the body for implementation improvements that preserve the interface.
- Treat specification changes as compatibility changes.
- After deployment, check
USER_OBJECTSandUSER_ERRORS. - Compile and test dependent objects after changing public declarations.
Exercise
You add a private helper function to a package body but do not change the specification. Should a procedure that calls an existing public function normally need a source-code change?
Show answer
No. The caller depends on the unchanged public specification. Oracle recompiles the body, while the caller can continue using the same public function interface.