Sub topic 10
Reading Compilation Errors and Understanding Invalidation
This chapter assumes you can write a basic stored procedure or function with CREATE OR REPLACE, and that you can run SQL in SQL*Plus, SQL Developer, or a similar tool. You will learn how to find out why a subprogram did not compile. You will also learn how Oracle tracks which objects depend on which, and why a working subprogram can suddenly become INVALID.
What happens when a subprogram fails to compile
When you run CREATE OR REPLACE PROCEDURE, Oracle parses the code and stores it in the data dictionary. If the code has a syntax error or refers to something that does not exist, Oracle still stores the subprogram. It marks the subprogram INVALID and reports this message:
Warning: Procedure created with compilation errors.This differs from a plain SQL statement, which fails and creates nothing. A stored subprogram with errors exists, but it cannot run. Calling it raises ORA-06550 or, if it was invalidated earlier, Oracle tries to recompile it first.
Because CREATE OR REPLACE overwrites the old version, a failed replace also replaces a working version with a broken one. Keep your source in version control and do not rely on the database as the only copy.
Seeing the errors
The message "created with compilation errors" does not tell you what is wrong. There are two ways to see the details.
In SQL*Plus and SQL Developer's script runner, type SHOW ERRORS right after the failed statement. It shows the errors for the last object you compiled.
In any tool, query the USER_ERRORS view. It holds the errors for every object in your schema. Use ALL_ERRORS to see objects you can access in other schemas, and DBA_ERRORS if you have DBA access.
SELECT line, position, text
FROM user_errors
WHERE name = 'RAISE_SALARY'
AND type = 'PROCEDURE'
ORDER BY sequence;The main columns are:
| Column | Meaning |
|---|---|
| `NAME` | Name of the object, in uppercase unless you quoted it |
| `TYPE` | `PROCEDURE`, `FUNCTION`, `PACKAGE`, `PACKAGE BODY`, `TRIGGER`, and so on |
| `SEQUENCE` | Order in which the errors were reported |
| `LINE` | Line number in the source |
| `POSITION` | Column number on that line |
| `TEXT` | The error message |
| `ATTRIBUTE` | `ERROR` or `WARNING` |
The line number counts from the first line of the CREATE statement, so you can match it to your script.
A worked example
This procedure has a typo in the column name:
CREATE OR REPLACE PROCEDURE raise_salary (
p_employee_id IN employees.employee_id%TYPE,
p_percent IN NUMBER DEFAULT 10
) AS
BEGIN
UPDATE employees
SET salary = salary * (1 + p_percent / 100)
WHERE employe_id = p_employee_id;
END raise_salary;
/SHOW ERRORS reports something like this:
LINE/COL ERROR
-------- -----------------------------------------------------------------
6/3 PL/SQL: SQL Statement ignored
8/10 PL/SQL: ORA-00904: "EMPLOYE_ID": invalid identifierTwo things to notice. First, Oracle reports several messages for one problem. The first, "SQL Statement ignored", is a consequence. The second one holds the real cause. Read errors from the most specific message, which is usually the last one for a given statement. Second, the position points at the identifier that failed, not at the start of the statement.
Common errors
| Message | Typical cause |
|---|---|
| `PLS-00201: identifier must be declared` | Misspelled name, or an object you have no privilege on |
| `PLS-00103: Encountered the symbol ...` | Syntax error such as a missing `;` or `THEN` |
| `PLS-00306: wrong number or types of arguments in call` | A call does not match any declared signature |
| `PLS-00905: object is invalid` | A called subprogram is itself invalid |
| `PLS-00363: expression cannot be used as an assignment target` | Assigning to an `IN` parameter |
| `PLS-00382: expression is of wrong type` | Type mismatch in an expression |
| `ORA-00942: table or view does not exist` | Missing table, or the privilege came only through a role |
The last row is a frequent surprise. Inside a definer's rights subprogram, privileges granted through a role do not count at compile time. You need a direct grant on the table. This is covered in the chapter on privileges and rights.
Compiler warnings
Some problems are legal but suspicious, for example a variable that is never used or code that can never run. Oracle reports these as warnings only if you turn them on:
ALTER SESSION SET PLSQL_WARNINGS = 'ENABLE:ALL';Recompile the subprogram afterwards, then query USER_ERRORS. Rows with ATTRIBUTE = 'WARNING' show the warnings, with codes such as PLW-06002: Unreachable code. A warning does not mark the subprogram invalid. Many teams enable warnings in development and treat selected ones as errors with 'ERROR:ALL' or a list of codes.
Object status and the data dictionary
Every stored object has a status in USER_OBJECTS:
SELECT object_name, object_type, status, last_ddl_time
FROM user_objects
WHERE object_type IN ('PROCEDURE', 'FUNCTION', 'PACKAGE', 'PACKAGE BODY')
AND status = 'INVALID'
ORDER BY object_type, object_name;STATUS is VALID or INVALID. LAST_DDL_TIME tells you when the object was last changed or recompiled, which helps when you ask what changed before something broke.
How dependencies work
A stored subprogram depends on every object it references: tables, views, other procedures and functions, packages, types, and synonyms. Oracle records these links when it compiles the subprogram. You can read them from USER_DEPENDENCIES:
SELECT name, type, referenced_name, referenced_type
FROM user_dependencies
WHERE name = 'RAISE_SALARY';To find what would be affected if you changed EMPLOYEES, reverse the search:
SELECT name, type
FROM user_dependencies
WHERE referenced_name = 'EMPLOYEES'
AND referenced_type = 'TABLE';Dependencies are transitive. If procedure A calls function B, and B reads table T, then A depends on B directly and on T indirectly.
What invalidates a subprogram
Oracle marks a dependent object INVALID when something it depends on changes in a way that might break it. Common triggers:
ALTER TABLEthat changes a column the subprogram uses, or other DDL on the referenced table.CREATE OR REPLACEon a called subprogram, which also invalidates its callers (see the fine-grained rule below).- Dropping a referenced object. The dependent becomes invalid and stays invalid until the object is recreated.
- Revoking a privilege that the subprogram needed.
Invalidation spreads. When B becomes invalid, every object that depends on B is marked invalid as well.
Fine-grained dependency tracking
Since Oracle 11g, Oracle tracks dependencies at a finer level than whole objects. If you add a new column to a table, procedures that do not use that column usually stay valid. If you add a new overload to a package specification, callers of the other overloads are generally unaffected. Oracle still invalidates the objects when it cannot prove the change is safe. Do not count on a particular DDL leaving everything valid. Test it.
Automatic recompilation
An invalid subprogram is not permanently broken. The next time you call it, Oracle tries to recompile it. If the recompile succeeds, the call runs normally and the status returns to VALID. If it fails, the call raises an error such as ORA-06575: Package or function X is in invalid state.
Automatic recompilation has two costs:
- The first caller after a change pays the compile time.
- In a package with state, invalidation discards the package's session state. A session that held values in package variables gets
ORA-04068: existing state of packages has been discarded. Retrying the call usually works, because the package then reinitializes.
For these reasons, production deployments normally recompile everything during the release instead of waiting for users to trigger it.
Recompiling on purpose
To compile one object by hand:
ALTER PROCEDURE raise_salary COMPILE;
ALTER FUNCTION get_full_name COMPILE;
ALTER PACKAGE fmt COMPILE;
ALTER PACKAGE fmt COMPILE BODY;ALTER PACKAGE ... COMPILE compiles both the specification and the body. COMPILE BODY compiles only the body, which does not invalidate the callers of the package.
To compile every invalid object in a schema, use DBMS_UTILITY.COMPILE_SCHEMA:
BEGIN
DBMS_UTILITY.COMPILE_SCHEMA(
schema => USER,
compile_all => FALSE
);
END;
/With compile_all => FALSE, only invalid objects are compiled. With TRUE, everything is recompiled, which is slower and invalidates more than needed. Another option is UTL_RECOMP.RECOMP_PARALLEL for large schemas, which needs additional privileges. Oracle also ships the utlrp.sql script for recompiling invalid objects database-wide.
After recompiling, check USER_OBJECTS again. Objects that are still invalid have real errors, and USER_ERRORS will show them.
Working practices
- Run
SHOW ERRORSor queryUSER_ERRORSevery time a deployment prints "created with compilation errors". Do not assume the next script fixed it. - Compile dependencies in an order that works: tables, then types and package specifications, then package bodies, then standalone subprograms and triggers. Circular dependencies between standalone subprograms can compile in either order after a second pass. Package bodies avoid many of these problems because callers depend only on the specification.
- Keep package specifications stable. Changing a body does not invalidate callers. Changing a specification does.
- After any DDL on a table, query
USER_OBJECTSfor invalid objects before you release the change.
Exercise
You run ALTER TABLE employees ADD (middle_name VARCHAR2(30)). Afterwards, SELECT object_name FROM user_objects WHERE status = 'INVALID' returns nothing, although several procedures read EMPLOYEES. Is this a mistake? Then say how you would list every object that could be affected by a change to EMPLOYEES.
Show answer
It is not a mistake. Fine-grained dependency tracking (Oracle 11g and later) lets Oracle keep dependents valid when a change, such as adding a column they do not use, cannot affect them. To list the potentially affected objects, query USERDEPENDENCIES with referencedname = 'EMPLOYEES' and referenced_type = 'TABLE'. Objects that depend on EMPLOYEES only indirectly, through another subprogram, need the search repeated on the names found, or a hierarchical query.
Exercise
A deployment script prints "Warning: Function created with compilation errors." for GETFULLNAME. Write the query that shows the cause, and state which row you should read first for a failing SQL statement.
Show answer
SELECT line, position, text FROM usererrors WHERE name = 'GETFULL_NAME' AND type = 'FUNCTION' ORDER BY sequence; Messages such as "SQL Statement ignored" are consequences. Read the more specific message that follows it, for example the ORA-00904 or PLS-00201 text, because that names the real cause.