Skip to content

pg_tiktoken_c

Fast tiktoken BPE tokenizer for PostgreSQL implemented in C

pg_tiktoken_c · Fast tiktoken BPE tokenizer for PostgreSQL implemented in C

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
1880pg_tiktoken_cpg_tiktoken_c1.1RAGApache-2.0C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d--NoYesNoYesnono
Built from upstream main snapshot fa2957b; bundles five vocabularies and includes DESTDIR and correctness patches. Upstream README declares Apache-2.0, but the pinned snapshot omits the referenced LICENSE file.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY1.119 18 17 16 15pg_tiktoken_c-
RPMPIGSTY1.119 18 17 16 15pg_tiktoken_c_$v-
DEBPIGSTY1.119 18 17 16 15postgresql-$v-pg-tiktoken-c-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
el8.aarch64N/APIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
el9.x86_64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
el9.aarch64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
el10.x86_64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
el10.aarch64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
d12.x86_64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
d12.aarch64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
d13.x86_64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
d13.aarch64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
u22.x86_64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
u22.aarch64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
u24.x86_64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
u24.aarch64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
u26.x86_64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
u26.aarch64PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1PIGSTY 1.1
PackageVersionOSORGSIZEFile URL
pg_tiktoken_c_191.1el9.x86_64pigsty2.1 MiBpg_tiktoken_c_19-1.1-1PGSTY.el9.x86_64.rpm
pg_tiktoken_c_191.1el9.aarch64pigsty2.1 MiBpg_tiktoken_c_19-1.1-1PGSTY.el9.aarch64.rpm
pg_tiktoken_c_191.1el10.x86_64pigsty2.1 MiBpg_tiktoken_c_19-1.1-1PGSTY.el10.x86_64.rpm
pg_tiktoken_c_191.1el10.aarch64pigsty2.1 MiBpg_tiktoken_c_19-1.1-1PGSTY.el10.aarch64.rpm
postgresql-19-pg-tiktoken-c1.1d12.x86_64pigsty1.8 MiBpostgresql-19-pg-tiktoken-c_1.1-1PGSTY~bookworm_amd64.deb
postgresql-19-pg-tiktoken-c1.1d12.aarch64pigsty1.8 MiBpostgresql-19-pg-tiktoken-c_1.1-1PGSTY~bookworm_arm64.deb
postgresql-19-pg-tiktoken-c1.1d13.x86_64pigsty1.8 MiBpostgresql-19-pg-tiktoken-c_1.1-1PGSTY~trixie_amd64.deb
postgresql-19-pg-tiktoken-c1.1d13.aarch64pigsty1.8 MiBpostgresql-19-pg-tiktoken-c_1.1-1PGSTY~trixie_arm64.deb
postgresql-19-pg-tiktoken-c1.1u22.x86_64pigsty2.1 MiBpostgresql-19-pg-tiktoken-c_1.1-1PGSTY~jammy_amd64.deb
postgresql-19-pg-tiktoken-c1.1u22.aarch64pigsty2.1 MiBpostgresql-19-pg-tiktoken-c_1.1-1PGSTY~jammy_arm64.deb
postgresql-19-pg-tiktoken-c1.1u24.x86_64pigsty2.1 MiBpostgresql-19-pg-tiktoken-c_1.1-1PGSTY~noble_amd64.deb
postgresql-19-pg-tiktoken-c1.1u24.aarch64pigsty2.1 MiBpostgresql-19-pg-tiktoken-c_1.1-1PGSTY~noble_arm64.deb
postgresql-19-pg-tiktoken-c1.1u26.x86_64pigsty2.1 MiBpostgresql-19-pg-tiktoken-c_1.1-1PGSTY~resolute_amd64.deb
postgresql-19-pg-tiktoken-c1.1u26.aarch64pigsty2.1 MiBpostgresql-19-pg-tiktoken-c_1.1-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_tiktoken_c_181.1el8.x86_64pigsty2.4 MiBpg_tiktoken_c_18-1.1-1PGSTY.el8.x86_64.rpm
pg_tiktoken_c_181.1el8.aarch64pigsty2.4 MiBpg_tiktoken_c_18-1.1-1PGSTY.el8.aarch64.rpm
pg_tiktoken_c_181.1el9.x86_64pigsty2.1 MiBpg_tiktoken_c_18-1.1-1PGSTY.el9.x86_64.rpm
pg_tiktoken_c_181.1el9.aarch64pigsty2.1 MiBpg_tiktoken_c_18-1.1-1PGSTY.el9.aarch64.rpm
pg_tiktoken_c_181.1el10.x86_64pigsty2.1 MiBpg_tiktoken_c_18-1.1-1PGSTY.el10.x86_64.rpm
pg_tiktoken_c_181.1el10.aarch64pigsty2.1 MiBpg_tiktoken_c_18-1.1-1PGSTY.el10.aarch64.rpm
postgresql-18-pg-tiktoken-c1.1d12.x86_64pigsty1.8 MiBpostgresql-18-pg-tiktoken-c_1.1-1PGSTY~bookworm_amd64.deb
postgresql-18-pg-tiktoken-c1.1d12.aarch64pigsty1.8 MiBpostgresql-18-pg-tiktoken-c_1.1-1PGSTY~bookworm_arm64.deb
postgresql-18-pg-tiktoken-c1.1d13.x86_64pigsty1.8 MiBpostgresql-18-pg-tiktoken-c_1.1-1PGSTY~trixie_amd64.deb
postgresql-18-pg-tiktoken-c1.1d13.aarch64pigsty1.8 MiBpostgresql-18-pg-tiktoken-c_1.1-1PGSTY~trixie_arm64.deb
postgresql-18-pg-tiktoken-c1.1u22.x86_64pigsty2.1 MiBpostgresql-18-pg-tiktoken-c_1.1-1PGSTY~jammy_amd64.deb
postgresql-18-pg-tiktoken-c1.1u22.aarch64pigsty2.1 MiBpostgresql-18-pg-tiktoken-c_1.1-1PGSTY~jammy_arm64.deb
postgresql-18-pg-tiktoken-c1.1u24.x86_64pigsty2.1 MiBpostgresql-18-pg-tiktoken-c_1.1-1PGSTY~noble_amd64.deb
postgresql-18-pg-tiktoken-c1.1u24.aarch64pigsty2.1 MiBpostgresql-18-pg-tiktoken-c_1.1-1PGSTY~noble_arm64.deb
postgresql-18-pg-tiktoken-c1.1u26.x86_64pigsty2.1 MiBpostgresql-18-pg-tiktoken-c_1.1-1PGSTY~resolute_amd64.deb
postgresql-18-pg-tiktoken-c1.1u26.aarch64pigsty2.1 MiBpostgresql-18-pg-tiktoken-c_1.1-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_tiktoken_c_171.1el8.x86_64pigsty2.4 MiBpg_tiktoken_c_17-1.1-1PGSTY.el8.x86_64.rpm
pg_tiktoken_c_171.1el8.aarch64pigsty2.4 MiBpg_tiktoken_c_17-1.1-1PGSTY.el8.aarch64.rpm
pg_tiktoken_c_171.1el9.x86_64pigsty2.1 MiBpg_tiktoken_c_17-1.1-1PGSTY.el9.x86_64.rpm
pg_tiktoken_c_171.1el9.aarch64pigsty2.1 MiBpg_tiktoken_c_17-1.1-1PGSTY.el9.aarch64.rpm
pg_tiktoken_c_171.1el10.x86_64pigsty2.1 MiBpg_tiktoken_c_17-1.1-1PGSTY.el10.x86_64.rpm
pg_tiktoken_c_171.1el10.aarch64pigsty2.1 MiBpg_tiktoken_c_17-1.1-1PGSTY.el10.aarch64.rpm
postgresql-17-pg-tiktoken-c1.1d12.x86_64pigsty1.8 MiBpostgresql-17-pg-tiktoken-c_1.1-1PGSTY~bookworm_amd64.deb
postgresql-17-pg-tiktoken-c1.1d12.aarch64pigsty1.8 MiBpostgresql-17-pg-tiktoken-c_1.1-1PGSTY~bookworm_arm64.deb
postgresql-17-pg-tiktoken-c1.1d13.x86_64pigsty1.8 MiBpostgresql-17-pg-tiktoken-c_1.1-1PGSTY~trixie_amd64.deb
postgresql-17-pg-tiktoken-c1.1d13.aarch64pigsty1.8 MiBpostgresql-17-pg-tiktoken-c_1.1-1PGSTY~trixie_arm64.deb
postgresql-17-pg-tiktoken-c1.1u22.x86_64pigsty2.1 MiBpostgresql-17-pg-tiktoken-c_1.1-1PGSTY~jammy_amd64.deb
postgresql-17-pg-tiktoken-c1.1u22.aarch64pigsty2.1 MiBpostgresql-17-pg-tiktoken-c_1.1-1PGSTY~jammy_arm64.deb
postgresql-17-pg-tiktoken-c1.1u24.x86_64pigsty2.1 MiBpostgresql-17-pg-tiktoken-c_1.1-1PGSTY~noble_amd64.deb
postgresql-17-pg-tiktoken-c1.1u24.aarch64pigsty2.1 MiBpostgresql-17-pg-tiktoken-c_1.1-1PGSTY~noble_arm64.deb
postgresql-17-pg-tiktoken-c1.1u26.x86_64pigsty2.1 MiBpostgresql-17-pg-tiktoken-c_1.1-1PGSTY~resolute_amd64.deb
postgresql-17-pg-tiktoken-c1.1u26.aarch64pigsty2.1 MiBpostgresql-17-pg-tiktoken-c_1.1-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_tiktoken_c_161.1el8.x86_64pigsty2.4 MiBpg_tiktoken_c_16-1.1-1PGSTY.el8.x86_64.rpm
pg_tiktoken_c_161.1el8.aarch64pigsty2.4 MiBpg_tiktoken_c_16-1.1-1PGSTY.el8.aarch64.rpm
pg_tiktoken_c_161.1el9.x86_64pigsty2.1 MiBpg_tiktoken_c_16-1.1-1PGSTY.el9.x86_64.rpm
pg_tiktoken_c_161.1el9.aarch64pigsty2.1 MiBpg_tiktoken_c_16-1.1-1PGSTY.el9.aarch64.rpm
pg_tiktoken_c_161.1el10.x86_64pigsty2.1 MiBpg_tiktoken_c_16-1.1-1PGSTY.el10.x86_64.rpm
pg_tiktoken_c_161.1el10.aarch64pigsty2.1 MiBpg_tiktoken_c_16-1.1-1PGSTY.el10.aarch64.rpm
postgresql-16-pg-tiktoken-c1.1d12.x86_64pigsty1.8 MiBpostgresql-16-pg-tiktoken-c_1.1-1PGSTY~bookworm_amd64.deb
postgresql-16-pg-tiktoken-c1.1d12.aarch64pigsty1.8 MiBpostgresql-16-pg-tiktoken-c_1.1-1PGSTY~bookworm_arm64.deb
postgresql-16-pg-tiktoken-c1.1d13.x86_64pigsty1.8 MiBpostgresql-16-pg-tiktoken-c_1.1-1PGSTY~trixie_amd64.deb
postgresql-16-pg-tiktoken-c1.1d13.aarch64pigsty1.8 MiBpostgresql-16-pg-tiktoken-c_1.1-1PGSTY~trixie_arm64.deb
postgresql-16-pg-tiktoken-c1.1u22.x86_64pigsty2.1 MiBpostgresql-16-pg-tiktoken-c_1.1-1PGSTY~jammy_amd64.deb
postgresql-16-pg-tiktoken-c1.1u22.aarch64pigsty2.1 MiBpostgresql-16-pg-tiktoken-c_1.1-1PGSTY~jammy_arm64.deb
postgresql-16-pg-tiktoken-c1.1u24.x86_64pigsty2.1 MiBpostgresql-16-pg-tiktoken-c_1.1-1PGSTY~noble_amd64.deb
postgresql-16-pg-tiktoken-c1.1u24.aarch64pigsty2.1 MiBpostgresql-16-pg-tiktoken-c_1.1-1PGSTY~noble_arm64.deb
postgresql-16-pg-tiktoken-c1.1u26.x86_64pigsty2.1 MiBpostgresql-16-pg-tiktoken-c_1.1-1PGSTY~resolute_amd64.deb
postgresql-16-pg-tiktoken-c1.1u26.aarch64pigsty2.1 MiBpostgresql-16-pg-tiktoken-c_1.1-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_tiktoken_c_151.1el8.x86_64pigsty2.4 MiBpg_tiktoken_c_15-1.1-1PGSTY.el8.x86_64.rpm
pg_tiktoken_c_151.1el8.aarch64pigsty2.4 MiBpg_tiktoken_c_15-1.1-1PGSTY.el8.aarch64.rpm
pg_tiktoken_c_151.1el9.x86_64pigsty2.1 MiBpg_tiktoken_c_15-1.1-1PGSTY.el9.x86_64.rpm
pg_tiktoken_c_151.1el9.aarch64pigsty2.1 MiBpg_tiktoken_c_15-1.1-1PGSTY.el9.aarch64.rpm
pg_tiktoken_c_151.1el10.x86_64pigsty2.1 MiBpg_tiktoken_c_15-1.1-1PGSTY.el10.x86_64.rpm
pg_tiktoken_c_151.1el10.aarch64pigsty2.1 MiBpg_tiktoken_c_15-1.1-1PGSTY.el10.aarch64.rpm
postgresql-15-pg-tiktoken-c1.1d12.x86_64pigsty1.8 MiBpostgresql-15-pg-tiktoken-c_1.1-1PGSTY~bookworm_amd64.deb
postgresql-15-pg-tiktoken-c1.1d12.aarch64pigsty1.8 MiBpostgresql-15-pg-tiktoken-c_1.1-1PGSTY~bookworm_arm64.deb
postgresql-15-pg-tiktoken-c1.1d13.x86_64pigsty1.8 MiBpostgresql-15-pg-tiktoken-c_1.1-1PGSTY~trixie_amd64.deb
postgresql-15-pg-tiktoken-c1.1d13.aarch64pigsty1.8 MiBpostgresql-15-pg-tiktoken-c_1.1-1PGSTY~trixie_arm64.deb
postgresql-15-pg-tiktoken-c1.1u22.x86_64pigsty2.1 MiBpostgresql-15-pg-tiktoken-c_1.1-1PGSTY~jammy_amd64.deb
postgresql-15-pg-tiktoken-c1.1u22.aarch64pigsty2.1 MiBpostgresql-15-pg-tiktoken-c_1.1-1PGSTY~jammy_arm64.deb
postgresql-15-pg-tiktoken-c1.1u24.x86_64pigsty2.1 MiBpostgresql-15-pg-tiktoken-c_1.1-1PGSTY~noble_amd64.deb
postgresql-15-pg-tiktoken-c1.1u24.aarch64pigsty2.1 MiBpostgresql-15-pg-tiktoken-c_1.1-1PGSTY~noble_arm64.deb
postgresql-15-pg-tiktoken-c1.1u26.x86_64pigsty2.1 MiBpostgresql-15-pg-tiktoken-c_1.1-1PGSTY~resolute_amd64.deb
postgresql-15-pg-tiktoken-c1.1u26.aarch64pigsty2.1 MiBpostgresql-15-pg-tiktoken-c_1.1-1PGSTY~resolute_arm64.deb

