Postgres 13, new family of polymorphic types - anycompatible
One very interesting PostgerSQL's feature are polymorphic types. It's interesting mainly for people who writes extensions.
Example: I can write own
isnull
function:CREATE OR REPLACE FUNCTION isnull(anyelement, anyelement) RETURNS anyelement AS $$ SELECT coalesce($1, $2) $$ LANGUAGE sql; postgres=# SELECT public.isnull(NULL, 1); ┌────────┐ │ isnull │ ╞════════╡ │ 1 │ └────────┘ (1 row) postgres=# SELECT public.isnull(NULL, CURRENT_DATE); ┌────────────┐ │ isnull │ ╞════════════╡ │ 2020-03-31 │ └────────────┘ (1 row) -- but postgres=# SELECT public.isnull(1, 1.1); ERROR: function public.isnull(integer, numeric) does not exist LINE 1: SELECT public.isnull(1, 1.1); ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
It is working for all supported types. One and big limit of type family
any*
is rigidity. It is working for all types, but for previous example, first and second parameter has to have exactly same types. Similar buildin functions are more tolerant - usually requires same type category only. This limit should be removed with new family of polymorphic types anycompatible
. When there are more arguments with this type, then most common type is calculated and all arguments of anycompatible
are casted to selected most common type:CREATE OR REPLACE FUNCTION isnull(anycompatible, anycompatible) RETURNS anycompatible AS $$ SELECT coalesce($1, $2) $$ LANGUAGE sql; postgres=# SELECT public.isnull(1, 1.1); ┌────────┐ │ isnull │ ╞════════╡ │ 1 │ └────────┘ (1 row) postgres=# SELECT public.isnull(NULL, 1.1); ┌────────┐ │ isnull │ ╞════════╡ │ 1.1 │ └────────┘ (1 row)
Now, it is working as expected.
This feature is interesting mainly for authors of extensions that has to emulate some different databases (like Orafce or similar).