pg_similarity

support similarity queries

Overview

PackageVersionCategoryLicenseLanguage
pg_similarity1.0RAGBSD 3-ClauseC
IDExtensionBinLibLoadCreateTrustRelocSchema
1840pg_similarityNoYesNoYesNoYes-
Relatedvector smlar fuzzystrmatch pg_trgm vchord pg_bigm citext unaccent

Version

TypeRepoVersionPG VerPackageDeps
EXTMIXED1.01817161514pg_similarity-
RPMPIGSTY1.01817161514pg_similarity_$v-
DEBPGDG1.01817161514postgresql-$v-similarity-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el9.x86_64
el9.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el10.x86_64
el10.aarch64
d12.x86_64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
d12.aarch64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
d13.x86_64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
d13.aarch64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
u22.x86_64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
u22.aarch64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
u24.x86_64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
u24.aarch64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0

Build

You can build the RPM packages for pg_similarity using pig build:

pig build pkg pg_similarity         # build RPM packages

Install

You can install pg_similarity directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pg_similarity;          # Install for current active PG version
pig ext install -y pg_similarity -v 18  # PG 18
pig ext install -y pg_similarity -v 17  # PG 17
pig ext install -y pg_similarity -v 16  # PG 16
pig ext install -y pg_similarity -v 15  # PG 15
pig ext install -y pg_similarity -v 14  # PG 14
dnf install -y pg_similarity_18       # PG 18
dnf install -y pg_similarity_17       # PG 17
dnf install -y pg_similarity_16       # PG 16
dnf install -y pg_similarity_15       # PG 15
dnf install -y pg_similarity_14       # PG 14
apt install -y postgresql-18-similarity   # PG 18
apt install -y postgresql-17-similarity   # PG 17
apt install -y postgresql-16-similarity   # PG 16
apt install -y postgresql-15-similarity   # PG 15
apt install -y postgresql-14-similarity   # PG 14

Create Extension:

CREATE EXTENSION pg_similarity;

Usage

pg_similarity: Support similarity queries on PostgreSQL. Source: README.md

pg_similarity is an extension to support similarity queries on PostgreSQL. The implementation is tightly integrated in the RDBMS in the sense that it defines operators so instead of the traditional operators (= and <>) you can use ~~~ and ~!~ (any of these operators represents a similarity function).

pg_similarity has three main components:

  • Functions: a set of functions that implements similarity algorithms available in the literature. These functions can be used as UDFs and will be the base for implementing the similarity operators;
  • Operators: a set of operators defined at the top of similarity functions. They use similarity functions to obtain the similarity threshold and compare its value to a user-defined threshold to decide if it is a match or not;
  • Session Variables: a set of variables that store similarity function parameters. These variables can be defined at run time.

Functions and Operators

This extension supports a set of similarity algorithms. The most known algorithms are covered by this extension. You must be aware that each algorithm is suited for a specific domain. The following algorithms are provided:

  • L1 Distance (as known as City Block or Manhattan Distance)
  • Cosine Distance
  • Dice Coefficient
  • Euclidean Distance
  • Hamming Distance
  • Jaccard Coefficient
  • Jaro Distance
  • Jaro-Winkler Distance
  • Levenshtein Distance
  • Matching Coefficient
  • Monge-Elkan Coefficient
  • Needleman-Wunsch Coefficient
  • Overlap Coefficient
  • Q-Gram Distance
  • Smith-Waterman Coefficient
  • Smith-Waterman-Gotoh Coefficient
  • Soundex Distance
