Partition pruning helps the database skip searching parts of data that are not needed. This makes queries faster and saves resources.
Partition pruning behavior in PostgreSQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
PostgreSQL
SELECT * FROM sales WHERE sale_date = '2024-01-01';
PostgreSQL
SELECT * FROM sales WHERE sale_date >= '2024-01-01' AND sale_date < '2024-02-01';
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
CREATE TABLE sales ( id SERIAL PRIMARY KEY, sale_date DATE NOT NULL, region TEXT NOT NULL, amount NUMERIC ) PARTITION BY RANGE (sale_date); CREATE TABLE sales_2024_01 PARTITION OF sales FOR VALUES FROM ('2024-01-01') TO ('2024-02-01'); CREATE TABLE sales_2024_02 PARTITION OF sales FOR VALUES FROM ('2024-02-01') TO ('2024-03-01'); INSERT INTO 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';
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. What is the main purpose of partition pruning in PostgreSQL?
easy
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 CQuick Check:
Partition pruning = skip irrelevant partitions [OK]
Hint: Partition pruning skips irrelevant partitions to speed queries [OK]
Common Mistakes:
- Thinking pruning merges partitions
- Assuming pruning creates partitions
- Confusing pruning with backup
2. Which of the following WHERE clauses will enable partition pruning on a table partitioned by column
region?easy
Solution
Step 1: Identify pruning conditions
Partition pruning works best with simple direct comparisons on the partition key.Step 2: Analyze each option
WHERE region = 'US' uses a direct equality onregion, enabling pruning. Options A, B, and D use functions or patterns, preventing pruning.Final Answer:
WHERE region = 'US' -> Option AQuick Check:
Simple equality on partition key enables pruning [OK]
Hint: Use simple column = value filters on partition keys [OK]
Common Mistakes:
- Using functions on partition keys disables pruning
- Using LIKE patterns disables pruning
- Assuming any WHERE clause prunes partitions
3. Given a table
sales partitioned by year with partitions for 2021 and 2022, what will the query below scan?SELECT * FROM sales WHERE year = 2021;
medium
Solution
Step 1: Understand partition pruning with equality filter
The query filters onyear = 2021, which matches exactly one partition.Step 2: Determine scanned partitions
PostgreSQL will prune and scan only the 2021 partition, skipping 2022.Final Answer:
Only the 2021 partition -> Option AQuick Check:
Filter on partition key = value scans matching partition only [OK]
Hint: Equality on partition key scans only matching partition [OK]
Common Mistakes:
- Assuming all partitions scan regardless of filter
- Thinking query returns empty if filter matches a partition
- Believing full table scan always happens
4. You wrote this query on a partitioned table
Why might partition pruning NOT occur?
orders partitioned by order_date:SELECT * FROM orders WHERE EXTRACT(YEAR FROM order_date) = 2023;
Why might partition pruning NOT occur?
medium
Solution
Step 1: Identify pruning limitation
Partition pruning requires direct use of the partition key in filters without wrapping functions.Step 2: Analyze the query
The query usesEXTRACT(YEAR FROM order_date), a function on the partition key, preventing pruning.Final Answer:
Using a function on the partition key disables pruning -> Option BQuick Check:
Functions on partition keys disable pruning [OK]
Hint: Avoid functions on partition keys for pruning [OK]
Common Mistakes:
- Thinking pruning needs indexes
- Believing pruning works with any filter
- Assuming query syntax error causes pruning failure
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
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 onevent_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 DQuick 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
