Resolving ORA-04061: A Practical Guide to PL/SQL Package Invalidation

Getting PL/SQL error ORA-04061? Learn why recompiling a stateful package invalidates its state and how you can fix or prevent the issue.

Getting PL/SQL error ORA-04061? Learn why recompiling a stateful package invalidates its state and how you can fix or prevent the issue.
Photo by BoliviaInteligente

Source

  1. Annals of Oracle’s Improbable Errors: ORA-04061: existing state of package has been invalidated.

What ORA-04061 means

ORA-04061 usually appears in an error stack with ORA-04068, ORA-04065, and ORA-06508. The package body was invalidated while the session still held state for that package. Oracle discards that state and re-instantiates the package on a later reference. See Oracle’s PL/SQL package documentation for the package-state rules.

ORA-04068: existing state of packages has been discarded
ORA-04061: existing state of package body "schema.package" has been invalidated
ORA-04065: not executed, altered or dropped package body "schema.package"
ORA-06508: PL/SQL: could not find program unit being called: "schema.package"

Why package state is discarded

A PL/SQL package is stateful when its specification or body declares package-level variables, constants, or cursors. Recompiling an instantiated stateful package discards its state; the next invocation raises ORA-04068 and the following reference re-initializes the package.

You cannot eliminate this behavior while the package depends on session state. Review declarations outside functions and procedures, especially package-level variables and cursors. On Oracle Database 10g, constants also contribute to package state. On Oracle Database 11g Release 2 and later, compile-time constants can be treated as stateless when their values are known at compile time.

Practical recovery and prevention

  1. Read the entire error stack and identify the package and dependency that changed.
  2. Retry the reference after Oracle re-instantiates the package, then confirm whether the error is repeatable.
  3. Move variables and cursors into the smallest procedure or function scope that needs them.
  4. Keep stable constants in a standalone package specification when that matches the dependency design.
  5. Use SERIALLY_REUSABLE only when the package lifecycle and session behavior are appropriate.

Find package-level variables with PL/Scope

Enable PL/Scope in the schema-owner session, recompile the package bodies, and query the identifier metadata:

ALTER SESSION SET plscope_settings = 'IDENTIFIERS:ALL';
SELECT object_name AS package,
       type,
       name AS variable_name
FROM user_identifiers
WHERE object_type IN ('PACKAGE', 'PACKAGE BODY')
  AND usage = 'DECLARATION'
  AND type IN ('VARIABLE', 'CONSTANT')
  AND usage_context_id IN (
      SELECT usage_id
      FROM user_identifiers
      WHERE type = 'PACKAGE'
  );

The result identifies packages whose global state deserves review. Package invalidation can also follow dependency invalidation and revalidation, so inspect related schema objects before applying a production change.

Example: a stateful package

CREATE OR REPLACE PACKAGE BODY mypkg IS
  v_statevar NUMBER := 5;

  PROCEDURE myproc IS
    myval NUMBER;
  BEGIN
    SELECT x INTO myval FROM dependonme;
    myval := myval * v_statevar;
    DBMS_OUTPUT.PUT_LINE('My Result is: ' || myval);
  END;
END mypkg;
/

Here v_statevar is package-level state. If the package body is invalidated and recompiled while a session holds an instance, the next call can produce the ORA-04061 and ORA-04068 sequence described above.

ORA-04061 at a glance

ORA-04061 usually appears when Oracle invalidates the state of a stateful PL/SQL package after recompilation or dependency changes. The related ORA-04068 message means the existing package state has been discarded.

Safe recovery path

  1. Read the complete error stack and identify the package and dependency that changed.
  2. Allow the session to re-instantiate the package, then confirm whether the error is repeatable.
  3. Inspect package-level variables, constants, and cursors before changing production code.
  4. Use PL/Scope and the diagnostic query below to locate package-level state across the schema.

This guide explains the cause, the stateful-package rules, and practical ways to reduce repeated invalidation.

2026-04-10plsql, sql Off Comments off on Resolving ORA-04061: A Practical Guide to PL/SQL Package Invalidation547