Oracle 19c打开\u游标超出问题范围

Oracle 19c打开\u游标超出问题范围,oracle,oracle10g,oracle19c,Oracle,Oracle10g,Oracle19c,我们在Oracle 10g和19c中有相同的存储过程,具有相同的数据集和设置。 该过程执行了大量的数据获取和操作。 当我们使用同一组数据(比如10000条记录)执行时,它在10g中工作正常,时间更短,但在19c中需要花费很多时间,一段时间后会抛出“超出打开光标限制”错误。 我们从两个数据库对相同大小的OPEN_游标和CACHED_游标进行了基本比较 为了解决此问题,我们还可以从服务器端比较哪些参数或设置?我无法告诉您是什么导致了最大打开游标问题,但我告诉您如何通过使用GV$open\u CURS

我们在Oracle 10g和19c中有相同的存储过程,具有相同的数据集和设置。 该过程执行了大量的数据获取和操作。 当我们使用同一组数据(比如10000条记录)执行时,它在10g中工作正常,时间更短,但在19c中需要花费很多时间,一段时间后会抛出“超出打开光标限制”错误。 我们从两个数据库对相同大小的OPEN_游标和CACHED_游标进行了基本比较


为了解决此问题,我们还可以从服务器端比较哪些参数或设置?

我无法告诉您是什么导致了最大打开游标问题,但我告诉您如何通过使用
GV$open\u CURSOR
识别相关会话和SQL语句来查找原因

如果幸运的话,您可以通过计算每个会话打开的游标数的简单查询立即找到问题。下面的查询中有很多列,请使用IDE以便轻松浏览所有数据。根据我的经验,只要看一下诸如用户名和SQL文本之类的列就足以找出罪魁祸首

select count(*) over (partition by inst_id, sid) cursors_per_session, gv$open_cursor.*
from gv$open_cursor
order by cursors_per_session desc, inst_id, sid;
请记住,在该视图中会有许多奇怪的查询,这些查询可能会使计数超出您的预期。对于所有的递归和缓存查询,一个“无聊”的会话使用50个游标并不罕见。您正在寻找具有数百个打开游标的会话。(除非有人愚蠢地将参数值降低到默认值以下。)

不幸的是,
GV$OPEN\u CURSOR
不包含历史数据,如果在快速打开大量光标的紧循环中出现异常,这些问题可能会快速启动和停止。下面的PL/SQL块一直运行,直到找到一个包含大量打开游标的会话、存储数据并退出为止。这个PL/SQL块非常昂贵,会占用整个等待适当时机的处理会话,因此只需使用一次就可以找到问题

--Create table to hold the results.
create table too_many_cursors as
select 1 cursors_per_session, gv$open_cursor.*
from gv$open_cursor
where 1 = 0;


--Write the open cursor data when a session gets more than N open cursors.
declare
    v_open_cursor_threshold number := 50;
    v_count number;
begin
    --Loop forever until the problem is found.
    loop
        --Count the largest numbe of open cursors.
        select max(the_count)
        into v_count
        from
        (
            select count(*) the_count
            from gv$open_cursor
            group by inst_id, sid
        );

        --If the threshold is reached, write the data, commit it, and quit the program.
        if v_count >= v_open_cursor_threshold then

            insert into too_many_cursors
            select *
            from
            (
                select count(*) over (partition by inst_id, sid) cursors_per_session, gv$open_cursor.*
                from gv$open_cursor
            )
            where cursors_per_session >= v_open_cursor_threshold;
            
            commit;
            
            exit;
        end if;
        
    end loop;
end;
/


--Your problem should now be in this table:
select * from too_many_cursors;
如果要测试监控,可以使用下面的PL/SQL块打开大量游标

--Open a large number of cursors in and wait for 20 seconds.
--(Done by creating a dynamic PL/SQL block with many "open" commands with a "sleep" at the end.
declare
    v_number_of_open_cursors number := 200;
    v_declarations clob;
    v_opens clob;
    v_sql clob;
begin
    for i in 1 .. v_number_of_open_cursors loop
        v_declarations := v_declarations || 'v_cursor'|| i ||' sys_refcursor;' || chr(10);
        v_opens := v_opens || 'open v_cursor' || i || ' for select * from dual;';
    end loop;

    v_sql :=
        'declare '||chr(10)||v_declarations||chr(10)||
        'begin'||chr(10)||v_opens||chr(10)||
        'dbms_lock.sleep(20);'||chr(10)||'end;';

    --Print for debugging.
    --dbms_output.put_line(v_sql);

    execute immediate v_sql;
end;
/

该错误实际上总是表明应用程序代码存在游标泄漏,因此通常只能通过修复应用程序来修复。您可以启动
open\u cursors
参数,但如果出现光标泄漏,这只会延迟错误,而不会阻止错误发生。使用10000条记录作为起点,并尽可能尝试根据此数量记录进行批处理。