Sub topic 13
Designing PL/SQL Packages Well
Assume you know basic SQL, PL/SQL variables and control flow, standalone subprograms, and exception handling. This chapter focuses on design choices that keep packages understandable, safe to change, and easy to use.
A package should represent one cohesive area of responsibility. For example, an employee_api package can expose employee operations, while an unrelated reporting calculation belongs elsewhere. A package that contains unrelated utilities becomes a second “miscellaneous” folder that is difficult to test and maintain.
Start with a narrow public interface
The package specification is a contract with its callers. Put an item in the specification only when callers need to use it. Keep validation helpers, SQL details, temporary variables, and implementation choices in the body.
CREATE OR REPLACE PACKAGE invoice_rules AS
c_max_discount_pct CONSTANT NUMBER := 25;
e_invalid_discount EXCEPTION;
PRAGMA EXCEPTION_INIT(e_invalid_discount, -20001);
FUNCTION discount_amount(
p_subtotal IN NUMBER,
p_discount_pct IN NUMBER
) RETURN NUMBER;
END invoice_rules;
/
CREATE OR REPLACE PACKAGE BODY invoice_rules AS
FUNCTION valid_discount(p_discount_pct IN NUMBER) RETURN BOOLEAN IS
BEGIN
RETURN p_discount_pct BETWEEN 0 AND c_max_discount_pct;
END valid_discount;
FUNCTION discount_amount(
p_subtotal IN NUMBER,
p_discount_pct IN NUMBER
) RETURN NUMBER IS
BEGIN
IF p_subtotal < 0 OR NOT valid_discount(p_discount_pct) THEN
RAISE e_invalid_discount;
END IF;
RETURN p_subtotal * p_discount_pct / 100;
END discount_amount;
END invoice_rules;
/Callers can use invoice_rules.discount_amount and the documented constant and exception. They cannot call valid_discount. This reduces coupling, which means callers depend on fewer implementation details.
Choose names and parameter contracts carefully
Use names that explain intent. Prefix parameters consistently, such as p_, and local variables with l_. Use %TYPE when a parameter or variable represents a table column. This keeps declarations aligned when the column datatype changes.
For public parameters, specify modes explicitly. Use IN for inputs. Use OUT or IN OUT only when returning several values through one procedure is clearer than returning a record or using a function.
| Design choice | Prefer | Avoid |
|---|---|---|
| Public interface | Small, cohesive set of operations | Exposing every helper |
| Errors | Named application exceptions | Undocumented numeric errors |
| Transactions | Let the caller commit or roll back | Hidden commits in reusable code |
| State | Local variables or explicit inputs | Unnecessary package globals |
| Dependencies | Stable tables and types | Exposing implementation details |
Define error and transaction behavior
Document what each public subprogram does when input is invalid, a row is missing, or more than one row matches. Named exceptions make these outcomes easier for callers to handle.
A reusable package usually should not issue COMMIT or ROLLBACK. The caller may need to combine several package calls into one transaction. A hidden commit can make that impossible. If a package must control a transaction for a specific reason, make that behavior explicit in its contract.
Use RAISE_APPLICATION_ERROR when a named exception is not needed, or declare public exceptions when callers should handle a particular condition.
Control state and security deliberately
Package variables persist for the lifetime of a database session. This can be useful for a session cache or configuration, but it can also preserve stale or user-specific data. Prefer passing values as parameters unless session state is part of the design.
Choose rights behavior intentionally. A definer-rights package runs SQL with the package owner's privileges by default. AUTHID CURRENT_USER creates an invoker-rights package, so SQL uses the calling user's privileges and name resolution. Use invoker rights when the caller's permissions should control access. Otherwise, document why definer rights are appropriate.
Consider dependencies and maintenance
A package body can change without changing the public contract. This lets you improve SQL or private algorithms while callers continue to compile against the same specification. Changing the specification can invalidate dependent views, packages, procedures, and functions, so make interface changes deliberately.
Keep each package easy to test. Public operations should have predictable inputs and outputs. Avoid relying on initialization order, hidden global state, or the current session user unless those behaviors are required and documented. Use PRAGMA SERIALLY_REUSABLE only when package state should be discarded after a server call or request. It is not a general solution for poorly controlled state.
Design packages around business capabilities, expose the smallest useful interface, and make side effects visible in the contract.
Exercise
A package body contains a private helper and a public procedure that performs an update. Where should the helper be declared, and who should control the transaction?
Show answer
Declare the helper only in the package body. The caller should normally control COMMIT and ROLLBACK so several operations can share one transaction.