首批通过分布式安全可靠测评,为关键业务系统打造
关于 DBMS_OUTPUT 包 BUFFER_SIZE 缓冲区的说明
更新时间:2026-05-29 08:46
本文主要介绍 DBMS_OUTPUT 包 BUFFER_SIZE 缓冲区大小及如何调整缓冲区。
详细说明
DBMS_OUTPUT 包缓冲区的工作机制如下:
缓冲与输出时机: 当您调用
PUT或PUT_LINE时,信息只是被放入了一个缓冲区。这个缓冲区的内容,要等到执行这个PL/SQL程序单元(如匿名块、存储过程)的会话结束时,才会被传递到客户端。这也是为什么在程序块中,即使先调用了PUT_LINE,您也需要等到块内的所有代码(包括其中的等待或长时间运算)都执行完毕,才能在客户端(如 SQL*Plus、OceanBase 的 OBClient 等)看到输出。客户端设置: 为了在客户端(如 SQL*Plus、OceanBase 的 OBClient 等)看到
DBMS_OUTPUT的输出,您需要先执行SET SERVEROUTPUT ON来启用输出功能。但这只是控制客户端是否显示,并不改变缓冲区本身的刷新时机。
默认的缓冲区大小为 20000 字节
测试如下:
obclient(SYS@oracle)[SYS]> create or replace procedure test_sp(p_in int)
as
begin
for i in 1..p_in loop
DBMS_OUTPUT.PUT_LINE('these are 20 chars!!');
end loop;
end;
/
Query OK, 0 rows affected (0.051 sec)
obclient(SYS@oracle)[SYS]> set serveroutput on;
obclient(SYS@oracle)[SYS]> BEGIN
test_sp(1000); -- 可以正常输出的!
END;
/
Query OK, 1 row affected (0.398 sec)
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
...
...
...
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
obclient(SYS@oracle)[SYS]>
obclient(SYS@oracle)[SYS]> BEGIN
test_sp(1000);
DBMS_OUTPUT.PUT_LINE('a');
END;
/
OBE-00600: internal error code, arguments: -4024, Buffer not enough
at package body oceanbase.DBMS_OUTPUT.PUT , line : 75, col : 11
at package body oceanbase.DBMS_OUTPUT.PUT_LINE , line : 88, col : 7
at anonymous block , line : 3, col : 3
可以使用 DBMS_OUTPUT.ENABLE 存储过程来调整缓冲区大小
测试如下:
obclient(SYS@oracle)[SYS]> set serveroutput on;
obclient(SYS@oracle)[SYS]> call dbms_output.enable(null); -- 代表无限制
Query OK, 0 rows affected (0.001 sec)
obclient(SYS@oracle)[SYS]> BEGIN
test_sp(1000);
DBMS_OUTPUT.PUT_LINE('a');
END;
/
Query OK, 1 row affected (0.279 sec)
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
...
...
...
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
或者指定具体的缓冲区大小:
obclient(SYS@oracle)[SYS]> set serveroutput on;
obclient(SYS@oracle)[SYS]> call dbms_output.enable(1000000);
Query OK, 0 rows affected (0.001 sec)
obclient(SYS@oracle)[SYS]> BEGIN
test_sp(1000);
DBMS_OUTPUT.PUT_LINE('a');
END;
/
Query OK, 1 row affected (0.297 sec)
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
...
...
...
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
在 OBClient 中每执行一次 SET SERVEROUTPUT ON; 就会自动将 BUFFER_SIZE RESET 回默认值 20000 个字节
测试如下:
obclient(SYS@oracle)[SYS]> set serveroutput on;
obclient(SYS@oracle)[SYS]> call dbms_output.enable(1000000);
Query OK, 0 rows affected (0.001 sec)
obclient(SYS@oracle)[SYS]> BEGIN
test_sp(1000);
DBMS_OUTPUT.PUT_LINE('a');
END;
/
Query OK, 1 row affected (0.297 sec)
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
...
...
...
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
these are 20 chars!!
a
obclient(SYS@oracle)[SYS]>
obclient(SYS@oracle)[SYS]> call dbms_output.enable(1000000);
Query OK, 0 rows affected (0.001 sec)
obclient(SYS@oracle)[SYS]> set serveroutput on;
obclient(SYS@oracle)[SYS]> BEGIN
test_sp(1000);
DBMS_OUTPUT.PUT_LINE('a');
END;
/
OBE-00600: internal error code, arguments: -4024, Buffer not enough
at package body oceanbase.DBMS_OUTPUT.PUT , line : 75, col : 11
at package body oceanbase.DBMS_OUTPUT.PUT_LINE , line : 88, col : 7
at anonymous block , line : 3, col : 3
在 OBClient 中不支持 SET SERVEROUTPUT ON SIZE NNN; 语法,会报错的
obclient(SYS@oracle)[SYS]> set serveroutput on size 1000000;
SP2-0265: serveroutput value is ON or OFF <<<<<<====== 这边报错了!!!
适用版本
OceanBase 数据库 V2.2.77 及更高版本的 Oracle 模式租户。
更多参考
关于 DBMS_OUTPUT.ENABLE 存储过程的详细说明介绍,可以参见文档:ENABLE。