Source

pig build pkg pg_tiktoken_c;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pg_tiktoken_c;		# install by package name

pig install pg_tiktoken_c -v 19;   # install for PG 19
pig install pg_tiktoken_c -v 18;   # install for PG 18
pig install pg_tiktoken_c -v 17;   # install for PG 17
pig install pg_tiktoken_c -v 16;   # install for PG 16
pig install pg_tiktoken_c -v 15;   # install for PG 15

Create this extension with:

CREATE EXTENSION pg_tiktoken_c;

Usage

Sources:

pg_tiktoken_c implements OpenAI-compatible tiktoken encoding in C inside PostgreSQL. Use it to count or materialize tokens near stored text and to split text into token-bounded chunks before embedding or model requests.

Create the Extension

CREATE EXTENSION pg_tiktoken_c;

The implementation depends on PCRE2 10.30 or later at build time. It does not require shared_preload_libraries; vocabulary data is loaded and cached per backend as encodings are used.

Encode and Count

SELECT tiktoken_encode('cl100k_base', 'PostgreSQL search');
SELECT tiktoken_count('cl100k_base', 'PostgreSQL search');

tiktoken_encode returns a bigint array of token identifiers. tiktoken_count returns the token count without requiring the caller to retain the token array.

The bundled selectors include cl100k_base, o200k_base, r50k_base, p50k_base, and p50k_edit, together with aliases documented by the project. Choose the encoding required by the target model rather than assuming all models share a vocabulary.

