Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Reji.

Asked: June 08, 2000 - 8:57 am UTC

Last updated: March 02, 2012 - 6:50 am UTC

Version: 8.1.6

Viewed 10K+ times! This question is

You Asked

Tom:
When you set the open_cursor parameter, what all you have
to consider? When Oracle create a cursor? For example
in pl/sql, you explicitly open a create a cursor and use it,
if you have to, but when will Oracle implicitly create a
cursor? ex: In JDBC, when you execute a Statement, SQLJ
when you create an iterator, I am just guessing here but
would you please give me the situations where Oracle creates
cursors?

Is it possible two client sessions share the same cursor?

Any help is really appreciated?

TIA
Reji



and Tom said...

You have to consider all cursors -- cursors you open, cursors your application framework will open on your behalf, cursors the database must open to perform recursive SQL.

For example, if you are in forms and have a default block and nothing else and run that form, you'll find (before doing ANYTHING) you have some cursors open. Forms (your application framework) has done some sql and cached the cursors on your behalf.

As you do more work in forms (eg: query up a row, update a field in the row) you'll see more and more cursors appear. Some you opened (by doing the query). Others were opened on your behalf (eg: forms locking the record for us when we updated the field).

You might also see some cursors come and go that reference SYS tables. These would be recursive queries and would happen when the optimizer parses your query, needs to allocate space for your query and so on.

You might also see other cursor come into play when triggers fire, stored procedures run and so on.


It is not possible for 2 sessions to share the same exact cursor but it is highly probable that 2 sessions will share the same "shared_pool" entry for the underlying query (see SHARED SQL in the concepts guide). A cursor is in your space, the parse tree, optimization, security and such is in a shared space.

It should be noted that OPEN_CURSORS simply allocates a fixed number of slots but does not allocate memory for these slots for a client (eg: it sets an array up to have 1,000 cursors for example but does not allocate 1,000 cursors). Rather, we will allocate 64 cursor contexts at a time, as needed (so the first cursor will allocate 64 contexts, the 65'th will get 64 more and so on). So, setting open-cursors to 1,000 or so it not harmful (but don't go overboard and set it to 1,000,000 or something ;)

Rating

  (8 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Pauline, April 09, 2001 - 3:23 pm UTC

You give the very clear concept explanation here.

open_cursors

Parag Jayant Patankar, January 29, 2005 - 6:42 am UTC

Hi Tom,

I am trying to understand concept of open_cursors in Oracle 9.2. Thanks for your excellent answer in this thread.

Can you pl tell me

Q1. How to arrive the good paramter ( optimum parameter ) of open_cursors ?

Q2. Suppose I set up the value overboard that is 1,000,000 for this parameter ( you said not to set this value overboard in this question ) ? Where it will make a problem ?

regards & thanks
pjp

Tom Kyte
January 29, 2005 - 8:57 am UTC

q1) it'll be at least one more than your applications actually use.

Meaning, you need to look to your applications to answer this.


q2) It'll allocate an array of 1,000,000 cursor slots at connect time (memory). It'll let a buggy application leak cursors like mad for a long time before blowing up (eating ram, consuming resources).

where wouldn't it make a problem would be a better question

OPEN_CURSORS

Suraj Sharma, May 22, 2008 - 5:41 am UTC

Tom,

Quite confusing!!!

On one hand you are saying "It should be noted that OPEN_CURSORS simply allocates a fixed number of slots but does not allocate memory for these slots for a client"

On the other hand:

¿q2) It'll allocate an array of 1,000,000 cursor slots at connect time (memory). It'll let a buggy application leak cursors like mad for a long time before blowing up (eating ram, consuming resources).¿

Please explain the same.

Suraj Sharma

Tom Kyte
May 22, 2008 - 7:17 am UTC

what do you mean by "on the other hand", I see two statements that say the same thing

a) "simply allocates a fixed number of slots" - eg: allocates an array

b) "it'll allocate an array of 1,000,000 cursor slots" - eg: allocates an array


do you see they say the same thing?

RE:OPEN_CURSORS

Suraj Sharma, May 22, 2008 - 8:38 am UTC

My apology for not being clear in this:

I was actually talking about memory allocation. On one statement you said that " OPEN_CURSORS simply allocates a fixed number of
slots but does not allocate memory for these slots for a client" ON THE OTHER HAND "It'll allocate an array of 1,000,000 cursor slots at connect time (memory). It'll let a buggy
application leak cursors like mad for a long time before blowing up (eating ram, consuming resources)."

I am sorry if I could not understand this properly.

Tom Kyte
May 23, 2008 - 7:56 am UTC

do you understand how much memory an array with 1,000,000 slots would consume.

