Updated on 2026-07-01 GMT+08:00

ALTER FUNCTION

Description

Alters attributes of a user-defined function or recompiles a function.

Precautions

  • Only the function owner or a user granted the ALTER permission can run the ALTER FUNCTION command. The system administrator has this permission by default. The permissions required vary depending on the attributes to be altered:
    • If a function involves operations on temporary tables, ALTER FUNCTION cannot be used.
    • To alter the owner or schema of a function, you must be the function owner or system administrator and a member of the new owner role.
    • Only the system administrator and initial user can alter the schema of a function to PUBLIC.
  • When separation of duties is enabled, no role is allowed to alter the owner of a function with the definer permission.
  • When separation of duties is disabled, only the initial user and system administrator can alter the owner of a function with the definer permission. However, the function owner cannot be altered to an O&M administrator.
  • Only the initial user can alter the owner of a function to the initial user.

Syntax

  • Change additional parameters of a user-defined function.
    1
    2
    ALTER FUNCTION function_name ( [ { [ argname ] [ argmode ] argtype} [, ...] ] )
        action [ ... ] [ RESTRICT ];
    

    The syntax of the ACTION clause is as follows.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    {CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT}
     | {IMMUTABLE | STABLE | VOLATILE}
     | {SHIPPABLE | NOT SHIPPABLE}
     | {NOT FENCED | FENCED}
     | [ NOT ] LEAKPROOF
     | { [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER }
     | AUTHID { DEFINER | CURRENT_USER }
     | COST execution_cost
     | ROWS result_rows
     | SET configuration_parameter { { TO | = } { value | DEFAULT }| FROM CURRENT}
     | RESET {configuration_parameter | ALL}
    
  • Rename the user-defined function.
    1
    2
    ALTER FUNCTION function_name ( [ { [ argname ] [ argmode ] argtype} [, ...] ] )
        RENAME TO new_name;
    
  • Change the owner of the user-defined function.
    1
    2
    ALTER FUNCTION function_name ( [ { [ argname ] [ argmode ] argtype} [, ...] ] )
        OWNER TO new_owner;
    
  • Change the schema of the user-defined function.
    1
    2
    ALTER FUNCTION function_name ( [ { [ argname ] [ argmode ] argtype} [, ...] ] )
        SET SCHEMA new_schema;
    

Parameters

  • function_name

    Specifies the name of the function to be altered.

    Value range: an existing function name

  • argmode

    Specifies whether a parameter is an input or output parameter.

    Value range:

    • IN: declares input parameters.
    • OUT: declares output parameters.
    • INOUT: declares input and output parameters.
    • VARIADIC: declares parameters of the array type.
  • argname

    Parameter name.

    Value range: a string that complies with the Identifier Naming Conventions.

  • argtype

    Specifies the parameter type.

    Value range: a valid type. For details, see Data Types.

  • CALLED ON NULL INPUT

    Specifies that the function can be called normally when some parameters of the function are NULL. Omitting this parameter is the same as specifying it.

  • RETURNS NULL ON NULL INPUT | STRICT

    Specifies that the function always returns NULL when a parameter of the function is NULL. If this parameter is specified, the function is not executed when a parameter is NULL; instead a NULL result is returned automatically.

    RETURNS NULL ON NULL INPUT and STRICT have the same functions.

  • IMMUTABLE

    Specifies that the function always returns the same result if the parameter values are the same.

  • STABLE

    Specifies that the function cannot modify the database, and that within a single table scan it will consistently return the same result for the same parameter value, but its result varies by SQL statements.

  • VOLATILE

    Specifies that the function value can change in a single table scan and no optimization is performed.

  • SHIPPABLE | NOT SHIPPABLE

    Specifies whether the function can be pushed down to DN for execution.

    • Functions of the IMMUTABLE type can always be pushed down to DNs.
    • Functions of the STABLE or VOLATILE type can be pushed down to DNs only if their attribute is SHIPPABLE.
  • LEAKPROOF

    Specifies that the function has no side effect and the parameter contains only the return value. LEAKPROOF can be set only by the system administrator.

  • EXTERNAL

    (Optional) The purpose is to be compatible with SQL. This feature applies to all functions, not only external functions.

  • SECURITY INVOKER | AUTHID CURRENT_USER

    Specifies that a function will be executed with the permissions of the user who calls it. Omitting this parameter is the same as specifying it.

    SECURITY INVOKER and AUTHID CURRENT_USER have the same functions.

  • SECURITY DEFINER| AUTHID DEFINER

    Specifies that a function will be executed with the permissions of the user who created it.

    AUTHID DEFINER and SECURITY DEFINER have the same functions.

  • COST execution_cost

    Estimates the execution cost of a function.

    The unit of execution_cost is cpu_operator_cost.

    Value range: a positive integer

  • ROWS result_rows

    Estimates the number of rows returned by the function. This is only allowed when the function is declared to return a set.

    Value range: a positive number. The default value is 1000.

  • configuration_parameter
    • value

      Sets a specified database session parameter to a specific value.

      Value range: a string.

      • DEFAULT: The default system setting is used in the new session.
      • OFF: The setting is disabled.
      • RESET: The default system setting is used in the new session.
      • User-specified value: The value must meet the restriction of the modified parameter.
    • FROM CURRENT

      Uses the value of configuration_parameter of the current session.

  • new_name

    Specifies the new name of a function.

    Value range: a string that complies with the Identifier Naming Conventions.

  • new_owner

    Specifies the new owner of a function. To alter the owner of a function, the new owner must have the CREATE permission on the schema to which the function belongs.

    Value range: an existing user role

  • new_schema

    Specifies the new schema of a function. To alter the schema of a function, you must have the CREATE permission on the new schema.

    Value range: an existing schema

Examples

The following is an example of modifying a function:

-- Create a function.
gaussdb=# CREATE OR REPLACE FUNCTION test_func(a int) RETURN int
IS
    proc_var int;
BEGIN
    proc_var := a;
    return 1;
END;
/

-- Alter the name of the function test_func(a int) to test_func_tk(a int).
gaussdb=# ALTER FUNCTION test_func(a int) RENAME TO test_func_tk;

-- Create a user jim.
gaussdb=# CREATE USER jim PASSWORD '********';

-- Create a test schema.
gaussdb=# CREATE SCHEMA test;

-- Change the owner of the function to jim.
gaussdb=# ALTER FUNCTION test_func_tk(a int) OWNER TO jim;

-- Change the function schema to test.
gaussdb=# ALTER FUNCTION test_func_tk(a int) SET SCHEMA test;

-- Delete the function.
gaussdb=# DROP FUNCTION test.test_func_tk(a int);

-- Delete user jim.
gaussdb=# DROP USER jim;

-- Delete a schema.
gaussdb=# DROP SCHEMA test;

Helpful Links

CREATE FUNCTION and DROP FUNCTION