pg_emd
pg_emd · Fast approximate Earth Mover’s Distance using dynamic tree embeddings
Overview
PostgreSQL · 17
Usage
Sources:
- Official README
- Version 0.1.0 SQL declarations
- PostgreSQL-facing Rust implementation
- Cargo and PostgreSQL compatibility
pg_emd 0.1.0 computes Earth Mover’s Distance. The current emd implementation is an exact O(n) calculation for one-dimensional histograms whose array positions are bins. emd_weighted uses a randomized dynamic tree embedding for approximate distance between weighted multidimensional points. The extension is a research implementation, currently built only for PostgreSQL 17.
One-Dimensional Histograms
Both arrays must have the same length; an empty pair returns zero. Treat each value as mass at bin positions 0, 1, .... Validate that values are finite and nonnegative and that total masses are equal before calling, because the function does not enforce those distribution invariants.
Weighted Multidimensional Points
tree_distance exposes the underlying randomized tree metric for inspection; it is not exact Euclidean distance.
Critical Planner and Input Caveats
The SQL declares all three functions IMMUTABLE PARALLEL SAFE, but emd_weighted and tree_distance construct tree embeddings with random grid offsets on each call. Their results can vary for identical arguments, so the volatility declaration is unsafe. Do not use those two functions in expression indexes, generated columns, partition bounds, cached constant expressions, or constraints unless the implementation and SQL declarations are corrected.
emd_weighted derives dimension from the first point and does not fully validate equal dimensions, finite coordinates, nonnegative weights, or equal total mass; nonnumeric coordinate entries are filtered during conversion. Validate and normalize JSON in the caller. Empty weighted input on either side returns zero, which may not match the application’s missing-data semantics.
The README’s performance and approximation figures are project measurements, not workload guarantees. Pin the build, validate numerical error and repeatability against an exact implementation, and test memory and latency with production distribution sizes before adoption.