Sub topic 11
Polymorphic Functions
Prerequisites: you should know how to create SQL functions, use PostgreSQL data types, and call functions with SELECT. You should also understand that a function's return type is part of its definition.
A polymorphic function is a function whose input and output types are chosen from the types used at each call. The function definition uses special pseudo-types instead of one fixed data type. This lets one function work with integers, text, or other compatible types.
Polymorphic types are constraints, not storage types. You cannot create a table column with type anyelement. PostgreSQL resolves these types when it parses a function call.
Linking input and output types
The older polymorphic family includes anyelement and anyarray. They are linked:
anyelementrepresents one arbitrary type.anyarrayrepresents an array whose element type is that same arbitrary type.- Every argument using
anyelementmust resolve to the same type. - The result must satisfy the relationship declared by the function.
This function accepts two values of one type and returns an array of that type:
CREATE FUNCTION make_array(anyelement, anyelement)
RETURNS anyarray
LANGUAGE sql
IMMUTABLE
AS $$
SELECT ARRAY[$1, $2];
$$;
SELECT make_array(1, 2);
SELECT make_array('a'::text, 'b'::text);The first call returns an integer array, such as {1,2}. The second returns a text array, such as {a,b}. The positional references $1 and $2 refer to the first and second arguments.
The two arguments must resolve to one common anyelement type. This call is therefore usually rejected:
SELECT make_array(1, 'two');An integer and an unknown string literal do not automatically provide a single valid element type for this function. Cast values explicitly when type resolution is unclear:
SELECT make_array(1::text, 'two'::text);`anycompatible` types
PostgreSQL also provides an anycompatible family, including anycompatiblearray. These types allow PostgreSQL to find a common type for compatible arguments instead of requiring exactly the same type. Built-in functions such as array_cat use this approach in supported PostgreSQL versions.
Use anyelement when all arguments must have one exact type. Use anycompatible when PostgreSQL should promote compatible types to a shared type. The choice affects both accepted calls and the type returned to the caller.
| Polymorphic family | Type matching behavior | Typical use |
|---|---|---|
| `anyelement` and `anyarray` | Arguments resolve to the same type | Constructing an array from same-typed values |
| `anycompatible` and `anycompatiblearray` | Arguments can resolve to a common compatible type | Combining values or arrays with type promotion |
Common errors
A polymorphic function needs enough type information at the call site. An untyped NULL does not provide that information:
SELECT make_array(NULL, NULL);Cast at least the arguments so PostgreSQL can determine the element type:
SELECT make_array(NULL::integer, NULL::integer);Declare volatility according to the implementation. The example is IMMUTABLE because its result depends only on its arguments and array construction. A polymorphic function that reads a table should usually be STABLE instead. Polymorphism does not make a function safe to optimize or independent of database state.
Exercise
What result type does make_array(10::bigint, 20::bigint) have, and why?
Show answer
It has type bigint[]. Both arguments resolve anyelement to bigint, so anyarray resolves to an array whose element type is bigint.