skip to content

integers, numeric and floats

int vs bigint, numeric for money, and 0.1 + 0.2 in float8 vs numeric.

address
everything.types.numbers
part
part 6, types and values
chapter
chapter 2 of 11
reading time
8 min

three families. integers (smallint, integer, bigint) are exact and fixed-size, and fail loudly when they overflow. numeric is exact decimal arithmetic at any size, at some cost in speed and space. floats (real, double precision) are binary approximations: fast, and unable to hold 0.1.

money goes in numeric. and 5 / 2 is 2.

the picture

you know how 0.1 + 0.2 is 0.30000000000000004 in javascript? postgres's double precision does exactly the same thing, because it's the same IEEE 754 double. the surprise is that SELECT 0.1 + 0.2 in postgres says 0.3. a number with a dot in SQL is a numeric, an exact decimal, unless you ask for a float.

think of a float as a ruler marked in binary fractions: halves, quarters, eighths, all the way down to 2⁻⁵². a tenth falls between two marks, so it gets stored at the nearest one. the number you get back is very close to 0.1 and not 0.1.

float8 =
postgres prints
0.1
the bits hold
0.1000000000000000055511151231257827021181583404541015625

a tenth is 0.000110011001100... in binary, repeating forever. the mantissa keeps 52 bits of it, and the last ones round up (1001 becomes 1010), so the stored value is a hair above 0.1.

exponent bitswhere the stored value stops matching
real output, postgres 18.6 (PGlite 0.5.12) · numbers
pick a value. these are the 64 bits postgres stores for it, and the exact decimal value those bits hold. the digits in hot are where the stored value stops being the number you typed. 0.1 + 0.2 lands on the very next double above 0.3's, so only the last bits differ.

the bits come from float8send, which hands back a double's 8 bytes, and the long decimal is postgres turning those same bits into a numeric, where nothing rounds. nothing in the figure is computed by the page.

integers

three sizes. pick by the biggest value the column will ever hold, not by today's:

sqlempty db
SELECT 32767::smallint AS smallint_max, 2147483647::integer AS integer_max, 9223372036854775807::bigint AS bigint_max;
output, from postgres 18.6
SELECT 1
smallint_maxint2integer_maxint4bigint_maxint8
3276721474836479223372036854775807
(1 row)

one more than the top doesn't wrap around to a negative number like it does in C or java. in a bigger type it's just a number. in the same type, postgres stops:

sqlempty db
SELECT 2147483647::bigint + 1 AS as_bigint;
SELECT 2147483647 + 1 AS as_integer;
output, from postgres 18.6
#1 · SELECT 1
as_bigintint8
2147483648
(1 row)
#2 · ERROR 22003
ERROR 22003: integer out of range

this is the error a busy table's integer id column hits one day, around the 2.1 billionth row. new id columns should be bigint: 4 more bytes per row, and a problem you'll never have (identity columns).

integer division

predict the output
sql
SELECT 5 / 2;
sqlempty db
SELECT 5 / 2 AS int_div, -5 / 2 AS negative, 5 % 2 AS remainder, 5 / 2.0 AS numeric_div, 5::numeric / 2 AS cast_first;
output, from postgres 18.6
SELECT 1
int_divint4negativeint4remainderint4numeric_divnumericcast_firstnumeric
2-212.50000000000000002.5000000000000000
(1 row)

this is the bug in every "percentage" column that's always 0: count(*) FILTER (WHERE paid) / count(*) divides two bigints. multiply by 100.0 first, or cast one side.

numeric

numeric stores decimal digits, so decimal arithmetic is exact:

sqlempty db
SELECT 0.1 + 0.2 AS sum, 0.1 + 0.2 = 0.3 AS equal, pg_typeof(0.1 + 0.2) AS type;
output, from postgres 18.6
SELECT 1
sumnumericequalbooltyperegtype
0.3tnumeric
(1 row)

with numeric(p, s) you fix the scale: s digits after the point, p digits in all. extra decimals are rounded off when you store, and a value too big for p is an error, not a silent cut:

sqlempty db
SELECT 123.456::numeric(5,2) AS rounded;
SELECT 1234.5::numeric(5,2) AS too_big;
output, from postgres 18.6
#1 · SELECT 1
roundednumeric
123.46
(1 row)
#2 · ERROR 22003
ERROR 22003: numeric field overflow
DETAIL: A field with precision 5, scale 2 must round to an absolute value less than 10^3.

the price is size and speed. integers and floats are fixed-width, numeric grows with its digits:

