0
0
PostgreSQLquery~10 mins

Partition pruning behavior in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Partition pruning behavior
Query with WHERE clause
Planner analyzes query
Identify relevant partitions
Exclude irrelevant partitions
Execute query only on needed partitions
Combine results from partitions
Return final result
The query planner uses the WHERE clause to find which partitions to scan, skipping others to improve speed.
Execution Sample
PostgreSQL
CREATE TABLE sales (
  id INT,
  region TEXT,
  amount INT
) PARTITION BY LIST (region);

SELECT * FROM sales WHERE region = 'north';
This query only scans the 'north' partition, skipping others.
Execution Table
StepActionPartitions ConsideredPartitions PrunedPartitions ScannedResult
1Parse queryAll partitions: north, south, east, westNone yetNone yetQuery ready for planning
2Planner analyzes WHERE region = 'north'north, south, east, westsouth, east, westnorthOnly 'north' partition scanned
3Execute scan on 'north' partitionnorthsouth, east, westnorthRows from 'north' partition returned
4Combine resultsnorthsouth, east, westnorthFinal result set returned
5Endnorthsouth, east, westnorthQuery complete
💡 Query ends after scanning only the 'north' partition; others pruned due to WHERE clause
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Partitions Considerednorth, south, east, westnorth, south, east, westnorthnorth
Partitions Prunednonesouth, east, westsouth, east, westsouth, east, west
Partitions Scannednonenonenorthnorth
Key Moments - 2 Insights
Why are some partitions not scanned even though they exist?
Because the planner uses the WHERE clause to prune partitions that cannot contain matching rows, as shown in step 2 of the execution_table where south, east, and west are pruned.
Does partition pruning happen before or after scanning partitions?
Partition pruning happens before scanning. The planner decides which partitions to scan (step 2) and then scans only those (step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step are partitions south, east, and west pruned?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Check the 'Partitions Pruned' column in the execution_table rows.
According to variable_tracker, which partitions are scanned after step 3?
Anorth, south
Ball partitions
Cnorth only
Dnone
💡 Hint
Look at 'Partitions Scanned' after step 3 in variable_tracker.
If the WHERE clause was removed, how would the 'Partitions Pruned' column change at step 2?
AAll partitions pruned
BNo partitions would be pruned
COnly one partition pruned
DPartitions pruned after scanning
💡 Hint
Without a WHERE clause, the planner cannot exclude any partitions before scanning.
Concept Snapshot
Partition pruning lets PostgreSQL skip scanning partitions that can't match the query.
The planner uses WHERE conditions to decide which partitions to scan.
Only relevant partitions are scanned, improving query speed.
Pruning happens during query planning, before execution.
Without pruning, all partitions are scanned.
Full Transcript
Partition pruning behavior in PostgreSQL means the query planner looks at the query's WHERE clause to decide which partitions to scan. It excludes partitions that cannot have matching rows, so the query runs faster by scanning fewer partitions. For example, if a table is partitioned by region and the query filters for region = 'north', only the 'north' partition is scanned. The planner prunes other partitions like 'south', 'east', and 'west' before execution. This pruning happens during query planning, not after scanning. If no WHERE clause filters partitions, all partitions are scanned. This behavior helps improve performance by reducing unnecessary data reads.