Updated on 2025-09-22 GMT+08:00

Functions

Overview

A function is a database object that returns a single value or a table object. A function must have a defined return value type. It applies to scenarios where a single value needs to be returned, for example, calculation, data conversion, or table data return.

Examples

The following example shows the creation of a function named add_num() that takes two integer parameters as input and returns their sum.

-- Create a function.
gaussdb=# CREATE OR REPLACE FUNCTION add_num(int, int) RETURNS int AS $$
BEGIN
        RETURN $1+$2; 
END;
$$ LANGUAGE plpgsql;

-- Use the add_num() function.
gaussdb=# SELECT add_num(3,4);
 add_num 
---------
       7
(1 row)

-- Drop the function.
gaussdb=# DROP FUNCTION add_num;
DROP FUNCTION