perf(postgres): optimize PgMemoryStore for throughput and query efficiency#1
Open
ZhangHuiGui wants to merge 1 commit into
Open
perf(postgres): optimize PgMemoryStore for throughput and query efficiency#1ZhangHuiGui wants to merge 1 commit into
ZhangHuiGui wants to merge 1 commit into
Conversation
…iency
Six targeted performance improvements to the PostgreSQL memory store:
1. Batch transaction writes (upsertL1Batch / upsertL0Batch):
- Wrap multi-record inserts in a single BEGIN/COMMIT transaction
- Eliminates per-row connection acquisition and network round-trips
- Falls back to single-record path when batch size is 1
2. Hybrid search SQL rewrite (searchL1HybridNative):
- Carry all columns through CTEs, eliminating the final JOIN back to
the main table (saves a full table scan on large datasets)
- Add WHERE content <@> to_bm25query(...) > 0 to exclude zero-relevance
BM25 results from RRF fusion
- Use named prepared statement for server-side plan caching
3. Named prepared statements for high-frequency searches:
- search_l1_vector_v1, search_l1_fts_v1
- search_l0_vector_v1, search_l0_fts_v1
- l1_hybrid_native_v2
Avoids repeated parse+plan overhead on every call.
4. Concurrent reindexing (reindexAll):
- Replace serial embed+update loop with paginated reads
- Bounded-parallelism embedding (CONCURRENCY=4, Promise.allSettled)
- Batched UPDATE via unnest() arrays instead of per-row UPDATE
5. VACUUM ANALYZE after bulk deletes (deleteL1Expired / deleteL0Expired):
- Fire-and-forget VACUUM ANALYZE when >50 rows are deleted
- Prevents DiskANN/HNSW index bloat from dead tuples
6. BM25 query normalization improvement:
- Add Chinese stopword filtering (60+ high-frequency function words)
- Filter single-character CJK tokens (too generic for BM25 precision)
- Cap token count at MAX_BM25_TOKENS=8 to maintain query precision
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.
Summary
Six targeted performance improvements to the PostgreSQL memory store (
src/core/store/postgres.ts), reducing latency and improving throughput for high-frequency operations.Changes
1. Batch Transaction Writes (
upsertL1Batch/upsertL0Batch)BEGIN/COMMITtransaction2. Hybrid Search SQL Rewrite (
searchL1HybridNative)WHERE content <@> to_bm25query(...) > 0to exclude zero-relevance BM25 results from RRF fusion3. Named Prepared Statements for High-Frequency Searches
search_l1_vector_v1,search_l1_fts_v1search_l0_vector_v1,search_l0_fts_v1l1_hybrid_native_v2Avoids repeated parse+plan overhead on every call.
4. Concurrent Reindexing (
reindexAll)Promise.allSettled)unnest()arrays instead of per-row UPDATE5. VACUUM ANALYZE After Bulk Deletes
VACUUM ANALYZEwhen >50 rows are deleted6. BM25 Query Normalization Improvement
MAX_BM25_TOKENS=8to maintain query precisionImpact
Testing