upid_pg
upid_pg · Prefixed, sortable 128-bit identifier type and generator
Overview
PostgreSQL · 18
Usage
Sources:
- Official UPID README
- PostgreSQL extension implementation
- PostgreSQL extension manifest
- Core identifier implementation
upid_pg adds the upid type: a sortable 128-bit identifier containing a short semantic prefix, a timestamp, random bits, and a format version. It is useful when UUID-sized keys should remain globally generated while still revealing a compact object kind such as user.
Generate and store UPIDs
The binary layout places a 40-bit Unix timestamp first for approximate chronological ordering, followed by 64 random bits and 24 bits for the prefix and version. Timestamp precision is 256 milliseconds, so generation order within the same time bucket is random rather than monotonic.
Conversion functions
gen_upid(prefix)generates a new value using the current UTC time.upid_to_prefix(upid)andupid_to_timestamp(upid)extract the embedded prefix and approximate timestamp.upid_to_bytea(upid)exposes the 16-byte representation.upid_to_uuid(upid)andupid_from_uuid(uuid)preserve the same 128 bits.
The extension also defines implicit casts between uuid and upid, and from upid to bytea or timestamp:
Converting an arbitrary UUID to upid is reversible, but the decoded prefix and timestamp have no semantic meaning.
Prefix and maturity boundaries
Pass exactly four lowercase Latin letters when generating identifiers. The implementation silently normalizes other input: a short prefix is right-padded with z, a long prefix is truncated to four characters, and characters outside its alphabet are replaced with z. Validate prefixes in application code if silent normalization is undesirable.
The embedded timestamp is metadata, not an authentication or trustworthy creation-time proof. The 64 random bits provide probabilistic uniqueness, not a database-enforced collision guarantee. Catalog version 0.0.0 denotes an alpha-stage API; the reviewed pgrx manifest has build features for PostgreSQL 15 through 18, but actual package availability and upgrade compatibility must be checked for the target server. Pin the revision and test type I/O, indexes, casts, dump/restore, and upgrades before adoption.