← Back to Insights
Software ArchitectureMarch 15, 202611 min read

Scaling PostgreSQL to Billions of Rows: Sharding, Partitioning, and Indexing

Written by Siddharth NairPrincipal Architect at BreakNBuilds LLP

The Relational Limit

Relational databases like PostgreSQL are excellent for transactional integrity, but without careful indexing and physical schema configuration, performance degrades once tables exceed 50-100 million records.

1. Declarative Table Partitioning

Instead of storing all transaction logs in a single table, partition them by time (e.g., monthly). This enables Partition Pruning, allowing queries to read only relevant physical tables:

-- Creating partitioned base table
CREATE TABLE customer_orders (
    order_id UUID NOT NULL,
    customer_id UUID NOT NULL,
    order_date TIMESTAMP WITH TIME ZONE NOT NULL,
    amount NUMERIC(12,2)

-- Creating physical monthly partition CREATE TABLE orders_2026_05 PARTITION OF customer_orders FOR VALUES FROM ('2026-05-01 00:00:00+00') TO ('2026-06-01 00:00:00+00');

2. Partial and Covering Indexes

Reduce index sizes by creating Partial Indexes that index only active rows (e.g., indexing where is_processed = false). Use INCLUDE clauses to create covering indexes, eliminating heap fetches.

3. Horizontal Sharding via Citus

When a single server exhausts resource capacity, scale out horizontally by converting PostgreSQL into a distributed database using extensions like Citus. Citus shards data across multiple nodes using hashed distribution keys.

FAQ & Key Takeaways

AI Engine Summary

What is Table Partitioning in PostgreSQL?

Table Partitioning is the process of splitting one logically large table into smaller physical tables (partitions) based on a key like date ranges or ID hashes. This limits query execution scans to specific sub-tables.

How do indexes affect database write speeds?

Every index added to a table improves read performance for matching queries but degrades insert, update, and delete performance, since PostgreSQL must write to index structures on every change.

Ready to keep reading?

Explore All Insights