smallint2 bytes
integer4 bytes
bigint8 bytes
float88 bytes
numeric 15 bytes
numeric 12345.679 bytes
numeric, 55 digits31 bytes
bytes per value, measured with pg_column_size on values stored in a table. numeric takes room per digit; 0.1's exact double, written out in full, costs 31 bytes.

and numeric math runs in software, a few digits at a time, where integer and float math are single CPU instructions. for sums of money over a few million rows that's fine. for physics, use floats.

floats

real (float4, about 6 significant digits) and double precision (float8, about 15) are the IEEE types. they're fast and they're approximations, and the error adds up:

type =
sum.sql
  1. 1SELECT sum(0.1::float4) AS total, sum(0.1::float4) = 1 AS is_one FROM generate_series(1, 10);
the result for real (float4)
totalfloat4is_onebool
1.0000001f
(1 row)

ten float4 0.1s: real has about 7 significant digits, and the error shows in the 8th.

real output, postgres 18.6 (PGlite 0.5.12) · numbers
ten 0.1s summed in each type. float4 and float8 both miss 1, in different directions; numeric hits it.

what you see depends on a setting, extra_float_digits. since postgres 12 the default, 1, prints the shortest decimal that reads back as the same bits, which is why 0.1 prints as 0.1 and the sum shows its tail. set it to 0 and postgres rounds to 15 significant digits for display, and the problem disappears from view, but not from the value:

sqlempty db
SET extra_float_digits = 0;
SELECT 0.1::float8 + 0.2::float8 AS shown, 0.1::float8 + 0.2::float8 = 0.3 AS equal;
output, from postgres 18.6
#1 · SET
#2 · SELECT 1
shownfloat8equalbool
0.3f
(1 row)

0.3, and still not equal. never compare floats with =. floats are for measurements (a temperature, a distance, a model's score), where the last bits never meant anything anyway.

money goes in numeric

postgres has a money type. it stores a 64-bit integer count of cents, which is fine. the trouble is the rest:

sqlempty db
SELECT 1234.5::money AS shown, '10.00'::money / 3 AS a_third, ('10.00'::money / 3) * 3 AS three_thirds;
output, from postgres 18.6
SELECT 1
shownmoneya_thirdmoneythree_thirdsmoney
$1,234.50$3.33$9.99
(1 row)

the currency symbol and the number of decimals come from the server's lc_monetary setting, not from the value. this browser's database says C, which means dollars. here are the same 1234.5, under two other settings on a real server:

sqlempty db
SET lc_monetary = 'de_DE.UTF-8';
SELECT 1234.5::money AS germany;
SET lc_monetary = 'en_GB.UTF-8';
SELECT 1234.5::money AS uk;
output, from a real postgres 18.6 server
#1 · SET
#2 · SELECT 1
germanymoney
1.234,50 €
(1 row)
#3 · SET
#4 · SELECT 1
ukmoney
£1,234.50
(1 row)

checked on a real postgres 18.6 server. the browser engine can't run this one.

same stored number, two currencies. restore a dump on a server with a different setting and every price changes currency without changing value. money also can't hold a fraction of a cent (a unit price of 0.0042 is gone) and knows nothing about which currency a row is in. use numeric(12,2) (or more scale if you price in fractions), and put the currency in its own column when there's more than one.

NaN and Infinity

floats have always had NaN, Infinity and -Infinity. numeric has had NaN for a long time, and infinities since postgres 14 pg 14+. postgres sorts them on purpose, so ORDER BY and indexes still work:

sqlempty db
SELECT x
FROM (VALUES ('NaN'::numeric), ('Infinity'), (5), ('-Infinity'), (NULL)) AS v (x)
ORDER BY x;
output, from postgres 18.6
SELECT 5
xnumeric
-Infinity
5
Infinity
NaN
null
(5 rows)

NaN sorts above every number, even Infinity, and 'NaN' = 'NaN' is true here, unlike in IEEE arithmetic, where NaN equals nothing. postgres bends that rule so that NaN can be grouped, sorted and indexed like any other value.

sqlempty db
SELECT 'NaN'::numeric = 'NaN'::numeric AS nan_equals_nan,
       'Infinity'::numeric + 1 AS inf_plus_one,
       'Infinity'::numeric - 'Infinity'::numeric AS inf_minus_inf;
output, from postgres 18.6
SELECT 1
nan_equals_nanboolinf_plus_onenumericinf_minus_infnumeric
tInfinityNaN
(1 row)

keylines

  • integers are exact and fixed-size, and overflow is an error. new id columns should be bigint.
  • integer / integer truncates: 5 / 2 is 2.
  • numeric is exact decimal arithmetic, at the cost of space and speed.
  • floats can't hold 0.1. never compare them with =, never store money in them.
  • money goes in numeric, with the currency in its own column.