Skip to content

opencc

Convert Chinese scripts and Mainland, Taiwan, and Hong Kong vocabulary directly inside PostgreSQL with embedded OpenCC dictionaries. Ideal for cross-script search indexes, regional content localization, script validation, and editorial proofreading.

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) or opencc.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 INSERT time with CHECK (opencc.detect(body) = ...).
  • Context-aware phrases, not naive char maps: Compare character conversion (s2t / s2tw) against phrase-aware localization (s2twp):
ConfigInput: 鼠标里面的硅二极管坏了,导致光标分辨率降低。What changed
s2t鼠標裏面的硅二極管壞了,導致光標分辨率降低。OpenCC standard Traditional glyphs
s2tw鼠標裡面的硅二極管壞了,導致光標分辨率降低。Taiwan glyph variant (裏 → 裡)
s2twp滑鼠裡面的矽二極體壞了,導致游標解析度降低。Taiwan glyphs + regional vocabulary (鼠标→滑鼠, 硅→矽, 光标→游標, 分辨率→解析度)

How opencc works

  1. Zero external dependencies: Dictionaries and configs are compiled into the extension binary with a SHA-256 digest (opencc.version()).
  2. 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.preload so forked backends share read-only memory pages.
  3. Safe dictionary upgrades: When a future release updates embedded dictionaries, opencc.dependents() inspects pg_depend and generates the exact REINDEX, UPDATE, REFRESH, or constraint re-validation SQL for stored objects that rely on opencc.

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.