Oracle 23.26PL/SQLLab-verified · 2026-11-24

ORA-04068: existing state of packages has been discarded

A package your session had already used (and holds state for) was recompiled or invalidated by another session.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-04068: existing state of packages has been discarded

Jump to a cause

  1. 01Package body recompiled while a session holds its variables
  2. 02ALTER TABLE on a table the package uses

Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-24 · container image gvenzl/oracle-free:23-slim

ORA-04068 appears in a session that already called a package and still holds its state (package-level variables), after someone else recompiled that package or changed an object it depends on. The next call in the old session fails once, with ORA-04061 and ORA-04065 under it naming the package.

The tests below use two sessions: session A keeps calling the package, session B makes the change in between. Both causes produced exactly the same three error lines.

01Package body recompiled while a session holds its variables

The body of TICKET_PKG keeps a counter in the package variable g_count. Session A called next_no and got 1, so it now holds that state. Session B ran ALTER PACKAGE ticket_pkg COMPILE BODY. Session A's next call failed with ORA-04068, and ORA-04061 said the state of LAB_A.TICKET_PKG had been invalidated.

The body was rewritten without a package-level variable, taking the number from the sequence ticket_seq instead. With the same steps, session A got 1, B recompiled the body, and A's next call returned 2 with no error.

Setup
Setuprun as lab_a
CREATE SEQUENCE ticket_seq;
CREATE OR REPLACE PACKAGE ticket_pkg AS
  FUNCTION next_no RETURN NUMBER;
END;
/
CREATE OR REPLACE PACKAGE BODY ticket_pkg AS
  g_count NUMBER := 0;
  FUNCTION next_no RETURN NUMBER IS
  BEGIN
    g_count := g_count + 1;
    RETURN g_count;
  END;
END;
/
OUTPUT
Sequence created.


Package created.


Package body created.
Reproduce2 sessions open at the same time (A, B), run in this order
Session Arun as lab_a
SELECT ticket_pkg.next_no FROM dual;
OUTPUT
   NEXT_NO
----------
         1

1 row selected.
Session Brun as lab_a
ALTER PACKAGE ticket_pkg COMPILE BODY;
OUTPUT
Package body altered.
Session Arun as lab_a
SELECT ticket_pkg.next_no FROM dual;
OUTPUT
SELECT ticket_pkg.next_no FROM dual
*
ERROR at line 1:
ORA-04068: existing state of packages has been discarded
ORA-04061: existing state of package body "LAB_A.TICKET_PKG" has been invalidated
ORA-04065: not executed, altered or dropped package body "LAB_A.TICKET_PKG"
Help: https://docs.oracle.com/error-help/db/ora-04068/
Fixrun as lab_a
CREATE OR REPLACE PACKAGE BODY ticket_pkg AS
  FUNCTION next_no RETURN NUMBER IS
  BEGIN
    RETURN ticket_seq.NEXTVAL;
  END;
END;
/
OUTPUT
Package body created.

A package without package-level variables has no state to lose, so recompiling it does not break other sessions. Keep counters and caches in a table or sequence.

After the fix2 sessions open at the same time (A, B), run in this order
Session Arun as lab_a
SELECT ticket_pkg.next_no FROM dual;
OUTPUT
   NEXT_NO
----------
         1

1 row selected.
Session Brun as lab_a
ALTER PACKAGE ticket_pkg COMPILE BODY;
OUTPUT
Package body altered.
Session Arun as lab_a
SELECT ticket_pkg.next_no FROM dual;
OUTPUT
   NEXT_NO
----------
         2

1 row selected.

02ALTER TABLE on a table the package uses

EMP_PKG has a variable g_calls in its spec and reads the EMP table. Session A called emp_count (1). Then session B widened a column with ALTER TABLE emp MODIFY (name VARCHAR2(40)). That invalidated the package, and session A's next call failed with ORA-04068 / ORA-04061 / ORA-04065, without anyone touching the package itself.

Adding PRAGMA SERIALLY_REUSABLE to both the spec and the body fixed it. With the same steps, session A printed count = 1 before and after the ALTER TABLE. In the test the package was called from a PL/SQL block.

