Timescale Logo

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 2 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 including addition information including owner, source code & description etc. (psql)


Edit function

\ef myfunction

// Edit a function in default editor (psql)

Timescale Logo

Subscribe to the Timescale Newsletter

By submitting, you acknowledge Timescale’s Privacy Policy
2023 © Timescale Inc. All rights reserved.