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.
- 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.
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:
SELECT 32767::smallint AS smallint_max, 2147483647::integer AS integer_max, 9223372036854775807::bigint AS bigint_max;| smallint_maxint2 | integer_maxint4 | bigint_maxint8 |
|---|---|---|
| 32767 | 2147483647 | 9223372036854775807 |
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:
SELECT 2147483647::bigint + 1 AS as_bigint;
SELECT 2147483647 + 1 AS as_integer;| as_bigintint8 |
|---|
| 2147483648 |
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
SELECT 5 / 2;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;| int_divint4 | negativeint4 | remainderint4 | numeric_divnumeric | cast_firstnumeric |
|---|---|---|---|---|
| 2 | -2 | 1 | 2.5000000000000000 | 2.5000000000000000 |
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:
SELECT 0.1 + 0.2 AS sum, 0.1 + 0.2 = 0.3 AS equal, pg_typeof(0.1 + 0.2) AS type;| sumnumeric | equalbool | typeregtype |
|---|---|---|
| 0.3 | t | numeric |
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:
SELECT 123.456::numeric(5,2) AS rounded;
SELECT 1234.5::numeric(5,2) AS too_big;| roundednumeric |
|---|
| 123.46 |
the price is size and speed. integers and floats are fixed-width, numeric grows with its digits:
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:
- 1SELECT sum(0.1::float4) AS total, sum(0.1::float4) = 1 AS is_one FROM generate_series(1, 10);
| totalfloat4 | is_onebool |
|---|---|
| 1.0000001 | f |
ten float4 0.1s: real has about 7 significant digits, and the error shows in the 8th.
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:
SET extra_float_digits = 0;
SELECT 0.1::float8 + 0.2::float8 AS shown, 0.1::float8 + 0.2::float8 = 0.3 AS equal;| shownfloat8 | equalbool |
|---|---|
| 0.3 | f |
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:
SELECT 1234.5::money AS shown, '10.00'::money / 3 AS a_third, ('10.00'::money / 3) * 3 AS three_thirds;| shownmoney | a_thirdmoney | three_thirdsmoney |
|---|---|---|
| $1,234.50 | $3.33 | $9.99 |
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:
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;| germanymoney |
|---|
| 1.234,50 € |
| ukmoney |
|---|
| £1,234.50 |
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:
SELECT x
FROM (VALUES ('NaN'::numeric), ('Infinity'), (5), ('-Infinity'), (NULL)) AS v (x)
ORDER BY x;| xnumeric |
|---|
| -Infinity |
| 5 |
| Infinity |
| NaN |
| null |
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.
SELECT 'NaN'::numeric = 'NaN'::numeric AS nan_equals_nan,
'Infinity'::numeric + 1 AS inf_plus_one,
'Infinity'::numeric - 'Infinity'::numeric AS inf_minus_inf;| nan_equals_nanbool | inf_plus_onenumeric | inf_minus_infnumeric |
|---|---|---|
| t | Infinity | NaN |
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.