Skip to content

pg_tiktoken

tiktoken tokenizer for use with OpenAI models in postgres

pg_tiktoken · tiktoken tokenizer for use with OpenAI models in postgres

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
1870pg_tiktokenpg_tiktoken0.0.1RAGApache-2.0Rust
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d--NoYesNoYesnono

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY0.0.119 18 17 16 15pg_tiktoken-
RPMPIGSTY0.0.119 18 17 16 15pg_tiktoken_$v-
DEBPIGSTY0.0.119 18 17 16 15postgresql-$v-pg-tiktoken-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
el8.aarch64N/APIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
el9.x86_64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
el9.aarch64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
el10.x86_64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
el10.aarch64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
d12.x86_64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
d12.aarch64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
d13.x86_64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
d13.aarch64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
u22.x86_64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
u22.aarch64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
u24.x86_64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
u24.aarch64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
u26.x86_64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
u26.aarch64PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1PIGSTY 0.0.1
PackageVersionOSORGSIZEFile URL
pg_tiktoken_190.0.1el9.x86_64pigsty2.1 MiBpg_tiktoken_19-0.0.1-6PGSTY.el9.x86_64.rpm
pg_tiktoken_190.0.1el9.aarch64pigsty2.0 MiBpg_tiktoken_19-0.0.1-6PGSTY.el9.aarch64.rpm
pg_tiktoken_190.0.1el10.x86_64pigsty2.1 MiBpg_tiktoken_19-0.0.1-6PGSTY.el10.x86_64.rpm
pg_tiktoken_190.0.1el10.aarch64pigsty2.0 MiBpg_tiktoken_19-0.0.1-6PGSTY.el10.aarch64.rpm
postgresql-19-pg-tiktoken0.0.1d12.x86_64pigsty1.8 MiBpostgresql-19-pg-tiktoken_0.0.1-5PGSTY~bookworm_amd64.deb
postgresql-19-pg-tiktoken0.0.1d12.aarch64pigsty1.6 MiBpostgresql-19-pg-tiktoken_0.0.1-5PGSTY~bookworm_arm64.deb
postgresql-19-pg-tiktoken0.0.1d13.x86_64pigsty1.8 MiBpostgresql-19-pg-tiktoken_0.0.1-5PGSTY~trixie_amd64.deb
postgresql-19-pg-tiktoken0.0.1d13.aarch64pigsty1.6 MiBpostgresql-19-pg-tiktoken_0.0.1-5PGSTY~trixie_arm64.deb
postgresql-19-pg-tiktoken0.0.1u22.x86_64pigsty2.0 MiBpostgresql-19-pg-tiktoken_0.0.1-5PGSTY~jammy_amd64.deb
postgresql-19-pg-tiktoken0.0.1u22.aarch64pigsty1.9 MiBpostgresql-19-pg-tiktoken_0.0.1-5PGSTY~jammy_arm64.deb
postgresql-19-pg-tiktoken0.0.1u24.x86_64pigsty2.0 MiBpostgresql-19-pg-tiktoken_0.0.1-5PGSTY~noble_amd64.deb
postgresql-19-pg-tiktoken0.0.1u24.aarch64pigsty1.9 MiBpostgresql-19-pg-tiktoken_0.0.1-5PGSTY~noble_arm64.deb
postgresql-19-pg-tiktoken0.0.1u26.x86_64pigsty2.0 MiBpostgresql-19-pg-tiktoken_0.0.1-5PGSTY~resolute_amd64.deb
postgresql-19-pg-tiktoken0.0.1u26.aarch64pigsty1.9 MiBpostgresql-19-pg-tiktoken_0.0.1-5PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_tiktoken_180.0.1el8.x86_64pigsty2.3 MiBpg_tiktoken_18-0.0.1-4PIGSTY.el8.x86_64.rpm
pg_tiktoken_180.0.1el8.aarch64pigsty2.2 MiBpg_tiktoken_18-0.0.1-4PIGSTY.el8.aarch64.rpm
pg_tiktoken_180.0.1el9.x86_64pigsty2.1 MiBpg_tiktoken_18-0.0.1-6PGSTY.el9.x86_64.rpm
pg_tiktoken_180.0.1el9.aarch64pigsty2.0 MiBpg_tiktoken_18-0.0.1-6PGSTY.el9.aarch64.rpm
pg_tiktoken_180.0.1el10.x86_64pigsty2.1 MiBpg_tiktoken_18-0.0.1-6PGSTY.el10.x86_64.rpm
pg_tiktoken_180.0.1el10.aarch64pigsty2.0 MiBpg_tiktoken_18-0.0.1-6PGSTY.el10.aarch64.rpm
postgresql-18-pg-tiktoken0.0.1d12.x86_64pigsty1.8 MiBpostgresql-18-pg-tiktoken_0.0.1-5PGSTY~bookworm_amd64.deb
postgresql-18-pg-tiktoken0.0.1d12.aarch64pigsty1.6 MiBpostgresql-18-pg-tiktoken_0.0.1-5PGSTY~bookworm_arm64.deb
postgresql-18-pg-tiktoken0.0.1d13.x86_64pigsty1.8 MiBpostgresql-18-pg-tiktoken_0.0.1-5PGSTY~trixie_amd64.deb
postgresql-18-pg-tiktoken0.0.1d13.aarch64pigsty1.6 MiBpostgresql-18-pg-tiktoken_0.0.1-5PGSTY~trixie_arm64.deb
postgresql-18-pg-tiktoken0.0.1u22.x86_64pigsty2.0 MiBpostgresql-18-pg-tiktoken_0.0.1-5PGSTY~jammy_amd64.deb
postgresql-18-pg-tiktoken0.0.1u22.aarch64pigsty1.9 MiBpostgresql-18-pg-tiktoken_0.0.1-5PGSTY~jammy_arm64.deb
postgresql-18-pg-tiktoken0.0.1u24.x86_64pigsty2.0 MiBpostgresql-18-pg-tiktoken_0.0.1-5PGSTY~noble_amd64.deb
postgresql-18-pg-tiktoken0.0.1u24.aarch64pigsty1.9 MiBpostgresql-18-pg-tiktoken_0.0.1-5PGSTY~noble_arm64.deb
postgresql-18-pg-tiktoken0.0.1u26.x86_64pigsty2.0 MiBpostgresql-18-pg-tiktoken_0.0.1-5PGSTY~resolute_amd64.deb
postgresql-18-pg-tiktoken0.0.1u26.aarch64pigsty1.9 MiBpostgresql-18-pg-tiktoken_0.0.1-5PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_tiktoken_170.0.1el8.x86_64pigsty2.3 MiBpg_tiktoken_17-0.0.1-4PIGSTY.el8.x86_64.rpm
pg_tiktoken_170.0.1el8.aarch64pigsty2.2 MiBpg_tiktoken_17-0.0.1-4PIGSTY.el8.aarch64.rpm
pg_tiktoken_170.0.1el9.x86_64pigsty2.1 MiBpg_tiktoken_17-0.0.1-6PGSTY.el9.x86_64.rpm
pg_tiktoken_170.0.1el9.aarch64pigsty2.0 MiBpg_tiktoken_17-0.0.1-6PGSTY.el9.aarch64.rpm
pg_tiktoken_170.0.1el10.x86_64pigsty2.1 MiBpg_tiktoken_17-0.0.1-6PGSTY.el10.x86_64.rpm
pg_tiktoken_170.0.1el10.aarch64pigsty2.0 MiBpg_tiktoken_17-0.0.1-6PGSTY.el10.aarch64.rpm
postgresql-17-pg-tiktoken0.0.1d12.x86_64pigsty1.8 MiBpostgresql-17-pg-tiktoken_0.0.1-5PGSTY~bookworm_amd64.deb
postgresql-17-pg-tiktoken0.0.1d12.aarch64pigsty1.6 MiBpostgresql-17-pg-tiktoken_0.0.1-5PGSTY~bookworm_arm64.deb
postgresql-17-pg-tiktoken0.0.1d13.x86_64pigsty1.8 MiBpostgresql-17-pg-tiktoken_0.0.1-5PGSTY~trixie_amd64.deb
postgresql-17-pg-tiktoken0.0.1d13.aarch64pigsty1.6 MiBpostgresql-17-pg-tiktoken_0.0.1-5PGSTY~trixie_arm64.deb
postgresql-17-pg-tiktoken0.0.1u22.x86_64pigsty2.0 MiBpostgresql-17-pg-tiktoken_0.0.1-5PGSTY~jammy_amd64.deb
postgresql-17-pg-tiktoken0.0.1u22.aarch64pigsty1.9 MiBpostgresql-17-pg-tiktoken_0.0.1-5PGSTY~jammy_arm64.deb
postgresql-17-pg-tiktoken0.0.1u24.x86_64pigsty2.0 MiBpostgresql-17-pg-tiktoken_0.0.1-5PGSTY~noble_amd64.deb
postgresql-17-pg-tiktoken0.0.1u24.aarch64pigsty1.9 MiBpostgresql-17-pg-tiktoken_0.0.1-5PGSTY~noble_arm64.deb
postgresql-17-pg-tiktoken0.0.1u26.x86_64pigsty2.0 MiBpostgresql-17-pg-tiktoken_0.0.1-5PGSTY~resolute_amd64.deb
postgresql-17-pg-tiktoken0.0.1u26.aarch64pigsty1.9 MiBpostgresql-17-pg-tiktoken_0.0.1-5PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_tiktoken_160.0.1el8.x86_64pigsty2.3 MiBpg_tiktoken_16-0.0.1-4PIGSTY.el8.x86_64.rpm
pg_tiktoken_160.0.1el8.aarch64pigsty2.2 MiBpg_tiktoken_16-0.0.1-4PIGSTY.el8.aarch64.rpm
pg_tiktoken_160.0.1el9.x86_64pigsty2.1 MiBpg_tiktoken_16-0.0.1-6PGSTY.el9.x86_64.rpm
pg_tiktoken_160.0.1el9.aarch64pigsty2.0 MiBpg_tiktoken_16-0.0.1-6PGSTY.el9.aarch64.rpm
pg_tiktoken_160.0.1el10.x86_64pigsty2.1 MiBpg_tiktoken_16-0.0.1-6PGSTY.el10.x86_64.rpm
pg_tiktoken_160.0.1el10.aarch64pigsty2.0 MiBpg_tiktoken_16-0.0.1-6PGSTY.el10.aarch64.rpm
postgresql-16-pg-tiktoken0.0.1d12.x86_64pigsty1.8 MiBpostgresql-16-pg-tiktoken_0.0.1-5PGSTY~bookworm_amd64.deb
postgresql-16-pg-tiktoken0.0.1d12.aarch64pigsty1.6 MiBpostgresql-16-pg-tiktoken_0.0.1-5PGSTY~bookworm_arm64.deb
postgresql-16-pg-tiktoken0.0.1d13.x86_64pigsty1.8 MiBpostgresql-16-pg-tiktoken_0.0.1-5PGSTY~trixie_amd64.deb
postgresql-16-pg-tiktoken0.0.1d13.aarch64pigsty1.6 MiBpostgresql-16-pg-tiktoken_0.0.1-5PGSTY~trixie_arm64.deb
postgresql-16-pg-tiktoken0.0.1u22.x86_64pigsty2.0 MiBpostgresql-16-pg-tiktoken_0.0.1-5PGSTY~jammy_amd64.deb
postgresql-16-pg-tiktoken0.0.1u22.aarch64pigsty1.9 MiBpostgresql-16-pg-tiktoken_0.0.1-5PGSTY~jammy_arm64.deb
postgresql-16-pg-tiktoken0.0.1u24.x86_64pigsty2.0 MiBpostgresql-16-pg-tiktoken_0.0.1-5PGSTY~noble_amd64.deb
postgresql-16-pg-tiktoken0.0.1u24.aarch64pigsty1.9 MiBpostgresql-16-pg-tiktoken_0.0.1-5PGSTY~noble_arm64.deb
postgresql-16-pg-tiktoken0.0.1u26.x86_64pigsty2.0 MiBpostgresql-16-pg-tiktoken_0.0.1-5PGSTY~resolute_amd64.deb
postgresql-16-pg-tiktoken0.0.1u26.aarch64pigsty1.9 MiBpostgresql-16-pg-tiktoken_0.0.1-5PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_tiktoken_150.0.1el8.x86_64pigsty2.3 MiBpg_tiktoken_15-0.0.1-4PIGSTY.el8.x86_64.rpm
pg_tiktoken_150.0.1el8.aarch64pigsty2.2 MiBpg_tiktoken_15-0.0.1-4PIGSTY.el8.aarch64.rpm
pg_tiktoken_150.0.1el9.x86_64pigsty2.1 MiBpg_tiktoken_15-0.0.1-6PGSTY.el9.x86_64.rpm
pg_tiktoken_150.0.1el9.aarch64pigsty2.0 MiBpg_tiktoken_15-0.0.1-6PGSTY.el9.aarch64.rpm
pg_tiktoken_150.0.1el10.x86_64pigsty2.1 MiBpg_tiktoken_15-0.0.1-6PGSTY.el10.x86_64.rpm
pg_tiktoken_150.0.1el10.aarch64pigsty2.0 MiBpg_tiktoken_15-0.0.1-6PGSTY.el10.aarch64.rpm
postgresql-15-pg-tiktoken0.0.1d12.x86_64pigsty1.8 MiBpostgresql-15-pg-tiktoken_0.0.1-5PGSTY~bookworm_amd64.deb
postgresql-15-pg-tiktoken0.0.1d12.aarch64pigsty1.6 MiBpostgresql-15-pg-tiktoken_0.0.1-5PGSTY~bookworm_arm64.deb
postgresql-15-pg-tiktoken0.0.1d13.x86_64pigsty1.8 MiBpostgresql-15-pg-tiktoken_0.0.1-5PGSTY~trixie_amd64.deb
postgresql-15-pg-tiktoken0.0.1d13.aarch64pigsty1.6 MiBpostgresql-15-pg-tiktoken_0.0.1-5PGSTY~trixie_arm64.deb
postgresql-15-pg-tiktoken0.0.1u22.x86_64pigsty2.0 MiBpostgresql-15-pg-tiktoken_0.0.1-5PGSTY~jammy_amd64.deb
postgresql-15-pg-tiktoken0.0.1u22.aarch64pigsty1.9 MiBpostgresql-15-pg-tiktoken_0.0.1-5PGSTY~jammy_arm64.deb
postgresql-15-pg-tiktoken0.0.1u24.x86_64pigsty2.0 MiBpostgresql-15-pg-tiktoken_0.0.1-5PGSTY~noble_amd64.deb
postgresql-15-pg-tiktoken0.0.1u24.aarch64pigsty1.9 MiBpostgresql-15-pg-tiktoken_0.0.1-5PGSTY~noble_arm64.deb
postgresql-15-pg-tiktoken0.0.1u26.x86_64pigsty2.0 MiBpostgresql-15-pg-tiktoken_0.0.1-5PGSTY~resolute_amd64.deb
postgresql-15-pg-tiktoken0.0.1u26.aarch64pigsty1.9 MiBpostgresql-15-pg-tiktoken_0.0.1-5PGSTY~resolute_arm64.deb

