PL/SQL & T-SQL (Oracle and SQL Server) Module 11 – Subprograms, packages and robust error logging
Dependencies, compilation and warnings
Reading object dependencies and status in the data dictionary, predicting which DDL changes invalidate dependent PL/SQL, and compiler warnings.
What you will learn
- Read dependencies and object status in USER_DEPENDENCIES and USER_OBJECTS
- Predict which dependents an ALTER TABLE ... ADD column invalidates
- Choose how to recompile a schema after a deployment and find what failed
- Use PLSQL_WARNINGS and conditional compilation to catch problems at compile time
- Explain how editions let old and new code run side by side
Before you start
What you’ll learn · Before you start
What you will learn
- Read dependencies and object status in USER_DEPENDENCIES and USER_OBJECTS
- Predict which dependents an ALTER TABLE ... ADD column invalidates
- Choose how to recompile a schema after a deployment and find what failed
- Use PLSQL_WARNINGS and conditional compilation to catch problems at compile time
- Explain how editions let old and new code run side by side
Before you start
On this page
A release adds one nullable column to a table that has five direct dependents, and Oracle recompiles exactly two of the six objects around it. Which two depends on how each unit uses the table, not on how big the change is. Predicting that list is what turns a quiet column addition from a surprise into a plan.
This lesson shows how Oracle records dependencies and status, why a stale unit is not a broken one, and how compiler warnings catch risky code before it runs. Two deeper parts then apply the documented rules to a real change, recompile a schema after a deployment, and cover conditional compilation, optimisation and editions.
The dictionary lists what depends on what and whether it is current
A dependency is a reference from one object to another: a procedure that selects from a table depends on that table. Oracle records every such reference and gives every object a status. The views USER_DEPENDENCIES, ALL_DEPENDENCIES and DBA_DEPENDENCIES list each object dependency: A reference from one database object to another, recorded in USER_DEPENDENCIES and checked when the referenced object changes.: the dependent object and its type, and the object and type it references. CREATE statements update the dependencies on their own. Dynamic SQL does not: a statement run with EXECUTE IMMEDIATE creates no dependencies, so a unit that reaches a table only that way is never marked stale when the table changes.
The object status: The STATUS column in USER_OBJECTS: VALID when the object compiled against its current references, INVALID when it compiled with errors, a reference changed or a privilege was revoked. of an object is in USER_OBJECTS. Oracle distinguishes four states: valid, compiled with errors, invalid because something it references changed, and unauthorized because a privilege was revoked. The *_OBJECTS views show the last three all as INVALID. A dependent that references another object is a direct dependent of it, and a dependent of that dependent is an indirect one. The figure shows the six objects of the lesson’s example.
The drawing shows the whole scene; its 4 steps are listed below.
Every step as text
- Five direct dependents. USER_DEPENDENCIES lists a row for each object that references dep_customers directly. Two views, a function and two procedures name it, so the table has five direct dependents.
- A sixth object, one step away. dep_report calls dep_name_of and never names the table. It is an indirect dependent of the table, and a direct dependent of the function only.
- Status says if it is current. USER_OBJECTS.STATUS reads VALID for all six objects after they compile. A change to the table can make a dependent stale, and the status then reads INVALID until Oracle revalidates it.
- Dynamic SQL leaves no row. A statement run with EXECUTE IMMEDIATE creates no dependency, so a unit that reaches the table only that way never appears in the list and is never marked stale.
Checkpoint
Check: Dependencies and status in the dictionary
A stale unit is revalidated on use, not broken
INVALID means that the compiled form may no longer match what the unit references, not that the code is wrong. Oracle decides how far a change reaches. With coarse-grained invalidation, a DDL statement that changes a referenced object invalidates all of its dependents. With fine-grained invalidation: Invalidation that marks only the dependents a DDL change affects, those that rely on the changed attribute or whose compiled metadata is no longer correct, not every dependent. it invalidates only the dependents that rely on the changed attribute, or whose compiled metadata is no longer correct. A change that invalidates one object also invalidates that object’s dependents, which Oracle calls cascading invalidation.
When someone next uses an invalid PL/SQL unit, Oracle checks whether a referenced object changed in a way that affects it. If so, it recompiles the unit; if not, it revalidates it without recompiling. A recompile that finishes with errors leaves the unit INVALID, and the errors are in USER_ERRORS and in SHOW ERRORS. A package that is invalidated and revalidated also loses its instantiation in every session that held it, which is the discarded state of the packages lesson.
The drawing shows the whole scene; its 6 steps are listed below.
Every step as text
- The change runs. An ALTER TABLE changes a referenced object. Oracle looks up the dependents of the table and decides, for each one, whether the change affects it.
- The rules decide. With fine-grained invalidation a dependent is marked only when it relies on what changed or its compiled metadata is no longer correct. With coarse-grained invalidation every dependent is marked.
- Unaffected units stay VALID. A dependent the change does not touch keeps its status and its compiled form. Nothing is recompiled and nobody notices.
- Affected units go stale. An affected dependent is marked INVALID, and so are its own dependents by cascade. Nothing is recompiled yet; the work waits for the next use.
- The next use revalidates. When the unit is referenced, Oracle checks whether a referenced object changed in a way that affects it. If so it recompiles the unit, and if not it revalidates it without recompiling.
- Success or errors. A clean compile or revalidation makes the unit VALID again. A compile with errors leaves it INVALID, and its errors are in USER_ERRORS and SHOW ERRORS.
Checkpoint
Check: Stale units and revalidation
Compiler warnings flag risky code before it runs
A warning reports a condition that is not serious enough to stop compilation. The message code has the form PLW-nnnnn, and each warning belongs to one of three categories: SEVERE for conditions that might cause wrong results, such as aliasing problems with parameters; PERFORMANCE for conditions that might slow the code, such as passing a VARCHAR2 value to a NUMBER column; and INFORMATIONAL for code that is merely untidy, such as code that can never run. PLSQL_WARNINGS: A compilation parameter that enables or disables compiler warnings by category or number, or turns a named warning into an error; ENABLE:ALL turns them all on. turns warnings on and off for the instance, the session or one stored unit.
Warnings are read where errors are: in USER_ERRORS and with SHOW ERRORS.
Two parts of depth follow. The first applies Oracle’s rule table to the six objects, predicts what an ADD column and a DROP column invalidate, and recompiles after a deployment.
The second part covers conditional compilation: Directives such as $IF and $ELSE that are evaluated at compile time, selecting which code to include based on version or other constants., which selects code at compile time. It also covers the PLSQL_OPTIMIZE_LEVEL: A compilation parameter that sets how much effort the compiler spends optimising PL/SQL library units. and native compilation: Compiling a PL/SQL unit to native machine code instead of interpreting it., which trade compile effort for speed. The last topic is edition-based redefinition: A feature that lets old and new PL/SQL code run side by side in separate editions during online upgrades., which keep old and new code side by side.
Checkpoint
Check: Compiler warnings and their categories
Predicting what a DDL change invalidates, and recompiling after it Pro: opens with a Pro, Premium or Ultimate pass
Oracle publishes the rules that decide which dependents a change invalidates, and they can be applied by hand. This part applies them to the six objects, checks the answer with a model and a script, and then recompiles a schema after a deployment and reads what failed.
In this part:
- Applying the rule table to an ADD column
- The same table for a DROP column, and the unit that cannot recompile
- Watching it in the dictionary, and what Oracle’s note adds
- Recompiling after a deployment and reading what failed
1 diagram · 3 runnable examples (Python, SQL) · about 9 minutes
Opening this part with your pass…
This part opens with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so this part can’t be opened at the moment. The free part above stands on its own. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
Conditional compilation, optimisation, native compilation and editions Pro: opens with a Pro, Premium or Ultimate pass
Some problems are better caught when a unit is compiled than when it runs. This part applies warnings to a unit with a branch that cannot run, selects code by database version, and weighs the optimisation level, native compilation and editions.
In this part:
- Reading a warning, then making it an error
- Choosing code at compile time with conditional compilation
- The optimisation level and native compilation
- Editions and wrapping
4 runnable examples (Python, SQL) · about 6 minutes
Opening this part with your pass…
This part opens with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so this part can’t be opened at the moment. The free part above stands on its own. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
Practise: predict the invalid units
Each question gives a unit and a change. Work out which exception of the rule applies, if any, before you choose.
Practice that adapts to you
Practise this lesson
7 questions about 9 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.
Interview questions
Warm-up (fresher to 2–5 years): how do you find every object that depends on a table?
Query USER_DEPENDENCIES with WHERE referenced_name = 'YOUR_TABLE'; each row gives a dependent object and its type. The ALL_ and DBA_ versions widen the search to other schemas. The list is complete only for static references: a statement run through dynamic SQL creates no dependency, so search the source for the table name as well when a unit builds statements at run time.
Three longer questions follow, about a deployment that left objects invalid, a column addition whose effects you must predict, and an upgrade that cannot take the application offline:
-
Mid to senior
After a large schema migration, 150 PL/SQL objects show STATUS INVALID. The team plans to let them recompile on first call. What is the risk, and how would you handle them before the application starts?
-
Senior to staff
A release adds a nullable column to a busy table. Its dependents are a view with SELECT *, a function that selects one named column with a bind variable in its WHERE clause, a procedure that selects one named column with no variable, and a procedure using table%ROWTYPE. Which will Oracle recompile, and how would you check?
-
Senior to staff
Your team is deploying a major PL/SQL change and the application must stay online. A colleague suggests edition-based redefinition. What does the team need to prepare, and how would you roll back if the new code fails?
Flashcards · 8 cards
Recall it, then check
Say the answer to yourself before you look: recalling it is what makes it stick.
-
Where does Oracle record which object depends on which?
In USER_DEPENDENCIES, ALL_DEPENDENCIES and DBA_DEPENDENCIES: each row gives the dependent object and its type and the referenced object and its type. CREATE statements update these on their own.
-
Why is a unit that reaches a table only through EXECUTE IMMEDIATE never marked stale?
Dynamic SQL creates no dependencies, so Oracle records nothing to invalidate when the table changes.
-
Which states can an object be in, and how do the dictionary views show them?
Valid; compiled with errors; invalid because something it references changed; and unauthorized because a privilege was revoked. The OBJECTS views show the last three all as INVALID.
-
What does INVALID actually mean for a PL/SQL unit?
That the compiled form may no longer match what the unit references, not that the code is wrong. On its next use Oracle recompiles it if a referenced object changed in a way that affects it, and otherwise revalidates it without recompiling.
-
How do coarse-grained and fine-grained invalidation differ, and what cascades?
Coarse-grained invalidation marks every dependent of the changed object; fine-grained marks only the dependents that rely on the changed attribute or whose compiled metadata is no longer correct. A change that invalidates one object also invalidates that object's dependents, which Oracle calls cascading invalidation.
-
Where are the errors of a recompile that failed?
The unit stays INVALID and the errors are in USER_ERRORS and in SHOW ERRORS. A package that is invalidated and revalidated also loses its instantiation in every session that held it.
-
What are the three categories of PL/SQL compiler warning?
SEVERE for conditions that might cause wrong results, such as aliasing problems with parameters; PERFORMANCE for conditions that might slow the code, such as passing a VARCHAR2 value to a NUMBER column; and INFORMATIONAL for untidy code, such as code that can never run. Each message code has the form PLW-nnnnn.
-
What can PLSQL_WARNINGS be set for, and where do warnings appear?
For the instance, the session or one stored unit. Warnings are read where errors are: in USER_ERRORS and with SHOW ERRORS.
Key takeaways
- USER_DEPENDENCIES lists the references between objects, and USER_OBJECTS.STATUS shows whether each is current.
- INVALID means stale, not broken: Oracle recompiles or revalidates the unit on its next use.
- Fine-grained invalidation marks only the dependents a change affects, and each operation has its own rule table.
- After a deployment, recompile on purpose instead of letting the first caller find a failure.
- Warnings, conditional compilation and editions are settings chosen at compile or upgrade time.
Terms in this lesson
- conditional compilation
- Directives such as $IF and $ELSE that are evaluated at compile time, selecting which code to include based on version or other constants.
- edition-based redefinition
- A feature that lets old and new PL/SQL code run side by side in separate editions during online upgrades.
- fine-grained invalidation
- Invalidation that marks only the dependents a DDL change affects, those that rely on the changed attribute or whose compiled metadata is no longer correct, not every dependent.
- native compilation
- Compiling a PL/SQL unit to native machine code instead of interpreting it.
- object dependency
- A reference from one database object to another, recorded in USER_DEPENDENCIES and checked when the referenced object changes.
- object status
- The STATUS column in USER_OBJECTS: VALID when the object compiled against its current references, INVALID when it compiled with errors, a reference changed or a privilege was revoked.
- PLSQL_OPTIMIZE_LEVEL
- A compilation parameter that sets how much effort the compiler spends optimising PL/SQL library units.
- PLSQL_WARNINGS
- A compilation parameter that enables or disables compiler warnings by category or number, or turns a named warning into an error; ENABLE:ALL turns them all on.
References
- Application Development Guide 26ai: Schema Object Dependency (Oracle)
- PL/SQL Language Reference 26ai: Conditional Compilation (Oracle)
- PL/SQL Language Reference 26ai: Compile-Time Warnings (Oracle)
- PL/SQL Language Reference 26ai: Compiling PL/SQL Units for Native Execution (Oracle)
- PL/SQL Language Reference 26ai: PL/SQL Source Text Wrapping (Oracle)
- PL/SQL Packages and Types Reference 26ai: DBMS_UTILITY (COMPILE_SCHEMA) (Oracle)
- PL/SQL Packages and Types Reference 26ai: UTL_RECOMP (Oracle)
- Application Development Guide 26ai: Using Edition-Based Redefinition (Oracle)
- Database Reference 26ai: DBMS_DB_VERSION (Oracle)
- Database Reference 26ai: PLSQL_OPTIMIZE_LEVEL (Oracle)
Report a problem with this lesson
Kept only in this browser. Your Learn progress