Skip to main content

VillageSQL is a drop-in replacement for MySQL with extensions.

All examples in this guide work on VillageSQL. Install Now →
This guide requires VillageSQL 0.0.4 or later.
Two strings that refer to the same thing — a person’s name, a company, a place — often look different in practice. Misspellings, transliterations, transcription errors, and regional variations all produce variation that = and LIKE miss. VillageSQL’s vsql_fuzzystrmatch extension provides four approaches: Soundex (sounds-alike grouping), Levenshtein edit distance (character-by-character cost), Metaphone (pronunciation-based encoding), and Double Metaphone (handles ambiguous pronunciations from multiple linguistic origins).

Installation

All functions are in the vsql_fuzzystrmatch schema. All return NULL when any argument is NULL.

Soundex

Soundex reduces an English word to a 4-character code based on how it sounds. Words with the same code are considered phonetic matches.
difference returns a 0–4 score for how many Soundex code characters two strings share. 4 = identical codes.
Use Soundex when you want a simple sounds-alike grouping and your data is primarily English names. It’s fast and easy to explain, but it can conflate unrelated names and misses non-English phonetics.

Finding phonetic matches in a table

To find all names that are a close phonetic match (not necessarily identical codes):

Levenshtein edit distance

Levenshtein counts the minimum number of single-character edits — insertions, deletions, substitutions — needed to turn one string into another.
Use edit distance when you want to match strings that differ by a small number of typos, and you want precise control over the distance threshold.

Custom operation costs

levenshtein_cost lets you weight insertions, deletions, and substitutions differently:

Early-exit threshold

levenshtein_less_equal short-circuits once the distance exceeds a threshold — more efficient for filtering. When the actual distance exceeds max_d, it returns max_d + 1 instead of computing the full distance:
Combined with custom costs:
Both input strings must be ≤ 255 characters. Costs must be non-negative.

Deduplication by edit distance

Metaphone

Metaphone applies more English pronunciation rules than Soundex and produces variable-length output. The max_output_length parameter caps the result.
Use Metaphone when Soundex produces too many false positives or misses matches for less common names. It handles more pronunciation patterns (TH → 0, silent letters, etc.).

Double Metaphone

Double Metaphone handles ambiguous pronunciations from multiple linguistic origins — names of German, Slavic, Spanish, Hebrew, and other origins that get anglicized in different ways. It produces two codes: primary and alternate.
When no alternate exists, dmetaphone_alt falls back to the primary code. Use Double Metaphone when your data contains names from multiple linguistic backgrounds, or when Metaphone misses matches that a human would consider equivalent.

Matching with both codes

Choosing an approach

Function reference

Known limitations

No function overloading. VEF registers functions by name only; two functions with the same name cannot coexist. Where PostgreSQL uses overloads (levenshtein with 2 vs 5 args), this extension uses distinct names (levenshtein vs levenshtein_cost). String length limit. levenshtein inputs must be ≤ 255 characters.

See also