Chunk Text

Return chunks as an array:

SELECT chunk_text(
  'long document text',
  chunk_size => 512,
  chunk_overlap => 64,
  encoding => 'cl100k_base'
);

Or return one row per chunk:

SELECT *
FROM chunk_text_table(
  'long document text',
  chunk_size => 512,
  chunk_overlap => 64,
  encoding => 'cl100k_base'
);

chunk_text_table returns chunk_index, chunk, and token_count. The chunk index is zero-based. Overlap repeats boundary tokens between neighboring chunks and must be smaller than the chunk size.

Function Index

  • tiktoken_encode(selector, text) returns bigint[] token identifiers.
  • tiktoken_count(selector, text) returns bigint token count.
  • chunk_text(input_text, chunk_size, chunk_overlap default 0, encoding default cl100k_base) returns text[].
  • chunk_text_table(input_text, chunk_size, chunk_overlap default 0, encoding default cl100k_base) returns one row per chunk with its index and token count.

The SQL functions are declared immutable and parallel safe. They can therefore be used in generated expressions or parallel plans only when the selected vocabulary files are deployed consistently across every server.

Operational Notes

  • Tokenization is model-encoding specific. Confirm both the encoding name and the model’s current context limits in the application.
  • Counting or chunking large text consumes backend CPU and memory; batch large corpora and monitor query latency.
  • Backend-local caches avoid repeated parsing but increase memory use in sessions that touch several vocabularies.
  • The upstream README’s compatibility list can lag packaging. Test the exact pg_tiktoken_c build against the target PostgreSQL major version instead of inferring support from a different binary.

Table reader