Sub topic 6
Implicit, Assignment and Explicit Casts
Assume you know basic SELECT, INSERT, UPDATE, and PostgreSQL types such as integer, bigint, numeric, text, and smallint. PostgreSQL can convert values in three different cast contexts. The context controls when a conversion is allowed and whether you must request it.
The three cast contexts
An implicit cast happens automatically while PostgreSQL resolves an operator or function call. PostgreSQL uses these casts cautiously because an automatic conversion can change which operator or function is selected.
An assignment cast is allowed when a value is assigned to a column, such as during INSERT or UPDATE. It is broader than an implicit cast, but it still must be registered as a valid conversion.
An explicit cast is requested directly with CAST, the :: operator, or a typed literal. Explicit casts allow conversions that PostgreSQL will not choose automatically.
| Context | Typical location | How you request it |
|---|---|---|
| Implicit | Operators and function arguments | PostgreSQL chooses it |
| Assignment | `INSERT` and `UPDATE` values | PostgreSQL chooses it for a column |
| Explicit | Any expression | `CAST(value AS type)` or `value::type` |
For example, integer to bigint is normally implicit because bigint can represent every ordinary integer value. The reverse conversion can lose range, so PostgreSQL does not generally use it implicitly.
SELECT 1::integer + 2::bigint AS total;
CREATE TEMP TABLE cast_targets (
narrow_value smallint,
wide_value bigint
);
-- Assignment conversion is considered for the target column.
INSERT INTO cast_targets (narrow_value, wide_value)
VALUES (42, 42);
-- Explicit conversion makes the direction clear.
SELECT CAST(42::bigint AS smallint) AS explicit_value;An assignment conversion does not guarantee that the value fits. An out-of-range value can still raise an error when PostgreSQL performs the conversion.
Unknown literals and operator resolution
A quoted literal such as '42' starts with the special unknown type. PostgreSQL delays choosing its final type until it examines the surrounding expression.
SELECT 1 + '2' AS result;
SELECT 1 + '2'::text AS fails;The first query can resolve '2' as an integer. The second query explicitly says that the value is text, and PostgreSQL does not automatically use text as an integer operand for the + operator.
This behavior explains why adding an explicit cast can fix an ambiguous expression, but can also make an expression fail by removing the flexibility of an unknown literal.
Inspecting cast rules
PostgreSQL records built-in casts in pg_cast. The castcontext column uses i for implicit, a for assignment-only, and e for explicit-only casts.
SELECT castsource::regtype AS source_type,
casttarget::regtype AS target_type,
castcontext
FROM pg_cast
WHERE castsource = 'int4'::regtype
AND casttarget IN ('int8'::regtype, 'int2'::regtype);The result typically shows integer to bigint with context i, and integer to smallint with context a. Both conversions remain available explicitly.
Exercise
Which context should you use when converting a bigint expression to smallint in a SELECT list, and why?
Show answer
Use an explicit cast, such as value::smallint. Assignment rules apply when storing a value in a column, while an ordinary expression does not provide an assignment context.