Setup
Setuprun as lab_a
CREATE TABLE emp (emp_id NUMBER, name VARCHAR2(20));
INSERT INTO emp VALUES (1, 'KIM');
COMMIT;
CREATE OR REPLACE PACKAGE emp_pkg AS
  g_calls NUMBER := 0;
  FUNCTION emp_count RETURN NUMBER;
END;
/
CREATE OR REPLACE PACKAGE BODY emp_pkg AS
  FUNCTION emp_count RETURN NUMBER IS
    v NUMBER;
  BEGIN
    g_calls := g_calls + 1;
    SELECT COUNT(name) INTO v FROM emp;
    RETURN v;
  END;
END;
/
OUTPUT
Table created.


1 row created.


Commit complete.


Package created.


Package body created.
Reproduce2 sessions open at the same time (B, A), run in this order
Session Brun as lab_a
ALTER TABLE emp MODIFY (name VARCHAR2(20));
OUTPUT
Table altered.
Session Arun as lab_a
SELECT emp_pkg.emp_count FROM dual;
OUTPUT
 EMP_COUNT
----------
         1

1 row selected.
Session Brun as lab_a
ALTER TABLE emp MODIFY (name VARCHAR2(40));
OUTPUT
Table altered.
Session Arun as lab_a
SELECT emp_pkg.emp_count FROM dual;
OUTPUT
SELECT emp_pkg.emp_count FROM dual
*
ERROR at line 1:
ORA-04068: existing state of packages has been discarded
ORA-04061: existing state of package body "LAB_A.EMP_PKG" has been invalidated
ORA-04065: not executed, altered or dropped package body "LAB_A.EMP_PKG"
Help: https://docs.oracle.com/error-help/db/ora-04068/
Fixrun as lab_a
CREATE OR REPLACE PACKAGE emp_pkg AS
  PRAGMA SERIALLY_REUSABLE;
  g_calls NUMBER := 0;
  FUNCTION emp_count RETURN NUMBER;
END;
/
CREATE OR REPLACE PACKAGE BODY emp_pkg AS
  PRAGMA SERIALLY_REUSABLE;
  FUNCTION emp_count RETURN NUMBER IS
    v NUMBER;
  BEGIN
    g_calls := g_calls + 1;
    SELECT COUNT(name) INTO v FROM emp;
    RETURN v;
  END;
END;
/
OUTPUT
Package created.


Package body created.

With SERIALLY_REUSABLE, package state lives only for one call, so there is nothing to discard. The flip side is that values in package variables are not kept between calls.

After the fix2 sessions open at the same time (B, A), run in this order
Session Brun as lab_a
ALTER TABLE emp MODIFY (name VARCHAR2(20));
OUTPUT
Table altered.
Session Arun as lab_a
SET SERVEROUTPUT ON
BEGIN DBMS_OUTPUT.PUT_LINE('count = ' || emp_pkg.emp_count); END;
/
OUTPUT
count = 1

PL/SQL procedure successfully completed.
Session Brun as lab_a
ALTER TABLE emp MODIFY (name VARCHAR2(40));
OUTPUT
Table altered.
Session Arun as lab_a
BEGIN DBMS_OUTPUT.PUT_LINE('count = ' || emp_pkg.emp_count); END;
/
OUTPUT
count = 1

PL/SQL procedure successfully completed.

FAQ

Why does ORA-04068 happen after deploying a package?

Sessions that were already connected (for example in a connection pool) still hold the old package state. Their first call after the deployment fails. In the test the second call in session A failed right after another session recompiled the body.

What do ORA-04061 and ORA-04065 under ORA-04068 mean?

They name the package. ORA-04061 says the state of that package body was invalidated, and ORA-04065 says it was not executed because it was altered or dropped. Both appeared together in every test.

Can ALTER TABLE cause ORA-04068 even if I did not change the package?

Yes. In the test, ALTER TABLE emp MODIFY (name VARCHAR2(40)) invalidated EMP_PKG, which reads EMP, and the next call in the other session failed with ORA-04068.

How do I stop ORA-04068 for good?

Remove package-level state (in the test: a sequence instead of a counter variable) or mark the package PRAGMA SERIALLY_REUSABLE. Both versions ran without error after the same recompile or ALTER TABLE.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0