Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

305
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda