September 26, 2024: PostgreSQL 17 Released!
Supported Versions: Current (17) / 16 / 15 / 14 / 13 / 12
Development Versions: devel
Unsupported versions: 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

5.3. Functions

Function Call Type Resolution

  1. Check for an exact match in the pg_proc system catalog. (Cases involving unknown will never find a match at this step.)

  2. Look for the best match.

    1. Make a list of all functions of the same name with the same number of arguments for which the input types match or can be coerced to match. (unknown literals are assumed to be coercible to anything for this purpose.) If there is only one, use it; else continue to the next step.

    2. Run through all candidates and keep those with the most exact matches on input types. Keep all candidates if none have any exact matches. If only one candidate remains, use it; else continue to the next step.

    3. Run through all candidates and keep those with the most exact or binary-compatible matches on input types. Keep all candidates if none have any exact or binary-compatible matches. If only one candidate remains, use it; else continue to the next step.

    4. Run through all candidates and keep those that accept preferred types at the most positions where type coercion will be required. Keep all candidates if none accept preferred types. If only one candidate remains, use it; else continue to the next step.

    5. If any input arguments are "unknown", check the type categories accepted at those argument positions by the remaining candidates. At each position, select "string" category if any candidate accepts that category (this bias towards string is appropriate since an unknown-type literal does look like a string). Otherwise, if all the remaining candidates accept the same type category, select that category; otherwise fail because the correct choice cannot be deduced without more clues. Also note whether any of the candidates accept a preferred datatype within the selected category. Now discard operator candidates that do not accept the selected type category; furthermore, if any candidate accepts a preferred type at a given argument position, discard candidates that accept non-preferred types for that argument.

    6. If only one candidate remains, use it. If no candidate or more than one candidate remains, then fail.

  3. If no best match could be identified, see whether the function call appears to be a trivial type coercion request. This happens if the function call has just one argument and the function name is the same as the (internal) name of some datatype. Furthermore, the function argument must be either an unknown-type literal or a type that is binary-compatible with the named datatype. When these conditions are met, the function argument is coerced to the named datatype.

5.3.1. Examples

5.3.1.1. Factorial Function

There is only one factorial function defined in the pg_proc catalog. So the following query automatically converts the int2 argument to int4:

tgl=> select int4fac(int2 '4');
 int4fac
---------
      24
(1 row)
and is actually transformed by the parser to
tgl=> select int4fac(int4(int2 '4'));
 int4fac
---------
      24
(1 row)

5.3.1.2. Substring Function

There are two substr functions declared in pg_proc. However, only one takes two arguments, of types text and int4.

If called with a string constant of unspecified type, the type is matched up directly with the only candidate function type:

tgl=> select substr('1234', 3);
 substr
--------
     34
(1 row)

If the string is declared to be of type varchar, as might be the case if it comes from a table, then the parser will try to coerce it to become text:

tgl=> select substr(varchar '1234', 3);
 substr
--------
     34
(1 row)
which is transformed by the parser to become
tgl=> select substr(text(varchar '1234'), 3);
 substr
--------
     34
(1 row)

Note: Actually, the parser is aware that text and varchar are "binary compatible", meaning that one can be passed to a function that accepts the other without doing any physical conversion. Therefore, no explicit type conversion call is really inserted in this case.

And, if the function is called with an int4, the parser will try to convert that to text:

tgl=> select substr(1234, 3);
 substr
--------
     34
(1 row)
actually executes as
tgl=> select substr(text(1234), 3);
 substr
--------
     34
(1 row)
This succeeds because there is a conversion function text(int4) in the system catalog.