Skip to content

pg_pinyin

Pinyin romanization and search helpers for PostgreSQL

pg_pinyin · Pinyin romanization and search helpers for PostgreSQL

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
2200pg_pinyinpg_pinyin0.0.10FTSMITRust
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-rNoYesNoYesyesno
Optional pg_search tokenizer overload.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY0.0.1019 18 17 16 15pg_pinyin-
RPMPIGSTY0.0.1019 18 17 16 15pg_pinyin_$v-
DEBPIGSTY0.0.1019 18 17 16 15postgresql-$v-pinyin-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
el8.aarch64N/APIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
el9.x86_64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
el9.aarch64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
el10.x86_64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
el10.aarch64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
d12.x86_64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
d12.aarch64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
d13.x86_64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
d13.aarch64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
u22.x86_64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
u22.aarch64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
u24.x86_64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
u24.aarch64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
u26.x86_64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
u26.aarch64PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10PIGSTY 0.0.10
PackageVersionOSORGSIZEFile URL
pg_pinyin_180.0.10el8.x86_64pigsty3.2 MiBpg_pinyin_18-0.0.10-1PGSTY.el8.x86_64.rpm
pg_pinyin_180.0.10el8.aarch64pigsty3.0 MiBpg_pinyin_18-0.0.10-1PGSTY.el8.aarch64.rpm
pg_pinyin_180.0.10el9.x86_64pigsty3.1 MiBpg_pinyin_18-0.0.10-1PGSTY.el9.x86_64.rpm
pg_pinyin_180.0.10el9.aarch64pigsty3.0 MiBpg_pinyin_18-0.0.10-1PGSTY.el9.aarch64.rpm
pg_pinyin_180.0.10el10.x86_64pigsty3.1 MiBpg_pinyin_18-0.0.10-1PGSTY.el10.x86_64.rpm
pg_pinyin_180.0.10el10.aarch64pigsty3.0 MiBpg_pinyin_18-0.0.10-1PGSTY.el10.aarch64.rpm
postgresql-18-pinyin0.0.10d12.x86_64pigsty2.7 MiBpostgresql-18-pinyin_0.0.10-1PGSTY~bookworm_amd64.deb
postgresql-18-pinyin0.0.10d12.aarch64pigsty2.5 MiBpostgresql-18-pinyin_0.0.10-1PGSTY~bookworm_arm64.deb
postgresql-18-pinyin0.0.10d13.x86_64pigsty2.7 MiBpostgresql-18-pinyin_0.0.10-1PGSTY~trixie_amd64.deb
postgresql-18-pinyin0.0.10d13.aarch64pigsty2.5 MiBpostgresql-18-pinyin_0.0.10-1PGSTY~trixie_arm64.deb
postgresql-18-pinyin0.0.10u22.x86_64pigsty3.0 MiBpostgresql-18-pinyin_0.0.10-1PGSTY~jammy_amd64.deb
postgresql-18-pinyin0.0.10u22.aarch64pigsty2.9 MiBpostgresql-18-pinyin_0.0.10-1PGSTY~jammy_arm64.deb
postgresql-18-pinyin0.0.10u24.x86_64pigsty3.0 MiBpostgresql-18-pinyin_0.0.10-1PGSTY~noble_amd64.deb
postgresql-18-pinyin0.0.10u24.aarch64pigsty2.8 MiBpostgresql-18-pinyin_0.0.10-1PGSTY~noble_arm64.deb
postgresql-18-pinyin0.0.10u26.x86_64pigsty2.9 MiBpostgresql-18-pinyin_0.0.10-1PGSTY~resolute_amd64.deb
postgresql-18-pinyin0.0.10u26.aarch64pigsty2.8 MiBpostgresql-18-pinyin_0.0.10-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_pinyin_170.0.10el8.x86_64pigsty3.2 MiBpg_pinyin_17-0.0.10-1PGSTY.el8.x86_64.rpm
pg_pinyin_170.0.10el8.aarch64pigsty3.0 MiBpg_pinyin_17-0.0.10-1PGSTY.el8.aarch64.rpm
pg_pinyin_170.0.10el9.x86_64pigsty3.1 MiBpg_pinyin_17-0.0.10-1PGSTY.el9.x86_64.rpm
pg_pinyin_170.0.10el9.aarch64pigsty3.0 MiBpg_pinyin_17-0.0.10-1PGSTY.el9.aarch64.rpm
pg_pinyin_170.0.10el10.x86_64pigsty3.1 MiBpg_pinyin_17-0.0.10-1PGSTY.el10.x86_64.rpm
pg_pinyin_170.0.10el10.aarch64pigsty3.0 MiBpg_pinyin_17-0.0.10-1PGSTY.el10.aarch64.rpm
postgresql-17-pinyin0.0.10d12.x86_64pigsty2.7 MiBpostgresql-17-pinyin_0.0.10-1PGSTY~bookworm_amd64.deb
postgresql-17-pinyin0.0.10d12.aarch64pigsty2.5 MiBpostgresql-17-pinyin_0.0.10-1PGSTY~bookworm_arm64.deb
postgresql-17-pinyin0.0.10d13.x86_64pigsty2.7 MiBpostgresql-17-pinyin_0.0.10-1PGSTY~trixie_amd64.deb
postgresql-17-pinyin0.0.10d13.aarch64pigsty2.5 MiBpostgresql-17-pinyin_0.0.10-1PGSTY~trixie_arm64.deb
postgresql-17-pinyin0.0.10u22.x86_64pigsty3.0 MiBpostgresql-17-pinyin_0.0.10-1PGSTY~jammy_amd64.deb
postgresql-17-pinyin0.0.10u22.aarch64pigsty2.8 MiBpostgresql-17-pinyin_0.0.10-1PGSTY~jammy_arm64.deb
postgresql-17-pinyin0.0.10u24.x86_64pigsty3.0 MiBpostgresql-17-pinyin_0.0.10-1PGSTY~noble_amd64.deb
postgresql-17-pinyin0.0.10u24.aarch64pigsty2.8 MiBpostgresql-17-pinyin_0.0.10-1PGSTY~noble_arm64.deb
postgresql-17-pinyin0.0.10u26.x86_64pigsty2.9 MiBpostgresql-17-pinyin_0.0.10-1PGSTY~resolute_amd64.deb
postgresql-17-pinyin0.0.10u26.aarch64pigsty2.8 MiBpostgresql-17-pinyin_0.0.10-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_pinyin_160.0.10el8.x86_64pigsty3.2 MiBpg_pinyin_16-0.0.10-1PGSTY.el8.x86_64.rpm
pg_pinyin_160.0.10el8.aarch64pigsty3.0 MiBpg_pinyin_16-0.0.10-1PGSTY.el8.aarch64.rpm
pg_pinyin_160.0.10el9.x86_64pigsty3.1 MiBpg_pinyin_16-0.0.10-1PGSTY.el9.x86_64.rpm
pg_pinyin_160.0.10el9.aarch64pigsty3.0 MiBpg_pinyin_16-0.0.10-1PGSTY.el9.aarch64.rpm
pg_pinyin_160.0.10el10.x86_64pigsty3.1 MiBpg_pinyin_16-0.0.10-1PGSTY.el10.x86_64.rpm
pg_pinyin_160.0.10el10.aarch64pigsty3.0 MiBpg_pinyin_16-0.0.10-1PGSTY.el10.aarch64.rpm
postgresql-16-pinyin0.0.10d12.x86_64pigsty2.7 MiBpostgresql-16-pinyin_0.0.10-1PGSTY~bookworm_amd64.deb
postgresql-16-pinyin0.0.10d12.aarch64pigsty2.5 MiBpostgresql-16-pinyin_0.0.10-1PGSTY~bookworm_arm64.deb
postgresql-16-pinyin0.0.10d13.x86_64pigsty2.7 MiBpostgresql-16-pinyin_0.0.10-1PGSTY~trixie_amd64.deb
postgresql-16-pinyin0.0.10d13.aarch64pigsty2.5 MiBpostgresql-16-pinyin_0.0.10-1PGSTY~trixie_arm64.deb
postgresql-16-pinyin0.0.10u22.x86_64pigsty3.0 MiBpostgresql-16-pinyin_0.0.10-1PGSTY~jammy_amd64.deb
postgresql-16-pinyin0.0.10u22.aarch64pigsty2.8 MiBpostgresql-16-pinyin_0.0.10-1PGSTY~jammy_arm64.deb
postgresql-16-pinyin0.0.10u24.x86_64pigsty3.0 MiBpostgresql-16-pinyin_0.0.10-1PGSTY~noble_amd64.deb
postgresql-16-pinyin0.0.10u24.aarch64pigsty2.8 MiBpostgresql-16-pinyin_0.0.10-1PGSTY~noble_arm64.deb
postgresql-16-pinyin0.0.10u26.x86_64pigsty2.9 MiBpostgresql-16-pinyin_0.0.10-1PGSTY~resolute_amd64.deb
postgresql-16-pinyin0.0.10u26.aarch64pigsty2.8 MiBpostgresql-16-pinyin_0.0.10-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_pinyin_150.0.10el8.x86_64pigsty3.2 MiBpg_pinyin_15-0.0.10-1PGSTY.el8.x86_64.rpm
pg_pinyin_150.0.10el8.aarch64pigsty3.0 MiBpg_pinyin_15-0.0.10-1PGSTY.el8.aarch64.rpm
pg_pinyin_150.0.10el9.x86_64pigsty3.1 MiBpg_pinyin_15-0.0.10-1PGSTY.el9.x86_64.rpm
pg_pinyin_150.0.10el9.aarch64pigsty3.0 MiBpg_pinyin_15-0.0.10-1PGSTY.el9.aarch64.rpm
pg_pinyin_150.0.10el10.x86_64pigsty3.1 MiBpg_pinyin_15-0.0.10-1PGSTY.el10.x86_64.rpm
pg_pinyin_150.0.10el10.aarch64pigsty3.0 MiBpg_pinyin_15-0.0.10-1PGSTY.el10.aarch64.rpm
postgresql-15-pinyin0.0.10d12.x86_64pigsty2.7 MiBpostgresql-15-pinyin_0.0.10-1PGSTY~bookworm_amd64.deb
postgresql-15-pinyin0.0.10d12.aarch64pigsty2.5 MiBpostgresql-15-pinyin_0.0.10-1PGSTY~bookworm_arm64.deb
postgresql-15-pinyin0.0.10d13.x86_64pigsty2.7 MiBpostgresql-15-pinyin_0.0.10-1PGSTY~trixie_amd64.deb
postgresql-15-pinyin0.0.10d13.aarch64pigsty2.5 MiBpostgresql-15-pinyin_0.0.10-1PGSTY~trixie_arm64.deb
postgresql-15-pinyin0.0.10u22.x86_64pigsty3.0 MiBpostgresql-15-pinyin_0.0.10-1PGSTY~jammy_amd64.deb
postgresql-15-pinyin0.0.10u22.aarch64pigsty2.8 MiBpostgresql-15-pinyin_0.0.10-1PGSTY~jammy_arm64.deb
postgresql-15-pinyin0.0.10u24.x86_64pigsty3.0 MiBpostgresql-15-pinyin_0.0.10-1PGSTY~noble_amd64.deb
postgresql-15-pinyin0.0.10u24.aarch64pigsty2.8 MiBpostgresql-15-pinyin_0.0.10-1PGSTY~noble_arm64.deb
postgresql-15-pinyin0.0.10u26.x86_64pigsty2.9 MiBpostgresql-15-pinyin_0.0.10-1PGSTY~resolute_amd64.deb
postgresql-15-pinyin0.0.10u26.aarch64pigsty2.8 MiBpostgresql-15-pinyin_0.0.10-1PGSTY~resolute_arm64.deb

