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

ORA-01000: maximum open cursors for session exceeded

One session kept opening cursors without closing them until it reached the open_cursors limit.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-01000: maximum open cursors for session exceeded Potential Leaked SQL_ID: buav8k4m4d5fy

What changed in 26ai error messages →

Jump to a cause

  1. 01A function returns a SYS_REFCURSOR and the caller never closes it
  2. 02DBMS_SQL.OPEN_CURSOR in a loop without CLOSE_CURSOR

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

ORA-01000 means one session has more cursors open at the same time than the open_cursors parameter allows. The limit in the lab database was 300. In both cases below a loop of 500 iterations opened a cursor each time and never closed it, so it failed partway through.

23ai adds a hint to the message: Potential Leaked SQL_ID: buav8k4m4d5fy. Looking that SQL_ID up in v$sql (DBA privilege needed) shows the statement whose cursors piled up. The fix verified here is closing the cursor in the loop; the loop then finished all 500 iterations with the limit still at 300.

01A function returns a SYS_REFCURSOR and the caller never closes it

get_order_cur opens and returns a SYS_REFCURSOR. The caller loop did c := get_order_cur(i); FETCH c INTO v; 500 times without CLOSE c. Assigning a new cursor to c does not close the previous one, so they piled up and the block failed with maximum open cursors for session exceeded. The stack pointed to LAB_A.GET_ORDER_CUR, line 4, the OPEN.

open_cursors was 300. The SQL_ID in the message, buav8k4m4d5fy, was SELECT STATUS FROM ORDERS WHERE ORDER_ID = :B1 in v$sql, the query inside the function. Adding CLOSE c; after the FETCH printed 500 orders read.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id NUMBER PRIMARY KEY, status VARCHAR2(10));
INSERT INTO orders SELECT level, 'NEW' FROM dual CONNECT BY level <= 500;
COMMIT;
OUTPUT
Table created.


500 rows created.


Commit complete.
Setuprun as lab_a
CREATE OR REPLACE FUNCTION get_order_cur(p_id NUMBER) RETURN SYS_REFCURSOR IS
  c SYS_REFCURSOR;
BEGIN
  OPEN c FOR SELECT status FROM orders WHERE order_id = p_id;
  RETURN c;
END;
/
OUTPUT
Function created.
Reproducerun as lab_a
SET SERVEROUTPUT ON
DECLARE
  c SYS_REFCURSOR;
  v VARCHAR2(10);
  n NUMBER := 0;
BEGIN
  FOR i IN 1 .. 500 LOOP
    c := get_order_cur(i);
    FETCH c INTO v;
    n := n + 1;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE(n || ' orders read');
END;
/
✕ ERROR
DECLARE
*
ERROR at line 1:
ORA-01000: maximum open cursors for session exceeded Potential Leaked SQL_ID: buav8k4m4d5fy
ORA-06512: at "LAB_A.GET_ORDER_CUR", line 4
ORA-06512: at line 7
Help: https://docs.oracle.com/error-help/db/ora-01000/
Checkrun as system
COLUMN value FORMAT A10
SELECT name, value FROM v$parameter WHERE name = 'open_cursors';
OUTPUT
NAME                           VALUE
------------------------------ ----------
open_cursors                   300

1 row selected.
Checkrun as system
COLUMN sql_text FORMAT A50
SELECT sql_id, sql_text FROM v$sql WHERE sql_id = 'buav8k4m4d5fy' AND ROWNUM = 1;
OUTPUT
SQL_ID        SQL_TEXT
------------- --------------------------------------------------
buav8k4m4d5fy SELECT STATUS FROM ORDERS WHERE ORDER_ID = :B1

1 row selected.

Whoever receives a ref cursor must close it, including in the exception handler.

After the fixrun as lab_a
SET SERVEROUTPUT ON
DECLARE
  c SYS_REFCURSOR;
  v VARCHAR2(10);
  n NUMBER := 0;
BEGIN
  FOR i IN 1 .. 500 LOOP
    c := get_order_cur(i);
    FETCH c INTO v;
    CLOSE c;
    n := n + 1;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE(n || ' orders read');
