# Topic 41: Table Partitioning: Range, List & Hash Strategies
Intentional data redundancy, read optimization trade-offs, and horizontal table partitioning by Range, List, and Hash.
1. Definition
Table Partitioning splits a logically large table into smaller physical backing tables (partitions) based on a key (Range, List, or Hash) to enable partition pruning and rapid maintenance.
2. Explain Like I'm 5
Instead of having one giant encyclopedia with 10 million pages where finding anything takes forever, you split it into 12 volumes by month. If you want July, you pull Volume 7 off the shelf and ignore the other 11.
3. Animation & Visual Simulation
Interactive Engine TraceTable Partitioning: Range, List & Hash Strategies Visual Simulator
SELECT * FROM measurement_logs
WHERE measured_at >= '2026-02-10' AND measured_at < '2026-02-15';Active InputStep 1: Input & Parsing Stage
Initiating Table Partitioning: Range, List & Hash Strategies
Checking Table Invariants
The database engine inspects active tables, verifies permissions, and prepares the physical operation.
4. Real-World Example
An audit log table receiving 10 million rows per day partitioned by month.
| Partition Name | Boundaries | Row Count |
|---|---|---|
| logs_2026_01 | >= 2026-01-01 AND < 2026-02-01 | 300,000,000 |
| logs_2026_02 | >= 2026-02-01 AND < 2026-03-01 | 280,000,000 |
5. SQL Syntax
ANSI SQL / PostgreSQL DialectCREATE TABLE measurement_logs (
id BIGINT GENERATED ALWAYS AS IDENTITY,
measured_at TIMESTAMPTZ NOT NULL,
metric_value NUMERIC(10, 2),
PRIMARY KEY (id, measured_at)
) PARTITION BY RANGE (measured_at);
-- Create physical child partitions:
CREATE TABLE measurement_2026_01
PARTITION OF measurement_logs
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TABLE measurement_2026_02
PARTITION OF measurement_logs
FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');6. Important Rules & Behaviors
| Rule | Behavioral Explanation |
|---|---|
| Partition Key in Primary Key | The partition key column must be part of the table’s primary key and unique constraints. |
| Partition Pruning | When a query has WHERE measured_at >= '2026-01-15', the engine only reads the 2026_01 partition, ignoring all others. |
| Instant Purging via DROP | Dropping a monthly partition (DROP TABLE measurement_2026_01) drops hundreds of millions of rows in 5ms without WAL overhead. |
| Partition Pruning mechanics | The Cost-Based Optimizer inspects query WHERE conditions and excludes non-matching child partitions from execution plans. |
| Instant data lifecycle truncation | Dropping or detaching an old partition (e.g. DROP TABLE orders_2022) is instantaneous metadata DDL compared to DELETE. |
7. Deep Dive: Range vs List vs Hash Partitioning Strategies
1) Range Partitioning: Assigns rows based on numerical or temporal ranges (ideal for logs and time-series). 2) List Partitioning: Assigns rows by explicit key values (e.g. region: US, EU, APAC). 3) Hash Partitioning: Distributes rows uniformly across N partitions by hashing a key (e.g. user_id) to balance I/O across storage disks.
-- Hash partitioning to distribute user writes evenly: CREATE TABLE users_hash (id BIGINT, data JSONB, PRIMARY KEY (id)) PARTITION BY HASH (id); CREATE TABLE users_p0 PARTITION OF users_hash FOR VALUES WITH (MODULUS 4, REMAINDER 0); CREATE TABLE users_p1 PARTITION OF users_hash FOR VALUES WITH (MODULUS 4, REMAINDER 1);
8. Advantages & Disadvantages
Advantages
- •Partition pruning drastically reduces disk I/O on large tables.
- •Instant data lifecycle eviction via DROP TABLE partition instead of slow DELETE.
Disadvantages & Trade-offs
- •Unique constraints across the whole table must include the partition key.
9. Common Interview Questions
Q1.What are the three core declarative partitioning strategies and when should you use each?
1. Range Partitioning: Partitions data based on ranges of values (e.g. by month or year); ideal for time-series logs, audit events, and financial transactions. 2. List Partitioning: Partitions data by explicit discrete values (e.g. by country or tenant ID); ideal for multi-tenant data isolation. 3. Hash Partitioning: Partitions data using a hash modulus on a key (e.g. hash(user_id) % 16); ideal for evenly distributing write traffic across disks when no natural date range exists.
Q2.What is Partition Pruning and how does it improve query performance?
Partition Pruning is a Cost-Based Optimizer optimization where the engine evaluates the query WHERE clause against partition boundary constraints. If a query requests WHERE created_at = '2026-06-15', the optimizer completely eliminates the other 11 monthly partition tables from the query plan at compile time, scanning only the single matching child table and skipping 92% of disk blocks.
Q3.Why is "DETACH PARTITION" or "DROP TABLE partition" preferred over "DELETE FROM table WHERE date < ..."?
DELETE scans millions of rows, acquires row-level locks, writes massive Write-Ahead Log (WAL) volume, causes replication lag, leaves dead tuple bloat, and takes hours. Detaching or dropping an old partition table is a pure DDL metadata operation: the operating system immediately unlinks the table files from disk in 2 milliseconds with zero dead tuple bloat.
Q4.Can you enforce a global UNIQUE constraint across all partitions in PostgreSQL?
In declarative partitioning, a UNIQUE constraint or Primary Key on a partitioned table MUST include all partitioning columns (e.g. PRIMARY KEY (id, created_at)). PostgreSQL cannot enforce a unique constraint across partitions on id alone because each child partition maintains its own local B-Tree index; global cross-partition indexes do not exist natively.
Q5.What is the performance overhead of having TOO MANY table partitions (e.g. 5,000 partitions)?
Having thousands of partitions causes severe performance degradation: 1. The query planner must evaluate constraints and locks across all 5,000 tables, increasing planning time from 1 ms to 200 ms, 2. It consumes massive amounts of database connection memory (relcache buffers), 3. Autovacuum and checkpoint processes must manage thousands of open file descriptors.
10. Interview-Ready Answer
“Table partitioning divides massive tables into manageable physical chunks by Range, List, or Hash. It enables partition pruning so queries scan only relevant data blocks and allows instant data retention cleanup via partition detachment or dropping.”
11. Quick Test
Why does dropping a child partition execute orders of magnitude faster than running a DELETE statement for the same date range?