Source

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

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

Create this extension with:

CREATE EXTENSION pg_pinyin;

Usage

Sources:

pg_pinyin romanizes Chinese text and exposes tokenizer and query helpers for search applications. Use pg_pinyin to create stable Pinyin search keys, tokenize Han text, or expand Pinyin input into a pg_search regular-expression query.

0.0.10 supports PostgreSQL 15–19. Install the library for the matching server major before applying the packaged SQL update.

Create the Extension

CREATE EXTENSION pg_pinyin;

The extension is relocatable and does not require shared_preload_libraries or a server restart.

Romanize Text

Romanize character by character or use word-aware segmentation:

SELECT pinyin_char_romanize('重庆');
SELECT pinyin_word_romanize('重庆火锅');
SELECT pinyin_word_romanize('重庆火锅', '_custom');

The optional suffix selects custom dictionary tables; it is not an output separator. For example, _custom selects pinyin.pinyin_mapping_custom and pinyin.pinyin_words_custom, whose entries override the built-in dictionaries. Word mode uses dictionary segmentation to resolve contextual pronunciations. Clear the suffix cache after modifying these tables using public.pinyin_clear_suffix_cache(’_custom’).

Use pg_search Tokenizer Input

Word romanization also accepts a pg_search tokenizer result when that extension is available:

