Skip to main content

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​

PitfallConsequencePrevention
Using smallint for growing IDsOverflow riskUse integer or bigint for identifiers

Best Practices​

  • Use smallint only when you know the value domain is small.
  • Enforce the domain with CHECK.

What's Next​