Sub topic 9
Choosing smallint, integer or bigint
This chapter assumes basic CREATE TABLE, INSERT, and SELECT, familiarity with primary keys, and a basic understanding of transactions. It also assumes you know that smallint, integer, and bigint are signed integer types.
An identity column generates values through an implicit sequence. The column type limits which generated values are valid. Choose the type from the largest ID the table may need, not from the number of rows it has today.
| Type | Storage | Lowest value | Highest value |
|---|---|---|---|
| `smallint` | 2 bytes | -32,768 | 32,767 |
| `integer` | 4 bytes | -2,147,483,648 | 2,147,483,647 |
| `bigint` | 8 bytes | -9,223,372,036,854,775,807 | 9,223,372,036,854,775,807 |
For the usual positive sequence that starts at 1, the practical limits are about 32 thousand, 2.1 billion, and 9.2 quintillion generated IDs. A non-default start or increment changes how quickly the sequence reaches its limit.
How the type controls identity values
The identity sequence produces a number, and PostgreSQL stores that number in the identity column. If the next number does not fit the column type, the insert fails with an overflow error.
CREATE TABLE id_type_demo (
small_id smallint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
regular_id integer GENERATED ALWAYS AS IDENTITY,
large_id bigint GENERATED ALWAYS AS IDENTITY,
description text NOT NULL
);
INSERT INTO id_type_demo (description)
VALUES ('first row')
RETURNING small_id, regular_id, large_id;The three columns use different amounts of storage. The bigint column uses twice as much space as an integer column, and four times as much as a smallint column. This difference affects the table, indexes, foreign keys, and related tables that store the ID.
Which type should you choose?
Use smallint only when the limit is a deliberate business rule. Examples include a fixed list of fewer than 32,768 local codes or a small lookup table. Do not use it merely because the table is small today.
Use integer when the table has a known limit below about 2.1 billion IDs and saving space matters. It is a reasonable choice for many ordinary applications.
Use bigint when the table may grow for many years, receives IDs from many writers, or has related systems that already use 64-bit IDs. The extra four bytes per value are often a good trade for avoiding a difficult future migration. Many teams choose bigint as their default for new primary keys.
The primary key does not remove the type limit. It prevents duplicate values, but it cannot make an out-of-range value valid.
Changing from integer to bigint later requires checking indexes, foreign keys, application code, and dependent tables. Plan the type before production data accumulates.
Exercise
A table currently has 500,000 rows and may grow indefinitely. Which identity type is the safer default: smallint, integer, or bigint?
Show answer
bigint is the safer default because the future size is unknown, and its larger range avoids an eventual overflow migration.