Functions

Create a new function

$$
CREATE FUNCTION add(integer, integer) RETURNS integer
 AS 'select $1 + $2;'
 LANGUAGE SQL
 IMMUTABLE
 RETURNS NULL ON NULL INPUT;
$$

// Create a function to add 2 integers (SQL)

This function takes two integers as parameters IMMUTABLE means that the function cannot modify the database and always returns the same result when given the same argument values.


Calling function

select add(5,9);
 add 
-----
  14
(1 row)

// Function call (SQL)


List functions

\df
                        List of functions
 Schema | Name | Result data type | Argument data types |  Type  
--------+------+------------------+---------------------+--------
 public | add  | integer          | integer, integer    | normal
(1 row)

// Display all Functions (psql)

\df+

// Display all functions and additional information, including owner, source code, and description, etc. (psql)


Edit function

\ef myfunction

// Edit a function in default editor (psql)