DBMS_OUTPUT Package

1. Purpose

DBMS_OUTPUT records debugging messages from anonymous blocks, stored procedures, and functions. The package stores messages in a session buffer for later retrieval.

2. Functionality

The ivorysql_ora extension provides DBMS_OUTPUT. Make sure the extension is installed in the current database. Connect with IvorySQL’s psql using the Oracle compatibility port, which defaults to 1521 and selects Oracle compatibility mode for the session.

psql -h localhost -p 1521 -d postgres

2.1. Interfaces

Call each procedure with the DBMS_OUTPUT. prefix.

Procedure Description

ENABLE(buffer_size INTEGER DEFAULT 20000)

Enables buffering. The default is 20,000 bytes, and values below 2,000 are treated as 2,000. Pass NULL for unlimited capacity.

DISABLE()

Disables buffering and clears the buffer. Writes while disabled are ignored.

PUT(a TEXT)

Appends text to the current line without ending it.

PUT_LINE(a TEXT)

Appends text and ends the current line.

NEW_LINE()

Ends the current line. If there is no pending text, it adds an empty line.

GET_LINE(line OUT TEXT, status OUT INTEGER)

Retrieves one line. A status of 0 means a line was read, and 1 means no line was available. The line output also supports VARCHAR2.

GET_LINES(lines OUT TEXT[], numlines IN OUT INTEGER)

Retrieves lines in a batch. Pass the requested count in numlines. The procedure returns the actual count in the same parameter. The lines output also supports VARCHAR2[].

2.2. Usage Notes

  • Call ENABLE before writing. Use NEW_LINE or PUT_LINE to complete a line started with PUT before reading it.

  • In IvorySQL’s psql, a top-level CALL to GET_LINE or GET_LINES displays the output parameters as result columns. psql does not support SQL*Plus’s SET SERVEROUTPUT ON.

  • In a PL/iSQL block, read the output into local variables. You can print the retrieved text with RAISE NOTICE.

  • Use the same database connection to write and read. Reading removes the retrieved lines. The buffer survives commits and rollbacks.

  • Repeating ENABLE updates the capacity limit and preserves unread lines and unfinished PUT text. Call DISABLE followed by ENABLE to start with an empty buffer. DISCARD ALL, DISCARD PACKAGE, and disconnecting also clear the buffer. DISCARD PACKAGE is available in Oracle compatibility mode.

  • Each line can contain up to 32,767 bytes. Exceeding the line limit raises ORU-10028. Exceeding the buffer limit raises ORU-10027. Shorten lines, read them sooner, or adjust the buffer capacity as appropriate.

3. Examples

Run these examples in IvorySQL’s psql.

3.1. Write and Read One Line

CALL DBMS_OUTPUT.ENABLE();
CALL DBMS_OUTPUT.PUT_LINE('Hello, IvorySQL!');
CALL DBMS_OUTPUT.GET_LINE(NULL, NULL);

The two NULL arguments are placeholders for the OUT parameters.

       line       | status
------------------+--------
 Hello, IvorySQL! |      0
(1 row)

3.2. Assemble Text and Read a Batch

CALL DBMS_OUTPUT.ENABLE(NULL);
CALL DBMS_OUTPUT.PUT('Task: ');
CALL DBMS_OUTPUT.PUT('demo');
CALL DBMS_OUTPUT.NEW_LINE();
CALL DBMS_OUTPUT.PUT_LINE('Finished');
CALL DBMS_OUTPUT.GET_LINES(NULL::text[], 10);

NULL::text[] selects the TEXT[] overload. This call requests up to 10 lines and returns two. Call GET_LINES again with the desired batch size if more lines remain.

          lines          | numlines
-------------------------+----------
 {"Task: demo",Finished} |        2
(1 row)