The idea · Lab 3.4.1A B-tree index makes finding matching rows within a table faster, but it does not shrink the table itself — every index still ultimately points back into one large heap. Partitioning takes a structurally different approach: the table is split into several genuinely separate child tables, each holding a specific range (or list, or hash bucket) of rows, unified under one parent table name for querying. A query whose WHERE clause can be matched against the partitioning column lets the planner skip entire child partitions before touching a single row in them — not a faster scan of everything, but no scan at all of what clearly cannot match.
This lab builds a genuinely partitioned table by month, confirms real rows land in the correct child partition, and proves partition pruning directly in EXPLAIN: a query for one month's data touches only that partition, and a query spanning two months touches exactly those two while a third, irrelevant one is completely absent from the plan.
A Range-Partitioned Table, Three Months
CREATE TABLE sql_orders_part (id int, order_date date, amount numeric) PARTITION BY RANGE (order_date);
CREATE TABLE sql_orders_part_jan PARTITION OF sql_orders_part FOR VALUES FROM ('2025-01-01') TO ('2025-02-01');
CREATE TABLE sql_orders_part_feb PARTITION OF sql_orders_part FOR VALUES FROM ('2025-02-01') TO ('2025-03-01');
CREATE TABLE sql_orders_part_mar PARTITION OF sql_orders_part FOR VALUES FROM ('2025-03-01') TO ('2025-04-01');
Each child partition is a genuine, separate table with its own explicit range boundary — FOR VALUES FROM ... TO ... is a half-open interval, inclusive of the start date and exclusive of the end.
Real Rows, Routed to the Correct Partition
INSERT INTO sql_orders_part SELECT g, DATE '2025-01-01' + ((g%89)||' days')::interval, g FROM generate_series(1,90000) g;
SELECT count(*) FROM ONLY sql_orders_part_jan; -- 31362
SELECT count(*) FROM ONLY sql_orders_part_feb; -- 28308
SELECT count(*) FROM ONLY sql_orders_part_mar; -- 30330
A single INSERT against the parent table sql_orders_part — PostgreSQL routes every row to its correct child partition automatically, based on the row's own order_date value. ONLY restricts the count to that specific child table, confirming the routing directly rather than trusting it blindly.
Partition Pruning, Proven in EXPLAIN
EXPLAIN SELECT * FROM sql_orders_part WHERE order_date >= '2025-01-01' AND order_date < '2025-02-01';
Seq Scan on sql_orders_part_jan sql_orders_part (cost=0.00..476.00 rows=102 width=40)
Only sql_orders_part_jan appears — _feb and _mar are not scanned, not even considered, because the planner proved from the WHERE clause alone that they cannot contain any matching rows.
EXPLAIN SELECT * FROM sql_orders_part WHERE order_date >= '2025-01-15' AND order_date < '2025-02-15';
Append (cost=0.00..908.17 rows=194 width=40)
-> Seq Scan on sql_orders_part_jan sql_orders_part_1
-> Seq Scan on sql_orders_part_feb sql_orders_part_2
This range spans January and February — both appear under Append, and _mar is completely absent, correctly excluded even though it was never explicitly mentioned in the query.
Key concepts
Partition pruning
The planner's ability to prove, from a query's WHERE clause and each partition's declared range/list/hash bounds, that certain child partitions cannot possibly contain any matching rows — and to exclude them from the plan entirely, not merely scan them quickly. A pruned partition does not appear in EXPLAIN's output at all, which is the direct, checkable evidence that pruning genuinely happened rather than being assumed.
PARTITION BY RANGE / LIST / HASH
The three partitioning strategies PostgreSQL supports: RANGE assigns rows to a partition based on where their value falls within a declared interval (dates, sequential IDs); LIST assigns rows based on exact value membership in an explicit set (a country code, a status value); HASH assigns rows to a fixed number of partitions based on a hash of the partitioning column, used to spread load evenly when there is no natural range or list grouping.
What to remember
- Partitioning splits one logical table into genuinely separate child tables by range, list, or hash — a query that can be matched against the partition key can skip entire child tables, not just scan them faster
- A single INSERT against the parent table automatically routes each row to its correct child partition based on the row's own value
- Partition pruning is directly checkable in EXPLAIN: a pruned partition does not appear in the plan at all, rather than appearing with a suspiciously low cost
- FOR VALUES FROM ... TO ... ranges are half-open by convention, guaranteeing every value belongs to exactly one partition with no boundary ambiguity
- pg_partman automates the exact manual DDL this lab performed by hand — understanding the manual mechanics first is what makes trusting the automation reasonable