Partition pruning helps the database skip searching parts of data that are not needed. This makes queries faster and saves resources.
When you have a large table split into smaller parts by date, and you want to query only recent data.
When you want to improve query speed by avoiding scanning irrelevant partitions.
When you use partitioned tables and want the database to automatically ignore partitions that don't match your query conditions.
When you want to reduce disk I/O and CPU usage during data retrieval.
When you want to optimize queries on large datasets divided by categories like region or type.
Syntax
PostgreSQL
SELECT * FROM partitioned_table WHERE partition_key = value;
Partition pruning happens automatically when the query filters on the partition key.
It works best when the filter condition is simple and directly on the partition key.
Examples
This query prunes partitions by filtering on the sale_date column, which is the partition key.
PostgreSQL
SELECT * FROM sales WHERE sale_date = '2024-01-01';
This query prunes partitions covering January 2024 only, skipping others.
PostgreSQL
SELECT * FROM sales WHERE sale_date >= '2024-01-01'AND sale_date < '2024-02-01';
If region is a partition key, this query prunes partitions for the 'North' region only.
PostgreSQL
SELECT * FROM sales WHERE region = 'North';
Sample Program
This example creates a partitioned sales table by month. The query filters on sale_date to trigger partition pruning. The EXPLAIN shows which partitions are scanned.
PostgreSQL
CREATETABLE sales (
id SERIALPRIMARYKEY,
sale_date DATENOTNULL,
region TEXTNOTNULL,
amount NUMERIC
) PARTITION BY RANGE (sale_date);
CREATETABLE sales_2024_01 PARTITION OF sales
FOR VALUESFROM ('2024-01-01') TO ('2024-02-01');
CREATETABLE sales_2024_02 PARTITION OF sales
FOR VALUESFROM ('2024-02-01') TO ('2024-03-01');
INSERTINTO sales (sale_date, region, amount) VALUES
('2024-01-10', 'North', 100),
('2024-01-20', 'South', 150),
('2024-02-10', 'North', 200);
EXPLAIN SELECT * FROM sales WHERE sale_date = '2024-01-15';
OutputSuccess
Important Notes
Partition pruning only works if the query condition matches the partition key exactly or with a range.
Complex expressions or functions on the partition key may prevent pruning.
Pruning reduces query time by scanning fewer partitions, especially useful for large datasets.
Summary
Partition pruning skips unnecessary partitions to speed up queries.
It works automatically when filtering on the partition key.
Use simple conditions on partition keys for best pruning results.
Practice
(1/5)
1. What is the main purpose of partition pruning in PostgreSQL?
easy
A. To merge all partitions into one table
B. To create new partitions automatically
C. To skip scanning partitions that cannot contain matching rows
D. To backup partitions separately
Solution
Step 1: Understand partition pruning concept
Partition pruning means the database avoids scanning partitions that do not match the query filter.
Step 2: Identify the main benefit
This skipping reduces query time by focusing only on relevant partitions.
Final Answer:
To skip scanning partitions that cannot contain matching rows -> Option C
5. A table events is range partitioned by event_date with monthly partitions. You want to query events in March 2023. Which query will maximize partition pruning?
hard
A. SELECT * FROM events WHERE event_date BETWEEN '2023-02-28' AND '2023-03-31';
B. SELECT * FROM events WHERE EXTRACT(MONTH FROM event_date) = 3 AND EXTRACT(YEAR FROM event_date) = 2023;
C. SELECT * FROM events WHERE TO_CHAR(event_date, 'YYYY-MM') = '2023-03';
D. SELECT * FROM events WHERE event_date >= '2023-03-01' AND event_date < '2023-04-01';
Solution
Step 1: Understand pruning with range partitions
Range partitions work best with direct range conditions on the partition key.
Step 2: Evaluate each query
SELECT * FROM events WHERE event_date >= '2023-03-01' AND event_date < '2023-04-01'; uses a direct range filter on event_date, enabling pruning. Options B and C use functions, disabling pruning. SELECT * FROM events WHERE event_date BETWEEN '2023-02-28' AND '2023-03-31'; includes dates outside March, scanning extra partitions.
Final Answer:
SELECT * FROM events WHERE event_date >= '2023-03-01' AND event_date < '2023-04-01'; -> Option D
Quick Check:
Direct range filters maximize pruning [OK]
Hint: Use direct range filters on partition keys for pruning [OK]
Common Mistakes:
Using functions disables pruning
Including extra dates scans more partitions
Assuming BETWEEN always prunes perfectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.