tinyint
tinyint-postgresql · One-byte signed integer type with arithmetic, casts, aggregates, and B-tree, hash, and GIN operator classes.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100088 | tinyint | tinyint-postgresql | 0.1.1 | TYPE | PostgreSQL | C |
PostgreSQL · 10
Usage
Sources:
tinyint adds a one-byte signed integer type for compact values whose documented range is -127 through 127. It supports integer-style casts, comparisons, arithmetic, aggregates, and indexes, but row alignment can erase the expected storage saving and the old upstream code needs compatibility testing on the target PostgreSQL release.
Core Workflow
Create the extension and use the type for values that are guaranteed to stay in its narrow range:
Conversions from smallint, integer, and bigint are assignment casts and reject out-of-range values. Conversions from tinyint to the wider integer types are implicit.
Operators, Aggregates, and Indexes
- Arithmetic includes unary signs and
+,-,*, and/; mixed operations with wider integer types return the wider type. - Comparisons work between
tinyintandsmallint,integer, orbigintin either argument order. min,max,sum, andavgare defined;sumuses abiginttransition/result andavguses PostgreSQL’s integer average finalizer.btree_tinyint_opsandhash_tinyint_opssupport scalar B-tree and hash indexing._tinyint_opssupports GIN indexing oftinyint[]arrays.
Caveats
Arithmetic and casts raise an error on overflow, so validate inputs before bulk loads and migrations. This is a signed type and is not a drop-in replacement for an unsigned TINYINT from another database.
Although the datum itself occupies one byte, PostgreSQL can add alignment padding depending on column order and surrounding types. Measure the actual tuple and index sizes before adopting it purely for space savings. The reviewed upstream revision dates from 2017 and its published compatibility material is old; build, install, upgrade, dump/restore, operator, aggregate, and index tests are required on each intended PostgreSQL major version.