Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

304
Vistas
Unique constraint with Hamming difference in postgres

Last column image_hash contains image hash of image files(varchar).

My goal is to create unique constraint on this column but it should follow one specific condition.

For example if image_hash 1 and image_hash 2 are the same - then similarity is 1

If they are completely different - similarity →0

For similarity I guess Hamming difference should suit well.

Condition is:

If similarity difference between image_hash 1 and image hash 2 is less then X (for example 0.1), then hashes considered the same and this would be uniqueness violation.

If similarity is bigger then X, then unique constraint is not violated.

I have tried :

SELECT image_hash, similarity(image_hash, '00041dffff101800') AS sml
FROM   archives_imagemodel
WHERE id=431

But it is way to sensitive and I still don’t know how to convert it to unique constraint.

I am bad in Postgres, so that sorry if question is dumb or out of reality

Any ideas?

Thanks

enter image description here

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

That is possible in principle, but it would require writing an extension in C that provides:

  • A similarity operator that implements the Hamming distance. It would return TRUE if the similarly exceeds the threshold.

  • A GiST operator class for text that supports the operator.

Then you can create an exclusion constraint using that operator, which would do exactly what you want. But be warned that you'd have to immerse yourself into the innards of PostgreSQL for that (but you wouldn't have to modify the server).

A more mundane approach would be to use a trigger, but without a GiST index as outlined above that would mean a sequential scan for each data modification, which would kill performance. Moreover, unless you use the SERIALIZABLE isolation level, such triggers are subject to race conditions.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda