Sub topic 2
Integers and Identity Columns
This chapter assumes you know SELECT, INSERT, CREATE TABLE, and basic constraints. It focuses on PostgreSQL integer types and automatically generated identifiers.
Choosing an integer type
PostgreSQL provides three signed integer types:
| Type | Storage | Range |
|---|---|---|
| `smallint` | 2 bytes | -32,768 to 32,767 |
| `integer` | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| `bigint` | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Use integer when its range is sufficient. Use bigint for identifiers or counters that may grow beyond about two billion, especially in shared schemas where changing the type later would be disruptive. Use smallint only when the range is a real domain rule, such as a small rating scale or a bounded status code.
The type affects storage and arithmetic, but it does not automatically enforce a business limit such as “quantity must be positive.” Add a constraint for that rule.
CREATE TABLE products (
id bigint PRIMARY KEY,
quantity integer NOT NULL CHECK (quantity >= 0),
rating smallint CHECK (rating BETWEEN 1 AND 5)
);An integer overflow occurs when a calculation or inserted value exceeds the type's range. PostgreSQL reports an error rather than silently wrapping the value. Choose a wider type before the data approaches the limit.
Identity columns
An identity column generates values from an internal sequence. It is useful for a surrogate key, which is a database-created identifier with no business meaning.
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id bigint NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO orders (customer_id)
VALUES (42), (43);
SELECT id, customer_id
FROM orders
ORDER BY id;GENERATED ALWAYS means PostgreSQL normally rejects an explicitly supplied value for id. This protects the sequence from accidental manual values. To insert a deliberate value, use the OVERRIDING SYSTEM VALUE clause.
GENERATED BY DEFAULT also generates a value when one is omitted, but it permits an explicitly supplied value. Choose it when imports or data migrations need controlled identifier values.
Identity values are not guaranteed to have no gaps. A rolled-back transaction can consume a sequence value, and deleted rows do not cause values to be reused. Treat an identity as a unique key, not as a row count or a guaranteed business sequence.
Common errors and design choices
Older PostgreSQL code may use serial or bigserial. These create an integer column and a sequence through shorthand, but identity columns express the relationship more clearly and provide better control over explicit inserts. Prefer identity columns for new designs.
Do not use an integer for values that are not whole numbers. Prices need numeric, and timestamps need date-time types. Also remember that PRIMARY KEY implies both uniqueness and NOT NULL; adding both separately is unnecessary.
Exercise
Which definition is appropriate for a table expected to contain billions of rows: integer GENERATED ALWAYS AS IDENTITY or bigint GENERATED ALWAYS AS IDENTITY?
Show answer
Use bigint GENERATED ALWAYS AS IDENTITY, because its much larger range avoids an integer overflow as the identifier grows.