When Indexes Become a Bottleneck
PostgreSQL indexes are essential for database optimization. Imagine an e-commerce store with 500,000 products. Filtering by category and price takes 10 seconds. Users leave, conversion drops. EXPLAIN analysis shows a sequential scan of the products table — no index on category_id. Adding a B-tree index cuts the time to 50 ms. This is a classic case where a single DDL line changes everything. As engineers with 10+ years of experience, we have seen this many times.
But often the problem is different: indexes exist but are not used, are duplicated, or slow down writes. For example, the orders table mistakenly has three similar indexes on the same columns — they waste space and slow INSERT with no benefit. Statistics show that in an average project, up to 20% of indexes are junk. We perform an index audit to identify issues.
Our engineers perform database audits and PostgreSQL index tuning to eliminate such issues. Experience shows that proper index configuration reduces response time by up to 90% and lowers CPU load. Every case is unique, but our approach is systematic.
Problems We Solve
- Missing indexes on foreign keys. Deleting a parent row causes a Full Table Scan on the child table — a typical mistake. Adding a B-tree index on
category_idandpost_idsolves it. - Duplicate indexes. Developers often create indexes manually without checking existing ones. For example,
idx_products_category_idandidx_products_category_created— the second covers the first, making the first redundant. We find and remove such duplicates, saving space. - Wrong column order in composite indexes. Equality conditions should come first, then range/sort. Otherwise, the index is partially used and part of the filtering hits the heap.
- Index bloat. Over time, indexes fragment; when
dead_tuple_percent > 20%, performance drops. We rebuild problematic indexes withREINDEX CONCURRENTLYwithout locking. Regular VACUUM and autovacuum tuning help manage bloat, and using pg_repack can rebuild indexes without locks. Understanding underlying mechanisms like TOAST storage, visibility map, and autovacuum thresholds helps in diagnosing bloat. The B-tree deduplication feature in PostgreSQL 14 reduces space for duplicate keys. Right-hand growth in indexes can be mitigated by using hash indexes for equality conditions. Cardinality estimation relies on statistics; ensuring up-to-date statistics improves query planning.
How We Optimize Indexes
- Analyze query plans. Collect
pg_stat_statements, find slow queries, examineEXPLAIN (ANALYZE, BUFFERS). - Audit existing indexes. Check for unused (
idx_scan = 0), duplicate, and missing FK indexes. - Design optimal indexes. Build a query matrix → recommend partial, covering, GIN indexes. Align with developers. We recommend GIN index for full-text search. Leverage index-only scans for covering indexes.
- Create and drop indexes. All production changes via
CREATE INDEX CONCURRENTLYandDROP INDEX CONCURRENTLY— no write locking. - Test. Run load tests, verify timing improvements.
- Document and migrate. Record changes in
migrations/, add code comments.
Comparison of Index Types
Detailed index type table
| Type | When to use | Size | Write impact |
|---|---|---|---|
| B-tree | Equality, ranges, ORDER BY, LIKE 'prefix%' | Medium | Moderate |
| GIN index | Arrays, JSONB, full-text search | Large | Slow inserts |
| GiST | Geodata, range types, full-text | Smaller than GIN | Faster build |
| BRIN | Sequentially inserted data (logs, metrics) | Very small | Minimal |
| Hash | Only equality | Small | Fast (rarely needed) |
Practical Example: Partial Index for Orders
In an e-commerce store, 80% of orders have status 'completed'. We rarely search for 'pending' or 'processing'. We create a partial index:
CREATE INDEX idx_orders_pending ON orders (user_id, created_at DESC) WHERE status IN ('pending', 'processing'); It takes 2 MB instead of 50 MB for a full index, and searching for active orders sped up 10× (according to EXPLAIN). Partial indexes are 10 times better than full indexes for filtered queries. Composite indexes with columns in the right order are 50 times better for multi-condition queries. Refer to PostgreSQL documentation for detailed syntax.
How to Know if Indexes Need Optimization?
If query execution time grows with table size, if EXPLAIN shows Seq Scan on large tables, or if pg_stat_user_indexes.idx_scan = 0 for some indexes — it's time to act. We perform an audit within 48 hours and provide a detailed report.
Why Trust Us with Index Tuning?
Our engineers hold PostgreSQL certifications and have 10+ years of experience in web development. We have optimized databases for over 100 projects — from e-commerce stores to SaaS platforms. Our focus on PostgreSQL performance ensures at least 30% query performance improvement (measured via pg_stat_statements). Our combined index audit and query optimization strategies ensure query acceleration.
Deliverables
- Audit report with query plans "before/after".
- List of created and dropped indexes.
- SQL migration scripts with comments.
- Monitoring instructions (queries for
pg_stat_user_indexes, bloat check). - 2 weeks of support (consultations on new queries).
Additional Performance Metrics
| Query | Time Before (ms) | Time After (ms) | Speedup |
|---|---|---|---|
| Select orders by status | 450 | 40 | 11× |
| Filter products by category and price | 320 | 25 | 13× |
| Full-text search on description | 1200 | 85 | 14× |
For one client, removing duplicate indexes reduced database size by 15 GB, saving $250 per month in cloud storage. This represents a 60% reduction in storage costs. Query speedup of 90% lowered compute costs by $500 per month and reduced CPU load by 30%. An audit of a 50 GB database costs $2,000. Audit costs $2,000 for a typical 50 GB database, and the resulting savings can exceed $750 per month. Get in touch to order an audit and index tuning for your project. We'll show you which queries can be accelerated.
Estimated Timelines
Audit and recommendations — from 1 day (depending on database size). Development and implementation of optimal indexes — from 2 to 5 days. Pricing is determined individually after analysis.







