opencc
opencc (developed in the pgsty/pg_opencc repository) compiles OpenCC and its full dictionary set directly into a single PostgreSQL shared library—no external dictionary files or system libopencc required at runtime.
Why run OpenCC inside PostgreSQL
Converting Simplified and Traditional Chinese in application code leaves your database unable to index or constrain normalized text. Because opencc embeds versioned dictionaries directly inside the extension, every conversion function is IMMUTABLE and PARALLEL SAFE within a release:
- Cross-script search indexes: Index
opencc.tw2sp(title)oropencc.t2s(title)so a search for英伟达or算法finds rows written as輝達or演算法using a normal B-tree index scan. - Stored generated columns and constraints: Maintain localized regional columns automatically or reject mixed-script text at
INSERTtime withCHECK (opencc.detect(body) = ...). - Context-aware phrases, not naive char maps: Compare character conversion (
s2t/s2tw) against phrase-aware localization (s2twp):
| Config | Input: 鼠标里面的硅二极管坏了,导致光标分辨率降低。 | What changed |
|---|---|---|
s2t | 鼠標裏面的硅二極管壞了,導致光標分辨率降低。 | OpenCC standard Traditional glyphs |
s2tw | 鼠標裡面的硅二極管壞了,導致光標分辨率降低。 | Taiwan glyph variant (裏 → 裡) |
s2twp | 滑鼠裡面的矽二極體壞了,導致游標解析度降低。 | Taiwan glyphs + regional vocabulary (鼠标→滑鼠, 硅→矽, 光标→游標, 分辨率→解析度) |
How opencc works
- Zero external dependencies: Dictionaries and configs are compiled into the extension binary with a SHA-256 digest (
opencc.version()). - Lazy per-backend loading (or shared preload): Each PostgreSQL backend loads a converter the first time it is called. For high-connection workloads using the same configs, preload them via
opencc.preloadso forked backends share read-only memory pages. - Safe dictionary upgrades: When a future release updates embedded dictionaries,
opencc.dependents()inspectspg_dependand generates the exactREINDEX,UPDATE,REFRESH, or constraint re-validation SQL for stored objects that rely onopencc.
Install opencc in a UTF8 PostgreSQL database and run your first script, phrase, and regional conversions in under three minutes.
Use opencc in real PostgreSQL workloads: cross-script search indexes, regional localization routing, script CHECK constraints, editorial ambiguity proofreading, shared memory preloading, and safe upgrades.
Complete SQL API lookup for opencc v0.1.0 (vendored OpenCC 1.4.2): all 16 built-in configs, conversion and inspection functions, catalog views, and the opencc.preload GUC.