Create Function

Defines a new function within the database.

Synopsis

CREATE FUNCTION name ( [ argtype [, ...] ] )
       RETURNS returntype
       AS 'definition'
       LANGUAGE 'langname'
       [ WITH ( attribute [, ...] ) ]
CREATE FUNCTION name ( [ argtype [, ...] ] )
       RETURNS returntype
       AS 'obj_file' [, 'link_symbol' ]
       LANGUAGE 'langname'
       [ WITH ( attribute [, ...] ) ]

Parameters

name

The name of the new function being created.

argtype

The data type of the argument, or arguments, to be accepted by the new function. There are three general input types you may use: base types, complex types, or the special opaque type. The opaque type explicitly allows the function to accept arguments of invalid SQL types. The opaque type is generally used by internal functions, or functions written in internal language such as C, or PL/pgSQL, where the return type is not provided as a standard SQL data type.

returntype

The data type of the value or values returned by the new function. This may be set as a base type, complex type, setof type (a normal data type, prefixed by setof), or the opaque type.

Using the setof modifier determines that the function will return multiple rows worth of data (by default, a function returns only one row). For example, a return type defined as setof integer creates a function that can return more than a single row of integer values.

attribute

An optional function attribute. Valid attributes, as of PostgreSQL 7.1.x, are isstrict and iscacheable.

definition

The definition of the function to create. This ...

Get Practical PostgreSQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.