skip to content

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.

SELECT
every + postgres has for this shape, from pg_operator
candidate operators
left_typeright_typeresult_type
bigintintegerbigint
dateintegerdate
integerintegerinteger
smallintintegerinteger
(4 rows)
query.sql
  1. 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.

step 1/4
real output, postgres 18.6 (PGlite 0.5.12) · types-and-casts
pick an expression and step through what the parser does with it. a dashed type is still open, a type that just got settled turns hot, and the operator postgres takes is lit. the types, the candidate lists and the last step are real postgres output.

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:

sqlempty db
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;
output, from postgres 18.6
SELECT 1
quotedregtypesmall_numberregtypebig_numberregtypewith_a_dotregtypeboolregtype
unknownintegerbigintnumericboolean
(1 row)

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:

sqlempty db
SELECT CAST('42' AS integer) AS cast_fn,
       '42'::integer         AS colons,
       integer '42'          AS typed_literal;
output, from postgres 18.6
SELECT 1
cast_fnint4colonsint4typed_literalint4
424242
(1 row)

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:

sqlempty db
SELECT '12abc'::integer;
output, from postgres 18.6
ERROR 22P02: invalid input syntax for type integer: "12abc"
1 │ SELECT '12abc'::integer;
at line 1, column 8

(' 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.

sqlempty db
SELECT castcontext AS context, count(*) AS casts
FROM pg_cast
GROUP BY castcontext
ORDER BY castcontext;
output, from postgres 18.6
SELECT 3
contextcharcastsint8
a77
e41
i117
(3 rows)
rows: the type you have. columns: the type you want.
from ↓ to →int2int4int8numflt8datetstzbool
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.

i implicita assignmente explicit· no cast
real output, postgres 18.6 (PGlite 0.5.12) · types-and-casts
eight types from pg_cast, the type you have down the side, the one you want across the top. click a cell. widening (integer to bigint) is implicit, narrowing (bigint to integer) is assignment, booleans and numbers only meet when you say so, and an empty cell has no cast at all.

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:

VALUES =
store.sql
  1. 1INSERT INTO t (n) VALUES (3.7) RETURNING n;
the result for 3.7
nint4
4
(1 row)

3.7 is a numeric. numeric to integer is an assignment cast, so storing it rounds it.

real output, postgres 18.6 (PGlite 0.5.12) · types-and-casts
five values into one 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:

  1. if one side is unknown, first assume it has the other side's type. '1' + 1 becomes a search for integer + integer, which exists. done.
  2. otherwise, keep the candidates the arguments can reach through implicit casts, and prefer the ones that need the fewest.
  3. if an unknown is still open, prefer a candidate that treats it as a string.
predict the output
sql
SELECT '1' + '1';
sqlempty db
SELECT '1' + '1';
output, from postgres 18.6
ERROR 42725: operator is not unique: unknown + unknown
1 │ SELECT '1' + '1';
at line 1, column 12
HINT: Could not choose a best candidate operator. You might need to add explicit type casts.

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:

sqlempty db
SELECT 1 || 1;
output, from postgres 18.6
ERROR 42883: operator does not exist: integer || integer
1 │ SELECT 1 || 1;
at line 1, column 10
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.

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:

sqlempty db
SELECT 1 || 'x' AS mixed, 1::text || 1 AS cast_one, '1' || 1 AS literal;
output, from postgres 18.6
SELECT 1
mixedtextcast_onetextliteraltext
1x1111
(1 row)

and storing a number into a text column is an assignment, so that works without any cast at all:

sqlempty db
CREATE TABLE notes (body text);
INSERT INTO notes (body) VALUES (42);
SELECT body, pg_typeof(body) FROM notes;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 1
#3 · SELECT 1
bodytextpg_typeofregtype
42text
(1 row)

keylines

  • every value and every result column has one type, decided before any row is read.
  • CAST(x AS t) is the standard, x::t is 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.