Skip to main content

Posts

Showing posts with the label PostgreSQL

PostgreSQL Index Bloat: When VACUUM Isn't Enough and REINDEX CONCURRENTLY Saves Production

  You have a high-throughput PostgreSQL database. You monitor your disk usage and notice your indexes are growing disproportionately to your table data. Query performance is degrading—specifically, Index Scans are becoming I/O bound. You check   pg_stat_user_tables   and confirm that   autovacuum   is running frequently. The natural assumption is that  autovacuum  handles space reclamation. However, in write-heavy environments—especially those with random-access updates or deletes— autovacuum  often fails to shrink the physical footprint of an index. The index becomes "bloated," containing massive amounts of empty space that the OS filesystem still has to cache or read. Historically, reclaiming this space required  REINDEX , which locks the table against writes—a non-starter for 24/7 production systems. The solution introduced in PostgreSQL 12 (and refined since) is  REINDEX CONCURRENTLY . The Root Cause: MVCC and B-Tree Fragmentation To...

PostgreSQL pgvector Optimization: Tuning HNSW vs IVFFlat for Billion-Scale Embeddings

  The "Day Two" Scaling Trap Building a vector search prototype with PostgreSQL and  pgvector  is deceptively simple. You spin up a Docker container, ingest 10,000 document chunks, run a standard Cosine Similarity query, and get results in 15 milliseconds. Then "Day Two" arrives. You move to production with 50 million vectors (e.g., OpenAI’s  text-embedding-3-small  at 1536 dimensions). Suddenly, your index build takes 14 hours, your RAM usage spikes to 100%, causing OOM kills, and your simple  SELECT  query takes 2 seconds to return. The default configuration of  pgvector  is not tuned for high-throughput or large datasets. Scaling requires a deliberate choice between  IVFFlat  (Inverted File Flat) and  HNSW  (Hierarchical Navigable Small World) indexes, and more importantly, tuning the critical build-time and query-time parameters that govern the precision-performance trade-off. Root Cause: The Geometry of Indexing The p...