10.3. Functions

The specific function to be used in a function invocation is determined according to the following steps.

Function Type Resolution

  1. Select the functions to be considered from the pg_proc system catalog. If an unqualified function name was used, the functions considered are those of the right name and argument count that are visible in the current search path (see Seção 5.7.3). If a qualified function name was given, only functions in the specified schema are considered.

    1. If the search path finds multiple functions of identical argument types, only the one appearing earliest in the path is considered. But functions of different argument types are considered on an equal footing regardless of search path position.

  2. Check for a function accepting exactly the input argument types. If one exists (there can be only one exact match in the set of functions considered), use it. (Cases involving unknown will never find a match at this step.)

  3. If no exact match is found, see whether the function call appears to be a trivial type conversion request. This happens if the function call has just one argument and the function name is the same as the (internal) name of some data type. Furthermore, the function argument must be either an unknown-type literal or a type that is binary-compatible with the named data type. When these conditions are met, the function argument is converted to the named data type without any actual function call.

  4. Look for the best match.

    1. Discard candidate functions for which the input types do not match and cannot be converted (using an implicit conversion) to match. unknown literals are assumed to be convertible to anything for this purpose. If only one candidate remains, use it; else continue to the next step.

    2. Run through all candidates and keep those with the most exact matches on input types. (Domains are considered the same as their base type for this purpose.) 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 that accept preferred types (of the input data type's type category) at the most positions where type conversion will be required. Keep all candidates if none accept preferred types. If only one candidate remains, use it; else continue to the next step.

    4. If any input arguments are unknown, check the type categories accepted at those argument positions by the remaining candidates. At each position, select the 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. Now discard 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.

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

Note that the "best match" rules are identical for operator and function type resolution. Some examples follow.

Exemplo 10-4. Rounding Function Argument Type Resolution

There is only one round function with two arguments. (The first is numeric, the second is integer.) So the following query automatically converts the first argument of type integer to numeric:

SELECT round(4, 4);

 round
--------
 4.0000
(1 row)

That query is actually transformed by the parser to

SELECT round(CAST (4 AS numeric), 4);

Since numeric constants with decimal points are initially assigned the type numeric, the following query will require no type conversion and may therefore be slightly more efficient:

SELECT round(4.0, 4);

Exemplo 10-5. Substring Function Type Resolution

There are several substr functions, one of which takes types text and integer. If called with a string constant of unspecified type, the system chooses the candidate function that accepts an argument of the preferred category string (namely of type text).

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 convert it to become text:

SELECT substr(varchar '1234', 3);

 substr
--------
     34
(1 row)

This is transformed by the parser to effectively become

SELECT substr(CAST (varchar '1234' AS text), 3);

Nota: The parser learns from the pg_cast catalog 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 argument of type integer, the parser will try to convert that to text:

SELECT substr(1234, 3);

 substr
--------
     34
(1 row)

This actually executes as

SELECT substr(CAST (1234 AS text), 3);

This automatic transformation can succeed because there is an implicitly invocable cast from integer to text.

SourceForge.net Logo CSS válido!