Skip to main content

Postgres indexing utilities to implement high-throughput queries with on-the-fly deduplication

Project description

dedup-pg

A library with functions useful for implementing a MinHash-based deduplication indexing layer in Postgres, or any relational database.

Use cases

In cases where you have to search for specific items in a dataset derived from noisy data, it is likely that there are duplicates which hurt retrieval quality. We can estimate the similarity between such items by hashing their components in a way to approximate their Jaccard similarity. This can be useful for deduplication before item ingestion into an online production database.

However, if your system has special constraints, particularly multi-tenancy where you cannot simply delete items for every user (because some users might not have access to certain duplicates), it becomes more infeasible to compute Jaccard similarity pair-wise per query. This library helps solve this by using locality-sensitive hashing to bucket items that are likely to be above a specific Jaccard similarity.

In short, it makes query-time deduplication possible and efficient for search systems with special needs such as multi-tenant retrieval-augmented generation (RAG).

Usage

Below is an example of usage for deduplicating textual chunks.

from collections import defaultdict

from dedup_pg import DedupIndex
from dedup_pg.helpers import n_grams

# A corpus of named items we want to deduplicate
corpus = [
    ("key1", "The quick brown fox jumps over the lazy dog"),
    ("key2", " he quic  bnown f x jump  over the  azy dog"),
    ("key3", "An entirely different sentence!"),
]

# Our deduplication index - this can be Postgres-backed with configuration
lsh = DedupIndex()

# Using n=3 character n-grams is a strong choice for deduplicating textual chunks
n_gram_corpus = [(key, n_grams(text, n=3)) for key, text in corpus]

# Index bands for each key which help us determine duplicates
duplicate_map = defaultdict(list)
for key, n_gram in n_gram_corpus:
    cluster_key = lsh.query(n_gram)
    duplicate_map[cluster_key].append(key)

# `key1` and `key2` are in the same cluster in contrast to `key3`
print(duplicate_map)

For ease-of-use, we provide the dedup_pg.backend.sqlalchemy.SQLAlchemy backend, which you use by passing it the the DedupIndex initialization.

Alternatives

This library is the easiest way to implement deduplication in Postgres, and has been successfully used in production (at the company I'm working at). Most similar libraries are built for local usage and have non-compact serialization incompatible with Postgres.

However, datasketch and rensa are good alternatives if you would like something different.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dedup_pg-0.4.2.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dedup_pg-0.4.2-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file dedup_pg-0.4.2.tar.gz.

File metadata

  • Download URL: dedup_pg-0.4.2.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.19

File hashes

Hashes for dedup_pg-0.4.2.tar.gz
Algorithm Hash digest
SHA256 3c4164119d4d15206bc713857e28b328302a2f08b5927a9c546a5e2d3844fea2
MD5 5626a02ce47809a3f685ef3d30f993bb
BLAKE2b-256 040ef0c17059b3d826ba433069c25ec7c312849d2f072c8d72040173bac92c93

See more details on using hashes here.

File details

Details for the file dedup_pg-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: dedup_pg-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.19

File hashes

Hashes for dedup_pg-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 57d7e0bb397587cbd5d09f9b370d7583989c6e80ad7913024a535be0abc8b59a
MD5 56d38593acff02eb1e94f1f23ec92f94
BLAKE2b-256 013f60e496d9860593a7132c3fa9a0b00dc65e1f70122e5209ed19ff604d0084

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page