Tokenization Models
In pg_tokenizer, a Model maps string tokens (or raw text) to integer token IDs (INT[]). You can choose from four built-in pretrained models, train a custom vocabulary model directly on your PostgreSQL table, import any HuggingFace tokenizer.json, or use a Lindera morphological model.
1. Built-in Pretrained Models
| Model Name | Source / Architecture | Requires text_analyzer? |
Memory Footprint | Best For |
|---|---|---|---|---|
llmlingua2 |
microsoft/llmlingua-2-xlm-roberta-large-meetingbank (XLM-RoBERTa multilingual subword) |
No (forbidden) | ~200 MB (default preloaded) | Multilingual general text out of the box without training a custom vocabulary. |
bert_base_uncased |
google-bert/bert-base-uncased (WordPiece, 30,522 vocab) |
No (forbidden) | ~15 MB | General English text; fast startup and compact vocabulary. |
wiki_tocken |
Snowolf/wiki-tocken (Built-in English Wikipedia token dictionary) |
Yes (required) | ~30 MB | English full-text search combined with custom stemmers, stopwords, and synonyms. |
gemma2b |
google/gemma-2b (SentencePiece BPE, 256k vocab) |
No (forbidden) | ~100 MB | Code, technical identifiers, and multilingual LLM workloads. |
Example: Using bert_base_uncased or llmlingua2
2. Custom Corpus Models (create_custom_model)
For maximum BM25 accuracy and minimal memory footprint, you can build a custom vocabulary model directly from your own table column. Unlike subword LLM tokenizers (which split rare domain words into multiple sub-tokens), a custom model assigns a dedicated 1:1 integer ID to every unique normalized token produced by your text_analyzer—and weighs only a few megabytes in memory.
Option A: One-Step Helper (create_custom_model_tokenizer_and_trigger)
This convenience function creates the custom model, creates the tokenizer, populates the target bm25vector column for all existing rows, and installs a BEFORE INSERT OR UPDATE trigger that automatically learns new vocabulary tokens and updates the bm25vector column when rows change:
Option B: Step-by-Step (create_custom_model Without Trigger)
If you prefer not to attach an automatic trigger (or want to control when embedding is populated):
3. Importing HuggingFace Tokenizers (create_huggingface_model)
You can import any HuggingFace tokenizers JSON specification (tokenizer.json) into PostgreSQL using create_huggingface_model.
In psql, you can fetch tokenizer.json directly from HuggingFace Hub into a variable using \set and pass it to create_huggingface_model:
Or pass the contents of a tokenizer.json file directly as a dollar-quoted string:
4. Lindera Morphological Models (create_lindera_model)
For East Asian languages such as Japanese, Korean, and Chinese that benefit from morphological dictionary analysis, pg_tokenizer integrates lindera.
If you build pg_tokenizer from source, enable the desired Lindera dictionary features at compile time:
Example configuration using ipadic with iteration mark normalization, NFKC Unicode normalization, character mapping, compound word merging, number normalization, Japanese part-of-speech stop-tags, Katakana stemming, and diacritical mark removal:
5. Model Preloading (add_preload_model)
Large models (llmlingua2 at ~200 MB, gemma2b at ~100 MB, japanese_ipadic) take several seconds and significant RAM to load on first query in each new PostgreSQL backend process. By preloading models during shared_preload_libraries startup, PostgreSQL loads the model once in the postmaster process and shares it across all backend connections via copy-on-write memory (llmlingua2 is preloaded by default):
Restart PostgreSQL after calling add_preload_model(...) (with shared_preload_libraries = 'pg_tokenizer.so' set in postgresql.conf) for preloading to take effect across all backends.