Kokil Thapa - Professional Web Developer in Nepal
Freelancer Web Developer in Nepal with 15+ Years of Experience

Kokil Thapa is an experienced full-stack web developer focused on building fast, secure, and scalable web applications. He helps businesses and individuals create SEO-friendly, user-focused digital platforms designed for long-term growth.

Vector Databases Explained for RAG

By Kokil Thapa | Last reviewed: September 2026

You need vector databases explained for RAG because most teams hit the same wall: the LLM knows general facts, but it does not know your product docs, legal forms, or order history. Retrieval-Augmented Generation (RAG) fixes that by fetching relevant chunks at query time and passing them into the prompt. The vector database is the engine that makes that retrieval fast and accurate. If you already read our RAG explained guide, this page goes deeper on the storage and search layer that sits behind it.

What is a vector database and why does RAG need one?

A vector database stores high-dimensional numeric arrays called embeddings. Each embedding represents the semantic meaning of a chunk of text, an image, or another object. Traditional SQL indexes excel at exact matches and ranges. They fail when a user asks “How do I cancel a booking?” and your docs say “refund policy for trek deposits.” The phrases differ, but the meaning overlaps.

Vector search closes that gap. You embed the user question, compare it against stored document vectors, and return the nearest neighbours. That is the retrieval step in RAG. Without it, you either stuff the entire knowledge base into the prompt (expensive and impossible at scale) or hope the model guesses correctly (risky for legal, medical, or financial content).

Vector Database Role in RAGSource DocsPDF, HTML, DBChunk + Embed512–1024 tokensVector Indexpgvector, PineconeLLMAnswerQuery Path at RuntimeUser QueryNatural languageQuery EmbedSame modelTop-K SearchCosine / HNSWPromptRetrieved chunks become grounded context for the LLM response
Vector databases explained for RAG: ingestion embeds chunks; query time runs similarity search before the LLM generates an answer.

On legal-tech portals I have worked on, RAG is attractive because you can update a notary checklist or court-marriage FAQ without retraining a model. You re-index the changed pages. The vector store holds the searchable memory. The LLM handles language. That split keeps costs predictable and answers traceable to source documents.

Embeddings: the unit vector databases actually store

An embedding is a fixed-length float array. OpenAI’s text-embedding-3-small returns 1536 dimensions by default. Cohere, Voyage, and open-source models like nomic-embed-text use different sizes. You must embed documents and queries with the same model and dimension count. Mixing models breaks similarity math entirely.

Distance metrics matter. Cosine similarity is the default for normalized embeddings. Inner product (dot product) works when vectors are not normalized. Euclidean (L2) distance appears in some libraries. PostgreSQL’s pgvector extension supports all three. Pick one metric and stay consistent across index creation and query code.

Metadata filters alongside vectors

Production RAG rarely runs pure vector search alone. You filter by tenant ID, document type, language, or publication date. A law firm portal might scope search to “client-visible” files only. A multi-vendor marketplace might restrict results to one seller’s catalogue. Store metadata in the same row as the vector. Apply SQL or JSON filters before or after the approximate nearest-neighbour step depending on the engine.

How does the RAG ingestion and retrieval pipeline work?

RAG has two distinct phases: offline indexing and online retrieval. Teams that treat them as one script usually regret it. Indexing is batch-oriented, idempotent, and safe to rerun. Retrieval is latency-sensitive and runs on every user message.

  1. Extract — Pull text from HTML, Markdown, PDF, database rows, or API responses.
  2. Chunk — Split text into overlapping segments, typically 300–800 tokens with 10–20% overlap.
  3. Embed — Call an embedding API or local model for each chunk.
  4. Upsert — Write vector + text + metadata into the vector store.
  5. Query — Embed the user question, fetch top-K neighbours, optionally re-rank.
  6. Generate — Pass retrieved chunks into the LLM system prompt and return the answer with citations.

Chunk quality drives retrieval quality more than index tuning does. A common mistake is splitting on fixed character counts and cutting sentences in half. Prefer structure-aware chunking: headings, paragraphs, or logical sections from your CMS. On a Laravel documentation site, chunking by heading hierarchy often beats naive token windows.

RAG Indexing vs Retrieval PipelineOffline IndexingOnline RetrievalExtractChunkEmbedUpsertVector DatabaseVectors + text + metadataUser QueryEmbed QueryTop-K + FilterLLM AnswerSeparate batch jobs from request-time search for cleaner ops
RAG splits offline document indexing from online vector search and LLM generation.

