Skip to content

fix(server): avoid duplicate face embeddings on refresh - #33709

Open
NikiforovG wants to merge 1 commit into
immich-app:mainfrom
NikiforovG:fix/26373-face-embedding-refresh
Open

NikiforovG wants to merge 1 commit into
immich-app:mainfrom
NikiforovG:fix/26373-face-embedding-refresh

Conversation

@NikiforovG

@NikiforovG NikiforovG commented Nov 26, 2026 •

Copy link
Copy Markdown

Description

Fixes #26373

Rerunning face detection on a manually tagged or imported face can fail with duplicate key value violates unique constraint "face_search_pkey". The face already has an embedding, and the refresh tries to insert another row with the same faceId.

The refresh now updates the existing embedding. When several detections match the same tagged face, it writes one embedding, choosing the detection with the greatest bounding-box overlap. If the overlaps are equal within floating-point tolerance, the first detection wins. This also prevents PostgreSQL from trying to update the same row twice in one statement. Repeated matches to an existing machine-learning face preserve its embedding.

The repository writes embeddings in faceId order so concurrent refreshes of the same faces acquire row locks in a consistent order.

Credit to @spinninghypercube for identifying the duplicate-detection case and proposing the embedding upsert and per-face selection. Original analysis

How Has This Been Tested?

All three regression tests failed on unpatched main and passed with the fix:

  • Refreshing the same face twice replaces its stored embedding in PostgreSQL.
  • When several detections match one manual face, the first detection with the greatest overlap supplies the embedding.
  • Matching an existing machine-learning face twice leaves its embedding intact.

A separate local PostgreSQL check ran 12 pairs of concurrent refreshes. Each pair supplied the same 32 face IDs in opposite orders. All 12 pairs completed successfully.

Checklist:

  • I have carefully read CONTRIBUTING.md
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if applicable
  • I have no unrelated changes in the PR.
  • I have confirmed that any new dependencies are strictly necessary.
  • I have written tests for new code (if applicable)
  • I have followed naming conventions/patterns in the surrounding code
  • All code in src/services/ uses repositories implementations for database calls, filesystem operations, etc.
  • All code in src/repositories/ is pretty basic/simple and does not have any immich specific logic (that belongs in src/services/)

Please describe to which degree, if any, an LLM was used in creating this pull request.

An LLM was used to assist in writing the code and related tests.

@immich-push-o-matic

immich-push-o-matic Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Label error. Requires exactly 1 of: changelog:.*. Found: 🗄️server, auto-closed:template. A maintainer will add the required label.

@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically closed as the description doesn't follow our template. Please make sure every section is present and filled in. After you edit it to match the template, the PR will automatically be reopened.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

duplicated key value for face_search_pkey

1 participant