Retain deadlock: replacing a document deletes its memory units in scan order against link inserts
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 55/100
Research direction
Start with writes.delete_document, delete_stale_observations, bulk_insert_links, and the graph relink path identified in the issue; trace how each statement acquires locks on memory_units. Reproduce the deadlock using PostgreSQL 16 and the supplied SQL steps. Done means replacing a document and linking to its memories serialize without deadlocks, including through graph maintenance.
Written by the indexing model from the issue text.
Description
Bug Description
A retain that replaces a document deadlocks against a concurrent writer that links new memories to that document's memories.
- The replace path deletes the old units with
writes.delete_document:DELETE FROM memory_units WHERE document_id = $1 AND bank_id = $2. The DELETE takes its row locks in scan order. The sweep before it (delete_stale_observations) locks the same rows in id order, butFOR NO KEY UPDATE, which does not conflict withFOR KEY SHARE, so it does not order the DELETE's lock. - Link writers (
bulk_insert_links, from a retain's link phase and from the graph-maintenance relink) lock their target units withWITH locked AS (SELECT id FROM memory_units WHERE id = ANY($7::uuid[]) ORDER BY id FOR KEY SHARE) INSERT INTO memory_links .... A retain inserts semantic, temporal, causal and entity links in separate statements, so across one transaction itsFOR KEY SHARElocks are not in one id order either.
A linker that holds KEY SHARE on one unit of the document and then needs another unit that the DELETE has already removed waits for the replace, while the DELETE waits for that first unit.
Steps to Reproduce
On postgres:16, with only the keys and columns the statements touch. Document X has units 1 and 2, 2 stored first, so the scan meets it first:
docker run -d --rm --name repl -e POSTGRES_PASSWORD=pw postgres:16 >/dev/null; sleep 4
psql() { docker exec -i repl psql -U postgres -q "$@"; }
u=00000000-0000-0000-0000-00000000000
psql <<SQL
CREATE TABLE memory_units (id uuid PRIMARY KEY, bank_id text, document_id text);
CREATE INDEX ON memory_units (document_id);
CREATE TABLE memory_links (from_unit_id uuid REFERENCES memory_units ON DELETE CASCADE,
to_unit_id uuid REFERENCES memory_units ON DELETE CASCADE, PRIMARY KEY (from_unit_id, to_unit_id));
INSERT INTO memory_units VALUES ('${u}2','b','X'), ('${u}1','b','X'), ('${u}9','b','Y');
SQL
# retain R replaces X: the sweep's ordered NO KEY UPDATE pass, then delete_document
psql -c "BEGIN;
SELECT id FROM memory_units WHERE bank_id = 'b' AND id = ANY('{${u}1,${u}2}') ORDER BY id FOR NO KEY UPDATE;
SELECT pg_sleep(0.6);
DELETE FROM memory_units WHERE document_id = 'X' AND bank_id = 'b';
SELECT pg_sleep(1.5); COMMIT;" &
sleep 0.3
# retain L links Y's unit to X's units in two link statements
psql -c "BEGIN;
WITH locked AS (SELECT id FROM memory_units WHERE id = '${u}1' FOR KEY SHARE)
INSERT INTO memory_links SELECT '${u}9', id FROM locked;
SELECT pg_sleep(0.6);
WITH locked AS (SELECT id FROM memory_units WHERE id = ANY('{${u}1,${u}2}') ORDER BY id FOR KEY SHARE)
INSERT INTO memory_links SELECT '${u}9', id FROM locked ON CONFLICT DO NOTHING;
COMMIT;" &
wait; docker rm -f repl >/dev/null
Output: ERROR: deadlock detected ... while deleting tuple (0,2) in relation "memory_units".
Expected Behavior
Replacing a document and linking to its memories serialize instead of deadlocking. For example, a link writer locks every unit it will link to in one sorted statement before its first link insert, and the replace deletes the old units in id order with the lock mode the DELETE needs.
Actual Behavior
In two days of the PostgreSQL log of one bank: 43 deadlocks between DELETE FROM public.memory_units WHERE document_id = $1 AND bank_id = $2 and the memory_links insert, 6 between the chunk delete and the memory_links insert, 4 between DELETE FROM public.documents ... and the memory_links insert. In the worker log, 31 graph-maintenance tasks failed in two days with the same deadlock at:
graph.py, line 851, in _relink_batch
links.py, line 197, in _bulk_insert_links
ops_postgresql.py, line 402, in bulk_insert_links
asyncpg.exceptions.DeadlockDetectedError: deadlock detected
The replacing retain also waits up to HINDSIGHT_API_DB_COMMAND_TIMEOUT (60 s) on SELECT id FROM memory_units ... ORDER BY id FOR NO KEY UPDATE and fails with TimeoutError: 43 such task failures in one day.
Version
1.3.1, PostgreSQL 16 (Cloud SQL), two workers, HINDSIGHT_API_RETAIN_MAX_CONCURRENT=4.
LLM Provider
Gemini (Vertex AI)
- Dominant language
- Python
- Stars
- 45.2k
- Forks
- 5.9k
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from vectorize-io/hindsight
-
core
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
vectorize-io/hindsight#7254 ·
-
Control Plane shows different calendar dates for the same occurrence in Constellation and TimelineOpencore
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
vectorize-io/hindsight#7240 · 1 comment ·
-
core
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
vectorize-io/hindsight#7233 ·
-
core
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
vectorize-io/hindsight#7231 ·
-
core
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
vectorize-io/hindsight#7228 · 1 comment ·
All issues in vectorize-io/hindsight
Similar issues
-
Test: to_hijri() accepts datetime.datetime and uses only the date partPossibly taken A pull request linked to this issue is open or already merged. Opengood first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
omarelshamy25x/shamy-hijri#63 · 3 comments ·
Maintainers usually reply within 1 day
-
polish
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
521xueweihan/HelloGitHub#4729 ·
-
[Bug]: Score parser reads parts of scientific notation as valid scoresPossibly taken @AshutoshSingh840 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
UKGovernmentBEIS/control-arena#992 ·
Maintainers usually reply within 3 days
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
mvalentsev/awesome-free-ai-coding#70 ·
Maintainers usually reply within 1 day