PostgreSQL Enterprise (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What is Table Partitioning in PostgreSQL (Range, List, Hash)?
senior LevelCode ExampleSystem DesignDatabases & SQLPostgreSQL
In Simple Words: Declarative partitioning splits a large table into smaller physical partition tables:
- Range Partitioning: e.g. Partition by month or year (
PARTITION BY RANGE (created_at)). - List Partitioning: e.g. Partition by country code (
PARTITION BY LIST (country)). - Hash Partitioning: e.g. Distribute rows evenly across $N$ partitions using hash (
PARTITION BY HASH (user_id)). - Partition Pruning: The query planner automatically excludes unneeded partition tables during query execution.
sql
-- Declarative Range Partitioning:
CREATE TABLE measurements (
id BIGINT,
recorded_at DATE NOT NULL
) PARTITION BY RANGE (recorded_at);
CREATE TABLE measurements_2026_01 PARTITION OF measurements
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');