0
0
PostgreSQLquery~20 mins

Partition pruning behavior in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Partition Pruning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Partition pruning with static query filters
Given a partitioned table sales partitioned by region, which query will prune partitions correctly and return only rows from the north region?
PostgreSQL
SELECT * FROM sales WHERE region = 'north';
ASELECT * FROM sales WHERE region LIKE '%north%';
BSELECT * FROM sales WHERE region IN ('north', 'south');
CSELECT * FROM sales WHERE region = 'north';
DSELECT * FROM sales WHERE region <> 'south';
Attempts:
2 left
💡 Hint
Partition pruning works best with exact matches on the partition key.
🧠 Conceptual
intermediate
2:00remaining
Understanding partition pruning timing
When does PostgreSQL perform partition pruning during query execution?
AAt query planning time for static filters and at execution time for parameterized queries.
BOnly at query execution time regardless of query type.
COnly at query planning time for all queries.
DAfter the query finishes scanning all partitions.
Attempts:
2 left
💡 Hint
Think about how static values and parameters affect planning.
📝 Syntax
advanced
2:00remaining
Which query syntax enables effective partition pruning with a parameter?
Given a partitioned table orders partitioned by order_date, which query syntax will allow PostgreSQL to prune partitions when using a parameter for the date filter?
PostgreSQL
Prepare a query to select orders for a specific date using a parameter.
ASELECT * FROM orders WHERE order_date = $1;
BSELECT * FROM orders WHERE order_date::text = $1;
CSELECT * FROM orders WHERE EXTRACT(YEAR FROM order_date) = $1;
DSELECT * FROM orders WHERE order_date > $1 - INTERVAL '1 day';
Attempts:
2 left
💡 Hint
Partition pruning requires direct comparison on the partition key without transformations.
🔧 Debug
advanced
2:00remaining
Why does this query scan all partitions despite a filter?
A table events is partitioned by event_type. The query SELECT * FROM events WHERE event_type = 'click' OR event_type = 'view'; scans all partitions. Why?
ABecause the table is not actually partitioned.
BBecause the OR condition prevents pruning; only single equality filters prune partitions.
CBecause the query uses string literals instead of parameters.
DBecause the partition key is not indexed.
Attempts:
2 left
💡 Hint
Consider how multiple conditions affect pruning.
optimization
expert
2:00remaining
Optimizing partition pruning for range partitions
Given a table logs partitioned by range on log_date, which query will maximize partition pruning efficiency for retrieving logs from January 2024?
ASELECT * FROM logs WHERE log_date BETWEEN '2024-01-01' AND '2024-01-31';
BSELECT * FROM logs WHERE TO_CHAR(log_date, 'YYYY-MM') = '2024-01';
CSELECT * FROM logs WHERE EXTRACT(MONTH FROM log_date) = 1 AND EXTRACT(YEAR FROM log_date) = 2024;
DSELECT * FROM logs WHERE log_date >= '2024-01-01' AND log_date < '2024-02-01';
Attempts:
2 left
💡 Hint
Partition pruning works best with direct range comparisons on the partition key.