Error this answers
ORA-01000: maximum open cursors exceeded
Symptom
An application starts failing with:
ORA-01000: maximum open cursors exceeded
usually from one app and usually after it has been up for a while.
Why it happens
Each session can hold at most open_cursors cursors open at once. Applications leak them when they open a statement or result set and never close it, which is common with JDBC code that skips close() in error paths. The leak grows with uptime until a session hits the limit.
Check
Find sessions above a threshold, grouped by machine and SQL (cur.sql) or by machine only (cur2.sql):
SQL> show parameter open_cursors
SQL> @cur.sql 200
Pick a threshold below open_cursors so you see who's close to the limit, not just who has hit it.
-- cur.sql: sessions holding more than N open cursors, grouped by machine and SQL
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/open-cursors-by-session/
--
-- Usage: @cur.sql <cursors>
-- @cur.sql 200
set lines 132
set trims on
col machine format a35
select count(*), inst_id, machine,status,sql_id,prev_sql_id
from gv$session
where (inst_id,sid) in (select st.inst_id, st.sid
from gv$sesstat st, v$statname n
where st.statistic# = n.statistic#
and n.name = 'opened cursors current'
and st.value > &&1)
group by inst_id,machine,status,sql_id,prev_sql_id;
undefine 1curl -O https://hashjoin.com/kb/oracle/open-cursors-by-session/cur.sql
-- cur2.sql: the same, grouped by machine only
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/open-cursors-by-session/
--
-- Usage: @cur2.sql <cursors>
-- @cur2.sql 200
set lines 132
set trims on
col machine format a35
select count(*), machine,status
from gv$session
where (inst_id,sid) in (select st.inst_id, st.sid
from gv$sesstat st, v$statname n
where st.statistic# = n.statistic#
and n.name = 'opened cursors current'
and st.value > &&1)
group by machine,status;
undefine 1curl -O https://hashjoin.com/kb/oracle/open-cursors-by-session/cur2.sql
Fix
- The real fix is in the application: close every statement and result set, including on error paths. The
SQL_IDandPREV_SQL_IDincur.sqlpoint at the code that opens the cursors that pile up. - As a stopgap, raise
open_cursors(it's dynamic:alter system set open_cursors=1000 scope=both sid='*';). It's a per-session limit and only allocates what's used, but it only buys time if the app keeps leaking. - Restarting the app's connection pool clears the leaked cursors until they build up again.
Verify
Run cur.sql again after the app fix is deployed and the pool has cycled. The same machines should no longer show up at your threshold.
Gotchas
- Cached cursors aren't open cursors.
session_cached_cursorskeeps closed cursors in a cache; they don't count towardopen_cursors. - The statistic is looked up by name (
opened cursors current), not by number: statistic numbers change between versions.
What it reads
gv$sesstat v$statname gv$session
Related
- Who's connected: sessions by service, host and status across RAC (gv$session)
- Get the SQL text and execution plan for a SQL_ID (dbms_xplan.display_cursor)
Tired of fixing the same thing twice?
HASHJOIN turns the checks and fixes your on-call runs by hand into tooling that gives the same answer every time. Fixed scope, and you own all of the code.
New fixes and field notes by email, about once a month.