All Products
Search
Document Center

PolarDB:CREATE PACKAGE BODY

Last Updated:Mar 28, 2026

Creates a package body for an existing package specification.

Syntax

CREATE [ OR REPLACE ] PACKAGE BODY name
{ IS | AS }
  [ declaration; ] [, ...]
  [ { PROCEDURE proc_name
      [ (argname [ IN | IN OUT | OUT ] argtype [ DEFAULT value ]
        [, ...]) ]
      [ STRICT ]
      [ LEAKPROOF ]
      [ PARALLEL { UNSAFE | RESTRICTED | SAFE } ]
      [ COST execution_cost ]
      [ ROWS result_rows ]
      [ SET config_param { TO value | = value | FROM CURRENT } ]
    { IS | AS }
        program_body
      END [ proc_name ];
    |
      FUNCTION func_name
      [ (argname [ IN | IN OUT | OUT ] argtype [ DEFAULT value ]
        [, ...]) ]
      RETURN rettype [ DETERMINISTIC ]
      [ STRICT ]
      [ LEAKPROOF ]
      [ PARALLEL { UNSAFE | RESTRICTED | SAFE } ]
      [ COST execution_cost ]
      [ ROWS result_rows ]
      [ SET config_param { TO value | = value | FROM CURRENT } ]
    { IS | AS }
        program_body
      END [ func_name ];
    }
  ] [, ...]
  [ BEGIN
      statement; [, ...] ]
  END [ name ]

Description

CREATE PACKAGE BODY creates a package body. CREATE OR REPLACE PACKAGE BODY creates a new package body or replaces an existing one.

If you specify a schema name, the package body is created in that schema. Otherwise, it is created in the current schema. The name of the new package body must match an existing package specification in the same schema, and it cannot duplicate the name of an existing package body in the same schema. To update an existing package body definition, use CREATE OR REPLACE PACKAGE BODY.

The STRICT, LEAKPROOF, PARALLEL, COST, ROWS, and SET keywords provide extended functionality for PolarDB for PostgreSQL (Compatible with Oracle) and are not supported by Oracle databases.

Parameters

ParameterDescription
nameThe name of the package body. Can be schema-qualified.
declarationA private variable, type, cursor, or REF CURSOR declaration.
proc_nameThe name of a stored procedure. If a procedure with the same name and signature exists in the package specification, the procedure is public and accessible outside the package. Otherwise, it is private and accessible only within the package body.
argnameThe name of an argument.
IN | IN OUT | OUTThe argument mode.
argtypeThe data type of the argument.
DEFAULT valueThe default value for an input argument.
STRICTThe function is not executed when called with a NULL argument. Instead, the function returns NULL.
LEAKPROOFThe function does not reveal information about its arguments through any means other than its return value.
PARALLEL { UNSAFE | RESTRICTED | SAFE }Controls whether the stored procedure or function can run in parallel mode. UNSAFE (default) — cannot run in parallel mode; any SQL statement containing this subprogram uses a serial execution plan. RESTRICTED — can run in parallel mode, but only on the parallel group leader. SAFE — can run in parallel mode without restrictions.
execution_costThe estimated execution cost, in units of cpu_operator_cost. For functions that return a set, this is the cost per returned row. Default: 0.0025.
result_rowsThe estimated number of rows the planner expects the function to return. Default: 1000.
SET config_param { TO value | = value | FROM CURRENT }Sets a configuration parameter for the duration of the function. FROM CURRENT restores the parameter value when the function exits.
program_bodyThe pragma, declarations, and SPL statements that make up the function or stored procedure body. The pragma can be PRAGMA AUTONOMOUS_TRANSACTION to mark the subprogram as an autonomous transaction. Declarations can include variables, types, REF CURSORs, and subprograms. Subprogram declarations must appear after all other variable, type, and REF CURSOR declarations.
func_nameThe name of a function. If a function with the same name and signature exists in the package specification, the function is public. Otherwise, it is private.
rettypeThe return data type.
DETERMINISTICSpecifies that the function always returns the same result for the same input and does not modify the database. Equivalent to the PostgreSQL IMMUTABLE option. If you specify DETERMINISTIC for a public function in the package body, you must also specify it in the function declaration in the package specification. For private functions, no package specification declaration is required.
statementAn SPL program statement. Statements in the package initialization section (BEGIN...END) execute once per session the first time the package is referenced.

Examples

The following example creates the package body for the empinfo package. The package body declares a private variable v_counter, implements the get_name procedure and display_counter function, and initializes v_counter in the package initialization section.

CREATE OR REPLACE PACKAGE BODY empinfo
IS
    v_counter       INTEGER;
    PROCEDURE get_name (
        p_empno     NUMBER
    )
    IS
    BEGIN
        SELECT ename INTO emp_name FROM emp WHERE empno = p_empno;
        v_counter := v_counter + 1;
    END;
    FUNCTION display_counter
    RETURN INTEGER
    IS
    BEGIN
        RETURN v_counter;
    END;
BEGIN
    v_counter := 0;
    DBMS_OUTPUT.PUT_LINE('Initialized counter');
END;

v_counter is a private variable — it is accessible to all subprograms inside the package body, but cannot be read or modified directly from outside the package. The public variable emp_name and the public function display_counter expose controlled access to package state.

The following two anonymous blocks call the stored procedure and function in the empinfo package and display the public variable.

BEGIN
    empinfo.get_name(7369);
    DBMS_OUTPUT.PUT_LINE('Employee Name    : ' || empinfo.emp_name);
    DBMS_OUTPUT.PUT_LINE('Number of queries: ' || empinfo.display_counter);
END;

Output:

Initialized counter
Employee name: SMITH
Number of queries: 1
BEGIN
    empinfo.get_name(7900);
    DBMS_OUTPUT.PUT_LINE('Employee Name    : ' || empinfo.emp_name);
    DBMS_OUTPUT.PUT_LINE('Number of queries: ' || empinfo.display_counter);
END;

Output:

Employee name: JAMES
Number of queries: 2

The initialization section (BEGIN v_counter := 0;) runs once when the package is first referenced in a session. Subsequent calls within the same session skip initialization, which is why the query counter increments across both anonymous blocks.

What's next

  • CREATE PACKAGE — define the package specification that this package body implements

  • DROP PACKAGE — remove a package and its body