Partition pruning speeds up queries on partitioned tables by skipping partitions that cannot contain rows matching the WHERE clause.
How it works
When the query planner processes a query against a partitioned table, it examines each partition's definition to determine whether that partition can contain rows satisfying the WHERE clause. Partitions that provably contain no matching rows are excluded from the query plan entirely. This reduces the amount of data read from disk and shortens processing time.
Partition pruning affects both the statistics of scanned objects and the execution plan of a query.
Static and dynamic pruning
Pruning happens at different points depending on when the WHERE condition can be evaluated:
Static pruning occurs during the planning stage. The planner evaluates the condition before the query runs. A typical trigger is a
WHEREclause that compares a partition key column to a constant literal.Dynamic pruning occurs at runtime. Exact partitions are unknown during planning. Typical triggers include
WHEREconditions that use operators or functions.
Pruning stages
PolarDB for PostgreSQL (Compatible with Oracle) categorizes conditions into three types and applies pruning at the corresponding stage:
| Condition type | Example | Pruning stage |
|---|---|---|
| Immutable (constant literal) | logdate >= DATE '2023-10-01' | Optimizer stage |
Stable (e.g., now() function) | logdate >= now() | Early executor stage |
Volatile (e.g., random() function) | logdate >= (SELECT to_date('2023-10-1')) | Later executor stage |
Partition pruning vs. constraint exclusion
| Aspect | Partition pruning | Constraint exclusion |
|---|---|---|
| Partition relationship awareness | Yes — knows which partition holds a specific value | No — checks each partition's constraint independently |
| Stage | Early optimization | Later optimization |
For example, when searching for a record in a list-partitioned table, partition pruning directly identifies the target partition. Constraint exclusion must inspect the constraint of every partition.
Verify pruning with EXPLAIN
Use EXPLAIN or EXPLAIN ANALYZE to confirm that pruning is active. The following examples use a range-partitioned table measurement with four quarterly partitions for 2023, created using the logdate column as the partition key:
CREATE TABLE measurement(
city_id int not null,
logdate date not null,
peaktemp int,
unitsales int
) PARTITION BY RANGE (logdate);
CREATE TABLE measurement_y2023q1 PARTITION OF measurement
FOR VALUES FROM ('2023-01-01') TO ('2023-04-01');
CREATE TABLE measurement_y2023q2 PARTITION OF measurement
FOR VALUES FROM ('2023-04-01') TO ('2023-07-01');
CREATE TABLE measurement_y2023q3 PARTITION OF measurement
FOR VALUES FROM ('2023-07-01') TO ('2023-10-01');
CREATE TABLE measurement_y2023q4 PARTITION OF measurement
FOR VALUES FROM ('2023-10-01') TO ('2024-04-01');Optimizer stage pruning (immutable condition)
A constant literal on the partition key lets the planner prune at the optimizer stage. The execution plan lists only the partitions that can match the condition.
EXPLAIN SELECT * FROM measurement WHERE logdate >= DATE '2023-10-01';Expected output:
QUERY PLAN
-----------------------------------------------------------------------------
Append (cost=0.00..34.09 rows=567 width=20)
-> Seq Scan on measurement_y2023q4 (cost=0.00..31.25 rows=567 width=20)
Filter: (logdate >= '01-OCT-23 00:00:00'::date)
(3 rows)Only measurement_y2023q4 appears in the plan. The three earlier partitions are pruned and not shown.
Early executor stage pruning (stable condition)
A stable expression such as now() cannot be evaluated during planning, but the executor resolves it once before scanning begins. The plan reports pruned subplans with Subplans Removed.
EXPLAIN SELECT * FROM measurement WHERE logdate >= now();Expected output (when run in July 2023):
QUERY PLAN
-----------------------------------------------------------------------------
Append (cost=0.00..153.34 rows=2268 width=20)
Subplans Removed: 2
-> Seq Scan on measurement_y2023q3 (cost=0.00..35.50 rows=567 width=20)
Filter: (logdate >= now())
-> Seq Scan on measurement_y2023q4 (cost=0.00..35.50 rows=567 width=20)
Filter: (logdate >= now())
(6 rows)Subplans Removed: 2 shows that the two earliest partitions were pruned. Only Q3 and Q4 are scanned.
Later executor stage pruning (volatile condition)
A volatile expression such as a subquery is resolved during execution. Pruned partitions appear in the plan but are marked (never executed). This pruning mode also occurs for subjoins and join conditions.
EXPLAIN ANALYZE SELECT * FROM measurement WHERE logdate >= (SELECT to_date('2023-10-1'));Expected output:
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Append (cost=0.01..136.35 rows=2268 width=20) (actual time=0.067..0.068 rows=0 loops=1)
InitPlan 1 (returns $0)
-> Result (cost=0.00..0.01 rows=1 width=8) (actual time=0.051..0.053 rows=1 loops=1)
-> Seq Scan on measurement_y2023q1 (cost=0.00..31.25 rows=567 width=20) (never executed)
Filter: (logdate >= $0)
-> Seq Scan on measurement_y2023q2 (cost=0.00..31.25 rows=567 width=20) (never executed)
Filter: (logdate >= $0)
-> Seq Scan on measurement_y2023q3 (cost=0.00..31.25 rows=567 width=20) (never executed)
Filter: (logdate >= $0)
-> Seq Scan on measurement_y2023q4 (cost=0.00..31.25 rows=567 width=20) (actual time=0.004..0.004 rows=0 loops=1)
Filter: (logdate >= $0)Partitions Q1, Q2, and Q3 are marked (never executed), meaning the executor skipped them.
How to read EXPLAIN output for pruning
| Signal | Pruning stage | Meaning |
|---|---|---|
| Partition absent from the plan | Optimizer stage | Pruned before planning completes |
Subplans Removed: N | Early executor stage | N partitions pruned before scanning |
(never executed) | Later executor stage | Partition included in plan but skipped at runtime |