Skip to content

fix(pgvector): make doc deletion query faster - #289

Merged
kyteinsky merged 5 commits into
masterfrom
fix/long-deletes
May 25, 2026
Merged

fix(pgvector): make doc deletion query faster#289
kyteinsky merged 5 commits into
masterfrom
fix/long-deletes

Conversation

@kyteinsky

@kyteinsky kyteinsky commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

CI logging for slow queries has also been enabled, not sure if we will see that in the CI though.

Sample output for the slow deletion query where a missing index on the source_id foreign key in access_list table was the culprit.
Calculated time: 3.495 + 0.310 + 0.129 = 3.934 ms
Actual time: 201177.123 ms or 201 s

        Query Text: DELETE FROM docs WHERE docs.source_id IN ($1::VARCHAR, $2::VARCHAR, ..., $275::VARCHAR) RETURNING docs.chunks
        Query Parameters: ...
        Delete on docs  (cost=1126.32..2018.25 rows=275 width=6) (actual time=0.192..3.495 rows=218 loops=1)
    ->  Bitmap Heap Scan on docs  (cost=1126.32..2018.25 rows=275 width=6) (actual time=0.144..0.310 rows=218 loops=1)
                Recheck Cond: ((source_id)::text = ANY ('{"files__default: 20392","files__default: 23092", ... }'::text[]))
                Heap Blocks: exact=25
                ->  Bitmap Index Scan on docs_pkey  (cost=0.00..1125.56 rows=275 width=0) (actual time=0.129..0.129 rows=218 loops=1)
                      Index Cond: ((source_id)::text = ANY ('{"files__default: 20392", ...
2026-03-19 11:28:59.760 UTC [6703] LOG:  duration: 201177.123 ms  execute <unnamed>: DELETE FROM docs WHERE docs.source_id IN ($1::VARCHAR, $2::VARCHAR, ..., $275::VARCHAR) RETURNING docs.chunks
2026-03-19 11:28:59.760 UTC [6703] DETAIL:  Parameters: $1 = 'files__default: 20392', $2 = ...

(put the chunking part in a different PR)

@kyteinsky
kyteinsky requested a review from marcelklehr as a code owner March 20, 2026 12:56
@kyteinsky
kyteinsky force-pushed the fix/long-deletes branch 2 times, most recently from 02b8435 to f03c10a Compare March 20, 2026 13:30
Comment thread context_chat_backend/vectordb/pgvector.py Outdated
@kyteinsky
kyteinsky marked this pull request as draft March 20, 2026 13:31
@kyteinsky

Copy link
Copy Markdown
Contributor Author

verified again the index actually does improve things:
before:

ccb=# EXPLAIN ANALYZE DELETE FROM docs WHERE source_id = 'files__default: 100';
                                                            QUERY PLAN                                                             
-----------------------------------------------------------------------------------------------------------------------------------
 Delete on docs  (cost=0.42..8.44 rows=0 width=0) (actual time=1.434..1.435 rows=0 loops=1)
   ->  Index Scan using source_id_modified_idx on docs  (cost=0.42..8.44 rows=1 width=6) (actual time=0.841..0.844 rows=1 loops=1)
         Index Cond: ((source_id)::text = 'files__default: 100'::text)
 Planning Time: 0.069 ms
 Trigger for constraint access_list_source_id_fkey: time=923.137 calls=1
 Execution Time: 924.593 ms
(6 rows)

after:

ccb=# EXPLAIN ANALYZE DELETE FROM docs WHERE source_id = 'files__default: 101';
                                                            QUERY PLAN                                                             
-----------------------------------------------------------------------------------------------------------------------------------
 Delete on docs  (cost=0.42..8.44 rows=0 width=0) (actual time=0.098..0.099 rows=0 loops=1)
   ->  Index Scan using source_id_modified_idx on docs  (cost=0.42..8.44 rows=1 width=6) (actual time=0.062..0.065 rows=1 loops=1)
         Index Cond: ((source_id)::text = 'files__default: 101'::text)
 Planning Time: 0.205 ms
 Trigger for constraint access_list_source_id_fkey: time=0.414 calls=1
 Execution Time: 0.563 ms
(6 rows)

the difference in time to pay attention to is Trigger for constraint access_list_source_id_fkey: time
without index: 923.137
with index: 0.414

index can be manually created like so: CREATE INDEX idx_access_list_source_id ON access_list (source_id);

@kyteinsky
kyteinsky marked this pull request as ready for review May 21, 2026 10:01
@kyteinsky kyteinsky changed the title fix(pgvector): make doc deletion query faster and use chunking fix(pgvector): make doc deletion query faster May 21, 2026
@kyteinsky
kyteinsky requested a review from Copilot May 21, 2026 10:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Comment thread .github/workflows/integration-test.yml
Comment thread .github/workflows/integration-test.yml
Comment thread .github/workflows/integration-test.yml Outdated
Comment thread context_chat_backend/repair/runner.py
Comment thread context_chat_backend/repair/runner.py Outdated
Comment thread context_chat_backend/repair/repair5004_date20260521105831.py
kyteinsky added 4 commits May 25, 2026 15:07
index the source_id column in the access_list table
use a defined name for the source_id index

Signed-off-by: Anupam Kumar <kyteinsky@gmail.com>
Signed-off-by: Anupam Kumar <kyteinsky@gmail.com>
…ir fails

also sort repair filenames

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Signed-off-by: kyteinsky <kyteinsky@gmail.com>
@kyteinsky
kyteinsky force-pushed the fix/long-deletes branch 3 times, most recently from 4439521 to 2768957 Compare May 25, 2026 09:59
…nfig

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
@kyteinsky
kyteinsky merged commit c40974a into master May 25, 2026
15 of 19 checks passed
@kyteinsky
kyteinsky deleted the fix/long-deletes branch May 25, 2026 12:20
@kyteinsky kyteinsky mentioned this pull request May 26, 2026
kyteinsky added a commit that referenced this pull request May 26, 2026
## 5.4.0-beta0 - 2026-05-26

### Added
- add network embedding batching (#276) @fcharlaix-opendsi
- add kubernetes support and reverse content/indexing flow (#284)
@kyteinsky @marcelklehr
- add gh workflows for docker builds and do separate cpu, cuda and rocm
(vulkan) images (#295) @kyteinsky

### Changed
- update readme according to the latest changes (#300) @kyteinsky
- bump llama_cpp_python to 0.3.23 (#301) @kyteinsky

### Fixed
- improve loadSources error handling (#288) @kyteinsky
- fix(pgvector): add chunking to prevent long list of args in queries
(#290) @kyteinsky
- fix(pgvector): make doc deletion query faster (#289) @kyteinsky
@kyteinsky kyteinsky mentioned this pull request Jun 24, 2026
1 task
kyteinsky added a commit that referenced this pull request Jun 24, 2026
## 5.4.0 - 2026-06-24

### Highlights
- The indexing direction has been reversed now. Instead of the
context_chat PHP app sending documents to the context_chat_backend
ExApp, the ExApp downloads the documents from the server according to a
list obtained from the PHP app. This also means that the `occ
context_chat:scan` command serves no purpose and has been removed.
Indexing should be smoother and run continuously now.
- Kubernetes support to scale the CPU computation
- Separate docker images for CPU, CUDA and ROCM (uses Vulkan) instead of
one heavy CUDA/CPU image
- CUDA 12.8 is shipped in the CUDA image so the host drivers should be
updated to this at the minimum.

### Added
- add network embedding batching (#276) @fcharlaix-opendsi
- add kubernetes support and reverse content/indexing flow (#284)
@kyteinsky @marcelklehr
- add gh workflows for docker builds and do separate cpu, cuda and rocm
(vulkan) images (#295) @kyteinsky

### Changed
- update readme according to the latest changes (#300) @kyteinsky
- bump llama_cpp_python to 0.3.23 (#301) @kyteinsky
- move task types to the backend (#321) @kyteinsky
- adjust comment in Dockerfile regarding RTX5090 support (#316)
@kyteinsky

### Fixed
- improve loadSources error handling (#288) @kyteinsky
- fix(pgvector): add chunking to prevent long list of args in queries
(#290) @kyteinsky
- fix(pgvector): make doc deletion query faster (#289) @kyteinsky
- drop latin-1 decode in source title and userIds (#306) @kyteinsky
- handle validation errors of files and content providers individually
(#308) @kyteinsky
- prevent race condition in vectordb tables creation (#308) @kyteinsky
- pass actual error in the error object (#308) @kyteinsky
- add container hostname to /etc/hosts to silence sudo warning (#311)
@sanzakicesarr

## 🤖 AI (if applicable)

- [ ] The content of this PR was partly or fully generated using AI

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants