Sub topic 8
Partition pruning and reading EXPLAIN
Partitioning only pays off if the planner can skip partitions that cannot contain matching rows. This skipping is called partition pruning. This chapter shows when pruning happens, how to confirm it in EXPLAIN output, and which query shapes quietly defeat it.
It assumes you can read basic EXPLAIN output (node names, Filter: lines, cost numbers) and that you have the measurements table from the range partitioning chapter, with monthly partitions for January and February 2026.
What pruning is
Each partition has a bound: a range, a list of values, or a modulus and remainder. When a query has a condition on the partition key, the planner compares that condition with the bounds. A partition whose bound cannot be satisfied is left out of the plan, so it is never opened or scanned.
Pruning is controlled by the setting enable_partition_pruning, which is on by default. Turn it off only to compare plans while debugging.
SHOW enable_partition_pruning;When pruning happens
Pruning can happen at three points. The point depends on how much the planner knows about the filter value.
| Stage | Trigger | How it shows in EXPLAIN |
|---|---|---|
| Plan time | The filter compares the key with constants, or with expressions the planner can evaluate to a constant while planning. | Pruned partitions are absent from the plan. |
| Executor startup (initial pruning) | The value is known only when execution begins: a bound parameter of a prepared statement, or a stable function such as `now()`. | `Subplans Removed: N` under `Append`. |
| During execution (runtime pruning) | The value changes while the query runs, for example the inner side of a parameterized nested loop join. | Partitions marked `(never executed)` under `EXPLAIN ANALYZE`. |
Reading a plan-time pruned plan
Run the range query from the earlier chapter, with COSTS OFF to keep the output short:
EXPLAIN (COSTS OFF)
SELECT *
FROM measurements
WHERE recorded_at >= '2026-02-05' AND recorded_at < '2026-02-10';Seq Scan on measurements_2026_02 measurements
Filter: ((recorded_at >= '2026-02-05 00:00:00+00'::timestamp with time zone) AND (recorded_at < '2026-02-10 00:00:00+00'::timestamp with time zone))Three details matter here:
- Only
measurements_2026_02appears.measurements_2026_01was pruned, and nothing in the plan mentions it. - With a single surviving partition, PostgreSQL drops the
Appendnode and scans the partition directly. With two or more survivors you seeAppend(orMerge Appendfor ordered output) with one child per partition. - The scan is still a
Seq Scan. Pruning chooses which partitions to read. It does not choose an access method inside a partition. Without an index onrecorded_at, each surviving partition is read in full.
Compare this with a query that has no usable filter:
EXPLAIN (COSTS OFF)
SELECT * FROM measurements WHERE reading > 30;Append
-> Seq Scan on measurements_2026_01 measurements_1
Filter: (reading > '30'::numeric)
-> Seq Scan on measurements_2026_02 measurements_2
Filter: (reading > '30'::numeric)reading is not the partition key, so every partition must be scanned. Counting the children of Append is the quickest check of whether pruning worked.
Pruning with parameters and stable functions
A prepared statement does not know its parameter values when it is planned. After several executions, PostgreSQL may switch to a generic plan that is built without them. You can force this to see the behavior:
SET plan_cache_mode = force_generic_plan;
PREPARE month_readings (timestamptz) AS
SELECT *
FROM measurements
WHERE recorded_at >= $1
AND recorded_at < $1 + interval '1 day';
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
EXECUTE month_readings ('2026-02-05');Append (actual rows=0 loops=1)
Subplans Removed: 1
-> Seq Scan on measurements_2026_02 measurements_1 (actual rows=0 loops=1)
Filter: ((recorded_at >= $1) AND (recorded_at < ($1 + '1 day'::interval)))The plan still lists $1, but at executor startup PostgreSQL evaluated the bounds, found that only February can match, and removed the other subplan. Subplans Removed: 1 is the evidence. This example assumes the table has no default partition. A default partition would remain in the plan whenever the evaluated range could hold rows outside every defined partition.
Stable functions behave the same way. WHERE recorded_at >= now() - interval '1 day' cannot be reduced to a constant during planning, because now() is fixed per transaction rather than per plan. It is evaluated at startup, so the pruned partitions show up as Subplans Removed.
Reset the setting after the experiment:
RESET plan_cache_mode;Runtime pruning in joins
When a partitioned table is the inner side of a nested loop, each outer row supplies a new value for the join column. If that column is the partition key, PostgreSQL prunes again for every outer row. EXPLAIN ANALYZE reports partitions that were never touched:
-> Seq Scan on measurements_2026_01 m_1 (never executed)(never executed) means the executor skipped that node for every outer row. This differs from a node that ran and returned zero rows, which shows actual rows=0 loops=1.
Query shapes that defeat pruning
Pruning works by comparing the partition key column itself with a value. Anything that hides the column from the planner turns pruning off for that condition.
-- Scans every partition: the key is wrapped in a function
EXPLAIN (COSTS OFF)
SELECT * FROM measurements
WHERE date_trunc('month', recorded_at) = '2026-02-01';
-- Scans only February: the same intent expressed as a range on the bare column
EXPLAIN (COSTS OFF)
SELECT * FROM measurements
WHERE recorded_at >= '2026-02-01' AND recorded_at < '2026-03-01';Other common causes:
- Casts on the key.
recorded_at::date = '2026-02-05'is an expression on the key, so it is not pruned. Rewrite it as a half-open range. - Mismatched types. If a comparison forces a cast of the key column rather than of the constant, pruning can be lost. Keep the constant in the key's type.
- Volatile functions.
random()or a function markedVOLATILEcannot be evaluated before rows are read, so no pruning happens. Stable and immutable functions are fine. - Filters on other columns. A condition on
device_iddoes nothing for a table partitioned byrecorded_at. - Hash partitioning with ranges. Hash partitions are pruned only by equality (or
IN) on the key. A range such asaccount_id > 1000must visit every hash partition.
OR conditions are handled: WHERE region = 'emea' OR region = 'amer' on a list-partitioned table keeps both matching partitions and prunes the rest. If one branch of the OR does not mention the key, the whole condition can no longer prune.
The default partition and pruning
A default partition accepts every value not covered elsewhere, so the planner treats its implicit bound as "everything else." For an equality filter on a value that belongs to a defined partition, the default partition is pruned. For a range that reaches beyond the defined bounds, such as recorded_at >= '2026-02-15' with no March partition, the default partition stays in the plan because matching rows might be stored there. A default partition that unexpectedly appears in plans is often a sign that rows have landed in it, so check it with SELECT count(*) FROM measurements_default.
A verification routine
Do this whenever you partition a table or add a query that should benefit from it:
- Run
EXPLAIN (COSTS OFF)with realistic literal values and count the children ofAppend. - If the query uses parameters, use
EXPLAIN (ANALYZE, COSTS OFF)on a generic plan and look forSubplans Removed. - Rewrite any condition that wraps the key in a function or cast into a plain comparison on the column.
- Check that surviving partitions use indexes where expected, since pruning does not replace indexing.
Exercise
A table is partitioned by RANGE (recordedat). EXPLAIN for `WHERE recordedat::date = '2026-02-05' lists every partition under Append`. Why, and how do you rewrite the query so that only the February partition is scanned?
Show answer
The cast recorded_at::date wraps the partition key in an expression, so the planner cannot compare the key with the partition bounds and does not prune. Rewrite it as a half-open range on the bare column: WHERE recorded_at >= '2026-02-05' AND recorded_at < '2026-02-06'. Then EXPLAIN shows only measurements_2026_02.
Exercise
In EXPLAIN (ANALYZE) output for a prepared statement, you see Subplans Removed: 3 under Append, and another plan has a child marked (never executed). What does each tell you, and in which situations does each appear?
Show answer
Subplans Removed: 3 means initial pruning at executor startup eliminated three partitions after parameter values or stable function results became known, such as $1 or now(). (never executed) means runtime pruning skipped that child for every row of the outer side of a join, which happens when the join column is the partition key and its value changes during execution.