pg_semantic_cache
pg_semantic_cache · Caches query results and retrieves semantically similar results using pgvector embeddings and configurable eviction policies.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 101212 | pg_semantic_cache | pg_semantic_cache | 0.1.0 | RAG | PostgreSQL | C |
PostgreSQL · 14, 15, 16, 17, 18
Usage
Sources:
pg_semantic_cache stores JSON query results with vector embeddings and retrieves them for semantically similar inputs. The application supplies both the embeddings and cached result; the extension does not call an embedding model or execute the stored query text.
Core Workflow
rebuild_index() is appropriate here only because the demonstration cache is empty. All embeddings in a cache must match the configured vector dimension. Cache only after producing an authoritative result, and independently decide whether the returned similarity score is safe for that query class.
Object Index
cache_query(...) RETURNS bigintstores query text, embedding, JSONB result, TTL, and tags.get_cached_result(...)returnsfound,result_data,similarity_score, andage_seconds.invalidate_cache(pattern, tag),evict_expired,evict_lru,evict_lfu,auto_evict, andclear_cacheremove entries.cache_stats,cache_hit_rate,cache_health, andrecent_cache_activityexpose usage and health.set_vector_dimension,set_index_type, andrebuild_indexconfigure the vector layout and IVFFlat/HNSW index.cache_configstores keys such asmax_cache_size_mb,default_ttl_seconds,eviction_policy, andsimilarity_threshold.
Operational Notes
Version 0.1.0-beta4 is a preview release. It requires vector, uses the fixed semantic_cache schema, is non-relocatable, and declares no preload requirement. rebuild_index() clears cached data; treat dimension or index changes as destructive maintenance.
Semantic similarity does not prove result equivalence. Include tenant, authorization, model/version, locale, schema, and other result-changing context in application cache design; apply short TTLs or explicit invalidation for mutable data. Cached JSONB can contain sensitive source results, so grant schema/table/function access narrowly. Monitor false hits, stale data, storage growth, eviction, and index recall before production use.