Guides and operations
1. Cross-script search with expression indexes
Because opencc functions are IMMUTABLE, you can normalize titles inside an expression index. Folding Taiwan Traditional and regional vocabulary into Simplified Chinese (opencc.tw2sp) lets a single query find every spelling variant via an index scan:
Both opencc.tw2sp(title) and opencc.convert(title, 'tw2sp') expand to the same SQL expression, so either form hits articles_fold_idx. On PostgreSQL 18+, write STORED explicitly on generated columns that call extension functions.
2. Regional localization with opencc.localize()
Use opencc.localize(input, target, source => 'auto') when your application works with locale tags (zh-Hans, zh-Hant, zh-CN, zh-TW, zh-HK, ja) instead of raw OpenCC config names. Region tags are case-insensitive and accept _ in place of - (zh-tw equals ZH_TW).
Important: source => 'auto' detects script, not region
Auto-detection tells Simplified from Traditional or Japanese, so converting from Taiwan or Hong Kong regional vocabulary back to Mainland terms requires naming source explicitly:
Inspect the exact config chain used for any region pair:
3. Script detection and CHECK constraints
opencc.detect() classifies text as simplified, traditional, mixed, japanese, neutral (Han characters shared across scripts, such as 中文), or none (no CJK characters). opencc.script_stats() returns the exact counts (han, simplified, traditional, kana):
Because opencc.detect() is IMMUTABLE, you can enforce a single script right in a table CHECK constraint (use full sentences rather than isolated characters, since shared characters classify as neutral):
4. Editorial proofreading and tracing
When publishing books, subtitles, or formal documents, Simplified-to-Traditional conversion has classic one-to-many traps (such as personal names like 文丑 or polysemous characters like 里). Use opencc.ambiguities(), opencc.candidates(), and opencc.explain() to flag spans for human review:
5. Preloading, memory, and safe upgrades
Share converter memory across connections
By default, each backend loads a config into private memory on its first call. For servers with hundreds of connections calling the same configs, preload only those configs at startup so forked backends share read-only pages via copy-on-write:
Restart PostgreSQL, then verify which configs are loaded in the current backend:
See the initial release memory and throughput measurements for per-config footprint details.
Upgrade safely when dictionaries change
A new opencc release may ship updated OpenCC dictionaries whose conversion output differs slightly from the previous release. After upgrading, run opencc.dependents() to inventory every stored object (indexes, stored generated columns, CHECK constraints, materialized views) that directly calls opencc functions, together with the exact SQL to rebuild or revalidate each one:
Note that opencc.dependents() reports direct catalog dependencies; calls wrapped inside application PL/pgSQL functions require a manual check. Running DROP EXTENSION opencc leaves the empty fixed opencc schema in place.