Bird
Raised Fist0
PostgreSQLquery~20 mins

Partition pruning behavior in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
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.

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

  1. Step 1: Understand partition pruning concept

    Partition pruning means the database avoids scanning partitions that do not match the query filter.
  2. Step 2: Identify the main benefit

    This skipping reduces query time by focusing only on relevant partitions.
  3. Final Answer:

    To skip scanning partitions that cannot contain matching rows -> Option C
  4. Quick 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
A. WHERE region = 'US'
B. WHERE UPPER(region) = 'US'
C. WHERE LENGTH(region) > 2
D. WHERE region LIKE '%US%'

Solution

  1. Step 1: Identify pruning conditions

    Partition pruning works best with simple direct comparisons on the partition key.
  2. Step 2: Analyze each option

    WHERE region = 'US' uses a direct equality on region, enabling pruning. Options A, B, and D use functions or patterns, preventing pruning.
  3. Final Answer:

    WHERE region = 'US' -> Option A
  4. Quick 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
A. Only the 2021 partition
B. Both 2021 and 2022 partitions
C. No partitions, query returns empty
D. All partitions plus a full table scan

Solution

  1. Step 1: Understand partition pruning with equality filter

    The query filters on year = 2021, which matches exactly one partition.
  2. Step 2: Determine scanned partitions

    PostgreSQL will prune and scan only the 2021 partition, skipping 2022.
  3. Final Answer:

    Only the 2021 partition -> Option A
  4. Quick 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 orders partitioned by order_date:
SELECT * FROM orders WHERE EXTRACT(YEAR FROM order_date) = 2023;

Why might partition pruning NOT occur?
medium
A. Partition pruning only works with numeric columns
B. Using a function on the partition key disables pruning
C. The query syntax is invalid
D. Partition pruning requires an index on order_date

Solution

  1. Step 1: Identify pruning limitation

    Partition pruning requires direct use of the partition key in filters without wrapping functions.
  2. Step 2: Analyze the query

    The query uses EXTRACT(YEAR FROM order_date), a function on the partition key, preventing pruning.
  3. Final Answer:

    Using a function on the partition key disables pruning -> Option B
  4. Quick 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
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

  1. Step 1: Understand pruning with range partitions

    Range partitions work best with direct range conditions on the partition key.
  2. 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.
  3. Final Answer:

    SELECT * FROM events WHERE event_date >= '2023-03-01' AND event_date < '2023-04-01'; -> Option D
  4. 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