5.2. Operators

Operator Type Resolution

  1. Check for an exact match in the pg_operator system catalog.

    1. 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.

  2. Look for the best match.

    1. 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.

    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.

5.2.1. Examples

5.2.1.1. Exponentiation Operator

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 int4 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)
or
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.

5.2.1.2. String Concatenation

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.

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 bitstring-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.

5.2.1.3. Factorial

This example illustrates an interesting result. Traditionally, the factorial operator is defined for integers only. The Postgres operator catalog has only one entry for factorial, taking an integer operand. If given a non-integer numeric argument, Postgres will try to convert that argument to an integer for evaluation of the factorial.

tgl=> select (4.3 !);
 ?column?
----------
       24
(1 row)

Note: Of course, this leads to a mathematically suspect result, since in principle the factorial of a non-integer is not defined. However, the role of a database is not to teach mathematics, but to be a tool for data manipulation. If a user chooses to take the factorial of a floating point number, Postgres will try to oblige.