The DBMS_UTILITY package provides utility subprograms for gathering database statistics, managing names, inspecting call stacks, and retrieving system information. PolarDB for PostgreSQL (Compatible with Oracle) supports a subset of the functions and stored procedures available in Oracle's DBMS_UTILITY package.
Supported subprograms
| Function or stored procedure | Type | Return type | Description |
|---|---|---|---|
ANALYZE_DATABASE(method [, estimate_rows [, estimate_percent [, method_opt ]]]) | Stored procedure | N/A | Analyzes all tables in all schemas within the current database. |
ANALYZE_PART_OBJECT(schema, object_name [, object_type [, command_type [, command_opt [, sample_clause ]]]]) | Stored procedure | N/A | Analyzes a single partitioned table. |
ANALYZE_SCHEMA(schema, method [, estimate_rows [, estimate_percent [, method_opt ]]]) | Stored procedure | N/A | Analyzes all tables in a specified schema. |
CANONICALIZE(name, canon_name OUT, canon_len) | Stored procedure | N/A | Canonicalizes a string by normalizing its case and stripping extra characters. |
COMMA_TO_TABLE(list, tablen OUT, tab OUT) | Stored procedure | N/A | Converts a comma-delimited list of names into a table of names. |
DB_VERSION(version OUT, compatibility OUT) | Stored procedure | N/A | Returns the database version. |
EXEC_DDL_STATEMENT(parse_string) | Stored procedure | N/A | Runs a DDL statement. |
FORMAT_CALL_STACK | Function | TEXT | Returns the current call stack in a readable format. |
FORMAT_ERROR_STACK | Function | TEXT | Returns the call stack at the point where an error was raised. |
FORMAT_ERROR_BACKTRACE | Function | TEXT | Returns the call stack at the point where an error was raised. |
GET_CPU_TIME | Function | NUMBER | Returns the current CPU time in hundredths of a second. |
GET_DEPENDENCY(type, schema, name) | Stored procedure | N/A | Lists objects that depend on a specified object. |
GET_HASH_VALUE(name, base, hash_size) | Function | NUMBER | Computes a hash value for a string. |
GET_PARAMETER_VALUE(parnam, intval OUT, strval OUT) | Stored procedure | BINARY_INTEGER | Returns the value of a database initialization parameter. |
GET_TIME | Function | NUMBER | Returns the current time in hundredths of a second. |
NAME_TOKENIZE(name, a OUT, b OUT, c OUT, dblink OUT, nextpos OUT) | Stored procedure | N/A | Parses a dotted name into its component parts. |
TABLE_TO_COMMA(tab, tablen OUT, list OUT) | Stored procedure | N/A | Converts a table of names into a comma-delimited list. |
PolarDB for PostgreSQL (Compatible with Oracle) supports only the subprograms listed above. Oracle's full DBMS_UTILITY package contains additional subprograms not available in this cluster.
Public variables
| Variable | Data type | Value | Description |
|---|---|---|---|
inv_error_on_restrictions | PLS_INTEGER | 1 | Used by the INVALIDATE stored procedure. |
lname_array | TABLE | — | Stores lists of long names. |
uncl_array | TABLE | — | Stores lists of users and names. |
LNAME_ARRAY
LNAME_ARRAY stores lists of long names, including fully qualified names.
TYPE lname_array IS TABLE OF VARCHAR2(4000) INDEX BY BINARY_INTEGER;UNCL_ARRAY
UNCL_ARRAY stores lists of users and names.
TYPE uncl_array IS TABLE OF VARCHAR2(227) INDEX BY BINARY_INTEGER;ANALYZE_DATABASE, ANALYZE_SCHEMA, and ANALYZE_PART_OBJECT
These three stored procedures gather distribution statistics on database tables. When executed, Postgres samples the data in a table and records the results in the pg_statistics system table.
The three procedures differ in scope:
ANALYZE_DATABASEanalyzes all tables in all schemas within the current database.ANALYZE_SCHEMAanalyzes all tables in a specified schema.ANALYZE_PART_OBJECTanalyzes a single table.
Syntax
ANALYZE_DATABASE(method VARCHAR2 [, estimate_rows NUMBER
[, estimate_percent NUMBER [, method_opt VARCHAR2 ]]])
ANALYZE_SCHEMA(schema VARCHAR2, method VARCHAR2
[, estimate_rows NUMBER [, estimate_percent NUMBER
[, method_opt VARCHAR2 ]]])
ANALYZE_PART_OBJECT(schema VARCHAR2, object_name VARCHAR2
[, object_type CHAR [, command_type CHAR
[, command_opt VARCHAR2 [, sample_clause ]]]])Parameters — ANALYZE_DATABASE and ANALYZE_SCHEMA
| Parameter | Description |
|---|---|
method | Controls whether statistics are gathered or removed. COMPUTE and ESTIMATE are identical: both run the Postgres ANALYZE statement. DELETE removes the relevant rows from pg_statistics. |
estimate_rows | The number of rows on which estimated statistics are based. Required if you specify ESTIMATE, but ignored in practice; included for compatibility. |
estimate_percent | The percentage of rows on which estimated statistics are based. Required if you specify ESTIMATE, but ignored in practice; included for compatibility. |
method_opt | The object types to analyze. Accepted combinations: FOR TABLE, FOR ALL [INDEXED] COLUMNS [SIZE n], FOR ALL INDEXES. Ignored in practice; included for compatibility. |
Parameters — ANALYZE_PART_OBJECT
| Parameter | Description |
|---|---|
schema | The schema that contains the object to analyze. |
object_name | The name of the partitioned object to analyze. |
object_type | The object type: T for table, I for index. Ignored in practice; included for compatibility. |
command_type | The analysis function to run: E (estimated statistics), C (exact statistics), V (validates partition structure and integrity). Ignored in practice; included for compatibility. |
command_opt | When command_type is C or E, accepted combinations are: FOR TABLE, FOR ALL COLUMNS, FOR ALL LOCAL INDEXES. When command_type is V and object_type is T, CASCADE is accepted. Ignored in practice; included for compatibility. |
sample_clause | When command_type is E, specifies the sample size: SAMPLE n { ROWS | PERCENT }. Ignored in practice; included for compatibility. |
CANONICALIZE
CANONICALIZE normalizes a name string according to the following rules:
If the string is not enclosed in double quotation marks, its characters must form a valid identifier; if not, an exception is raised.
If the string is not enclosed in double quotation marks and contains no periods, all alphabetic characters are capitalized and leading/trailing spaces are removed.
If the string is enclosed in double quotation marks and contains no periods, the double quotation marks are stripped.
If the string contains periods and no portion is double-quoted, each segment is capitalized and enclosed in double quotation marks.
If the string contains periods and some segments are double-quoted, double-quoted segments are returned unchanged (including the quotation marks), and unquoted segments are capitalized and enclosed in double quotation marks.
Syntax
CANONICALIZE(name VARCHAR2, canon_name OUT VARCHAR2,
canon_len BINARY_INTEGER)Parameters
| Parameter | Description |
|---|---|
name | The input string to canonicalize. |
canon_name | The canonicalized output string. |
canon_len | The number of bytes to process, starting from the first character. |
Examples
All examples use this wrapper procedure:
CREATE OR REPLACE PROCEDURE canonicalize (
p_name VARCHAR2,
p_length BINARY_INTEGER DEFAULT 30
)
IS
v_canon VARCHAR2(100);
BEGIN
DBMS_UTILITY.CANONICALIZE(p_name, v_canon, p_length);
DBMS_OUTPUT.PUT_LINE('Canonicalized name ==>' || v_canon || '<==');
DBMS_OUTPUT.PUT_LINE('Length: ' || LENGTH(v_canon));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('SQLERRM: ' || SQLERRM);
DBMS_OUTPUT.PUT_LINE('SQLCODE: ' || SQLCODE);
END;Quoted identifier with special characters — quotes stripped:
EXEC canonicalize('"_+142%"');Canonicalized name ==>_+142%<==
Length: 6COMMA_TO_TABLE
COMMA_TO_TABLE converts a comma-delimited list of names into a table of names. Each entry in the list becomes a table entry. Names must be formatted as valid identifiers.
Syntax
COMMA_TO_TABLE(list VARCHAR2, tablen OUT BINARY_INTEGER,
tab OUT { LNAME_ARRAY | UNCL_ARRAY })Parameters
| Parameter | Description |
|---|---|
list | The comma-delimited input list of names. |
tablen | The number of entries in the list. |
tab | The output table that receives the names. Use LNAME_ARRAY for long names or UNCL_ARRAY for user and name pairs. |
Example
CREATE OR REPLACE PROCEDURE comma_to_table (
p_list VARCHAR2
)
IS
r_lname DBMS_UTILITY.LNAME_ARRAY;
v_length BINARY_INTEGER;
BEGIN
DBMS_UTILITY.COMMA_TO_TABLE(p_list, v_length, r_lname);
FOR i IN 1..v_length LOOP
DBMS_OUTPUT.PUT_LINE(r_lname(i));
END LOOP;
END;EXEC comma_to_table('polardb.dept, polardb.emp, polardb.jobhist');polardb.dept
polardb.emp
polardb.jobhistDB_VERSION
DB_VERSION returns the engine version and compatibility string for the current database instance.
Syntax
DB_VERSION(version OUT VARCHAR2, compatibility OUT VARCHAR2)Parameters
| Parameter | Description |
|---|---|
version | The engine version of the instance. |
compatibility | The compatibility string. Its meaning is defined by the implementation. |
Example
DECLARE
v_version VARCHAR2(150);
v_compat VARCHAR2(150);
BEGIN
DBMS_UTILITY.DB_VERSION(v_version, v_compat);
DBMS_OUTPUT.PUT_LINE('Version: ' || v_version);
DBMS_OUTPUT.PUT_LINE('Compatibility: ' || v_compat);
END;Version: PostgreSQL 11.15 (POLARDB Database Compatible with Oracle 11.15.25)
Compatibility: PostgreSQL 11.15 (POLARDB Database Compatible with Oracle 11.15.25)EXEC_DDL_STATEMENT
EXEC_DDL_STATEMENT runs a DDL statement passed as a string.
Syntax
EXEC_DDL_STATEMENT(parse_string VARCHAR2)Parameters
| Parameter | Description |
|---|---|
parse_string | The DDL statement to run. |
Example
BEGIN
DBMS_UTILITY.EXEC_DDL_STATEMENT(
'CREATE TABLE job (' ||
'jobno NUMBER(3),' ||
'jname VARCHAR2(9))'
);
END;Ifparse_stringis not a valid DDL statement, PolarDB for PostgreSQL (Compatible with Oracle) raises an error: Oracle databases accept an invalidparse_stringwithout raising an error.
EXEC dbms_utility.exec_ddl_statement('select rownum from dual');
ERROR: 'parse_string' must be a valid DDL statementFORMAT_CALL_STACK
FORMAT_CALL_STACK returns the current call stack as a formatted text string. Call it from within a stored procedure, function, or package to inspect the active call chain at any point during execution.
Syntax
DBMS_UTILITY.FORMAT_CALL_STACK RETURN TEXTFORMAT_ERROR_BACKTRACE
FORMAT_ERROR_BACKTRACE returns the call stack at the point where an error was raised, as a formatted text string. Call it from within an exception handler to trace the origin of an error through nested subprogram calls.
Syntax
DBMS_UTILITY.FORMAT_ERROR_BACKTRACE RETURN TEXTFORMAT_ERROR_STACK
FORMAT_ERROR_STACK returns the call stack at the point where an error was raised, as a formatted text string.
Syntax
DBMS_UTILITY.FORMAT_ERROR_STACK RETURN TEXTPolarDB for PostgreSQL (Compatible with Oracle) implementsFORMAT_ERROR_STACKdifferently from Oracle. In Oracle,FORMAT_ERROR_STACKreturns strings containing SQLCODE and SQLERRM information. In PolarDB for PostgreSQL (Compatible with Oracle),FORMAT_ERROR_STACKbehaves the same asFORMAT_ERROR_BACKTRACE.
GET_CPU_TIME
GET_CPU_TIME returns the current CPU time in hundredths of a second, measured from an arbitrary reference point. Use the difference between two calls to measure CPU consumption for a specific operation.
Syntax
DBMS_UTILITY.GET_CPU_TIME RETURN NUMBERParameters
| Parameter | Description |
|---|---|
cputime | The number of hundredths of a second of CPU time elapsed. |
Example
SELECT DBMS_UTILITY.GET_CPU_TIME FROM DUAL;get_cpu_time
--------------
603The value 603 means 6.03 hundredths of a second, or 0.0603 seconds of CPU time.
GET_DEPENDENCY
GET_DEPENDENCY lists all objects that depend on a specified object and outputs the results using DBMS_OUTPUT.
Usage notes
GET_DEPENDENCYdoes not show dependencies for functions or stored procedures.
Syntax
GET_DEPENDENCY(type VARCHAR2, schema VARCHAR2, name VARCHAR2)Parameters
| Parameter | Description |
|---|---|
type | The type of the object. Valid values: INDEX, PACKAGE, PACKAGE BODY, SEQUENCE, TABLE, TRIGGER, TYPE, VIEW. |
schema | The schema that contains the object. |
name | The name of the object for which to retrieve dependencies. |
Example
BEGIN
DBMS_UTILITY.GET_DEPENDENCY('TABLE', 'public', 'EMP');
END;DEPENDENCIES ON public.EMP
------------------------------------------------------------------
*TABLE public.EMP()
* CONSTRAINT c public.emp()
* CONSTRAINT f public.emp()
* CONSTRAINT p public.emp()
* TYPE public.emp()
* CONSTRAINT c public.emp()
* CONSTRAINT f public.jobhist()
* VIEW .empname_view()GET_HASH_VALUE
GET_HASH_VALUE computes a hash value for a string within a specified range.
Syntax
hash NUMBER GET_HASH_VALUE(name VARCHAR2, base NUMBER, hash_size NUMBER)Parameters
| Parameter | Description |
|---|---|
name | The input string to hash. |
base | The starting value of the hash range. |
hash_size | The number of distinct hash values in the range. |
hash | The computed hash value. |
Example
The following example builds a hash table from the ename column of the emp table, generating up to 1,024 hash values starting from 100.
DECLARE
v_hash NUMBER;
TYPE hash_tab IS TABLE OF NUMBER INDEX BY VARCHAR2(10);
r_hash HASH_TAB;
CURSOR emp_cur IS SELECT ename FROM emp;
BEGIN
FOR r_emp IN emp_cur LOOP
r_hash(r_emp.ename) :=
DBMS_UTILITY.GET_HASH_VALUE(r_emp.ename, 100, 1024);
END LOOP;
FOR r_emp IN emp_cur LOOP
DBMS_OUTPUT.PUT_LINE(RPAD(r_emp.ename, 10) || ' ' ||
r_hash(r_emp.ename));
END LOOP;
END;SMITH 377
ALLEN 740
WARD 718
JONES 131
MARTIN 176
BLAKE 568
CLARK 621
SCOTT 1097
KING 235
TURNER 850
ADAMS 156
JAMES 942
FORD 775
MILLER 148GET_PARAMETER_VALUE
GET_PARAMETER_VALUE returns the current value of a database initialization parameter. Parameters are listed in the pg_settings system view.
Syntax
status BINARY_INTEGER GET_PARAMETER_VALUE(parnam VARCHAR2,
intval OUT INTEGER, strval OUT VARCHAR2)Parameters
| Parameter | Description |
|---|---|
parnam | The name of the initialization parameter. Parameters are listed in the pg_settings system view. |
intval | For integer parameters, the parameter value. For string parameters, the length of strval. |
strval | The value of string parameters. |
status | Returns 0 if the parameter value is INTEGER or BOOLEAN. Returns 1 if the parameter value is a string. |
Example
DECLARE
v_intval INTEGER;
v_strval VARCHAR2(80);
BEGIN
DBMS_UTILITY.GET_PARAMETER_VALUE('client_encoding', v_intval, v_strval);
DBMS_OUTPUT.PUT_LINE('client_encoding' || ': ' || v_strval);
END;client_encoding: UTF8GET_TIME
GET_TIME returns the current time in hundredths of a second. The absolute value is not meaningful on its own — use the difference between two calls to measure elapsed time.
Syntax
DBMS_UTILITY.GET_TIME RETURN NUMBERParameters
| Parameter | Description |
|---|---|
time | The number of hundredths of a second elapsed since the program started. |
Example
SELECT DBMS_UTILITY.GET_TIME FROM DUAL; get_time
----------
1555860NAME_TOKENIZE
NAME_TOKENIZE parses a dotted name into its component parts. Unquoted components are capitalized; double quotation marks are stripped from quoted components.
Syntax
NAME_TOKENIZE(name VARCHAR2, a OUT VARCHAR2, b OUT VARCHAR2,
c OUT VARCHAR2, dblink OUT VARCHAR2, nextpos OUT BINARY_INTEGER)Parameters
| Parameter | Description |
|---|---|
name | The input string, in the format a[.b[.c]][@dblink]. |
a | The leftmost component. |
b | The second component, if present. |
c | The third component, if present. |
dblink | The database link name, if present. |
nextpos | The position of the last character parsed in the input string. |
Examples
All examples use this wrapper procedure:
CREATE OR REPLACE PROCEDURE name_tokenize (
p_name VARCHAR2
)
IS
v_a VARCHAR2(30);
v_b VARCHAR2(30);
v_c VARCHAR2(30);
v_dblink VARCHAR2(30);
v_nextpos BINARY_INTEGER;
BEGIN
DBMS_UTILITY.NAME_TOKENIZE(p_name, v_a, v_b, v_c, v_dblink, v_nextpos);
DBMS_OUTPUT.PUT_LINE('name : ' || p_name);
DBMS_OUTPUT.PUT_LINE('a : ' || v_a);
DBMS_OUTPUT.PUT_LINE('b : ' || v_b);
DBMS_OUTPUT.PUT_LINE('c : ' || v_c);
DBMS_OUTPUT.PUT_LINE('dblink : ' || v_dblink);
DBMS_OUTPUT.PUT_LINE('nextpos: ' || v_nextpos);
END;Single unquoted name — capitalized:
BEGIN
name_tokenize('emp');
END;name : emp
a : EMP
b :
c :
dblink :
nextpos: 3Two-part name — both components capitalized:
BEGIN
name_tokenize('polardb.list_emp');
END;name : polardb.list_emp
a : polardb
b : LIST_EMP
c :
dblink :
nextpos: 16Three-part name with mixed quoting — quoted parts preserved, unquoted part capitalized:
BEGIN
name_tokenize('"polardb"."Emp_Admin".update_emp_sal');
END;name : "polardb"."Emp_Admin".update_emp_sal
a : polardb
b : Emp_Admin
c : UPDATE_EMP_SAL
dblink :
nextpos: 36Name with database link:
BEGIN
name_tokenize('polardb.emp@polardb_dblink');
END;name : polardb.emp@polardb_dblink
a : polardb
b : EMP
c :
dblink : polardb_DBLINK
nextpos: 26TABLE_TO_COMMA
TABLE_TO_COMMA converts a table of names into a comma-delimited list. Each table entry becomes a list entry. Names must be formatted as valid identifiers.
Syntax
TABLE_TO_COMMA(tab { LNAME_ARRAY | UNCL_ARRAY },
tablen OUT BINARY_INTEGER, list OUT VARCHAR2)Parameters
| Parameter | Description |
|---|---|
tab | The input table of names. Use LNAME_ARRAY for long names or UNCL_ARRAY for user and name pairs. |
tablen | The number of entries in the output list. |
list | The comma-delimited output list of names. |
Example
The following example converts a comma-separated list into a table using COMMA_TO_TABLE, then converts it back to a comma-separated list using TABLE_TO_COMMA.
CREATE OR REPLACE PROCEDURE table_to_comma (
p_list VARCHAR2
)
IS
r_lname DBMS_UTILITY.LNAME_ARRAY;
v_length BINARY_INTEGER;
v_listlen BINARY_INTEGER;
v_list VARCHAR2(80);
BEGIN
DBMS_UTILITY.COMMA_TO_TABLE(p_list, v_length, r_lname);
DBMS_OUTPUT.PUT_LINE('Table Entries');
DBMS_OUTPUT.PUT_LINE('-------------');
FOR i IN 1..v_length LOOP
DBMS_OUTPUT.PUT_LINE(r_lname(i));
END LOOP;
DBMS_OUTPUT.PUT_LINE('-------------');
DBMS_UTILITY.TABLE_TO_COMMA(r_lname, v_listlen, v_list);
DBMS_OUTPUT.PUT_LINE('Comma-Delimited List: ' || v_list);
END;EXEC table_to_comma('polardb.dept, polardb.emp, polardb.jobhist');Table Entries
-------------
polardb.dept
polardb.emp
polardb.jobhist
-------------
Comma-Delimited List: polardb.dept, polardb.emp, polardb.jobhist