RefLib

Reference library for AI

Back

postgresql_16:6.1.5.3 SQL Functions on Composite Types

PostgreSQL 16 > Server Programming > Extending SQL > Query Language (SQL) Functions > SQL Functions on Composite Types

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 that emp 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 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

Notice the use of the syntax $1.salary to select one field of the argument row value. Also notice how the calling SELECT command uses *table_name*.* 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: SELECT name, double_salary(emp) AS dream FROM emp WHERE emp.cubicle ~= point '(2,1)'; but this usage is deprecated since it's easy to get confused. (See (see postgresql_16:3.5.16.5) for details about these two notations for the composite value of a table row.)

Sometimes it is handy to construct a composite argument value on-the-fly. This can be done with the ROW construct. For example, we could adjust the data being passed to the function: SELECT name, double_salary(ROW(name, salary*1.1, age, cubicle)) AS dream FROM emp;

It is also possible to build a function that returns a composite type. This is an example of a function that returns a single emp row: 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; 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 composite type. (Naming the columns, as we did above, is irrelevant to the system.) We must ensure each expression's type can be cast to that of the corresponding column of the composite type. Otherwise we'll get errors like this: ERROR: return type mismatch in function declared to return emp DETAIL: Final statement returns text instead of point at column 4. As with the base-type case, the system will not insert explicit casts automatically, only implicit or assignment casts.

A different way to define the same function is: CREATE FUNCTION new_emp() RETURNS emp AS $$ SELECT ROW('None', 1000.0, 25, '(2,2)')::emp; $$ LANGUAGE SQL; Here we wrote a SELECT 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. Another example is that if we are trying to write a function that returns a domain over composite, rather than a plain composite type, it is always necessary to write it as returning a single column, since there is no way to cause a coercion of the whole row result.

We could call this function directly either by using it in a value expression: SELECT new_emp(); new_emp -------------------------- (None,1000.0,25,"(2,2)") or by calling it as a table function: SELECT * FROM new_emp(); name | salary | age | cubicle ------+--------+-----+--------- None | 1000.0 | 25 | (2,2) The second way is described more fully in (see postgresql_16:6.1.5.8).

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: SELECT (new_emp()).name; name ------ None 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: SELECT new_emp().name; ERROR: syntax error at or near "." LINE 1: SELECT new_emp().name; ^

Another option is to use functional notation for extracting an attribute: SELECT name(new_emp()); name ------ None As explained in (see postgresql_16:3.5.16.5), the field notation and functional notation are equivalent.

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: CREATE FUNCTION getname(emp) RETURNS text AS $$ SELECT $1.name; $$ LANGUAGE SQL; SELECT getname(new_emp()); getname --------- None (1 row)

Back