smallint
Learning Focus
Use this lesson to understand PostgreSQL smallint and when it is (and is not) the right choice.
smallint Overview
- Storage: 2 bytes
- Range: -32,768 to 32,767
- Best for: small bounded counters (ratings, small category IDs, short enums represented as integers)
Example
CREATE TABLE product_reviews (
review_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
rating smallint NOT NULL CHECK (rating BETWEEN 1 AND 5)
);
Common Pitfalls
| Pitfall | Consequence | Prevention |
|---|---|---|
| Using smallint for growing IDs | Overflow risk | Use integer or bigint for identifiers |
Best Practices
- Use
smallintonly when you know the value domain is small. - Enforce the domain with
CHECK.