END;
/
✓ SUCCESS
500 orders read

PL/SQL procedure successfully completed.

02DBMS_SQL.OPEN_CURSOR in a loop without CLOSE_CURSOR

A dynamic SQL loop called DBMS_SQL.OPEN_CURSOR, PARSE and EXECUTE for each order, 500 times, and never called DBMS_SQL.CLOSE_CURSOR. It failed at SYS.DBMS_SQL with ORA-01000. This time the message ended with Potential Leaked SQL_ID: and no ID.

Adding DBMS_SQL.CLOSE_CURSOR(c); after EXECUTE let the loop finish with 500 rows updated.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id NUMBER PRIMARY KEY, status VARCHAR2(10));
INSERT INTO orders SELECT level, 'NEW' FROM dual CONNECT BY level <= 500;
COMMIT;
OUTPUT
Table created.


500 rows created.


Commit complete.
Reproducerun as lab_a
SET SERVEROUTPUT ON
DECLARE
  c INTEGER;
  r INTEGER;
  n NUMBER := 0;
BEGIN
  FOR i IN 1 .. 500 LOOP
    c := DBMS_SQL.OPEN_CURSOR;
    DBMS_SQL.PARSE(c, 'UPDATE orders SET status = ''DONE'' WHERE order_id = ' || i, DBMS_SQL.NATIVE);
    r := DBMS_SQL.EXECUTE(c);
    n := n + r;
  END LOOP;
  COMMIT;
  DBMS_OUTPUT.PUT_LINE(n || ' rows updated');
END;
/
✕ ERROR
DECLARE
*
ERROR at line 1:
ORA-01000: maximum open cursors for session exceeded Potential Leaked SQL_ID:
ORA-06512: at "SYS.DBMS_SQL", line 1215
ORA-06512: at line 7
Help: https://docs.oracle.com/error-help/db/ora-01000/
Checkrun as system
COLUMN value FORMAT A10
SELECT name, value FROM v$parameter WHERE name = 'open_cursors';
OUTPUT
NAME                           VALUE
------------------------------ ----------
open_cursors                   300

1 row selected.

Each DBMS_SQL.OPEN_CURSOR needs a matching CLOSE_CURSOR. If you don't need DBMS_SQL, EXECUTE IMMEDIATE does not leave a cursor for you to close.

After the fixrun as lab_a
SET SERVEROUTPUT ON
DECLARE
  c INTEGER;
  r INTEGER;
  n NUMBER := 0;
BEGIN
  FOR i IN 1 .. 500 LOOP
    c := DBMS_SQL.OPEN_CURSOR;
    DBMS_SQL.PARSE(c, 'UPDATE orders SET status = ''DONE'' WHERE order_id = ' || i, DBMS_SQL.NATIVE);
    r := DBMS_SQL.EXECUTE(c);
    DBMS_SQL.CLOSE_CURSOR(c);
    n := n + r;
  END LOOP;
  COMMIT;
  DBMS_OUTPUT.PUT_LINE(n || ' rows updated');
END;
/
✓ SUCCESS
500 rows updated

PL/SQL procedure successfully completed.

FAQ

What does ORA-01000 maximum open cursors exceeded mean?

One session has more cursors open at once than open_cursors allows (300 in the lab). It is almost always cursors that are opened in a loop and never closed.

How do I find which SQL is leaking cursors?

In 23ai the message includes Potential Leaked SQL_ID. Look it up with SELECT sql_text FROM v$sql WHERE sql_id = '...'. In the lab it was the query inside the function that returned the ref cursor.

Should I just increase open_cursors?

In both lab cases the cursor count grew with the number of loop iterations, so a bigger limit would only move the failure further out. Closing the cursor fixed it with the limit unchanged at 300.

Do I need to close a SYS_REFCURSOR in PL/SQL?

Yes. In the lab, assigning a new ref cursor to the same variable did not close the old one; the session hit ORA-01000 until CLOSE c was added.

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