The operand types of an operator invocation are resolved following the procedure below. Note that this procedure is indirectly affected by the precedence of the involved operators. See Section 1.4 for more information.
Operand Type Resolution
Check for an exact match in the pg_operator system catalog.
If one argument of a binary operator is unknown type, then assume it is the same type as the other argument for this check. Other cases involving unknown will never find a match at this step.
Look for the best match.
Make a list of all operators of the same name 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.
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.
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.
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.
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. Also note whether any of the candidates accept a preferred data type 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.
If only one candidate remains, use it. If no candidate or more than one candidate remains, then fail.
Example 5-1. Exponentiation Operator Type Resolution
There is only one exponentiation operator defined in the catalog, and it takes arguments of type double precision. The scanner assigns an initial type of integer to both arguments of this query expression:
tgl=> SELECT 2 ^ 3 AS "Exp"; Exp ----- 8 (1 row)
So the parser does a type conversion on both operands and the query is equivalent to
tgl=> SELECT CAST(2 AS double precision) ^ CAST(3 AS double precision) AS "Exp"; Exp ----- 8 (1 row)
tgl=> SELECT 2.0 ^ 3.0 AS "Exp"; Exp ----- 8 (1 row)
Note: This last form has the least overhead, since no functions are called to do implicit type conversion. This is not an issue for small queries, but may have an impact on the performance of queries involving large tables.
Example 5-2. String Concatenation Operator Type Resolution
A string-like syntax is used for working with string types as well as for working with complex extended types. Strings with unspecified type are matched with likely operator candidates.
An example with one unspecified argument:
tgl=> SELECT text 'abc' || 'def' AS "Text and Unknown"; Text and Unknown ------------------ abcdef (1 row)
In this case the parser looks to see if there is an operator taking text for both arguments. Since there is, it assumes that the second argument should be interpreted as of type text.
Concatenation on unspecified types:
tgl=> SELECT 'abc' || 'def' AS "Unspecified"; Unspecified ------------- abcdef (1 row)
In this case there is no initial hint for which type to use, since no types are specified in the query. So, the parser looks for all candidate operators and finds that there are candidates accepting both string-category and bit-string-category inputs. Since string category is preferred when available, that category is selected, and then the "preferred type" for strings, text, is used as the specific type to resolve the unknown literals to.
Example 5-3. Absolute-Value and Factorial Operator Type Resolution
The PostgreSQL operator catalog has several entries for the prefix operator @, all of which implement absolute-value operations for various numeric data types. One of these entries is for type float8, which is the preferred type in the numeric category. Therefore, PostgreSQL will use that entry when faced with a non-numeric input:
tgl=> select @ text '-4.5' as "abs"; abs ----- 4.5 (1 row)
Here the system has performed an implicit text-to-float8 conversion before applying the chosen operator. We can verify that float8 and not some other type was used:
tgl=> select @ text '-4.5e500' as "abs"; ERROR: Input '-4.5e500' is out of range for float8
On the other hand, the postfix operator ! (factorial) is defined only for integer data types, not for float8. So, if we try a similar case with !, we get:
tgl=> select text '44' ! as "factorial"; ERROR: Unable to identify a postfix operator '!' for type 'text' You may need to add parentheses or an explicit cast
This happens because the system can't decide which of the several possible ! operators should be preferred. We can help it out with an explicit cast:
tgl=> select cast(text '44' as int8) ! as "factorial"; factorial --------------------- 2673996885588443136 (1 row)