|Oracle® Database Administrator's Guide
11g Release 2 (11.2)
Part Number E17120-11
|PDF · Mobi · ePub|
Some types of schema objects reference other objects. For example, a view contains a query that references tables or other views, and a PL/SQL subprogram might invoke other subprograms and might use static SQL to reference tables or views. An object that references another object is called a dependent object, and an object being referenced is a referenced object. These references are established at compile time, and if the compiler cannot resolve them, the dependent object being compiled is marked invalid.
Oracle Database provides an automatic mechanism to ensure that a dependent object is always up to date with respect to its referenced objects. When a dependent object is created, the database tracks dependencies between the dependent object and its referenced objects. When a referenced object is changed in a way that might affect a dependent object, the dependent object is marked invalid. An invalid dependent object must be recompiled against the new definition of a referenced object before the dependent object can be used. Recompilation occurs automatically when the invalid dependent object is referenced.
It is important to be aware of changes that can invalidate schema objects, because invalidation affects applications running on the database. This section describes how objects become invalid, how you can identify invalid objects, and how you can validate invalid objects.
In a typical running application, you would not expect to see views or stored procedures become invalid, because applications typically do not change table structures or change view or stored procedure definitions during normal execution. Changes to tables, views, or PL/SQL units typically occur when an application is patched or upgraded using a patch script or ad-hoc DDL statements. Dependent objects might be left invalid after a patch has been applied to change a set of referenced objects.
Use the following query to display the set of invalid objects in the database:
SELECT object_name, object_type FROM dba_objects WHERE status = 'INVALID';
The Database Home page in Enterprise Manager displays an alert when schema objects become invalid.
Object invalidation affects applications in two ways. First, an invalid object must be revalidated before it can be used by an application. Revalidation adds latency to application execution. If the number of invalid objects is large, the added latency on the first execution can be significant. Second, invalidation of a procedure, function or package can cause exceptions in other sessions concurrently executing the procedure, function or package. If a patch is applied when the application is in use in a different session, the session executing the application notices that an object in use has been invalidated and raises one of the following 4 exceptions: ORA-04061, ORA-04064, ORA-04065 or ORA-04068. These exceptions must be remedied by restarting application sessions following a patch.
You can force the database to recompile a schema object using the appropriate SQL statement with the
COMPILE clause. See "Manually Recompiling Invalid Objects with DDL" for more information.
If you know that there are a large number of invalid objects, use the
UTL_RECOMP PL/SQL package to perform a mass recompilation. See "Manually Recompiling Invalid Objects with PL/SQL Package Procedures" for details.
The following are some general rules for the invalidation of schema objects:
Between a referenced object and each of its dependent objects, the database tracks the elements of the referenced object that are involved in the dependency. For example, if a single-table view selects only a subset of columns in a table, only those columns are involved in the dependency. For each dependent of an object, if a change is made to the definition of any element involved in the dependency (including dropping the element), the dependent object is invalidated. Conversely, if changes are made only to definitions of elements that are not involved in the dependency, the dependent object remains valid.
In many cases, therefore, developers can avoid invalidation of dependent objects and unnecessary extra work for the database if they exercise care when changing schema objects.
Dependent objects are cascade invalidated. If any object becomes invalid for any reason, all of that object's dependent objects are immediately invalidated.
If you revoke any object privileges on a schema object, dependent objects are cascade invalidated.
See Also:Oracle Database Concepts for more detailed information about schema object dependencies
You can use an
ALTER statement to manually recompile a single schema object. For example, to recompile package body
Pkg1, you would execute the following DDL statement:
ALTER PACKAGE pkg1 COMPILE REUSE SETTINGS;
See Also:Oracle Database SQL Language Reference for syntax and other information about the various
Following an application upgrade or patch, it is good practice to revalidate invalid objects to avoid application latencies that result from on-demand object revalidation. Oracle provides the
UTL_RECOMP package to assist in object revalidation. The
RECOMP_SERIAL procedure recompiles all invalid objects in a specified schema, or all invalid objects in the database if you do not supply the schema name argument. The
RECOMP_PARALLEL procedure does the same, but in parallel, employing multiple CPUs.
Execute the following PL/SQL block to revalidate all invalid objects in the database, in parallel and in dependency order:
begin utl_recomp.recomp_parallel(); end; /
You can also revalidate individual invalid objects using the package
DBMS_UTILITY. The following PL/SQL block revalidates the procedure
UPDATE_SALARY in schema
begin dbms_utility.validate('HR', 'UPDATE_SALARY', namespace=>1); end; /
The following PL/SQL block revalidates the package body
begin dbms_utility.validate('HR', 'ACCT_MGMT', namespace=>2); end; /
See Also:Oracle Database PL/SQL Packages and Types Reference for more information on the