How We Cut PostgreSQL P95 Query Latency from 4.2s to 14ms Under 15,000 RPS Without Upgrading AWS RDS
Shafikul Islam
Backend & Cloud Infrastructure Engineer · Infrastructure & Cloud Team Lead

Why Throwing Larger RDS Instances at Slow Queries Fails
When a multi-tenant B2B SaaS database hits 98% CPU utilization and 4.2-second P95 query latency, the knee-jerk reaction is to scale the AWS RDS instance vertically from db.r6g.xlarge to db.r6g.4xlarge.
Vertical scaling buys temporary breathing room, doubles the monthly infrastructure bill, and leaves the underlying execution bottleneck untouched. As soon as tenant data grows another 30%, CPU saturates again.
Here is how we dropped PostgreSQL P95 query latency from 4.2 seconds to 14ms under 15,000 requests per second (RPS) without upgrading the AWS RDS instance.
1. Diagnosing the Sequential Scan Trap with EXPLAIN (ANALYZE, BUFFERS)
Our slowest endpoint fetched active dashboard notifications and pending tasks for a specific tenant and user role. Even though we had single-column indexes on tenant_id and status, PostgreSQL was spending 3.8 seconds reading tens of thousands of historical archived rows from disk and sorting them in memory.
Running EXPLAIN (ANALYZE, BUFFERS) exposed two expensive operations:
- Bitmap Heap Scan with Recheck: PostgreSQL combined two separate single-column indexes, read 180,000 historical pages, and filtered out 96% of them where is_archived = true.
- External Merge Disk Sort: Because the sort column (created_at DESC) was not part of a composite index, PostgreSQL spilled temporary sort files to disk on every dashboard refresh.
-- Before: Single-column indexes forced Bitmap Heap Scans + Disk Sort
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, title, priority, created_at
FROM tenant_tasks
WHERE tenant_id = '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
AND status = 'PENDING'
AND is_archived = false
ORDER BY created_at DESC
LIMIT 25;
2. Partial Composite Covering Indexes (CONCURRENTLY)
Because 94% of rows in tenant_tasks were completed or archived historical records, indexing the entire table wasted gigabytes of shared buffer RAM.
We replaced the single-column indexes with a partial composite B-Tree index built CONCURRENTLY so live production writes were never blocked:
CREATE INDEX CONCURRENTLY idx_tenant_tasks_active_feed
ON tenant_tasks (tenant_id, created_at DESC)
INCLUDE (id, title, priority)
WHERE status = 'PENDING' AND is_archived = false;
Why This Index Reduced Latency by 99.6%
- Partial Predicate (WHERE status = 'PENDING' AND is_archived = false): Shrunk the index footprint by 89%, keeping the entire active working set pinned inside PostgreSQL shared_buffers RAM.
- Composite Sort Match (tenant_id, created_at DESC): PostgreSQL walks the B-Tree in exact reverse chronological order and stops immediately after reading 25 rows.
- Covering Payload (INCLUDE id, title, priority): Enabled an Index-Only Scan with 0 heap fetches on clean visibility map pages.
3. Eliminating Connection Thrash with PgBouncer Transaction Pooling
At 15,000 RPS across 40 Kubernetes microservice pods, opening 800 direct PostgreSQL connections caused severe Linux kernel context-switching and lock contention on ProcArrayLock.
We deployed PgBouncer in transaction pooling mode between the application pods and RDS:
[databases]
saas_prod = host=rds-primary.internal port=5432 dbname=saas_prod
[pgbouncer]
pool_mode = transaction
max_client_conn = 4000
default_pool_size = 48
reserve_pool_size = 12
server_idle_timeout = 60
By multiplexing 4,000 application connections down to 48 active PostgreSQL backend processes, database CPU utilization dropped from 98% to 24%, and P95 query latency stabilized at 14ms.