Source

pg_tiktoken-0.0.1+git20260825.99cb61d.tar.gz

pig build pkg pg_tiktoken;		# 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;		# install by package name

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

Create this extension with:

CREATE EXTENSION pg_tiktoken;

Usage

pg_tiktoken: tiktoken tokenizer for use with OpenAI models in PostgreSQL. Source: README.md

pg_tiktoken is a PostgreSQL extension that provides input tokenization using OpenAI’s tiktoken library. It allows you to count and encode tokens directly in SQL, which is useful for managing input length limits when working with OpenAI models.


Functions

tiktoken_count

Count the number of tokens for a given encoding or model:

SELECT tiktoken_count('p50k_edit', 'A long time ago in a galaxy far, far away');
 tiktoken_count
----------------
             11
(1 row)

tiktoken_encode

Get the token IDs for a given encoding or model:

SELECT tiktoken_encode('cl100k_base', 'A long time ago in a galaxy far, far away');
                  tiktoken_encode
----------------------------------------------------
 {32,1317,892,4227,304,264,34261,3117,11,3117,3201}
(1 row)

Both tiktoken_count and tiktoken_encode accept either an encoding name or an OpenAI model name as the first argument.


Supported Models

Encoding nameOpenAI models
cl100k_baseChatGPT models, text-embedding-ada-002
p50k_baseCode models, text-davinci-002, text-davinci-003
p50k_editEdit models like text-davinci-edit-001, code-davinci-edit-001
r50k_base (or gpt2)GPT-3 models like davinci

Table reader