types and casts
every value has a type. ::, implicit casts, and why '1' + 1 works but 1 || 1 doesn't.
- address
- everything.types.types_and_casts
- part
- part 6, types and values
- chapter
- chapter 1 of 11
- reading time
- 9 min
every value has a type, fixed before the query runs. a cast turns one type into another: CAST(x AS int) or x::int. postgres casts on its own only where the catalog says it may: implicit casts anywhere, assignment casts when storing into a column, explicit ones only when you ask.
a quoted literal starts out as unknown and takes the type the context wants. that's why '1' + 1 is 2 and 1 || 1 is an error.
the picture
you know how '1' + 1 gives you 2 in postgres, no complaints, but 1 || 1 refuses to glue two ones together? it looks backwards. the first one adds a string to a number. the second one just wants a string out of two numbers.
think of a quoted literal as a blank name tag (the type unknown). it walks up to an operator, the operator decides which seat is free, and the tag gets the name on the seat. 1 without quotes arrives with its name already printed: integer. two printed tags and no seat for them, and nobody gets in.
| left_type | right_type | result_type |
|---|---|---|
| bigint | integer | bigint |
| date | integer | date |
| integer | integer | integer |
| smallint | integer | integer |
- SELECT '1' + 1 AS result, pg_typeof('1' + 1) AS type;
type each side'1' is in quotes, so it's a string of characters with no type yet: unknown. 1 has no quotes and no dot, so it's an integer.
notice that postgres decides everything from the types, never from what's inside the quotes. '1.5' + 1 fails for exactly the reason '1' + 1 works: the operator is picked first, then the string has to fit it.
every value has a type
pg_typeof tells you what postgres thinks something is:
SELECT pg_typeof('hello') AS quoted,
pg_typeof(1) AS small_number,
pg_typeof(3000000000) AS big_number,
pg_typeof(1.5) AS with_a_dot,
pg_typeof(true) AS bool;| quotedregtype | small_numberregtype | big_numberregtype | with_a_dotregtype | boolregtype |
|---|---|---|---|---|
| unknown | integer | bigint | numeric | boolean |
a quoted string is unknown until something needs it to be more. a number without a dot is an integer, unless it's too big for one (then bigint). a dot makes it numeric, not a float. that last one matters, and integers, numeric and floats is about why.
and the type is fixed per column, not per row. a column of a result has exactly one type, decided before the first row is read, which is why a CASE that returns a number in one branch and a word in another fails instead of mixing them.
three ways to cast
the standard spelling is CAST(x AS type). postgres also has x::type, which is shorter and postgres only postgres only, and a typed literal, type 'string', which the standard has for a few types (DATE '2026-01-31') and postgres allows for any:
SELECT CAST('42' AS integer) AS cast_fn,
'42'::integer AS colons,
integer '42' AS typed_literal;| cast_fnint4 | colonsint4 | typed_literalint4 |
|---|---|---|
| 42 | 42 | 42 |
all three call the same thing: integer's input function, on the string 42. a cast from a string is a parse, and it fails like one:
SELECT '12abc'::integer;(' 42 '::integer is fine, by the way. integer input skips spaces around the digits.)
implicit, assignment, explicit
so who decides when postgres may cast without being asked? a catalog table, pg_cast. every row is a cast from one type to another, and its castcontext says where it's allowed: i anywhere, a when assigning to a column, e only when you write it.
SELECT castcontext AS context, count(*) AS casts
FROM pg_cast
GROUP BY castcontext
ORDER BY castcontext;| contextchar | castsint8 |
|---|---|
| a | 77 |
| e | 41 |
| i | 117 |
| from ↓ to → | int2 | int4 | int8 | num | flt8 | date | tstz | bool |
|---|---|---|---|---|---|---|---|---|
| int2 | ||||||||
| int4 | ||||||||
| int8 | ||||||||
| num | ||||||||
| flt8 | ||||||||
| date | ||||||||
| tstz | ||||||||
| bool |
int4 → int8: implicit. postgres adds this cast on its own wherever it helps: in arithmetic, in a comparison, in a function call.
the pattern is about losing information. a cast that can't lose anything (integer to bigint, date to timestamptz) is implicit. one that can (numeric to integer drops the fraction, bigint to integer can overflow) is only done when you store into a column of that type, because then you clearly meant it. one that changes the meaning (boolean to integer) waits for you to ask.
text isn't on the grid because it isn't in pg_cast. every type can be written as text and read from text through its input and output functions, and postgres treats those as casts too: into text at assignment level, out of text explicit only. so storing 42 into a text column works, and adding a text to a number doesn't.
the assignment level is easiest to see on an INSERT:
- 1INSERT INTO t (n) VALUES (3.7) RETURNING n;
| nint4 |
|---|
| 4 |
3.7 is a numeric. numeric to integer is an assignment cast, so storing it rounds it.
int column. pick a value: the same column, but what happens depends on the type the value arrives with, not on what it looks like.3.7 and '3.7' look the same and behave in opposite ways. 3.7 is a numeric, and numeric to integer is an assignment cast that rounds, so you get 4 and no warning. '3.7' is an unknown literal, so it's read directly as an integer, and it isn't one.
why '1' + 1 works
when postgres meets x + y, it looks up every operator called + and keeps the ones that could take these types. the manual has the whole algorithm (it's a page of numbered steps); the part that matters here is short:
- if one side is unknown, first assume it has the other side's type.
'1' + 1becomes a search forinteger + integer, which exists. done. - otherwise, keep the candidates the arguments can reach through implicit casts, and prefer the ones that need the fewest.
- if an unknown is still open, prefer a candidate that treats it as a string.
SELECT '1' + '1';SELECT '1' + '1';postgres even says what it's missing: a type for one side.
why 1 || 1 doesn't
|| is concatenation. there's a text version, an array version, a jsonb version, a bytea one, but none for two integers, and integer doesn't become text on its own:
SELECT 1 || 1;give it one text side and it's happy. there's a text || anynonarray (and the mirror image), which takes any value and turns it into text inside the operator:
SELECT 1 || 'x' AS mixed, 1::text || 1 AS cast_one, '1' || 1 AS literal;| mixedtext | cast_onetext | literaltext |
|---|---|---|
| 1x | 11 | 11 |
and storing a number into a text column is an assignment, so that works without any cast at all:
CREATE TABLE notes (body text);
INSERT INTO notes (body) VALUES (42);
SELECT body, pg_typeof(body) FROM notes;| bodytext | pg_typeofregtype |
|---|---|
| 42 | text |
keylines
- every value and every result column has one type, decided before any row is read.
CAST(x AS t)is the standard,x::tis postgres's shorthand, and both do the same thing.- pg_cast says where postgres may cast on its own: implicit anywhere, assignment when storing, explicit never.
- a quoted literal is unknown until an operator or a column gives it a type.
- postgres picks the operator from the types first, then reads the string to fit it.