In PostgreSQL full-text search, what does a text search configuration control, and how do you add unaccent so 'parametres' matches 'paramètres'?
answer
- parser plus dictionary lists
- per token type mapping
- first recogniser wins
- filtering dictionary passes it on
- 18 reads with the cluster's provider
basics
~20 sA text search configuration binds a parser to a dictionary list per token type, which turns tokens into lexemes. Copy one such as french and ALTER MAPPING FOR hword, hword_part, word WITH unaccent, french_stem, so accents are stripped before stemming.
solid answer
~50 sThe parser splits text into typed tokens (`asciiword`, `word`, `hword`, `email`, `url` and so on); for each type the configuration lists dictionaries that are consulted in order until one recognises the token, and a token that is a stop word or recognised by none is dropped. A Snowball stemmer such as `french_stem` recognises everything, so it goes last. `unaccent` is a **filtering** dictionary: it rewrites the token and passes it on, so `CREATE TEXT SEARCH CONFIGURATION help_fr (COPY = french)` plus `ALTER MAPPING FOR hword, hword_part, word WITH unaccent, french_stem` makes both spellings stem to the same lexeme. Because the two-argument `to_tsvector('help_fr', body)` is immutable, it works in a stored generated column, whereas wrapping the text in the `STABLE` `unaccent()` function is rejected there. On PostgreSQL 18, dictionaries are read with the cluster's default collation provider.
code
sql · 12 linesCREATE EXTENSION IF NOT EXISTS unaccent;
CREATE TEXT SEARCH CONFIGURATION help_fr (COPY = french);
ALTER TEXT SEARCH CONFIGURATION help_fr
ALTER MAPPING FOR hword, hword_part, word
WITH unaccent, french_stem;
SELECT to_tsvector('help_fr', 'Paramètres du compte')
@@ websearch_to_tsquery('help_fr', 'parametres'); -- true
SELECT alias, token, dictionaries, lexemes
FROM ts_debug('help_fr', 'Paramètres');go deeper
Recall that a configuration such as english or french decides how words become lexemes, and that it must be the same for documents and queries.
Explain parser token types, the ordered dictionary list, why the stemmer goes last and how a filtering dictionary such as unaccent passes its output on.
Avoid the STABLE unaccent() trap in generated columns and indexes, plan recomputation when a configuration changes, and reindex FTS and pg_trgm indexes after upgrading an ICU or builtin cluster to 18.
Weigh per-language configurations, a per-row regconfig column or one simple configuration against the relevance each gives, and own the maintenance cost of custom dictionaries and rules files.
## What a configuration is made of Every full-text function that takes a `regconfig` argument (`to_tsvector`, `websearch_to_tsquery`, `ts_headline` and the rest) delegates language handling to a **text search configuration**. A configuration has two parts: - a **parser**, which splits text into tokens and labels each with a **token type**: `asciiword` (letters, all ASCII), `word` (letters including non-ASCII ones), `hword` and `hword_part` (a hyphenated word and its parts), `email`, `url`, `int` and more; - a **mapping** from each token type to an ordered list of **dictionaries**, which turn tokens into lexemes. PostgreSQL ships configurations for many languages (`english`, `french`, `german`, `simple` and others). `default_text_search_config` names the one used when a function call omits the argument; its built-in default is `pg_catalog.simple`, and `initdb` normally writes a language configuration matching the cluster's `lc_ctype` into `postgresql.conf`. ## How dictionaries are consulted For every token, the dictionaries listed for its type run **in order**: 1. A dictionary that recognises the token returns one or more lexemes, and normally the remaining dictionaries are skipped. 2. A dictionary that recognises the token as a **stop word** causes it to be discarded. 3. A dictionary that does not recognise the token passes it to the next one. 4. A token no dictionary recognises is discarded. A **filtering dictionary** breaks rule 1 on purpose: it replaces the token with a modified one and hands it on. The general rule is therefore *narrow dictionaries first, a catch-all last*. Snowball stemmers (`english_stem`, `french_stem`) and `simple` recognise everything, so anything placed after them never runs. | Dictionary kind | Template | Typical role | |---|---|---| | Snowball stemmer | `snowball` | strips suffixes; catch-all, goes last | | Simple | `simple` | lower-cases, optional stop words; catch-all | | Synonym | `synonym` | maps a word to another, file `$SHAREDIR/tsearch_data/<name>.syn` | | Thesaurus | `thesaurus` | replaces phrases with a preferred term | | Ispell | `ispell` | dictionary-based normal forms | | unaccent | `unaccent` (contrib) | filtering: removes diacritics, then passes on | ## Adding unaccent for a French help centre A vendor's French help articles say *Paramètres du compte*; users type `parametres`. The `unaccent` contrib extension provides both a dictionary and a function. It is marked trusted, so a non-superuser with `CREATE` privilege on the database can install it. Build your own configuration rather than altering the shipped one: - `CREATE TEXT SEARCH CONFIGURATION help_fr (COPY = french);` - `ALTER TEXT SEARCH CONFIGURATION help_fr ALTER MAPPING FOR hword, hword_part, word WITH unaccent, french_stem;` Only the token types that can contain non-ASCII letters need the extra step; a token already all-ASCII is an `asciiword` and goes straight to `french_stem`. Now `Paramètres` is unaccented to `Parametres`, then stemmed, and the query word `parametres` is stemmed to the same lexeme, so the match succeeds in both directions. `ts_debug('help_fr', 'Paramètres')` shows which dictionaries saw the token and what each returned. ## Why the dictionary beats the function The tempting shortcut is `to_tsvector('french', unaccent(body))`. The two-argument `to_tsvector` is immutable, but `unaccent()` is declared **`STABLE`**, because its result depends on a dictionary and rules file that can change. A stored generated column then fails with `generation expression is not immutable`, and an expression index fails with `functions in index expression must be marked IMMUTABLE`. Putting `unaccent` inside the configuration keeps the expression to `to_tsvector('help_fr', body)`, which is accepted. The cost is a promise you must keep: if you later change the configuration or its rules file, stored `tsvector` values and indexes do not update themselves; recompute the stored values and rebuild the indexes. ## PostgreSQL 18: dictionaries follow the cluster's collation provider Through 17, full-text search always used **libc** to read configuration files and dictionaries and to process characters by `LC_CTYPE` rules. From **18** it uses the **cluster's default collation provider**. On a libc cluster nothing changes. On a cluster whose default provider is ICU or builtin, some full-text functions and `pg_trgm` can now behave differently for characters where those providers disagree with libc. The 18 release notes therefore recommend that clusters upgraded with `pg_upgrade` **reindex every full-text and `pg_trgm` index** after the upgrade; stored `tsvector` columns built by the old rules deserve the same review. How the three providers differ is collation material.
- Why not simply write to_tsvector('french', unaccent(body)) in the generated column?`unaccent()` is declared `STABLE`, so the expression is not immutable: a stored generated column fails with `generation expression is not immutable` and an expression index is rejected too. With `unaccent` inside the configuration, `to_tsvector('help_fr', body)` stays immutable and is accepted, at the price of recomputing stored values yourself if the configuration ever changes.
- What changed for text search in PostgreSQL 18, and what do you do after pg_upgrade?Full-text search now reads configuration files and dictionaries with the cluster's default collation provider rather than always libc. On ICU or builtin clusters some full-text functions and `pg_trgm` can behave differently for certain characters, so the release notes recommend reindexing all full-text and `pg_trgm` indexes after `pg_upgrade`.
- How do you see which dictionary handled each token?`ts_debug('help_fr', 'text')` returns one row per token with its type alias, the dictionaries consulted, the one that answered and the lexemes produced. `ts_lexize('french_stem', 'paramètres')` tests a single dictionary on its own.
- unaccent() is immutable, so it can wrap the text inside a generated tsvector column.
- Put the stemmer first in the dictionary list so stemming happens before anything else.
- The parser decides which words are stop words.
- Altering a configuration's mapping updates existing stored tsvectors automatically.
- Only a superuser can install the unaccent extension.
Assumes PostgreSQL 18, where full-text search reads dictionaries with the cluster's default collation provider instead of libc; ICU or builtin clusters should reindex FTS and pg_trgm indexes after pg_upgrade.