Scaling PostgreSQL past 100 million rows requires composite covering B-Tree indexes (`INCLUDE`) to enable Index-Only Scans, Block Range Indexes (BRIN) or declarative monthly range partitioning on append-only time-series tables to keep working indexes in RAM, and transaction-mode connection pooling (PgBouncer/Supavisor) to prevent serverless connection exhaustion.
When a SaaS dashboard slows to a 6-second crawl, teams often blame PostgreSQL and reach prematurely for complex NoSQL clusters. In reality, a single well-tuned PostgreSQL instance can easily sustain 10,000+ queries per second across hundreds of millions of rows. Almost every PostgreSQL bottleneck at scale stems from three root causes: bloated indexes that no longer fit in shared buffers RAM, unpartitioned append-only tables, or exhausted connection pools.
1. Reading `EXPLAIN (ANALYZE, BUFFERS)`: Why Row Counts Lie and I/O Tells the Truth
Never optimize a slow SQL query by guessing or looking only at execution time. Always run `EXPLAIN (ANALYZE, BUFFERS)` and inspect `Buffers: shared hit` (pages read from fast memory cache) versus `shared read` (pages fetched from disk).
A query that scans 5,000 scattered heap pages across disk to filter out soft-deleted rows will take 400ms under load. Converting that index into a **Partial Covering Index** (`WHERE deleted_at IS NULL INCLUDE (...)`) reduces heap fetches to zero via an Index-Only Scan.
2. Choosing the Right Index: B-Tree, Partial, Covering, GIN, and BRIN
Every index speeds up specific reads at the direct cost of slowing down `INSERT`/`UPDATE` writes and consuming RAM. Selecting the exact index structure for your access pattern is critical:
| PostgreSQL Index Type | Storage Footprint (100M Rows) | Optimal Query Operators | Ideal Production Use Case |
|---|---|---|---|
| B-Tree (Composite + INCLUDE) | 2.5 – 6.0 GB | `=`, `<`, `<=`, `>`, `BETWEEN`, `ORDER BY` | High-selectivity multi-tenant lookups & pagination |
| Partial B-Tree (`WHERE ...`) | 200 – 800 MB (80% smaller) | Filtered equality & range queries | Indexing only `status = 'PENDING'` or active rows |
| GIN (Generalized Inverted) | 3.0 – 8.5 GB | `@>`, `?`, `?&`, `@@` (JSONB & Full-Text) | Searching JSONB metadata attributes or tsvector text |
| BRIN (Block Range Index) | 2 – 5 MB (99.8% smaller!) | Time-range filters on append-only tables | Chronological audit logs & IoT sensor streams |
3. Declarative Partitioning at 100M+ Rows & Partition Pruning
Once a table exceeds 50–100 million rows (or ~50 GB on disk), its B-Tree indexes exceed available RAM, causing cache thrashing and multi-hour `VACUUM` passes. By converting large time-series or event tables (`campaign_events`, `audit_logs`) to **Declarative Range Partitioning** by month, PostgreSQL's query planner automatically performs **Partition Pruning**—scanning only the current month's compact partition and ignoring historical partitions.
Better yet, expiring data older than your 12-month retention policy changes from an expensive `DELETE FROM logs WHERE created_at < ...` (which generates millions of dead tuples and bloats disk) into an instantaneous metadata operation: `DROP TABLE telemetry_logs_y2025m09;`.
4. Solving Serverless Connection Exhaustion with Transaction Pooling
Each native PostgreSQL backend connection spawns an OS process consuming ~5–10 MB of RAM. When a Next.js serverless deployment scales to 300 concurrent Lambda/Edge invocations during a traffic spike, opening 300 direct Postgres connections will trigger `FATAL: sorry, too many clients already`.
Always route application traffic through PgBouncer or Supavisor in **Transaction Pooling Mode**, which multiplexes thousands of lightweight application connections across a stable pool of 40–80 physical Postgres database backends.
5. Tuning Autovacuum to Prevent Transaction ID Wraparound & Dead Tuple Bloat
In PostgreSQL's MVCC architecture, every `UPDATE` is physically a `DELETE` + `INSERT`. On high-churn tables, lower `autovacuum_vacuum_scale_factor` from the default `0.2` (20% of table rows!) down to `0.02` (2%) with a higher `autovacuum_vacuum_cost_limit` so dead tuples are reclaimed continuously in small increments rather than massive I/O spikes.
-- 1. Covering index enabling Index-Only Scan without heap fetches
CREATE INDEX CONCURRENTLY idx_campaign_events_tenant_date_cover
ON campaign_events (tenant_id, occurred_at DESC)
INCLUDE (event_type, prospect_id)
WHERE deleted_at IS NULL;
-- 2. Declarative Range Partitioning for 100M+ append-only telemetry rows
CREATE TABLE telemetry_logs (
id BIGINT GENERATED ALWAYS AS IDENTITY,
tenant_id UUID NOT NULL,
recorded_at TIMESTAMPTZ NOT NULL,
payload JSONB NOT NULL,
PRIMARY KEY (tenant_id, recorded_at, id)
) PARTITION BY RANGE (recorded_at);
CREATE TABLE telemetry_logs_y2026m10 PARTITION OF telemetry_logs
FOR VALUES FROM ('2026-10-01 00:00:00+00') TO ('2026-11-01 00:00:00+00');Conclusion
Before sharding your database or migrating to exotic distributed datastores, master PostgreSQL's native arsenal: covering partial indexes, BRIN indexes, declarative range partitioning, and transaction pooling.
Verified Documentation & Evidence Sources
- PostgreSQL 16 Official Documentation — Index Types (B-Tree, GIN, GiST, BRIN)Accessed 2026-10-24
- PostgreSQL Official Documentation — Table Partitioning (5.11)Accessed 2026-10-24
Experiencing database bottlenecks or scaling challenges?
Work with Codiest's senior backend and database engineers to audit, index, and scale your PostgreSQL infrastructure.


