pgtoken
PostgreSQL extension for compact, codebook-compressed LLM token ID storage. Encode once, retrieve forever. O(1) token count without decoding.
pgtoken · PostgreSQL extension for compact, codebook-compressed LLM token ID storage. Encode once, retrieve forever. O(1) token count without decoding.
Overview
Usage
Sources:
- Official upstream README
- Official extension control file (pgtoken.control)
- Official extension SQL (pgtoken–1.0.sql)
pgtoken — PostgreSQL extension for rank-varint token storage. Store LLM token IDs compactly as bytea. No re-tokenization on read. Use it for the corresponding vector, model, or retrieval workflow. Use the pinned upstream revision linked above as the API boundary and test it on the target PostgreSQL build.
Core Workflow
Install the extension in the intended database, run the smallest upstream example above when available, and verify the installed version and returned values before integrating it into application SQL.
Important Objects
pgtoken_count(encoded bytea)is an extension function and returnsinteger.pgtoken_decode(encoded bytea, codebook text DEFAULT 'cl100k_base')is an extension function and returnsinteger[].pgtoken_encode(token_ids integer[], codebook text DEFAULT 'cl100k_base')is an extension function and returnsbytea.pgtoken_reload_codebooks()is an extension function and returnsvoid.pgtoken_codebooksis an extension-defined view.
Requirements and Caveats
- The reviewed control file declares default version
1.0. - The control file marks the extension as relocatable.
- Confirm privileges, supported PostgreSQL versions, upgrade behavior, and failure cases against the pinned source before production use.