Lesson 3 of 4 Vector Search and RAG with PostgreSQL

Sizing PostgreSQL for Vector Workloads

This lesson explains how to estimate vector and index sizes, why indexes must fit in memory, and how compute, storage, replicas and monitoring fit together.

Learning outcomes

After completing this lesson, you will be able to:

  • Estimate the size of vectors and indexes.
  • Explain why vector indexes should stay in memory.
  • Choose between scaling up, adding replicas and faster storage.
  • Identify metrics that show an undersized server.

Estimating the size of vectors and indexes

A vector element is a 4-byte float, so a 1,536-dimension embedding occupies about 6 KB, and a million of them about 6 GB before any index. An HNSW index stores the vectors again plus the graph's neighbour lists, so it is typically somewhat larger than the raw vectors; halfvec halves both. Rather than relying on estimates, measure real sizes on a representative sample and scale up.

SELECT pg_size_pretty(pg_relation_size('chunks'))                AS table_size,
       pg_size_pretty(pg_relation_size('chunks_embedding_hnsw')) AS hnsw_index_size,
       pg_size_pretty(pg_total_relation_size('chunks'))          AS total_with_indexes;

The figure that matters for latency is the size of the indexes that searches touch, compared with the memory available for caching them.

Fill in the value

Sizing vectors

Exam skills 2.2.4

Estimate the size of embeddings from their dimensions.

Log in to try this activity Interactive activities are free with an account.

Memory: shared buffers and the operating system cache

PostgreSQL caches table and index pages in shared buffers and also benefits from the operating system's file cache. On flexible server, shared_buffers is set according to the compute size. An HNSW search visits graph nodes in an unpredictable order, so if the index is not in memory, each search causes many random disk reads, and latency can rise from a few milliseconds to hundreds.

The practical rule is to choose a server whose memory comfortably holds the vector indexes in use plus the rest of the working set. This often points to the Memory Optimized tier, which has about twice the memory per vCore of General Purpose. After a restart or failover the cache is cold; the pg_prewarm extension can load an index into memory ahead of traffic. work_mem affects sorts and hash operations per query, and maintenance_work_mem affects index builds; both are per operation, so large values multiplied by many connections can exhaust memory.

The following diagram compares the size of an HNSW index with the memory of each server size, and shows the evidence of the problem.

Index size compared with server memory. At 12 million chunks the HNSW index is 70 GB, far more than the 32 GiB of the 8-vCore General Purpose server, so searches read graph pages from disk: CPU stays moderate while read IOPS rise. A Memory Optimized server with about 128 GiB keeps the index in memory.
Sizes are illustrative.
True or false

Memory for vector search

Exam skills 2.2.4

Decide whether each statement about memory and vector search is true.

Log in to try this activity Interactive activities are free with an account.

Compute, storage and replicas

Each search uses CPU to compute distances, so throughput at a given latency rises with vCores, and exact searches can use parallel workers. Index builds are also CPU-heavy, and a common pattern is to scale up compute for a large build or re-embedding and scale down afterwards, accepting the brief restart that a compute change needs.

Storage performance matters when the working set does not fit in memory and during bulk loads. With Premium SSD, IOPS grow with the provisioned size, so a small disk may have too few IOPS; Premium SSD v2 sets IOPS and throughput independently. Read replicas scale search throughput horizontally: retrieval queries go to replicas while writes go to the primary, at the cost of slight replication lag.

Scale up compute
FixesCPU-bound searches, memory too small for indexes
CostHigher per-hour price
CaveatBrief restart to change size
Choose when Indexes do not fit or CPU is saturated
Add read replicas
FixesToo many concurrent searches for one server
CostA full server per replica
CaveatReplication lag; writes still go to the primary
Choose when Read throughput is the limit
Faster storage
FixesDisk-bound loads and cache misses
CostHigher storage price
CaveatDoes not replace enough memory
Choose when IOPS or throughput metrics are at their limits
Scenario questions

Scaling a vector server

Exam skills 2.2.4

Choose the change that addresses each bottleneck.

Log in to try this activity Interactive activities are free with an account.

Monitoring the plan

Azure Monitor metrics for flexible server show whether the sizing holds. Persistently high CPU percent suggests more vCores or cheaper queries; high memory percent with rising read IOPS suggests indexes no longer fit in memory; disk queue depth and IOPS near the provisioned limit point to storage; and active connections near max_connections point to pooling. In the database, the ratio of buffer hits to reads in pg_statio_user_indexes shows how often each index is served from memory.

  1. Measure

    Record index sizes and baseline latency on representative data.

  2. Size

    Choose a tier and size whose memory holds the active vector indexes.

  3. Load

    Raise maintenance_work_mem and parallel workers for builds, then restore them.

  4. Watch

    Track CPU, memory, read IOPS and cache hit ratios as data grows.

Match the pairs

Reading the metrics

Exam skills 2.2.4

Match each metric pattern with what it suggests.

Log in to try this activity Interactive activities are free with an account.
Worked example

Latency that degrades as data grows

A RAG service on a General Purpose server with 8 vCores (32 GiB) searched 4 million chunks in about 15 ms. After growing to 12 million chunks, p95 latency is over 300 ms. CPU is at 35%, read IOPS have risen sharply, and the HNSW index is now 70 GB.

Moderate CPU with rising read IOPS indicates that the index no longer fits in memory, so searches read graph pages from disk. Moving to Memory Optimized compute with enough memory for the 70 GB index (for example 16 vCores with about 128 GiB) restores in-memory searches, and converting the index to halfvec would roughly halve its size. Pre-warming the index after the change avoids slow first queries.

Adding vCores within General Purpose adds CPU that is not the bottleneck. More IOPS would make disk reads faster but still far slower than memory. Raising hnsw.ef_search would increase the work per query and make latency worse.

Check yourself

About how much storage do one million 1,536-dimension vector embeddings need before indexing?

Show answer

About 6 GB: each element is a 4-byte float, so each vector is about 6 KB. An HNSW index adds roughly as much again, and halfvec halves both.

Exam Tips

Remember for the exam

  • A 1,536-dimension float32 vector is about 6 KB; measure index sizes with pg_relation_size.
  • Keep active vector indexes in memory; Memory Optimized doubles memory per vCore.
  • Scale up for builds, add read replicas for read throughput, faster storage for I/O limits.
  • Rising read IOPS with moderate CPU means the index has outgrown memory.