PostgreSQL 9.4.1 Documentation | |||
---|---|---|---|
Prev | Up | Chapter 35. ExtendingSQL | Next |
35.4. Query Language (SQL) Functions
SQL functions execute an arbitrary list of SQL statements, returning the result of the last query in the list. In the simple (non-set) case, the first row of the last query's result will be returned. (Bear in mind that"the first row" of a multirow result is not well-defined unless you useORDER BY.) If the last query happens to return no rows at all, the null value will be returned.
Alternatively, an SQL function can be declared to return a set (that is, multiple rows) by specifying the function's return type asSETOFsometype, or equivalently by declaring it asRETURNS TABLE(columns). In this case all rows of the last query's result are returned. Further details appear below.
The body of an SQL function must be a list of SQL statements separated by semicolons. A semicolon after the last statement is optional. Unless the function is declared to returnvoid, the last statement must be aSELECT, or anINSERT,UPDATE, orDELETE that has aRETURNING clause.
Any collection of commands in theSQL language can be packaged together and defined as a function. BesidesSELECT queries, the commands can include data modification queries (INSERT,UPDATE, andDELETE), as well as other SQL commands. (You cannot use transaction control commands, e.g.COMMIT,SAVEPOINT, and some utility commands, e.g.VACUUM, inSQL functions.) However, the final command must be aSELECT or have aRETURNING clause that returns whatever is specified as the function's return type. Alternatively, if you want to define a SQL function that performs actions but has no useful value to return, you can define it as returningvoid. For example, this function removes rows with negative salaries from theemp table: Note: The entire body of a SQL function is parsed before any of it is executed. While a SQL function can contain commands that alter the system catalogs (e.g.,CREATE TABLE), the effects of such commands will not be visible during parse analysis of later commands in the function. Thus, for example,CREATE TABLE foo (...); INSERT INTO foo VALUES(...); will not work as desired if packaged up into a single SQL function, sincefoo won't exist yet when theINSERT command is parsed. It's recommended to usePL/PgSQL instead of a SQL function in this type of situation. The syntax of theCREATE FUNCTION command requires the function body to be written as a string constant. It is usually most convenient to use dollar quoting (seeSection 4.1.2.4) for the string constant. If you choose to use regular single-quoted string constant syntax, you must double single quote marks (') and backslashes (\) (assuming escape string syntax) in the body of the function (seeSection 4.1.2.1). Arguments of a SQL function can be referenced in the function body using either names or numbers. Examples of both methods appear below. To use a name, declare the function argument as having a name, and then just write that name in the function body. If the argument name is the same as any column name in the current SQL command within the function, the column name will take precedence. To override this, qualify the argument name with the name of the function itself, that isfunction_name.argument_name. (If this would conflict with a qualified column name, again the column name wins. You can avoid the ambiguity by choosing a different alias for the table within the SQL command.) In the older numeric approach, arguments are referenced using the syntax$n:$1 refers to the first input argument,$2 to the second, and so on. This will work whether or not the particular argument was declared with a name. If an argument is of a composite type, then the dot notation, e.g.,argname.fieldname or$1.fieldname, can be used to access attributes of the argument. Again, you might need to qualify the argument's name with the function name to make the form with an argument name unambiguous. SQL function arguments can only be used as data values, not as identifiers. Thus for example this is reasonable: but this will not work: Note: The ability to use names to reference SQL function arguments was added inPostgreSQL 9.2. Functions to be used in older servers must use the$n notation. When writing functions with arguments of composite types, we must not only specify which argument we want but also the desired attribute (field) of that argument. For example, suppose thatemp is a table containing employee data, and therefore also the name of the composite type of each row of the table. Here is a function Notice the use of the syntax$1.salary to select one field of the argument row value. Also notice how the callingSELECT command uses* to select the entire current row of a table as a composite value. The table row can alternatively be referenced using just the table name, like this: but this usage is deprecated since it's easy to get confused. Sometimes it is handy to construct a composite argument value on-the-fly. This can be done with theROW construct. For example, we could adjust the data being passed to the function: It is also possible to build a function that returns a composite type. This is an example of a function that returns a singleemp row: In this example we have specified each of the attributes with a constant value, but any computation could have been substituted for these constants. Note two important things about defining the function: The select list order in the query must be exactly the same as that in which the columns appear in the table associated with the composite type. (Naming the columns, as we did above, is irrelevant to the system.) You must typecast the expressions to match the definition of the composite type, or you will get errors like this: A different way to define the same function is: Here we wrote aSELECT that returns just a single column of the correct composite type. This isn't really better in this situation, but it is a handy alternative in some cases — for example, if we need to compute the result by calling another function that returns the desired composite value. We could call this function directly in either of two ways: The second way is described more fully inSection 35.4.7. When you use a function that returns a composite type, you might want only one field (attribute) from its result. You can do that with syntax like this: The extra parentheses are needed to keep the parser from getting confused. If you try to do it without them, you get something like this: Another option is to use functional notation for extracting an attribute. The simple way to explain this is that we can use the notationsattribute(table) andtable.attribute interchangeably. Tip: The equivalence between functional notation and attribute notation makes it possible to use functions on composite types to emulate"computed fields". For example, using the previous definition fordouble_salary(emp), we can write An application using this wouldn't need to be directly aware thatdouble_salary isn't a real column of the table. (You can also emulate computed fields with views.) Because of this behavior, it's unwise to give a function that takes a single composite-type argument the same name as any of the fields of that composite type. Another way to use a function returning a composite type is to pass the result to another function that accepts the correct row type as input: Still another way to use a function that returns a composite type is to call it as a table function, as described inSection 35.4.7. An alternative way of describing a function's results is to define it withoutput parameters, as in this example: This is not essentially different from the version ofadd_em shown inSection 35.4.2. The real value of output parameters is that they provide a convenient way of defining functions that return several columns. For example, What has essentially happened here is that we have created an anonymous composite type for the result of the function. The above example has the same end result as but not having to bother with the separate composite type definition is often handy. Notice that the names attached to the output parameters are not just decoration, but determine the column names of the anonymous composite type. (If you omit a name for an output parameter, the system will choose a name on its own.) Notice that output parameters are not included in the calling argument list when invoking such a function from SQL. This is becausePostgreSQL considers only the input parameters to define the function's calling signature. That means also that only the input parameters matter when referencing the function for purposes such as dropping it. We could drop the above function with either of Parameters can be marked asIN (the default),OUT,INOUT, orVARIADIC. AnINOUT parameter serves as both an input parameter (part of the calling argument list) and an output parameter (part of the result record type).VARIADIC parameters are input parameters, but are treated specially as described next. SQL functions can be declared to accept variable numbers of arguments, so long as all the"optional" arguments are of the same data type. The optional arguments will be passed to the function as an array. The function is declared by marking the last parameter asVARIADIC; this parameter must be declared as being of an array type. For example: Effectively, all the actual arguments at or beyond theVARIADIC position are gathered up into a one-dimensional array, as if you had written You can't actually write that, though — or at least, it will not match this function definition. A parameter markedVARIADIC matches one or more occurrences of its element type, not of its own type. Sometimes it is useful to be able to pass an already-constructed array to a variadic function; this is particularly handy when one variadic function wants to pass on its array parameter to another one. You can do that by specifyingVARIADIC in the call: This prevents expansion of the function's variadic parameter into its element type, thereby allowing the array argument value to match normally.VARIADIC can only be attached to the last actual argument of a function call. SpecifyingVARIADIC in the call is also the only way to pass an empty array to a variadic function, for example: Simply writingSELECT mleast() does not work because a variadic parameter must match at least one actual argument. (You could define a second function also namedmleast, with no parameters, if you wanted to allow such calls.) The array element parameters generated from a variadic parameter are treated as not having any names of their own. This means it is not possible to call a variadic function using named arguments (Section 4.3), except when you specifyVARIADIC. For example, this will work: but not these: Functions can be declared with default values for some or all input arguments. The default values are inserted whenever the function is called with insufficiently many actual arguments. Since arguments can only be omitted from the end of the actual argument list, all parameters after a parameter with a default value have to have default values as well. (Although the use of named argument notation could allow this restriction to be relaxed, it's still enforced so that positional argument notation works sensibly.) For example: The= sign can also be used in place of the key wordDEFAULT. All SQL functions can be used in theFROM clause of a query, but it is particularly useful for functions returning composite types. If the function is defined to return a base type, the table function produces a one-column table. If the function is defined to return a composite type, the table function produces a column for each attribute of the composite type. Here is an example: As the example shows, we can work with the columns of the function's result just the same as if they were columns of a regular table. Note that we only got one row out of the function. This is because we did not useSETOF. That is described in the next section. When an SQL function is declared as returningSETOFsometype, the function's final query is executed to completion, and each row it outputs is returned as an element of the result set. This feature is normally used when calling the function in theFROM clause. In this case each row returned by the function becomes a row of the table seen by the query. For example, assume that tablefoo has the same contents as above, and we say: Then we would get: It is also possible to return multiple rows with the columns defined by output parameters, like this: The key point here is that you must writeRETURNS SETOF record to indicate that the function returns multiple rows instead of just one. If there is only one output parameter, write that parameter's type instead ofrecord. It is frequently useful to construct a query's result by invoking a set-returning function multiple times, with the parameters for each invocation coming from successive rows of a table or subquery. The preferred way to do this is to use theLATERAL key word, which is described inSection 7.2.1.5. Here is an example using a set-returning function to enumerate elements of a tree structure: This example does not do anything that we couldn't have done with a simple join, but in more complex calculations the option to put some of the work into a function can be quite convenient. Currently, functions returning sets can also be called in the select list of a query. For each row that the query generates by itself, the function returning set is invoked, and an output row is generated for each element of the function's result set. Note, however, that this capability is deprecated and might be removed in future releases. The previous example could also be done with queries like these: In the lastSELECT, notice that no output row appears forChild2,Child3, etc. This happens because Note: If a function's last command isINSERT,UPDATE, orDELETE withRETURNING, that command will always be executed to completion, even if the function is not declared withSETOF or the calling query does not fetch all the result rows. Any extra rows produced by theRETURNING clause are silently dropped, but the commanded table modifications still happen (and are all completed before returning from the function). Note: The key problem with using set-returning functions in the select list, rather than theFROM clause, is that putting more than one set-returning function in the same select list does not behave very sensibly. (What you actually get if you do so is a number of output rows equal to the least common multiple of the numbers of rows produced by each set-returning function.) TheLATERAL syntax produces less surprising results when calling multiple set-returning functions, and should usually be used instead. There is another way to declare a function as returning a set, which is to use the syntaxRETURNS TABLE(columns). This is equivalent to using one or moreOUT parameters plus marking the function as returningSETOF record (orSETOF a single output parameter's type, as appropriate). This notation is specified in recent versions of the SQL standard, and thus may be more portable than usingSETOF. For example, the preceding sum-and-product example could also be done this way: It is not allowed to use explicitOUT orINOUT parameters with theRETURNS TABLE notation — you must put all the output columns in theTABLE list. SQL functions can be declared to accept and return the polymorphic typesanyelement,anyarray,anynonarray,anyenum, andanyrange. SeeSection 35.2.5 for a more detailed explanation of polymorphic functions. Here is a polymorphic function Notice the use of the typecast'a'::text to specify that the argument is of typetext. This is required if the argument is just a string literal, since otherwise it would be treated as typeunknown, and array ofunknown is not a valid type. Without the typecast, you will get errors like this: It is permitted to have polymorphic arguments with a fixed return type, but the converse is not. For example: Polymorphism can be used with functions that have output arguments. For example: Polymorphism can also be used with variadic functions. For example: When a SQL function has one or more parameters of collatable data types, a collation is identified for each function call depending on the collations assigned to the actual arguments, as described inSection 22.2. If a collation is successfully identified (i.e., there are no conflicts of implicit collations among the arguments) then all the collatable parameters are treated as having that collation implicitly. This will affect the behavior of collation-sensitive operations within the function. For example, using the will depend on the database's default collation. InC locale the result will beABC, but in many other locales it will beabc. The collation to use can be forced by adding aCOLLATE clause to any of the arguments, for example Alternatively, if you wish a function to operate with a particular collation regardless of what it is called with, insertCOLLATE clauses as needed in the function definition. This version of But note that this will throw an error if applied to a non-collatable data type. If no common collation can be identified among the actual arguments, then a SQL function treats its parameters as having their data types' default collation (which is usually the database's default collation, but could be different for parameters of domain types). The behavior of collatable parameters can be thought of as a limited form of polymorphism, applicable only to textual data types.CREATE FUNCTION clean_emp() RETURNS void AS ' DELETE FROM emp WHERE salary < 0;' LANGUAGE SQL;SELECT clean_emp(); clean_emp-----------(1 row)
35.4.1. Arguments forSQL Functions
INSERT INTO mytable VALUES ($1);
INSERT INTO $1 VALUES (42);
35.4.2.SQL Functions on Base Types
35.4.3.SQL Functions on Composite Types
double_salary
that computes what someone's salary would be if it were doubled:CREATE TABLE emp ( name text, salary numeric, age integer, cubicle point);INSERT INTO emp VALUES ('Bill', 4200, 45, '(2,1)');CREATE FUNCTION double_salary(emp) RETURNS numeric AS $$ SELECT $1.salary * 2 AS salary;$$ LANGUAGE SQL;SELECT name, double_salary(emp.*) AS dream FROM emp WHERE emp.cubicle ~= point '(2,1)'; name | dream------+------- Bill | 8400
SELECT name, double_salary(emp) AS dream FROM emp WHERE emp.cubicle ~= point '(2,1)';
SELECT name, double_salary(ROW(name, salary*1.1, age, cubicle)) AS dream FROM emp;
CREATE FUNCTION new_emp() RETURNS emp AS $$ SELECT text 'None' AS name, 1000.0 AS salary, 25 AS age, point '(2,2)' AS cubicle;$$ LANGUAGE SQL;
ERROR: function declared to return emp returns varchar instead of text at column 1
CREATE FUNCTION new_emp() RETURNS emp AS $$ SELECT ROW('None', 1000.0, 25, '(2,2)')::emp;$$ LANGUAGE SQL;
SELECT new_emp(); new_emp-------------------------- (None,1000.0,25,"(2,2)")SELECT * FROM new_emp(); name | salary | age | cubicle------+--------+-----+--------- None | 1000.0 | 25 | (2,2)
SELECT (new_emp()).name; name------ None
SELECT new_emp().name;ERROR: syntax error at or near "."LINE 1: SELECT new_emp().name; ^
SELECT name(new_emp()); name------ None
-- This is the same as:-- SELECT emp.name AS youngster FROM emp WHERE emp.age < 30;SELECT name(emp) AS youngster FROM emp WHERE age(emp) < 30; youngster----------- Sam Andy
SELECT emp.name, emp.double_salary FROM emp;
CREATE FUNCTION getname(emp) RETURNS text AS $$ SELECT $1.name;$$ LANGUAGE SQL;SELECT getname(new_emp()); getname--------- None(1 row)
35.4.4.SQL Functions with Output Parameters
CREATE FUNCTION add_em (IN x int, IN y int, OUT sum int)AS 'SELECT x + y'LANGUAGE SQL;SELECT add_em(3,7); add_em-------- 10(1 row)
CREATE FUNCTION sum_n_product (x int, y int, OUT sum int, OUT product int)AS 'SELECT x + y, x * y'LANGUAGE SQL; SELECT * FROM sum_n_product(11,42); sum | product-----+--------- 53 | 462(1 row)
CREATE TYPE sum_prod AS (sum int, product int);CREATE FUNCTION sum_n_product (int, int) RETURNS sum_prodAS 'SELECT $1 + $2, $1 * $2'LANGUAGE SQL;
DROP FUNCTION sum_n_product (x int, y int, OUT sum int, OUT product int);DROP FUNCTION sum_n_product (int, int);
35.4.5.SQL Functions with Variable Numbers of Arguments
CREATE FUNCTION mleast(VARIADIC arr numeric[]) RETURNS numeric AS $$ SELECT min($1[i]) FROM generate_subscripts($1, 1) g(i);$$ LANGUAGE SQL;SELECT mleast(10, -1, 5, 4.4); mleast -------- -1(1 row)
SELECT mleast(ARRAY[10, -1, 5, 4.4]); -- doesn't work
SELECT mleast(VARIADIC ARRAY[10, -1, 5, 4.4]);
SELECT mleast(VARIADIC ARRAY[]::numeric[]);
SELECT mleast(VARIADIC arr := ARRAY[10, -1, 5, 4.4]);
SELECT mleast(arr := 10);SELECT mleast(arr := ARRAY[10, -1, 5, 4.4]);
35.4.6.SQL Functions with Default Values for Arguments
CREATE FUNCTION foo(a int, b int DEFAULT 2, c int DEFAULT 3)RETURNS intLANGUAGE SQLAS $$ SELECT $1 + $2 + $3;$$;SELECT foo(10, 20, 30); foo ----- 60(1 row)SELECT foo(10, 20); foo ----- 33(1 row)SELECT foo(10); foo ----- 15(1 row)SELECT foo(); -- fails since there is no default for the first argumentERROR: function foo() does not exist
35.4.7.SQL Functions as Table Sources
CREATE TABLE foo (fooid int, foosubid int, fooname text);INSERT INTO foo VALUES (1, 1, 'Joe');INSERT INTO foo VALUES (1, 2, 'Ed');INSERT INTO foo VALUES (2, 1, 'Mary');CREATE FUNCTION getfoo(int) RETURNS foo AS $$ SELECT * FROM foo WHERE fooid = $1;$$ LANGUAGE SQL;SELECT *, upper(fooname) FROM getfoo(1) AS t1; fooid | foosubid | fooname | upper-------+----------+---------+------- 1 | 1 | Joe | JOE(1 row)
35.4.8.SQL Functions Returning Sets
CREATE FUNCTION getfoo(int) RETURNS SETOF foo AS $$ SELECT * FROM foo WHERE fooid = $1;$$ LANGUAGE SQL;SELECT * FROM getfoo(1) AS t1;
fooid | foosubid | fooname-------+----------+--------- 1 | 1 | Joe 1 | 2 | Ed(2 rows)
CREATE TABLE tab (y int, z int);INSERT INTO tab VALUES (1, 2), (3, 4), (5, 6), (7, 8);CREATE FUNCTION sum_n_product_with_tab (x int, OUT sum int, OUT product int)RETURNS SETOF recordAS $$ SELECT $1 + tab.y, $1 * tab.y FROM tab;$$ LANGUAGE SQL;SELECT * FROM sum_n_product_with_tab(10); sum | product-----+--------- 11 | 10 13 | 30 15 | 50 17 | 70(4 rows)
SELECT * FROM nodes; name | parent-----------+-------- Top | Child1 | Top Child2 | Top Child3 | Top SubChild1 | Child1 SubChild2 | Child1(6 rows)CREATE FUNCTION listchildren(text) RETURNS SETOF text AS $$ SELECT name FROM nodes WHERE parent = $1$$ LANGUAGE SQL STABLE;SELECT * FROM listchildren('Top'); listchildren-------------- Child1 Child2 Child3(3 rows)SELECT name, child FROM nodes, LATERAL listchildren(name) AS child; name | child--------+----------- Top | Child1 Top | Child2 Top | Child3 Child1 | SubChild1 Child1 | SubChild2(5 rows)
SELECT listchildren('Top'); listchildren-------------- Child1 Child2 Child3(3 rows)SELECT name, listchildren(name) FROM nodes; name | listchildren--------+-------------- Top | Child1 Top | Child2 Top | Child3 Child1 | SubChild1 Child1 | SubChild2(5 rows)
listchildren
returns an empty set for those arguments, so no result rows are generated. This is the same behavior as we got from an inner join to the function result when using theLATERAL syntax.35.4.9.SQL Functions ReturningTABLE
CREATE FUNCTION sum_n_product_with_tab (x int)RETURNS TABLE(sum int, product int) AS $$ SELECT $1 + tab.y, $1 * tab.y FROM tab;$$ LANGUAGE SQL;
35.4.10. PolymorphicSQL Functions
make_array
that builds up an array from two arbitrary data type elements:CREATE FUNCTION make_array(anyelement, anyelement) RETURNS anyarray AS $$ SELECT ARRAY[$1, $2];$$ LANGUAGE SQL;SELECT make_array(1, 2) AS intarray, make_array('a'::text, 'b') AS textarray; intarray | textarray----------+----------- {1,2} | {a,b}(1 row)
ERROR: could not determine polymorphic type because input has type "unknown"
CREATE FUNCTION is_greater(anyelement, anyelement) RETURNS boolean AS $$ SELECT $1 > $2;$$ LANGUAGE SQL;SELECT is_greater(1, 2); is_greater------------ f(1 row)CREATE FUNCTION invalid_func() RETURNS anyelement AS $$ SELECT 1;$$ LANGUAGE SQL;ERROR: cannot determine result data typeDETAIL: A function returning a polymorphic type must have at least one polymorphic argument.
CREATE FUNCTION dup (f1 anyelement, OUT f2 anyelement, OUT f3 anyarray)AS 'select $1, array[$1,$1]' LANGUAGE SQL;SELECT * FROM dup(22); f2 | f3----+--------- 22 | {22,22}(1 row)
CREATE FUNCTION anyleast (VARIADIC anyarray) RETURNS anyelement AS $$ SELECT min($1[i]) FROM generate_subscripts($1, 1) g(i);$$ LANGUAGE SQL;SELECT anyleast(10, -1, 5, 4); anyleast ---------- -1(1 row)SELECT anyleast('abc'::text, 'def'); anyleast ---------- abc(1 row)CREATE FUNCTION concat_values(text, VARIADIC anyarray) RETURNS text AS $$ SELECT array_to_string($2, $1);$$ LANGUAGE SQL;SELECT concat_values('|', 1, 4, 2); concat_values --------------- 1|4|2(1 row)
35.4.11.SQL Functions with Collations
anyleast
function described above, the result ofSELECT anyleast('abc'::text, 'ABC');
SELECT anyleast('abc'::text, 'ABC' COLLATE "C");
anyleast
would always useen_US locale to compare strings:CREATE FUNCTION anyleast (VARIADIC anyarray) RETURNS anyelement AS $$ SELECT min($1[i] COLLATE "en_US") FROM generate_subscripts($1, 1) g(i);$$ LANGUAGE SQL;