Sub topic 9
How Partition Pruning Reduces Scanned Data
Assume you know basic SQL, PostgreSQL table definitions, indexes, constraints, and the purpose of EXPLAIN. Partition pruning is PostgreSQL’s process of removing partitions from a query plan when their bounds cannot contain matching rows.
A partitioned table is one logical table backed by several physical tables. PostgreSQL compares a query’s condition with each partition’s bounds. It scans only partitions that might contain a match.
A query that can be pruned
This example creates two monthly partitions and asks for rows from only part of January.
CREATE TEMP TABLE orders (
order_id bigint NOT NULL,
order_date date NOT NULL,
amount numeric(12, 2) NOT NULL
) PARTITION BY RANGE (order_date);
CREATE TEMP TABLE orders_2026_01
PARTITION OF orders
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TEMP TABLE orders_2026_02
PARTITION OF orders
FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');
INSERT INTO orders (order_id, order_date, amount)
VALUES
(1, DATE '2026-01-10', 25.00),
(2, DATE '2026-02-10', 40.00);
EXPLAIN (COSTS OFF)
SELECT *
FROM orders
WHERE order_date >= DATE '2026-01-10'
AND order_date < DATE '2026-01-15';The plan should show a scan of orders_2026_01 and no scan of orders_2026_02. COSTS OFF makes the plan easier to read. In a larger table, avoiding unrelated partitions can reduce disk reads, planning work, and the number of rows passed to later operations.
When pruning works
Pruning works best when the query has a condition on the partition key that PostgreSQL can evaluate. For range partitioning, use comparisons that align with the bounds:
order_date >= DATE '2026-01-01'order_date < DATE '2026-02-01'order_date BETWEEN ... AND ...
Use half-open ranges, with an inclusive lower bound and an exclusive upper bound. This matches PostgreSQL partition bounds and avoids accidentally including the first value of the next partition.
PostgreSQL can also perform runtime pruning when a prepared statement or join supplies the value during execution. However, expressions that hide the partition key, such as applying an incompatible function or cast, can make pruning less effective. Test important queries with EXPLAIN, rather than assuming pruning occurred.
> Callout note > Partition pruning decides which partitions to scan. It does not replace indexes inside the selected partitions. A query may need both effective pruning and suitable partition-local indexes.
Check pruning explicitly
Look for partition names in EXPLAIN (ANALYZE, BUFFERS). ANALYZE executes the query, so use it carefully with expensive statements. Subplans Removed can indicate runtime pruning for some prepared or parameterized plans.
Exercise
A table is range-partitioned by orderdate. A query filters only rows where orderdate >= DATE '2026-02-01' and order_date < DATE '2026-03-01'. Which partitions should PostgreSQL normally scan?
Show answer
It should scan the partition whose bounds cover February 2026 and prune partitions whose bounds cannot contain those dates. Confirm this with EXPLAIN.