it would consume at least 1,000,000 bytes of storage and if it were an array of pointers - between 4,000,000 and 8,000,000 bytes of storage. If it were an array of a more complex structure - it would consume even more.

1,000,000 of ANYTHING is big.


we allocate a fixed sized array of something. If you set open cursors to a reasonable number, this is not a problem. If you set it to a wildly high number - well - think about it...

open cursors

Jay, September 30, 2010 - 1:01 am UTC

Hi Tom,
Does this parameter have an impact on performance? If we set it too small, Oracle may throw an error when an application tries to open too many cursors. If we set it too high, it will let buggy applications get away with not managing cursors properly.
I have this doubt because I am trying to find out why a file generation program (extract dta from Oracle db) written in c++ is taking a few minutes on one machine and an hour on another. The same db dump was applied on both machines. So data content, structures are same. Both machines have same configuration (RAM, CPU)etc. On comparing the parameters, open_cursors is 1000 on one and 2000 on the other. Would that cause a performance difference?

Tom Kyte
September 30, 2010 - 7:30 am UTC

the down side of setting large will be increased client and server memory usage (eery cursor you have opened takes memory) and possibly increased cursor opening time as we have more slots to look through to find an empty one.

1000 to 2000 would not cause "hours of difference"

run the application with dbms_monitoring on (with wait event tracing) and tkprof the results. You will discover instantly "what is different" - if anything - at the database level by comparing those reports.

OPEN CURSOR

KHURSHEED ALAM KHAN, March 02, 2012 - 4:47 am UTC

Dear TOM,

I also got the same error when I run one JAVA based application which make huge updates and insertions in database.

I checked the default cursor value as follow:

sql>Sho parameter open_cursors;

It is displayed as 300

I have change the parameter using the below query:

alter system set open_cursors=500 scope=both;

system altered.

The value is changed but I am facing the same error:

Open cursor exceeds.

Please suggest me on this.I will thankful to you.

Thanks
Tom Kyte
March 02, 2012 - 5:48 am UTC

you are leaking cursors, your program has a serious bug. Your program is almost certainly corrupting data in the database.


Here is what I think is happening (99.99999999999% sure on this, I see it so many times....)

you open a cursor
you hit an error
you fly over the code that would have closed the cursor
your exception block HIDES the error (you are swallowing exceptions - google up that term and read and read and read about it)
you return from your method - and no one knows that you failed at your task

you just leaked a cursor, you cannot reach it anymore, it is out of scope.



While your program is running, query v$open_cursor every now and then and display all of the duplicate sql your session has open - that'll give you a clue where the problem is in your code.

And then remove all of your exception blocks that swallow exceptions so that your program just DIES with unhandled exceptions (that is something you should do, just let it DIE) - or fix them to

a) release resources
b) re-raise the exception
c) die

You cannot survive exceptions when you have no clue what they are about.

Open Cursors in 11g

carsten, March 02, 2012 - 6:35 am UTC

Actually the java 11 drivers do not actually close a cursor on serverside if you does that on the client.
It seems that Oracle has choosen to fix the cursor reuse problem on the driver side. Our application now requires well over 1000 open cursors as we have this many different statements overall.
So prepare the server resources for your needs is what I found as suggestion.

Tom Kyte
March 02, 2012 - 6:50 am UTC

documentation, bug or metalink note number please.

It sounds like you might be saying that they have enabled statement caching by default - but that would be a simple thing to turn off and/or configure to cache a smaller number.

Impact of High value open_cursors and processes?

Rohit, January 19, 2015 - 12:59 pm UTC

Hi,

We have just upgraded Oracle from version from 11.2.0.2 to 11.2.0.4.
As part of upgrade the values of few parameters got changed.
Parameter Name Post Prior to upgrade
----------------------------------------------------
optimizer_secure_view_merging ->FALSE -> TRUE
open_cursors ->1500 ->500
session_cached_cursors ->300 ->50
job_queue_processes ->128 ->32
processes ->800 ->300
_cursor_features_enabled ->MISSING ->10

Now one of our processes which use Oracle Queues, has got performance degradation.
But now when we change values of few parameters back to the original one i.e. previous version, the process delivers as per SLA. The following parameters are impacting the process

1. Without setting value of _cursor_features_enabled as 10, it doesn’t work i.e response is very slow. So this has become must for us.

2. Either of following combinations work
->a. Processes = 800 and open_cursors = 500
->b. Processes = 1500 and open_cursors = 300
Regarding _cursor_features_enabled, perhaps there was one bug in Oracle. As solution of that a patch along with setting some value of this parameter solves the issue.

Could you help me in understanding how this change is impacting performance?
• Reducing value of process to 800 from 1500
• Reducing value of open_cursors to 300 from 500


Apologies for typos.

Regards,
Rohit

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library