Full answer
The largest signed 64-bit integer is 9223372036854775807. Totals of large values, such as amounts in the smallest currency unit over many years, can approach it.
CREATE TABLE ledger (amount INTEGER);
INSERT INTO ledger VALUES (9223372036854775807), (1);SQLite's SUM refuses to give a wrong answer:
SELECT SUM(amount) FROM ledger; -- error on purposeError: integer overflowSQLite's TOTAL() uses floating point, so it returns a result, but the low digits are lost:
SELECT TOTAL(amount) AS total_float FROM ledger;| total_float |
|---|
| 9.223372036854776e+18 |
PostgreSQL's SUM over bigint returns numeric, so it is exact:
SELECT SUM(amount) AS exact_sum, pg_typeof(SUM(amount)) AS type FROM ledger;| exact_sum | type |
|---|---|
| 9223372036854775808 | numeric |
Plain addition of two bigints in PostgreSQL still overflows:
SELECT 9223372036854775807::bigint + 1::bigint; -- error on purposeError: bigint out of rangeSo the engines differ on which operation is safe. For money totals, PostgreSQL's numeric result is convenient. In SQLite, an overflow error is better than a silent rounding, and the usual remedy is choosing units so totals stay far from the limit.
CREATE TABLE ledger (amount INTEGER);
INSERT INTO ledger VALUES (9223372036854775807), (1);
-- error on purpose
SELECT SUM(amount) FROM ledger;
SELECT TOTAL(amount) AS total_float FROM ledger;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- Max 64-bit signed integer is 9223372036854775807
- SQLite SUM raises integer overflow; TOTAL() returns an imprecise float
- PostgreSQL SUM(bigint) returns exact numeric
- PostgreSQL bigint + bigint can still overflow
Common mistakes
- Assuming every engine silently promotes to a bigger type
- Using a floating-point total for money and losing cents
Follow-ups
- What type does SUM(integer) return in PostgreSQL?
- How would you store large monetary amounts safely?
Tips
- Mention the exact limit; it shows you know what 'large' means here