SELECT pinyin_word_romanize(
  description::pdb.icu::text[]
)
FROM documents;

The overload returns romanized text; it does not expose a row-per-token API. Use the plain-text overload when pg_search tokenization is not required.

Build a pg_search Query

When pg_search was installed before pg_pinyin, pg_pinyin provides a typed overload that returns pdb.query:

SELECT *
FROM documents
WHERE id @@@ pinyin_regex_phrase(
  'chong qing',
  slope => 1,
  max_expansions => 64,
  generated_pinyin => true
);

If pg_search is absent, the same entry point is installed as an error-reporting stub rather than silently returning a different type. Install dependencies in the intended order and test the function signature after upgrades.

Object Index

  • pinyin_char_romanize(text [, suffix]) returns character-based Pinyin text.
  • pinyin_word_romanize(text [, suffix]) returns dictionary-segmented Pinyin text.
  • pinyin_word_romanize(tokenizer_input [, suffix]) accepts a pg_search tokenizer result.
  • pinyin_regex_phrase(text, slope, max_expansions, generated_pinyin) constructs a pg_search Pinyin phrase query when that integration is available.
  • pinyin_regex_phrase_patterns is an internal pattern-building helper; prefer the public query function.

Operational Notes

The extension ships generated character and word dictionaries in its pinyin schema. Treat those tables as extension-managed data rather than application tables. Romanization is normalization, not translation, and ambiguous or domain-specific readings may require application-side review.

