CREATE FUNCTION
Defines a new function.
Synopsis
CREATE [OR REPLACE] FUNCTION <name>
( [ [<argmode>] [<argname>] <argtype> [ { DEFAULT | = } <default_expr> ] [, ...] ] )
[ RETURNS <rettype>
| RETURNS TABLE ( <column_name> <column_type> [, ...] ) ]
{ LANGUAGE <langname>
| WINDOW
| IMMUTABLE | STABLE | VOLATILE | [NOT] LEAKPROOF
| CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
| NO SQL | CONTAINS SQL | READS SQL DATA | MODIFIES SQL
| [EXTERNAL] SECURITY INVOKER | [EXTERNAL] SECURITY DEFINER
| EXECUTE ON { ANY | MASTER | ALL SEGMENTS | INITPLAN }
| COST <execution_cost>
| ROWS <result_rows>
| SET <configuration_parameter> { TO <value> | = <value> | FROM CURRENT }
| AS '<definition>'
| AS '<obj_file>', '<link_symbol>' } ...
[ WITH ({ DESCRIBE = <describe_function>
} [, ...] ) ]
Description
CREATE FUNCTION defines a new function.
CREATE OR REPLACE FUNCTION either creates a new function, or replaces an existing definition.
The name of the new function must not match any existing function with the same input argument types in the same schema. However, functions of different argument types may share a name (overloading).
To update the definition of an existing function, use CREATE OR REPLACE FUNCTION.
It is not possible to change the name or argument types of a function this way (this would actually create a new, distinct function).
Also, CREATE OR REPLACE FUNCTION will not let you change the return type of an existing function.
To do that, you must drop and recreate the function.
When using OUT parameters, that means you cannot change the types of any OUT parameters except by dropping the function.
If you drop and then recreate a function, you will have to drop existing objects (rules, views, triggers, and so on) that refer to the old function.
Use CREATE OR REPLACE FUNCTION to change a function definition without breaking objects that refer to the function.
The user that creates the function becomes the owner of the function.
To be able to create a function, you must have the USAGE privilege on the argument types and the return type.
For more information about creating functions, see User Defined Functions in the PostgreSQL documentation.
Limited use of VOLATILE and STABLE functions
To prevent data from becoming out-of-sync across the segments in Greengage DB, any function classified as STABLE or VOLATILE cannot be run at the segment level if it contains SQL or modifies the database in any way.
For example, functions such as random() or timeofday() are not allowed to run on distributed data in Greengage DB because they could potentially cause inconsistent data between the segment instances.
To ensure data consistency, VOLATILE and STABLE functions can safely be used in statements that are evaluated on and run from the master.
For example, the following statements are always run on the master (statements without a FROM clause):
SELECT setval('myseq', 201);
SELECT foo();
In cases where a statement has a FROM clause containing a distributed table and the function used in the FROM clause simply returns a set of rows, execution may be allowed on the segments:
SELECT * FROM foo();
One exception to this rule are functions that return a table reference (rangeFuncs) or functions that use the refCursor data type.
Note that you cannot return a refcursor from any kind of function in Greengage DB.
Function volatility and EXECUTE ON attributes
Volatility attributes (IMMUTABLE, STABLE, VOLATILE) and EXECUTE ON attributes specify two different aspects of function execution.
In general, volatility indicates when the function is run, and EXECUTE ON indicates where it is run.
For example, a function defined with the IMMUTABLE attribute can be run at query planning time, while a function with the VOLATILE attribute must be run for every row in the query.
A function with the EXECUTE ON MASTER attribute is run only on the master segment and a function with the EXECUTE ON ALL SEGMENTS attribute is run on all primary segment instances (not the master).
Functions and replicated tables
A user-defined function that runs only SELECT commands on replicated tables can run on segments.
Replicated tables, created with the DISTRIBUTED REPLICATED clause, store all of their rows on every segment.
It is safe for a function to read them on the segments, but updates to replicated tables must run on the master instance.
Parameters
| Parameter | Description |
|---|---|
name |
The name (optionally schema-qualified) of the function to create |
argmode |
The mode of an argument: either |
argname |
The name of an argument. Some languages (currently only SQL and PL/pgSQL) let you use the name in the function body. For other languages the name of an input argument is just extra documentation, so far as the function itself is concerned; but you can use input argument names when calling a function to improve readability. In any case, the name of an output argument is significant, since it defines the column name in the result row type. If you omit the name for an output argument, the system will choose a default column name |
argtype |
The data types of the function’s arguments (optionally schema-qualified), if any.
The argument types may be base, composite, or domain types, or may reference the type of a table column.
Depending on the implementation language it may also be allowed to specify pseudotypes such as |
default_expr |
An expression to be used as the default value if the parameter is not specified.
The expression must be coercible to the argument type of the parameter.
Only |
rettype |
The return data type (optionally schema-qualified).
The return type can be a base, composite, or domain type, or may reference the type of a table column.
Depending on the implementation language it may also be allowed to specify pseudotypes such as |
column_name |
The name of an output column in the |
column_type |
The data type of an output column in the |
langname |
The name of the language that the function is implemented in.
May be |
WINDOW |
|
IMMUTABLE |
These attributes inform the query optimizer about the behavior of the function.
At most one choice may be specified.
If none of these appear, |
CALLED ON NULL INPUT |
|
NO SQL |
These attributes inform the query optimizer about whether or not the function contains SQL statements and whether, if it does, those statements read and/or write data |
[EXTERNAL] SECURITY INVOKER |
|
EXECUTE ON ANY |
The
Greengage DB does not support the
For information about using |
COST <execution_cost> |
A positive number identifying the estimated execution cost for the function, in cpu_operator_cost units.
If the function returns a set, |
ROWS <result_rows> |
A positive number giving the estimated number of rows that the planner should expect the function to return.
This is only allowed when the function is declared to return a set.
The default assumption is |
SET <configuration_parameter> { TO <value> | = <value> | FROM CURRENT } |
The |
definition |
A string constant defining the function; the meaning depends on the language. It may be an internal function name, the path to an object file, an SQL command, or text in a procedural language |
obj_file, link_symbol |
This form of the |
describe_function |
The name of a callback function to run when a query that calls this function is parsed. The callback function returns a tuple descriptor that indicates the result type |
Notes
Any compiled code (shared library files) for custom functions must be placed in the same location on every host in your Greengage DB cluster (master and all segments).
This location must also be in the LD_LIBRARY_PATH so that the server can locate the files.
It is recommended to locate shared libraries either relative to $libdir (which is located at $GPHOME/lib) or through the dynamic library path (set by the dynamic_library_path server configuration parameter) on all master segment instances in the Greengage DB array.
The full SQL type syntax is allowed for input arguments and return value.
However, some details of the type specification (such as the precision field for type numeric) are the responsibility of the underlying function implementation and are not recognized or enforced by the CREATE FUNCTION command.
Greengage DB allows function overloading. The same name can be used for several different functions so long as they have distinct input argument types. However, the C names of all functions must be different, so you must give overloaded C functions different C names (for example, use the argument types as part of the C names).
Two functions are considered the same if they have the same names and input argument types, ignoring any OUT parameters.
Thus, for example, these declarations conflict:
CREATE FUNCTION foo(int) ...
CREATE FUNCTION foo(int, out text) ...
Functions that have different argument type lists are not considered to conflict at creation time, but if argument defaults are provided, they might conflict in use. For example, consider:
CREATE FUNCTION foo(int) ...
CREATE FUNCTION foo(int, int default 42) ...
The call foo(10) will fail due to the ambiguity about which function should be called.
When repeated CREATE FUNCTION calls refer to the same object file, the file is only loaded once.
To unload and reload the file, use the LOAD command.
You must have the USAGE privilege on a language to be able to define a function using that language.
It is often helpful to use dollar quoting to write the function definition string, rather than the normal single quote syntax.
Without dollar quoting, any single quotes or backslashes in the function definition must be escaped by doubling them.
A dollar-quoted string constant consists of a dollar sign ($), an optional tag of zero or more characters, another dollar sign, an arbitrary sequence of characters that makes up the string content, a dollar sign, the same tag that began this dollar quote, and a dollar sign.
Inside the dollar-quoted string, single quotes, backslashes, or any character can be used without escaping.
The string content is always written literally.
For example, here are two different ways to specify the string "Dianne’s horse" using dollar quoting:
$$Dianne's horse$$ $SomeTag$Dianne's horse$SomeTag$
If a SET clause is attached to a function, the effects of a SET LOCAL command run inside the function for the same variable are restricted to the function; the configuration parameter’s prior value is still restored when the function exits.
However, an ordinary SET command (without LOCAL) overrides the CREATE FUNCTION SET clause, much as it would for a previous SET LOCAL command.
The effects of such a command will persist after the function exits, unless the current transaction is rolled back.
If a function with a VARIADIC argument is declared as STRICT, the strictness check tests that the variadic array as a whole is non-null.
PL/pgSQL will still call the function if the array has null elements.
When replacing an existing function with CREATE OR REPLACE FUNCTION, there are restrictions on changing parameter names.
You cannot change the name already assigned to any input parameter (although you can add names to parameters that had none before).
If there is more than one output parameter, you cannot change the names of the output parameters because that would change the column names of the anonymous composite type that describes the function’s result.
These restrictions are made to ensure that existing calls of the function do not stop working when it is replaced.
Using functions with queries on distributed data
In some cases, Greengage DB does not support using functions in a query where the data in a table specified in the FROM clause is distributed over Greengage DB segments.
As an example, this SQL query contains the function func():
SELECT func(a) FROM table1;
The function is not supported for use in the query if all of the following conditions are met:
-
The data of table
table1is distributed over Greengage DB segments. -
The function
func()reads or modifies data from distributed tables. -
The function
func()returns more than one row or takes an argument (a) that comes fromtable1.
If any of the conditions are not met, the function is supported. Specifically, the function is supported if any of the following conditions apply:
-
The function
func()does not access data from distributed tables, or accesses data that is only on the Greengage DB master. -
The table
table1is a master only table. -
The function
func()returns only one row and only takes input arguments that are constant values. -
The function is supported if it can be changed to require no input arguments.
Using EXECUTE ON attributes
Most functions that run queries to access tables can only run on the master.
However, functions that run only SELECT queries on replicated tables can run on segments.
If the function accesses a hash-distributed table or a randomly distributed table, the function should be defined with the EXECUTE ON MASTER attribute.
Otherwise, the function might return incorrect results when the function is used in a complicated query.
Without the attribute, planner optimization might determine it would be beneficial to push the function invocation to segment instances.
These are limitations for functions defined with the EXECUTE ON MASTER or EXECUTE ON ALL SEGMENTS attribute:
-
The function must be a set-returning function.
-
The function cannot be in the
FROMclause of a query. -
The function cannot be in the
SELECTlist of a query with aFROMclause. -
A query that includes the function falls back from GPORCA to the Postgres planner.
The attribute EXECUTE ON INITPLAN indicates that the function contains an SQL command that dispatches queries to the segment instances and requires special processing on the master instance by Greengage DB.
When possible, Greengage DB handles the function on the master instance in the following manner:
-
Greengage DB runs the function as part of an InitPlan node on the master instance and holds the function output temporarily.
-
In the MainPlan of the query plan, the function is called in an EntryDB (a special query executor (QE) that runs on the master instance) and Greengage DB returns the data that was captured when the function was run as part of the InitPlan node.
-
The function is not run in the MainPlan.
This example uses the function get_data() in a CTAS command to create a table using data from the table country.
The function contains a SELECT command that retrieves data from the table country and uses the EXECUTE ON INITPLAN attribute:
CREATE TABLE country
(
c_id integer,
c_name text,
region int
)
DISTRIBUTED RANDOMLY;
INSERT INTO country
VALUES (11, 'INDIA', 1),
(22, 'CANADA', 2),
(33, 'USA', 3);
CREATE OR REPLACE FUNCTION get_data()
RETURNS TABLE
(
c_id integer,
c_name text
)
AS
$$
SELECT c.c_id,
c.c_name
FROM country c;
$$
LANGUAGE SQL EXECUTE ON INITPLAN;
CREATE TABLE t AS
SELECT *
FROM get_data() DISTRIBUTED RANDOMLY;
If you view the query plan of the CTAS command with EXPLAIN ANALYZE VERBOSE, the plan shows that the function is run as part of an InitPlan node, and one of the listed slices is labeled as entry db.
The query plan of a simple CTAS command without the function does not have an InitPlan node or an entry db slice.
If the function did not contain the EXECUTE ON INITPLAN attribute, the CTAS command returns the error function cannot execute on a QE slice.
When a function uses the EXECUTE ON INITPLAN attribute, a command that uses the function such as CREATE TABLE t AS SELECT * FROM get_data() gathers the results of the function onto the master segment and then redistributes the results to segment instances when inserting the data.
If the function returns a large amount of data, the master might become a bottleneck when gathering and redistributing data.
Performance might improve if you rewrite the function to run the CTAS command in the user defined function and use the table name as an input parameter.
In this example, the function runs a CTAS command and does not require the EXECUTE ON INITPLAN attribute.
Running the SELECT command creates the table t1 using the function that runs the CTAS command:
CREATE OR REPLACE FUNCTION my_ctas(_tbl text) RETURNS VOID AS
$$
BEGIN
EXECUTE format('CREATE TABLE %s AS SELECT c.c_id, c.c_name FROM country c DISTRIBUTED RANDOMLY', _tbl);
END
$$
LANGUAGE plpgsql;
SELECT my_ctas('t1');
Examples
An addition function:
CREATE FUNCTION add(integer, integer)
RETURNS integer
AS
'select $1 + $2;'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
Increment an integer, making use of an argument name, in PL/pgSQL:
CREATE OR REPLACE FUNCTION increment(i integer)
RETURNS integer
AS
$$
BEGIN
RETURN i + 1;
END;
$$ LANGUAGE plpgsql;
Increase the default segment host memory per query for a PL/pgSQL function:
CREATE OR REPLACE FUNCTION function_with_query()
RETURNS SETOF text
AS
$$
BEGIN
RETURN QUERY
EXPLAIN ANALYZE SELECT * FROM large_table;
END;
$$ LANGUAGE plpgsql
SET statement_mem = '256MB';
Use polymorphic types to return an ENUM array:
CREATE TYPE rainbow AS ENUM ('red','orange','yellow','green','blue','indigo','violet');
CREATE FUNCTION return_enum_as_array(anyenum, anyelement, anyelement)
RETURNS TABLE
(
ae anyenum,
aa anyarray
)
AS
$$
SELECT $1, array [$2, $3]
$$ LANGUAGE SQL STABLE;
SELECT *
FROM return_enum_as_array('red'::rainbow, 'green'::rainbow, 'blue'::rainbow);
Return a record containing multiple output parameters:
CREATE FUNCTION dup(in int, out f1 int, out f2 text)
AS
$$
SELECT $1, CAST($1 AS text) || ' is text'
$$
LANGUAGE SQL;
SELECT * FROM dup(42);
You can do the same thing more verbosely with an explicitly named composite type:
CREATE TYPE dup_result AS
(
f1 int,
f2 text
);
CREATE FUNCTION dup(int)
RETURNS dup_result
AS
$$
SELECT $1, CAST($1 AS text) || ' is text'
$$
LANGUAGE SQL;
SELECT * FROM dup(42);
Another way to return multiple columns is to use a TABLE function:
CREATE FUNCTION dup(int)
RETURNS TABLE
(
f1 int,
f2 text
)
AS
$$
SELECT $1, CAST($1 AS text) || ' is text'
$$
LANGUAGE SQL;
SELECT * FROM dup(4);
This function is defined with EXECUTE ON ALL SEGMENTS to run on all primary segment instances.
The SELECT command runs the function that returns the time it was run on each segment instance.
CREATE FUNCTION run_on_segs(text)
returns setof text
as
$$
begin
return next ($1 || ' - ' || now()::text);
end;
$$ language plpgsql VOLATILE
EXECUTE ON ALL SEGMENTS;
SELECT run_on_segs('my test');
This function looks up a part name in the parts table.
The parts table is replicated, so the function can run on the master or on the primary segments.
CREATE OR REPLACE FUNCTION get_part_name(partno int)
RETURNS text
AS
$$
DECLARE
result text := ' ';
BEGIN
SELECT part_name INTO result FROM parts WHERE part_id = partno;
RETURN result;
END;
$$ LANGUAGE plpgsql;
If you run SELECT get_part_name(100); at the master, the function runs on the master.
The master instance directs the query to a single primary segment.
If orders is a distributed table, and you run the following query, the get_part_name() function runs on the primary segments:
SELECT order_id, get_part_name(orders.part_no) FROM orders;
Compatibility
CREATE FUNCTION is defined in SQL:1999 and later.
The Greengage DB version is similar but not fully compatible.
The attributes are not portable, neither are the different available languages.
For compatibility with some other database systems, argmode can be written either before or after argname.
But only the first way is standard-compliant.
For parameter defaults, the SQL standard specifies only the syntax with the DEFAULT keyword.
The syntax with = is used in T-SQL and Firebird.