When audit logs or event streams exceed 50 million rows, standard B-Tree indices exceed RAM cache sizes. Partitioning tables by month or tenant allows PostgreSQL query planner to prune untouched partitions completely.
Creating Declarative Range Partitions in PostgreSQL 16+sql
CREATE TABLE telemetry_events (
id BIGSERIAL,
event_time TIMESTAMPTZ NOT NULL,
device_id UUID NOT NULL,
payload JSONB
) PARTITION BY RANGE (event_time);
-- Monthly partition
CREATE TABLE telemetry_2026_02 PARTITION OF telemetry_events
FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');
CREATE INDEX idx_telemetry_2026_02_device
ON telemetry_2026_02 (device_id, event_time);