0.0.9 query modes and batch input

Version 0.0.9 used pgrx 0.20.0 and introduced the query modes and batch interfaces below. PostgreSQL 19 was a release candidate at the time.

Explicit apostrophes preserve syllable boundaries such as xi’an. pinyin_regex_phrase_mode adds opt-in full, initials, autocomplete and auto modes; pinyin_phrase provides exact native term/phrase queries for plain syllable fields. Use a mode matching the field encoding and user intent. pg_search remains optional and its native phrase APIs must be available for that integration.

pinyin_word_romanize_batch(text[]) accepts independent plain-text documents with NULL and optional suffix handling. Use the documented COPY staging/batch workflow for imports. Embedded Rust pattern parsers are immutable and parallel safe without runtime SPI; this does not imply every dictionary-dependent romanization function has the same contract. Existing defaults remain available; malformed explicit-mode input raises errors rather than searching a silently truncated prefix.

0.0.10 Parsing and Compatibility

Version 0.0.10 adds PostgreSQL 19 GA support. Its minimum server minors are 15.20, 16.16, 17.12, 18.7 and 19.0; source builds use pgrx 0.20.1. The core extension does not require pg_search; the release’s pg_search integration was validated separately on PostgreSQL 18 with pg_search 0.27.0.

Longest-first parsing can now backtrack when a complete sequence exists: tuikuanguize resolves to tui kuan gui ze. Explicit spaces, apostrophes and tone boundaries still constrain parsing. Dictionary lookup caches now respect snapshot and command state, savepoint rollback, dictionary refresh and suffix-table changes. Tokenizer cast caching respects role and privilege changes, including casts returning SQL NULL.

After installing matching files, reconnect and run ALTER EXTENSION pg_pinyin UPDATE TO '0.0.10'. The migration preserves live dictionaries and stored generated values; it does not retroactively recompute stored search keys. Independent SQL-backend users must refresh sql/pinyin.sql and sql/word.sql. Test existing search inputs and regenerate derived keys deliberately when required.

Table reader