Skip to content

feat: add endpoints to wipe the vector db efficiently - #374

Open
kyteinsky wants to merge 6 commits into
masterfrom
feat/vector-db-wipe
Open

kyteinsky wants to merge 6 commits into
masterfrom
feat/vector-db-wipe

Conversation

@kyteinsky

@kyteinsky kyteinsky commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

and some performance improvements.
it needs context chat changes to pass the tests.

related: #359 and nextcloud/context_chat#175

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI (N/A)

…the vector DB

used by `occ context_chat:wipe`

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Assisted-by: Github Copilot:claude-opus-5-5
Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Assisted-by: Github Copilot:claude-opus-5-5
Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Assisted-by: Github Copilot:claude-opus-5-5
don't fetch the chunk ids into the python app

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Assisted-by: Github Copilot:claude-opus-5-5
Synchronous counterpart of the delete_provider_id action, so
context_chat:wipe --provider can report the result and reindex right
after. The bg threads are stopped while it runs.

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Assisted-by: Github Copilot:claude-opus-5-5
@nextcloud-command nextcloud-command added the AI assisted This PR contains AI-assisted commits label Oct 27, 2026
Any batch the threads are working on is finished first, so nothing they fetched
before can be written to the vector db while the caller runs.
'''
with BG_THREADS_PAUSE_LOCK:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worky in k8s?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch!
yeah this will just be received in one pod, we don't know about others.
see the context_chat part of the change: nextcloud/context_chat#290, we can turn the tap off there and wait for the locked docs to be done before starting a wipe, that might work well.

there is one issue that some docs are locked for 24h due to failures, they won't clear up in the timeout of 30 mins. that we wait for the batch the complete.

this code can probably be removed from CCB then I suppose.

@marcelklehr

marcelklehr commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Claude says:

enabled_handler doesn't take BG_THREADS_PAUSE_LOCK (controller.py:205-215):

  • Enable during a pause: THREAD_STOP_EVENT is set, so the handler calls start_bg_threads(), and indexing starts again in the middle of the wipe.
  • Disable during a pause: the finally restarts the threads anyway, so they run while the app is disabled. They then just poll app_enabled, so the effect is small.

Taking the same lock in the handler, or checking a "paused" flag there, would fix both.

The pause also waits for the request-processing thread. wait_for_bg_threads() joins every thread, including the one answering chat/LLM queries. That thread only reads, so it doesn't need to stop for a wipe. Waiting for a long LLM task, or for an indexing batch still being embedded on CPU, can hold the HTTP request open for minutes. The occ context_chat:wipe call on the PHP side may time out before then. Two suggestions:

  • Pause only the indexing and updates threads.
  • Check the HTTP client timeout in context_chat for these two endpoints.


# The embeddings table is shared by all the langchain collections in this db. Lock it so no
# other collection can add rows between the check and the truncate, the lock is released on commit.
session.execute(sa.text(f'LOCK TABLE {embeddings_table} IN ACCESS EXCLUSIVE MODE'))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude says:

the code truncates docs/access_list (exclusive lock) first and then locks the embeddings table. Another worker or pod in add_indocuments may already hold a lock on the embeddings table and be waiting for docs. Postgres would detect the deadlock and abort one side, so it fails cleanly but it does fail. Locking all three tables at the start, in a fixed order, avoids this.

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
@kyteinsky
kyteinsky force-pushed the feat/vector-db-wipe branch from bbbca5c to 401aa3d Compare October 29, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI assisted This PR contains AI-assisted commits

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants