Numeric precision and overflow
Core idea
Precision describes how accurately a number can be represented. Overflow occurs when a result cannot fit in its numeric type. A wider type or exact decimal arithmetic may be needed before doing a calculation, not after it has already lost information.
Minimal example
SQLite 3.46.1 example.
SELECT typeof(9223372036854775807 + 0), typeof(9223372036854775807 + 1), (9223372036854775807 + 1) = (9223372036854775807 + 2);Observed rows (JSON notation; null means SQL NULL):
[ [ "integer", "real", 1 ]]How it works
SQLite signed INTEGER values fit in 64 bits. When integer addition overflows, SQLite can promote the expression to REAL, losing integer precision. PostgreSQL bigint addition outside its range instead raises an error. Cast operands to PostgreSQL numeric before the operation when exact results beyond bigint are required; casting the already overflowing result is too late. For binary floating-point comparisons, an appropriate tolerance depends on the scale and error requirements. Integer cents avoid decimal representation error only while the arithmetic stays within the integer range.
Practical example
SQLite 3.46.1 example.
SELECT (0.1 + 0.2) = 0.3 AS exact_match, ABS((0.1 + 0.2) - 0.3) < 0.000001 AS close_match, 10 + 20 AS exact_cents;Observed rows (JSON notation; null means SQL NULL):
[ [ 0, 1, 30 ]]Common mistake and practice
Never assume SQLite DECIMAL(p,s) enforces PostgreSQL-style fixed precision. For exact monetary arithmetic, validate amount ranges and use integer minor units or an actual exact-decimal engine type. Practice: predict the storage class of 9223372036854775807 + 0 and + 1 in SQLite; then explain why a conversion after addition cannot restore lost precision.
PostgreSQL 17 exact arithmetic — not executed
SELECT CAST(9223372036854775807 AS numeric) + 1 AS exact_result;Expected exact numeric value: 9223372036854775808. PostgreSQL runtime unavailable.