SMALLINT (2-byte integer)

Last updated: 2026-11-04

SMALLINT (2-byte integer)

SMALLINT is PostgreSQL's smallest integer type, occupying 2 bytes with a range of -32768 to 32767. It suits ratings, counters and status codes whose natural range is small, and storing them as SMALLINT instead of INTEGER saves meaningful space in very large tables. The range is enforced by the type system: an out-of-range value raises an error instead of being silently truncated.

Kindtype
Example LanguageSQL

📝 Syntax

SMALLINT  ——  range -32768 to 32767, 2 bytes

Returns:Holds a 2-byte signed integer. The result type of arithmetic is decided by the operator's two input types, and a stored value outside -32768 to 32767 raises a "smallint out of range" error.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

 id | score  | col_type
----+--------+----------
  1 |     95 | smallint
  2 | -32768 | smallint
  3 |  32767 | smallint

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QWhy not use SMALLINT for primary keys?
AIt works technically, but any table that keeps growing will eventually hit the 32767 ceiling, and widening the type means rebuilding the table and its indexes. Primary keys are usually INTEGER or BIGINT: INTEGER is cheap and sufficient, while BIGINT is the choice for distributed or time-ordered keys.
QDoes arithmetic on SMALLINT promote the type automatically?
AYes. smallint + integer yields integer, smallint + bigint yields bigint, and smallint / smallint also yields integer. The range is only checked when the result is assigned back to a SMALLINT column.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