pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #1041: Feature Request: Query Result Caching for Repeated HNSW Searches
4+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #1041: Feature Request: Query Result Caching for Repeated HNSW Searches
@ 2026-10-16 02:15 "kute1654 (@kute1654)" <noreply+kute1654@github.com>
  0 siblings, 0 replies; 4+ messages in thread

From: kute1654 (@kute1654) @ 2026-10-16 02:15 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

<html>
<body>
<!--StartFragment--><h1 data-pm-slice="1 1 []"><span>Feature Request: Query Result Caching for Repeated HNSW Searches</span></h1><h2><span>Background and Motivation</span></h2><p><span>I have been investigating QPS improvements for repeated vector searches in pgvector, particularly for workloads where a limited set of query vectors is executed repeatedly.</span></p><p><span>For HNSW searches, a significant portion of query execution time can be spent traversing the graph and computing distances. Query plan caching can reduce planning overhead, but it does not eliminate the cost of executing the HNSW search itself.</span></p><p><span>To investigate this opportunity, I implemented an experimental query result cache in a modified version of pgvector. The cache stores ordered HNSW search candidates and reuses them on subsequent cache hits, avoiding repeated graph traversal.</span></p><p><span>My initial benchmark shows a substantial throughput improvement when query result caching is enabled. I would like to discuss whether this optimization could be useful for the pgvector community edition and what correctness and implementation requirements would need to be addressed.</span></p><h2><span>Benchmark Results</span></h2><h3><span>Test configuration</span></h3><ul data-spread="false"><li><p><span>Dataset: </span><code dir="ltr"><span>nytimes-256-angular</span></code></p></li><li><p><span>Number of query statements: 1,000</span></p></li><li><p><span>Warm-up duration: 5 seconds</span></p></li><li><p><span>Measurement duration: 60 seconds</span></p></li><li><p><span>SQL type: </span><code dir="ltr"><span>text_literal</span></code></p></li><li><p><span>Protocol: </span><code dir="ltr"><span>simple</span></code></p></li><li><p><span>Cache disabled: </span><code dir="ltr"><span>requery_cache = 0</span></code></p></li><li><p><span>Cache enabled: </span><code dir="ltr"><span>requery_cache = 1</span></code></p></li></ul><p><span>In the following table, </span><code dir="ltr"><span>w</span></code><span> represents the warm-up phase and </span><code dir="ltr"><span>s</span></code><span> represents the measurement phase.</span></p>
Metric | Cache Disabled | Cache Enabled
-- | -- | --
Execution count (w/s) | 228 / 4,752 | 418 / 229,881
Execution time (ms, w/s) | 20.244 / 12.043 | 11.335 / 0.080
Vector-index-related time (ms, w/s) | 0.023 / 0.022 | 0.022 / 0.020
QPS | 79 | 3,824

