Sub topic 12
Result Types in UNION, CASE and ARRAY
This chapter assumes you know basic SELECT syntax and common PostgreSQL types such as integer, numeric, text, and boolean. The key idea is that PostgreSQL must choose one result type when several expressions are combined.
Why a common type is needed
A UNION produces one column for each column position, so every query in the union must provide compatible types. A CASE expression has one result type even though different branches may return different values. An ARRAY constructor needs one element type for the whole array.
PostgreSQL chooses a common type using implicit casts. It does not simply choose the type with the largest storage size.
SELECT pg_typeof(value) AS result_type, value
FROM (
SELECT 1 AS value
UNION ALL
SELECT 2.5
) AS combined;
SELECT pg_typeof(
CASE
WHEN true THEN 1
ELSE 2.5
END
) AS result_type;
SELECT pg_typeof(ARRAY[1, 2.5]) AS result_type;The first query returns numeric, the CASE expression returns numeric, and the array has type numeric[]. PostgreSQL finds a type that can represent all the input values.
The common-type algorithm
For these expressions, PostgreSQL broadly follows this process:
- If all non-unknown inputs already have the same type, use that type.
- Domain values are generally considered using their base type.
- Unknown literals such as
'42'andNULLare ignored while PostgreSQL examines known types. - If known types belong to incompatible type categories, PostgreSQL raises an error.
- PostgreSQL selects a candidate type that can accept the other inputs through implicit casts.
- It converts every input to the selected type.
CASE has one ordering detail: PostgreSQL considers the ELSE expression first, followed by the THEN expressions.
SELECT
pg_typeof(
CASE
WHEN false THEN 'not used'::text
ELSE 42
END
) AS result_type;This fails because text and integer are not implicitly convertible to one common type in this context. The fact that the text branch is unreachable does not remove it from type resolution.
Unknown literals and explicit casts
A quoted literal starts with the special unknown type. PostgreSQL can assign it a type based on context:
SELECT pg_typeof(x) AS result_type, x
FROM (
SELECT 1 AS x
UNION ALL
SELECT '2'
) AS values_to_combine;Here, '2' can become an integer, so the result is integer.
An explicit cast removes this flexibility:
SELECT 1
UNION ALL
SELECT '2'::text;This raises a type error because PostgreSQL cannot implicitly convert the integer result to text for this union. Cast the other branch explicitly when you want text:
SELECT 1::text
UNION ALL
SELECT '2'::text;Arrays use the same rule
Every element in an ARRAY[...] constructor must resolve to one common element type. The array type is then formed from that element type.
SELECT
ARRAY[1, 2.5] AS values,
pg_typeof(ARRAY[1, 2.5]) AS array_type;
SELECT
ARRAY[1, '2'] AS values,
pg_typeof(ARRAY[1, '2']) AS array_type;The first array is numeric[]. The second is integer[] because the unknown literal '2' can be converted to integer.
Use an explicit cast when the intended result is important or ambiguous:
SELECT ARRAY['1', '2', '3']::integer[] AS numbers;Exercise
What is the result type of CASE WHEN true THEN 10 ELSE NULL END, and why?
Show answer
It is integer. NULL starts as an unknown value and can be converted to the integer type selected by the other branch.