AlgorithmFunctionOperatorUse Index?Parameters
L1 Distanceblock(text, text) returns float8~++yespg_similarity.block_tokenizer, pg_similarity.block_threshold, pg_similarity.block_is_normalized
Cosine Distancecosine(text, text) returns float8~##yespg_similarity.cosine_tokenizer, pg_similarity.cosine_threshold, pg_similarity.cosine_is_normalized
Dice Coefficientdice(text, text) returns float8~-~yespg_similarity.dice_tokenizer, pg_similarity.dice_threshold, pg_similarity.dice_is_normalized
Euclidean Distanceeuclidean(text, text) returns float8~!!yespg_similarity.euclidean_tokenizer, pg_similarity.euclidean_threshold, pg_similarity.euclidean_is_normalized
Hamming Distancehamming(bit varying, bit varying) returns float8 / hamming_text(text, text) returns float8~@~nopg_similarity.hamming_threshold, pg_similarity.hamming_is_normalized
Jaccard Coefficientjaccard(text, text) returns float8~??yespg_similarity.jaccard_tokenizer, pg_similarity.jaccard_threshold, pg_similarity.jaccard_is_normalized
Jaro Distancejaro(text, text) returns float8~%%nopg_similarity.jaro_threshold, pg_similarity.jaro_is_normalized
Jaro-Winkler Distancejarowinkler(text, text) returns float8~@@nopg_similarity.jarowinkler_threshold, pg_similarity.jarowinkler_is_normalized
Levenshtein Distancelev(text, text) returns float8~==nopg_similarity.levenshtein_threshold, pg_similarity.levenshtein_is_normalized
Matching Coefficientmatchingcoefficient(text, text) returns float8~^^yespg_similarity.matching_tokenizer, pg_similarity.matching_threshold, pg_similarity.matching_is_normalized
Monge-Elkan Coefficientmongeelkan(text, text) returns float8~||nopg_similarity.mongeelkan_tokenizer, pg_similarity.mongeelkan_threshold, pg_similarity.mongeelkan_is_normalized
Needleman-Wunsch Coefficientneedlemanwunsch(text, text) returns float8~#~nopg_similarity.nw_threshold, pg_similarity.nw_is_normalized
Overlap Coefficientoverlapcoefficient(text, text) returns float8~**yespg_similarity.overlap_tokenizer, pg_similarity.overlap_threshold, pg_similarity.overlap_is_normalized
Q-Gram Distanceqgram(text, text) returns float8~~~yespg_similarity.qgram_threshold, pg_similarity.qgram_is_normalized
Smith-Waterman Coefficientsmithwaterman(text, text) returns float8~=~nopg_similarity.sw_threshold, pg_similarity.sw_is_normalized
Smith-Waterman-Gotoh Coefficientsmithwatermangotoh(text, text) returns float8~!~nopg_similarity.swg_threshold, pg_similarity.swg_is_normalized
Soundex Distancesoundex(text, text) returns float8~*~no

Parameters

The several parameters control the behavior of the pg_similarity functions and operators. They can be classified in three classes:

  • tokenizer: controls how the strings are tokenized. Valid values are alnum, gram, word, and camelcase. All tokens are lowercase. Default is alnum.
    • alnum: delimiters are any non-alphanumeric characters.
    • gram: an n-gram is a subsequence of length n, extracted using sliding-by-one technique.
    • word: delimiters are white space characters.
    • camelcase: delimiters are capitalized characters but they are also included as first token characters.
  • threshold: controls how flexible the result set will be. Values range from 0.0 to 1.0. Default is 0.7.
  • normalized: controls whether the similarity coefficient/distance is normalized (between 0.0 and 1.0) or not. Default is true.

Examples

Set parameters at run time:

SHOW pg_similarity.levenshtein_threshold;
-- 0.7

SET pg_similarity.levenshtein_threshold TO 0.5;

SET pg_similarity.cosine_tokenizer TO camelcase;

SET pg_similarity.euclidean_is_normalized TO false;

Simple tables for examples:

CREATE TABLE foo (a text);
INSERT INTO foo VALUES('Euler'),('Oiler'),('Euler Taveira de Oliveira'),('Maria Taveira dos Santos'),('Carlos Santos Silva');

CREATE TABLE bar (b text);
INSERT INTO bar VALUES('Euler T. de Oliveira'),('Euller'),('Oliveira, Euler Taveira'),('Sr. Oliveira');

Using similarity functions

SELECT a, b, cosine(a,b), jaro(a, b), euclidean(a, b) FROM foo, bar;

Using the levenshtein operator (~==)

SHOW pg_similarity.levenshtein_threshold;
-- 0.7

SELECT a, b, lev(a,b) FROM foo, bar WHERE a ~== b;
--              a             |          b           |   lev
-- ---------------------------+----------------------+----------
--  Euler                     | Euller               | 0.833333
--  Euler Taveira de Oliveira | Euler T. de Oliveira |     0.76

SET pg_similarity.levenshtein_threshold TO 0.5;

SELECT a, b, lev(a,b) FROM foo, bar WHERE a ~== b;
--              a             |          b           |   lev
-- ---------------------------+----------------------+----------
--  Euler                     | Euller               | 0.833333
--  Oiler                     | Euller               |      0.5
--  Euler Taveira de Oliveira | Euler T. de Oliveira |     0.76

Using the qgram operator (~~~)

SET pg_similarity.qgram_threshold TO 0.7;

SELECT a, b, qgram(a, b) FROM foo, bar WHERE a ~~~ b;
--              a             |            b            |  qgram
-- ---------------------------+-------------------------+----------
--  Euler                     | Euller                  |      0.8
--  Euler Taveira de Oliveira | Euler T. de Oliveira    |  0.77551
--  Euler Taveira de Oliveira | Oliveira, Euler Taveira | 0.807692

Comparing different operators

SELECT * FROM bar WHERE b ~@@ 'euler'; -- jaro-winkler operator
SELECT * FROM bar WHERE b ~~~ 'euler'; -- qgram operator
SELECT * FROM bar WHERE b ~== 'euler'; -- levenshtein operator
SELECT * FROM bar WHERE b ~## 'euler'; -- cosine operator

Last Modified 2026-03-12: add pg extension catalog (95749bf)