Sub topic 7
How Operators and Functions Pick Argument Types
This chapter assumes you know basic SELECT statements and common PostgreSQL types. You should also understand that a cast converts a value from one type to another.
PostgreSQL chooses an operator or function by comparing the types of the arguments with the available definitions. Operators and functions can be overloaded, which means the same name can have several definitions with different argument types.
The resolution process
When PostgreSQL sees an expression such as 1 + '2', it roughly follows this process:
- Look for an exact match for the argument types.
- Consider candidates that can accept implicit conversions.
- Use the type category and preferred types to resolve unknown literals.
- Raise an error if no candidate or more than one candidate remains.
An untyped string literal starts with the special unknown type. PostgreSQL can convert it to a suitable type while resolving the expression.
SELECT 1 + '2' AS integer_result;
SELECT lower('ABC') AS lowercase_text;
SELECT length(42) AS fails;
SELECT length(42::text) AS works;The first query works because PostgreSQL can interpret '2' as an integer for the integer + integer operator. The length(42) query fails because PostgreSQL has no implicit conversion from integer to the argument type accepted by length. The explicit cast makes the intended conversion clear.
An explicitly typed value is different from an unknown literal:
SELECT 1 + '2'::text AS fails;This fails because PostgreSQL does not normally convert text to integer implicitly for the addition operator. The literal is no longer available for flexible type resolution.
Function overloads and preferred types
You can inspect overloaded functions with pg_proc, PostgreSQL’s system catalog:
SELECT p.oid::regprocedure AS signature
FROM pg_proc AS p
WHERE p.proname = 'length'
ORDER BY signature;The result shows several length definitions. PostgreSQL selects one whose argument types match exactly or can be reached through allowed implicit casts.
Unknown literals often resolve toward the string type category. That is why lower('ABC') selects the text-oriented function. If the context does not provide enough information, PostgreSQL reports an ambiguous-function error instead of guessing.
A practical decision rule
| Situation | PostgreSQL behavior |
|---|---|
| Argument type exactly matches one candidate | Uses that candidate |
| Unknown literal has one sensible category | Converts it to the required type |
| Explicitly typed argument needs an unavailable implicit cast | Raises an error |
| Several candidates remain equally suitable | Raises an ambiguity error |
Operators use the same general rules as functions. For example, +, =, and comparison operators each have definitions for particular type combinations. Cast the argument rather than the operator expression when you need to control which definition PostgreSQL chooses.
Exercise
Why does 1 + '2' work while 1 + '2'::text usually fails?
Show answer
The untyped string literal has type unknown and can be resolved as an integer. The explicit text value must use an available implicit cast to integer, and PostgreSQL does not normally allow that conversion for addition.