Skip to content

int128

Signed 128-bit integers with arithmetic, casts, aggregates and index operator classes.

postgres_int128 · Signed 128-bit integers with arithmetic, casts, aggregates and index operator classes.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
102420int128postgres_int1281.0TYPEPostgreSQLC

PostgreSQL · 17, 18, 19

Usage

Sources:

int128 adds a signed 128-bit integer type for PostgreSQL 17–19. Version 1.0 stores values from -2^127 through 2^127-1 in 16 bytes, with checked arithmetic, casts, aggregates and index support. It is a fixed-width integer, not arbitrary-precision numeric.

Store and Query Large Integers

A superuser installs the extension. It is relocatable and needs no shared preload or restart.

CREATE EXTENSION int128;
CREATE TABLE ledger (id int128 PRIMARY KEY, amount int128 NOT NULL);
INSERT INTO ledger VALUES ('500000000000000000000'::int128, 100::int128);
SELECT * FROM ledger WHERE id = '500000000000000000000'::int128;
SELECT sum(amount), avg(amount) FROM ledger;

Explicitly cast out-of-range integer literals to int128; otherwise PostgreSQL can choose a numeric comparison that cannot use the integer index. Arithmetic overflow and out-of-range conversions raise errors. Integer division truncates toward zero. Numeric casts round half away from zero; floating-point casts follow the server’s round-half-to-even behavior.

Objects and Indexes

The type supplies arithmetic, comparison and bitwise operators, abs(), div(), mod(), gcd(), lcm(), mathematical functions and generate_series(). The sum() aggregate returns int128 and remains overflow checked; avg() returns exact numeric. Statistical, minimum/maximum and bitwise aggregates are also provided.

Btree and hash support ordinary equality and ordering; GiST and SP-GiST support range predicates and KNN ordering; GIN and BRIN operator classes are included. GIN range scans are not selective and GIN does not support the not-equal operator. GiST/SP-GiST KNN distances use double precision and lose integer precision beyond 2^53.

SELECT '[1,10)'::int128range @> 5::int128;
SELECT '{[1,10)}'::int128multirange;

int128range and int128multirange support range workflows. There is no type modifier. JSON conversion emits strings for this custom type; cast to numeric when a JSON number is required. Values that fit in bigint normally use less storage and processing there. No performance guarantee follows from the wider type.

Table reader