Skip to main content
Version: 2.0.0 (latest)

3.9. Functions

Functions are objects that store a collection of commands (routines) that can be reused in different ways from within the database system itself to external languages or programs. In PostgreSQL, these powerful objects are also used to create other objects like operators, triggers, casts, conversions, user-defined languages, and many others.

ch3.function-dialog.attributes12345678910111213

Attributes
#FieldGroup boxDescription
1Language

The language in which the function is written. There are four built-in languages available when a new model is created in pgModeler: c, internal, sql, and plpgsql. It is also possible to use the user-defined languages of the model.

2Security

Indicates the security context in which the function is executed. Two values are accepted: SECURITY INVOKER and SECURITY DEFINER. The first one indicates that the function is executed with the privileges of the user that calls it. The second one indicates that the function is executed with the privileges of the user that owns (defined) it.

3Return method

Defines the returning mode of the function. Three modes are accepted: Simple, Set, and Table. Simple indicates that the function returns a single value of the type specified in the Return type field. Set indicates that the function returns a set of values of that type. Table indicates that the function returns a table with different columns and data types instead of a simple value; in this mode, the columns and their types are configured in the list Return table.

4Function typeSettings

Indicates the volatility of the function. This attribute informs the PostgreSQL query optimizer about the behavior of the function. Three values are accepted: VOLATILE, STABLE, and IMMUTABLE. VOLATILE indicates that the function's result can change even within a single table scan, so no optimizations can be made. STABLE indicates that the function cannot modify the database and that, within a single table scan, it returns the same result for the same argument values. IMMUTABLE indicates that the function cannot modify the database and always returns the same result when called with the same arguments.

5Parallelism typeSettings

Defines the parallelism settings of the function. The possible values are PARALLEL UNSAFE, PARALLEL SAFE, and PARALLEL RESTRICTED. PARALLEL UNSAFE, the default value, indicates that the function can't be executed in parallel mode, and the presence of such a function in an SQL statement forces a serial execution plan. PARALLEL RESTRICTED indicates that the function can be executed in parallel mode, but only by the parallel group leader. PARALLEL SAFE indicates that the function is safe to run in parallel mode without restrictions.

6Execution costSettings

Specifies an estimated execution cost of the function (COST), which the PostgreSQL query planner can use.

7Behavior typeSettings

Indicates the behavior when the function is called with null arguments. Two values are accepted: CALLED ON NULL INPUT and RETURNS NULL ON NULL INPUT. The first one indicates that the function is called normally even if there is a null argument. The second one indicates that the function always returns null when any argument is null: in practice, the function is not executed and a null result is returned automatically.

8Rows returnedSettings

Specifies the estimated number of rows returned by the function, which the PostgreSQL query planner can expect. It is used (ROWS) only for functions that return a set of results.

9LeakproofSettings

Indicates that the function has no side effects and reveals no information about its arguments other than its return value.

10WindowSettings

Marks the function as a window function (WINDOW). A window function acts like an aggregate by processing a set of rows of a table, but, unlike aggregates, the rows are not grouped in a single result: they retain their separate identity. More details about window functions can be found in the PostgreSQL documentation.

11TypeReturn type

The data type. Besides the built-in types, the list also holds the user-defined types, domains and other types of the model.

12DimensionReturn type

Number of dimensions when the type is an array (type[]). 0 means that the type is not an array.

13FormatReturn type

Read-only preview of the data type as it is written in the SQL code, with length, precision, dimension and other modifiers applied.

ch3.function-dialog.transform-types1234

Transform types
#FieldGroup boxDescription
1TypeData type

The data type. Besides the built-in types, the list also holds the user-defined types, domains and other types of the model.

2DimensionData type

Number of dimensions when the type is an array (type[]). 0 means that the type is not an array.

3FormatData type

Read-only preview of the data type as it is written in the SQL code, with length, precision, dimension and other modifiers applied.

4Transform types

The transforms that a call to the routine should apply. Select the type in the group above and use the add button to include it in the list.

ch3.function-dialog.definition1

Definition
#FieldDescription
1Source code

The complete set of commands that the function must execute. This field is language-dependent, which means that you cannot mix commands from different languages: only the language selected in the field Language can be used. When the language is c, this field is replaced by the fields Library and Symbol.

ch3.function-dialog.configuration1

Configuration
#FieldDescription
1Configuration parameters

Runtime configuration parameters of the routine (SET configuration_parameter = value). Type the parameter and the value, then use the add button to include them in the list.

When the return method is Table, the tab Attributes also shows a list with the set of columns returned by the function. The user is asked to configure a new column every time the add button is activated; since the returned table has no complex syntax, demanding only a name and data type for each column, the parameter editing form is used instead of the column editing form.

The fields of the tab Definition vary according to the selected language. For the c language, the fields Symbol (the function name that defines the function in a shared library) and Library (the path to the shared object file on the destination server) are shown instead of the source code field.

Parameters​

Parameters are defined by a separate object and assigned to functions. This kind of object is quite similar to columns, but some of the attributes present on the latter, like NOT NULL and Sequence, are absent, and one additional attribute exists as detailed below.

ch3.function-dialog.parameters1

Parameters
#FieldDescription
1Parameters

The parameters of the function. Each one is configured in the parameter form described in the next section.

Each parameter is configured in the following form:

ch3.parameter-dialog12345678

Parameter
#FieldGroup boxDescription
1Name

The name of the object. It follows the PostgreSQL rules for identifiers (up to 63 bytes in a default installation). Leaving it empty raises an error when the form is applied.

2Default value

The default value of the parameter, used when the argument is omitted in the call.

3INMode

The parameter is an input one (the default). It can be combined with OUT to create a parameter that is both input and output.

4OUTMode

The parameter is used as a result of the function. It can be combined with IN.

5VARIADICMode

The function accepts a variable number of arguments of the parameter's type, received as an array. It must be the last parameter. This option is exclusive with IN and OUT: it is enabled only when both are unchecked and, when checked, they are disabled.

6TypeData type

The data type. Besides the built-in types, the list also holds the user-defined types, domains and other types of the model.

7DimensionData type

Number of dimensions when the type is an array (type[]). 0 means that the type is not an array.

8FormatData type

Read-only preview of the data type as it is written in the SQL code, with length, precision, dimension and other modifiers applied.

References​

Function DDL
https://www.postgresql.org/docs/current/static/sql-createfunction.html

Window functions
https://www.postgresql.org/docs/current/static/tutorial-window.html

Function parameters
https://www.postgresql.org/docs/current/static/xfunc-sql.html

Runtime settings
https://www.postgresql.org/docs/current/runtime-config.html