Skip to content
Introducing AI OS for Business & School — our most powerful products yet.Explore →
App Corp iconApp Corp
Technology Deep-Dive

RAG with PostgreSQL & pgvector

Vector search without the infrastructure complexity

Add semantic search to your existing PostgreSQL — build RAG systems, knowledge bases, and hybrid search with no new infrastructure to manage.

When to Use RAG with PostgreSQL & pgvector

  • Knowledge bases requiring semantic search over documents
  • Customer support systems that need to retrieve relevant articles
  • Internal documentation search with access control
  • Applications already using PostgreSQL that need vector search
  • Multi-tenant RAG where tenant isolation is critical
  • Projects where operational simplicity matters more than cutting-edge vector features

When NOT to Use RAG with PostgreSQL & pgvector

  • Real-time streaming data requiring millisecond vector similarity search
  • Massive-scale vector search (billions of vectors) — consider dedicated vector databases
  • Applications requiring advanced vector operations (clustering, anomaly detection at scale)
  • Projects where the team has no PostgreSQL experience
  • Use cases where keyword search is sufficient — don't add vector search for novelty

Architecture

We build RAG systems on PostgreSQL with the pgvector extension. The architecture:

Document ingestion pipeline: documents are chunked (typically 500-1000 tokens with overlap), embedded using an embedding model (OpenAI, Cohere, or open-source), and stored in PostgreSQL with pgvector indexes.

Hybrid search combines vector similarity (semantic) with keyword search (BM25) for better retrieval quality. PostgreSQL's full-text search handles the keyword component; pgvector handles the semantic component. Results are combined using reciprocal rank fusion.

Row-Level Security enforces tenant isolation at the database level — each tenant's vectors are only searchable by authorized users. This eliminates a common RAG vulnerability where cross-tenant data leakage is possible.

Query processing: user queries are embedded, similarity search runs against the vector index, results are re-ranked, and the most relevant chunks are passed to the LLM for answer generation with citations.

Trade-offs

No separate vector database infrastructure

pgvector runs inside PostgreSQL — no additional service to manage, monitor, or pay for. Trade-off: pgvector may be slower than dedicated vector databases at extreme scale.

Hybrid search improves retrieval quality

Combining semantic and keyword search catches cases where either alone fails. Trade-off: hybrid search adds query complexity and requires tuning the fusion algorithm.

RLS provides multi-tenant isolation

Vector search automatically respects tenant boundaries. Trade-off: RLS policies must be designed and tested carefully — mistakes can leak data across tenants.

Mature ecosystem and tooling

PostgreSQL has decades of tooling, monitoring, and operational knowledge. Trade-off: vector search is a newer capability — some edge cases are less documented than dedicated vector databases.

Production Concerns

  • pgvector index creation can be slow for large datasets — build indexes incrementally
  • IVFFlat indexes require enough rows before creation — HNSW indexes are more flexible
  • Embedding model consistency is critical — use the same model for ingestion and queries
  • Chunk size and overlap significantly affect retrieval quality — tune empirically
  • Hybrid search weight tuning (vector vs keyword) requires testing with real queries
  • Connection pooling is essential for concurrent vector search queries
  • Monitoring query latency and recall quality in production

Cost Implications

PostgreSQL with pgvector is significantly cheaper than dedicated vector databases. If you are already running PostgreSQL, pgvector adds zero infrastructure cost — just the extension. Supabase includes pgvector in all tiers. For a knowledge base with 100K documents, embedding costs are $5-20 one-time (depending on model), and vector storage adds approximately 20% to database size. No ongoing vector-specific infrastructure costs.

App Corp Experience

We use PostgreSQL with pgvector for all RAG systems unless the scale or feature requirements justify a dedicated vector database. Our strongest experience is building multi-tenant RAG where Row-Level Security ensures each tenant can only search their own documents.

We have built hybrid search systems combining pgvector's semantic search with PostgreSQL's full-text search for improved retrieval quality. The key insight: semantic search catches conceptual matches but misses exact terminology; keyword search catches exact matches but misses conceptual similarity. Combining both produces significantly better results.

Our RAG architecture includes document chunking strategies (recursive character splitting, semantic chunking), embedding model selection (OpenAI, Cohere, open-source), and retrieval evaluation pipelines. Every RAG system we deliver includes a retrieval quality benchmark against real queries.

Frequently Asked Questions

Is pgvector as good as Pinecone or Weaviate?
For most applications, yes. pgvector handles vector search with reasonable performance for datasets up to millions of vectors. Dedicated vector databases like Pinecone and Weaviate offer more advanced features (managed infrastructure, specialized indexing, advanced filtering) at higher cost and complexity. We recommend pgvector when you already use PostgreSQL and want operational simplicity.
How do I choose an embedding model?
Trade-off between quality, speed, and cost. OpenAI's text-embedding-3-small is fast and affordable for most use cases. Cohere's embed-v3 offers multilingual support. Open-source models (BGE, E5) run locally but require infrastructure. We test multiple models against your actual data and measure retrieval quality before recommending one.
How many documents can pgvector handle?
pgvector handles millions of vectors with appropriate indexing. The practical limit depends on your hardware and performance requirements. For most SaaS applications (10K-1M documents), pgvector on a standard PostgreSQL instance performs well. Beyond that, we evaluate read replicas, sharding, or dedicated vector databases.

Building a RAG system?

Tell us about your knowledge base. We will recommend the right architecture — whether that is pgvector, a dedicated vector database, or something simpler than RAG entirely.

We value your privacy

We use strictly necessary cookies to keep our site secure and functional. With your consent, we also use performance cookies to understand how you interact with our site, and functional cookies to remember your preferences. Read our full Cookie Policy