fix: intermittent Postgres deadlock for concurrent memory searches#2146
Open
supreme-gg-gg wants to merge 2 commits into
Open
fix: intermittent Postgres deadlock for concurrent memory searches#2146supreme-gg-gg wants to merge 2 commits into
supreme-gg-gg wants to merge 2 commits into
Conversation
Signed-off-by: Jet Chiang <pokyuen.jetchiang-ext@solo.io>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses intermittent PostgreSQL deadlocks triggered by concurrent memory searches that increment memory.access_count on overlapping row sets. It makes row-lock acquisition deterministic (by id order) and changes access-count increments to be best-effort so memory search results are not blocked by bookkeeping failures.
Changes:
- Update
IncrementMemoryAccessCountto lock rows in a deterministic id order before updating to prevent deadlocks between concurrent overlapping increments. - Make
SearchAgentMemoryignore (and log) access-count increment failures so searches still return results. - Add a concurrency-focused database test to catch regressions around concurrent search/access-count updates.
Reviewed changes
Copilot reviewed 3 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| go/core/internal/database/queries/memory.sql | Changes access-count increment SQL to lock rows in id order before updating. |
| go/core/internal/database/gen/querier.go | Regenerates sqlc interface to reflect the updated query/comments. |
| go/core/internal/database/gen/memory.sql.go | Regenerates sqlc output with the updated SQL for access-count increments. |
| go/core/internal/database/client_postgres.go | Makes access-count increments best-effort during memory search (no longer fail the search). |
| go/core/internal/database/client_test.go | Adds a concurrent search test intended to reproduce/prevent the deadlock scenario. |
Files not reviewed (2)
- go/core/internal/database/gen/memory.sql.go: Generated file
- go/core/internal/database/gen/querier.go: Generated file
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Jet Chiang <pokyuen.jetchiang-ext@solo.io>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Close #2135
Fixes intermittent Postgres deadlocks when concurrent memory searches update overlapping rows'
access_count(e.g.PrefetchMemoryToolfanning out per-sentence searches). Row locks are now acquired in id order, and access-count increments are best-effort so a bookkeeping failure no longer fails the search.The root cause is described in #2135 but in short when two concurrent search requests return results like
["memory_1", "memory_2"]and["memory_2", "memory_1"], it will result in a deadlock when trying to update the rows.I've added a concurrency test that reproduced the error without this fix to the query and passed after the fix.