<p><span>Under this workload, the measured QPS increases from 79 to 3,824, approximately a 48.4x improvement.</span></p><p><span>The cache recorded 1,000 misses during the test workload, with the remaining counted lookups reported as hits. The workload is limited to 1,000 query statements, which allows repeated execution of the same queries to benefit from cache reuse.</span></p><p><span>These results suggest that caching HNSW search candidates may substantially improve throughput for workloads with a high repetition rate. Further benchmarks are needed to characterize the effects of query diversity, concurrency, cache capacity, and data updates.</span></p><h2><span>Proposed Design</span></h2><p><span>The experimental implementation caches the ordered candidate list generated by HNSW search, rather than the complete SQL result or execution plan.</span></p><h3><span>1. Cache placement</span></h3><p><span>The cache is placed at the HNSW scan entry point, before </span><code dir="ltr"><span>GetScanItems</span></code><span> is invoked, where the query vector and search parameters are available.</span></p><ul data-spread="false"><li><p><span>On a cache hit, the cached ordered candidate list is replayed to the scan.</span></p></li><li><p><span>On a cache miss, normal HNSW search is performed and the resulting candidate list is inserted into the cache.</span></p></li><li><p><span>The executor continues to retrieve heap tuples and apply SQL-level operations such as </span><code dir="ltr"><span>LIMIT</span></code><span>.</span></p></li></ul><p><span>This approach aims to avoid changes to the executor and query plan tree.</span></p><h3><span>2. Cache key</span></h3><p><span>The current composite key includes:</span></p><ul data-spread="false"><li><p><span>Index file identifier (</span><code dir="ltr"><span>relfilenumber</span></code><span>)</span></p></li><li><p><span>Query vector</span></p></li><li><p><code dir="ltr"><span>ef_search</span></code></p></li><li><p><span>Distance function identifier</span></p></li><li><p><span>Vector dimension</span></p></li></ul><p><span>The query vector is hashed for lookup, followed by exact byte-level comparison to confirm equality.</span></p><p><code dir="ltr"><span>LIMIT</span></code><span> is excluded from the key because the cache stores an ordered candidate list. Reuse with different limits is intended to return the appropriate prefix of the cached list, subject to candidate sufficiency and correctness constraints.</span></p><p><span>The key and its associated validity checks would need to cover all relevant search semantics and object lifecycle changes before the implementation could be considered production-ready.</span></p><h3><span>3. Invalidation</span></h3><p><span>The current prototype uses a write-generation mechanism and TTL-based expiration.</span></p><ul data-spread="false"><li><p><span>A write-generation counter is intended to invalidate entries associated with modified data.</span></p></li><li><p><span>Index rebuilds and relation rewrites require appropriate lifecycle invalidation.</span></p></li><li><p><span>Entries expire after the configured TTL, which defaults to 60 seconds.</span></p></li><li><p><span>Cache invalidation is intended to favor correctness over hit rate: when validity cannot be established, the search should bypass the cache.</span></p></li></ul><p><span>The current design still requires further engineering and testing to establish safe behavior across PostgreSQL backends, concurrent writes, MVCC snapshots, and index lifecycle changes. TTL should be treated as a resource-management mechanism, not as a substitute for the consistency guarantees required by PostgreSQL.</span></p><h3><span>4. Resource management and scope</span></h3><p><span>The prototype includes configurable limits for:</span></p><ul data-spread="false"><li><p><span>Maximum rows per cache entry</span></p></li><li><p><span>Maximum size of an individual entry</span></p></li><li><p><span>Maximum number of entries</span></p></li><li><p><span>Maximum total cache memory</span></p></li></ul><p><span>It uses FIFO replacement and expiration-based cleanup.</span></p><p><span>To avoid interpreting incompatible index layouts or search semantics, the current implementation caches only indexes whose operator-class input type is </span><code dir="ltr"><span>vector</span></code><span>. Unsupported vector types and searches involving scalar filtering during graph traversal bypass the cache.</span></p><h2><span>Questions for the Community</span></h2><p><span>I would appreciate feedback on the following questions.</span></p><ol data-spread="true" start="1"><li><p><strong><span>Scope:</span></strong><span> Would query result caching for repeated HNSW searches be a useful feature for pgvector, or would it be better implemented at the PostgreSQL core or application layer?</span></p></li><li><p><strong><span>Correctness and MVCC:</span></strong><span> What guarantees would be required to safely reuse cached candidates across transactions and PostgreSQL backends, particularly with concurrent inserts, updates, deletes, and different MVCC snapshots?</span></p></li><li><p><strong><span>Invalidation:</span></strong><span> What would be an appropriate invalidation strategy for data modifications, index rebuilds, relation rewrites, and object lifecycle changes? Would a shared generation mechanism be a reasonable approach?</span></p></li><li><p><strong><span>Cache key completeness:</span></strong><span> Which additional parameters or execution-context properties must be included in the key to prevent incorrect reuse?</span></p></li><li><p><strong><span>Candidate-list reuse:</span></strong><span> Under what conditions is it safe to reuse an ordered HNSW candidate list when </span><code dir="ltr"><span>LIMIT</span></code><span> changes, and how should insufficient candidate lists or invisible/deleted tuples be handled?</span></p></li><li><p><strong><span>Performance evaluation:</span></strong><span> What benchmark methodology would best demonstrate the benefits and costs of this optimization across different query repetition rates, concurrency levels, and memory limits?</span></p></li></ol><h2><span>Next Steps</span></h2><p><span>I am interested in refining this prototype into a more robust implementation and providing the source changes, benchmark scripts, and correctness tests for review.</span></p><p><span>Before proposing a production-ready implementation, I plan to focus on:</span></p><ul data-spread="false"><li><p><span>Cross-backend invalidation and transaction visibility</span></p></li><li><p><span>Cache-key completeness and index lifecycle safety</span></p></li><li><p><span>Differential testing with caching enabled and disabled</span></p></li><li><p><span>Concurrent write and index-maintenance tests</span></p></li><li><p><span>Cache hit/miss statistics and memory-overhead measurements</span></p></li><li><p><span>Performance evaluation under different query repetition rates</span></p></li></ul><p><span>I am sharing this proposal to gather feedback on the feature's usefulness, implementation scope, and correctness requirements before pursuing a pull request.</span></p><p><span>Thank you for your feedback.</span></p><!--EndFragment-->
</body>
</html>

^ permalink  raw  [nested|flat] 4+ messages in thread

* Re: [pgvector/pgvector] issue #1041: Feature Request: Query Result Caching for Repeated HNSW Searches
@ 2026-10-16 02:18 ` "kute1654 (@kute1654)" <noreply+kute1654@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: kute1654 (@kute1654) @ 2026-10-16 02:18 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @jkatz @nodomain @mulander @hlinnaka 
Could you please take a look at this proposal when you have a chance? The initial benchmark shows a significant QPS improvement for repeated HNSW searches with query result caching (79 → 3,824 QPS, approximately 48.4x).

I'd really appreciate your feedback on the design, correctness requirements, and whether this feature would be a good fit for pgvector.

Thanks for your time and consideration!

^ permalink  raw  [nested|flat] 4+ messages in thread

* Re: [pgvector/pgvector] issue #1041: Feature Request: Query Result Caching for Repeated HNSW Searches
@ 2026-10-16 02:27 ` "ankane (@ankane)" <noreply+ankane@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: ankane (@ankane) @ 2026-10-16 02:27 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @kute1654, users who want caching should add it at another layer in the stack (like the application layer).

^ permalink  raw  [nested|flat] 4+ messages in thread

* Re: [pgvector/pgvector] issue #1041: Feature Request: Query Result Caching for Repeated HNSW Searches
@ 2026-10-16 02:27 ` "ankane (@ankane)" <noreply+ankane@github.com>
  2 siblings, 0 replies; 4+ messages in thread

From: ankane (@ankane) @ 2026-10-16 02:27 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2026-10-16 02:27 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-16 02:15 [pgvector/pgvector] issue #1041: Feature Request: Query Result Caching for Repeated HNSW Searches "kute1654 (@kute1654)" <noreply+kute1654@github.com>
2026-10-16 02:18 ` "kute1654 (@kute1654)" <noreply+kute1654@github.com>
2026-10-16 02:27 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-16 02:27 ` "ankane (@ankane)" <noreply+ankane@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox