CREATE FUNCTION

An SQL command for defining a function

CREATE FUNCTION is a DDL command for defining a function.

CREATE FUNCTION has always been present in PostgreSQL.

Change history

Note: any changes prior to PostgreSQL 7.3 are not reflected in this list.

Examples

Basic execution of CREATE FUNCTION using SQL:

postgres=# CREATE FUNCTION foo()
             RETURNS INT
             LANGUAGE SQL
           AS $$
             SELECT 1;
           $$;
CREATE FUNCTION

postgres=# SELECT foo();
 foo
-----
   1
(1 row)

Replacing the function body:

postgres=# CREATE OR REPLACE FUNCTION foo()
             RETURNS INT
           AS $$
             SELECT 2;
           $$ LANGUAGE SQL;
CREATE FUNCTION

postgres=# SELECT foo();
 foo
-----
   2
(1 row)

Creating a PL/pgSQL function:

postgres=# CREATE FUNCTION bar()
             RETURNS INT
           AS $$
             BEGIN
               RETURN 1;
             END;
           $$ LANGUAGE plpgsql;
CREATE FUNCTION

Note that the LANGUAGE clause may be placed as the final element; this was mandatory until PostgreSQL 7.3, and can still be seen in many function examples.

Categories

DDL, Functions and procedures

See also

ALTER FUNCTION, DROP FUNCTION, CREATE PROCEDURE, check_function_bodies