Approximate nearest neighbour (ANN) indexes

Exact brute-force comparison against every vector works for thousands of rows. It fails at millions. ANN algorithms trade a small accuracy loss for large speed gains. HNSW (Hierarchical Navigable Small World) is the most common choice in 2026. IVFFlat partitions vectors into lists and searches a subset. pgvector supports both. Managed services like Pinecone and Weaviate build indexes automatically.

Index build time and memory use are operational costs people forget. HNSW indexes can consume several times the raw vector storage. Plan disk and RAM on your PostgreSQL server before you index 500k legal document chunks. Our pgvector vs Pinecone comparison walks through those trade-offs on real hosting budgets.

Re-ranking after vector retrieval

Top-K vector results are fast but imperfect. A cross-encoder re-ranker scores each query-chunk pair more accurately than bi-encoder embeddings alone. The pattern: retrieve 20–50 candidates by vector search, re-rank to the best 5, then send those to the LLM. Latency increases by 100–300 ms. Answer quality often jumps noticeably on technical documentation and long-form legal guides.

Which vector database should you choose for production RAG?

There is no universal winner. The right choice depends on data volume, existing stack, team skills, and latency budget. PHP and Laravel teams often already run MySQL 9.7 or PostgreSQL 18. Adding pgvector to PostgreSQL keeps one database for transactional data and vectors. That simplifies backups, replication, and access control you already understand from multi-tenant database design.

OptionBest forTrade-offsTypical cost (small prod)
pgvector (PostgreSQL 18)Laravel/PHP apps, <5M vectors, strong metadata filtersYou manage index tuning, RAM, and vacuumRs 3,000–8,000/mo (~USD 22–60) on a VPS you already pay for
Managed vector DB (Pinecone, Qdrant Cloud)Fast launch, dedicated ANN ops, multi-regionExtra vendor, egress fees, data residency questionsRs 8,000–25,000/mo (~USD 60–185) at moderate scale
Redis 8.10 with vector searchLow-latency cache + small vector setsMemory-bound; not ideal as primary doc storeOften bundled with existing Redis bill
OpenSearch / ElasticsearchHybrid keyword + vector search on large corporaHeavier ops footprint; JVM tuningRs 15,000+/mo (~USD 110+) for dedicated cluster

For most client projects I evaluate, pgvector inside PostgreSQL wins when vector count stays under a few million and the team already runs Laravel with PostgreSQL. Managed Pinecone makes sense when you need sub-50 ms search at scale and no DBA time. Read RAG vs fine-tuning before you pay for either a large vector cluster or model training.

Vector DB Selection Decision TreeStarting RAG project?Already on Postgres?Laravel + PG 18Need hybrid search?Keyword + vectorMillions of vectors?Strict latency SLApgvectorSee Laravel setup guideOpenSearchBM25 + kNNManaged ANNPinecone / QdrantStart simple with pgvector; migrate when metrics prove you need dedicated ANN infra
Choose a vector database for RAG based on existing Postgres usage, hybrid search needs, and vector scale.

When a plain relational table is enough

Not every “AI search” feature needs a dedicated vector engine on day one. Below roughly 10,000 chunks, a PostgreSQL table with a sequential scan and proper caching can serve internal admin tools. Once p95 query latency crosses your threshold or CPU spikes during search, add an HNSW index. Incremental adoption beats over-provisioning a managed vector cluster for a brochure site with forty FAQ entries.

How do you implement vector search in a Laravel or PHP stack?

I integrate LLM APIs into Laravel applications; I do not train custom embedding models. That is the realistic path for most Nepal agencies and product teams in 2026. You call an embedding endpoint, store results in pgvector, and query from a service class or queued job. Our pgvector and Laravel practical setup covers migration details. Here is the core schema pattern.

CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE document_chunks (
    id BIGSERIAL PRIMARY KEY,
    tenant_id UUID NOT NULL,
    source_url TEXT NOT NULL,
    chunk_index INT NOT NULL,
    content TEXT NOT NULL,
    embedding vector(1536) NOT NULL,
    metadata JSONB DEFAULT '{}',
    created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX document_chunks_embedding_idx
ON document_chunks
USING hnsw (embedding vector_cosine_ops);

Laravel 13 on PHP 8.3+ can use raw DB queries or packages like pgvector/pgvector-php. Keep embedding calls in a queued job so HTTP requests stay fast. Batch embed where the API allows it. OpenAI’s embedding endpoints accept array inputs, which cuts round trips during nightly re-index jobs.

use Illuminate\Support\Facades\DB;
use OpenAI\Laravel\Facades\OpenAI;

$response = OpenAI::embeddings()->create([
    'model' => 'text-embedding-3-small',
    'input' => $chunks,
]);

foreach ($response->embeddings as $i => $item) {
    DB::table('document_chunks')->insert([
        'tenant_id' => $tenantId,
        'content' => $chunks[$i],
        'embedding' => '[' . implode(',', $item->embedding) . ']',
        'metadata' => json_encode(['lang' => 'en']),
    ]);
}

Similarity query with metadata filter

Always parameterize tenant and visibility filters. A missing WHERE tenant_id = ? clause in multi-tenant RAG is a data leak waiting to happen. This query pattern uses cosine distance via the <-> operator in pgvector.

$results = DB::select('
    SELECT id, content, source_url,
           1 - (embedding <=> ?::vector) AS similarity
    FROM document_chunks
    WHERE tenant_id = ?
      AND metadata->>\'visibility\' = \'public\'
    ORDER BY embedding <=> ?::vector
    LIMIT 8
', [$queryVector, $tenantId, $queryVector]);

Validate JSON payloads with a JSON formatter during development. Bad metadata shapes silently break filters at runtime. For production chat UIs, see building a RAG chatbot for product documentation and AI-powered search for Laravel products.

PHP developers without Laravel can follow the same SQL patterns from our vector databases for PHP developers guide. Symfony 8.1 apps on PHP 8.4.1 use identical PostgreSQL integration through Doctrine DBAL raw queries or native PDO.

How do you evaluate, secure, and operate vector databases for RAG?

Vector search fails quietly. The API returns 200 OK. The answer just sounds confident and wrong. You need evaluation metrics and observability from week one, not after a client complaint.

  • Recall@K — Did the correct source appear in the top K results for a labelled question set?
  • Answer faithfulness — Does the LLM response stick to retrieved text without inventing facts?
  • Latency p95 — Embedding + search + LLM must fit your UX budget, often under 3–5 seconds total.
  • Index freshness — Track last indexed timestamp per document; stale chunks cause outdated answers.
  • Cost per query — Embedding and token usage add up; see AI rate limits and cost optimization.
Common Vector RAG Failure ModesBefore Fix• Chunks split mid-sentence• Stale index after CMS update• Missing tenant filter• Query/doc embedding mismatchAfter Fix• Heading-aware chunking• Webhook-triggered re-index• Scoped SQL metadata filter• Single embedding model versionProduction ChecklistMonitor: recall@5, p95 search ms, index lag hoursBackup: vectors live in Postgres — include in nightly dumpsVersion: pin embedding model in config and migration notes
Vector databases explained for RAG include knowing failure modes: chunking, freshness, filters, and embedding consistency.

Security and compliance basics

Vectors are not encryption. Anyone with database access can read stored chunk text. Treat the vector table like any sensitive content table. Use row-level security in PostgreSQL for tenant isolation. Redact PAN numbers, passport details, or client identifiers before indexing on legal portals. If you host on a VPS you manage, fold vector backups into the same cron dumps described in our Linux system administration workflows.

For regulated content, log which chunk IDs were retrieved for each answer. That audit trail matters when a user challenges automated guidance on a notary or immigration workflow. A portal like Translation Nepal or Mijar Law Associates benefits from cite-back links in the UI, not just a chat bubble with plain text.

Operational tasks your cron should handle

Schedule these jobs explicitly:

  1. Nightly full or incremental re-index of changed CMS pages.
  2. Weekly vacuum and index health check on pgvector HNSW indexes.
  3. Embedding model version audit — never silently upgrade the model without re-embedding all chunks.
  4. Dead chunk cleanup when source documents are deleted or unpublished.

Managed vector services shift index maintenance to the vendor. You still own ingestion logic, chunk quality, and prompt design. Either way, RAG is an application feature, not a one-time database install.

Key Takeaways

  • Vector databases store embeddings and run similarity search so RAG retrieves relevant chunks before the LLM answers.
  • Chunking quality and metadata filters usually matter more than picking the fanciest ANN algorithm on day one.
  • pgvector on PostgreSQL 18 fits most Laravel and PHP projects until you prove you need a dedicated managed vector service.
  • Always scope queries by tenant and visibility; vector search without filters is a common multi-tenant data leak.
  • Measure recall@K and index freshness — bad retrieval returns fluent but wrong answers.
  • Pin embedding model versions and re-index when you change models; never mix vectors from different embedders.

People Also Ask

Is a vector database the same as a regular database?

No. A regular relational database stores rows and columns with exact-match indexes. A vector database—or a vector extension like pgvector—adds similarity search over float arrays. Many teams use both in one PostgreSQL instance: orders in normal tables, document embeddings in a vector column with an HNSW index.

Do you need a vector database if you use RAG?

You need vector search capability, not necessarily a separate product. RAG requires storing embeddings and finding nearest neighbours at query time. That can live in pgvector, Redis vector sets, OpenSearch, or a managed service. The architecture demands similarity retrieval; the vendor is flexible.

How many documents can pgvector handle for RAG?

pgvector handles hundreds of thousands to a few million vectors comfortably on a well-sized server with HNSW indexes. Beyond that, query latency, index build time, and RAM pressure may push you toward a dedicated ANN service or sharding strategy. Start with Postgres unless benchmarks say otherwise.

What embedding model should I use for RAG in 2026?

Use one production-grade embedding model and keep it consistent. OpenAI text-embedding-3-small is a common default for English and multilingual business content. Open-source models work when data cannot leave your server. Match the model across indexing and query paths, and document the version in your deployment config.

Ship RAG with the right vector layer

Vector databases explained for RAG boil down to a simple division of labour: embeddings and similarity search find the facts; the LLM formats the answer. Pick pgvector when you already run PostgreSQL. Reach for managed ANN when scale and latency demand it. Invest in chunking, filters, and evaluation before you invest in exotic infrastructure.

If you want RAG on a Laravel portal, legal knowledge base, or internal documentation site, I help teams design the ingestion pipeline, database schema, and AI integration without over-engineering the stack. See related work in our portfolio or start a scoped discussion via contact us. For API design around your retrieval service, our API development and custom software development pages outline how we typically structure those projects.

Frequently Asked Questions

It stores numeric embedding arrays and finds semantically similar chunks by cosine or dot-product distance, so RAG can retrieve relevant text before calling the LLM.

No. RAG needs similarity search, which pgvector, Redis, OpenSearch, or managed services provide. A dedicated product is optional until scale or latency demands it.

Typically Rs 3,000–8,000 per month (~USD 22–60) on a VPS you already pay for, since pgvector adds to existing PostgreSQL 18 rather than a separate bill.

A vector database stores high-dimensional float arrays called embeddings that capture semantic meaning of text chunks. RAG needs one because traditional SQL indexes excel at exact matches but fail when phrasing differs, such as a user asking about cancelling a booking while docs describe trek deposit refunds. Vector search embeds the question, compares it against stored document vectors, and returns nearest neighbours. Without that retrieval step you either stuff the entire knowledge base into every prompt or hope the LLM guesses correctly, which is expensive at scale and risky for legal, medical, or financial content.

RAG splits into two phases teams should not merge into one script. Offline indexing is batch-oriented and idempotent: extract text from HTML, PDF, or CMS rows, chunk into overlapping segments, embed each chunk, then upsert vector, text, and metadata into the store. Online retrieval runs on every user message: embed the question, fetch top-K neighbours with optional metadata filters, optionally re-rank candidates, then pass the best chunks into the LLM prompt with citations. Indexing can rerun safely overnight; retrieval must stay latency-sensitive because it sits on the critical path of every chat response.

An embedding is a fixed-length float array representing semantic meaning. OpenAI text-embedding-3-small returns 1536 dimensions by default; Cohere, Voyage, and open-source models like nomic-embed-text use different sizes. You must embed both documents and queries with the same model and dimension count because mixing models breaks similarity math entirely. Distance metrics also matter: cosine similarity is the default for normalized embeddings, inner product works when vectors are not normalized, and Euclidean distance appears in some libraries. PostgreSQL pgvector supports all three, but you must pick one metric and stay consistent across index creation and query code.

There is no universal winner. PHP and Laravel teams already running PostgreSQL 18 often add pgvector to keep transactional data and vectors in one database, simplifying backups and tenant access control for under roughly five million vectors. Managed Pinecone or Qdrant Cloud suits fast launch and sub-50 ms search at scale when you have no DBA time, at Rs 8,000–25,000 per month (~USD 60–185). Redis 8.10 works for low-latency cache plus small vector sets. OpenSearch fits hybrid keyword and vector search on large corpora but carries heavier ops overhead. Match the choice to data volume, existing stack, and latency budget rather than hype.

pgvector comfortably handles hundreds of thousands to a few million vectors on a properly sized PostgreSQL 18 server. Exact brute-force comparison works for thousands of rows but fails at millions, which is when you add an HNSW or IVFFlat approximate nearest neighbour index. HNSW indexes can consume several times the raw vector storage, so plan disk and RAM before indexing five hundred thousand legal document chunks. For most client projects I evaluate, pgvector wins when vector count stays under a few million and the team already runs Laravel with PostgreSQL. Beyond that, managed services or dedicated clusters become worth evaluating.

Not every AI search feature needs a dedicated vector engine on day one. Below roughly ten thousand chunks, a PostgreSQL table with sequential scan and proper caching can serve internal admin tools adequately. Once p95 query latency crosses your threshold or CPU spikes during search, add an HNSW index. Incremental adoption beats over-provisioning a managed vector cluster for a brochure site with forty FAQ entries. This mirrors how I approach client projects: prove retrieval value with minimal infrastructure first, then scale indexes when real traffic and document volume justify the RAM and maintenance overhead.

Split text into overlapping segments of roughly three hundred to eight hundred tokens with ten to twenty percent overlap. Chunk quality drives retrieval quality more than index tuning does. A common mistake is splitting on fixed character counts and cutting sentences in half. Prefer structure-aware chunking using headings, paragraphs, or logical CMS sections. On a Laravel documentation site, chunking by heading hierarchy often beats naive token windows. Poor chunks produce confident but wrong LLM answers even when your vector index is perfectly tuned, so invest time in extraction and splitting before worrying about HNSW parameters.

Exact brute-force vector comparison works for thousands of rows but fails at millions. Approximate nearest neighbour algorithms trade a small accuracy loss for large speed gains. HNSW, Hierarchical Navigable Small World, is the most common choice in 2026. IVFFlat partitions vectors into lists and searches a subset; pgvector supports both. Managed services like Pinecone and Weaviate build indexes automatically. Index build time and memory use are operational costs people forget. Schedule weekly vacuum and index health checks on pgvector HNSW indexes, and plan disk and RAM before large-scale legal document indexing jobs.

Top-K vector results are fast but imperfect. A cross-encoder re-ranker scores each query-chunk pair more accurately than bi-encoder embeddings alone. The practical pattern: retrieve twenty to fifty candidates by vector search, re-rank down to the best five, then send those to the LLM. Latency increases by roughly one hundred to three hundred milliseconds, but answer quality often jumps noticeably on technical documentation and long-form legal guides. For production chat UIs on product docs or notary checklists, that quality gain usually justifies the extra latency within a three to five second total UX budget.

Enable the vector extension, create a document_chunks table with tenant_id, content, embedding vector(1536), and metadata JSONB, then add an HNSW index using vector_cosine_ops. Laravel 13 on PHP 8.3 or higher can use raw DB queries or the pgvector/pgvector-php package. Keep embedding calls in queued jobs so HTTP requests stay fast, and batch embed where OpenAI endpoints accept array inputs to cut round trips during nightly re-index jobs. Always parameterize tenant and visibility filters in similarity queries. A missing WHERE tenant_id clause in multi-tenant RAG is a data leak waiting to happen.

Production RAG rarely runs pure vector search alone. You filter by tenant ID, document type, language, or publication date before or after the approximate nearest-neighbour step depending on the engine. A law firm portal might scope search to client-visible files only; a multi-vendor marketplace might restrict results to one seller catalogue. Store metadata in the same row as the vector. Apply SQL or JSON filters alongside the pgvector cosine distance operator. Validate JSON payloads during development because bad metadata shapes silently break filters at runtime, returning empty or overly broad result sets without obvious errors.

Vectors are not encryption; anyone with database access can read stored chunk text. Treat the vector table like sensitive content, use PostgreSQL row-level security for tenant isolation, and redact PAN numbers or passport details before indexing on legal portals. Log which chunk IDs were retrieved for each answer so users can challenge automated guidance. Measure recall@K on labelled questions, answer faithfulness to retrieved text, latency p95 across embed-search-LLM, index freshness per document, and cost per query. Vector search fails quietly with 200 OK responses and fluent wrong answers, so observability from week one matters more than after a client complaint.

Share this article

0 Comments

Leave a comment

Your email is not published. Comments appear once they have been read. Sign in to have your details filled in.

Quick Contact Options
Choose how you want to connect me: