Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Understanding Partition Pruning Behavior in PostgreSQL
📖 Scenario: You are managing a large sales database for a retail company. The sales data is partitioned by year to improve query performance. You want to learn how PostgreSQL's partition pruning works to efficiently query only relevant partitions.
🎯 Goal: Build a partitioned table for sales data by year, insert sample data, and write queries that demonstrate partition pruning behavior in PostgreSQL.
📋 What You'll Learn
Create a partitioned table named sales partitioned by range on the sale_year column.
Create partitions for years 2021, 2022, and 2023.
Insert sample sales data into each partition with exact values.
Write a query filtering sales for year 2022 to demonstrate partition pruning.
Write a query filtering sales for years 2021 and 2023 to demonstrate pruning multiple partitions.
💡 Why This Matters
🌍 Real World
Partitioning large tables by date or other keys is common in real-world databases to improve query speed and manageability.
💼 Career
Understanding partition pruning is important for database administrators and developers to optimize queries and maintain large datasets efficiently.
Progress0 / 4 steps
1
Create the partitioned table and partitions
Create a table called sales partitioned by range on the column sale_year. Then create three partitions named sales_2021, sales_2022, and sales_2023 for the year ranges 2021, 2022, and 2023 respectively.
PostgreSQL
Hint
Use PARTITION BY RANGE (sale_year) when creating the main table. Then create partitions for each year range using FOR VALUES FROM (...) TO (...).
2
Insert sample sales data into partitions
Insert the following sales data into the sales table: (1, 2021, 100.00), (2, 2021, 150.50), (3, 2022, 200.00), (4, 2022, 250.75), (5, 2023, 300.00). Use the exact sale_id, sale_year, and amount values as shown.
PostgreSQL
Hint
Use a single INSERT INTO sales (sale_id, sale_year, amount) VALUES (...), (...), ...; statement with the exact values.
3
Query sales for year 2022 to demonstrate partition pruning
Write a SELECT query to get all sales from the sales table where sale_year is 2022. Use the exact query: SELECT * FROM sales WHERE sale_year = 2022;
PostgreSQL
Hint
Write the exact query SELECT * FROM sales WHERE sale_year = 2022; to see partition pruning in action.
4
Query sales for years 2021 and 2023 to prune multiple partitions
Write a SELECT query to get all sales from the sales table where sale_year is either 2021 or 2023. Use the exact query: SELECT * FROM sales WHERE sale_year IN (2021, 2023);
PostgreSQL
Hint
Use the exact query SELECT * FROM sales WHERE sale_year IN (2021, 2023); to see pruning of multiple partitions.
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]